Skip to content

Fix stubs for function pointers (closes #462) #565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions server/src/Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "printers/TestsPrinter.h"
#include "utils/KleeUtils.h"
#include "utils/StringUtils.h"
#include "utils/StubsUtils.h"

#include "loguru.h"

Expand All @@ -31,7 +32,9 @@ Tests::MethodDescription::MethodDescription()
{ Tests::ERROR_SUITE_NAME, std::vector<int>() }},
codeText{{ Tests::DEFAULT_SUITE_NAME, std::string() },
{ Tests::ERROR_SUITE_NAME, std::string() }},
modifiers{} { }
modifiers{} {
stubsStorage = std::make_shared<StubsStorage>();
}

static const std::unordered_map<std::string, std::string> FPSpecialValuesMappings = {
{"nan", "NAN"},
Expand Down Expand Up @@ -201,13 +204,13 @@ std::shared_ptr<ArrayValueView> KTestObjectParser::multiArrayView(const std::vec
std::shared_ptr<FunctionPointerView> KTestObjectParser::functionPointerView(const std::optional<std::string> &scopeName,
const std::string &methodName, const std::string &paramName) {
std::string value =
PrinterUtils::getFunctionPointerStubName(scopeName, methodName, paramName).substr(1);
StubsUtils::getFunctionPointerStubName(scopeName, methodName, paramName).substr(1);
return std::make_shared<FunctionPointerView>(value);
}

std::shared_ptr<FunctionPointerView> KTestObjectParser::functionPointerView(const std::string &structName,
const std::string &fieldName) {
std::string value = PrinterUtils::getFunctionPointerAsStructFieldStubName(structName, fieldName, false).substr(1);
std::string value = StubsUtils::getFunctionPointerAsStructFieldStubName(structName, fieldName, false).substr(1);
return std::make_shared<FunctionPointerView>(value);
}

Expand Down Expand Up @@ -677,14 +680,10 @@ types::Type KTestObjectParser::traverseLazyInStruct(std::vector<bool> &visited,
void KTestObjectParser::assignTypeStubVar(Tests::MethodTestCase &testCase,
const Tests::MethodDescription &methodDescription) {
for (auto const &obj : testCase.objects) {
if (StringUtils::endsWith(obj.name, PrinterUtils::KLEE_SYMBOLIC_SUFFIX)) {
std::string stubFuncName = obj.name.substr(0, obj.name.length() - PrinterUtils::KLEE_SYMBOLIC_SUFFIX.length());
if (!CollectionUtils::contains(methodDescription.functionPointers, stubFuncName)) {
std::string message = "Can't find function pointer with name " + stubFuncName;
LOG_S(WARNING) << message;
continue;
}
types::Type stubType = types::Type::createArray(methodDescription.functionPointers.at(stubFuncName)->returnType);
std::optional<std::shared_ptr<FunctionInfo>> maybeFunctionInfo =
methodDescription.stubsStorage->getFunctionPointerByKTestObjectName(obj.name);
if (maybeFunctionInfo.has_value()) {
types::Type stubType = types::Type::createArray(maybeFunctionInfo.value()->returnType);
std::shared_ptr<AbstractValueView> stubView = testParameterView({obj.name, obj.bytes}, {stubType, obj.name},
PointerUsage::PARAMETER, testCase.lazyAddressToName,
testCase.lazyReferences, methodDescription);
Expand Down
3 changes: 3 additions & 0 deletions server/src/Tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <tsl/ordered_map.h>
#include <tsl/ordered_set.h>
#include "Paths.h"
#include "stubs/StubsStorage.h"

#include <cassert>
#include <climits>
Expand Down Expand Up @@ -498,6 +499,8 @@ namespace tests {

typedef std::unordered_map<std::string, std::shared_ptr<types::FunctionInfo>> FPointerMap;
FPointerMap functionPointers;
std::shared_ptr<StubsStorage> stubsStorage;

std::vector<MethodTestCase> testCases;
typedef std::unordered_map<std::string, std::vector<int>> SuiteNameToTestCasesMap;
SuiteNameToTestCasesMap suiteTestCases;
Expand Down
3 changes: 2 additions & 1 deletion server/src/printers/KleeConstraintsPrinter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "KleeConstraintsPrinter.h"

#include "utils/PrinterUtils.h"
#include "utils/StubsUtils.h"
#include "exceptions/UnImplementedException.h"

#include "loguru.h"
Expand Down Expand Up @@ -125,7 +126,7 @@ void KleeConstraintsPrinter::genConstraintsForStruct(const ConstraintsState &sta
field.type,
isStruct ? state.endString : false,
state.depth + 1 };
std::string stubFunctionName = PrinterUtils::getFunctionPointerAsStructFieldStubName(curStruct.name, field.name);
std::string stubFunctionName = StubsUtils::getFunctionPointerAsStructFieldStubName(curStruct.name, field.name);
switch (typesHandler->getTypeKind(field.type)) {
case TypeKind::STRUCT_LIKE:
genConstraintsForStruct(newState);
Expand Down
3 changes: 2 additions & 1 deletion server/src/printers/KleePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "utils/CollectionUtils.h"
#include "utils/FileSystemUtils.h"
#include "utils/KleeUtils.h"
#include "utils/StubsUtils.h"
#include "visitors/KleeAssumeParamVisitor.h"
#include "visitors/KleeAssumeReturnValueVisitor.h"

Expand Down Expand Up @@ -396,7 +397,7 @@ void KleePrinter::genParamsDeclarations(
bool KleePrinter::genParamDeclaration(const Tests::MethodDescription &testMethod,
const Tests::MethodParam &param) {
std::string stubFunctionName =
PrinterUtils::getFunctionPointerStubName(testMethod.isClassMethod() ? std::make_optional(testMethod.classObj->name) : std::nullopt,
StubsUtils::getFunctionPointerStubName(testMethod.isClassMethod() ? std::make_optional(testMethod.classObj->name) : std::nullopt,
testMethod.name, param.name);
if (types::TypesHandler::isPointerToFunction(param.type)) {
strDeclareVar(getTypedefFunctionPointer(testMethod.name, param.name, false), param.name,
Expand Down
5 changes: 3 additions & 2 deletions server/src/printers/Printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "types/SimpleType.h"
#include "utils/ArgumentsUtils.h"
#include "utils/Copyright.h"
#include "utils/StubsUtils.h"
#include "visitors/VerboseParameterVisitor.h"
#include "printers/KleeConstraintsPrinter.h"

Expand Down Expand Up @@ -549,8 +550,8 @@ namespace printer {
std::string scopeName = (forKlee ? testMethod.getClassName().value_or("") : "");
std::string prefix = PrinterUtils::getKleePrefix(forKlee);
for (const auto &[name, pointerFunctionStub] : testMethod.functionPointers) {
std::string stubName = PrinterUtils::getFunctionPointerStubName(scopeName,
testMethod.name, name, true);
std::string stubName = StubsUtils::getFunctionPointerStubName(scopeName, testMethod.name, name, true);
testMethod.stubsStorage->registerFunctionPointerStub(testMethod.name, pointerFunctionStub);
writeStubForParam(typesHandler, pointerFunctionStub, testMethod.name, stubName, true,
forKlee);
}
Expand Down
41 changes: 24 additions & 17 deletions server/src/printers/TestsPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "visitors/VerboseAssertsReturnValueVisitor.h"
#include "visitors/VerboseParameterVisitor.h"
#include "utils/KleeUtils.h"
#include "utils/StubsUtils.h"

#include "loguru.h"

Expand Down Expand Up @@ -453,20 +454,27 @@ void TestsPrinter::verboseParameters(const Tests::MethodDescription &methodDescr
ss << NL;
}

if (!testCase.stubValuesTypes.empty()) {
strComment("Initialize symbolic stubs");
for (auto i = 0; i < testCase.stubValuesTypes.size(); i++) {
const auto &param = testCase.stubValuesTypes[i];
const auto &value = testCase.stubValues[i];
if (param.type.isTwoDimensionalPointer()) {
Tests::MethodParam valueParam{ param.type, param.underscoredName(), param.alignment };
verboseParameter(methodDescription, valueParam, value, true);
gen2DPointer(param, false);
} else {
verboseParameter(methodDescription, param, value, false);
std::vector<std::vector<tests::Tests::MethodParam>> types = { testCase.stubValuesTypes, testCase.stubParamTypes };
std::vector<std::vector<tests::Tests::TestCaseParamValue>> values = { testCase.stubParamValues, testCase.stubParamValues };

for (int j = 0; j < types.size(); j++) {
if (!types[j].empty()) {
if (j == 0) {
strComment("Initialize symbolic stubs");
}
for (auto i = 0; i < types[j].size(); i++) {
const auto &param = types[j][i];
const auto &value = values[j][i];
if (param.type.isTwoDimensionalPointer()) {
Tests::MethodParam valueParam{ param.type, param.underscoredName(), param.alignment };
verboseParameter(methodDescription, valueParam, value, true);
gen2DPointer(param, false);
} else {
verboseParameter(methodDescription, param, value, false);
}
}
ss << NL;
}
ss << NL;
}

if (!testCase.paramValues.empty()) {
Expand All @@ -489,7 +497,7 @@ void TestsPrinter::printFunctionParameters(const Tests::MethodDescription &metho
Tests::TestCaseParamValue value = testCase.paramValues[i];
Tests::MethodParam valueParam = getValueParam(param);
value.name = valueParam.name;
if (param.type.isLValueReference() || param.type.isSimple()) {
if (param.type.isLValueReference() || param.type.isSimple() || param.type.isPointerToFunction()) {
verboseParameter(methodDescription, valueParam, value, true);
}
}
Expand All @@ -500,9 +508,8 @@ void TestsPrinter::verboseParameter(const Tests::MethodDescription &method,
const Tests::MethodParam &param,
const Tests::TestCaseParamValue &value,
bool needDeclaration) {
std::string stubFunctionName =
PrinterUtils::getFunctionPointerStubName(method.getClassTypeName(),
method.name, param.name);
std::string stubFunctionName = StubsUtils::getFunctionPointerStubName(method.getClassTypeName(),
method.name, param.name);
if (types::TypesHandler::isPointerToFunction(param.type)) {
strDeclareVar(getTypedefFunctionPointer(method.name, param.name, false), param.name,
stubFunctionName);
Expand Down Expand Up @@ -660,7 +667,7 @@ void TestsPrinter::printPointerParameter(const Tests::MethodDescription &methodD
const auto &value = testCase.paramValues[param_num];
if (types::TypesHandler::isArrayOfPointersToFunction(param.type)) {
auto type = getTypedefFunctionPointer(methodDescription.name, param.name, false);
std::string stubName = PrinterUtils::getFunctionPointerStubName(
std::string stubName = StubsUtils::getFunctionPointerStubName(
methodDescription.getClassTypeName(), methodDescription.name, param.name);
strDeclareArrayOfFunctionPointerVar(type, param.name, stubName);
} else if (types::TypesHandler::isCStringType(param.type)) {
Expand Down
13 changes: 13 additions & 0 deletions server/src/stubs/StubsStorage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "StubsStorage.h"

#include <utility>
#include "utils/StubsUtils.h"

void StubsStorage::registerFunctionPointerStub(const std::string &methodName, std::shared_ptr<types::FunctionInfo> functionInfo) {
_functionPointers[StubsUtils::getStubSymbolicVarName(methodName + "_" + functionInfo->name)] = std::move(functionInfo);
}

std::optional<std::shared_ptr<types::FunctionInfo>>
StubsStorage::getFunctionPointerByKTestObjectName(const std::string &objectName) {
return CollectionUtils::getOptionalValue(_functionPointers, objectName);
}
19 changes: 19 additions & 0 deletions server/src/stubs/StubsStorage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef UTBOTCPP_STUBSSTORAGE_H
#define UTBOTCPP_STUBSSTORAGE_H

#include <string>
#include <unordered_map>
#include <memory>

#include "types/Types.h"

class StubsStorage {
public:
void registerFunctionPointerStub(const std::string &methodName, std::shared_ptr<types::FunctionInfo>);
std::optional<std::shared_ptr<types::FunctionInfo>> getFunctionPointerByKTestObjectName(const std::string &objectName);
private:
std::unordered_map<std::string, std::shared_ptr<types::FunctionInfo>> _functionPointers;
};


#endif //UTBOTCPP_STUBSSTORAGE_H
25 changes: 0 additions & 25 deletions server/src/utils/PrinterUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,31 +62,6 @@ namespace PrinterUtils {
return LAZYRENAME + std::to_string(cnt);
}

std::string getFunctionPointerStubName(const std::optional<std::string> &scopeName,
const std::string &methodName,
const std::string &paramName,
bool omitSuffix) {
std::string stubName = "*" + scopeName.value_or("") + "_" + methodName;
if (!omitSuffix) {
stubName += "_" + paramName + "_stub";
} else {
stubName = stubName.substr(1);
}
return stubName;
}

std::string getFunctionPointerAsStructFieldStubName(const std::string &structName,
const std::string &fieldName,
bool omitSuffix) {
std::string stubName = "*" + structName;
if (!omitSuffix) {
stubName += "_" + fieldName + "_stub";
} else {
stubName = stubName.substr(1);
}
return stubName;
}

std::string getKleePrefix(bool forKlee) {
return forKlee ? "klee_" : "";
}
Expand Down
9 changes: 0 additions & 9 deletions server/src/utils/PrinterUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,6 @@ namespace PrinterUtils {

std::string fillVarName(std::string const &temp, std::string const &varName);

std::string getFunctionPointerStubName(const std::optional<std::string> &scopeName,
const std::string &methodName,
const std::string &paramName,
bool omitSuffix = false);

std::string getFunctionPointerAsStructFieldStubName(const std::string &structName,
const std::string &fieldName,
bool omitSuffix = false);

std::string getKleePrefix(bool forKlee);

std::string wrapUserValue(const testsgen::ValidationType &type, const std::string &value);
Expand Down
33 changes: 33 additions & 0 deletions server/src/utils/StubsUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "StubsUtils.h"
#include "PrinterUtils.h"

namespace StubsUtils {
std::string getFunctionPointerStubName(const std::optional<std::string> &scopeName,
const std::string &methodName,
const std::string &paramName,
bool omitSuffix) {
std::string stubName = "*" + scopeName.value_or("") + "_" + methodName;
if (!omitSuffix) {
stubName += "_" + paramName + "_stub";
} else {
stubName = stubName.substr(1);
}
return stubName;
}

std::string getStubSymbolicVarName(const std::string &methodName) {
return methodName + PrinterUtils::KLEE_SYMBOLIC_SUFFIX;
}

std::string getFunctionPointerAsStructFieldStubName(const std::string &structName,
const std::string &fieldName,
bool omitSuffix) {
std::string stubName = "*" + structName;
if (!omitSuffix) {
stubName += "_" + fieldName + "_stub";
} else {
stubName = stubName.substr(1);
}
return stubName;
}
}
20 changes: 20 additions & 0 deletions server/src/utils/StubsUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef UTBOTCPP_STUBUTILS_H
#define UTBOTCPP_STUBUTILS_H

#include <string>
#include <optional>

namespace StubsUtils {
std::string getFunctionPointerStubName(const std::optional<std::string> &scopeName,
const std::string &methodName,
const std::string &paramName,
bool omitSuffix = false);

std::string getStubSymbolicVarName(const std::string &methodName);

std::string getFunctionPointerAsStructFieldStubName(const std::string &structName,
const std::string &fieldName,
bool omitSuffix = false);
}

#endif //UTBOTCPP_STUBUTILS_H
4 changes: 2 additions & 2 deletions server/src/visitors/FunctionPointerForStubsVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "printers/Printer.h"

#include <unordered_set>
#include "utils/StubsUtils.h"

namespace visitor {
FunctionPointerForStubsVisitor::FunctionPointerForStubsVisitor(const types::TypesHandler *typesHandler)
Expand Down Expand Up @@ -38,8 +39,7 @@ namespace visitor {
}
auto structInfo = typesHandler->getStructInfo(type);
for (const auto &[name, field] : structInfo.functionFields) {
auto stubName =
PrinterUtils::getFunctionPointerAsStructFieldStubName(structInfo.name, name, true);
auto stubName = StubsUtils::getFunctionPointerAsStructFieldStubName(structInfo.name, name, true);
printer.writeStubForParam(typesHandler, field, structInfo.name, stubName, false, true);
}
for (auto &field : structInfo.fields) {
Expand Down
Loading