forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 720
Fuzzing framework support #2252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
random-zebra
merged 33 commits into
PIVX-Project:master
from
furszy:2020_fuzzing_framework
May 28, 2021
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
5c3b550
Simple fuzzing framework
pstratem d6f6a85
doc: Add bare-bones documentation for fuzzing
laanwj 11150df
Make fuzzer actually test CTxOutCompressor
sipa faf2be6
Init ECC context for test_bitcoin_fuzzy.
gmaxwell 84f72da
[test] Speed up fuzzing by ~200x when using afl-fuzz
practicalswift 08d8ebe
[tests] Add libFuzzer support.
practicalswift 2e4ec58
[fuzzing] initialize chain params by default.
furszy d5dddde
[test] fuzz: make test_one_input return void
a568df5
test: Build fuzz targets into separate executables
furszy 393a126
fuzz: Move deserialize tests to test/fuzz/deserialize.cpp
58dbe79
add fuzzing binaries to gitignore.
furszy 89fe5b2
Add missing LIBBITCOIN_ZMQ to test target
furszy 541f442
qa: Add test/fuzz/test_runner.py
425742c
fuzz: test_runner: Better error message when built with afl
f28ac9a
build: Allow to configure --with-sanitizers=fuzzer
1266d3e
Disable other targets when enable-fuzz is set
qmma70 c3447b5
Update doc and CI config
qmma70 d642b67
[Build] Do not disable wallet when fuzz is enabled.
furszy 48cd0c8
doc: Improve fuzzing docs for macOS users
fjahr cd6134f
test: Log output even if fuzzer failed
b54b1d6
tests: Improve test runner output in case of target errors
practicalswift 2b4f8aa
doc: Remove --disable-ccache from docs
52693ee
fuzz: Add option to merge input dir to test runner
3205871
fuzz: Remove option --export_coverage from test_runner
b5f291c
tests: Add fuzzing harness for CheckTransaction(...), IsStandardTx(..…
furszy e1f666c
tests: Remove TRANSACTION_DESERIALIZE (replaced by transaction fuzzer)
practicalswift d058d8c
tests: Add deserialization fuzzing harnesses
furszy e1b92b6
ignore new fuzz targets gitignore
furszy 70a0ace
tests: Test serialisation as part of deserialisation fuzzing. Test ro…
practicalswift 9631f46
[doc] add sanitizers documentation in developer-notes.md
furszy f0887a0
Fuzzing documentation "PIVX-fication"
furszy 2396e6b
[fuzz] Add ContextualCheckTransaction call to transaction target.
furszy d059544
[Build] fuzz target, change LIBBITCOIN_ZEROCOIN link order.
furszy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| Fuzz-testing PIVX Core | ||
| ========================== | ||
|
|
||
| A special test harness in `src/test/fuzz/` is provided for each fuzz target to | ||
| provide an easy entry point for fuzzers and the like. In this document we'll | ||
| describe how to use it with AFL and libFuzzer. | ||
|
|
||
| ## Preparing fuzzing | ||
|
|
||
| The fuzzer needs some inputs to work on, but the inputs or seeds can be used | ||
| interchangeably between libFuzzer and AFL. | ||
|
|
||
| Extract the example seeds (or other starting inputs) into the inputs | ||
| directory before starting fuzzing. | ||
|
|
||
| ``` | ||
| git clone https://github.com/bitcoin-core/qa-assets | ||
| export DIR_FUZZ_IN=$PWD/qa-assets/fuzz_seed_corpus | ||
| ``` | ||
|
|
||
| AFL needs an input directory with examples, and an output directory where it | ||
| will place examples that it found. These can be anywhere in the file system, | ||
| we'll define environment variables to make it easy to reference them. | ||
|
|
||
| So, only for AFL you need to configure the outputs path: | ||
|
|
||
| ``` | ||
| mkdir outputs | ||
| export AFLOUT=$PWD/outputs | ||
| ``` | ||
|
|
||
| libFuzzer will use the input directory as output directory. | ||
|
|
||
| ## AFL | ||
|
|
||
| ### Building AFL | ||
|
|
||
| It is recommended to always use the latest version of afl: | ||
| ``` | ||
| wget http://lcamtuf.coredump.cx/afl/releases/afl-latest.tgz | ||
| tar -zxvf afl-latest.tgz | ||
| cd afl-<version> | ||
| make | ||
| export AFLPATH=$PWD | ||
| ``` | ||
|
|
||
| For macOS you may need to ignore x86 compilation checks when running `make`: | ||
| `AFL_NO_X86=1 make`. | ||
|
|
||
| ### Instrumentation | ||
|
|
||
| To build PIVX Core using AFL instrumentation (this assumes that the | ||
| `AFLPATH` was set as above): | ||
| ``` | ||
| ./configure --disable-shared --enable-tests --enable-fuzz CC=${AFLPATH}/afl-gcc CXX=${AFLPATH}/afl-g++ | ||
| export AFL_HARDEN=1 | ||
| make | ||
| ``` | ||
|
|
||
| If you are using clang you will need to substitute `afl-gcc` with `afl-clang` | ||
| and `afl-g++` with `afl-clang++`, so the first line above becomes: | ||
| ``` | ||
| ./configure --disable-shared --enable-tests --enable-fuzz CC=${AFLPATH}/afl-clang CXX=${AFLPATH}/afl-clang++ | ||
| ``` | ||
|
|
||
| We disable ccache because we don't want to pollute the ccache with instrumented | ||
| objects, and similarly don't want to use non-instrumented cached objects linked | ||
| in. | ||
|
|
||
| The fuzzing can be sped up significantly (~200x) by using `afl-clang-fast` and | ||
| `afl-clang-fast++` in place of `afl-gcc` and `afl-g++` when compiling. When | ||
| compiling using `afl-clang-fast`/`afl-clang-fast++` the resulting | ||
| binary will be instrumented in such a way that the AFL | ||
| features "persistent mode" and "deferred forkserver" can be used. See | ||
| https://github.com/google/AFL/tree/master/llvm_mode for details. | ||
|
|
||
| ### Fuzzing | ||
|
|
||
| To start the actual fuzzing use: | ||
|
|
||
| ``` | ||
| export FUZZ_TARGET=bech32 # Pick a fuzz_target | ||
| mkdir ${AFLOUT}/${FUZZ_TARGET} | ||
| $AFLPATH/afl-fuzz -i ${DIR_FUZZ_IN}/${FUZZ_TARGET} -o ${AFLOUT}/${FUZZ_TARGET} -m52 -- src/test/fuzz/${FUZZ_TARGET} | ||
| ``` | ||
|
|
||
| You may have to change a few kernel parameters to test optimally - `afl-fuzz` | ||
| will print an error and suggestion if so. | ||
|
|
||
| On macOS you may need to set `AFL_NO_FORKSRV=1` to get the target to run. | ||
| ``` | ||
| export FUZZ_TARGET=bech32 # Pick a fuzz_target | ||
| mkdir ${AFLOUT}/${FUZZ_TARGET} | ||
| AFL_NO_FORKSRV=1 $AFLPATH/afl-fuzz -i ${DIR_FUZZ_IN}/${FUZZ_TARGET} -o ${AFLOUT}/${FUZZ_TARGET} -m52 -- src/test/fuzz/${FUZZ_TARGET} | ||
| ``` | ||
|
|
||
| ## libFuzzer | ||
|
|
||
| A recent version of `clang`, the address sanitizer and libFuzzer is needed (all | ||
| found in the `compiler-rt` runtime libraries package). | ||
|
|
||
| To build all fuzz targets with libFuzzer, run | ||
|
|
||
| ``` | ||
| ./configure --enable-fuzz --with-sanitizers=fuzzer,address CC=clang CXX=clang++ | ||
| make | ||
| ``` | ||
|
|
||
| See https://llvm.org/docs/LibFuzzer.html#running on how to run the libFuzzer | ||
| instrumented executable. | ||
|
|
||
| Alternatively, you can run the script through the fuzzing test harness (only | ||
| libFuzzer supported so far). You need to pass it the inputs directory and | ||
| the specific test target you want to run. | ||
|
|
||
| ``` | ||
| ./test/fuzz/test_runner.py ${DIR_FUZZ_IN} bech32 | ||
| ``` | ||
|
|
||
| ### macOS hints for libFuzzer | ||
|
|
||
| The default clang/llvm version supplied by Apple on macOS does not include | ||
| fuzzing libraries, so macOS users will need to install a full version, for | ||
| example using `brew install llvm`. | ||
|
|
||
| Should you run into problems with the address sanitizer, it is possible you | ||
| may need to run `./configure` with `--disable-asm` to avoid errors | ||
| with certain assembly code from PIVX Core's code. See [developer notes on sanitizers](https://github.com/PIVX-Project/PIVX/blob/master/doc/developer-notes.md#sanitizers) | ||
| for more information. | ||
|
|
||
| You may also need to take care of giving the correct path for clang and | ||
| clang++, like `CC=/path/to/clang CXX=/path/to/clang++` if the non-systems | ||
| clang does not come first in your path. | ||
|
|
||
| Full configure that was tested on macOS Catalina with `brew` installed `llvm`: | ||
| ``` | ||
| ./configure --enable-fuzz --with-sanitizers=fuzzer,address,undefined CC=/usr/local/opt/llvm/bin/clang CXX=/usr/local/opt/llvm/bin/clang++ --disable-asm | ||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are no longer using Travis, it would be awesome to have the same thing for GA in the future.