Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c420a2a
remove unused expressions
butterunderflow Jul 5, 2025
fb2a2c4
let's start from the staged miniwasm interpreter
butterunderflow Jul 7, 2025
27e3e32
dup all concrete operations to symbolic
butterunderflow Jul 7, 2025
2143050
maintain a symbolic stack during the execution
butterunderflow Jul 7, 2025
8d81fbe
record path conditions
butterunderflow Jul 9, 2025
61215b6
The branch node only needs to remember the positive condition.
butterunderflow Jul 9, 2025
d18b5f7
symbolic runtime for explore tree
butterunderflow Jul 13, 2025
92ab8ba
add a to graphviz method, enhancing debug experience
butterunderflow Jul 13, 2025
e1d7fc8
put symbolic expression on the SymStack
butterunderflow Jul 14, 2025
77a4e6f
`type.symbolic` instruction
butterunderflow Jul 16, 2025
314ff5f
test staged concolic compilation in CI
butterunderflow Jul 16, 2025
8739369
dump graphviz by default
butterunderflow Jul 16, 2025
9a9988c
concolic driver
butterunderflow Jul 17, 2025
9ab162f
fix: add an unreachable node & use GENSYM_ASSERT
butterunderflow Jul 18, 2025
b75a627
call z3 to solve constraints
butterunderflow Jul 19, 2025
26c9917
remove unused & resize before update environment
butterunderflow Jul 19, 2025
319cfd6
use c++20
butterunderflow Jul 23, 2025
8f45912
branch in brtable
butterunderflow Jul 23, 2025
2e2259d
use driver's entrypoint by default
butterunderflow Jul 23, 2025
2b42b27
rename package name of staged miniwasm
butterunderflow Jul 23, 2025
619a8f0
tweak
butterunderflow Jul 23, 2025
af6751a
Reuse symbolic states (#90)
butterunderflow Aug 27, 2025
731ff9e
c++17 compatible
butterunderflow Aug 27, 2025
ffa5670
fix
butterunderflow Aug 29, 2025
b57929a
revert: don't split concrete/symbolic interpreter & don't support sna…
butterunderflow Aug 29, 2025
1bdb7da
introduce a SnapshotNode, which currently behaves same as UnexploredNode
butterunderflow Aug 30, 2025
64dce32
fill snapshot into SnapshotNode
butterunderflow Aug 30, 2025
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
1 change: 1 addition & 0 deletions .github/workflows/scala.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ jobs:
sbt 'testOnly gensym.wasm.TestConcolic'
sbt 'testOnly gensym.wasm.TestDriver'
sbt 'testOnly gensym.wasm.TestStagedEval'
sbt 'testOnly gensym.wasm.TestStagedConcolicEval'
1 change: 1 addition & 0 deletions benchmarks/wasm/branch-strip-buggy.wat
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
else
i32.const 0
call 2
i32.const 1 ;; to satisfy the type checker, this line will never be reached
end
end
)
Expand Down
22 changes: 22 additions & 0 deletions benchmarks/wasm/staged/brtable_concolic.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(module $brtable
(global (;0;) (mut i32) (i32.const 1048576))
(type (;0;) (func (param i32)))
(func (;0;) (type 1) (result i32)
i32.const 2
(block
(block
(block
i32.const 0
i32.symbolic
br_table 0 1 2 0 ;; br_table will consume an element from the stack
)
i32.const 1
call 1
br 1
)
i32.const 0
call 1
)
)
(import "console" "assert" (func (type 0)))
(start 0))
19 changes: 19 additions & 0 deletions benchmarks/wasm/staged/return_poly.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(module
(type (;0;) (func))
(type (;1;) (func (result i32)))
;; TODO: It seems that our parser or preprocessor has some problems; the result type of the last line doesn't take effect
(func (result i32)
block
i32.const 21
i32.const 35
i32.const 42
return
end
i32.const 100
)
(func (type 0)
call 0
;; unreachable
)
(export "$real_main" (func 1))
)
4 changes: 3 additions & 1 deletion headers/wasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
#define WASM_HEADERS

#include "wasm/concrete_rt.hpp"

#include "wasm/symbolic_rt.hpp"
#include "wasm/concolic_driver.hpp"
#include "wasm/utils.hpp"
#endif
106 changes: 106 additions & 0 deletions headers/wasm/concolic_driver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#ifndef CONCOLIC_DRIVER_HPP
#define CONCOLIC_DRIVER_HPP

#include "concrete_rt.hpp"
#include "smt_solver.hpp"
#include "symbolic_rt.hpp"
#include "utils.hpp"
#include <functional>
#include <ostream>
#include <string>
#include <vector>

class ConcolicDriver {
friend class ManagedConcolicCleanup;

public:
ConcolicDriver(std::function<void()> entrypoint, std::string tree_file)
: entrypoint(entrypoint), tree_file(tree_file) {}
ConcolicDriver(std::function<void()> entrypoint)
: entrypoint(entrypoint), tree_file(std::nullopt) {}
void run();

private:
Solver solver;
std::function<void()> entrypoint;
std::optional<std::string> tree_file;
};

class ManagedConcolicCleanup {
const ConcolicDriver &driver;

public:
ManagedConcolicCleanup(const ConcolicDriver &driver) : driver(driver) {}
~ManagedConcolicCleanup() {
if (driver.tree_file.has_value())
ExploreTree.dump_graphviz(driver.tree_file.value());
}
};

inline void ConcolicDriver::run() {
while (true) {
ManagedConcolicCleanup cleanup{*this};
ExploreTree.reset_cursor();

auto unexplored = ExploreTree.pick_unexplored();
if (!unexplored) {
GENSYM_INFO("No unexplored nodes found, exiting...");
return;
}
auto cond = unexplored->collect_path_conds();
auto result = solver.solve(cond);
if (!result.has_value()) {
GENSYM_INFO("Found an unreachable path, marking it as unreachable...");
unexplored->fillUnreachableNode();
continue;
}
auto new_env = result.value();
SymEnv.update(std::move(new_env));
try {
GENSYM_INFO("Now execute the program with symbolic environment: ");
GENSYM_INFO(SymEnv.to_string());
entrypoint();
GENSYM_INFO("Execution finished successfully with symbolic environment:");
GENSYM_INFO(SymEnv.to_string());
} catch (...) {
ExploreTree.fillFailedNode();
GENSYM_INFO("Caught runtime error with symbolic environment:");
GENSYM_INFO(SymEnv.to_string());
return;
}
#if defined(RUN_ONCE)
return;
#endif
}
}

static std::monostate reset_stacks() {
Stack.reset();
Frames.reset();
SymStack.reset();
SymFrames.reset();
initRand();
Memory = Memory_t(1);
return std::monostate{};
}

static void start_concolic_execution_with(
std::function<std::monostate(std::monostate)> entrypoint,
std::string tree_file) {
ConcolicDriver driver([=]() { entrypoint(std::monostate{}); }, tree_file);
driver.run();
}

static void start_concolic_execution_with(
std::function<std::monostate(std::monostate)> entrypoint) {

const char *env_tree_file = std::getenv("TREE_FILE");

ConcolicDriver driver =
env_tree_file ? ConcolicDriver([=]() { entrypoint(std::monostate{}); },
env_tree_file)
: ConcolicDriver([=]() { entrypoint(std::monostate{}); });
driver.run();
}

#endif // CONCOLIC_DRIVER_HPP
20 changes: 13 additions & 7 deletions headers/wasm/concrete_rt.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef WASM_CONCRETE_RT_HPP
#define WASM_CONCRETE_RT_HPP

#include <cassert>
#include <cstdint>
#include <cstdio>
Expand Down Expand Up @@ -49,8 +52,6 @@ static Num I32V(int v) { return v; }

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

using Slice = std::vector<Num>;

const int STACK_SIZE = 1024 * 64;

class Stack_t {
Expand All @@ -71,9 +72,7 @@ class Stack_t {

Num pop() {
#ifdef DEBUG
if (count == 0) {
throw std::runtime_error("Stack underflow");
}
assert(count > 0 && "Stack underflow");
#endif
Num num = stack_ptr[count - 1];
count--;
Expand Down Expand Up @@ -115,9 +114,12 @@ class Stack_t {
}

void initialize() {
// do nothing for now
// todo: remove this method
reset();
}

void reset() { count = 0; }

private:
int32_t count;
Num *stack_ptr;
Expand Down Expand Up @@ -148,6 +150,8 @@ class Frames_t {
count += size;
}

void reset() { count = 0; }

private:
int32_t count;
Num *stack_ptr;
Expand Down Expand Up @@ -200,4 +204,6 @@ struct Memory_t {
}
};

static Memory_t Memory(1); // 1 page memory
static Memory_t Memory(1); // 1 page memory

#endif // WASM_CONCRETE_RT_HPP
5 changes: 5 additions & 0 deletions headers/wasm/controls.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <functional>
#include <variant>

using MCont_t = std::function<std::monostate(std::monostate)>;
using Cont_t = std::function<std::monostate(MCont_t)>;
126 changes: 126 additions & 0 deletions headers/wasm/smt_solver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#ifndef SMT_SOLVER_HPP
#define SMT_SOLVER_HPP

#include "concrete_rt.hpp"
#include "symbolic_rt.hpp"
#include "utils.hpp"
#include "z3++.h"
#include <array>
#include <set>
#include <string>
#include <tuple>
#include <vector>

class Solver {
public:
Solver() {}
std::optional<std::vector<Num>> solve(const std::vector<SymVal> &conditions) {
// make an conjunction of all conditions
z3::expr conjunction = z3_ctx.bool_val(true);
for (const auto &cond : conditions) {
auto z3_cond = build_z3_expr(cond);
conjunction = conjunction && z3_cond != z3_ctx.bv_val(0, 32);
}
#ifdef DEBUG
std::cout << "Symbolic conditions size: " << conditions.size() << std::endl;
std::cout << "Solving conditions: " << conjunction << std::endl;
#endif
// call z3 to solve the condition
z3::solver z3_solver(z3_ctx);
z3_solver.add(conjunction);
switch (z3_solver.check()) {
case z3::unsat:
return std::nullopt; // No solution found
case z3::sat: {
z3::model model = z3_solver.get_model();
std::vector<Num> result;
// Reference:
// https://github.com/Z3Prover/z3/blob/master/examples/c%2B%2B/example.cpp#L59
GENSYM_INFO("Solved Z3 model");
GENSYM_INFO(model);
for (unsigned i = 0; i < model.size(); ++i) {
z3::func_decl var = model[i];
z3::expr value = model.get_const_interp(var);
std::string name = var.name().str();
if (starts_with(name, "s_")) {
int id = std::stoi(name.substr(2));
if (id >= result.size()) {
result.resize(id + 1);
}
result[id] = Num(value.get_numeral_int64());
} else {
GENSYM_INFO("Find a variable that is not created by GenSym: " + name);
}
}
return result;
}
case z3::unknown:
throw std::runtime_error("Z3 solver returned unknown status");
}
return std::nullopt; // Should not reach here
}

private:
z3::context z3_ctx;
z3::expr build_z3_expr(const SymVal &sym_val);
};

inline z3::expr Solver::build_z3_expr(const SymVal &sym_val) {
if (auto sym = std::dynamic_pointer_cast<Symbol>(sym_val.symptr)) {
return z3_ctx.bv_const(("s_" + std::to_string(sym->get_id())).c_str(), 32);
} else if (auto concrete =
std::dynamic_pointer_cast<SymConcrete>(sym_val.symptr)) {
return z3_ctx.bv_val(concrete->value.value, 32);
} else if (auto binary =
std::dynamic_pointer_cast<SymBinary>(sym_val.symptr)) {
auto bit_width = 32;
z3::expr zero_bv =
z3_ctx.bv_val(0, bit_width); // Represents 0 as a 32-bit bitvector
z3::expr one_bv =
z3_ctx.bv_val(1, bit_width); // Represents 1 as a 32-bit bitvector

z3::expr left = build_z3_expr(binary->lhs);
z3::expr right = build_z3_expr(binary->rhs);
// TODO: make sure the semantics of these operations are aligned with wasm
switch (binary->op) {
case EQ: {
auto temp_bool = left == right;
return z3::ite(temp_bool, one_bv, zero_bv);
}
case NEQ: {
auto temp_bool = left != right;
return z3::ite(temp_bool, one_bv, zero_bv);
}
case LT: {
auto temp_bool = left < right;
return z3::ite(temp_bool, one_bv, zero_bv);
}
case LEQ: {
auto temp_bool = left <= right;
return z3::ite(temp_bool, one_bv, zero_bv);
}
case GT: {
auto temp_bool = left > right;
return z3::ite(temp_bool, one_bv, zero_bv);
}
case GEQ: {
auto temp_bool = left >= right;
return z3::ite(temp_bool, one_bv, zero_bv);
}
case ADD: {
return left + right;
}
case SUB: {
return left - right;
}
case MUL: {
return left * right;
}
case DIV: {
return left / right;
}
}
}
throw std::runtime_error("Unsupported symbolic value type");
}
#endif // SMT_SOLVER_HPP
Loading