Skip to content

Commit 32ed6d3

Browse files
committed
Revert "always write command errors to log file"
This reverts commit 3882668.
1 parent 3882668 commit 32ed6d3

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

afl-cov

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

4747
__version__ = '0.6.1'
4848

49+
NO_OUTPUT = 0
50+
WANT_OUTPUT = 1
51+
LOG_ERRORS = 2
52+
4953
def main():
5054

5155
exit_success = 0
@@ -178,10 +182,10 @@ def process_afl_test_cases(cargs):
178182
### for the current AFL test case file
179183
if run_once:
180184
run_cmd(cargs.coverage_cmd.replace('AFL_FILE', f),
181-
cov_paths['log_file'], cargs)
185+
cov_paths['log_file'], cargs, NO_OUTPUT)
182186
else:
183187
out_lines = run_cmd(cargs.coverage_cmd.replace('AFL_FILE', f),
184-
cov_paths['log_file'], cargs)[1]
188+
cov_paths['log_file'], cargs, WANT_OUTPUT)[1]
185189
run_once = True
186190

187191
if cargs.afl_queue_id_limit \
@@ -575,29 +579,29 @@ def lcov_gen_coverage(cov_paths, cargs):
575579
+ " --no-checksum --capture --directory " \
576580
+ cargs.code_dir + " --output-file " \
577581
+ cov_paths['lcov_info'], \
578-
cov_paths['log_file'], cargs)
582+
cov_paths['log_file'], cargs, LOG_ERRORS)
579583

580584
if (cargs.disable_lcov_exclude_pattern):
581585
out_lines = run_cmd(cargs.lcov_path \
582586
+ lcov_opts
583587
+ " --no-checksum -a " + cov_paths['lcov_base'] \
584588
+ " -a " + cov_paths['lcov_info'] \
585589
+ " --output-file " + cov_paths['lcov_info_final'], \
586-
cov_paths['log_file'], cargs)[1]
590+
cov_paths['log_file'], cargs, WANT_OUTPUT)[1]
587591
else:
588592
tmp_file = NamedTemporaryFile(delete=False)
589593
run_cmd(cargs.lcov_path \
590594
+ lcov_opts
591595
+ " --no-checksum -a " + cov_paths['lcov_base'] \
592596
+ " -a " + cov_paths['lcov_info'] \
593597
+ " --output-file " + tmp_file.name, \
594-
cov_paths['log_file'], cargs)
598+
cov_paths['log_file'], cargs, LOG_ERRORS)
595599
out_lines = run_cmd(cargs.lcov_path \
596600
+ lcov_opts
597601
+ " --no-checksum -r " + tmp_file.name \
598602
+ " " + cargs.lcov_exclude_pattern + " --output-file " \
599603
+ cov_paths['lcov_info_final'],
600-
cov_paths['log_file'], cargs)[1]
604+
cov_paths['log_file'], cargs, WANT_OUTPUT)[1]
601605
if os.path.exists(tmp_file.name):
602606
os.unlink(tmp_file.name)
603607

@@ -634,7 +638,7 @@ def gen_web_cov_report(fuzz_dir, cov_paths, cargs):
634638
+ " --output-directory " \
635639
+ cov_paths['web_dir'] + " " \
636640
+ cov_paths['lcov_info_final'], \
637-
cov_paths['log_file'], cargs)
641+
cov_paths['log_file'], cargs, LOG_ERRORS)
638642

639643
logr("[+] Final lcov web report: %s/%s" % \
640644
(cov_paths['web_dir'], 'index.html'), cov_paths['log_file'], cargs)
@@ -682,18 +686,19 @@ def get_running_pid(stats_file, pid_re):
682686
break
683687
return pid
684688

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

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

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

695699
fh = None
696-
if cargs.disable_cmd_redirection:
700+
if cargs.disable_cmd_redirection or collect == WANT_OUTPUT \
701+
or collect == LOG_ERRORS:
697702
fh = NamedTemporaryFile(delete=False)
698703
else:
699704
fh = open(os.devnull, 'w')
@@ -703,24 +708,23 @@ def run_cmd(cmd, log_file, cargs):
703708

704709
fh.close()
705710

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

712-
if (es != 0):
718+
if (es != 0) and (collect == LOG_ERRORS or collect == WANT_OUTPUT):
713719
if log_file:
714720
logr(" Non-zero exit status '%d' for CMD: %s" % (es, cmd),
715721
log_file, cargs)
716-
for line in output:
722+
for line in out:
717723
logr(line, log_file, cargs)
718724
else:
719725
print " Non-zero exit status '%d' for CMD: %s" % (es, cmd)
720-
for line in output:
721-
print line
722726

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

725729
def import_fuzzing_dirs(cov_paths, cargs):
726730

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

805809
run_cmd(cargs.lcov_path \
806810
+ lcov_opts
807811
+ " --no-checksum --capture --initial" \
808812
+ " --directory " + cargs.code_dir \
809813
+ " --output-file " \
810814
+ cov_paths['lcov_base'], \
811-
cov_paths['log_file'], cargs)
815+
cov_paths['log_file'], cargs, LOG_ERRORS)
812816

813817
return True
814818

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

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

0 commit comments

Comments
 (0)