@@ -46,6 +46,10 @@ except ImportError:
46
46
47
47
__version__ = '0.6.1'
48
48
49
+ NO_OUTPUT = 0
50
+ WANT_OUTPUT = 1
51
+ LOG_ERRORS = 2
52
+
49
53
def main ():
50
54
51
55
exit_success = 0
@@ -178,10 +182,10 @@ def process_afl_test_cases(cargs):
178
182
### for the current AFL test case file
179
183
if run_once :
180
184
run_cmd (cargs .coverage_cmd .replace ('AFL_FILE' , f ),
181
- cov_paths ['log_file' ], cargs )
185
+ cov_paths ['log_file' ], cargs , NO_OUTPUT )
182
186
else :
183
187
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 ]
185
189
run_once = True
186
190
187
191
if cargs .afl_queue_id_limit \
@@ -575,29 +579,29 @@ def lcov_gen_coverage(cov_paths, cargs):
575
579
+ " --no-checksum --capture --directory " \
576
580
+ cargs .code_dir + " --output-file " \
577
581
+ cov_paths ['lcov_info' ], \
578
- cov_paths ['log_file' ], cargs )
582
+ cov_paths ['log_file' ], cargs , LOG_ERRORS )
579
583
580
584
if (cargs .disable_lcov_exclude_pattern ):
581
585
out_lines = run_cmd (cargs .lcov_path \
582
586
+ lcov_opts
583
587
+ " --no-checksum -a " + cov_paths ['lcov_base' ] \
584
588
+ " -a " + cov_paths ['lcov_info' ] \
585
589
+ " --output-file " + cov_paths ['lcov_info_final' ], \
586
- cov_paths ['log_file' ], cargs )[1 ]
590
+ cov_paths ['log_file' ], cargs , WANT_OUTPUT )[1 ]
587
591
else :
588
592
tmp_file = NamedTemporaryFile (delete = False )
589
593
run_cmd (cargs .lcov_path \
590
594
+ lcov_opts
591
595
+ " --no-checksum -a " + cov_paths ['lcov_base' ] \
592
596
+ " -a " + cov_paths ['lcov_info' ] \
593
597
+ " --output-file " + tmp_file .name , \
594
- cov_paths ['log_file' ], cargs )
598
+ cov_paths ['log_file' ], cargs , LOG_ERRORS )
595
599
out_lines = run_cmd (cargs .lcov_path \
596
600
+ lcov_opts
597
601
+ " --no-checksum -r " + tmp_file .name \
598
602
+ " " + cargs .lcov_exclude_pattern + " --output-file " \
599
603
+ cov_paths ['lcov_info_final' ],
600
- cov_paths ['log_file' ], cargs )[1 ]
604
+ cov_paths ['log_file' ], cargs , WANT_OUTPUT )[1 ]
601
605
if os .path .exists (tmp_file .name ):
602
606
os .unlink (tmp_file .name )
603
607
@@ -634,7 +638,7 @@ def gen_web_cov_report(fuzz_dir, cov_paths, cargs):
634
638
+ " --output-directory " \
635
639
+ cov_paths ['web_dir' ] + " " \
636
640
+ cov_paths ['lcov_info_final' ], \
637
- cov_paths ['log_file' ], cargs )
641
+ cov_paths ['log_file' ], cargs , LOG_ERRORS )
638
642
639
643
logr ("[+] Final lcov web report: %s/%s" % \
640
644
(cov_paths ['web_dir' ], 'index.html' ), cov_paths ['log_file' ], cargs )
@@ -682,18 +686,19 @@ def get_running_pid(stats_file, pid_re):
682
686
break
683
687
return pid
684
688
685
- def run_cmd (cmd , log_file , cargs ):
689
+ def run_cmd (cmd , log_file , cargs , collect ):
686
690
687
- output = []
688
-
689
- if log_file :
690
- logr (" CMD: %s" % cmd , log_file , cargs )
691
+ out = []
691
692
692
693
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
694
698
695
699
fh = None
696
- if cargs .disable_cmd_redirection :
700
+ if cargs .disable_cmd_redirection or collect == WANT_OUTPUT \
701
+ or collect == LOG_ERRORS :
697
702
fh = NamedTemporaryFile (delete = False )
698
703
else :
699
704
fh = open (os .devnull , 'w' )
@@ -703,24 +708,23 @@ def run_cmd(cmd, log_file, cargs):
703
708
704
709
fh .close ()
705
710
706
- if cargs .disable_cmd_redirection :
711
+ if cargs .disable_cmd_redirection or collect == WANT_OUTPUT \
712
+ or collect == LOG_ERRORS :
707
713
with open (fh .name , 'r' ) as f :
708
714
for line in f :
709
- output .append (line .rstrip ('\n ' ))
715
+ out .append (line .rstrip ('\n ' ))
710
716
os .unlink (fh .name )
711
717
712
- if (es != 0 ):
718
+ if (es != 0 ) and ( collect == LOG_ERRORS or collect == WANT_OUTPUT ) :
713
719
if log_file :
714
720
logr (" Non-zero exit status '%d' for CMD: %s" % (es , cmd ),
715
721
log_file , cargs )
716
- for line in output :
722
+ for line in out :
717
723
logr (line , log_file , cargs )
718
724
else :
719
725
print " Non-zero exit status '%d' for CMD: %s" % (es , cmd )
720
- for line in output :
721
- print line
722
726
723
- return es , output
727
+ return es , out
724
728
725
729
def import_fuzzing_dirs (cov_paths , cargs ):
726
730
@@ -800,15 +804,15 @@ def init_tracking(cov_paths, cargs):
800
804
run_cmd (cargs .lcov_path \
801
805
+ lcov_opts \
802
806
+ " --no-checksum --zerocounters --directory " \
803
- + cargs .code_dir , cov_paths ['log_file' ], cargs )
807
+ + cargs .code_dir , cov_paths ['log_file' ], cargs , LOG_ERRORS )
804
808
805
809
run_cmd (cargs .lcov_path \
806
810
+ lcov_opts
807
811
+ " --no-checksum --capture --initial" \
808
812
+ " --directory " + cargs .code_dir \
809
813
+ " --output-file " \
810
814
+ cov_paths ['lcov_base' ], \
811
- cov_paths ['log_file' ], cargs )
815
+ cov_paths ['log_file' ], cargs , LOG_ERRORS )
812
816
813
817
return True
814
818
@@ -823,7 +827,7 @@ def is_bin_gcov_enabled(binary, cargs):
823
827
824
828
### run readelf against the binary to see if it contains gcov support
825
829
for line in run_cmd ("%s -a %s" % (cargs .readelf_path , binary ),
826
- False , cargs )[1 ]:
830
+ False , cargs , WANT_OUTPUT )[1 ]:
827
831
if ' __gcov' in line :
828
832
if cargs .validate_args or cargs .gcov_check or cargs .gcov_check_bin :
829
833
print "[+] Binary '%s' is compiled with code coverage support gcc." % binary
0 commit comments