Skip to content

Commit 693247b

Browse files
[test] Speed up fuzzing by ~200x when using afl-fuzz
Enable the `afl-clang-fast++` features deferred forkserver (`__AFL_INIT`) and persistent mode (`__AFL_LOOP(1000)`). Before this patch: ``` $ afl-fuzz -i input -o output -m512 -- src/test/test_bitcoin_fuzzy [*] Validating target binary... [!] WARNING: The target binary is pretty slow! See /usr/local/share/doc/afl/perf_tips.txt. [+] Here are some useful stats: Test case count : 1 favored, 0 variable, 1 total Bitmap range : 1072 to 1072 bits (average: 1072.00 bits) Exec timing : 20.4k to 20.4k us (average: 20.4k us) … exec speed : 57.58/sec (slow!) exec speed : 48.35/sec (slow!) exec speed : 53.78/sec (slow!) ``` After this patch: ``` $ afl-fuzz -i input -o output -m512 -- src/test/test_bitcoin_fuzzy [*] Validating target binary... [+] Persistent mode binary detected. [+] Deferred forkserver binary detected. [+] Here are some useful stats: Test case count : 1 favored, 0 variable, 1 total Bitmap range : 24 to 24 bits (average: 24.00 bits) Exec timing : 114 to 114 us (average: 114 us) … exec speed : 15.9k/sec exec speed : 13.1k/sec exec speed : 15.1k/sec ```
1 parent 95546c8 commit 693247b

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

doc/fuzzing.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ We disable ccache because we don't want to pollute the ccache with instrumented
3232
objects, and similarly don't want to use non-instrumented cached objects linked
3333
in.
3434

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.
41+
3542
Preparing fuzzing
3643
------------------
3744

@@ -63,4 +70,3 @@ $AFLPATH/afl-fuzz -i ${AFLIN} -o ${AFLOUT} -m52 -- test/test_bitcoin_fuzzy
6370

6471
You may have to change a few kernel parameters to test optimally - `afl-fuzz`
6572
will print an error and suggestion if so.
66-

src/test/test_bitcoin_fuzzy.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ bool read_stdin(std::vector<char> &data) {
5959
return length==0;
6060
}
6161

62-
int main(int argc, char **argv)
62+
int do_fuzz()
6363
{
64-
ECCVerifyHandle globalVerifyHandle;
6564
std::vector<char> buffer;
6665
if (!read_stdin(buffer)) return 0;
6766

@@ -256,3 +255,23 @@ int main(int argc, char **argv)
256255
return 0;
257256
}
258257

258+
int main(int argc, char **argv)
259+
{
260+
ECCVerifyHandle globalVerifyHandle;
261+
#ifdef __AFL_INIT
262+
// Enable AFL deferred forkserver mode. Requires compilation using
263+
// afl-clang-fast++. See fuzzing.md for details.
264+
__AFL_INIT();
265+
#endif
266+
267+
#ifdef __AFL_LOOP
268+
// Enable AFL persistent mode. Requires compilation using afl-clang-fast++.
269+
// See fuzzing.md for details.
270+
while (__AFL_LOOP(1000)) {
271+
do_fuzz();
272+
}
273+
return 0;
274+
#else
275+
return do_fuzz();
276+
#endif
277+
}

0 commit comments

Comments
 (0)