Skip to content

Commit 0f7ca5a

Browse files
support numeric globals
1 parent 1b92fc0 commit 0f7ca5a

File tree

6 files changed

+59
-3
lines changed

6 files changed

+59
-3
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(module $simple_global
2+
(type (;0;) (func (param i32 i32) (result i32)))
3+
(type (;1;) (func (result i32)))
4+
(type (;2;) (func (param i32)))
5+
(func $real_main (type 1) (result i32)
6+
(local i32)
7+
i32.const 0
8+
i32.symbolic
9+
local.tee 0
10+
local.get 0
11+
global.set 0
12+
if
13+
else
14+
i32.const 0
15+
call 1
16+
end)
17+
(import "console" "assert" (func (type 2)))
18+
(memory (;0;) 16)
19+
(global $__stack_pointer (mut i32) (i32.const 1048576))
20+
(global (;1;) i32 (i32.const 1048576))
21+
(global (;2;) i32 (i32.const 1048576))
22+
(export "real_main" (func 0)))

headers/wasm/concolic_driver.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ inline void ConcolicDriver::run() {
9494
if (ExploreTree.all_branch_covered()) {
9595
GENSYM_INFO("All branches covered, exiting...");
9696
return;
97+
} else {
98+
GENSYM_INFO("Found a bug, but not all branches covered, continuing...");
9799
}
98100
}
99101
}

headers/wasm/concrete_rt.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ class Frames_t {
181181
};
182182

183183
static Frames_t Frames;
184+
static Frames_t Globals;
184185

185186
static void initRand() {
186187
// for now, just do nothing

headers/wasm/symbolic_rt.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ inline void SymFrames_t::reuse(Snapshot_t snapshot) {
283283
}
284284

285285
static SymFrames_t SymFrames;
286+
static SymFrames_t SymGlobals;
286287

287288
struct Node;
288289

src/main/scala/wasm/StagedConcolicMiniWasm.scala

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ trait StagedWasmEvaluator extends SAIOps {
679679
}
680680
val (instrs, locals) = (funBody.body, funBody.locals)
681681
resetStacks()
682+
initGlobals(module.globals)
682683
Frames.pushFrameC(locals)
683684
Frames.pushFrameS(locals)
684685
eval(instrs, (_: Context) => forwardKont, mkont, ((_: Context) => forwardKont)::Nil)(Context(Nil, locals))
@@ -883,6 +884,18 @@ trait StagedWasmEvaluator extends SAIOps {
883884
"reset-stacks".reflectCtrlWith[Unit]()
884885
}
885886

887+
def initGlobals(globals: List[RTGlobal]): Rep[Unit] = {
888+
Globals.reserveSpace(globals.size)
889+
for ((g, i) <- globals.view.zipWithIndex) {
890+
val initValue = g.value match {
891+
case n: Num => n
892+
case _ => throw new RuntimeException("Non-numeric global value is not supported yet")
893+
}
894+
Globals.setC(i, toStagedNum(initValue))
895+
Globals.setS(i, toStagedSymbolicNum(initValue))
896+
}
897+
}
898+
886899
// call unreachable
887900
def unreachable(): Rep[Unit] = {
888901
"unreachable".reflectCtrlWith[Unit]()
@@ -905,6 +918,11 @@ trait StagedWasmEvaluator extends SAIOps {
905918

906919
// global read/write
907920
object Globals {
921+
def reserveSpace(size: Int): Rep[Unit] = {
922+
"global-reserve".reflectCtrlWith[Unit](size)
923+
"sym-global-reserve".reflectCtrlWith[Unit](size)
924+
}
925+
908926
def getC(i: Int): StagedConcreteNum = {
909927
module.globals(i).ty match {
910928
case GlobalType(NumType(I32Type), _) => I32C("global-get".reflectCtrlWith[Num](i))
@@ -1352,8 +1370,6 @@ trait StagedWasmCppGen extends CGenBase with CppSAICodeGenBase {
13521370
emit("Frames.set("); shallow(i); emit(", "); shallow(value); emit(");\n")
13531371
case Node(_, "sym-frame-set", List(i, s_value), _) =>
13541372
emit("SymFrames.set("); shallow(i); emit(", "); shallow(s_value); emit(");\n")
1355-
case Node(_, "global-set", List(i, value), _) =>
1356-
emit("Global.globalSet("); shallow(i); emit(", "); shallow(value); emit(");\n")
13571373
// Note: The following code is copied from the traverse of CppBackend.scala, try to avoid duplicated code
13581374
case n @ Node(f, "λ", (b: LMSBlock)::LMSConst(0)::rest, _) =>
13591375
// TODO: Is a leading block followed by 0 a hint for top function?
@@ -1410,7 +1426,17 @@ trait StagedWasmCppGen extends CGenBase with CppSAICodeGenBase {
14101426
case Node(_, "stack-size", _, _) =>
14111427
emit("Stack.size()")
14121428
case Node(_, "global-get", List(i), _) =>
1413-
emit("Global.globalGet("); shallow(i); emit(")")
1429+
emit("Globals.get("); shallow(i); emit(")")
1430+
case Node(_, "sym-global-get", List(i), _) =>
1431+
emit("SymGlobal.get("); shallow(i); emit(")")
1432+
case Node(_, "global-set", List(i, value), _) =>
1433+
emit("Globals.set("); shallow(i); emit(", "); shallow(value); emit(")")
1434+
case Node(_, "sym-global-set", List(i, s_value), _) =>
1435+
emit("SymGlobals.set("); shallow(i); emit(", "); shallow(s_value); emit(")")
1436+
case Node(_, "global-reserve", List(i), _) =>
1437+
emit("Globals.pushFrame("); shallow(i); emit(")")
1438+
case Node(_, "sym-global-reserve", List(i), _) =>
1439+
emit("SymGlobals.pushFrame("); shallow(i); emit(")")
14141440
case Node(_, "is-zero", List(num), _) =>
14151441
emit("(0 == "); shallow(num); emit(")")
14161442
case Node(_, "sym-is-zero", List(s_num), _) =>

src/test/scala/genwasym/TestStagedConcolicEval.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ class TestStagedConcolicEval extends FunSuite {
6262
testFileConcolicCpp("./benchmarks/wasm/staged/brtable_concolic.wat", exitByCoverage=true)
6363
}
6464

65+
test("simple-global-bug-finding-cov-concolic") {
66+
testFileConcolicCpp("./benchmarks/wasm/staged/simple_global.wat", Some("real_main"), exitByCoverage=true)
67+
}
68+
6569
test("return-poly - concrete") {
6670
testFileConcreteCpp("./benchmarks/wasm/staged/return_poly.wat", Some("$real_main"), expect=Some(List(42)))
6771
}

0 commit comments

Comments
 (0)