Skip to content

Commit 1c6a045

Browse files
give every branch node an ID
1 parent 261c650 commit 1c6a045

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

headers/wasm/concolic_driver.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static void start_concolic_execution_with(
100100
}
101101

102102
static void start_concolic_execution_with(
103-
std::function<std::monostate(std::monostate)> entrypoint) {
103+
std::function<std::monostate(std::monostate)> entrypoint, int branchCount) {
104104

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

headers/wasm/symbolic_rt.hpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ struct NodeBox {
291291
std::unique_ptr<Node> node;
292292
NodeBox *parent;
293293

294-
std::monostate fillIfElseNode(SymVal cond);
294+
std::monostate fillIfElseNode(SymVal cond, int id);
295295
std::monostate fillFinishedNode();
296296
std::monostate fillFailedNode();
297297
std::monostate fillUnreachableNode();
@@ -344,10 +344,11 @@ struct IfElseNode : Node {
344344
SymVal cond;
345345
std::unique_ptr<NodeBox> true_branch;
346346
std::unique_ptr<NodeBox> false_branch;
347+
int id;
347348

348-
IfElseNode(SymVal cond, NodeBox *parent)
349+
IfElseNode(SymVal cond, NodeBox *parent, int id)
349350
: cond(cond), true_branch(std::make_unique<NodeBox>(parent)),
350-
false_branch(std::make_unique<NodeBox>(parent)) {}
351+
false_branch(std::make_unique<NodeBox>(parent)), id(id) {}
351352

352353
std::string to_string() override {
353354
std::string result = "IfElseNode {\n";
@@ -480,10 +481,10 @@ inline NodeBox::NodeBox(NodeBox *parent)
480481
/* TODO: avoid allocation of unexplored node */
481482
parent(parent) {}
482483

483-
inline std::monostate NodeBox::fillIfElseNode(SymVal cond) {
484+
inline std::monostate NodeBox::fillIfElseNode(SymVal cond, int id) {
484485
// fill the current NodeBox with an ifelse branch node when it's unexplored
485486
if (this->isUnexplored()) {
486-
node = std::make_unique<IfElseNode>(cond, this);
487+
node = std::make_unique<IfElseNode>(cond, this, id);
487488
}
488489
assert(
489490
dynamic_cast<IfElseNode *>(node.get()) != nullptr &&
@@ -581,8 +582,8 @@ class ExploreTree_t {
581582

582583
std::monostate fillFailedNode() { return cursor->fillFailedNode(); }
583584

584-
std::monostate fillIfElseNode(SymVal cond) {
585-
return cursor->fillIfElseNode(cond);
585+
std::monostate fillIfElseNode(SymVal cond, int id) {
586+
return cursor->fillIfElseNode(cond, id);
586587
}
587588

588589
std::monostate moveCursor(bool branch, Snapshot_t snapshot) {

src/main/scala/wasm/StagedConcolicMiniWasm.scala

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@ import gensym.wasm.symbolic.Concrete
1919
import gensym.wasm.symbolic.ExploreTree
2020
import gensym.structure.freer.Explore
2121

22+
object Counter {
23+
var currentId: Int = 0
24+
private val dict = new HashMap[WIR, Int]()
25+
26+
def reset(): Unit = {
27+
currentId = 0
28+
dict.clear()
29+
}
30+
31+
def getId(wir: WIR): Int = {
32+
if (dict.contains(wir)) {
33+
dict(wir)
34+
} else {
35+
val id = currentId
36+
currentId += 1
37+
dict(wir) = id
38+
id
39+
}
40+
}
41+
}
2242
@virtualize
2343
trait StagedWasmEvaluator extends SAIOps {
2444
def module: ModuleInstance
@@ -365,7 +385,8 @@ trait StagedWasmEvaluator extends SAIOps {
365385
val newRestCtx = restCtx.shift(offset, funcTy.out.size)
366386
eval(rest, kont, mk, trail)(newRestCtx)
367387
})
368-
ExploreTree.fillWithIfElse(symCond.s)
388+
val id = Counter.getId(inst)
389+
ExploreTree.fillWithIfElse(symCond.s, id)
369390
def thnK: Rep[Cont[Unit]] = topFun((mk: Rep[MCont[Unit]]) => {
370391
info("Entering the true branch of the if")
371392
eval(thn, restK _, mk, restK _ :: trail)(newCtx)
@@ -392,7 +413,8 @@ trait StagedWasmEvaluator extends SAIOps {
392413
val cond = Stack.popC(ty)
393414
val symCond = Stack.popS(ty)
394415
info(s"The br_if(${label})'s condition is ", cond.toInt)
395-
ExploreTree.fillWithIfElse(symCond.s)
416+
val id = Counter.getId(inst)
417+
ExploreTree.fillWithIfElse(symCond.s, id)
396418
def thnK: Rep[Cont[Unit]] = topFun((mk: Rep[MCont[Unit]]) => {
397419
trail(label)(newCtx)(mk)
398420
})
@@ -423,7 +445,8 @@ trait StagedWasmEvaluator extends SAIOps {
423445
val labelSym = Stack.peekS(ty)
424446
val cond = (label - toStagedNum(I32V(idx))).isZero()
425447
val condSym = (labelSym - toStagedSymbolicNum(I32V(idx))).isZero()
426-
ExploreTree.fillWithIfElse(condSym.s)
448+
val id = Counter.getId(inst)
449+
ExploreTree.fillWithIfElse(condSym.s, id)
427450
// When moving the cursor to a branch, we mark another branch as
428451
// snapshotNode (this is done by moveCursor's runtime implementation)
429452
// TODO: store snapshot into this snapshot node
@@ -622,6 +645,7 @@ trait StagedWasmEvaluator extends SAIOps {
622645
}
623646

624647
def evalTop(mkont: Rep[MCont[Unit]], main: Option[String]): Rep[Unit] = {
648+
Counter.reset()
625649
val funBody: FuncBodyDef = main match {
626650
case Some(func_name) =>
627651
module.defs.flatMap({
@@ -912,8 +936,8 @@ trait StagedWasmEvaluator extends SAIOps {
912936

913937
// Exploration tree,
914938
object ExploreTree {
915-
def fillWithIfElse(s: Rep[SymVal]): Rep[Unit] = {
916-
"tree-fill-if-else".reflectCtrlWith[Unit](s)
939+
def fillWithIfElse(s: Rep[SymVal], id: Int): Rep[Unit] = {
940+
"tree-fill-if-else".reflectCtrlWith[Unit](s, id)
917941
}
918942

919943
def fillWithFinished(): Rep[Unit] = {
@@ -1448,8 +1472,8 @@ trait StagedWasmCppGen extends CGenBase with CppSAICodeGenBase {
14481472
emit("SymEnv.read("); shallow(sym); emit(")")
14491473
case Node(_, "assert-true", List(cond), _) =>
14501474
emit("GENSYM_ASSERT("); shallow(cond); emit(")")
1451-
case Node(_, "tree-fill-if-else", List(sym), _) =>
1452-
emit("ExploreTree.fillIfElseNode("); shallow(sym); emit(")")
1475+
case Node(_, "tree-fill-if-else", List(sym, id), _) =>
1476+
emit("ExploreTree.fillIfElseNode("); shallow(sym); emit(", "); emit(id.toString); emit(")")
14531477
case Node(_, "tree-fill-finished", List(), _) =>
14541478
emit("ExploreTree.fillFinishedNode()")
14551479
case Node(_, "tree-move-cursor", List(b, snapshot), _) =>
@@ -1496,12 +1520,12 @@ trait StagedWasmCppGen extends CGenBase with CppSAICodeGenBase {
14961520
emitDatastructures(stream)
14971521
emitFunctions(stream)
14981522
emit(src)
1499-
emitln("""
1523+
emitln(s"""
15001524
|/*****************************************
15011525
|End of Generated Code
15021526
|*******************************************/
15031527
|int main(int argc, char *argv[]) {
1504-
| start_concolic_execution_with(Snippet);
1528+
| start_concolic_execution_with(Snippet, ${Counter.currentId});
15051529
| return 0;
15061530
|}""".stripMargin)
15071531
}

0 commit comments

Comments
 (0)