Skip to content

Commit 3882668

Browse files
committed
always write command errors to log file
1 parent 7d0b064 commit 3882668

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

afl-cov

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ except ImportError:
4646

4747
__version__ = '0.6.1'
4848

49-
NO_OUTPUT = 0
50-
WANT_OUTPUT = 1
51-
LOG_ERRORS = 2
52-
5349
def main():
5450

5551
exit_success = 0
@@ -182,10 +178,10 @@ def process_afl_test_cases(cargs):
182178
### for the current AFL test case file
183179
if run_once:
184180
run_cmd(cargs.coverage_cmd.replace('AFL_FILE', f),
185-
cov_paths['log_file'], cargs, NO_OUTPUT)
181+
cov_paths['log_file'], cargs)
186182
else:
187183
out_lines = run_cmd(cargs.coverage_cmd.replace('AFL_FILE', f),
188-
cov_paths['log_file'], cargs, WANT_OUTPUT)[1]
184+
cov_paths['log_file'], cargs)[1]
189185
run_once = True
190186

191187
if cargs.afl_queue_id_limit \
@@ -579,29 +575,29 @@ def lcov_gen_coverage(cov_paths, cargs):
579575
+ " --no-checksum --capture --directory " \
580576
+ cargs.code_dir + " --output-file " \
581577
+ cov_paths['lcov_info'], \
582-
cov_paths['log_file'], cargs, LOG_ERRORS)
578+
cov_paths['log_file'], cargs)
583579

584580
if (cargs.disable_lcov_exclude_pattern):
585581
out_lines = run_cmd(cargs.lcov_path \
586582
+ lcov_opts
587583
+ " --no-checksum -a " + cov_paths['lcov_base'] \
588584
+ " -a " + cov_paths['lcov_info'] \
589585
+ " --output-file " + cov_paths['lcov_info_final'], \
590-
cov_paths['log_file'], cargs, WANT_OUTPUT)[1]
586+
cov_paths['log_file'], cargs)[1]
591587
else:
592588
tmp_file = NamedTemporaryFile(delete=False)
593589
run_cmd(cargs.lcov_path \
594590
+ lcov_opts
595591
+ " --no-checksum -a " + cov_paths['lcov_base'] \
596592
+ " -a " + cov_paths['lcov_info'] \
597593
+ " --output-file " + tmp_file.name, \
598-
cov_paths['log_file'], cargs, LOG_ERRORS)
594+
cov_paths['log_file'], cargs)
599595
out_lines = run_cmd(cargs.lcov_path \
600596
+ lcov_opts
601597
+ " --no-checksum -r " + tmp_file.name \
602598
+ " " + cargs.lcov_exclude_pattern + " --output-file " \
603599
+ cov_paths['lcov_info_final'],
604-
cov_paths['log_file'], cargs, WANT_OUTPUT)[1]
600+
cov_paths['log_file'], cargs)[1]
605601
if os.path.exists(tmp_file.name):
606602
os.unlink(tmp_file.name)
607603

@@ -638,7 +634,7 @@ def gen_web_cov_report(fuzz_dir, cov_paths, cargs):
638634
+ " --output-directory " \
639635
+ cov_paths['web_dir'] + " " \
640636
+ cov_paths['lcov_info_final'], \
641-
cov_paths['log_file'], cargs, LOG_ERRORS)
637+
cov_paths['log_file'], cargs)
642638

643639
logr("[+] Final lcov web report: %s/%s" % \
644640
(cov_paths['web_dir'], 'index.html'), cov_paths['log_file'], cargs)
@@ -686,19 +682,18 @@ def get_running_pid(stats_file, pid_re):
686682
break
687683
return pid
688684

689-
def run_cmd(cmd, log_file, cargs, collect):
685+
def run_cmd(cmd, log_file, cargs):
690686

691-
out = []
687+
output = []
688+
689+
if log_file:
690+
logr(" CMD: %s" % cmd, log_file, cargs)
692691

693692
if cargs.verbose:
694-
if log_file:
695-
logr(" CMD: %s" % cmd, log_file, cargs)
696-
else:
697-
print " CMD: %s" % cmd
693+
print " CMD: %s" % cmd
698694

699695
fh = None
700-
if cargs.disable_cmd_redirection or collect == WANT_OUTPUT \
701-
or collect == LOG_ERRORS:
696+
if cargs.disable_cmd_redirection:
702697
fh = NamedTemporaryFile(delete=False)
703698
else:
704699
fh = open(os.devnull, 'w')
@@ -708,23 +703,24 @@ def run_cmd(cmd, log_file, cargs, collect):
708703

709704
fh.close()
710705

711-
if cargs.disable_cmd_redirection or collect == WANT_OUTPUT \
712-
or collect == LOG_ERRORS:
706+
if cargs.disable_cmd_redirection:
713707
with open(fh.name, 'r') as f:
714708
for line in f:
715-
out.append(line.rstrip('\n'))
709+
output.append(line.rstrip('\n'))
716710
os.unlink(fh.name)
717711

718-
if (es != 0) and (collect == LOG_ERRORS or collect == WANT_OUTPUT):
712+
if (es != 0):
719713
if log_file:
720714
logr(" Non-zero exit status '%d' for CMD: %s" % (es, cmd),
721715
log_file, cargs)
722-
for line in out:
716+
for line in output:
723717
logr(line, log_file, cargs)
724718
else:
725719
print " Non-zero exit status '%d' for CMD: %s" % (es, cmd)
720+
for line in output:
721+
print line
726722

727-
return es, out
723+
return es, output
728724

729725
def import_fuzzing_dirs(cov_paths, cargs):
730726

@@ -804,15 +800,15 @@ def init_tracking(cov_paths, cargs):
804800
run_cmd(cargs.lcov_path \
805801
+ lcov_opts \
806802
+ " --no-checksum --zerocounters --directory " \
807-
+ cargs.code_dir, cov_paths['log_file'], cargs, LOG_ERRORS)
803+
+ cargs.code_dir, cov_paths['log_file'], cargs)
808804

809805
run_cmd(cargs.lcov_path \
810806
+ lcov_opts
811807
+ " --no-checksum --capture --initial" \
812808
+ " --directory " + cargs.code_dir \
813809
+ " --output-file " \
814810
+ cov_paths['lcov_base'], \
815-
cov_paths['log_file'], cargs, LOG_ERRORS)
811+
cov_paths['log_file'], cargs)
816812

817813
return True
818814

@@ -827,7 +823,7 @@ def is_bin_gcov_enabled(binary, cargs):
827823

828824
### run readelf against the binary to see if it contains gcov support
829825
for line in run_cmd("%s -a %s" % (cargs.readelf_path, binary),
830-
False, cargs, WANT_OUTPUT)[1]:
826+
False, cargs)[1]:
831827
if ' __gcov' in line:
832828
if cargs.validate_args or cargs.gcov_check or cargs.gcov_check_bin:
833829
print "[+] Binary '%s' is compiled with code coverage support gcc." % binary

0 commit comments

Comments
 (0)