1# 2# american fuzzy lop++ - GCC plugin instrumentation 3# ----------------------------------------------- 4# 5# Written by Austin Seipp <[email protected]> and 6# Laszlo Szekeres <[email protected]> and 7# Michal Zalewski and 8# Heiko Eißfeldt <[email protected]> 9# 10# GCC integration design is based on the LLVM design, which comes 11# from Laszlo Szekeres. 12# 13# Copyright 2015 Google Inc. All rights reserved. 14# Copyright 2019-2024 AFLplusplus Project. All rights reserved. 15# 16# Licensed under the Apache License, Version 2.0 (the "License"); 17# you may not use this file except in compliance with the License. 18# You may obtain a copy of the License at: 19# 20# https://www.apache.org/licenses/LICENSE-2.0 21# 22#TEST_MMAP=1 23PREFIX ?= /usr/local 24HELPER_PATH ?= $(PREFIX)/lib/afl 25BIN_PATH ?= $(PREFIX)/bin 26DOC_PATH ?= $(PREFIX)/share/doc/afl 27MAN_PATH ?= $(PREFIX)/share/man/man8 28 29VERSION = $(shell grep '^$(HASH)define VERSION ' ./config.h | cut -d '"' -f2) 30 31CFLAGS ?= -O3 -g -funroll-loops 32# -D_FORTIFY_SOURCE=1 33CFLAGS_SAFE := -Wall -Iinclude -Wno-pointer-sign \ 34 -DAFL_PATH=\"$(HELPER_PATH)\" -DBIN_PATH=\"$(BIN_PATH)\" \ 35 -DGCC_VERSION=\"$(GCCVER)\" -DGCC_BINDIR=\"$(GCCBINDIR)\" \ 36 -Wno-unused-function 37override CFLAGS += $(CFLAGS_SAFE) 38 39CXXFLAGS ?= -O3 -g -funroll-loops 40# -D_FORTIFY_SOURCE=1 41CXXEFLAGS := $(CXXFLAGS) $(CPPFLAGS) -Wall -std=c++11 42 43CC ?= gcc 44CXX ?= g++ 45 46SYS = $(shell uname -s) 47 48ifeq "clang" "$(CC)" 49 CC = gcc 50 CXX = g++ 51endif 52 53ifeq "clang++" "$(CXX)" 54 CC = gcc 55 CXX = g++ 56endif 57 58ifeq "$(findstring Foundation,$(shell $(CC) --version))" "" 59 CC = gcc 60 CXX = g++ 61endif 62 63PLUGIN_BASE = "$(shell $(CC) -print-file-name=plugin)" 64PLUGIN_FLAGS = -fPIC -fno-rtti -fno-exceptions -I$(PLUGIN_BASE)/include -I$(PLUGIN_BASE) 65HASH=\# 66 67GCCVER = $(shell $(CC) --version 2>/dev/null | awk 'NR == 1 {print $$NF}') 68GCCBINDIR = $(shell dirname `command -v $(CC)` 2>/dev/null ) 69 70ifeq "$(shell echo '$(HASH)include <sys/ipc.h>@$(HASH)include <sys/shm.h>@int main() { int _id = shmget(IPC_PRIVATE, 65536, IPC_CREAT | IPC_EXCL | 0600); shmctl(_id, IPC_RMID, 0); return 0;}' | tr @ '\n' | $(CC) -x c - -o .test2 2>/dev/null && echo 1 || echo 0 ; rm -f .test2 )" "1" 71 SHMAT_OK=1 72else 73 SHMAT_OK=0 74 override CFLAGS_SAFE += -DUSEMMAP=1 75endif 76 77ifeq "$(TEST_MMAP)" "1" 78 SHMAT_OK=0 79 override CFLAGS_SAFE += -DUSEMMAP=1 80endif 81 82ifneq "$(SYS)" "Haiku" 83ifneq "$(SYS)" "OpenBSD" 84 LDFLAGS += -lrt 85endif 86else 87 CFLAGS_SAFE += -DUSEMMAP=1 88endif 89 90ifeq "$(SYS)" "OpenBSD" 91 CC = egcc 92 CXX = eg++ 93 PLUGIN_FLAGS += -I/usr/local/include 94endif 95 96ifeq "$(SYS)" "DragonFly" 97 PLUGIN_FLAGS += -I/usr/local/include 98endif 99 100ifeq "$(SYS)" "SunOS" 101 PLUGIN_FLAGS += -I/usr/include/gmp 102endif 103 104 105PASSES = ./afl-gcc-pass.so ./afl-gcc-cmplog-pass.so ./afl-gcc-cmptrs-pass.so 106 107PROGS = $(PASSES) ./afl-compiler-rt.o ./afl-compiler-rt-32.o ./afl-compiler-rt-64.o 108 109.PHONY: all 110all: test_shm test_deps $(PROGS) test_build all_done 111 112.PHONY: test_shm 113ifeq "$(SHMAT_OK)" "1" 114test_shm: 115 @echo "[+] shmat seems to be working." 116 @rm -f .test2 117else 118test_shm: 119 @echo "[-] shmat seems not to be working, switching to mmap implementation" 120endif 121 122.PHONY: test_deps 123test_deps: 124 @echo "[*] Checking for working '$(CC)'..." 125 @command -v $(CC) >/dev/null 2>&1 || ( echo "[-] Oops, can't find '$(CC)'. Make sure that it's in your \$$PATH (or set \$$CC and \$$CXX)."; exit 1 ) 126# @echo "[*] Checking for gcc for plugin support..." 127# @$(CC) -v 2>&1 | grep -q -- --enable-plugin || ( echo "[-] Oops, this gcc has not been configured with plugin support."; exit 1 ) 128 @echo "[*] Checking for gcc plugin development header files..." 129 @test -d `$(CC) -print-file-name=plugin`/include || ( echo "[-] Oops, can't find gcc header files. Be sure to install 'gcc-X-plugin-dev'."; exit 1 ) 130 @echo "[*] Checking for './afl-showmap'..." 131 @test -f ./afl-showmap || ( echo "[-] Oops, can't find './afl-showmap'. Be sure to compile AFL first."; exit 1 ) 132 @echo "[+] All set and ready to build." 133 134afl-common.o: ./src/afl-common.c 135 $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ $(LDFLAGS) 136 137./afl-compiler-rt.o: instrumentation/afl-compiler-rt.o.c 138 $(CC) $(CFLAGS_SAFE) $(CPPFLAGS) -O3 -Wno-unused-result -fPIC -c $< -o $@ 139 140./afl-compiler-rt-32.o: instrumentation/afl-compiler-rt.o.c 141 @printf "[*] Building 32-bit variant of the runtime (-m32)... " 142 @$(CC) $(CFLAGS_SAFE) $(CPPFLAGS) -O3 -Wno-unused-result -m32 -fPIC -c $< -o $@ 2>/dev/null; if [ "$$?" = "0" ]; then echo "success!"; else echo "failed (that's fine)"; fi 143 144./afl-compiler-rt-64.o: instrumentation/afl-compiler-rt.o.c 145 @printf "[*] Building 64-bit variant of the runtime (-m64)... " 146 @$(CC) $(CFLAGS_SAFE) $(CPPFLAGS) -O3 -Wno-unused-result -m64 -fPIC -c $< -o $@ 2>/dev/null; if [ "$$?" = "0" ]; then echo "success!"; else echo "failed (that's fine)"; fi 147 148$(PASSES): instrumentation/afl-gcc-common.h 149 150./afl-gcc-pass.so: instrumentation/afl-gcc-pass.so.cc | test_deps 151 $(CXX) $(CXXEFLAGS) $(PLUGIN_FLAGS) -shared $< -o $@ 152 ln -sf afl-cc afl-gcc-fast 153 ln -sf afl-cc afl-g++-fast 154 ln -sf afl-cc.8 afl-gcc-fast.8 155 ln -sf afl-cc.8 afl-g++-fast.8 156 157./afl-gcc-cmplog-pass.so: instrumentation/afl-gcc-cmplog-pass.so.cc | test_deps 158 $(CXX) $(CXXEFLAGS) $(PLUGIN_FLAGS) -shared $< -o $@ 159 160./afl-gcc-cmptrs-pass.so: instrumentation/afl-gcc-cmptrs-pass.so.cc | test_deps 161 $(CXX) $(CXXEFLAGS) $(PLUGIN_FLAGS) -shared $< -o $@ 162 163.PHONY: test_build 164test_build: $(PROGS) 165 @echo "[*] Testing the CC wrapper and instrumentation output..." 166 unset AFL_USE_ASAN AFL_USE_MSAN; ASAN_OPTIONS=detect_leaks=0 AFL_QUIET=1 AFL_INST_RATIO=100 AFL_PATH=. AFL_CC=$(CC) ./afl-gcc-fast $(CFLAGS) $(CPPFLAGS) ./test-instr.c -o test-instr $(LDFLAGS) 167 ASAN_OPTIONS=detect_leaks=0 ./afl-showmap -m none -q -o .test-instr0 ./test-instr </dev/null 168 echo 1 | ASAN_OPTIONS=detect_leaks=0 ./afl-showmap -m none -q -o .test-instr1 ./test-instr 169 @rm -f test-instr 170 @cmp -s .test-instr0 .test-instr1; DR="$$?"; rm -f .test-instr0 .test-instr1; if [ "$$DR" = "0" ]; then echo; echo "Oops, the instrumentation does not seem to be behaving correctly!"; echo; echo "Please post to https://github.com/AFLplusplus/AFLplusplus/issues to troubleshoot the issue."; echo; exit 1; fi 171 @echo "[+] All right, the instrumentation seems to be working!" 172 173.PHONY: all_done 174all_done: test_build 175 @echo "[+] All done! You can now use './afl-gcc-fast' to compile programs." 176 177.NOTPARALLEL: clean 178 179%.8: % 180 @echo .TH $* 8 `date "+%Y-%m-%d"` "AFL++" > ./$@ 181 @echo .SH NAME >> ./$@ 182 @echo .B $* >> ./$@ 183 @echo >> ./$@ 184 @echo .SH SYNOPSIS >> ./$@ 185 @./$* -h 2>&1 | head -n 3 | tail -n 1 | sed 's/^\.\///' >> ./$@ 186 @echo >> ./$@ 187 @echo .SH OPTIONS >> ./$@ 188 @echo .nf >> ./$@ 189 @./$* -h 2>&1 | tail -n +4 >> ./$@ 190 @echo >> ./$@ 191 @echo .SH AUTHOR >> ./$@ 192 @echo "AFL++ was written by Michal \"lcamtuf\" Zalewski and is maintained by Marc \"van Hauser\" Heuse <[email protected]>, Dominik Maier <[email protected]>, Andrea Fioraldi <[email protected]> and Heiko \"hexcoder-\" Eissfeldt <[email protected]>" >> ./$@ 193 @echo The homepage of AFL++ is: https://github.com/AFLplusplus/AFLplusplus >> ./$@ 194 @echo >> ./$@ 195 @echo .SH LICENSE >> ./$@ 196 @echo Apache License Version 2.0, January 2004 >> ./$@ 197 ln -sf afl-cc.8 ./afl-g++-fast.8 198 199.PHONY: install 200install: all 201 ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-gcc-fast 202 ln -sf afl-c++ $${DESTDIR}$(BIN_PATH)/afl-g++-fast 203 ln -sf afl-compiler-rt.o $${DESTDIR}$(HELPER_PATH)/afl-gcc-rt.o 204 install -m 755 ./afl-gcc-pass.so $${DESTDIR}$(HELPER_PATH) 205 install -m 755 ./afl-gcc-cmplog-pass.so $${DESTDIR}$(HELPER_PATH) 206 install -m 755 ./afl-gcc-cmptrs-pass.so $${DESTDIR}$(HELPER_PATH) 207 install -m 644 -T instrumentation/README.gcc_plugin.md $${DESTDIR}$(DOC_PATH)/README.gcc_plugin.md 208 209.PHONY: clean 210clean: 211 rm -f *.o *.so *~ a.out core core.[1-9][0-9]* test-instr .test-instr0 .test-instr1 .test2 212 rm -f $(PROGS) afl-common.o ./afl-g++-fast ./afl-g*-fast.8 instrumentation/*.o 213