-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuzz_sa_eval.py
executable file
·344 lines (272 loc) · 10.8 KB
/
fuzz_sa_eval.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
#!/usr/bin/python3
import argparse
import os
import re
import signal
import subprocess
import sys
import time
from config import *
CSMITH_ERROR = 0
EVAL_NUM = 0
TIMEOUT_NUM = 0
CRASH_NUM = 0
ANALYZER_TIMEOUT = "timeout 60 "
def read_value_from_file(file, match):
'''
extract the group(1) of searched pattern
'''
with open(file, 'r') as f:
pattern = re.compile(r''+match)
for line in f.readlines():
seed = pattern.search(line)
if seed:
return seed.group(1)
return ""
def save_crashing_file(num):
'''
save the cfile crashing analyzer
'''
global CRASH_NUM
os.system("mv test_%s.c crash%s.c " % (num, CRASH_NUM))
os.system("mv instrument_test_%s.c instrument_crash%s.c " % (num, CRASH_NUM))
os.system("mv instrument_test_%s.log instrument_crash%s.log " % (num, CRASH_NUM))
def do_preprocess(file_path, analyzer):
subprocess.run("%s %s -- -I %s "%(CFE, file_path, CSMITH_HEADER),
stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL, shell=True, check=True)
new_lines = []
with open(file_path, "r") as f:
lines = f.readlines()
if analyzer == "gcc":
new_lines.append("#include <stdbool.h>\n")
new_lines.append("void __analyzer_eval(int a){}\n")
elif analyzer == "clang":
new_lines.append("#include <stdbool.h>\n")
new_lines.append("void clang_analyzer_eval(int a){}\n")
for line in lines:
new_lines.append(line)
with open(file_path, "w") as f:
f.writelines(new_lines)
instrument_file = "instrument_" + file_path
if analyzer == "gcc":
subprocess.run("%s gcc %s -- -I %s > %s"%(INSTRUMENT_TOOL, file_path, CSMITH_HEADER, instrument_file),
stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL, shell=True , check=True )
elif analyzer == "clang":
subprocess.run("%s clang %s -- -I %s > %s"%(INSTRUMENT_TOOL, file_path, CSMITH_HEADER, instrument_file),
stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL, shell=True , check=True )
return instrument_file
# csmith --> test_0.c --> instrument_test_0.c -->instrument_test_0.log
def analyze_with_gcc(num, optimization_level, args):
'''
use gcc to analyze Csmith-generated c program
'''
global TIMEOUT_NUM
global CRASH_NUM
report_file = "instrument_test_%s.log" % num
cfile = "test_%s.c" % num
instrument_cfile = do_preprocess(cfile, args.analyzer)
ret = os.system(ANALYZER_TIMEOUT + GCC_ANALYZER + " -O" + optimization_level +
" -c -I " + CSMITH_HEADER + " " + instrument_cfile + " > " + report_file + " 2>&1")
ret >>= 8
print("gcc analyzer ret: " + str(ret))
if ret == 124:
TIMEOUT_NUM += 1
print(ANALYZER_TIMEOUT)
clean_gcc_products(num, args.saveProducts)
return None
elif ret != 0:
# TODO: 该处的逻辑是否有问题?返回值既不是 0 也不是 124 一定是 analyzer crash 吗?
save_crashing_file(num)
clean_gcc_products(num, args.saveProducts)
CRASH_NUM += 1
return None
return report_file
def process_gcc_report(num, report_file, args):
'''
check whether the given report contains the target error
'''
global EVAL_NUM
save_products_flag = args.saveProducts
if not os.path.exists(report_file):
print("report does not exist: " + str(report_file))
clean_gcc_products(num, save_products_flag)
return
check_cmd = 'grep "warning: FALSE"'
ret = os.system(check_cmd + " < " + report_file)
ret >>= 8
if ret == 0:
os.system("mv instrument_test_%s.c instrument_eval_%s.c " % (num, EVAL_NUM))
os.system("mv test_%s.c eval_%s.c " % (num, EVAL_NUM))
os.system("mv instrument_test_%s.log instrument_eval_%s.log " % (num, EVAL_NUM))
EVAL_NUM += 1
clean_gcc_products(num, save_products_flag)
def clean_gcc_products(num, save_products_flag):
if not save_products_flag:
os.system("rm -f instrument_test_%s* test_%s*" % (num, num))
def analyze_with_clang(num, optimization_level, args):
'''
use clang to analyze Csmith-generated c program
'''
global CRASH_NUM
global TIMEOUT_NUM
report_file = "instrument_test_%s.log" % num
cfile = "test_%s.c" % num
instrument_cfile = do_preprocess(cfile, args.analyzer)
ret = os.system(ANALYZER_TIMEOUT + CLANG + CLANG_DEBUG_OPTIONS + " -O" + optimization_level +
" -c -I " + CSMITH_HEADER + " " + instrument_cfile + " > " + report_file + " 2>&1")
ret >>= 8
print("clang ret: " + str(ret))
if ret == 124:
TIMEOUT_NUM += 1
print(ANALYZER_TIMEOUT)
clean_clang_products(num, args.saveProducts)
return None
elif ret != 0:
# TODO: 该处的逻辑是否有问题?返回值既不是 0 也不是 124 一定是 analyzer crash 吗?
save_crashing_file(num)
clean_clang_products(num, args.saveProducts)
return None
return report_file
def process_clang_report(num, report_file, args):
'''
check whether the given report contains the target error
'''
global EVAL_NUM
save_products_flag = args.saveProducts
if not os.path.exists(report_file):
print("report does not exist: " + str(report_file))
clean_clang_products(num, save_products_flag)
return
check_cmd = 'grep "warning: FALSE"'
ret = os.system(check_cmd + " < " + report_file)
ret >> 8
if ret == 0:
os.system("mv instrument_test_%s.c instrument_eval_%s.c " % (num, EVAL_NUM))
os.system("mv test_%s.c eval_%s.c " % (num, EVAL_NUM))
os.system("mv instrument_test_%s.log instrument_eval_%s.log " % (num, EVAL_NUM))
EVAL_NUM += 1
clean_clang_products(num, save_products_flag)
def clean_clang_products(num, save_products_flag):
if not save_products_flag:
os.system("rm -f test_%s* instrument_test_%s*" % (num, num))
def generate_code(num, args):
'''
generate wanted-size code with csmith
'''
cfile = "test_%s.c" % num
os.system("rm -f %s" % cfile)
while True:
cmd = "csmith %s --output %s" % (CSMITH_USER_OPTIONS, cfile)
os.system(cmd)
file_size = os.stat(cfile).st_size
seed = read_value_from_file(cfile, 'Seed:\s+(\d+)')
# print("generated a file, Seed: " + seed)
if len(seed) <= 0:
print("Random program %s has no seed information!\n" % cfile)
return None
ctrl_max = args.max
if ctrl_max:
print("ctrl_max %d"%ctrl_max)
if file_size < ctrl_max and file_size > int(MIN_PROGRAM_SIZE):
print("succ generated a file whose size is larger than %s and smaller than %s, seed %s: " % (
MIN_PROGRAM_SIZE, ctrl_max, seed))
break
else:
if file_size > int(MIN_PROGRAM_SIZE):
print("succ generated a file whose size is larger than %s, seed %s: " % (
MIN_PROGRAM_SIZE, seed))
break
print(cfile + ' ' + seed)
return cfile
def gcc_test_one(num, args):
global CSMITH_ERROR
cfile = generate_code(num, args)
if cfile:
report_file = analyze_with_gcc(num, str(args.optimize), args)
if report_file is not None:
process_gcc_report(num, report_file, args)
else:
CSMITH_ERROR += 1
def clang_test_one(num, args):
global CSMITH_ERROR
cfile = generate_code(num, args)
if cfile:
report_file = analyze_with_clang(num, str(args.optimize), args)
if report_file is not None:
process_clang_report(num, report_file, args)
else:
CSMITH_ERROR += 1
def get_analyzer_version(analyzer):
res = subprocess.run([analyzer, "-v"], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, encoding='utf-8')
return res.stderr
def write_script_run_args(args):
'''
write doen script running args
'''
with open('script_run_args.info', 'w') as f:
f.write("Time: %s\n" % str(time.strftime(
"%Y-%m-%d %H:%M:%S %a", time.localtime())))
f.write("\nTimeout: %s\n" % ANALYZER_TIMEOUT)
f.write("\nCsmit options: \n" + CSMITH_USER_OPTIONS)
f.write("\n\nargs:\n" + str(args))
f.write("\n\nanalyzer info:\n" + get_analyzer_version(args.analyzer))
# if args.compiler == "clang":
# f.write("\n\nanalyzer options:\n" + CLANG_OPTIONS)
def write_fuzzing_result(stop_message):
'''
write down short statistic of fuzzing
'''
with open('fuzzing_result.txt', 'w') as f:
f.write("STOP: %s\n" % stop_message)
f.write("EVAL_NUM: %s\n" % EVAL_NUM)
f.write("\nTIMEOUT_NUM: %s\n" % TIMEOUT_NUM)
f.write("\nCRASH_NUM: %s\n" % CRASH_NUM)
def handle_args():
parser = argparse.ArgumentParser(
description="fuzz static analyzer with csmith-generated c program")
group = parser.add_mutually_exclusive_group()
group.add_argument("-v", "--verbose", action="store_true")
group.add_argument("-q", "--quiet", action="store_true")
parser.add_argument('analyzer', type=str, help='choose a analyzer')
parser.add_argument("-o", '--optimize', type=int,
choices={0, 1, 2, 3}, default=0, help='choose an optimize level')
parser.add_argument('num', type=int, help='number of generated c programs')
parser.add_argument("-m", "--max", type=int,
help="set the max size of generated c programs")
parser.add_argument("--min", type=int, default=MIN_PROGRAM_SIZE,
help="set the min size of generated c programs is no more than %s" % MIN_PROGRAM_SIZE)
parser.add_argument("-s", "--saveProducts", action="store_true",
help="do not delete generated files in analyzing process")
args = parser.parse_args()
if args.max and args.max <= args.min:
sys.stderr.write("max size should larger than min size\n")
exit(-1)
return args
def bye(signum, frame):
'''
responsible func of signal.SIGINT and signal.SIGTERM
'''
# print("Bye bye")
write_fuzzing_result("Interrupted")
exit(0)
def main():
args = handle_args()
print(args)
write_script_run_args(args)
target_analyzer = args.analyzer
num = args.num
signal.signal(signal.SIGINT, bye)
signal.signal(signal.SIGTERM, bye)
# signal.signal(signal.SIGKILL, bye)
if target_analyzer == "gcc":
for i in range(int(num)):
gcc_test_one(i, args)
elif target_analyzer == 'clang':
for i in range(int(num)):
clang_test_one(i, args)
else:
print("target analyzer: %s is not supported" % target_analyzer)
write_fuzzing_result("Over")
if __name__ == "__main__":
main()