-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunner.py
357 lines (321 loc) · 13.3 KB
/
runner.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import subprocess
import tempfile
import shutil
import glob
import stat
import os
import xml.etree.ElementTree as ET
def stack_from_asan_log(data):
stack = []
for line in data.split("\n"):
k = line.find("#")
if k != -1:
k = line.find("in", k)
stack.append(line[k+2:].rstrip())
return stack
def stack_frame_from_xml(xml_stack):
st = []
frames = xml_stack.findall('frame')
for f in frames:
lineno = None
directory = None
filename = None
lineno_xml = f.find('line')
fullpath = None
if lineno_xml != None:
lineno = int(lineno_xml.text)
directory_xml = f.find('dir')
if directory_xml != None:
directory = directory_xml.text
filename_xml = f.find('file')
if filename_xml != None:
filename = filename_xml.text
if directory != None and file != None:
fullpath = directory + "/" + filename
else:
obj_xml = f.find('obj')
assert obj_xml != None
fullpath = obj_xml.text
if lineno == None:
ip_xml = f.find('ip')
assert ip_xml != None
lineno = int(ip_xml.text, 16)
if fullpath != None and lineno != None:
fullid = fullpath + ":" + str(lineno)
st.append(fullid)
return st
def stack_from_xml(data):
root = None
try:
root = ET.fromstring(data)
# Sometimes, Valgrind got interrupted?
except ET.ParseError:
return None
#root = tree.getroot()
# Find the first error and get that stack.
errors = {}
xml_errors = root.findall('error')
for e in xml_errors:
u = int(e.find('unique').text, 16)
s = stack_frame_from_xml(e.find('stack'))
if s != None:
errors[u] = s
# Find the fatal_error, if present, and get that stack.
xml_fatal_error = root.find('fatal_signal')
fatal_error = None
if xml_fatal_error != None:
fatal_error = stack_frame_from_xml(xml_fatal_error.find('stack'))
# If we have a fatal_error stack, return that. Otherwise, return
# the first_error stack.
if fatal_error != None:
return fatal_error
else:
ekeys = errors.keys()
ekeys.sort()
if len(ekeys) == 0:
return None
else:
return errors[ekeys[-1]]
def run2_asan(run_tasks):
"""
Take a program and a list of arguments and stdins, run them
"""
tempdir = tempfile.mkdtemp()
cmdlines = []
idx_map = {}
idx = 0
for (program,(arguments,stdin_f),outdata) in run_tasks:
real_program = program
if program[:7] == "file://":
p = program[7:]
pbase = os.path.basename(p)
shutil.copy(p, "{0}/{1}-{2}".format(tempdir, pbase, idx))
real_program = "/sandbox/{}-{}".format(pbase, idx)
converted_arguments = []
for a in arguments:
if a[:7] == "file://":
b = a[7:]
bbase = os.path.basename(b)
shutil.copy(b, "{0}/{1}{2}".format(tempdir, bbase, idx))
converted_arguments.append("/sandbox/{1}{2}".format(bbase, idx))
else:
converted_arguments.append(a)
if stdin_f != None:
if stdin_f[:7] == "file://":
b = stdin_f[7:]
bbase = os.path.basename(b)
shutil.copy(b, "{0}/{1}{2}".format(tempdir, bbase, idx))
stdin_f = "/sandbox/{0}{1}".format(bbase, idx)
exec_c = []
logname = "/sandbox/out{i}".format(i=idx)
exec_c.append(real_program)
for a in converted_arguments:
exec_c.append(a)
cmd = " ".join(exec_c)
if stdin_f != None:
cmd = "{command} < {stdinput}".format(command=cmd, stdinput=stdin_f)
idx_map[idx] = outdata
env = "UBSAN_OPTIONS=print_stacktrace=1 ASAN_OPTIONS=detect_leaks=false,log_path={log}".format(log=logname)
timeout_cmd = "{e} timeout 45s {c}".format(e=env,c=cmd)
cmdlines.append(timeout_cmd)
idx = idx + 1
runsh = open("{}/run.sh".format(tempdir), "w")
runsh.write("#!/bin/bash\n")
runsh.write("echo \"started\" > /sandbox/started\n")
for l in cmdlines:
runsh.write("{}\n".format(l))
runsh.write("echo \"finished\" > /sandbox/finished\n")
runsh.write("exit 0\n")
runsh.close()
os.chmod("{}/run.sh".format(tempdir), stat.S_IREAD|stat.S_IEXEC)
subprocess.call(["/bin/sync"])
docker_cmdline = ["docker", "run", "--rm", "--user", "1000", "-m", "1024m"]
docker_cmdline.append("-v")
docker_cmdline.append("{}:/sandbox".format(tempdir))
docker_cmdline.append("ubuntu:16.04")
docker_cmdline.append("/sandbox/run.sh")
while True:
null_out = open('/dev/null', 'w')
result = subprocess.call(docker_cmdline, stdout=null_out, stderr=null_out)
#result = subprocess.call(docker_cmdline)
if result == 0:
if os.path.exists("{}/started".format(tempdir)):
r = open("{}/started".format(tempdir), "r").read()
if r.find("started") != -1:
result = 0
else:
result = 1
else:
result = 1
if os.path.exists("{}/finished".format(tempdir)):
r = open("{}/finished".format(tempdir), "r").read()
if r.find("finished") != -1:
result = 0
else:
result = 1
else:
result = 1
if result == 0:
break
# Gather up all the produced ASAN files and return them
datas = []
for i in range(0, idx):
u = glob.glob("{tdir}/out{index}.*".format(tdir=tempdir,index=i))
if len(u) > 0:
data = open(u[0], 'r').read()
else:
data = ""
o = idx_map[i]
o['stack'] = data
datas.append(o)
shutil.rmtree(tempdir)
return datas
def run2(program, arguments_list):
"""
Take program and a list of arguments and stdins, run them ALL
sequentially under the SAME docker container.
"""
# Create a temporary directory where job-related data will be stored.
tempdir = tempfile.mkdtemp()
# First, we need to go through and find file URIs in 'arguments', and
# make them available in the Docker container.
real_program = program
if program[:7] == "file://":
p = program[7:]
pbase = os.path.basename(p)
shutil.copy(p, "{0}/{1}".format(tempdir, pbase))
real_program = "/sandbox/{}".format(pbase)
cmdlines = []
idx = 0
for (arguments,stdin_f) in arguments_list:
converted_arguments = []
for a in arguments:
if a[:7] == "file://":
b = a[7:]
bbase = os.path.basename(b)
shutil.copy(b, "{0}/{1}{2}".format(tempdir, bbase, idx))
converted_arguments.append("/sandbox/{1}{2}".format(bbase, idx))
else:
converted_arguments.append(a)
if stdin_f != None:
if stdin_f[:7] == "file://":
b = stdin_f[7:]
bbase = os.path.basename(b)
shutil.copy(b, "{0}/{1}{2}".format(tempdir, bbase, idx))
stdin_f = "/sandbox/{0}{1}".format(bbase, idx)
valgrind_cmdline = ["/usr/bin/valgrind", "--xml=yes", "--xml-file=/sandbox/out{}.xml".format(idx)]
valgrind_cmdline.append(real_program)
for a in converted_arguments:
valgrind_cmdline.append(a)
valgrind_cmd = " ".join(valgrind_cmdline)
if stdin_f != None:
valgrind_cmd = "{0} < {1}".format(valgrind_cmd, stdin_f)
timeout_cmd = "timeout 5m {}".format(valgrind_cmd)
cmdlines.append(timeout_cmd)
idx = idx + 1
# Write all the cmdlines out into the run.sh file.
runsh = open("{}/run.sh".format(tempdir), "w")
runsh.write("#!/bin/bash\n")
for l in cmdlines:
runsh.write("{}\n".format(l))
runsh.write("exit 0\n")
runsh.close()
os.chmod("{}/run.sh".format(tempdir), stat.S_IREAD|stat.S_IEXEC)
# Run run.sh under docker
docker_cmdline = ["docker", "run", "--rm", "--user", "1000", "-m", "1024m"]
docker_cmdline.append("-v")
docker_cmdline.append("{}:/sandbox".format(tempdir))
docker_cmdline.append("grinder")
docker_cmdline.append("/sandbox/run.sh")
while True:
null_out = open('/dev/null', 'w')
result = subprocess.call(docker_cmdline) #, stdout=null_out, stderr=null_out)
if result == 0:
break
# Gather up all the produced XML files and return them in order.
datas = []
for i in range(0, idx):
data = open("{0}/out{1}.xml".format(tempdir, i), 'r').read()
datas.append(data)
shutil.rmtree(tempdir)
return datas
def run(program, arguments, stdin_f=None):
"""
Take program and arguments, run under valgrind. Parse the error
context if present, return it. If stdin is supplied, give that as
stdin to the child process. Return a tuple of (boolean, string)
where the first boolean is whether or not there was a fault, and
the second string is the call stack.
"""
# Create a temporary directory where job-related data will be stored.
tempdir = tempfile.mkdtemp()
# First, we need to go through and find file URIs in 'arguments', and
# make them available in the Docker container.
real_program = program
if program[:7] == "file://":
p = program[7:]
pbase = os.path.basename(p)
shutil.copy(p, "{0}/{1}".format(tempdir, pbase))
real_program = "/sandbox/{}".format(pbase)
converted_arguments = []
for a in arguments:
if a[:7] == "file://":
b = a[7:]
bbase = os.path.basename(b)
shutil.copy(b, "{0}/{1}".format(tempdir, bbase))
converted_arguments.append("/sandbox/{1}".format(bbase))
else:
converted_arguments.append(a)
if stdin_f != None:
if stdin_f[:7] == "file://":
b = stdin_f[7:]
bbase = os.path.basename(b)
shutil.copy(b, "{0}/{1}".format(tempdir, bbase))
stdin_f = "/sandbox/{0}".format(bbase)
# Then, create the Docker command line.
docker_cmdline = ["docker", "run", "--rm", "--user", "1000"]
docker_cmdline.append("-v")
docker_cmdline.append("{}:/sandbox".format(tempdir))
docker_cmdline.append("grinder")
# Then, create the Valgrind command line.
valgrind_cmdline = ["/usr/bin/valgrind", "--xml=yes", "--xml-file=/sandbox/out.xml"]
valgrind_cmdline.append(real_program)
for a in converted_arguments:
valgrind_cmdline.append(a)
valgrind_cmd = " ".join(valgrind_cmdline)
if stdin_f != None:
valgrind_cmd = "{0} < {1}".format(valgrind_cmd, stdin_f)
timeout_cmd = "timeout 5m {}".format(valgrind_cmd)
runsh = open("{}/run.sh".format(tempdir), "w")
runsh.write("#!/bin/bash\n{}\n".format(timeout_cmd))
runsh.close()
os.chmod("{}/run.sh".format(tempdir), stat.S_IREAD|stat.S_IEXEC)
# Then, jam them together and run them.
docker_cmdline.append("/sandbox/run.sh")
null_out = open('/dev/null', 'w')
#subprocess.call(docker_cmdline, stdout=null_out, stderr=null_out)
#subprocess.call(docker_cmdline, stdout=null_out)
print docker_cmdline
subprocess.call(docker_cmdline)
# Then, read the XML data file output.
data = open("{}/out.xml".format(tempdir), 'r').read()
# Tear down the temporary directory structure now that we don't need it.
shutil.rmtree(tempdir)
return data
if __name__ == '__main__':
programs = []
programs.append('file:///home/andrew/code/mesos-test-case-runner/cxxfilt-fuzzing/bins/2ea53e003163338a403d5afbb2046cafb8f3abe9/bin/c++filt')
programs.append('file:///home/andrew/code/mesos-test-case-runner/cxxfilt-fuzzing/bins/2ea53e003163338a403d5afbb2046cafb8f3abe9/bin/c++filt')
inputs = []
inputs.append(([],'file:///home/andrew/code/mesos-test-case-runner/cxxfilt-fuzzing/inputs/afl/2/outdir/crashes/id:000541,sig:11,src:007824,op:ext_AO,pos:38'))
inputs.append(([],'file:///home/andrew/code/mesos-test-case-runner/cxxfilt-fuzzing/inputs/afl/2/outdir/crashes/id:000543,sig:11,src:007835,op:ext_AO,pos:38'))
stuff = []
stuff.append({})
stuff.append({})
tasks = zip(programs,inputs,stuff)
rv = run2_asan(tasks)
print rv
# Some tests.
#rv = run("file:///home/andrew/code/mesos-test-case-runner/cxxfilt-fuzzing/bins/eee926f28e8745dcd03adcb1113f3e4a7b79b1e5/bin/c++filt", [], "file:///home//andrew/code/mesos-test-case-runner/cxxfilt-fuzzing/inputs/afl/8/outdir/crashes/id:000417,sig:11,src:005525,op:havoc,rep:8")
#rv = run2("file:///home/andrew/code/mesos-test-case-runner/cxxfilt-fuzzing/bins/eee926f28e8745dcd03adcb1113f3e4a7b79b1e5/bin/c++filt", [([], "file:///home//andrew/code/mesos-test-case-runner/cxxfilt-fuzzing/inputs/afl/8/outdir/crashes/id:000417,sig:11,src:005525,op:havoc,rep:8"),([],"file:///home//andrew/code/mesos-test-case-runner/cxxfilt-fuzzing/inputs/afl/8/outdir/crashes/id:000410,sig:11,src:005215,op:arith8,pos:29,val:-21"),([],"file:///home//andrew/code/mesos-test-case-runner/cxxfilt-fuzzing/inputs/afl/8/outdir/crashes/id:000413,sig:11,src:005294,op:havoc,rep:4")])
#print rv