Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 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
463871c
snapshot reuse via continuation
butterunderflow Aug 31, 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
114 changes: 114 additions & 0 deletions headers/wasm/concolic_driver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#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() {
ExploreTree.reset_cursor();
while (true) {
ManagedConcolicCleanup cleanup{*this};

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();

// update global symbolic environment from SMT solved model
SymEnv.update(std::move(new_env));
try {
GENSYM_INFO("Now execute the program with symbolic environment: ");
GENSYM_INFO(SymEnv.to_string());
if (auto snapshot_node =
dynamic_cast<SnapshotNode *>(unexplored->node.get())) {
snapshot_node->get_snapshot().resume_execution(SymEnv, unexplored);
} else {
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
44 changes: 37 additions & 7 deletions headers/wasm/concrete_rt.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#ifndef WASM_CONCRETE_RT_HPP
#define WASM_CONCRETE_RT_HPP

#include "wasm/utils.hpp"
#include <cassert>
#include <cstdint>
#include <cstdio>
Expand Down Expand Up @@ -49,8 +53,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 +73,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,7 +115,20 @@ class Stack_t {
}

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

void reset() { count = 0; }

void resize(int32_t new_size) {
assert(new_size >= 0);
count = new_size;
}

void set_from_front(int32_t index, const Num &num) {
assert(index >= 0 && index < count);
stack_ptr[index] = num;
}

private:
Expand Down Expand Up @@ -148,6 +161,21 @@ class Frames_t {
count += size;
}

void reset() { count = 0; }

size_t size() const { return count; }

void set_from_front(int32_t index, const Num &num) {
GENSYM_DBG(index);
assert(index >= 0 && index < count && "Index out of bounds");
stack_ptr[index] = num;
}

void resize(int32_t new_size) {
assert(new_size >= 0);
count = new_size;
}

private:
int32_t count;
Num *stack_ptr;
Expand Down Expand Up @@ -200,4 +228,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
12 changes: 12 additions & 0 deletions headers/wasm/controls.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

#ifndef WASM_CONTROLS_HPP
#define WASM_CONTROLS_HPP

#include <functional>

#include <variant>

using MCont_t = std::function<std::monostate(std::monostate)>;
using Cont_t = std::function<std::monostate(MCont_t)>;

#endif // WASM_CONTROLS_HPP
Loading
Loading