Merge #12063: [Trivial] Update license year range to 2018
[bitcoinplatinum.git] / doc / fuzzing.md
blob5dedcb51c895e612c6953b99ca3bb532899cb4aa
1 Fuzz-testing Bitcoin Core
2 ==========================
4 A special test harness `test_bitcoin_fuzzy` is provided to provide an easy
5 entry point for fuzzers and the like. In this document we'll describe how to
6 use it with AFL.
8 Building AFL
9 -------------
11 It is recommended to always use the latest version of afl:
12 ```
13 wget http://lcamtuf.coredump.cx/afl/releases/afl-latest.tgz
14 tar -zxvf afl-latest.tgz
15 cd afl-<version>
16 make
17 export AFLPATH=$PWD
18 ```
20 Instrumentation
21 ----------------
23 To build Bitcoin Core using AFL instrumentation (this assumes that the
24 `AFLPATH` was set as above):
25 ```
26 ./configure --disable-ccache --disable-shared --enable-tests CC=${AFLPATH}/afl-gcc CXX=${AFLPATH}/afl-g++
27 export AFL_HARDEN=1
28 cd src/
29 make test/test_bitcoin_fuzzy
30 ```
31 We disable ccache because we don't want to pollute the ccache with instrumented
32 objects, and similarly don't want to use non-instrumented cached objects linked
33 in.
35 The fuzzing can be sped up significantly (~200x) by using `afl-clang-fast` and
36 `afl-clang-fast++` in place of `afl-gcc` and `afl-g++` when compiling. When
37 compiling using `afl-clang-fast`/`afl-clang-fast++` the resulting
38 `test_bitcoin_fuzzy` binary will be instrumented in such a way that the AFL
39 features "persistent mode" and "deferred forkserver" can be used. See
40 https://github.com/mcarpenter/afl/tree/master/llvm_mode for details.
42 Preparing fuzzing
43 ------------------
45 AFL needs an input directory with examples, and an output directory where it
46 will place examples that it found. These can be anywhere in the file system,
47 we'll define environment variables to make it easy to reference them.
49 ```
50 mkdir inputs
51 AFLIN=$PWD/inputs
52 mkdir outputs
53 AFLOUT=$PWD/outputs
54 ```
56 Example inputs are available from:
58 - https://download.visucore.com/bitcoin/bitcoin_fuzzy_in.tar.xz
59 - http://strateman.ninja/fuzzing.tar.xz
61 Extract these (or other starting inputs) into the `inputs` directory before starting fuzzing.
63 Fuzzing
64 --------
66 To start the actual fuzzing use:
67 ```
68 $AFLPATH/afl-fuzz -i ${AFLIN} -o ${AFLOUT} -m52 -- test/test_bitcoin_fuzzy
69 ```
71 You may have to change a few kernel parameters to test optimally - `afl-fuzz`
72 will print an error and suggestion if so.