1
- #!/usr/bin/env python2
1
+ #!/usr/bin/env python3
2
2
#
3
3
# File: afl-cov
4
4
#
5
- # Version: 0.6.4
5
+ # Version: 0.6.5
6
6
#
7
7
# Purpose: Perform lcov coverage diff's against each AFL queue file to see
8
8
# new functions and line coverage evolve from an AFL fuzzing cycle.
45
45
except ImportError :
46
46
import subprocess
47
47
48
- __version__ = '0.6.4 '
48
+ __version__ = '0.6.5 '
49
49
50
50
NO_OUTPUT = 0
51
51
WANT_OUTPUT = 1
@@ -62,7 +62,7 @@ def main():
62
62
cargs .coverage_cmd = cargs .coverage_cmd .replace ('@@' , 'AFL_FILE' )
63
63
64
64
if cargs .version :
65
- print "afl-cov-" + __version__
65
+ print ( "afl-cov-" + __version__ )
66
66
return exit_success
67
67
68
68
if cargs .gcov_check or cargs .gcov_check_bin :
@@ -100,7 +100,7 @@ def run_in_background():
100
100
### capability anyway
101
101
pid = os .fork ()
102
102
if (pid < 0 ):
103
- print "[*] fork() error, exiting."
103
+ print ( "[*] fork() error, exiting." )
104
104
os ._exit ()
105
105
elif (pid > 0 ):
106
106
os ._exit (0 )
@@ -710,7 +710,7 @@ def run_cmd(cmd, log_file, cargs, collect, aflrun, fn):
710
710
if log_file :
711
711
logr (" CMD: %s" % cmd , log_file , cargs )
712
712
else :
713
- print " CMD: %s" % cmd
713
+ print ( " CMD: %s" % cmd )
714
714
715
715
es = subprocess .call (cmd , stdin = None ,
716
716
stdout = fh , stderr = subprocess .STDOUT , shell = True )
@@ -731,14 +731,14 @@ def run_cmd(cmd, log_file, cargs, collect, aflrun, fn):
731
731
for line in out :
732
732
logr (line , log_file , cargs )
733
733
else :
734
- print " Non-zero exit status '%d' for CMD: %s" % (es , cmd )
734
+ print ( " Non-zero exit status '%d' for CMD: %s" % (es , cmd ) )
735
735
736
736
return es , out
737
737
738
738
def import_fuzzing_dirs (cov_paths , cargs ):
739
739
740
740
if not cargs .afl_fuzzing_dir :
741
- print "[*] Must specify AFL fuzzing dir with --afl-fuzzing-dir or -d"
741
+ print ( "[*] Must specify AFL fuzzing dir with --afl-fuzzing-dir or -d" )
742
742
return False
743
743
744
744
if 'top_dir' not in cov_paths :
@@ -794,8 +794,8 @@ def init_tracking(cov_paths, cargs):
794
794
else :
795
795
if is_dir (cov_paths ['top_dir' ]):
796
796
if not cargs .func_search and not cargs .line_search :
797
- print "[*] Existing coverage dir %s found, use --overwrite to " \
798
- "re-calculate coverage" % (cov_paths ['top_dir' ])
797
+ print ( "[*] Existing coverage dir %s found, use --overwrite to " \
798
+ "re-calculate coverage" % (cov_paths ['top_dir' ]))
799
799
return False
800
800
else :
801
801
mkdirs (cov_paths , cargs )
@@ -839,18 +839,18 @@ def is_bin_gcov_enabled(binary, cargs):
839
839
False , cargs , WANT_OUTPUT , False , "" )[1 ]:
840
840
if ' __gcov' in line :
841
841
if cargs .validate_args or cargs .gcov_check or cargs .gcov_check_bin :
842
- print "[+] Binary '%s' is compiled with code coverage support via gcc." % binary
842
+ print ( "[+] Binary '%s' is compiled with code coverage support via gcc." % binary )
843
843
rv = True
844
844
break
845
845
846
846
if '__llvm_gcov' in line :
847
847
if cargs .validate_args or cargs .gcov_check or cargs .gcov_check_bin :
848
- print "[+] Binary '%s' is compiled with code coverage support via llvm." % binary
848
+ print ( "[+] Binary '%s' is compiled with code coverage support via llvm." % binary )
849
849
rv = True
850
850
break
851
851
852
852
if not rv and cargs .gcov_check_bin :
853
- print "[*] Binary '%s' is not compiled with code coverage support." % binary
853
+ print ( "[*] Binary '%s' is not compiled with code coverage support." % binary )
854
854
855
855
return rv
856
856
@@ -878,26 +878,26 @@ def check_requirements(cargs):
878
878
genhtml = which ( cargs .genhtml_path )
879
879
880
880
if ( lcov == None or gcov == None ):
881
- print "Required command not found :"
881
+ print ( "Required command not found :" )
882
882
else :
883
883
if (genhtml == None and not cargs .disable_lcov_web ):
884
- print "Required command not found :"
884
+ print ( "Required command not found :" )
885
885
else :
886
886
return True
887
887
888
888
if ( lcov == None ):
889
- print "[*] lcov command does not exist : %s" % (cargs .lcov_path )
889
+ print ( "[*] lcov command does not exist : %s" % (cargs .lcov_path ) )
890
890
if ( genhtml == None and not cargs .disable_lcov_web ):
891
- print "[*] genhtml command does not exist : %s" % (cargs .genhtml_path )
891
+ print ( "[*] genhtml command does not exist : %s" % (cargs .genhtml_path ) )
892
892
if ( gcov == None ):
893
- print "[*] gcov command does not exist : %s" % (cargs .gcov_path )
893
+ print ( "[*] gcov command does not exist : %s" % (cargs .gcov_path ) )
894
894
895
895
return False
896
896
897
897
def is_gcov_enabled (cargs ):
898
898
899
899
if not is_exe (cargs .readelf_path ):
900
- print "[*] Need a valid path to readelf, use --readelf-path"
900
+ print ( "[*] Need a valid path to readelf, use --readelf-path" )
901
901
return False
902
902
903
903
if cargs .coverage_cmd :
@@ -917,21 +917,21 @@ def is_gcov_enabled(cargs):
917
917
break
918
918
919
919
if not found_exec :
920
- print "[*] Could not find an executable binary " \
921
- "--coverage-cmd '%s'" % cargs .coverage_cmd
920
+ print ( "[*] Could not find an executable binary " \
921
+ "--coverage-cmd '%s'" % cargs .coverage_cmd )
922
922
return False
923
923
924
924
if not cargs .disable_gcov_check and not found_code_cov_binary :
925
- print "[*] Could not find an executable binary with code " \
925
+ print ( "[*] Could not find an executable binary with code " \
926
926
"coverage support ('-fprofile-arcs -ftest-coverage') " \
927
- "in --coverage-cmd '%s'" % cargs .coverage_cmd
927
+ "in --coverage-cmd '%s'" % cargs .coverage_cmd )
928
928
return False
929
929
930
930
elif cargs .gcov_check_bin :
931
931
if not is_bin_gcov_enabled (cargs .gcov_check_bin , cargs ):
932
932
return False
933
933
elif cargs .gcov_check :
934
- print "[*] Either --coverage-cmd or --gcov-check-bin required in --gcov-check mode"
934
+ print ( "[*] Either --coverage-cmd or --gcov-check-bin required in --gcov-check mode" )
935
935
return False
936
936
937
937
return True
@@ -943,12 +943,12 @@ def validate_cargs(cargs):
943
943
return False
944
944
else :
945
945
if not cargs .func_search and not cargs .line_search :
946
- print "[*] Must set --coverage-cmd or --func-search/--line-search"
946
+ print ( "[*] Must set --coverage-cmd or --func-search/--line-search" )
947
947
return False
948
948
949
949
if cargs .code_dir :
950
950
if not is_dir (cargs .code_dir ):
951
- print "[*] --code-dir path does not exist"
951
+ print ( "[*] --code-dir path does not exist" )
952
952
return False
953
953
954
954
### make sure code coverage support is compiled in
@@ -957,31 +957,31 @@ def validate_cargs(cargs):
957
957
958
958
else :
959
959
if not cargs .func_search and not cargs .line_search :
960
- print "[*] Must set --code-dir unless using --func-search " \
961
- "against existing afl-cov directory"
960
+ print ( "[*] Must set --code-dir unless using --func-search " \
961
+ "against existing afl-cov directory" )
962
962
return False
963
963
964
964
if cargs .func_search or cargs .line_search :
965
965
if not cargs .afl_fuzzing_dir :
966
- print "[*] Must set --afl-fuzzing-dir"
966
+ print ( "[*] Must set --afl-fuzzing-dir" )
967
967
return False
968
968
if cargs .func_search and '()' not in cargs .func_search :
969
969
cargs .func_search += '()'
970
970
if cargs .line_search and not cargs .src_file :
971
- print "[*] Must set --src-file in --line-search mode"
971
+ print ( "[*] Must set --src-file in --line-search mode" )
972
972
return False
973
973
974
974
if cargs .live and not cargs .ignore_core_pattern :
975
975
if not check_core_pattern ():
976
976
return False
977
977
978
978
if not cargs .live and not is_dir (cargs .afl_fuzzing_dir ):
979
- print "[*] It doesn't look like directory '%s' exists" \
980
- % (cargs .afl_fuzzing_dir )
979
+ print ( "[*] It doesn't look like directory '%s' exists" \
980
+ % (cargs .afl_fuzzing_dir ))
981
981
return False
982
982
983
983
if cargs .disable_lcov_web and cargs .lcov_web_all :
984
- print "[*] --disable-lcov-web and --lcov-web-all are incompatible"
984
+ print ( "[*] --disable-lcov-web and --lcov-web-all are incompatible" )
985
985
return False
986
986
987
987
return True
@@ -997,24 +997,24 @@ def gcno_files_exist(cargs):
997
997
if filename [- 5 :] == '.gcno' :
998
998
found_code_coverage_support = True
999
999
if not found_code_coverage_support :
1000
- print "[*] Could not find any *.gcno files in --code-dir " \
1000
+ print ( "[*] Could not find any *.gcno files in --code-dir " \
1001
1001
"'%s', is code coverage ('-fprofile-arcs -ftest-coverage') " \
1002
- "compiled in?" % cargs .code_dir
1002
+ "compiled in?" % cargs .code_dir )
1003
1003
return False
1004
1004
return True
1005
1005
1006
1006
def is_afl_running (cargs ):
1007
1007
while not is_dir (cargs .afl_fuzzing_dir ):
1008
1008
if not cargs .background :
1009
- print "[-] Sleep for %d seconds for AFL fuzzing directory to be created..." \
1010
- % cargs .sleep
1009
+ print ( "[-] Sleep for %d seconds for AFL fuzzing directory to be created..." \
1010
+ % cargs .sleep )
1011
1011
time .sleep (cargs .sleep )
1012
1012
1013
1013
### if we make it here then afl-fuzz is presumably running
1014
1014
while not is_afl_fuzz_running (cargs ):
1015
1015
if not cargs .background :
1016
- print "[-] Sleep for %d seconds waiting for afl-fuzz to be started...." \
1017
- % cargs .sleep
1016
+ print ( "[-] Sleep for %d seconds waiting for afl-fuzz to be started...." \
1017
+ % cargs .sleep )
1018
1018
time .sleep (cargs .sleep )
1019
1019
return
1020
1020
@@ -1053,7 +1053,7 @@ def is_dir(dpath):
1053
1053
1054
1054
def logr (pstr , log_file , cargs ):
1055
1055
if not cargs .background and not cargs .quiet :
1056
- print " " + pstr
1056
+ print ( " " + pstr )
1057
1057
append_file (pstr , log_file )
1058
1058
return
1059
1059
@@ -1066,22 +1066,22 @@ def stop_afl(cargs):
1066
1066
### is also stopped.
1067
1067
1068
1068
if not cargs .afl_fuzzing_dir :
1069
- print "[*] Must set --afl-fuzzing-dir"
1069
+ print ( "[*] Must set --afl-fuzzing-dir" )
1070
1070
return False
1071
1071
1072
1072
if not is_dir (cargs .afl_fuzzing_dir ):
1073
- print "[*] Doesn't look like AFL fuzzing directory '%s' exists." \
1074
- % cargs .afl_fuzzing_dir
1073
+ print ( "[*] Doesn't look like AFL fuzzing directory '%s' exists." \
1074
+ % cargs .afl_fuzzing_dir )
1075
1075
return False
1076
1076
1077
1077
if os .path .exists (cargs .afl_fuzzing_dir + '/fuzzer_stats' ):
1078
1078
afl_pid = get_running_pid (cargs .afl_fuzzing_dir + '/fuzzer_stats' ,
1079
1079
'fuzzer_pid\s+\:\s+(\d+)' )
1080
1080
if afl_pid :
1081
- print "[+] Stopping running afl-fuzz instance, PID: %d" % afl_pid
1081
+ print ( "[+] Stopping running afl-fuzz instance, PID: %d" % afl_pid )
1082
1082
os .kill (afl_pid , signal .SIGTERM )
1083
1083
else :
1084
- print "[-] No running afl-fuzz instance"
1084
+ print ( "[-] No running afl-fuzz instance" )
1085
1085
rv = False
1086
1086
else :
1087
1087
found = False
@@ -1090,12 +1090,12 @@ def stop_afl(cargs):
1090
1090
if os .path .exists (stats_file ):
1091
1091
afl_pid = get_running_pid (stats_file , 'fuzzer_pid\s+\:\s+(\d+)' )
1092
1092
if afl_pid :
1093
- print "[+] Stopping running afl-fuzz instance, PID: %d" \
1094
- % afl_pid
1093
+ print ( "[+] Stopping running afl-fuzz instance, PID: %d" \
1094
+ % afl_pid )
1095
1095
os .kill (afl_pid , signal .SIGTERM )
1096
1096
found = True
1097
1097
if not found :
1098
- print "[-] No running afl-fuzz instance"
1098
+ print ( "[-] No running afl-fuzz instance" )
1099
1099
rv = False
1100
1100
1101
1101
return rv
@@ -1112,8 +1112,8 @@ def check_core_pattern():
1112
1112
with open (core_pattern_file , 'r' ) as f :
1113
1113
if f .readline ().rstrip ()[0 ] == '|' :
1114
1114
### same logic as implemented by afl-fuzz itself
1115
- print "[*] afl-fuzz requires 'echo core >%s'" \
1116
- % core_pattern_file
1115
+ print ( "[*] afl-fuzz requires 'echo core >%s'" \
1116
+ % core_pattern_file )
1117
1117
rv = False
1118
1118
return rv
1119
1119
0 commit comments