drh | 4d35c41 | 2015-04-28 00:53:26 +0000 | [diff] [blame] | 1 | #!/usr/bin/tclsh |
| 2 | # |
| 3 | # Run this script in order to rebuild the fuzzdata1.txt file containing |
| 4 | # fuzzer data for the fuzzershell utility that is create by afl-fuzz. |
| 5 | # |
| 6 | # This script gathers all of the test cases identified by afl-fuzz and |
| 7 | # runs afl-cmin and afl-tmin over them all to try to generate a mimimum |
| 8 | # set of tests that cover all observed behavior. |
| 9 | # |
| 10 | # Options: |
| 11 | # |
| 12 | # --afl-bin DIR1 DIR1 contains the AFL binaries |
| 13 | # --fuzzershell PATH Full pathname of instrumented fuzzershell |
| 14 | # --afl-data DIR3 DIR3 is the "-o" directory from afl-fuzz |
| 15 | # -o FILE Write results into FILE |
| 16 | # |
| 17 | set AFLBIN {} |
| 18 | set FUZZERSHELL {} |
| 19 | set AFLDATA {} |
| 20 | set OUTFILE {} |
| 21 | |
| 22 | proc usage {} { |
| 23 | puts stderr "Usage: $::argv0 --afl-bin DIR --fuzzershell PATH\ |
| 24 | --afl-data DIR -o FILE" |
| 25 | exit 1 |
| 26 | } |
| 27 | proc cmdlineerr {msg} { |
| 28 | puts stderr $msg |
| 29 | usage |
| 30 | } |
| 31 | |
| 32 | for {set i 0} {$i<[llength $argv]} {incr i} { |
| 33 | set x [lindex $argv $i] |
| 34 | if {[string index $x 0]!="-"} {cmdlineerr "illegal argument: $x"} |
| 35 | set x [string trimleft $x -] |
| 36 | incr i |
| 37 | if {$i>=[llength $argv]} {cmdlineerr "no argument on --$x"} |
| 38 | set a [lindex $argv $i] |
| 39 | switch -- $x { |
| 40 | afl-bin {set AFLBIN $a} |
| 41 | afl-data {set AFLDATA $a} |
| 42 | fuzzershell {set FUZZERSHELL $a} |
| 43 | o {set OUTFILE $a} |
| 44 | default {cmdlineerr "unknown option: --$x"} |
| 45 | } |
| 46 | } |
| 47 | proc checkarg {varname option} { |
| 48 | set val [set ::$varname] |
| 49 | if {$val==""} {cmdlineerr "required option missing: --$option"} |
| 50 | } |
| 51 | checkarg AFLBIN afl-bin |
| 52 | checkarg AFLDATA afl-data |
| 53 | checkarg FUZZERSHELL fuzzershell |
| 54 | checkarg OUTFILE o |
| 55 | proc checkexec {x} { |
| 56 | if {![file exec $x]} {cmdlineerr "cannot find $x"} |
| 57 | } |
| 58 | checkexec $AFLBIN/afl-cmin |
| 59 | checkexec $AFLBIN/afl-tmin |
| 60 | checkexec $FUZZERSHELL |
| 61 | proc checkdir {x} { |
| 62 | if {![file isdir $x]} {cmdlineerr "no such directory: $x"} |
| 63 | } |
| 64 | checkdir $AFLDATA/queue |
| 65 | |
| 66 | proc progress {msg} { |
| 67 | puts "******** $msg" |
| 68 | flush stdout |
| 69 | } |
| 70 | progress "mkdir tmp1 tmp2" |
| 71 | file mkdir tmp1 tmp2 |
| 72 | progress "copying test cases from $AFLDATA into tmp1..." |
| 73 | set n 0 |
| 74 | foreach file [glob -nocomplain $AFLDATA/queue/id:*] { |
| 75 | incr n |
| 76 | file copy $file tmp1/$n |
| 77 | } |
| 78 | foreach file [glob -nocomplain $AFLDATA/crash*/id:*] { |
| 79 | incr n |
| 80 | file copy $file tmp1/$n |
| 81 | } |
| 82 | progress "total $n files copied." |
| 83 | progress "running: $AFLBIN/afl-cmin -i tmp1 -o tmp2 $FUZZERSHELL" |
| 84 | exec $AFLBIN/afl-cmin -i tmp1 -o tmp2 $FUZZERSHELL >&@ stdout |
| 85 | progress "afl-cmin complete." |
| 86 | # |
| 87 | # Experiments show that running afl-tmin is too slow for this application. |
| 88 | # And it doesn't really make the test cases that much smaller. So let's |
| 89 | # just skip it. |
| 90 | # |
| 91 | # foreach file [glob tmp2/*] { |
| 92 | # progress "$AFLBIN/afl-tmin -i $file -o tmp3/[file tail $file] $FUZZERSHELL" |
| 93 | # exec $AFLBIN/afl-tmin -i $file -o tmp3/[file tail $file] \ |
| 94 | # $FUZZERSHELL >&@ stdout |
| 95 | # } |
| 96 | progress "generating final output into $OUTFILE" |
| 97 | set out [open $OUTFILE wb] |
| 98 | puts $out "# Test data for use with fuzzershell. Automatically |
| 99 | # generated using $argv0. This file contains binary data |
| 100 | #" |
| 101 | set n 0 |
| 102 | foreach file [glob tmp2/*] { |
| 103 | incr n |
| 104 | puts -nonewline $out "/****<$n>****/" |
| 105 | set in [open $file rb] |
| 106 | puts -nonewline $out [read $in] |
| 107 | close $in |
| 108 | } |
| 109 | close $out |
| 110 | progress "done. $n test cases written to $OUTFILE" |
| 111 | progress "clean-up..." |
drh | 6c98415 | 2015-04-30 13:06:15 +0000 | [diff] [blame] | 112 | file delete -force tmp1 |
| 113 | progress "culled test cases left in the tmp2 directory" |