Skip to content

#318 Remove code duplicate and fix the procedure for writing statistic #319

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 2 commits into from
Jul 11, 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
237 changes: 94 additions & 143 deletions server/src/KleeRunner.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#include "KleeRunner.h"

#include "Paths.h"
#include "TimeExecStatistics.h"
#include "exceptions/FileNotPresentedInArtifactException.h"
#include "exceptions/FileNotPresentedInCommandsException.h"
#include "tasks/RunKleeTask.h"
#include "utils/ExecUtils.h"
#include "utils/FileSystemUtils.h"
#include "utils/KleeUtils.h"
#include "utils/LogUtils.h"
#include "TimeExecStatistics.h"

#include "loguru.h"

#include <fstream>
#include <utility>

using namespace tests;
Expand Down Expand Up @@ -92,13 +93,6 @@ void KleeRunner::runKlee(const std::vector<tests::TestMethod> &testMethods,
std::move(writeFunctor));
}

fs::path KleeRunner::getKleeMethodOutFile(const TestMethod &method) {
fs::path kleeOutDir = Paths::getKleeOutDir(projectTmpPath);
fs::path relative =
Paths::removeExtension(fs::relative(method.sourceFilePath, projectContext.projectPath));
return kleeOutDir / relative / ("klee_out_" + method.methodName);
}

namespace {
void clearUnusedData(const fs::path &kleeDir) {
fs::remove(kleeDir / "assembly.ll");
Expand All @@ -119,56 +113,10 @@ namespace {
}
}

void KleeRunner::processBatchWithoutInteractive(MethodKtests &ktestChunk,
const TestMethod &testMethod,
Tests &tests) {
if (!tests.isFilePresentedInArtifact) {
return;
}
if (testMethod.sourceFilePath != tests.sourceFilePath) {
std::string message = StringUtils::stringFormat(
"While generating tests for source file: %s tried to generate tests for method %s "
"from another source file: %s. This can cause invalid generation.\n",
tests.sourceFilePath, testMethod.methodName, testMethod.sourceFilePath);
LOG_S(WARNING) << message;
}

std::string entryPoint = KleeUtils::entryPointFunction(tests, testMethod.methodName, true);
std::string entryPointFlag = StringUtils::stringFormat("--entry-point=%s", entryPoint);
auto kleeOut = getKleeMethodOutFile(testMethod);
fs::create_directories(kleeOut.parent_path());
std::string outputDir = "--output-dir=" + kleeOut.string();
std::vector<std::string> argvData = { "klee",
entryPointFlag,
"--libc=klee",
"--posix-runtime",
"--fp-runtime",
"--only-output-states-covering-new",
"--allocate-determ",
"--external-calls=all",
"--timer-interval=1000ms",
"--bcov-check-interval=6s",
"-istats-write-interval=5s",
"--disable-verify",
"--check-div-zero=false",
"--check-overshift=false",
"--skip-not-lazy-and-symbolic-pointers",
outputDir };
if (settingsContext.useDeterministicSearcher) {
argvData.emplace_back("--search=dfs");
}
argvData.push_back(testMethod.bitcodeFilePath);
argvData.emplace_back("--sym-stdin");
argvData.emplace_back(std::to_string(types::Type::symStdinSize));
std::vector<char *> cargv, cenvp;
std::vector<std::string> tmp;
ExecUtils::toCArgumentsPtr(argvData, tmp, cargv, cenvp, false);
LOG_S(DEBUG) << "Klee command :: " + StringUtils::joinWith(argvData, " ");
MEASURE_FUNCTION_EXECUTION_TIME
RunKleeTask task(cargv.size(), cargv.data(), settingsContext.timeoutPerFunction);
ExecUtils::ExecutionResult result __attribute__((unused)) = task.run();
ExecUtils::throwIfCancelled();

static void processMethod(MethodKtests &ktestChunk,
tests::Tests &tests,
const fs::path &kleeOut,
const tests::TestMethod &method) {
if (fs::exists(kleeOut)) {
clearUnusedData(kleeOut);
bool hasTimeout = false;
Expand Down Expand Up @@ -198,32 +146,90 @@ void KleeRunner::processBatchWithoutInteractive(MethodKtests &ktestChunk,
return UTBotKTestObject{ kTestObject };
});

ktestChunk[testMethod].emplace_back(objects, status);
ktestChunk[method].emplace_back(objects, status);
}
}
}
if (hasTimeout) {
std::string message = StringUtils::stringFormat(
"Some tests for function '%s' were skipped, as execution of function is "
"out of timeout.",
testMethod.methodName);
method.methodName);
tests.commentBlocks.emplace_back(std::move(message));
}
if (hasError) {
std::string message = StringUtils::stringFormat(
"Some tests for function '%s' were skipped, as execution of function leads "
"KLEE to the internal error. See console log for more details.",
testMethod.methodName);
method.methodName);
tests.commentBlocks.emplace_back(std::move(message));
}

writeKleeStats(kleeOut);

if (!CollectionUtils::containsKey(ktestChunk, method) || ktestChunk.at(method).empty()) {
tests.commentBlocks.emplace_back(StringUtils::stringFormat(
"Tests for %s were not generated. Maybe the function is too complex.",
method.methodName));
}
}
}

if (!CollectionUtils::containsKey(ktestChunk, testMethod) ||
ktestChunk.at(testMethod).empty()) {
tests.commentBlocks.emplace_back(StringUtils::stringFormat(
"Tests for %s were not generated. Maybe the function is too complex.",
testMethod.methodName));
void KleeRunner::processBatchWithoutInteractive(MethodKtests &ktestChunk,
const TestMethod &testMethod,
Tests &tests) {
if (!tests.isFilePresentedInArtifact) {
return;
}
if (testMethod.sourceFilePath != tests.sourceFilePath) {
std::string message = StringUtils::stringFormat(
"While generating tests for source file: %s tried to generate tests for method %s "
"from another source file: %s. This can cause invalid generation.\n",
tests.sourceFilePath, testMethod.methodName, testMethod.sourceFilePath);
LOG_S(WARNING) << message;
}

std::string entryPoint = KleeUtils::entryPointFunction(tests, testMethod.methodName, true);
std::string entryPointFlag = StringUtils::stringFormat("--entry-point=%s", entryPoint);
auto kleeOut = Paths::kleeOutDirForEntrypoints(projectContext, projectTmpPath, testMethod.sourceFilePath,
testMethod.methodName);
fs::create_directories(kleeOut.parent_path());
std::string outputDir = "--output-dir=" + kleeOut.string();
std::vector<std::string> argvData = { "klee",
entryPointFlag,
"--libc=klee",
"--posix-runtime",
"--fp-runtime",
"--only-output-states-covering-new",
"--allocate-determ",
"--external-calls=all",
"--timer-interval=1000ms",
"--bcov-check-interval=6s",
"-istats-write-interval=5s",
"--disable-verify",
"--check-div-zero=false",
"--check-overshift=false",
"--skip-not-lazy-and-symbolic-pointers",
outputDir };
if (settingsContext.useDeterministicSearcher) {
argvData.emplace_back("--search=dfs");
}
argvData.push_back(testMethod.bitcodeFilePath);
argvData.emplace_back("--sym-stdin");
argvData.emplace_back(std::to_string(types::Type::symStdinSize));

{
std::vector<char *> cargv, cenvp;
std::vector<std::string> tmp;
ExecUtils::toCArgumentsPtr(argvData, tmp, cargv, cenvp, false);
LOG_S(DEBUG) << "Klee command :: " + StringUtils::joinWith(argvData, " ");
MEASURE_FUNCTION_EXECUTION_TIME

RunKleeTask task(cargv.size(), cargv.data(), settingsContext.timeoutPerFunction);
ExecUtils::ExecutionResult result __attribute__((unused)) = task.run();
ExecUtils::throwIfCancelled();

processMethod(ktestChunk, tests, kleeOut, testMethod);
}
}

Expand All @@ -238,7 +244,7 @@ void KleeRunner::processBatchWithInteractive(const std::vector<tests::TestMethod
if (method.sourceFilePath != tests.sourceFilePath) {
std::string message = StringUtils::stringFormat(
"While generating tests for source file: %s tried to generate tests for method %s "
"from another source file: %s. This can cause invalid generation.\n",
"from another source file: %s. This can cause invalid generation.\n",
tests.sourceFilePath, method.methodName, method.sourceFilePath);
LOG_S(WARNING) << message;
}
Expand All @@ -247,7 +253,7 @@ void KleeRunner::processBatchWithInteractive(const std::vector<tests::TestMethod
TestMethod testMethod = testMethods[0];
std::string entryPoint = KleeUtils::entryPointFunction(tests, testMethod.methodName, true);
std::string entryPointFlag = StringUtils::stringFormat("--entry-point=%s", entryPoint);
auto kleeOut = getKleeMethodOutFile(testMethod);
auto kleeOut = Paths::kleeOutDirForEntrypoints(projectContext, projectTmpPath, tests.sourceFilePath);
fs::create_directories(kleeOut.parent_path());

fs::path entrypoints = kleeOut.parent_path() / "entrypoints.txt";
Expand Down Expand Up @@ -287,86 +293,31 @@ void KleeRunner::processBatchWithInteractive(const std::vector<tests::TestMethod
argvData.push_back(testMethod.bitcodeFilePath);
argvData.emplace_back("--sym-stdin");
argvData.emplace_back(std::to_string(types::Type::symStdinSize));
std::vector<char *> cargv, cenvp;
std::vector<std::string> tmp;
ExecUtils::toCArgumentsPtr(argvData, tmp, cargv, cenvp, false);

LOG_S(DEBUG) << "Klee command :: " + StringUtils::joinWith(argvData, " ");
MEASURE_FUNCTION_EXECUTION_TIME
if (settingsContext.timeoutPerFunction.has_value()) {
RunKleeTask task(cargv.size(), cargv.data(), settingsContext.timeoutPerFunction.value() * testMethods.size());
ExecUtils::ExecutionResult result __attribute__((unused)) = task.run();
} else {
RunKleeTask task(cargv.size(), cargv.data(), settingsContext.timeoutPerFunction);
ExecUtils::ExecutionResult result __attribute__((unused)) = task.run();
}

ExecUtils::throwIfCancelled();

for (const auto &method : testMethods) {
std::string kleeMethodName = KleeUtils::entryPointFunction(tests, method.methodName, true);
fs::path newKleeOut = kleeOut / kleeMethodName;
MethodKtests ktestChunk;
if (fs::exists(newKleeOut)) {
clearUnusedData(newKleeOut);
bool hasTimeout = false;
bool hasError = false;
for (auto const &entry : fs::directory_iterator(newKleeOut)) {
auto const &path = entry.path();
if (Paths::isKtestJson(path)) {
if (Paths::hasEarly(path)) {
hasTimeout = true;
} else if (Paths::hasInternalError(path)) {
hasError = true;
} else {
std::unique_ptr<TestCase, decltype(&TestCase_free)> ktestData{
TC_fromFile(path.c_str()), TestCase_free
};
if (ktestData == nullptr) {
LOG_S(WARNING) << "Unable to open .ktestjson file";
continue;
}
UTBotKTest::Status status = Paths::hasError(path)
? UTBotKTest::Status::FAILED
: UTBotKTest::Status::SUCCESS;
std::vector<ConcretizedObject> kTestObjects(
ktestData->objects, ktestData->objects + ktestData->n_objects);
{
std::vector<char *> cargv, cenvp;
std::vector<std::string> tmp;
ExecUtils::toCArgumentsPtr(argvData, tmp, cargv, cenvp, false);

std::vector<UTBotKTestObject> objects = CollectionUtils::transform(
kTestObjects, [](const ConcretizedObject &kTestObject) {
return UTBotKTestObject{ kTestObject };
});
LOG_S(DEBUG) << "Klee command :: " + StringUtils::joinWith(argvData, " ");
MEASURE_FUNCTION_EXECUTION_TIME

ktestChunk[method].emplace_back(objects, status);
}
}
}
if (hasTimeout) {
std::string message = StringUtils::stringFormat(
"Some tests for function '%s' were skipped, as execution of function is "
"out of timeout.",
method.methodName);
tests.commentBlocks.emplace_back(std::move(message));
}
if (hasError) {
std::string message = StringUtils::stringFormat(
"Some tests for function '%s' were skipped, as execution of function leads "
"KLEE to the internal error. See console log for more details.",
method.methodName);
tests.commentBlocks.emplace_back(std::move(message));
}
}
RunKleeTask task(cargv.size(),
cargv.data(),
settingsContext.timeoutPerFunction.has_value()
? settingsContext.timeoutPerFunction.value() * testMethods.size()
: settingsContext.timeoutPerFunction);
ExecUtils::ExecutionResult result __attribute__((unused)) = task.run();

if (fs::exists(kleeOut)) {
writeKleeStats(kleeOut);
}
ExecUtils::throwIfCancelled();

if (!CollectionUtils::containsKey(ktestChunk, method) ||
ktestChunk.at(method).empty()) {
tests.commentBlocks.emplace_back(StringUtils::stringFormat(
"Tests for %s were not generated. Maybe the function is too complex.",
method.methodName));
for (const auto &method : testMethods) {
std::string kleeMethodName =
KleeUtils::entryPointFunction(tests, method.methodName, true);
fs::path newKleeOut = kleeOut / kleeMethodName;
MethodKtests ktestChunk;
processMethod(ktestChunk, tests, newKleeOut, method);
ktests.push_back(ktestChunk);
}
ktests.push_back(ktestChunk);
}
}
1 change: 0 additions & 1 deletion server/src/KleeRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class KleeRunner {
void processBatchWithInteractive(const std::vector<tests::TestMethod> &testMethods,
tests::Tests &tests,
std::vector<tests::MethodKtests> &ktests);
fs::path getKleeMethodOutFile(const tests::TestMethod &method);
};


Expand Down
46 changes: 11 additions & 35 deletions server/src/Paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ namespace Paths {
[&path](auto const &suffix) { return errorFileExists(path, suffix); });
}

fs::path kleeOutDirForEntrypoints(const utbot::ProjectContext &projectContext, const fs::path &projectTmpPath,
const fs::path &srcFilePath, const std::string &methodName) {
fs::path kleeOutDir = getKleeOutDir(projectTmpPath);
fs::path relative = (fs::relative(addOrigExtensionAsSuffixAndAddNew(srcFilePath, ""),
projectContext.projectPath));
if (!methodName.empty()) {
return kleeOutDir / relative / ("klee_out_" + methodName);
}
return kleeOutDir / relative / ("klee_out_" + srcFilePath.filename().stem().string());
}

//endregion

//region extensions
Expand Down Expand Up @@ -281,48 +292,13 @@ namespace Paths {
//endregion

//region transformation
const std::string MAKEFILE_EXTENSION = ".mk";
const std::string TEST_SUFFIX = "_test";
const std::string STUB_SUFFIX = "_stub";
const std::string DOT_SEP = "_dot_";
const std::string MAKE_WRAPPER_SUFFIX = "_wrapper";
const char dot = '.';

fs::path sourcePathToTestPath(const utbot::ProjectContext &projectContext,
const fs::path &sourceFilePath) {
return projectContext.testDirPath / getRelativeDirPath(projectContext, sourceFilePath) /
sourcePathToTestName(sourceFilePath);
}

static inline fs::path addOrigExtensionAsSuffixAndAddNew(const fs::path &path,
const std::string &newExt) {
std::string extensionAsSuffix = path.extension().string();
if (!extensionAsSuffix.empty()) {
std::string fnWithNewExt =
path.stem().string() + DOT_SEP + extensionAsSuffix.substr(1) + newExt;
return path.parent_path() / fnWithNewExt;
}
return replaceExtension(path, newExt);
}

static inline fs::path restoreExtensionFromSuffix(const fs::path &path,
const std::string &defaultExt) {
std::string fnWithoutExt = path.stem();
fs::path fnWithExt;
std::size_t posEncodedExtension = fnWithoutExt.rfind(DOT_SEP);
if (posEncodedExtension == std::string::npos) {
// In `sample_class_test.cpp` the `class` is not an extension
fnWithExt = fnWithoutExt + defaultExt;
}
else {
// In `sample_class_dot_cpp.cpp` the `cpp` is an extension
fnWithExt = fnWithoutExt.substr(0, posEncodedExtension)
+ dot
+ fnWithoutExt.substr(posEncodedExtension + DOT_SEP.length());
}
return path.parent_path() / fs::path(fnWithExt);
}

fs::path sourcePathToTestName(const fs::path &source) {
return addSuffix(addOrigExtensionAsSuffixAndAddNew(source, ".cpp"),
TEST_SUFFIX).filename();
Expand Down
Loading