Skip to content

Commit b2e13bc

Browse files
committed
implement ID range output in id-delta-cov file in --cover-corpus and --coverage-at-exit modes
1 parent c6dbd22 commit b2e13bc

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

afl-cov

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def process_afl_test_cases(cargs):
170170

171171
cov_paths['diff'] = "%s/%s" % \
172172
(cov_paths['diff_dir'], os.path.basename(f))
173+
id_range_update(f, cov_paths)
173174

174175
### execute the command to generate code coverage stats
175176
### for the current AFL test case file
@@ -191,6 +192,7 @@ def process_afl_test_cases(cargs):
191192
do_coverage = True
192193

193194
if do_coverage and not cargs.coverage_at_exit:
195+
194196
### generate the code coverage stats for this test case
195197
lcov_gen_coverage(cov_paths, cargs)
196198

@@ -199,6 +201,10 @@ def process_afl_test_cases(cargs):
199201
coverage_diff(curr_cycle, fuzz_dir, cov_paths, f,
200202
cov, cargs)
201203

204+
if cargs.cover_corpus:
205+
### reset the range values
206+
cov_paths['id_min'] = cov_paths['id_max'] = -1
207+
202208
if cargs.lcov_web_all:
203209
gen_web_cov_report(fuzz_dir, cov_paths, cargs)
204210

@@ -265,16 +271,39 @@ def process_afl_test_cases(cargs):
265271

266272
return rv
267273

274+
def id_range_update(afl_file, cov_paths):
275+
276+
id_val = int(os.path.basename(afl_file).split(',')[0].split(':')[1])
277+
278+
if cov_paths['id_min'] == -1:
279+
cov_paths['id_min'] = id_val
280+
elif id_val < cov_paths['id_min']:
281+
cov_paths['id_min'] = id_val
282+
283+
if cov_paths['id_max'] == -1:
284+
cov_paths['id_max'] = id_val
285+
elif id_val > cov_paths['id_max']:
286+
cov_paths['id_max'] = id_val
287+
288+
return
289+
268290
def coverage_diff(cycle_num, fuzz_dir, cov_paths, afl_file, cov, cargs):
269291

270292
log_lines = []
271293
delta_log_lines = []
272294
print_diff_header = True
273295

296+
### defaults
274297
a_file = '(init)'
275298
if cov_paths['id_file']:
276299
a_file = cov_paths['id_file']
277-
b_file = os.path.basename(afl_file)
300+
delta_file = b_file = os.path.basename(afl_file)
301+
302+
if cargs.cover_corpus or cargs.coverage_at_exit:
303+
a_file = 'id:%d...' % cov_paths['id_min']
304+
b_file = 'id:%d...' % cov_paths['id_max']
305+
delta_file = 'id:[%d-%d]...' % \
306+
(cov_paths['id_min'], cov_paths['id_max'])
278307

279308
new_cov = extract_coverage(cov_paths['lcov_info_final'], cargs)
280309

@@ -301,10 +330,10 @@ def coverage_diff(cycle_num, fuzz_dir, cov_paths, afl_file, cov, cargs):
301330
if ctype == 'line':
302331
if cargs.coverage_include_lines:
303332
delta_log_lines.append("%s, %s, %s, %s, %s\n" \
304-
% (b_file, cycle_num, f, ctype, val))
333+
% (delta_file, cycle_num, f, ctype, val))
305334
else:
306335
delta_log_lines.append("%s, %s, %s, %s, %s\n" \
307-
% (b_file, cycle_num, f, ctype, val))
336+
% (delta_file, cycle_num, f, ctype, val))
308337
elif f in cov['zero'] and f in cov['pos']:
309338
for ctype in new_cov['pos'][f]:
310339
for val in sorted(new_cov['pos'][f][ctype]):
@@ -321,11 +350,11 @@ def coverage_diff(cycle_num, fuzz_dir, cov_paths, afl_file, cov, cargs):
321350
if ctype == 'line':
322351
if cargs.coverage_include_lines:
323352
delta_log_lines.append("%s, %s, %s, %s, %s\n" \
324-
% (b_file, cycle_num, f, \
353+
% (delta_file, cycle_num, f, \
325354
ctype, val))
326355
else:
327356
delta_log_lines.append("%s, %s, %s, %s, %s\n" \
328-
% (b_file, cycle_num, f, \
357+
% (delta_file, cycle_num, f, \
329358
ctype, val))
330359

331360
### now that new positive coverage has been added, reset zero
@@ -724,8 +753,10 @@ def init_tracking(cov_paths, cargs):
724753
cov_paths['id_delta_cov'] = "%s/id-delta-cov" % cov_paths['top_dir']
725754
cov_paths['zero_cov'] = "%s/zero-cov" % cov_paths['top_dir']
726755
cov_paths['pos_cov'] = "%s/pos-cov" % cov_paths['top_dir']
727-
cov_paths['id_file'] = ''
728756
cov_paths['diff'] = ''
757+
cov_paths['id_file'] = ''
758+
cov_paths['id_min'] = -1 ### used in --cover-corpus mode
759+
cov_paths['id_max'] = -1 ### used in --cover-corpus mode
729760

730761
### raw lcov files
731762
cov_paths['lcov_base'] = "%s/trace.lcov_base" % cov_paths['lcov_dir']
@@ -949,7 +980,10 @@ def mkdirs(cov_paths, cargs):
949980

950981
### write coverage results in the following format
951982
cfile = open(cov_paths['id_delta_cov'], 'w')
952-
cfile.write("# id:NNNNNN*_file, cycle, src_file, coverage_type, fcn/line\n")
983+
if cargs.cover_corpus or cargs.coverage_at_exit:
984+
cfile.write("# id:[range]..., cycle, src_file, coverage_type, fcn/line\n")
985+
else:
986+
cfile.write("# id:NNNNNN*_file, cycle, src_file, coverage_type, fcn/line\n")
953987
cfile.close()
954988

955989
return

0 commit comments

Comments
 (0)