• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

MakefileH A D03-May-20224.5 KiB11369

README.llvmH A D26-Jan-20177.8 KiB191135

afl-clang-fast.cH A D03-May-20229.5 KiB359199

afl-llvm-pass.so.ccH A D22-Jun-20174.8 KiB18685

afl-llvm-rt.o.cH A D01-Feb-20176.9 KiB307120

README.llvm

1============================================
2Fast LLVM-based instrumentation for afl-fuzz
3============================================
4
5  (See ../docs/README for the general instruction manual.)
6
71) Introduction
8---------------
9
10The code in this directory allows you to instrument programs for AFL using
11true compiler-level instrumentation, instead of the more crude
12assembly-level rewriting approach taken by afl-gcc and afl-clang. This has
13several interesting properties:
14
15  - The compiler can make many optimizations that are hard to pull off when
16    manually inserting assembly. As a result, some slow, CPU-bound programs will
17    run up to around 2x faster.
18
19    The gains are less pronounced for fast binaries, where the speed is limited
20    chiefly by the cost of creating new processes. In such cases, the gain will
21    probably stay within 10%.
22
23  - The instrumentation is CPU-independent. At least in principle, you should
24    be able to rely on it to fuzz programs on non-x86 architectures (after
25    building afl-fuzz with AFL_NO_X86=1).
26
27  - The instrumentation can cope a bit better with multi-threaded targets.
28
29  - Because the feature relies on the internals of LLVM, it is clang-specific
30    and will *not* work with GCC.
31
32Once this implementation is shown to be sufficiently robust and portable, it
33will probably replace afl-clang. For now, it can be built separately and
34co-exists with the original code.
35
36The idea and much of the implementation comes from Laszlo Szekeres.
37
382) How to use
39-------------
40
41In order to leverage this mechanism, you need to have clang installed on your
42system. You should also make sure that the llvm-config tool is in your path
43(or pointed to via LLVM_CONFIG in the environment).
44
45Unfortunately, some systems that do have clang come without llvm-config or the
46LLVM development headers; one example of this is FreeBSD. FreeBSD users will
47also run into problems with clang being built statically and not being able to
48load modules (you'll see "Service unavailable" when loading afl-llvm-pass.so).
49
50To solve all your problems, you can grab pre-built binaries for your OS from:
51
52  http://llvm.org/releases/download.html
53
54...and then put the bin/ directory from the tarball at the beginning of your
55$PATH when compiling the feature and building packages later on. You don't need
56to be root for that.
57
58To build the instrumentation itself, type 'make'. This will generate binaries
59called afl-clang-fast and afl-clang-fast++ in the parent directory. Once this
60is done, you can instrument third-party code in a way similar to the standard
61operating mode of AFL, e.g.:
62
63  CC=/path/to/afl/afl-clang-fast ./configure [...options...]
64  make
65
66Be sure to also include CXX set to afl-clang-fast++ for C++ code.
67
68The tool honors roughly the same environmental variables as afl-gcc (see
69../docs/env_variables.txt). This includes AFL_INST_RATIO, AFL_USE_ASAN,
70AFL_HARDEN, and AFL_DONT_OPTIMIZE.
71
72Note: if you want the LLVM helper to be installed on your system for all
73users, you need to build it before issuing 'make install' in the parent
74directory.
75
763) Gotchas, feedback, bugs
77--------------------------
78
79This is an early-stage mechanism, so field reports are welcome. You can send bug
80reports to <afl-users@googlegroups.com>.
81
824) Bonus feature #1: deferred instrumentation
83---------------------------------------------
84
85AFL tries to optimize performance by executing the targeted binary just once,
86stopping it just before main(), and then cloning this "master" process to get
87a steady supply of targets to fuzz.
88
89Although this approach eliminates much of the OS-, linker- and libc-level
90costs of executing the program, it does not always help with binaries that
91perform other time-consuming initialization steps - say, parsing a large config
92file before getting to the fuzzed data.
93
94In such cases, it's beneficial to initialize the forkserver a bit later, once
95most of the initialization work is already done, but before the binary attempts
96to read the fuzzed input and parse it; in some cases, this can offer a 10x+
97performance gain. You can implement delayed initialization in LLVM mode in a
98fairly simple way.
99
100First, find a suitable location in the code where the delayed cloning can
101take place. This needs to be done with *extreme* care to avoid breaking the
102binary. In particular, the program will probably malfunction if you select
103a location after:
104
105  - The creation of any vital threads or child processes - since the forkserver
106    can't clone them easily.
107
108  - The initialization of timers via setitimer() or equivalent calls.
109
110  - The creation of temporary files, network sockets, offset-sensitive file
111    descriptors, and similar shared-state resources - but only provided that
112    their state meaningfully influences the behavior of the program later on.
113
114  - Any access to the fuzzed input, including reading the metadata about its
115    size.
116
117With the location selected, add this code in the appropriate spot:
118
119#ifdef __AFL_HAVE_MANUAL_CONTROL
120  __AFL_INIT();
121#endif
122
123You don't need the #ifdef guards, but including them ensures that the program
124will keep working normally when compiled with a tool other than afl-clang-fast.
125
126Finally, recompile the program with afl-clang-fast (afl-gcc or afl-clang will
127*not* generate a deferred-initialization binary) - and you should be all set!
128
1295) Bonus feature #2: persistent mode
130------------------------------------
131
132Some libraries provide APIs that are stateless, or whose state can be reset in
133between processing different input files. When such a reset is performed, a
134single long-lived process can be reused to try out multiple test cases,
135eliminating the need for repeated fork() calls and the associated OS overhead.
136
137The basic structure of the program that does this would be:
138
139  while (__AFL_LOOP(1000)) {
140
141    /* Read input data. */
142    /* Call library code to be fuzzed. */
143    /* Reset state. */
144
145  }
146
147  /* Exit normally */
148
149The numerical value specified within the loop controls the maximum number
150of iterations before AFL will restart the process from scratch. This minimizes
151the impact of memory leaks and similar glitches; 1000 is a good starting point,
152and going much higher increases the likelihood of hiccups without giving you
153any real performance benefits.
154
155A more detailed template is shown in ../experimental/persistent_demo/.
156Similarly to the previous mode, the feature works only with afl-clang-fast;
157#ifdef guards can be used to suppress it when using other compilers.
158
159Note that as with the previous mode, the feature is easy to misuse; if you
160do not fully reset the critical state, you may end up with false positives or
161waste a whole lot of CPU power doing nothing useful at all. Be particularly
162wary of memory leaks and of the state of file descriptors.
163
164PS. Because there are task switches still involved, the mode isn't as fast as
165"pure" in-process fuzzing offered, say, by LLVM's LibFuzzer; but it is a lot
166faster than the normal fork() model, and compared to in-process fuzzing,
167should be a lot more robust.
168
1696) Bonus feature #3: new 'trace-pc-guard' mode
170----------------------------------------------
171
172Recent versions of LLVM are shipping with a built-in execution tracing feature
173that provides AFL with the necessary tracing data without the need to
174post-process the assembly or install any compiler plugins. See:
175
176  http://clang.llvm.org/docs/SanitizerCoverage.html#tracing-pcs-with-guards
177
178As of this writing, the feature is only available on SVN trunk, and is yet to
179make it to an official release of LLVM. Nevertheless, if you have a
180sufficiently recent compiler and want to give it a try, build afl-clang-fast
181this way:
182
183  AFL_TRACE_PC=1 make clean all
184
185Note that this mode is currently about 20% slower than "vanilla" afl-clang-fast,
186and about 5-10% slower than afl-clang. This is likely because the
187instrumentation is not inlined, and instead involves a function call. On systems
188that support it, compiling your target with -flto should help.
189
190
191