-
Notifications
You must be signed in to change notification settings - Fork 7
/
NeoSemanticDiffFuzzer.py
executable file
·149 lines (141 loc) · 4.63 KB
/
NeoSemanticDiffFuzzer.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
#!/usr/bin/env python3
"""
Semantic diff fuzzer for CPython against the neo boa python VM
"""
from neodiff import PyDiffGenerator
from neodiff import NeoDiff
from boa.compiler import Compiler
import random
import secrets
import struct
import shutil
from logzero import logger
# random.seed(1)
while True:
seed = secrets.randbelow(1 << 32)
# logger.info()
code = PyDiffGenerator.run(
seed=seed,
illegal=False,
target="py",
mayblock=False,
verbose=False,
evalcode=False,
)
# print(code)
code2 = """
def Main():
a = 1
b = 2
c = a+b
return [1234, c]
"""
exec(code)
python_ret = Main()
if not python_ret:
# logger.error('code is not valid python')
continue
else:
# logger.info('python got: {}'.format(python_ret))
pass
with open("/tmp/contract.py", "wb") as f:
f.write(code.encode("utf-8"))
try:
code = Compiler.load_and_save("/tmp/contract.py")
except:
# logger.error("python code couldn't compile")
continue
# print(code)
hashes, neo_ret = NeoDiff.run_vm(code, 2, NeoDiff.python)
# print(hashes)
if len(python_ret) == 2 and len(neo_ret) == 2:
if python_ret[0] != neo_ret[0]:
logger.info(
"{} vs. {} | python PyDiffGenerator.py -s {}".format(
python_ret, neo_ret, seed
)
)
with open("/tmp/contract.py", "a+") as f:
f.write("\n# python: {}\n# Neo: {}\n".format(python_ret, neo_ret))
shutil.copyfile("/tmp/contract.py", "SemanticDiffs/{}.py".format(seed))
# logger.info('DIFF')
else:
if python_ret[1] != neo_ret[1]:
if (
type(python_ret[1]) == str
and type(neo_ret[1]) == bytes
and python_ret[1].encode("utf-8") == neo_ret[1]
):
continue
if (
type(python_ret[1]) == bytes
and type(neo_ret[1]) == str
and python_ret[1] == neo_ret[1].encode("utf-8")
):
continue
if (
type(python_ret[1]) == bool
and python_ret[1] == True
and neo_ret[1] == b"\x01"
):
continue
if (
type(python_ret[1]) == bool
and python_ret[1] == False
and neo_ret[1] == b""
):
continue
if python_ret[1] == None and neo_ret[1] == b"":
continue
if (
type(python_ret[1]) == bool
and python_ret[1] == False
and neo_ret[1] == b"\x00"
):
continue
if (
type(python_ret[1]) == dict
and python_ret[1] == {}
and neo_ret[1] == b""
):
continue
if (
type(python_ret[1]) == int
and python_ret[1] == 0
and neo_ret[1] == b""
):
continue
if type(python_ret[1]) == float and int.from_bytes(
neo_ret[1], "little", signed=True
) == int(python_ret[1]):
continue
if (
type(python_ret[1]) == list
and python_ret[1] == []
and neo_ret[1] == b""
):
continue
if type(python_ret[1]) == list and len(python_ret[1]) > 0:
# can't detect these diffs
continue
if (
type(python_ret[1]) == int
and int.from_bytes(neo_ret[1], "little", signed=True)
== python_ret[1]
):
continue
logger.info(
"{} vs. {} | python PyDiffGenerator.py -s {}".format(
python_ret, neo_ret, seed
)
)
with open("/tmp/contract.py", "a+") as f:
f.write("\n# python: {}\n# Neo: {}\n".format(python_ret, neo_ret))
shutil.copyfile("/tmp/contract.py", "SemanticDiffs/{}.py".format(seed))
# input()
continue
else:
# logger.error('neo cannot execute code')
continue
# python.stdin.write(b'\x00\x00\x00')
# input()