Skip to content

Commit

Permalink
Changes for access private members
Browse files Browse the repository at this point in the history
  • Loading branch information
ladisgin committed Nov 25, 2021
1 parent 2b298ae commit 2304ef5
Show file tree
Hide file tree
Showing 38 changed files with 277 additions and 154 deletions.
3 changes: 3 additions & 0 deletions server/src/KleeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ KleeGenerator::getCompileCommandForKlee(const fs::path &hintPath,
"-fno-elide-constructors",
"-D" + PrinterUtils::KLEE_MODE + "=1",
SanitizerUtils::CLANG_SANITIZER_CHECKS_FLAG };
if(Paths::isCXXFile(srcFilePath)) {
command.addFlagToBegin(StringUtils::stringFormat("-I%s", Paths::getAccessPrivateLibPath()));
}
command.addFlagsToBegin(flags);
command.addFlagsToBegin(extraFlags);
command.addFlagToBegin(
Expand Down
2 changes: 1 addition & 1 deletion server/src/StmtBordersFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void StmtBordersFinder::run(const MatchFinder::MatchResult &Result) {
lineInfo.scopeName = nodeParent != nullptr ? nodeParent->getNameAsString() : path.stem().string();
lineInfo.methodName = FS->getNameAsString();
const clang::QualType realReturnType = FS->getReturnType().getCanonicalType();
lineInfo.functionReturnType = ParamsHandler::getType(realReturnType, realReturnType);
lineInfo.functionReturnType = ParamsHandler::getType(realReturnType, realReturnType, sourceManager);
lineInfo.initialized = true;

string strRepresentation = ASTPrinter::getSourceText(currentStmt->getSourceRange(), sourceManager);
Expand Down
4 changes: 4 additions & 0 deletions server/src/environment/EnvironmentPaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ namespace Paths {
return getUTBotRootDir() / "gtest";
}

fs::path getAccessPrivateLibPath() {
return getUTBotRootDir() / "access_private/include";
}

fs::path getLLVMprofdata() {
return getUTBotInstallDir() / "bin/llvm-profdata";
}
Expand Down
3 changes: 3 additions & 0 deletions server/src/environment/EnvironmentPaths.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ namespace Paths {

fs::path getGtestLibPath();

fs::path getAccessPrivateLibPath();

fs::path getLLVMprofdata();

fs::path getLLVMcov();

fs::path getLLVMgold();
Expand Down
13 changes: 8 additions & 5 deletions server/src/fetchers/FetcherUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
using std::shared_ptr;

types::Type ParamsHandler::getType(const clang::QualType &paramDef,
const clang::QualType &paramDecl) {
const clang::QualType &paramDecl,
const clang::SourceManager &sourceManager) {
const clang::QualType realParamType = paramDef.getCanonicalType();
const std::string usedParamTypeString =
paramDecl.getNonReferenceType().getUnqualifiedType().getAsString();
return types::Type(realParamType, usedParamTypeString);
paramDecl.getNonReferenceType().getUnqualifiedType().getAsString();
return types::Type(realParamType, usedParamTypeString, sourceManager);
}

std::shared_ptr<types::FunctionInfo>
Expand All @@ -26,17 +27,19 @@ ParamsHandler::getFunctionPointerDeclaration(const clang::FunctionType *fType,
auto functionParamDescription = std::make_shared<types::FunctionInfo>();
functionParamDescription->name = fName;
functionParamDescription->returnType = types::Type(fType->getReturnType().getCanonicalType(),
fType->getReturnType().getAsString());
fType->getReturnType().getAsString(),
mng);
int paramCounter = 0;
if (auto fProtoType = llvm::dyn_cast<clang::FunctionProtoType>(fType)) {
for (const auto &ftParam : fProtoType->getParamTypes()) {
functionParamDescription->params.push_back(
{ getType(ftParam, ftParam), "param" + std::to_string(++paramCounter) });
{ getType(ftParam, ftParam, mng), "param" + std::to_string(++paramCounter) });
}
}
functionParamDescription->isArray = isArray;
return functionParamDescription;
}

ClangToolRunner::ClangToolRunner(
std::shared_ptr<clang::tooling::CompilationDatabase> compilationDatabase)
: compilationDatabase(std::move(compilationDatabase)) {
Expand Down
5 changes: 3 additions & 2 deletions server/src/fetchers/FetcherUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ class ClangToolRunner {

class ParamsHandler {
public:
static types::Type getType(const clang::QualType& paramDef,
const clang::QualType& paramDecl);
static types::Type getType(const clang::QualType &paramDef,
const clang::QualType &paramDecl,
const clang::SourceManager &sourceManager);

static std::shared_ptr<types::FunctionInfo> getFunctionPointerDeclaration(
const clang::FunctionType* fType,
Expand Down
12 changes: 5 additions & 7 deletions server/src/fetchers/FunctionDeclsMatchCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ using namespace clang;
FunctionDeclsMatchCallback::FunctionDeclsMatchCallback(const Fetcher *parent,
bool onlyNames,
bool toResolveReturnTypes,
bool onlyReturnTypes,
utbot::Language srcLanguage)
bool onlyReturnTypes)
: parent(parent), typesResolver(parent), onlyNames(onlyNames),
toResolveReturnTypes(toResolveReturnTypes), onlyReturnTypes(onlyReturnTypes),
srcLanguage(srcLanguage) {
toResolveReturnTypes(toResolveReturnTypes), onlyReturnTypes(onlyReturnTypes) {
}

void FunctionDeclsMatchCallback::run(const MatchFinder::MatchResult &Result) {
Expand All @@ -41,7 +39,7 @@ void FunctionDeclsMatchCallback::run(const MatchFinder::MatchResult &Result) {
return;
}
const clang::QualType realReturnType = FS->getReturnType().getCanonicalType();
methodDescription.returnType = ParamsHandler::getType(realReturnType, realReturnType);
methodDescription.returnType = ParamsHandler::getType(realReturnType, realReturnType, sourceManager);
if (onlyReturnTypes) {
addMethod(sourceFilePath, methodDescription);
return;
Expand All @@ -64,7 +62,7 @@ void FunctionDeclsMatchCallback::run(const MatchFinder::MatchResult &Result) {
string className = nodeParent->getNameAsString();
methodDescription.className = className;
}
methodDescription.returnType = ParamsHandler::getType(realReturnType, realReturnType);
methodDescription.returnType = ParamsHandler::getType(realReturnType, realReturnType, sourceManager);
methodDescription.hasIncompleteReturnType = ClangUtils::isIncomplete(realReturnType);
if (toResolveReturnTypes) {
typesResolver.resolve(realReturnType);
Expand Down Expand Up @@ -101,7 +99,7 @@ void FunctionDeclsMatchCallback::run(const MatchFinder::MatchResult &Result) {
if (name == methodDescription.name) {
name = mangledName;
}
auto paramType = ParamsHandler::getType(defParam->getType(), declParam->getType());
auto paramType = ParamsHandler::getType(defParam->getType(), declParam->getType(), sourceManager);
addFunctionPointer(methodDescription.functionPointers, declParam->getFunctionType(),
declParam->getType(), name, sourceManager, paramType);
auto alignment = AlignmentFetcher::fetch(defParam);
Expand Down
4 changes: 1 addition & 3 deletions server/src/fetchers/FunctionDeclsMatchCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ class FunctionDeclsMatchCallback : public clang::ast_matchers::MatchFinder::Matc
explicit FunctionDeclsMatchCallback(const Fetcher *parent,
bool onlyNames,
bool toResolveReturnTypes,
bool onlyReturnTypes,
utbot::Language srcLanguage = utbot::Language::C);
bool onlyReturnTypes);

void run(const MatchFinder::MatchResult &Result) override;

Expand All @@ -30,7 +29,6 @@ class FunctionDeclsMatchCallback : public clang::ast_matchers::MatchFinder::Matc
bool onlyNames;
bool toResolveReturnTypes;
bool onlyReturnTypes;
const utbot::Language srcLanguage;

using MethodsSet =
std::unordered_set<tests::Tests::MethodDescription, tests::Tests::MethodDescriptionHash>;
Expand Down
2 changes: 1 addition & 1 deletion server/src/fetchers/GlobalVariableUsageMatchCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void GlobalVariableUsageMatchCallback::handleUsage(const clang::FunctionDecl *fu
auto &method = methods[usage.functionName];
const clang::QualType realParamType = varDecl->getType().getCanonicalType();
const std::string usedParamTypeString = varDecl->getType().getAsString();
types::Type paramType(realParamType, usedParamTypeString);
types::Type paramType = types::Type(realParamType, usedParamTypeString, sourceManager);
method.globalParams.emplace_back(paramType, usage.variableName, AlignmentFetcher::fetch(varDecl));
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/fetchers/TypeDeclsMatchCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void TypeDeclsMatchCallback::checkStruct(const MatchFinder::MatchResult &Result)
typesResolver.resolveStruct(ST, name);
}

if (const auto *ST = Result.Nodes.getNodeAs<RecordDecl>(INNER_TYPEDEF_CLASS_DECL)) {
if (const auto *ST = Result.Nodes.getNodeAs<CXXRecordDecl>(INNER_TYPEDEF_CLASS_DECL)) {
string name = ST->getNameAsString();
if (name.empty()) {
if (const auto *TD = Result.Nodes.getNodeAs<TypedefDecl>(TYPEDEF_CLASS_DECL)) {
Expand All @@ -53,7 +53,7 @@ void TypeDeclsMatchCallback::checkStruct(const MatchFinder::MatchResult &Result)
}
}

if (const auto *ST = Result.Nodes.getNodeAs<RecordDecl>(CLASS_DECL)) {
if (const auto *ST = Result.Nodes.getNodeAs<CXXRecordDecl>(CLASS_DECL)) {
string name = ST->getNameAsString();
typesResolver.resolveStruct(ST, name);
}
Expand Down
30 changes: 10 additions & 20 deletions server/src/printers/KleeConstraintsPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
#include "utils/PrinterUtils.h"
#include "exceptions/UnImplementedException.h"

#define KLEE_PREFER_CEX "klee_prefer_cex"
#define KLEE_ASSUME "klee_assume"
#define ASSIGN " = "
#define EQ " == "


using namespace types;
using printer::KleeConstraintsPrinter;

Expand Down Expand Up @@ -52,7 +46,7 @@ KleeConstraintsPrinter::genConstraints(const Tests::MethodParam &param, const st
void KleeConstraintsPrinter::genConstraintsForPrimitive(const ConstraintsState &state) {
const auto &cons = cexConstraints(state.curElement, state.curType);
if (!cons.empty()) {
strFunctionCall(KLEE_PREFER_CEX, { state.paramName, cons });
strFunctionCall(PrinterUtils::KLEE_PREFER_CEX, { state.paramName, cons });
} else {
ss << TAB_N() << "// No constraints for " << state.curElement << NL;
}
Expand All @@ -63,12 +57,12 @@ void KleeConstraintsPrinter::genConstraintsForEnum(const ConstraintsState &state

std::stringstream _ss;
for (auto it = enumInfo.namesToEntries.begin(); it != enumInfo.namesToEntries.end(); ++it) {
_ss << state.curElement << EQ << it->second.name;
_ss << state.curElement << PrinterUtils::EQ_OPERATOR << it->second.name;
if (std::next(it) != enumInfo.namesToEntries.end()) {
_ss << " | ";
}
}
strFunctionCall(KLEE_ASSUME, { _ss.str() });
strFunctionCall(PrinterUtils::KLEE_ASSUME, { _ss.str() });
}

void KleeConstraintsPrinter::genConstraintsForUnion(const ConstraintsState &state) {
Expand All @@ -77,7 +71,7 @@ void KleeConstraintsPrinter::genConstraintsForUnion(const ConstraintsState &stat
for (const auto &field : curUnion.fields) {
std::string errorMessage = "Unrecognized field of type '" + field.type.typeName() +
"' in union '" + curUnion.name + "'.";
auto access = PrinterUtils::getFieldAccess(state.curElement, field.name);
auto access = PrinterUtils::getFieldAccess(state.curElement, field);
ConstraintsState newState = { state.paramName, access, field.type, false, state.depth + 1 };
switch (typesHandler->getTypeKind(field.type)) {
case TypeKind::PRIMITIVE:
Expand Down Expand Up @@ -123,24 +117,20 @@ void KleeConstraintsPrinter::genConstraintsForMultiPointerOrArray(const Constrai
TypesHandler::isCStringType(state.curType)) {
std::vector<string> charSizes(indexes.begin(), indexes.end() - 1);
const auto charElement = constrMultiIndex(state.curElement, charSizes);
ss << TAB_N() << "if (" << indexes.back() << EQ << sizes.back() - 1 << ")" << LB();
ss << TAB_N() << charElement << "[" << sizes.back() - 1 << "]" << ASSIGN << "'\\0'" << SCNL;
ss << TAB_N() << "if (" << indexes.back() << PrinterUtils::EQ_OPERATOR << sizes.back() - 1 << ")" << LB();
ss << TAB_N() << charElement << "[" << sizes.back() - 1 << "]" << PrinterUtils::ASSIGN_OPERATOR << "'\\0'" << SCNL;
ss << TAB_N() << "break" << SCNL;
ss << RB();
}

ConstraintsState newState;
ConstraintsState newState = { state.paramName, element, baseType };
if (assignPointersToNull) {
newState = { state.paramName, element, baseType };
genConstraintsForPointerInStruct(newState);
} else if (typesHandler->isStruct(baseType)) {
newState = { state.paramName, element, baseType };
genConstraintsForStruct(newState);
} else if (typesHandler->isEnum(baseType)) {
newState = { state.paramName, element, baseType };
genConstraintsForEnum(newState);
} else if (typesHandler->isUnion(baseType)) {
newState = { state.paramName, element, baseType };
genConstraintsForUnion(newState);
} else {
newState = { state.paramName, element, baseType };
Expand All @@ -151,7 +141,7 @@ void KleeConstraintsPrinter::genConstraintsForMultiPointerOrArray(const Constrai
}

void KleeConstraintsPrinter::genConstraintsForPointerInStruct(const ConstraintsState &state) {
strFunctionCall(KLEE_ASSUME, { state.curElement + EQ + PrinterUtils::C_NULL });
strFunctionCall(PrinterUtils::KLEE_ASSUME, { state.curElement + PrinterUtils::EQ_OPERATOR + PrinterUtils::C_NULL });
}

void KleeConstraintsPrinter::genConstraintsForStruct(const ConstraintsState &state) {
Expand All @@ -160,7 +150,7 @@ void KleeConstraintsPrinter::genConstraintsForStruct(const ConstraintsState &sta
for (const auto &field : curStruct.fields) {
std::string errorMessage = "Unrecognized field of type '" + field.type.typeName() +
"' in struct '" + curStruct.name + "'.";
auto access = PrinterUtils::getFieldAccess(state.curElement, field.name);
auto access = PrinterUtils::getFieldAccess(state.curElement, field);
ConstraintsState newState = { state.paramName, access, field.type, state.endString, state.depth + 1 };
TypeKind kind = typesHandler->getTypeKind(field.type);
string stubFunctionName = PrinterUtils::getFunctionPointerAsStructFieldStubName(curStruct.name, field.name);
Expand Down Expand Up @@ -215,7 +205,7 @@ string KleeConstraintsPrinter::cexConstraints(const string &name, const types::T

void printer::KleeConstraintsPrinter::genConstraintsForPointerInUnion(
const ConstraintsState &state) {
strFunctionCall(KLEE_ASSUME, { state.curElement + EQ + PrinterUtils::C_NULL });
strFunctionCall(PrinterUtils::KLEE_ASSUME, { state.curElement + PrinterUtils::EQ_OPERATOR + PrinterUtils::C_NULL });
}

utbot::Language printer::KleeConstraintsPrinter::getLanguage() const {
Expand Down
39 changes: 19 additions & 20 deletions server/src/printers/KleePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@

#include <unordered_set>

#define TAB " "

#define KLEE_PREFER_CEX "klee_prefer_cex"
#define KLEE_ASSUME "klee_assume"
#define KLEE_PATH_FLAG "kleePathFlag"
#define KLEE_PATH_FLAG_SYMBOLIC "kleePathFlagSymbolic"
#define EQ " == "
#define ASSIGN " = "

using namespace types;
using printer::KleePrinter;

Expand Down Expand Up @@ -81,13 +72,16 @@ fs::path KleePrinter::writeTmpKleeFile(
bool predicate = predicateInfo.has_value();
if (!onlyForOneEntity && !testedMethod.empty() && !predicate) {
strInclude(KLEE_GLOBAL_VAR_H);
strDeclareVar("int", KLEE_PATH_FLAG, "0");
strDeclareVar("int", PrinterUtils::KLEE_PATH_FLAG, "0");
}

strInclude("klee/klee.h");
strInclude("stdlib.h", true);
ss << NL;
writeStubsForStructureFields(tests);

writePrivateAccessMacros(typesHandler, tests);

for (const auto &[methodName, testMethod] : tests.methods) {
if (!methodFilter(testMethod)) {
continue;
Expand Down Expand Up @@ -211,7 +205,7 @@ string KleePrinter::addTestLineFlag(const std::shared_ptr<LineInfo> &lineInfo,
ss << BNL;
}
if (!needAssertion) {
ss << KLEE_PATH_FLAG << " = 1" << SCNL;
ss << PrinterUtils::KLEE_PATH_FLAG << " = 1" << SCNL;
}
}
if (lineCounter == lineInfo->begin + lineInfo->insertAfter + 1) {
Expand All @@ -222,7 +216,7 @@ string KleePrinter::addTestLineFlag(const std::shared_ptr<LineInfo> &lineInfo,
if (lineCounter == lineInfo->begin) {
if (needAssertion) {
ss << "#pragma push_macro(\"assert\")\n";
ss << "#define assert(expr) if (!(expr)) {" << KLEE_PATH_FLAG << " = 1;}" << NL;
ss << "#define assert(expr) if (!(expr)) {" << PrinterUtils::KLEE_PATH_FLAG << " = 1;}" << NL;
}
}
ss << currentLine << NL;
Expand All @@ -236,7 +230,7 @@ string KleePrinter::addTestLineFlag(const std::shared_ptr<LineInfo> &lineInfo,
fs::path flagFileFolder = Paths::getFlagsDir(projectContext);

fs::path globalFlagFilePath = flagFileFolder / KLEE_GLOBAL_VAR_H;
FileSystemUtils::writeToFile(globalFlagFilePath, StringUtils::stringFormat("extern int %s;", KLEE_PATH_FLAG));
FileSystemUtils::writeToFile(globalFlagFilePath, StringUtils::stringFormat("extern int %s;", PrinterUtils::KLEE_PATH_FLAG));

fs::path flagFilePath = flagFileFolder / lineInfo->filePath.filename();
FileSystemUtils::writeToFile(flagFilePath, ss.str());
Expand Down Expand Up @@ -383,9 +377,9 @@ void KleePrinter::genParamsKleeAssumes(
bool onlyForOneEntity) {
visitor::KleeAssumeReturnValueVisitor(typesHandler, this).visit(testMethod, predicateInfo);
if (!onlyForOneEntity && !testedMethod.empty() && !predicateInfo.has_value()) {
string assumption = concat("(", KLEE_PATH_FLAG, EQ, KLEE_PATH_FLAG_SYMBOLIC, ") & (",
KLEE_PATH_FLAG_SYMBOLIC, EQ, "1)");
strFunctionCall(KLEE_ASSUME, { assumption });
string assumption = concat("(", PrinterUtils::KLEE_PATH_FLAG, PrinterUtils::EQ_OPERATOR, PrinterUtils::KLEE_PATH_FLAG_SYMBOLIC, ") & (",
PrinterUtils::KLEE_PATH_FLAG_SYMBOLIC, PrinterUtils::EQ_OPERATOR, "1)");
strFunctionCall(PrinterUtils::KLEE_ASSUME, { assumption });
}
}

Expand All @@ -402,8 +396,8 @@ void KleePrinter::genKleePathSymbolicIfNeeded(
const string &testedMethod,
bool onlyForOneEntity) {
if (!predicateInfo.has_value() && !onlyForOneEntity && !testedMethod.empty()) {
strDeclareVar("int", KLEE_PATH_FLAG_SYMBOLIC);
strKleeMakeSymbolic(KLEE_PATH_FLAG_SYMBOLIC, true);
strDeclareVar("int", PrinterUtils::KLEE_PATH_FLAG_SYMBOLIC);
strKleeMakeSymbolic(PrinterUtils::KLEE_PATH_FLAG_SYMBOLIC, true);
}
}

Expand Down Expand Up @@ -449,8 +443,13 @@ void KleePrinter::genKleePathSymbolicAssumeIfNeeded(const std::optional<PredInfo
const string &testedMethod,
bool onlyForOneEntity) {
if (!onlyForOneEntity && !testedMethod.empty() && !predicateInfo.has_value()) {
strFunctionCall(KLEE_ASSUME, { concat("(", KLEE_PATH_FLAG, EQ, KLEE_PATH_FLAG_SYMBOLIC,
") & (", KLEE_PATH_FLAG_SYMBOLIC, EQ, "1)") });
strFunctionCall(PrinterUtils::KLEE_ASSUME, { concat("(", PrinterUtils::KLEE_PATH_FLAG,
PrinterUtils::EQ_OPERATOR,
PrinterUtils::KLEE_PATH_FLAG_SYMBOLIC,
") & (",
PrinterUtils::KLEE_PATH_FLAG_SYMBOLIC,
PrinterUtils::EQ_OPERATOR,
"1)") });
}
}

Expand Down
Loading

0 comments on commit 2304ef5

Please sign in to comment.