Skip to content

Commit 9a9988c

Browse files
concolic driver
1 parent 8739369 commit 9a9988c

File tree

7 files changed

+338
-40
lines changed

7 files changed

+338
-40
lines changed

headers/wasm.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
#include "wasm/concrete_rt.hpp"
55
#include "wasm/symbolic_rt.hpp"
6-
6+
#include "wasm/concolic_driver.hpp"
7+
#include "wasm/utils.hpp"
78
#endif

headers/wasm/concolic_driver.hpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#ifndef CONCOLIC_DRIVER_HPP
2+
#define CONCOLIC_DRIVER_HPP
3+
4+
#include "smt_solver.hpp"
5+
#include "symbolic_rt.hpp"
6+
#include <functional>
7+
#include <ostream>
8+
#include <string>
9+
10+
class ConcolicDriver {
11+
friend class ManagedConcolicCleanup;
12+
13+
public:
14+
ConcolicDriver(std::function<void()> entrypoint, std::string tree_file)
15+
: entrypoint(entrypoint), tree_file(tree_file) {}
16+
ConcolicDriver(std::function<void()> entrypoint)
17+
: entrypoint(entrypoint), tree_file(std::nullopt) {}
18+
void run();
19+
20+
private:
21+
Solver solver;
22+
std::function<void()> entrypoint;
23+
std::optional<std::string> tree_file;
24+
};
25+
26+
class ManagedConcolicCleanup {
27+
const ConcolicDriver &driver;
28+
29+
public:
30+
ManagedConcolicCleanup(const ConcolicDriver &driver) : driver(driver) {}
31+
~ManagedConcolicCleanup() {
32+
if (driver.tree_file.has_value())
33+
ExploreTree.dump_graphviz(driver.tree_file.value());
34+
}
35+
};
36+
37+
inline void ConcolicDriver::run() {
38+
ManagedConcolicCleanup cleanup{*this};
39+
while (true) {
40+
auto cond = ExploreTree.get_unexplored_conditions();
41+
ExploreTree.reset_cursor();
42+
43+
if (!cond.has_value()) {
44+
std::cout << "No unexplored conditions found, exiting..." << std::endl;
45+
return;
46+
}
47+
auto new_env = solver.solve(cond.value());
48+
if (!new_env.has_value()) {
49+
std::cout << "All unexplored paths are unreachable, exiting..."
50+
<< std::endl;
51+
return;
52+
}
53+
SymEnv.update(std::move(new_env.value()));
54+
try {
55+
entrypoint();
56+
std::cout << "Execution finished successfully with symbolic environment:"
57+
<< std::endl;
58+
std::cout << SymEnv.to_string() << std::endl;
59+
} catch (...) {
60+
ExploreTree.fillFailedNode();
61+
std::cout << "Caught runtime error with symbolic environment:"
62+
<< std::endl;
63+
std::cout << SymEnv.to_string() << std::endl;
64+
return;
65+
}
66+
}
67+
}
68+
69+
static std::monostate reset_stacks() {
70+
Stack.reset();
71+
Frames.reset();
72+
SymStack.reset();
73+
SymFrames.reset();
74+
initRand();
75+
Memory = Memory_t(1);
76+
return std::monostate{};
77+
}
78+
79+
static void start_concolic_execution_with(
80+
std::function<std::monostate(std::monostate)> entrypoint,
81+
std::string tree_file) {
82+
ConcolicDriver driver([=]() { entrypoint(std::monostate{}); }, tree_file);
83+
driver.run();
84+
}
85+
86+
static void start_concolic_execution_with(
87+
std::function<std::monostate(std::monostate)> entrypoint) {
88+
89+
const char *env_tree_file = std::getenv("TREE_FILE");
90+
91+
ConcolicDriver driver =
92+
env_tree_file ? ConcolicDriver([=]() { entrypoint(std::monostate{}); },
93+
env_tree_file)
94+
: ConcolicDriver([=]() { entrypoint(std::monostate{}); });
95+
driver.run();
96+
}
97+
98+
#endif // CONCOLIC_DRIVER_HPP

headers/wasm/concrete_rt.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ static Num I32V(int v) { return v; }
5252

5353
static Num I64V(int64_t v) { return v; }
5454

55-
using Slice = std::vector<Num>;
56-
5755
const int STACK_SIZE = 1024 * 64;
5856

5957
class Stack_t {
@@ -118,9 +116,12 @@ class Stack_t {
118116
}
119117

120118
void initialize() {
121-
// do nothing for now
119+
// todo: remove this method
120+
reset();
122121
}
123122

123+
void reset() { count = 0; }
124+
124125
private:
125126
int32_t count;
126127
Num *stack_ptr;
@@ -151,6 +152,8 @@ class Frames_t {
151152
count += size;
152153
}
153154

155+
void reset() { count = 0; }
156+
154157
private:
155158
int32_t count;
156159
Num *stack_ptr;

headers/wasm/smt_solver.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef SMT_SOLVER_HPP
2+
#define SMT_SOLVER_HPP
3+
4+
#include "concrete_rt.hpp"
5+
#include "symbolic_rt.hpp"
6+
#include <array>
7+
#include <vector>
8+
9+
class Solver {
10+
public:
11+
Solver() : count(0) {
12+
envs[0] = {Num(0), Num(0)};
13+
envs[1] = {Num(1), Num(2)};
14+
}
15+
std::optional<std::vector<Num>> solve(const std::vector<SymVal> &conditions) {
16+
// here is just a placeholder implementation to simulate solving result
17+
if (count >= envs.size()) {
18+
return std::nullopt; // No more environments to return
19+
}
20+
return envs[count++ % envs.size()];
21+
}
22+
23+
private:
24+
std::array<std::vector<Num>, 5> envs;
25+
int count;
26+
};
27+
28+
#endif // SMT_SOLVER_HPP

0 commit comments

Comments
 (0)