1*08b48e0bSAndroid Build Coastguard Worker# GCC-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 LLVM-based instrumentation, see [README.llvm.md](README.llvm.md). 6*08b48e0bSAndroid Build Coastguard Worker 7*08b48e0bSAndroid Build Coastguard WorkerThis document describes how to build and use `afl-gcc-fast` and `afl-g++-fast`, 8*08b48e0bSAndroid Build Coastguard Workerwhich instrument the target with the help of gcc plugins. 9*08b48e0bSAndroid Build Coastguard Worker 10*08b48e0bSAndroid Build Coastguard WorkerTL;DR: 11*08b48e0bSAndroid Build Coastguard Worker* Check the version of your gcc compiler: `gcc --version` 12*08b48e0bSAndroid Build Coastguard Worker* `apt-get install gcc-VERSION-plugin-dev` or similar to install headers for gcc 13*08b48e0bSAndroid Build Coastguard Worker plugins. 14*08b48e0bSAndroid Build Coastguard Worker* `gcc` and `g++` must match the gcc-VERSION you installed headers for. You can 15*08b48e0bSAndroid Build Coastguard Worker set `AFL_CC`/`AFL_CXX` to point to these! 16*08b48e0bSAndroid Build Coastguard Worker* `make` 17*08b48e0bSAndroid Build Coastguard Worker* Just use `afl-gcc-fast`/`afl-g++-fast` normally like you would do with 18*08b48e0bSAndroid Build Coastguard Worker `afl-clang-fast`. 19*08b48e0bSAndroid Build Coastguard Worker 20*08b48e0bSAndroid Build Coastguard Worker## 1) Introduction 21*08b48e0bSAndroid Build Coastguard Worker 22*08b48e0bSAndroid Build Coastguard WorkerThe code in this directory allows to instrument programs for AFL++ using true 23*08b48e0bSAndroid Build Coastguard Workercompiler-level instrumentation, instead of the more crude assembly-level 24*08b48e0bSAndroid Build Coastguard Workerrewriting approach taken by afl-gcc and afl-clang. This has several interesting 25*08b48e0bSAndroid Build Coastguard Workerproperties: 26*08b48e0bSAndroid Build Coastguard Worker 27*08b48e0bSAndroid Build Coastguard Worker- The compiler can make many optimizations that are hard to pull off when 28*08b48e0bSAndroid Build Coastguard Worker manually inserting assembly. As a result, some slow, CPU-bound programs will 29*08b48e0bSAndroid Build Coastguard Worker run up to around faster. 30*08b48e0bSAndroid Build Coastguard Worker 31*08b48e0bSAndroid Build Coastguard Worker The gains are less pronounced for fast binaries, where the speed is limited 32*08b48e0bSAndroid Build Coastguard Worker chiefly by the cost of creating new processes. In such cases, the gain will 33*08b48e0bSAndroid Build Coastguard Worker probably stay within 10%. 34*08b48e0bSAndroid Build Coastguard Worker 35*08b48e0bSAndroid Build Coastguard Worker- The instrumentation is CPU-independent. At least in principle, you should be 36*08b48e0bSAndroid Build Coastguard Worker able to rely on it to fuzz programs on non-x86 architectures (after building 37*08b48e0bSAndroid Build Coastguard Worker `afl-fuzz` with `AFL_NOX86=1`). 38*08b48e0bSAndroid Build Coastguard Worker 39*08b48e0bSAndroid Build Coastguard Worker- Because the feature relies on the internals of GCC, it is gcc-specific and 40*08b48e0bSAndroid Build Coastguard Worker will *not* work with LLVM (see [README.llvm.md](README.llvm.md) for an 41*08b48e0bSAndroid Build Coastguard Worker alternative). 42*08b48e0bSAndroid Build Coastguard Worker 43*08b48e0bSAndroid Build Coastguard WorkerOnce this implementation is shown to be sufficiently robust and portable, it 44*08b48e0bSAndroid Build Coastguard Workerwill probably replace afl-gcc. For now, it can be built separately and co-exists 45*08b48e0bSAndroid Build Coastguard Workerwith the original code. 46*08b48e0bSAndroid Build Coastguard Worker 47*08b48e0bSAndroid Build Coastguard WorkerThe idea and much of the implementation comes from Laszlo Szekeres. 48*08b48e0bSAndroid Build Coastguard Worker 49*08b48e0bSAndroid Build Coastguard Worker## 2) How to use 50*08b48e0bSAndroid Build Coastguard Worker 51*08b48e0bSAndroid Build Coastguard WorkerIn order to leverage this mechanism, you need to have modern enough GCC (>= 52*08b48e0bSAndroid Build Coastguard Workerversion 4.5.0) and the plugin development headers installed on your system. That 53*08b48e0bSAndroid Build Coastguard Workershould be all you need. On Debian machines, these headers can be acquired by 54*08b48e0bSAndroid Build Coastguard Workerinstalling the `gcc-VERSION-plugin-dev` packages. 55*08b48e0bSAndroid Build Coastguard Worker 56*08b48e0bSAndroid Build Coastguard WorkerTo build the instrumentation itself, type `make`. This will generate binaries 57*08b48e0bSAndroid Build Coastguard Workercalled `afl-gcc-fast` and `afl-g++-fast` in the parent directory. 58*08b48e0bSAndroid Build Coastguard Worker 59*08b48e0bSAndroid Build Coastguard WorkerThe gcc and g++ compiler links have to point to gcc-VERSION - or set these by 60*08b48e0bSAndroid Build Coastguard Workerpointing the environment variables `AFL_CC`/`AFL_CXX` to them. If the `CC`/`CXX` 61*08b48e0bSAndroid Build Coastguard Workerenvironment variables have been set, those compilers will be preferred over 62*08b48e0bSAndroid Build Coastguard Workerthose from the `AFL_CC`/`AFL_CXX` settings. 63*08b48e0bSAndroid Build Coastguard Worker 64*08b48e0bSAndroid Build Coastguard WorkerOnce this is done, you can instrument third-party code in a way similar to the 65*08b48e0bSAndroid Build Coastguard Workerstandard operating mode of AFL++, e.g.: 66*08b48e0bSAndroid Build Coastguard Worker 67*08b48e0bSAndroid Build Coastguard Worker``` 68*08b48e0bSAndroid Build Coastguard Worker CC=/path/to/afl/afl-gcc-fast 69*08b48e0bSAndroid Build Coastguard Worker CXX=/path/to/afl/afl-g++-fast 70*08b48e0bSAndroid Build Coastguard Worker export CC CXX 71*08b48e0bSAndroid Build Coastguard Worker ./configure [...options...] 72*08b48e0bSAndroid Build Coastguard Worker make 73*08b48e0bSAndroid Build Coastguard Worker``` 74*08b48e0bSAndroid Build Coastguard Worker 75*08b48e0bSAndroid Build Coastguard WorkerNote: We also used `CXX` to set the C++ compiler to `afl-g++-fast` for C++ code. 76*08b48e0bSAndroid Build Coastguard Worker 77*08b48e0bSAndroid Build Coastguard WorkerThe tool honors roughly the same environmental variables as `afl-gcc` (see 78*08b48e0bSAndroid Build Coastguard Worker[docs/env_variables.md](../docs/env_variables.md). This includes 79*08b48e0bSAndroid Build Coastguard Worker`AFL_INST_RATIO`, `AFL_USE_ASAN`, `AFL_HARDEN`, and `AFL_DONT_OPTIMIZE`. 80*08b48e0bSAndroid Build Coastguard Worker 81*08b48e0bSAndroid Build Coastguard WorkerNote: if you want the GCC plugin to be installed on your system for all users, 82*08b48e0bSAndroid Build Coastguard Workeryou need to build it before issuing 'make install' in the parent directory. 83*08b48e0bSAndroid Build Coastguard Worker 84*08b48e0bSAndroid Build Coastguard Worker## 3) Gotchas, feedback, bugs 85*08b48e0bSAndroid Build Coastguard Worker 86*08b48e0bSAndroid Build Coastguard WorkerThis is an early-stage mechanism, so field reports are welcome. You can send bug 87*08b48e0bSAndroid Build Coastguard Workerreports to [email protected]. 88*08b48e0bSAndroid Build Coastguard Worker 89*08b48e0bSAndroid Build Coastguard Worker## 4) Bonus feature #1: deferred initialization 90*08b48e0bSAndroid Build Coastguard Worker 91*08b48e0bSAndroid Build Coastguard WorkerSee 92*08b48e0bSAndroid Build Coastguard Worker[README.persistent_mode.md#3) Deferred initialization](README.persistent_mode.md#3-deferred-initialization). 93*08b48e0bSAndroid Build Coastguard Worker 94*08b48e0bSAndroid Build Coastguard Worker## 5) Bonus feature #2: persistent mode 95*08b48e0bSAndroid Build Coastguard Worker 96*08b48e0bSAndroid Build Coastguard WorkerSee 97*08b48e0bSAndroid Build Coastguard Worker[README.persistent_mode.md#4) Persistent mode](README.persistent_mode.md#4-persistent-mode). 98*08b48e0bSAndroid Build Coastguard Worker 99*08b48e0bSAndroid Build Coastguard Worker## 6) Bonus feature #3: selective instrumentation 100*08b48e0bSAndroid Build Coastguard Worker 101*08b48e0bSAndroid Build Coastguard WorkerIt can be more effective to fuzzing to only instrument parts of the code. For 102*08b48e0bSAndroid Build Coastguard Workerdetails, see [README.instrument_list.md](README.instrument_list.md). 103*08b48e0bSAndroid Build Coastguard Worker 104*08b48e0bSAndroid Build Coastguard Worker## 7) Bonus feature #4: CMPLOG 105*08b48e0bSAndroid Build Coastguard Worker 106*08b48e0bSAndroid Build Coastguard WorkerThe gcc_plugin also support CMPLOG/Redqueen, just set `AFL_GCC_CMPLOG` before 107*08b48e0bSAndroid Build Coastguard Workerinstrumenting the target. 108*08b48e0bSAndroid Build Coastguard WorkerRead more about this in the llvm document. 109*08b48e0bSAndroid Build Coastguard Worker 110