| 1 | # Fuzzing |
| 2 | |
| 3 | We use fuzzing to test the various entrypoints to the library. The fuzzer we use is [AFL++](https://aflplus.plus). All files related to fuzzing live within the `fuzz` directory, which has the following structure: |
| 4 | |
| 5 | ``` |
| 6 | fuzz |
| 7 | ├── corpus |
| 8 | │ ├── parse fuzzing corpus for parsing (a symlink to our fixtures) |
| 9 | │ └── regexp fuzzing corpus for regexp |
| 10 | ├── dict a AFL++ dictionary containing various tokens |
| 11 | ├── docker |
| 12 | │ └── Dockerfile for building a container with the fuzzer toolchain |
| 13 | ├── fuzz.c generic entrypoint for fuzzing |
| 14 | ├── heisenbug.c entrypoint for reproducing a crash or hang |
| 15 | ├── parse.c fuzz handler for parsing |
| 16 | ├── parse.sh script to run parsing fuzzer |
| 17 | ├── regexp.c fuzz handler for regular expression parsing |
| 18 | ├── regexp.sh script to run regexp fuzzer |
| 19 | └── tools |
| 20 | ├── backtrace.sh generates backtrace files for a crash directory |
| 21 | └── minimize.sh generates minimized crash or hang files |
| 22 | ``` |
| 23 | |
| 24 | ## Usage |
| 25 | |
| 26 | There are currently three fuzzing targets |
| 27 | |
| 28 | - `pm_serialize_parse` (parse) |
| 29 | - `pm_regexp_parse` (regexp) |
| 30 | |
| 31 | Respectively, fuzzing can be performed with |
| 32 | |
| 33 | ``` |
| 34 | make fuzz-run-parse |
| 35 | make fuzz-run-regexp |
| 36 | ``` |
| 37 | |
| 38 | To end a fuzzing job, interrupt with CTRL+C. To enter a container with the fuzzing toolchain and debug utilities, run |
| 39 | |
| 40 | ``` |
| 41 | make fuzz-debug |
| 42 | ``` |
| 43 | |
| 44 | # Out-of-bounds reads |
| 45 | |
| 46 | Currently, encoding functionality implementing the `pm_encoding_t` interface can read outside of inputs. For the time being, ASAN instrumentation is disabled for functions from src/enc. See `fuzz/asan.ignore`. |
| 47 | |
| 48 | To disable ASAN read instrumentation globally, use the `FUZZ_FLAGS` environment variable e.g. |
| 49 | |
| 50 | ``` |
| 51 | FUZZ_FLAGS="-mllvm -asan-instrument-reads=false" make fuzz-run-parse |
| 52 | ``` |
| 53 | |
| 54 | Note, that this may make reproducing bugs difficult as they may depend on memory outside of the input buffer. In that case, try |
| 55 | |
| 56 | ``` |
| 57 | make fuzz-debug # enter the docker container with build tools |
| 58 | make build/fuzz.heisenbug.parse # or .regexp |
| 59 | ./build/fuzz.heisenbug.parse path-to-problem-input |
| 60 | ``` |
| 61 | |
| 62 | # Triaging Crashes and Hangs |
| 63 | |
| 64 | Triaging crashes and hangs is easier when the inputs are as short as possible. In the fuzz container, an entire crash or hang directory can be minimized using |
| 65 | |
| 66 | ``` |
| 67 | ./fuzz/tools/minimize.sh <directory> |
| 68 | ``` |
| 69 | |
| 70 | e.g. |
| 71 | ``` |
| 72 | ./fuzz/tools/minimize.sh fuzz/output/parse/default/crashes |
| 73 | ``` |
| 74 | |
| 75 | This may take a long time. In the crash/hang directory, for each input file there will appear a minimized version with the extension `.min` appended. |
| 76 | |
| 77 | Backtraces for crashes (not hangs) can be generated en masse with |
| 78 | |
| 79 | ``` |
| 80 | ./fuzz/tools/backtrace.sh <directory> |
| 81 | ``` |
| 82 | |
| 83 | Files with basename equal to the input file name with extension `.bt` will be created e.g. |
| 84 | |
| 85 | ``` |
| 86 | id:000000,sig:06,src:000006+000190,time:8480,execs:18929,op:splice,rep:4 |
| 87 | id:000000,sig:06,src:000006+000190,time:8480,execs:18929,op:splice,rep:4.bt |
| 88 | ``` |