• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# american fuzzy lop++ - GCC plugin instrumentation
3# -----------------------------------------------
4#
5# Written by Austin Seipp <aseipp@pobox.com> and
6#            Laszlo Szekeres <lszekeres@google.com> and
7#            Michal Zalewski and
8#            Heiko Eißfeldt  <heiko@hexco.de>
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-2022 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 -D_FORTIFY_SOURCE=2
32CFLAGS_SAFE     := -Wall -Iinclude -Wno-pointer-sign \
33                   -DAFL_PATH=\"$(HELPER_PATH)\" -DBIN_PATH=\"$(BIN_PATH)\" \
34                   -DGCC_VERSION=\"$(GCCVER)\" -DGCC_BINDIR=\"$(GCCBINDIR)\" \
35                   -Wno-unused-function
36override CFLAGS += $(CFLAGS_SAFE)
37
38CXXFLAGS    ?= -O3 -g -funroll-loops -D_FORTIFY_SOURCE=2
39CXXEFLAGS   := $(CXXFLAGS) -Wall -std=c++11
40
41CC          ?= gcc
42CXX         ?= g++
43
44SYS = $(shell uname -s)
45
46ifeq "clang" "$(CC)"
47        CC  = gcc
48        CXX = g++
49endif
50
51ifeq "clang++" "$(CXX)"
52        CC  = gcc
53        CXX = g++
54endif
55
56ifeq "$(findstring Foundation,$(shell $(CC) --version))" ""
57        CC  = gcc
58        CXX = g++
59endif
60
61PLUGIN_BASE = "$(shell $(CC) -print-file-name=plugin)"
62PLUGIN_FLAGS = -fPIC -fno-rtti -I$(PLUGIN_BASE)/include -I$(PLUGIN_BASE)
63HASH=\#
64
65GCCVER    = $(shell $(CC) --version 2>/dev/null | awk 'NR == 1 {print $$NF}')
66GCCBINDIR = $(shell dirname `command -v $(CC)` 2>/dev/null )
67
68ifeq "$(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"
69	SHMAT_OK=1
70else
71	SHMAT_OK=0
72	override CFLAGS_SAFE += -DUSEMMAP=1
73endif
74
75ifeq "$(TEST_MMAP)" "1"
76	SHMAT_OK=0
77	override CFLAGS_SAFE += -DUSEMMAP=1
78endif
79
80ifneq "$(SYS)" "Haiku"
81ifneq "$(SYS)" "OpenBSD"
82  	LDFLAGS += -lrt
83endif
84else
85	CFLAGS_SAFE += -DUSEMMAP=1
86endif
87
88ifeq "$(SYS)" "OpenBSD"
89    CC  = egcc
90    CXX = eg++
91    PLUGIN_FLAGS += -I/usr/local/include
92endif
93
94ifeq "$(SYS)" "DragonFly"
95  	PLUGIN_FLAGS += -I/usr/local/include
96endif
97
98ifeq "$(SYS)" "SunOS"
99  	PLUGIN_FLAGS += -I/usr/include/gmp
100endif
101
102
103PROGS        = ./afl-gcc-pass.so ./afl-compiler-rt.o ./afl-compiler-rt-32.o ./afl-compiler-rt-64.o
104
105.PHONY: all
106all: test_shm test_deps $(PROGS) test_build all_done
107
108.PHONY: test_shm
109ifeq "$(SHMAT_OK)" "1"
110test_shm:
111	@echo "[+] shmat seems to be working."
112	@rm -f .test2
113else
114test_shm:
115	@echo "[-] shmat seems not to be working, switching to mmap implementation"
116endif
117
118.PHONY: test_deps
119test_deps:
120	@echo "[*] Checking for working '$(CC)'..."
121	@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 )
122#	@echo "[*] Checking for gcc for plugin support..."
123#	@$(CC) -v 2>&1 | grep -q -- --enable-plugin || ( echo "[-] Oops, this gcc has not been configured with plugin support."; exit 1 )
124	@echo "[*] Checking for gcc plugin development header files..."
125	@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 )
126	@echo "[*] Checking for './afl-showmap'..."
127	@test -f ./afl-showmap || ( echo "[-] Oops, can't find './afl-showmap'. Be sure to compile AFL first."; exit 1 )
128	@echo "[+] All set and ready to build."
129
130afl-common.o: ./src/afl-common.c
131	$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ $(LDFLAGS)
132
133./afl-compiler-rt.o: instrumentation/afl-compiler-rt.o.c
134	$(CC) $(CFLAGS_SAFE) $(CPPFLAGS) -O3 -Wno-unused-result -fPIC -c $< -o $@
135
136./afl-compiler-rt-32.o: instrumentation/afl-compiler-rt.o.c
137	@printf "[*] Building 32-bit variant of the runtime (-m32)... "
138	@$(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
139
140./afl-compiler-rt-64.o: instrumentation/afl-compiler-rt.o.c
141	@printf "[*] Building 64-bit variant of the runtime (-m64)... "
142	@$(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
143
144./afl-gcc-pass.so: instrumentation/afl-gcc-pass.so.cc | test_deps
145	$(CXX) $(CXXEFLAGS) $(PLUGIN_FLAGS) -shared $< -o $@
146	ln -sf afl-cc afl-gcc-fast
147	ln -sf afl-cc afl-g++-fast
148	ln -sf afl-cc.8 afl-gcc-fast.8
149	ln -sf afl-cc.8 afl-g++-fast.8
150
151.PHONY: test_build
152test_build: $(PROGS)
153	@echo "[*] Testing the CC wrapper and instrumentation output..."
154	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)
155	ASAN_OPTIONS=detect_leaks=0 ./afl-showmap -m none -q -o .test-instr0 ./test-instr </dev/null
156	echo 1 | ASAN_OPTIONS=detect_leaks=0 ./afl-showmap -m none -q -o .test-instr1 ./test-instr
157	@rm -f test-instr
158	@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
159	@echo "[+] All right, the instrumentation seems to be working!"
160
161.PHONY: all_done
162all_done: test_build
163	@echo "[+] All done! You can now use './afl-gcc-fast' to compile programs."
164
165.NOTPARALLEL: clean
166
167%.8: %
168	@echo .TH $* 8 `date "+%Y-%m-%d"` "afl++" > ./$@
169	@echo .SH NAME >> ./$@
170	@echo .B $* >> ./$@
171	@echo >> ./$@
172	@echo .SH SYNOPSIS >> ./$@
173	@./$* -h 2>&1 | head -n 3 | tail -n 1 | sed 's/^\.\///' >> ./$@
174	@echo >> ./$@
175	@echo .SH OPTIONS >> ./$@
176	@echo .nf >> ./$@
177	@./$* -h 2>&1 | tail -n +4 >> ./$@
178	@echo >> ./$@
179	@echo .SH AUTHOR >> ./$@
180	@echo "afl++ was written by Michal \"lcamtuf\" Zalewski and is maintained by Marc \"van Hauser\" Heuse <mh@mh-sec.de>, Heiko \"hexcoder-\" Eissfeldt <heiko.eissfeldt@hexco.de>, Andrea Fioraldi <andreafioraldi@gmail.com> and Dominik Maier <domenukk@gmail.com>" >> ./$@
181	@echo  The homepage of afl++ is: https://github.com/AFLplusplus/AFLplusplus >> ./$@
182	@echo >> ./$@
183	@echo .SH LICENSE >> ./$@
184	@echo Apache License Version 2.0, January 2004 >> ./$@
185	ln -sf afl-cc.8 ./afl-g++-fast.8
186
187.PHONY: install
188install: all
189	ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-gcc-fast
190	ln -sf afl-c++ $${DESTDIR}$(BIN_PATH)/afl-g++-fast
191	ln -sf afl-compiler-rt.o $${DESTDIR}$(HELPER_PATH)/afl-gcc-rt.o
192	install -m 755 ./afl-gcc-pass.so $${DESTDIR}$(HELPER_PATH)
193	install -m 644 -T instrumentation/README.gcc_plugin.md $${DESTDIR}$(DOC_PATH)/README.gcc_plugin.md
194
195.PHONY: clean
196clean:
197	rm -f *.o *.so *~ a.out core core.[1-9][0-9]* test-instr .test-instr0 .test-instr1 .test2
198	rm -f $(PROGS) afl-common.o ./afl-g++-fast ./afl-g*-fast.8 instrumentation/*.o
199