Skip to content

Commit 50fd194

Browse files
committed
[feat] Add options to store and rerun execution states during execution
1 parent 75bf174 commit 50fd194

32 files changed

+885
-854
lines changed

include/klee/ADT/SeedFromFile.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef KLEE_SEED_FROM_FILE_H
2+
#include <string>
3+
4+
class KTest;
5+
6+
namespace klee {
7+
8+
class SeedFromFile {
9+
public:
10+
KTest *ktest;
11+
unsigned maxInstructions;
12+
13+
SeedFromFile() {}
14+
SeedFromFile(std::string _path);
15+
};
16+
17+
} // namespace klee
18+
#define KLEE_SEED_FROM_FILE_H
19+
#endif

include/klee/Core/BranchTypes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
BTYPE(Realloc, 8U) \
2626
BTYPE(Free, 9U) \
2727
BTYPE(GetVal, 10U) \
28-
BMARK(END, 10U)
28+
BTYPE(InitialBranch, 11U) \
29+
BMARK(END, 11U)
2930
/// \endcond
3031

3132
/** @enum BranchType

include/klee/Core/Interpreter.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define KLEE_INTERPRETER_H
1111

1212
#include "TerminationTypes.h"
13+
#include "klee/ADT/SeedFromFile.h"
1314
#include "klee/Module/Annotation.h"
1415

1516
#include "klee/Module/SarifReport.h"
@@ -200,18 +201,9 @@ class Interpreter {
200201
// to record the symbolic path (as a stream of '0' and '1' bytes).
201202
virtual void setSymbolicPathWriter(TreeStreamWriter *tsw) = 0;
202203

203-
// supply a test case to replay from. this can be used to drive the
204-
// interpretation down a user specified path. use null to reset.
205-
virtual void setReplayKTest(const struct KTest *out) = 0;
206-
207-
// supply a list of branch decisions specifying which direction to
208-
// take on forks. this can be used to drive the interpretation down
209-
// a user specified path. use null to reset.
210-
virtual void setReplayPath(const std::vector<bool> *path) = 0;
211-
212204
// supply a set of symbolic bindings that will be used as "seeds"
213205
// for the search. use null to reset.
214-
virtual void useSeeds(const std::vector<struct KTest *> *seeds) = 0;
206+
virtual void useSeeds(std::vector<SeedFromFile> seeds) = 0;
215207

216208
virtual void runFunctionAsMain(llvm::Function *f, int argc, char **argv,
217209
char **envp) = 0;
@@ -237,6 +229,9 @@ class Interpreter {
237229
virtual void getConstraintLog(const ExecutionState &state, std::string &res,
238230
LogType logFormat = STP) = 0;
239231

232+
virtual void getSteppedInstructions(const ExecutionState &state,
233+
unsigned &res) = 0;
234+
240235
virtual bool getSymbolicSolution(const ExecutionState &state, KTest &res) = 0;
241236

242237
virtual void addSARIFReport(const ExecutionState &state) = 0;

include/klee/Core/TerminationTypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ enum Reason {
101101
ErrorOnWhichShouldExit,
102102
Interrupt,
103103
MaxDepth,
104+
MaxMemory,
104105
MaxStackFrames,
105106
MaxSolverTime,
106107
Unspecified

lib/ADT/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#
88
#===------------------------------------------------------------------------===#
99
add_library(kleeADT
10+
SeedFromFile.cpp
1011
SparseStorage.cpp
12+
SeedFromFile.cpp
1113
)
1214

1315
llvm_config(kleeADT "${USE_LLVM_SHARED}" support)

lib/ADT/SeedFromFile.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "klee/ADT/SeedFromFile.h"
2+
#include "klee/ADT/KTest.h"
3+
4+
#include <fstream>
5+
6+
using namespace klee;
7+
8+
SeedFromFile::SeedFromFile(std::string _path)
9+
: ktest(kTest_fromFile((_path + "ktest").c_str())) {
10+
std::ifstream seedInfoStream(_path + "seedinfo");
11+
if (seedInfoStream.good()) {
12+
seedInfoStream >> maxInstructions;
13+
}
14+
}

lib/Basic/KTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,13 @@ void kTest_free(KTest *bo) {
295295
free(bo->objects);
296296
free(bo);
297297
}
298+
299+
unsigned getKTestMemoryUsage(KTest *ktest) {
300+
size_t size = 0;
301+
for (unsigned i = 0; i < ktest->numObjects; i++) {
302+
size += std::strlen(ktest->objects[i].name) * sizeof(char);
303+
size += ktest->objects[i].numBytes * sizeof(unsigned char);
304+
size += ktest->objects[i].numPointers * sizeof(Pointer);
305+
}
306+
return size;
307+
}

lib/Core/ExecutionState.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ ExecutionState::ExecutionState(const ExecutionState &state)
170170
coveredNew(state.coveredNew), coveredNewError(state.coveredNewError),
171171
forkDisabled(state.forkDisabled), returnValue(state.returnValue),
172172
gepExprBases(state.gepExprBases), multiplexKF(state.multiplexKF),
173-
prevTargets_(state.prevTargets_), targets_(state.targets_),
174-
prevHistory_(state.prevHistory_), history_(state.history_),
175-
isTargeted_(state.isTargeted_) {
173+
isSeeded(state.isSeeded), prevTargets_(state.prevTargets_),
174+
targets_(state.targets_), prevHistory_(state.prevHistory_),
175+
history_(state.history_), isTargeted_(state.isTargeted_) {
176176
queryMetaData.id = state.id;
177177
}
178178

lib/Core/ExecutionState.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ class ExecutionState {
420420
// Temp: to know which multiplex path this state has taken
421421
KFunction *multiplexKF = nullptr;
422422

423+
bool isSeeded = false;
424+
423425
private:
424426
PersistentSet<ref<Target>> prevTargets_;
425427
PersistentSet<ref<Target>> targets_;
@@ -514,6 +516,11 @@ class ExecutionState {
514516

515517
inline void setTargeted(bool targeted) { isTargeted_ = targeted; }
516518

519+
inline void setTargets(const PersistentSet<ref<Target>> &targets) {
520+
targets_ = targets;
521+
areTargetsChanged_ = true;
522+
}
523+
517524
inline void setTargets(const TargetHashSet &targets) {
518525
targets_ = PersistentSet<ref<Target>>();
519526
for (auto target : targets) {

0 commit comments

Comments
 (0)