2017-06-29 23:53:52 +00:00
|
|
|
# Fuzzing
|
|
|
|
|
|
|
|
Each fuzzing target can be built with multiple engines.
|
2017-09-25 20:32:50 +00:00
|
|
|
Zstd provides a fuzz corpus for each target that can be downloaded with
|
|
|
|
the command:
|
|
|
|
|
|
|
|
```
|
|
|
|
make corpora
|
|
|
|
```
|
|
|
|
|
|
|
|
It will download each corpus into `./corpora/TARGET`.
|
2017-06-29 23:53:52 +00:00
|
|
|
|
2017-09-14 01:18:35 +00:00
|
|
|
## fuzz.py
|
|
|
|
|
|
|
|
`fuzz.py` is a helper script for building and running fuzzers.
|
|
|
|
Run `./fuzz.py -h` for the commands and run `./fuzz.py COMMAND -h` for
|
|
|
|
command specific help.
|
|
|
|
|
|
|
|
### Generating Data
|
|
|
|
|
|
|
|
`fuzz.py` provides a utility to generate seed data for each fuzzer.
|
|
|
|
|
|
|
|
```
|
|
|
|
make -C ../tests decodecorpus
|
|
|
|
./fuzz.py gen TARGET
|
|
|
|
```
|
|
|
|
|
|
|
|
By default it outputs 100 samples, each at most 8KB into `corpora/TARGET-seed`,
|
|
|
|
but that can be configured with the `--number`, `--max-size-log` and `--seed`
|
|
|
|
flags.
|
|
|
|
|
|
|
|
### Build
|
|
|
|
It respects the usual build environment variables `CC`, `CFLAGS`, etc.
|
|
|
|
The environment variables can be overridden with the corresponding flags
|
|
|
|
`--cc`, `--cflags`, etc.
|
|
|
|
The specific fuzzing engine is selected with `LIB_FUZZING_ENGINE` or
|
|
|
|
`--lib-fuzzing-engine`, the default is `libregression.a`.
|
2019-08-20 18:33:33 +00:00
|
|
|
Alternatively, you can use Clang's built in fuzzing engine with
|
|
|
|
`--enable-fuzzer`.
|
2017-09-14 01:18:35 +00:00
|
|
|
It has flags that can easily set up sanitizers `--enable-{a,ub,m}san`, and
|
|
|
|
coverage instrumentation `--enable-coverage`.
|
2019-04-12 18:18:11 +00:00
|
|
|
It sets sane defaults which can be overridden with flags `--debug`,
|
|
|
|
`--enable-ubsan-pointer-overflow`, etc.
|
2017-09-14 01:18:35 +00:00
|
|
|
Run `./fuzz.py build -h` for help.
|
|
|
|
|
|
|
|
### Running Fuzzers
|
|
|
|
|
|
|
|
`./fuzz.py` can run `libfuzzer`, `afl`, and `regression` tests.
|
|
|
|
See the help of the relevant command for options.
|
|
|
|
Flags not parsed by `fuzz.py` are passed to the fuzzing engine.
|
|
|
|
The command used to run the fuzzer is printed for debugging.
|
|
|
|
|
2017-06-29 23:53:52 +00:00
|
|
|
## LibFuzzer
|
|
|
|
|
2017-09-14 01:18:35 +00:00
|
|
|
```
|
|
|
|
# Build the fuzz targets
|
2019-08-20 18:33:33 +00:00
|
|
|
./fuzz.py build all --enable-fuzzer --enable-asan --enable-ubsan --cc clang --cxx clang++
|
2017-09-14 01:18:35 +00:00
|
|
|
# OR equivalently
|
2019-08-20 18:33:33 +00:00
|
|
|
CC=clang CXX=clang++ ./fuzz.py build all --enable-fuzzer --enable-asan --enable-ubsan
|
2017-09-14 01:18:35 +00:00
|
|
|
# Run the fuzzer
|
2019-08-20 18:33:33 +00:00
|
|
|
./fuzz.py libfuzzer TARGET <libfuzzer args like -jobs=4>
|
2017-09-14 01:18:35 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
where `TARGET` could be `simple_decompress`, `stream_round_trip`, etc.
|
|
|
|
|
|
|
|
### MSAN
|
|
|
|
|
2019-08-20 18:33:33 +00:00
|
|
|
Fuzzing with `libFuzzer` and `MSAN` is as easy as:
|
|
|
|
|
|
|
|
```
|
|
|
|
CC=clang CXX=clang++ ./fuzz.py build all --enable-fuzzer --enable-msan
|
|
|
|
./fuzz.py libfuzzer TARGET <libfuzzer args>
|
|
|
|
```
|
|
|
|
|
2017-09-14 01:18:35 +00:00
|
|
|
`fuzz.py` respects the environment variables / flags `MSAN_EXTRA_CPPFLAGS`,
|
|
|
|
`MSAN_EXTRA_CFLAGS`, `MSAN_EXTRA_CXXFLAGS`, `MSAN_EXTRA_LDFLAGS` to easily pass
|
|
|
|
the extra parameters only for MSAN.
|
2017-06-29 23:53:52 +00:00
|
|
|
|
|
|
|
## AFL
|
|
|
|
|
2017-09-14 01:18:35 +00:00
|
|
|
The default `LIB_FUZZING_ENGINE` is `libregression.a`, which produces a binary
|
|
|
|
that AFL can use.
|
2017-06-29 23:53:52 +00:00
|
|
|
|
|
|
|
```
|
2017-09-14 01:18:35 +00:00
|
|
|
# Build the fuzz targets
|
|
|
|
CC=afl-clang CXX=afl-clang++ ./fuzz.py build all --enable-asan --enable-ubsan
|
|
|
|
# Run the fuzzer without a memory limit because of ASAN
|
|
|
|
./fuzz.py afl TARGET -m none
|
2017-06-29 23:53:52 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Regression Testing
|
|
|
|
|
2019-09-09 15:43:22 +00:00
|
|
|
The regression test supports the `all` target to run all the fuzzers in one
|
2017-09-14 01:18:35 +00:00
|
|
|
command.
|
2017-06-29 23:53:52 +00:00
|
|
|
|
|
|
|
```
|
2017-09-14 01:18:35 +00:00
|
|
|
CC=clang CXX=clang++ ./fuzz.py build all --enable-asan --enable-ubsan
|
|
|
|
./fuzz.py regression all
|
|
|
|
CC=clang CXX=clang++ ./fuzz.py build all --enable-msan
|
|
|
|
./fuzz.py regression all
|
2017-06-29 23:53:52 +00:00
|
|
|
```
|