xref: /aosp_15_r20/external/AFLplusplus/instrumentation/README.instrument_list.md (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1*08b48e0bSAndroid Build Coastguard Worker# Using AFL++ with partial instrumentation
2*08b48e0bSAndroid Build Coastguard Worker
3*08b48e0bSAndroid Build Coastguard WorkerThis file describes two different mechanisms to selectively instrument only
4*08b48e0bSAndroid Build Coastguard Workerspecific parts in the target.
5*08b48e0bSAndroid Build Coastguard Worker
6*08b48e0bSAndroid Build Coastguard WorkerBoth mechanisms work for LLVM and GCC_PLUGIN, but not for afl-clang/afl-gcc.
7*08b48e0bSAndroid Build Coastguard Worker
8*08b48e0bSAndroid Build Coastguard Worker## 1) Description and purpose
9*08b48e0bSAndroid Build Coastguard Worker
10*08b48e0bSAndroid Build Coastguard WorkerWhen building and testing complex programs where only a part of the program is
11*08b48e0bSAndroid Build Coastguard Workerthe fuzzing target, it often helps to only instrument the necessary parts of the
12*08b48e0bSAndroid Build Coastguard Workerprogram, leaving the rest uninstrumented. This helps to focus the fuzzer on the
13*08b48e0bSAndroid Build Coastguard Workerimportant parts of the program, avoiding undesired noise and disturbance by
14*08b48e0bSAndroid Build Coastguard Workeruninteresting code being exercised.
15*08b48e0bSAndroid Build Coastguard Worker
16*08b48e0bSAndroid Build Coastguard WorkerFor this purpose, "partial instrumentation" support is provided by AFL++ that
17*08b48e0bSAndroid Build Coastguard Workerallows to specify what should be instrumented and what not.
18*08b48e0bSAndroid Build Coastguard Worker
19*08b48e0bSAndroid Build Coastguard WorkerBoth mechanisms for partial instrumentation can be used together.
20*08b48e0bSAndroid Build Coastguard Worker
21*08b48e0bSAndroid Build Coastguard Worker## 2) Selective instrumentation with __AFL_COVERAGE_... directives
22*08b48e0bSAndroid Build Coastguard Worker
23*08b48e0bSAndroid Build Coastguard WorkerIn this mechanism, the selective instrumentation is done in the source code.
24*08b48e0bSAndroid Build Coastguard Worker
25*08b48e0bSAndroid Build Coastguard WorkerAfter the includes, a special define has to be made, e.g.:
26*08b48e0bSAndroid Build Coastguard Worker
27*08b48e0bSAndroid Build Coastguard Worker```
28*08b48e0bSAndroid Build Coastguard Worker#include <stdio.h>
29*08b48e0bSAndroid Build Coastguard Worker#include <stdint.h>
30*08b48e0bSAndroid Build Coastguard Worker// ...
31*08b48e0bSAndroid Build Coastguard Worker
32*08b48e0bSAndroid Build Coastguard Worker__AFL_COVERAGE();  // <- required for this feature to work
33*08b48e0bSAndroid Build Coastguard Worker```
34*08b48e0bSAndroid Build Coastguard Worker
35*08b48e0bSAndroid Build Coastguard WorkerIf you want to disable the coverage at startup until you specify coverage should
36*08b48e0bSAndroid Build Coastguard Workerbe started, then add `__AFL_COVERAGE_START_OFF();` at that position.
37*08b48e0bSAndroid Build Coastguard Worker
38*08b48e0bSAndroid Build Coastguard WorkerFrom here on out, you have the following macros available that you can use in
39*08b48e0bSAndroid Build Coastguard Workerany function where you want:
40*08b48e0bSAndroid Build Coastguard Worker
41*08b48e0bSAndroid Build Coastguard Worker* `__AFL_COVERAGE_ON();` - Enable coverage from this point onwards.
42*08b48e0bSAndroid Build Coastguard Worker* `__AFL_COVERAGE_OFF();` - Disable coverage from this point onwards.
43*08b48e0bSAndroid Build Coastguard Worker* `__AFL_COVERAGE_DISCARD();` - Reset all coverage gathered until this point.
44*08b48e0bSAndroid Build Coastguard Worker* `__AFL_COVERAGE_SKIP();` - Mark this test case as unimportant. Whatever
45*08b48e0bSAndroid Build Coastguard Worker  happens, afl-fuzz will ignore it.
46*08b48e0bSAndroid Build Coastguard Worker
47*08b48e0bSAndroid Build Coastguard WorkerA special function is `__afl_coverage_interesting`. To use this, you must define
48*08b48e0bSAndroid Build Coastguard Worker`void __afl_coverage_interesting(u8 val, u32 id);`. Then you can use this
49*08b48e0bSAndroid Build Coastguard Workerfunction globally, where the `val` parameter can be set by you, the `id`
50*08b48e0bSAndroid Build Coastguard Workerparameter is for afl-fuzz and will be overwritten. Note that useful parameters
51*08b48e0bSAndroid Build Coastguard Workerfor `val` are: 1, 2, 3, 4, 8, 16, 32, 64, 128. A value of, e.g., 33 will be seen
52*08b48e0bSAndroid Build Coastguard Workeras 32 for coverage purposes.
53*08b48e0bSAndroid Build Coastguard Worker
54*08b48e0bSAndroid Build Coastguard Worker## 3) Selective instrumentation with AFL_LLVM_ALLOWLIST/AFL_LLVM_DENYLIST
55*08b48e0bSAndroid Build Coastguard Worker
56*08b48e0bSAndroid Build Coastguard WorkerThis feature is equivalent to llvm 12 sancov feature and allows to specify on a
57*08b48e0bSAndroid Build Coastguard Workerfilename and/or function name level to instrument these or skip them.
58*08b48e0bSAndroid Build Coastguard Worker
59*08b48e0bSAndroid Build Coastguard Worker### 3a) How to use the partial instrumentation mode
60*08b48e0bSAndroid Build Coastguard Worker
61*08b48e0bSAndroid Build Coastguard WorkerIn order to build with partial instrumentation, you need to build with
62*08b48e0bSAndroid Build Coastguard Workerafl-clang-fast/afl-clang-fast++ or afl-clang-lto/afl-clang-lto++. The only
63*08b48e0bSAndroid Build Coastguard Workerrequired change is that you need to set either the environment variable
64*08b48e0bSAndroid Build Coastguard Worker`AFL_LLVM_ALLOWLIST` or `AFL_LLVM_DENYLIST` set with a filename.
65*08b48e0bSAndroid Build Coastguard Worker
66*08b48e0bSAndroid Build Coastguard WorkerThat file should contain the file names or functions that are to be instrumented
67*08b48e0bSAndroid Build Coastguard Worker(`AFL_LLVM_ALLOWLIST`) or are specifically NOT to be instrumented
68*08b48e0bSAndroid Build Coastguard Worker(`AFL_LLVM_DENYLIST`).
69*08b48e0bSAndroid Build Coastguard Worker
70*08b48e0bSAndroid Build Coastguard WorkerGCC_PLUGIN: you can use either `AFL_LLVM_ALLOWLIST` or `AFL_GCC_ALLOWLIST` (or
71*08b48e0bSAndroid Build Coastguard Workerthe same for `_DENYLIST`), both work.
72*08b48e0bSAndroid Build Coastguard Worker
73*08b48e0bSAndroid Build Coastguard WorkerFor matching to succeed, the function/file name that is being compiled must end
74*08b48e0bSAndroid Build Coastguard Workerin the function/file name entry contained in this instrument file list. That is
75*08b48e0bSAndroid Build Coastguard Workerto avoid breaking the match when absolute paths are used during compilation.
76*08b48e0bSAndroid Build Coastguard Worker
77*08b48e0bSAndroid Build Coastguard Worker**NOTE:** In builds with optimization enabled, functions might be inlined and
78*08b48e0bSAndroid Build Coastguard Workerwould not match!
79*08b48e0bSAndroid Build Coastguard Worker
80*08b48e0bSAndroid Build Coastguard WorkerFor example, if your source tree looks like this:
81*08b48e0bSAndroid Build Coastguard Worker
82*08b48e0bSAndroid Build Coastguard Worker```
83*08b48e0bSAndroid Build Coastguard Workerproject/
84*08b48e0bSAndroid Build Coastguard Workerproject/feature_a/a1.cpp
85*08b48e0bSAndroid Build Coastguard Workerproject/feature_a/a2.cpp
86*08b48e0bSAndroid Build Coastguard Workerproject/feature_b/b1.cpp
87*08b48e0bSAndroid Build Coastguard Workerproject/feature_b/b2.cpp
88*08b48e0bSAndroid Build Coastguard Worker```
89*08b48e0bSAndroid Build Coastguard Worker
90*08b48e0bSAndroid Build Coastguard WorkerAnd you only want to test feature_a, then create an "instrument file list" file
91*08b48e0bSAndroid Build Coastguard Workercontaining:
92*08b48e0bSAndroid Build Coastguard Worker
93*08b48e0bSAndroid Build Coastguard Worker```
94*08b48e0bSAndroid Build Coastguard Workerfeature_a/a1.cpp
95*08b48e0bSAndroid Build Coastguard Workerfeature_a/a2.cpp
96*08b48e0bSAndroid Build Coastguard Worker```
97*08b48e0bSAndroid Build Coastguard Worker
98*08b48e0bSAndroid Build Coastguard WorkerHowever, if the "instrument file list" file contains only this, it works as
99*08b48e0bSAndroid Build Coastguard Workerwell:
100*08b48e0bSAndroid Build Coastguard Worker
101*08b48e0bSAndroid Build Coastguard Worker```
102*08b48e0bSAndroid Build Coastguard Workera1.cpp
103*08b48e0bSAndroid Build Coastguard Workera2.cpp
104*08b48e0bSAndroid Build Coastguard Worker```
105*08b48e0bSAndroid Build Coastguard Worker
106*08b48e0bSAndroid Build Coastguard WorkerBut it might lead to files being unwantedly instrumented if the same filename
107*08b48e0bSAndroid Build Coastguard Workerexists somewhere else in the project directories.
108*08b48e0bSAndroid Build Coastguard Worker
109*08b48e0bSAndroid Build Coastguard WorkerYou can also specify function names. Note that for C++ the function names must
110*08b48e0bSAndroid Build Coastguard Workerbe mangled to match! `nm` can print these names.
111*08b48e0bSAndroid Build Coastguard Worker
112*08b48e0bSAndroid Build Coastguard WorkerAFL++ is able to identify whether an entry is a filename or a function. However,
113*08b48e0bSAndroid Build Coastguard Workerif you want to be sure (and compliant to the sancov allow/blocklist format), you
114*08b48e0bSAndroid Build Coastguard Workercan specify source file entries like this:
115*08b48e0bSAndroid Build Coastguard Worker
116*08b48e0bSAndroid Build Coastguard Worker```
117*08b48e0bSAndroid Build Coastguard Workersrc: *malloc.c
118*08b48e0bSAndroid Build Coastguard Worker```
119*08b48e0bSAndroid Build Coastguard Worker
120*08b48e0bSAndroid Build Coastguard WorkerAnd function entries like this:
121*08b48e0bSAndroid Build Coastguard Worker
122*08b48e0bSAndroid Build Coastguard Worker```
123*08b48e0bSAndroid Build Coastguard Workerfun: MallocFoo
124*08b48e0bSAndroid Build Coastguard Worker```
125*08b48e0bSAndroid Build Coastguard Worker
126*08b48e0bSAndroid Build Coastguard WorkerNote that whitespace is ignored and comments (`# foo`) are supported.
127*08b48e0bSAndroid Build Coastguard Worker
128*08b48e0bSAndroid Build Coastguard Worker### 3b) UNIX-style pattern matching
129*08b48e0bSAndroid Build Coastguard Worker
130*08b48e0bSAndroid Build Coastguard WorkerYou can add UNIX-style pattern matching in the "instrument file list" entries.
131*08b48e0bSAndroid Build Coastguard WorkerSee `man fnmatch` for the syntax. Do not set any of the `fnmatch` flags.