xref: /aosp_15_r20/external/AFLplusplus/instrumentation/README.llvm.md (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1*08b48e0bSAndroid Build Coastguard Worker# Fast LLVM-based instrumentation for afl-fuzz
2*08b48e0bSAndroid Build Coastguard Worker
3*08b48e0bSAndroid Build Coastguard WorkerFor the general instruction manual, see [docs/README.md](../docs/README.md).
4*08b48e0bSAndroid Build Coastguard Worker
5*08b48e0bSAndroid Build Coastguard WorkerFor the GCC-based instrumentation, see
6*08b48e0bSAndroid Build Coastguard Worker[README.gcc_plugin.md](README.gcc_plugin.md).
7*08b48e0bSAndroid Build Coastguard Worker
8*08b48e0bSAndroid Build Coastguard Worker## 1) Introduction
9*08b48e0bSAndroid Build Coastguard Worker
10*08b48e0bSAndroid Build Coastguard Worker! llvm_mode works with llvm versions 3.8 up to 17 - but 13+ is recommended !
11*08b48e0bSAndroid Build Coastguard Worker
12*08b48e0bSAndroid Build Coastguard WorkerThe code in this directory allows you to instrument programs for AFL++ using
13*08b48e0bSAndroid Build Coastguard Workertrue compiler-level instrumentation, instead of the more crude assembly-level
14*08b48e0bSAndroid Build Coastguard Workerrewriting approach taken by afl-gcc and afl-clang. This has several interesting
15*08b48e0bSAndroid Build Coastguard Workerproperties:
16*08b48e0bSAndroid Build Coastguard Worker
17*08b48e0bSAndroid Build Coastguard Worker- The compiler can make many optimizations that are hard to pull off when
18*08b48e0bSAndroid Build Coastguard Worker  manually inserting assembly. As a result, some slow, CPU-bound programs will
19*08b48e0bSAndroid Build Coastguard Worker  run up to around 2x faster.
20*08b48e0bSAndroid Build Coastguard Worker
21*08b48e0bSAndroid Build Coastguard Worker  The gains are less pronounced for fast binaries, where the speed is limited
22*08b48e0bSAndroid Build Coastguard Worker  chiefly by the cost of creating new processes. In such cases, the gain will
23*08b48e0bSAndroid Build Coastguard Worker  probably stay within 10%.
24*08b48e0bSAndroid Build Coastguard Worker
25*08b48e0bSAndroid Build Coastguard Worker- The instrumentation is CPU-independent. At least in principle, you should be
26*08b48e0bSAndroid Build Coastguard Worker  able to rely on it to fuzz programs on non-x86 architectures (after building
27*08b48e0bSAndroid Build Coastguard Worker  afl-fuzz with AFL_NO_X86=1).
28*08b48e0bSAndroid Build Coastguard Worker
29*08b48e0bSAndroid Build Coastguard Worker- The instrumentation can cope a bit better with multi-threaded targets.
30*08b48e0bSAndroid Build Coastguard Worker
31*08b48e0bSAndroid Build Coastguard Worker- Because the feature relies on the internals of LLVM, it is clang-specific and
32*08b48e0bSAndroid Build Coastguard Worker  will *not* work with GCC (see ../gcc_plugin/ for an alternative once it is
33*08b48e0bSAndroid Build Coastguard Worker  available).
34*08b48e0bSAndroid Build Coastguard Worker
35*08b48e0bSAndroid Build Coastguard WorkerOnce this implementation is shown to be sufficiently robust and portable, it
36*08b48e0bSAndroid Build Coastguard Workerwill probably replace afl-clang. For now, it can be built separately and
37*08b48e0bSAndroid Build Coastguard Workerco-exists with the original code.
38*08b48e0bSAndroid Build Coastguard Worker
39*08b48e0bSAndroid Build Coastguard WorkerThe idea and much of the initial implementation came from Laszlo Szekeres.
40*08b48e0bSAndroid Build Coastguard Worker
41*08b48e0bSAndroid Build Coastguard Worker## 2a) How to use this - short
42*08b48e0bSAndroid Build Coastguard Worker
43*08b48e0bSAndroid Build Coastguard WorkerSet the `LLVM_CONFIG` variable to the clang version you want to use, e.g.:
44*08b48e0bSAndroid Build Coastguard Worker
45*08b48e0bSAndroid Build Coastguard Worker```
46*08b48e0bSAndroid Build Coastguard WorkerLLVM_CONFIG=llvm-config-9 make
47*08b48e0bSAndroid Build Coastguard Worker```
48*08b48e0bSAndroid Build Coastguard Worker
49*08b48e0bSAndroid Build Coastguard WorkerIn case you have your own compiled llvm version specify the full path:
50*08b48e0bSAndroid Build Coastguard Worker
51*08b48e0bSAndroid Build Coastguard Worker```
52*08b48e0bSAndroid Build Coastguard WorkerLLVM_CONFIG=~/llvm-project/build/bin/llvm-config make
53*08b48e0bSAndroid Build Coastguard Worker```
54*08b48e0bSAndroid Build Coastguard Worker
55*08b48e0bSAndroid Build Coastguard WorkerIf you try to use a new llvm version on an old Linux this can fail because of
56*08b48e0bSAndroid Build Coastguard Workerold c++ libraries. In this case usually switching to gcc/g++ to compile
57*08b48e0bSAndroid Build Coastguard Workerllvm_mode will work:
58*08b48e0bSAndroid Build Coastguard Worker
59*08b48e0bSAndroid Build Coastguard Worker```
60*08b48e0bSAndroid Build Coastguard WorkerLLVM_CONFIG=llvm-config-7 REAL_CC=gcc REAL_CXX=g++ make
61*08b48e0bSAndroid Build Coastguard Worker```
62*08b48e0bSAndroid Build Coastguard Worker
63*08b48e0bSAndroid Build Coastguard WorkerIt is highly recommended to use the newest clang version you can put your hands
64*08b48e0bSAndroid Build Coastguard Workeron :)
65*08b48e0bSAndroid Build Coastguard Worker
66*08b48e0bSAndroid Build Coastguard WorkerThen look at [README.persistent_mode.md](README.persistent_mode.md).
67*08b48e0bSAndroid Build Coastguard Worker
68*08b48e0bSAndroid Build Coastguard Worker## 2b) How to use this - long
69*08b48e0bSAndroid Build Coastguard Worker
70*08b48e0bSAndroid Build Coastguard WorkerIn order to leverage this mechanism, you need to have clang installed on your
71*08b48e0bSAndroid Build Coastguard Workersystem. You should also make sure that the llvm-config tool is in your path (or
72*08b48e0bSAndroid Build Coastguard Workerpointed to via LLVM_CONFIG in the environment).
73*08b48e0bSAndroid Build Coastguard Worker
74*08b48e0bSAndroid Build Coastguard WorkerNote that if you have several LLVM versions installed, pointing LLVM_CONFIG to
75*08b48e0bSAndroid Build Coastguard Workerthe version you want to use will switch compiling to this specific version - if
76*08b48e0bSAndroid Build Coastguard Workeryou installation is set up correctly :-)
77*08b48e0bSAndroid Build Coastguard Worker
78*08b48e0bSAndroid Build Coastguard WorkerUnfortunately, some systems that do have clang come without llvm-config or the
79*08b48e0bSAndroid Build Coastguard WorkerLLVM development headers; one example of this is FreeBSD. FreeBSD users will
80*08b48e0bSAndroid Build Coastguard Workeralso run into problems with clang being built statically and not being able to
81*08b48e0bSAndroid Build Coastguard Workerload modules (you'll see "Service unavailable" when loading afl-llvm-pass.so).
82*08b48e0bSAndroid Build Coastguard Worker
83*08b48e0bSAndroid Build Coastguard WorkerTo solve all your problems, you can grab pre-built binaries for your OS from:
84*08b48e0bSAndroid Build Coastguard Worker
85*08b48e0bSAndroid Build Coastguard Worker[https://llvm.org/releases/download.html](https://llvm.org/releases/download.html)
86*08b48e0bSAndroid Build Coastguard Worker
87*08b48e0bSAndroid Build Coastguard Worker...and then put the bin/ directory from the tarball at the beginning of your
88*08b48e0bSAndroid Build Coastguard Worker$PATH when compiling the feature and building packages later on. You don't need
89*08b48e0bSAndroid Build Coastguard Workerto be root for that.
90*08b48e0bSAndroid Build Coastguard Worker
91*08b48e0bSAndroid Build Coastguard WorkerTo build the instrumentation itself, type `make`. This will generate binaries
92*08b48e0bSAndroid Build Coastguard Workercalled afl-clang-fast and afl-clang-fast++ in the parent directory. Once this is
93*08b48e0bSAndroid Build Coastguard Workerdone, you can instrument third-party code in a way similar to the standard
94*08b48e0bSAndroid Build Coastguard Workeroperating mode of AFL, e.g.:
95*08b48e0bSAndroid Build Coastguard Worker
96*08b48e0bSAndroid Build Coastguard Worker```
97*08b48e0bSAndroid Build Coastguard Worker  CC=/path/to/afl/afl-clang-fast ./configure [...options...]
98*08b48e0bSAndroid Build Coastguard Worker  make
99*08b48e0bSAndroid Build Coastguard Worker```
100*08b48e0bSAndroid Build Coastguard Worker
101*08b48e0bSAndroid Build Coastguard WorkerBe sure to also include CXX set to afl-clang-fast++ for C++ code.
102*08b48e0bSAndroid Build Coastguard Worker
103*08b48e0bSAndroid Build Coastguard WorkerNote that afl-clang-fast/afl-clang-fast++ are just pointers to afl-cc. You can
104*08b48e0bSAndroid Build Coastguard Workeralso use afl-cc/afl-c++ and instead direct it to use LLVM instrumentation by
105*08b48e0bSAndroid Build Coastguard Workereither setting `AFL_CC_COMPILER=LLVM` or pass the parameter `--afl-llvm` via
106*08b48e0bSAndroid Build Coastguard WorkerCFLAGS/CXXFLAGS/CPPFLAGS.
107*08b48e0bSAndroid Build Coastguard Worker
108*08b48e0bSAndroid Build Coastguard WorkerThe tool honors roughly the same environmental variables as afl-gcc (see
109*08b48e0bSAndroid Build Coastguard Worker[docs/env_variables.md](../docs/env_variables.md)). This includes
110*08b48e0bSAndroid Build Coastguard Worker`AFL_USE_ASAN`, `AFL_HARDEN`, and `AFL_DONT_OPTIMIZE`. However, `AFL_INST_RATIO`
111*08b48e0bSAndroid Build Coastguard Workeris not honored as it does not serve a good purpose with the more effective
112*08b48e0bSAndroid Build Coastguard WorkerPCGUARD analysis.
113*08b48e0bSAndroid Build Coastguard Worker
114*08b48e0bSAndroid Build Coastguard Worker## 3) Options
115*08b48e0bSAndroid Build Coastguard Worker
116*08b48e0bSAndroid Build Coastguard WorkerSeveral options are present to make llvm_mode faster or help it rearrange the
117*08b48e0bSAndroid Build Coastguard Workercode to make afl-fuzz path discovery easier.
118*08b48e0bSAndroid Build Coastguard Worker
119*08b48e0bSAndroid Build Coastguard WorkerIf you need just to instrument specific parts of the code, you can create the
120*08b48e0bSAndroid Build Coastguard Workerinstrument file list which C/C++ files to actually instrument. See
121*08b48e0bSAndroid Build Coastguard Worker[README.instrument_list.md](README.instrument_list.md)
122*08b48e0bSAndroid Build Coastguard Worker
123*08b48e0bSAndroid Build Coastguard WorkerFor splitting memcmp, strncmp, etc., see
124*08b48e0bSAndroid Build Coastguard Worker[README.laf-intel.md](README.laf-intel.md).
125*08b48e0bSAndroid Build Coastguard Worker
126*08b48e0bSAndroid Build Coastguard WorkerThen there are different ways of instrumenting the target:
127*08b48e0bSAndroid Build Coastguard Worker
128*08b48e0bSAndroid Build Coastguard Worker1. A better instrumentation strategy uses LTO and link time instrumentation.
129*08b48e0bSAndroid Build Coastguard Worker   Note that not all targets can compile in this mode, however, if it works it
130*08b48e0bSAndroid Build Coastguard Worker   is the best option you can use. To go with this option, use
131*08b48e0bSAndroid Build Coastguard Worker   afl-clang-lto/afl-clang-lto++. See [README.lto.md](README.lto.md).
132*08b48e0bSAndroid Build Coastguard Worker
133*08b48e0bSAndroid Build Coastguard Worker2. Alternatively you can choose a completely different coverage method:
134*08b48e0bSAndroid Build Coastguard Worker
135*08b48e0bSAndroid Build Coastguard Worker2a. N-GRAM coverage - which combines the previous visited edges with the current
136*08b48e0bSAndroid Build Coastguard Worker    one. This explodes the map but on the other hand has proven to be effective
137*08b48e0bSAndroid Build Coastguard Worker    for fuzzing. See
138*08b48e0bSAndroid Build Coastguard Worker    [7) AFL++ N-Gram Branch Coverage](#7-afl-n-gram-branch-coverage).
139*08b48e0bSAndroid Build Coastguard Worker
140*08b48e0bSAndroid Build Coastguard Worker2b. Context sensitive coverage - which combines the visited edges with an
141*08b48e0bSAndroid Build Coastguard Worker    individual caller ID (the function that called the current one). See
142*08b48e0bSAndroid Build Coastguard Worker    [6) AFL++ Context Sensitive Branch Coverage](#6-afl-context-sensitive-branch-coverage).
143*08b48e0bSAndroid Build Coastguard Worker
144*08b48e0bSAndroid Build Coastguard WorkerThen - additionally to one of the instrumentation options above - there is a
145*08b48e0bSAndroid Build Coastguard Workervery effective new instrumentation option called CmpLog as an alternative to
146*08b48e0bSAndroid Build Coastguard Workerlaf-intel that allow AFL++ to apply mutations similar to Redqueen. See
147*08b48e0bSAndroid Build Coastguard Worker[README.cmplog.md](README.cmplog.md).
148*08b48e0bSAndroid Build Coastguard Worker
149*08b48e0bSAndroid Build Coastguard WorkerFinally, if your llvm version is 8 or lower, you can activate a mode that
150*08b48e0bSAndroid Build Coastguard Workerprevents that a counter overflow result in a 0 value. This is good for path
151*08b48e0bSAndroid Build Coastguard Workerdiscovery, but the llvm implementation for x86 for this functionality is not
152*08b48e0bSAndroid Build Coastguard Workeroptimal and was only fixed in llvm 9. You can set this with AFL_LLVM_NOT_ZERO=1.
153*08b48e0bSAndroid Build Coastguard Worker
154*08b48e0bSAndroid Build Coastguard WorkerSupport for thread safe counters has been added for all modes. Activate it with
155*08b48e0bSAndroid Build Coastguard Worker`AFL_LLVM_THREADSAFE_INST=1`. The tradeoff is better precision in multi threaded
156*08b48e0bSAndroid Build Coastguard Workerapps for a slightly higher instrumentation overhead. This also disables the
157*08b48e0bSAndroid Build Coastguard Workernozero counter default for performance reasons.
158*08b48e0bSAndroid Build Coastguard Worker
159*08b48e0bSAndroid Build Coastguard Worker## 4) deferred initialization, persistent mode, shared memory fuzzing
160*08b48e0bSAndroid Build Coastguard Worker
161*08b48e0bSAndroid Build Coastguard WorkerThis is the most powerful and effective fuzzing you can do. For a full
162*08b48e0bSAndroid Build Coastguard Workerexplanation, see [README.persistent_mode.md](README.persistent_mode.md).
163*08b48e0bSAndroid Build Coastguard Worker
164*08b48e0bSAndroid Build Coastguard Worker## 5) Bonus feature: 'dict2file' pass
165*08b48e0bSAndroid Build Coastguard Worker
166*08b48e0bSAndroid Build Coastguard WorkerJust specify `AFL_LLVM_DICT2FILE=/absolute/path/file.txt` and during compilation
167*08b48e0bSAndroid Build Coastguard Workerall constant string compare parameters will be written to this file to be used
168*08b48e0bSAndroid Build Coastguard Workerwith afl-fuzz' `-x` option.
169*08b48e0bSAndroid Build Coastguard Worker
170*08b48e0bSAndroid Build Coastguard WorkerAdding `AFL_LLVM_DICT2FILE_NO_MAIN=1` will skip parsing `main()` which often
171*08b48e0bSAndroid Build Coastguard Workerdoes command line parsing which has string comparisons that are not helpful
172*08b48e0bSAndroid Build Coastguard Workerfor fuzzing.
173*08b48e0bSAndroid Build Coastguard Worker
174*08b48e0bSAndroid Build Coastguard Worker## 6) AFL++ Context Sensitive Branch Coverage
175*08b48e0bSAndroid Build Coastguard Worker
176*08b48e0bSAndroid Build Coastguard Worker### What is this?
177*08b48e0bSAndroid Build Coastguard Worker
178*08b48e0bSAndroid Build Coastguard WorkerThis is an LLVM-based implementation of the context sensitive branch coverage.
179*08b48e0bSAndroid Build Coastguard Worker
180*08b48e0bSAndroid Build Coastguard WorkerBasically every function gets its own ID and, every time when an edge is logged,
181*08b48e0bSAndroid Build Coastguard Workerall the IDs in the callstack are hashed and combined with the edge transition
182*08b48e0bSAndroid Build Coastguard Workerhash to augment the classic edge coverage with the information about the calling
183*08b48e0bSAndroid Build Coastguard Workercontext.
184*08b48e0bSAndroid Build Coastguard Worker
185*08b48e0bSAndroid Build Coastguard WorkerSo if both function A and function B call a function C, the coverage collected
186*08b48e0bSAndroid Build Coastguard Workerin C will be different.
187*08b48e0bSAndroid Build Coastguard Worker
188*08b48e0bSAndroid Build Coastguard WorkerIn math the coverage is collected as follows: `map[current_location_ID ^
189*08b48e0bSAndroid Build Coastguard Workerprevious_location_ID >> 1 ^ hash_callstack_IDs] += 1`
190*08b48e0bSAndroid Build Coastguard Worker
191*08b48e0bSAndroid Build Coastguard WorkerThe callstack hash is produced XOR-ing the function IDs to avoid explosion with
192*08b48e0bSAndroid Build Coastguard Workerrecursive functions.
193*08b48e0bSAndroid Build Coastguard Worker
194*08b48e0bSAndroid Build Coastguard Worker### Usage
195*08b48e0bSAndroid Build Coastguard Worker
196*08b48e0bSAndroid Build Coastguard WorkerSet the `AFL_LLVM_INSTRUMENT=CTX` or `AFL_LLVM_CTX=1` environment variable.
197*08b48e0bSAndroid Build Coastguard Worker
198*08b48e0bSAndroid Build Coastguard WorkerIt is highly recommended to increase the MAP_SIZE_POW2 definition in config.h to
199*08b48e0bSAndroid Build Coastguard Workerat least 18 and maybe up to 20 for this as otherwise too many map collisions
200*08b48e0bSAndroid Build Coastguard Workeroccur.
201*08b48e0bSAndroid Build Coastguard Worker
202*08b48e0bSAndroid Build Coastguard Worker### Caller Branch Coverage
203*08b48e0bSAndroid Build Coastguard Worker
204*08b48e0bSAndroid Build Coastguard WorkerIf the context sensitive coverage introduces too may collisions and becoming
205*08b48e0bSAndroid Build Coastguard Workerdetrimental, the user can choose to augment edge coverage with just the called
206*08b48e0bSAndroid Build Coastguard Workerfunction ID, instead of the entire callstack hash.
207*08b48e0bSAndroid Build Coastguard Worker
208*08b48e0bSAndroid Build Coastguard WorkerIn math the coverage is collected as follows: `map[current_location_ID ^
209*08b48e0bSAndroid Build Coastguard Workerprevious_location_ID >> 1 ^ previous_callee_ID] += 1`
210*08b48e0bSAndroid Build Coastguard Worker
211*08b48e0bSAndroid Build Coastguard WorkerSet the `AFL_LLVM_INSTRUMENT=CALLER` or `AFL_LLVM_CALLER=1` environment
212*08b48e0bSAndroid Build Coastguard Workervariable.
213*08b48e0bSAndroid Build Coastguard Worker
214*08b48e0bSAndroid Build Coastguard Worker## 7) AFL++ N-Gram Branch Coverage
215*08b48e0bSAndroid Build Coastguard Worker
216*08b48e0bSAndroid Build Coastguard Worker### Source
217*08b48e0bSAndroid Build Coastguard Worker
218*08b48e0bSAndroid Build Coastguard WorkerThis is an LLVM-based implementation of the n-gram branch coverage proposed in
219*08b48e0bSAndroid Build Coastguard Workerthe paper
220*08b48e0bSAndroid Build Coastguard Worker["Be Sensitive and Collaborative: Analyzing Impact of Coverage Metrics in Greybox Fuzzing"](https://www.usenix.org/system/files/raid2019-wang-jinghan.pdf)
221*08b48e0bSAndroid Build Coastguard Workerby Jinghan Wang, et. al.
222*08b48e0bSAndroid Build Coastguard Worker
223*08b48e0bSAndroid Build Coastguard WorkerNote that the original implementation (available
224*08b48e0bSAndroid Build Coastguard Worker[here](https://github.com/bitsecurerlab/afl-sensitive)) is built on top of AFL's
225*08b48e0bSAndroid Build Coastguard WorkerQEMU mode. This is essentially a port that uses LLVM vectorized instructions
226*08b48e0bSAndroid Build Coastguard Worker(available from llvm versions 4.0.1 and higher) to achieve the same results when
227*08b48e0bSAndroid Build Coastguard Workercompiling source code.
228*08b48e0bSAndroid Build Coastguard Worker
229*08b48e0bSAndroid Build Coastguard WorkerIn math the branch coverage is performed as follows: `map[current_location ^
230*08b48e0bSAndroid Build Coastguard Workerprev_location[0] >> 1 ^ prev_location[1] >> 1 ^ ... up to n-1`] += 1`
231*08b48e0bSAndroid Build Coastguard Worker
232*08b48e0bSAndroid Build Coastguard Worker### Usage
233*08b48e0bSAndroid Build Coastguard Worker
234*08b48e0bSAndroid Build Coastguard WorkerThe size of `n` (i.e., the number of branches to remember) is an option that is
235*08b48e0bSAndroid Build Coastguard Workerspecified either in the `AFL_LLVM_INSTRUMENT=NGRAM-{value}` or the
236*08b48e0bSAndroid Build Coastguard Worker`AFL_LLVM_NGRAM_SIZE` environment variable. Good values are 2, 4, or 8, valid
237*08b48e0bSAndroid Build Coastguard Workerare 2-16.
238*08b48e0bSAndroid Build Coastguard Worker
239*08b48e0bSAndroid Build Coastguard WorkerIt is highly recommended to increase the MAP_SIZE_POW2 definition in config.h to
240*08b48e0bSAndroid Build Coastguard Workerat least 18 and maybe up to 20 for this as otherwise too many map collisions
241*08b48e0bSAndroid Build Coastguard Workeroccur.
242*08b48e0bSAndroid Build Coastguard Worker
243*08b48e0bSAndroid Build Coastguard Worker## 8) NeverZero counters
244*08b48e0bSAndroid Build Coastguard Worker
245*08b48e0bSAndroid Build Coastguard WorkerIn larger, complex, or reiterative programs, the byte sized counters that
246*08b48e0bSAndroid Build Coastguard Workercollect the edge coverage can easily fill up and wrap around. This is not that
247*08b48e0bSAndroid Build Coastguard Workermuch of an issue - unless, by chance, it wraps just to a value of zero when the
248*08b48e0bSAndroid Build Coastguard Workerprogram execution ends. In this case, afl-fuzz is not able to see that the edge
249*08b48e0bSAndroid Build Coastguard Workerhas been accessed and will ignore it.
250*08b48e0bSAndroid Build Coastguard Worker
251*08b48e0bSAndroid Build Coastguard WorkerNeverZero prevents this behavior. If a counter wraps, it jumps over the value 0
252*08b48e0bSAndroid Build Coastguard Workerdirectly to a 1. This improves path discovery (by a very small amount) at a very
253*08b48e0bSAndroid Build Coastguard Workerlow cost (one instruction per edge).
254*08b48e0bSAndroid Build Coastguard Worker
255*08b48e0bSAndroid Build Coastguard Worker(The alternative of saturated counters has been tested also and proved to be
256*08b48e0bSAndroid Build Coastguard Workerinferior in terms of path discovery.)
257*08b48e0bSAndroid Build Coastguard Worker
258*08b48e0bSAndroid Build Coastguard WorkerThis is implemented in afl-gcc and afl-gcc-fast, however, for llvm_mode this is
259*08b48e0bSAndroid Build Coastguard Workeroptional if multithread safe counters are selected or the llvm version is below
260*08b48e0bSAndroid Build Coastguard Worker9 - as there are severe performance costs in these cases.
261*08b48e0bSAndroid Build Coastguard Worker
262*08b48e0bSAndroid Build Coastguard WorkerIf you want to enable this for llvm versions below 9 or thread safe counters,
263*08b48e0bSAndroid Build Coastguard Workerthen set
264*08b48e0bSAndroid Build Coastguard Worker
265*08b48e0bSAndroid Build Coastguard Worker```
266*08b48e0bSAndroid Build Coastguard Workerexport AFL_LLVM_NOT_ZERO=1
267*08b48e0bSAndroid Build Coastguard Worker```
268*08b48e0bSAndroid Build Coastguard Worker
269*08b48e0bSAndroid Build Coastguard WorkerIn case you are on llvm 9 or greater and you do not want this behavior, then you
270*08b48e0bSAndroid Build Coastguard Workercan set:
271*08b48e0bSAndroid Build Coastguard Worker
272*08b48e0bSAndroid Build Coastguard Worker```
273*08b48e0bSAndroid Build Coastguard WorkerAFL_LLVM_SKIP_NEVERZERO=1
274*08b48e0bSAndroid Build Coastguard Worker```
275*08b48e0bSAndroid Build Coastguard Worker
276*08b48e0bSAndroid Build Coastguard WorkerIf the target does not have extensive loops or functions that are called a lot,
277*08b48e0bSAndroid Build Coastguard Workerthen this can give a small performance boost.
278*08b48e0bSAndroid Build Coastguard Worker
279*08b48e0bSAndroid Build Coastguard WorkerPlease note that the default counter implementations are not thread safe!
280*08b48e0bSAndroid Build Coastguard Worker
281*08b48e0bSAndroid Build Coastguard WorkerSupport for thread safe counters in mode LLVM CLASSIC can be activated with
282*08b48e0bSAndroid Build Coastguard Workersetting `AFL_LLVM_THREADSAFE_INST=1`.
283*08b48e0bSAndroid Build Coastguard Worker
284*08b48e0bSAndroid Build Coastguard Worker## 8) Source code coverage through instrumentation
285*08b48e0bSAndroid Build Coastguard Worker
286*08b48e0bSAndroid Build Coastguard WorkerMeasuring source code coverage is a common task in fuzzing, but it is very
287*08b48e0bSAndroid Build Coastguard Workerdifficut to do in some situations (e.g. when using snapshot fuzzing).
288*08b48e0bSAndroid Build Coastguard Worker
289*08b48e0bSAndroid Build Coastguard WorkerWhen using the `AFL_LLVM_INSTRUMENT=llvm-codecov` option, afl-cc will use
290*08b48e0bSAndroid Build Coastguard Workernative trace-pc-guard instrumentation but additionally select options that
291*08b48e0bSAndroid Build Coastguard Workerare required to utilize the instrumentation for source code coverage.
292*08b48e0bSAndroid Build Coastguard Worker
293*08b48e0bSAndroid Build Coastguard WorkerIn particular, it will switch the instrumentation to be per basic block
294*08b48e0bSAndroid Build Coastguard Workerinstead of instrumenting edges, disable all guard pruning and enable the
295*08b48e0bSAndroid Build Coastguard Workerexperimental pc-table support that allows the runtime to gather 100% of
296*08b48e0bSAndroid Build Coastguard Workerinstrumented basic blocks at start, including their locations.
297*08b48e0bSAndroid Build Coastguard Worker
298*08b48e0bSAndroid Build Coastguard WorkerNote: You must compile AFL with the `CODE_COVERAGE=1` option to enable the
299*08b48e0bSAndroid Build Coastguard Workerrespective parts in the AFL compiler runtime. Support is currently only
300*08b48e0bSAndroid Build Coastguard Workerimplemented for Nyx, but can in theory also work without Nyx.
301*08b48e0bSAndroid Build Coastguard Worker
302*08b48e0bSAndroid Build Coastguard WorkerNote: You might have to adjust `MAP_SIZE_POW2` in include/config.h to ensure
303*08b48e0bSAndroid Build Coastguard Workerthat your coverage map is large enough to hold all basic blocks of your
304*08b48e0bSAndroid Build Coastguard Workertarget program without any collisions.
305*08b48e0bSAndroid Build Coastguard Worker
306*08b48e0bSAndroid Build Coastguard WorkerMore documentation on how to utilize this with Nyx will follow.
307