forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathmodfuzz.py
193 lines (147 loc) · 4.36 KB
/
modfuzz.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python
# To use:
# 1) Update the 'decls' list below with your fuzzing configuration.
# 2) Run with the clang binary as the command-line argument.
from __future__ importabsolute_import, division, print_function
importrandom
importsubprocess
importsys
importos
clang=sys.argv[1]
none_opts=0.3
classDecl(object):
def__init__(self, text, depends=[], provides=[], conflicts=[]):
self.text=text
self.depends=depends
self.provides=provides
self.conflicts=conflicts
defvalid(self, model):
foriinself.depends:
ifinotinmodel.decls:
returnFalse
foriinself.conflicts:
ifiinmodel.decls:
returnFalse
returnTrue
defapply(self, model, name):
foriinself.provides:
model.decls[i] =True
model.source+=self.text% {"name": name}
decls= [
Decl("struct X { int n; };\n", provides=["X"], conflicts=["X"]),
Decl('static_assert(X{.n=1}.n == 1, "");\n', depends=["X"]),
Decl("X %(name)s;\n", depends=["X"]),
]
classFS(object):
def__init__(self):
self.fs= {}
self.prevfs= {}
defwrite(self, path, contents):
self.fs[path] =contents
defdone(self):
forf, sinself.fs.items():
ifself.prevfs.get(f) !=s:
f=file(f, "w")
f.write(s)
f.close()
forfinself.prevfs:
iffnotinself.fs:
os.remove(f)
self.prevfs, self.fs=self.fs, {}
fs=FS()
classCodeModel(object):
def__init__(self):
self.source=""
self.modules= {}
self.decls= {}
self.i=0
defmake_name(self):
self.i+=1
return"n"+str(self.i)
deffails(self):
fs.write(
"module.modulemap",
"".join(
'module %s { header "%s.h" export * }\n'% (m, m)
forminself.modules.keys()
),
)
form, (s, _) inself.modules.items():
fs.write("%s.h"%m, s)
fs.write("main.cc", self.source)
fs.done()
return (
subprocess.call(
[clang, "-std=c++11", "-c", "-fmodules", "main.cc", "-o", "/dev/null"]
)
!=0
)
defgenerate():
model=CodeModel()
m= []
try:
fordinmutations(model):
d(model)
m.append(d)
ifnotmodel.fails():
return
exceptKeyboardInterrupt:
print()
returnTrue
sys.stdout.write("\nReducing:\n")
sys.stdout.flush()
try:
whileTrue:
assertm, "got a failure with no steps; broken clang binary?"
i=random.choice(list(range(len(m))))
x=m[0:i] +m[i+1 :]
m2=CodeModel()
fordinx:
d(m2)
ifm2.fails():
m=x
model=m2
else:
sys.stdout.write(".")
sys.stdout.flush()
exceptKeyboardInterrupt:
# FIXME: Clean out output directory first.
model.fails()
returnmodel
defchoose(options):
whileTrue:
i=int(random.uniform(0, len(options) +none_opts))
ifi>=len(options):
break
yieldoptions[i]
defmutations(model):
options= [create_module, add_top_level_decl]
foroptinchoose(options):
yieldopt(model, options)
defcreate_module(model, options):
n=model.make_name()
defgo(model):
model.modules[n] = (model.source, model.decls)
(model.source, model.decls) = ("", {})
options+= [lambdamodel, options: add_import(model, options, n)]
returngo
defadd_top_level_decl(model, options):
n=model.make_name()
d=random.choice([declfordeclindeclsifdecl.valid(model)])
defgo(model):
ifnotd.valid(model):
return
d.apply(model, n)
returngo
defadd_import(model, options, module_name):
defgo(model):
ifmodule_nameinmodel.modules:
model.source+='#include "%s.h"\n'%module_name
model.decls.update(model.modules[module_name][1])
returngo
sys.stdout.write("Finding bug: ")
whileTrue:
ifgenerate():
break
sys.stdout.write(".")
sys.stdout.flush()