-
Notifications
You must be signed in to change notification settings - Fork 476
/
Copy pathtest_examples.py
152 lines (114 loc) · 4.42 KB
/
test_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import unittest
from manticore.wasm import ManticoreWASM
from manticore.wasm.cli import wasm_main
from manticore.wasm.types import I32
from manticore.core.plugin import Plugin
from manticore.utils import config
from pathlib import Path
from collections import namedtuple
import glob
import os
def getchar(state, addr):
res = state.new_symbolic_value(32, "getchar_res")
state.constrain(res > 0)
state.constrain(res < 8)
return [res]
def arg_gen(state):
arg = state.new_symbolic_value(32, "collatz_arg")
state.constrain(arg > 3)
state.constrain(arg < 9)
state.constrain(arg % 2 == 0)
return [arg]
class CallCounterPlugin(Plugin):
def did_execute_instruction_callback(self, state, instruction):
with self.locked_context("counter", dict) as ctx:
val = ctx.setdefault(instruction.mnemonic, 0)
ctx[instruction.mnemonic] = val + 1
collatz_file = str(
Path(__file__).parent.parent.parent.joinpath("examples", "wasm", "collatz", "collatz.wasm")
)
if_check_file = str(
Path(__file__).parent.parent.parent.joinpath("examples", "wasm", "if_check", "if_check.wasm")
)
class TestCollatz(unittest.TestCase):
def test_getchar(self):
m = ManticoreWASM(collatz_file, env={"getchar": getchar})
m.invoke("main")
m.run()
results = []
for idx, val_list in enumerate(m.collect_returns()):
results.append(val_list[0][0])
self.assertEqual(sorted(results), [0, 1, 2, 5, 7, 8, 16])
def test_symbolic_args(self):
m = ManticoreWASM(collatz_file, env={})
m.invoke("collatz", arg_gen)
m.run()
results = []
for idx, val_list in enumerate(m.collect_returns()):
results.append(val_list[0][0])
self.assertEqual(sorted(results), [2, 3, 8])
m.finalize()
inputs = []
for fn in glob.glob(m.workspace + "/*.input"):
with open(fn, "r") as f:
raw = f.read().strip()
inputs.append(int(raw.replace("collatz_arg: ", "")))
self.assertEqual(sorted(inputs), [4, 6, 8])
def test_plugin(self):
def arg_gen(_state):
return [I32(1337)]
m = ManticoreWASM(collatz_file)
counter_plugin = CallCounterPlugin()
m.register_plugin(counter_plugin)
m.invoke("collatz", arg_gen)
m.run()
# counts = m.context.get("<class 'test_examples.CallCounterPlugin'>").get("counter")
counts = counter_plugin.context.get("counter")
self.assertEqual(counts["br_if"], 45)
self.assertEqual(counts["loop"], 44)
self.assertEqual(counts["i32.add"], 88)
results = []
for idx, val_list in enumerate(m.collect_returns()):
results.append(val_list[0][0])
self.assertEqual(sorted(results), [44])
def test_implicit_call(self):
m = ManticoreWASM(collatz_file)
counter_plugin = CallCounterPlugin()
m.register_plugin(counter_plugin)
m.collatz(lambda s: [I32(1337)])
counts = counter_plugin.context.get("counter")
self.assertEqual(counts["br_if"], 45)
self.assertEqual(counts["loop"], 44)
self.assertEqual(counts["i32.add"], 88)
results = []
for idx, val_list in enumerate(m.collect_returns()):
results.append(val_list[0][0])
self.assertEqual(sorted(results), [44])
m.collatz(lambda s: [I32(1338)])
results = []
for idx, val_list in enumerate(m.collect_returns()):
results.append(val_list[0][0])
self.assertEqual(sorted(results), [70])
def test_wasm_main(self):
m = wasm_main(
namedtuple("Args", ["argv", "workspace", "policy"])([collatz_file], "mcore_tmp", "ALL"),
None,
)
with open(os.path.join(m.workspace, "test_00000000.status")) as output:
data = output.read()
self.assertIn("Execution returned 0", data)
def getchar2(state):
res = state.new_symbolic_value(32, "getchar_res")
state.constrain(res > 0)
state.constrain(res < 256)
return [res]
class TestIfCheck(unittest.TestCase):
def test_getchar(self):
m = ManticoreWASM(if_check_file, env={"getchar": getchar2})
m.main()
results = []
for idx, val_list in enumerate(m.collect_returns()):
results.append(val_list[0][0])
self.assertEqual(sorted(results), [-1, 0])
if __name__ == "__main__":
unittest.main()