Skip to content

Commit

Permalink
[wip] Add kmoduleOrig
Browse files Browse the repository at this point in the history
  • Loading branch information
misonijnik committed Nov 11, 2022
1 parent f31030a commit e125f1d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
3 changes: 2 additions & 1 deletion include/klee/Core/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ class Interpreter {
/// \return The final module after it has been optimized, checks
/// inserted, and modified for interpretation.
virtual llvm::Module *
setModule(std::vector<std::unique_ptr<llvm::Module>> &modules,
setModule(std::unique_ptr<llvm::Module> mainModule,
std::vector<std::unique_ptr<llvm::Module>> &modules,
const ModuleOptions &opts,
const std::vector<std::string> &mainFunctions) = 0;

Expand Down
9 changes: 8 additions & 1 deletion lib/Core/Executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,13 +593,15 @@ Executor::Executor(LLVMContext &ctx, const InterpreterOptions &opts,
}

llvm::Module *
Executor::setModule(std::vector<std::unique_ptr<llvm::Module>> &modules,
Executor::setModule(std::unique_ptr<llvm::Module> mainModule,
std::vector<std::unique_ptr<llvm::Module>> &modules,
const ModuleOptions &opts,
const std::vector<std::string> &mainFunctions) {
assert(!kmodule && !modules.empty() &&
"can only register one module"); // XXX gross

kmodule = std::unique_ptr<KModule>(new KModule());
kmoduleOrig = std::unique_ptr<KModule>(new KModule());

// Preparing the final module happens in multiple stages

Expand All @@ -613,6 +615,11 @@ Executor::setModule(std::vector<std::unique_ptr<llvm::Module>> &modules,
klee_error("Could not load KLEE intrinsic file %s", LibPath.c_str());
}

std::vector<std::unique_ptr<llvm::Module>> mainModuleVector;
mainModuleVector.push_back(std::move(mainModule));
kmoduleOrig->link(mainModuleVector, opts.EntryPoint);
kmoduleOrig->checkModule();

// 1.) Link the modules together
while (kmodule->link(modules, opts.EntryPoint)) {
// 2.) Apply different instrumentation
Expand Down
4 changes: 3 additions & 1 deletion lib/Core/Executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class Executor : public Interpreter {
static const std::unordered_set <llvm::Intrinsic::ID> modelledFPIntrinsics;

std::unique_ptr<KModule> kmodule;
std::unique_ptr<KModule> kmoduleOrig;
InterpreterHandler *interpreterHandler;
Searcher *searcher;

Expand Down Expand Up @@ -569,7 +570,8 @@ class Executor : public Interpreter {
}

llvm::Module *
setModule(std::vector<std::unique_ptr<llvm::Module>> &modules,
setModule(std::unique_ptr<llvm::Module> mainModule,
std::vector<std::unique_ptr<llvm::Module>> &modules,
const ModuleOptions &opts,
const std::vector<std::string> &mainFunctions) override;

Expand Down
15 changes: 14 additions & 1 deletion lib/Runner/run_klee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1846,11 +1846,17 @@ int run_klee(int argc, char **argv, char **envp) {
// Load the bytecode...
std::string errorMsg;
LLVMContext ctx;
LLVMContext originalCtx;
std::vector<std::unique_ptr<llvm::Module>> loadedModules;
std::vector<std::unique_ptr<llvm::Module>> originalLoadedModules;
if (!klee::loadFile(InputFile, ctx, loadedModules, errorMsg)) {
klee_error("error loading program '%s': %s", InputFile.c_str(),
errorMsg.c_str());
}
if (!klee::loadFile(InputFile, originalCtx, originalLoadedModules, errorMsg)) {
klee_error("error loading program '%s': %s", InputFile.c_str(),
errorMsg.c_str());
}
// Load and link the whole files content. The assumption is that this is the
// application under test.
// Nothing gets removed in the first place.
Expand All @@ -1861,6 +1867,13 @@ int run_klee(int argc, char **argv, char **envp) {
errorMsg.c_str());
}

std::unique_ptr<llvm::Module> OM(klee::linkModules(
originalLoadedModules, "" /* link all modules together */, errorMsg));
if (!OM) {
klee_error("error loading program '%s': %s", InputFile.c_str(),
errorMsg.c_str());
}

llvm::Module *mainModule = M.get();

if (ExecutionMode == Interpreter::GuidanceKind::ErrorGuidance) {
Expand Down Expand Up @@ -2080,7 +2093,7 @@ int run_klee(int argc, char **argv, char **envp) {
// Get the desired main function. klee_main initializes uClibc
// locale and other data and then calls main.

auto finalModule = interpreter->setModule(loadedModules, Opts, mainFunctions);
auto finalModule = interpreter->setModule(std::move(OM), loadedModules, Opts, mainFunctions);

if (InteractiveMode) {
klee_message("KLEE finish preprocessing.");
Expand Down

0 comments on commit e125f1d

Please sign in to comment.