Skip to content

Commit 7cdfe53

Browse files
update sym_assert and timer
1 parent 6dd52b2 commit 7cdfe53

File tree

5 files changed

+64
-11
lines changed

5 files changed

+64
-11
lines changed

headers/wasm/concolic_driver.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class ConcolicDriver {
3131

3232
private:
3333
void main_exploration_loop();
34-
Solver solver;
3534
std::function<void()> entrypoint;
3635
std::optional<std::string> tree_file;
3736
std::vector<NodeBox *> work_list;

headers/wasm/profile.hpp

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <chrono>
88
#include <iomanip>
99
#include <variant>
10+
#include <vector>
1011

1112
enum class StepProfileKind {
1213
PUSH,
@@ -157,8 +158,7 @@ class Profile_t {
157158
<< std::setprecision(15)
158159
<< time_count[static_cast<std::size_t>(TimeProfileKind::INSTR)]
159160
<< ",\n";
160-
os << " \"total_time_solver_s\": "
161-
<< std::setprecision(15)
161+
os << " \"total_time_solver_s\": " << std::setprecision(15)
162162
<< time_count[static_cast<std::size_t>(TimeProfileKind::SOLVER)]
163163
<< ",\n";
164164
os << " \"total_time_resuming_from_snapshot_s\": "
@@ -191,23 +191,50 @@ class Profile_t {
191191

192192
static Profile_t Profile;
193193

194-
class ManagedTimer {
194+
class Timer {
195195
public:
196-
ManagedTimer() = delete;
197-
ManagedTimer(TimeProfileKind kind) : kind(kind) {
196+
Timer() = delete;
197+
Timer(TimeProfileKind kind) : kind(kind) {
198+
elapsed = std::chrono::duration<double>::zero();
198199
start = std::chrono::high_resolution_clock::now();
199200
}
200-
~ManagedTimer() {
201+
~Timer() {
201202
auto end = std::chrono::high_resolution_clock::now();
202-
std::chrono::duration<double> elapsed = end - start;
203+
elapsed += end - start;
203204
Profile.add_instruction_time(kind, elapsed.count());
204205
}
206+
void stop() { elapsed += std::chrono::high_resolution_clock::now() - start; }
207+
void resume() { start = std::chrono::high_resolution_clock::now(); }
205208

206209
private:
210+
std::chrono::duration<double> elapsed;
207211
TimeProfileKind kind;
208212
std::chrono::high_resolution_clock::time_point start;
209213
};
210214

215+
static std::vector<Timer> TimerStack = []() {
216+
std::vector<Timer> v;
217+
v.reserve(3); // initial capacity
218+
return v;
219+
}();
220+
221+
class ManagedTimer {
222+
public:
223+
ManagedTimer() = delete;
224+
ManagedTimer(TimeProfileKind kind) {
225+
if (TimerStack.size() > 0) {
226+
TimerStack.back().stop();
227+
}
228+
TimerStack.emplace_back(kind);
229+
}
230+
~ManagedTimer() {
231+
TimerStack.pop_back();
232+
if (TimerStack.size() > 0) {
233+
TimerStack.back().resume();
234+
}
235+
}
236+
};
237+
211238
struct CostManager_t {
212239
int instr_cost;
213240

headers/wasm/smt_solver.hpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <set>
1212
#include <string>
1313
#include <tuple>
14+
#include <variant>
1415
#include <vector>
1516

1617
class Solver {
@@ -73,11 +74,11 @@ class Solver {
7374

7475
z3::context z3_ctx;
7576
z3::expr build_z3_expr(SymVal &sym_val);
76-
77-
private:
7877
z3::expr build_z3_expr_aux(SymVal &sym_val);
7978
};
8079

80+
static Solver solver;
81+
8182
inline z3::expr Solver::build_z3_expr_aux(SymVal &sym_val) {
8283
if (auto sym = std::dynamic_pointer_cast<Symbol>(sym_val.symptr)) {
8384
return z3_ctx.bv_const(("s_" + std::to_string(sym->get_id())).c_str(), 32);
@@ -164,4 +165,16 @@ inline z3::expr Solver::build_z3_expr(SymVal &sym_val) {
164165
sym_val.z3_expr = std::make_shared<z3::expr>(e);
165166
return e;
166167
}
168+
169+
inline std::monostate GENSYM_SYM_ASSERT(SymVal &sym_cond) {
170+
std::vector<SymVal> conds = ExploreTree.collect_current_path_conds();
171+
conds.push_back(sym_cond.negate());
172+
auto result = solver.solve(conds);
173+
if (result.has_value()) {
174+
std::cout << "Symbolic assertion failed" << std::endl;
175+
throw std::runtime_error("Symbolic assertion failed");
176+
}
177+
return std::monostate{};
178+
}
179+
167180
#endif // SMT_SOLVER_HPP

headers/wasm/symbolic_rt.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,10 @@ class ExploreTree_t {
957957
return cursor->fillNotToExploreNode();
958958
}
959959

960+
std::vector<SymVal> collect_current_path_conds() {
961+
return cursor->collect_path_conds();
962+
}
963+
960964
bool worth_to_create_snapshot() {
961965
if (!ENABLE_COST_MODEL) {
962966
return REUSE_SNAPSHOT;

src/main/scala/wasm/StagedConcolicMiniWasm.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,11 @@ trait StagedWasmEvaluator extends SAIOps {
576576
}
577577
()
578578
case Import("i32", "sym_assert", _) =>
579-
// TODO: implement sym_assert
579+
val (condTy, newCtx) = ctx.pop()
580+
val v = Stack.popC(condTy)
581+
val s = Stack.popS(condTy)
582+
runtimeSymAssert(s)
583+
runtimeAssert(v.toInt != 0)
580584
eval(rest, kont, mkont, trail)(ctx.pop()._2)
581585
case Import(m, f, _) => throw new Exception(s"Unknown import $m.$f at $funcIndex")
582586
case _ => throw new Exception(s"Definition at $funcIndex is not callable")
@@ -715,6 +719,10 @@ trait StagedWasmEvaluator extends SAIOps {
715719
"assert-true".reflectCtrlWith[Unit](b)
716720
}
717721

722+
def runtimeSymAssert(s: StagedSymbolicNum): Rep[Unit] = {
723+
"sym-assert-true".reflectCtrlWith[Unit](s.s)
724+
}
725+
718726
// stack operations
719727
object Stack {
720728
def shiftC(offset: Int, size: Int) = {
@@ -1578,6 +1586,8 @@ trait StagedWasmCppGen extends CGenBase with CppSAICodeGenBase {
15781586
emit("SymEnv.read("); shallow(sym); emit(")")
15791587
case Node(_, "assert-true", List(cond), _) =>
15801588
emit("GENSYM_ASSERT("); shallow(cond); emit(")")
1589+
case Node(_, "sym-assert-true", List(s_cond), _) =>
1590+
emit("GENSYM_SYM_ASSERT("); shallow(s_cond); emit(")")
15811591
case Node(_, "tree-fill-if-else", List(sym, id), _) =>
15821592
emit("ExploreTree.fillIfElseNode("); shallow(sym); emit(", "); emit(id.toString); emit(")")
15831593
case Node(_, "tree-fill-not-to-explore", List(), _) =>

0 commit comments

Comments
 (0)