Skip to content

Commit 217b416

Browse files
committed
Merge #10415: [tests] Speed up fuzzing by ~200x when using afl-fuzz
693247b [test] Speed up fuzzing by ~200x when using afl-fuzz (practicalswift) Tree-SHA512: 95922fc2616b8cb00dd531ed1140a52bbda4e04292dd8c1c60a8f49dbf6ccb797a18b61180b3fb68d695456b478a1f5ae7fda47e8ecee41dd65555487aef40a3
2 parents 6512913 + 693247b commit 217b416

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)