Skip to content

Commit 84f72da

Browse files
practicalswiftfurszy
authored andcommitted
[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 faf2be6 commit 84f72da

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

doc/fuzzing.md

+7-1
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

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

61-
int main(int argc, char **argv)
61+
int do_fuzz()
6262
{
63-
ECCVerifyHandle globalVerifyHandle;
6463
std::vector<char> buffer;
6564
if (!read_stdin(buffer)) return 0;
6665

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

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

0 commit comments

Comments
 (0)