forked from SRI-CSL/OCCAM-Benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runbench.py
executable file
·381 lines (332 loc) · 14.1 KB
/
runbench.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python
#-------------------------------------------------------------------#
# Benchmark runner
#-------------------------------------------------------------------#
# NOTE: this script reads from the standard output/error to extract
# information about OCCAM and ROPgadget. Thus, any change in these
# tools might break this script.
import sys
import os
import os.path
import re
import datetime
import collections
import argparse as a
import pptable
import commands as cmd
verbose = False
# named tuple for occam stats
occam_stats = collections.namedtuple('OccamStats', 'funcs insts mem_insts')
def get_occam_home():
path = os.environ.get('OCCAM_HOME')
if path is None:
cmd.raise_error("need to set OCCAM_HOME environment variable")
return path
def get_ropgadget():
ropgadget = None
if 'ROPGADGET' in os.environ: ROPGADGET = os.environ ['ROPGADGET']
if not cmd.is_exec(ropgadget): ropgadget = cmd.which('ropgadget')
if not cmd.is_exec(ropgadget): ropgadget = cmd.which('ROPgadget.py')
if not cmd.is_exec(ropgadget):
raise IOError("Cannot find ropgadget")
return ropgadget
def get_benchmarks(name):
import json
with open(name, "r") as f:
benchs = list()
try:
d = json.load(f)
benchs = d['benchmarks']
except ValueError as msg:
print "Error: while decoding JSON file " + name
print msg
f.close()
return benchs
def read_occam_output(logfile):
b_funcs, b_insts, b_mem = 0, 0, 0
a_funcs, a_insts, a_mem = 0, 0, 0
before_done = False
with open(logfile, 'r') as fd:
for line in fd:
if re.search('^Statistics for \S* after specialization', line):
before_done = True
if re.search('Number of functions', line):
n = int(line.split()[0])
if not before_done:
b_funcs = n
else:
a_funcs = n
if re.search('Number of instructions', line):
n = int(line.split()[0])
if not before_done:
b_insts = n
else:
a_insts = n
if re.search('Statically unknown memory accesses', line):
n = int(line.split()[0])
if not before_done:
b_mem = n
else:
a_mem = n
before = occam_stats(funcs=b_funcs, insts=b_insts, mem_insts=b_mem)
after = occam_stats(funcs=a_funcs, insts=a_insts, mem_insts=a_mem)
fd.close()
return (before, after)
def read_ropgadget_output(logfile):
num_gadgets = 0
with open(logfile, 'r') as fd:
for line in fd:
if re.search('Unique gadgets found:', line):
num_gadgets = int(line.split()[3])
fd.close()
return num_gadgets
fd.close()
return num_gadgets
def pretty_printing_occam(results):
tab = []
for benchmark, before, after in results:
if before is None or after is None:
continue
func_red = 0.0
if (before.funcs > 0):
func_red = (1.0 - (float(after.funcs) / float(before.funcs))) * 100.0
insts_red = 0.0
if (before.insts > 0):
insts_red = (1.0 - (float(after.insts) / float(before.insts))) * 100.0
mem_red = 0.0
if (before.mem_insts > 0):
mem_red = (1.0 - (float(after.mem_insts) / float(before.mem_insts))) * 100.0
tab.append([benchmark,
before.funcs,
after.funcs,
float("{0:.2f}".format(func_red)),
before.insts,
after.insts,
float("{0:.2f}".format(insts_red)),
before.mem_insts,
after.mem_insts,
float("{0:.2f}".format(mem_red))])
table = [["Program", \
"B Fun", "A Fun", "% Fun Red", \
"B Ins", "A Ins", "% Ins Red", \
"B Mem Ins", "A Mem Ins", "% Mem Ins Red"]]
for row in tab:
table.append(row)
import pptable
out = sys.stdout
pptable.pprint_table(out,table)
def pretty_printing_ropgadget(results):
tab = []
for benchmark, \
total_before, total_after, \
jop_before, jop_after, \
sys_before, sys_after, \
rop_before, rop_after in results:
# total_red = 0.0
# if (total_before > 0):
# total_red = (1.0 - (float(total_after) / float(total_before))) * 100.0
rop_red = 0.0
if (rop_before > 0):
rop_red = (1.0 - (float(rop_after) / float(rop_before))) * 100.0
sys_red = 0.0
if (sys_before > 0):
sys_red = (1.0 - (float(sys_after) / float(sys_before))) * 100.0
jop_red = 0.0
if (jop_before > 0):
jop_red = (1.0 - (float(jop_after) / float(jop_before))) * 100.0
tab.append([benchmark,
#total_before,
#total_after,
#float("{0:.2f}".format(total_red)),
rop_before,
rop_after,
float("{0:.2f}".format(rop_red)),
sys_before,
sys_after,
float("{0:.2f}".format(sys_red)),
jop_before,
jop_after,
float("{0:.2f}".format(jop_red))])
table = [["Program", \
"B ROP", "A ROP", "% ROP Red", \
"B SYS", "A SYS", "% SYS Red", \
"B JOP", "A JOP", "% JOP Red"]]
for row in tab:
table.append(row)
out = sys.stdout
pptable.pprint_table(out,table)
def run_occam(dirname, execname, workdir, cpu, mem, slash_opts= []):
#benchmark_name = os.path.basename(os.path.normpath(dirname))
benchmark_name = execname
outfile = benchmark_name + ".occam.out"
errfile = benchmark_name + ".occam.err"
outfd = open(os.path.join(workdir, outfile), "w")
errfd = open(os.path.join(workdir, errfile), "w")
res_before, res_after = None, None
#1. Generate bitcode: run `make`
returncode,_,_,_ = cmd.run_limited_cmd(['make'], outfd, errfd, benchmark_name, dirname)
if returncode <> 0:
cmd.warning("something failed while running \"make\"" + benchmark_name + "\n" + \
"Read logs " + outfile + " and " + errfile)
else:
#2. Run slash (OCCAM) on it: `build.sh opts`
slash_args = ['./build.sh']
slash_args.extend(slash_opts)
print "Running slash with options " + str(slash_args)
returncode,_,_,_ = \
cmd.run_limited_cmd(slash_args, outfd, errfd, benchmark_name, dirname, cpu, mem)
if returncode <> 0:
cmd.warning("something failed while running \"" + ' '.join(slash_args) + \
"\"" + benchmark_name + "\n" + \
"Read logs " + outfile + " and " + errfile)
outfd.close()
errfd.close()
if returncode == 0:
#All OCCAM output is redirected to error
logfile = os.path.join(workdir, errfile)
(res_before, res_after) = read_occam_output(logfile)
return (benchmark_name, res_before, res_after)
#Pre: run_occam has been executed already and thus, there are two
#executables in dirname: the original one and the one after occam.
def run_ropgadget(dirname, execname, workdir, cpu, mem):
def run_ropgadget_on_pair(bench_name, prog_before, prog_after, opts, \
logfile, outfd, errfd):
outfd.seek(0,0)
errfd.seek(0,0)
res_before, res_after = None, None
args = [get_ropgadget(), '--binary', prog_before] + opts
returncode,_,_,_ = cmd.run_limited_cmd(args, outfd, errfd, bench_name)
# ROPgadget returns 1 if success
if returncode <> 1:
cmd.warning("something failed while running \"" + ' '.join(args) + "\"")
else:
res_before = read_ropgadget_output(logfile)
outfd.seek(0,0)
errfd.seek(0,0)
args = [get_ropgadget(), '--binary', prog_after] + opts
returncode,_,_,_ = cmd.run_limited_cmd(args, outfd, errfd, bench_name)
# ROPgadget returns 1 if success
if returncode <> 1:
cmd.warning("something failed while running \"" + ' '.join(args) + "\"")
else:
res_after = read_ropgadget_output(logfile)
return (res_before, res_after)
#benchname = os.path.basename(os.path.normpath(dirname))
benchname = execname
outfile = benchname + ".ropgadget.out"
errfile = benchname + ".ropgadget.err"
outfd = open(os.path.join(workdir, outfile), "w")
errfd = open(os.path.join(workdir, errfile), "w")
prog_before = os.path.join(dirname, benchname + "_orig")
prog_after = os.path.join(dirname, benchname + "_slashed")
total_before, total_after = None, None
rop_before, rop_after = None, None
jop_before, jop_after = None, None
sys_before, sys_after = None, None
if os.path.exists(prog_before) and os.path.exists(prog_after):
# total gadgets: ropgadget --binary prog
# only jop: ropgadget --binary --norop --nosys
# only sys: ropgadget --binary --nojop --norop
# only rop: ropgadget --binary --nojop --nosys
logfile = os.path.join(workdir, outfile)
opts = []
(total_before, total_after) = run_ropgadget_on_pair(benchname, prog_before, prog_after, \
opts, logfile, outfd, errfd)
opts = ['--norop','--nosys']
(jop_before, jop_after) = run_ropgadget_on_pair(benchname, prog_before, prog_after, \
opts, logfile, outfd, errfd)
opts = ['--nojop', '--norop']
(sys_before, sys_after) = run_ropgadget_on_pair(benchname, prog_before, prog_after, \
opts, logfile, outfd, errfd)
opts = ['--nojop', '--nosys']
(rop_before, rop_after) = run_ropgadget_on_pair(benchname, prog_before, prog_after, \
opts, logfile, outfd, errfd)
else:
if not os.path.exists(prog_before):
cmd.warning(prog_before + " does not exist")
if not os.path.exists(prog_after):
cmd.warning(prog_after + " does not exist")
outfd.close()
errfd.close()
return (benchname, \
total_before, total_after, \
jop_before, jop_after, \
sys_before, sys_after, \
rop_before, rop_after)
def parse_opt (argv):
p = a.ArgumentParser (description='Benchmark Runner for OCCAM')
p.add_argument ('--save-temps', '--keep-temps',
dest="save_temps",
help="Do not delete temporary files",
action="store_true", default=False)
p.add_argument ('--temp-dir', dest='temp_dir', metavar='DIR',
help="Temporary directory", default=None)
p.add_argument ('--cpu', type=int, dest='cpu', metavar='SEC',
help='CPU time limit (seconds)', default=-1)
p.add_argument ('--mem', type=int, dest='mem', metavar='MB',
help='MEM limit (MB)', default=-1)
p.add_argument ('--sets', type=str,
help="List of .set files (separated by comma)",
dest='benchmark_sets', default="")
p.add_argument ('--slash-opts', type=str,
help="Options passed to slash.py (options separated by comma)",
dest='slash_opts', default="")
p.add_argument ('--rop', help="Generate statistics about ROP/SYS/JOP gadgets (not maintained anymore)",
dest='rop', default=False, action="store_true")
args = p.parse_args (argv)
return args
def main (argv):
args = parse_opt (argv[1:])
workdir = cmd.create_work_dir(args.temp_dir, args.save_temps)
sets = []
occam_opts = []
occam_tab = list()
ropgadget_tab = list()
for slash_opt in args.slash_opts.split(","):
occam_opts += [slash_opt]
for benchmark_set in args.benchmark_sets.split(","):
if benchmark_set is not "":
sets += [benchmark_set]
if not sets:
print "Warning: you need to choose a benchmark set. Use option --sets"
return 0
if args.rop:
print "Warning: option --rop is not maintained anymore."
args.rop = False
dt = datetime.datetime.now ().strftime ('%d/%m/%Y %H:%M:%S')
print "[" + dt + "] " + "STARTED runbench"
for s in sets:
for t in get_benchmarks(s):
if t['enabled'] == 'false':
continue
dirname = t['dirname']
if not os.path.isdir(dirname):
dirname = os.path.join(get_occam_home(), dirname)
if not os.path.isdir(dirname):
cmd.raise_error(t['dirname'] + " is not a directory")
execname = t['execname']
res = run_occam(dirname, execname, workdir, args.cpu, args.mem, occam_opts)
occam_tab.append(res)
if args.rop:
res = run_ropgadget(dirname, execname, workdir, args.cpu, args.mem)
ropgadget_tab.append(res)
dt = datetime.datetime.now ().strftime ('%d/%m/%Y %H:%M:%S')
print "[" + dt + "] " + "FINISHED runbench\n"
print "\nProgram Reduction: (B:before and A:after OCCAM with " + \
' '.join(occam_opts) + ")\n"
pretty_printing_occam(occam_tab)
if args.rop:
print "\nGadget Reduction: (B:before and A:after OCCAM with " + \
' '.join(occam_opts) + ")\n"
pretty_printing_ropgadget(ropgadget_tab)
return 0
if __name__ == '__main__':
res = None
try:
res = main(sys.argv)
except Exception as e:
print e
except KeyboardInterrupt:
pass
finally:
sys.exit(res)