• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# american fuzzy lop++ - LLVM instrumentation
2# -----------------------------------------
3#
4# Written by Laszlo Szekeres <lszekeres@google.com> and
5#            Michal Zalewski
6#
7# LLVM integration design comes from Laszlo Szekeres.
8#
9# Copyright 2015, 2016 Google Inc. All rights reserved.
10#
11# Licensed under the Apache License, Version 2.0 (the "License");
12# you may not use this file except in compliance with the License.
13# You may obtain a copy of the License at:
14#
15#   https://www.apache.org/licenses/LICENSE-2.0
16#
17
18# For Heiko:
19#TEST_MMAP=1
20HASH=\#
21
22PREFIX      ?= /usr/local
23HELPER_PATH ?= $(PREFIX)/lib/afl
24BIN_PATH    ?= $(PREFIX)/bin
25DOC_PATH    ?= $(PREFIX)/share/doc/afl
26MISC_PATH   ?= $(PREFIX)/share/afl
27MAN_PATH    ?= $(PREFIX)/share/man/man8
28
29BUILD_DATE  ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "+%Y-%m-%d" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "+%Y-%m-%d" 2>/dev/null || date -u "+%Y-%m-%d")
30
31VERSION     = $(shell grep '^$(HASH)define VERSION ' ./config.h | cut -d '"' -f2)
32
33SYS = $(shell uname -s)
34
35ifeq "$(SYS)" "OpenBSD"
36  LLVM_CONFIG ?= $(BIN_PATH)/llvm-config
37  HAS_OPT = $(shell test -x $(BIN_PATH)/opt && echo 0 || echo 1)
38  ifeq "$(HAS_OPT)" "1"
39    $(warning llvm_mode needs a complete llvm installation (versions 6.0 up to 13) -> e.g. "pkg_add llvm-7.0.1p9")
40  endif
41else
42  LLVM_CONFIG ?= llvm-config
43endif
44
45LLVMVER  = $(shell $(LLVM_CONFIG) --version 2>/dev/null | sed 's/git//' | sed 's/svn//' )
46LLVM_MAJOR = $(shell $(LLVM_CONFIG) --version 2>/dev/null | sed 's/\..*//' )
47LLVM_MINOR = $(shell $(LLVM_CONFIG) --version 2>/dev/null | sed 's/.*\.//' | sed 's/git//' | sed 's/svn//' | sed 's/ .*//' )
48LLVM_UNSUPPORTED = $(shell $(LLVM_CONFIG) --version 2>/dev/null | egrep -q '^[0-2]\.|^3.[0-7]\.' && echo 1 || echo 0 )
49LLVM_TOO_NEW = $(shell $(LLVM_CONFIG) --version 2>/dev/null | egrep -q '^1[5-9]' && echo 1 || echo 0 )
50LLVM_NEW_API = $(shell $(LLVM_CONFIG) --version 2>/dev/null | egrep -q '^1[0-9]' && echo 1 || echo 0 )
51LLVM_10_OK = $(shell $(LLVM_CONFIG) --version 2>/dev/null | egrep -q '^1[1-9]|^10\.[1-9]|^10\.0.[1-9]' && echo 1 || echo 0 )
52LLVM_HAVE_LTO = $(shell $(LLVM_CONFIG) --version 2>/dev/null | egrep -q '^1[1-9]' && echo 1 || echo 0 )
53LLVM_BINDIR = $(shell $(LLVM_CONFIG) --bindir 2>/dev/null)
54LLVM_LIBDIR = $(shell $(LLVM_CONFIG) --libdir 2>/dev/null)
55LLVM_STDCXX = gnu++11
56LLVM_APPLE_XCODE = $(shell $(CC) -v 2>&1 | grep -q Apple && echo 1 || echo 0)
57LLVM_LTO   = 0
58
59ifeq "$(LLVMVER)" ""
60  $(warning [!] llvm_mode needs llvm-config, which was not found. Set LLVM_CONFIG to its path and retry.)
61endif
62
63ifeq "$(LLVM_UNSUPPORTED)" "1"
64  $(error llvm_mode only supports llvm from version 3.8 onwards)
65endif
66
67ifeq "$(LLVM_TOO_NEW)" "1"
68  $(warning you are using an in-development llvm version - this might break llvm_mode!)
69endif
70
71LLVM_TOO_OLD=1
72
73ifeq "$(LLVM_MAJOR)" "9"
74  $(info [+] llvm_mode detected llvm 9, enabling neverZero implementation)
75  LLVM_TOO_OLD=0
76endif
77
78ifeq "$(LLVM_NEW_API)" "1"
79  $(info [+] llvm_mode detected llvm 10+, enabling neverZero implementation and c++14)
80  LLVM_STDCXX = c++14
81  LLVM_TOO_OLD=0
82endif
83
84ifeq "$(LLVM_TOO_OLD)" "1"
85  $(info [!] llvm_mode detected an old version of llvm, upgrade to at least 9 or preferable 11!)
86  $(shell sleep 1)
87endif
88
89ifeq "$(LLVM_MAJOR)" "15"
90  $(info [!] llvm_mode detected llvm 15, which is currently broken for LTO plugins.)
91  LLVM_LTO = 0
92  LLVM_HAVE_LTO = 0
93endif
94
95ifeq "$(LLVM_HAVE_LTO)" "1"
96  $(info [+] llvm_mode detected llvm 11+, enabling afl-lto LTO implementation)
97  LLVM_LTO = 1
98  #TEST_MMAP = 1
99endif
100
101ifeq "$(LLVM_LTO)" "0"
102  $(info [+] llvm_mode detected llvm < 11 or llvm 15, afl-lto LTO will not be build.)
103endif
104
105ifeq "$(LLVM_APPLE_XCODE)" "1"
106  $(warning llvm_mode will not compile with Xcode clang...)
107endif
108
109# We were using llvm-config --bindir to get the location of clang, but
110# this seems to be busted on some distros, so using the one in $PATH is
111# probably better.
112
113CC         = $(LLVM_BINDIR)/clang
114CXX        = $(LLVM_BINDIR)/clang++
115
116# llvm-config --bindir may not providing a valid path, so ...
117ifeq "$(shell test -e $(CC) || echo 1 )" "1"
118  # however we must ensure that this is not a "CC=gcc make"
119  ifeq "$(shell command -v $(CC) 2> /dev/null)" ""
120    # we do not have a valid CC variable so we try alternatives
121    ifeq "$(shell test -e '$(BIN_DIR)/clang' && echo 1)" "1"
122      # we found one in the local install directory, lets use these
123      CC         = $(BIN_DIR)/clang
124    else
125      # hope for the best
126      $(warning we have trouble finding clang - llvm-config is not helping us)
127      CC         = clang
128    endif
129  endif
130endif
131# llvm-config --bindir may not providing a valid path, so ...
132ifeq "$(shell test -e $(CXX) || echo 1 )" "1"
133  # however we must ensure that this is not a "CXX=g++ make"
134  ifeq "$(shell command -v $(CXX) 2> /dev/null)" ""
135    # we do not have a valid CXX variable so we try alternatives
136    ifeq "$(shell test -e '$(BIN_DIR)/clang++' && echo 1)" "1"
137      # we found one in the local install directory, lets use these
138      CXX        = $(BIN_DIR)/clang++
139    else
140      # hope for the best
141      $(warning we have trouble finding clang++ - llvm-config is not helping us)
142      CXX        = clang++
143    endif
144  endif
145endif
146
147# sanity check.
148# Are versions of clang --version and llvm-config --version equal?
149CLANGVER = $(shell $(CC) --version | sed -E -ne '/^.*version\ (1?[0-9]\.[0-9]\.[0-9]).*/s//\1/p')
150
151# I disable this because it does not make sense with what we did before (marc)
152# We did exactly set these 26 lines above with these values, and it would break
153# "CC=gcc make" etc. usages
154ifeq "$(findstring clang, $(shell $(CC) --version 2>/dev/null))" ""
155  CC_SAVE := $(LLVM_BINDIR)/clang
156else
157  CC_SAVE := $(CC)
158endif
159ifeq "$(findstring clang, $(shell $(CXX) --version 2>/dev/null))" ""
160  CXX_SAVE := $(LLVM_BINDIR)/clang++
161else
162  CXX_SAVE := $(CXX)
163endif
164
165CLANG_BIN := $(CC_SAVE)
166CLANGPP_BIN := $(CXX_SAVE)
167
168ifeq "$(CC_SAVE)" "$(LLVM_BINDIR)/clang"
169  USE_BINDIR = 1
170else
171  ifeq "$(CXX_SAVE)" "$(LLVM_BINDIR)/clang++"
172    USE_BINDIR = 1
173  else
174    USE_BINDIR = 0
175  endif
176endif
177
178# On old platform we cannot compile with clang because std++ libraries are too
179# old. For these we need to use gcc/g++, so if we find REAL_CC and REAL_CXX
180# variable we override the compiler variables here
181ifneq "$(REAL_CC)" ""
182  CC  = $(REAL_CC)
183endif
184ifneq "$(REAL_CXX)" ""
185  CXX = $(REAL_CXX)
186endif
187
188#
189# Now it can happen that CC points to clang - but there is no clang on the
190# system. Then we fall back to cc
191#
192ifeq "$(shell command -v $(CC) 2>/dev/null)" ""
193  CC = cc
194endif
195ifeq "$(shell command -v $(CXX) 2>/dev/null)" ""
196  CXX = c++
197endif
198
199
200# After we set CC/CXX we can start makefile magic tests
201
202#ifeq "$(shell echo 'int main() {return 0; }' | $(CC) -x c - -march=native -o .test 2>/dev/null && echo 1 || echo 0 ; rm -f .test )" "1"
203#	CFLAGS_OPT = -march=native
204#endif
205
206ifeq "$(shell echo 'int main() {return 0; }' | $(CLANG_BIN) -x c - -flto=full -o .test 2>/dev/null && echo 1 || echo 0 ; rm -f .test )" "1"
207        AFL_CLANG_FLTO ?= -flto=full
208else
209 ifeq "$(shell echo 'int main() {return 0; }' | $(CLANG_BIN) -x c - -flto=thin -o .test 2>/dev/null && echo 1 || echo 0 ; rm -f .test )" "1"
210        AFL_CLANG_FLTO ?= -flto=thin
211 else
212  ifeq "$(shell echo 'int main() {return 0; }' | $(CLANG_BIN) -x c - -flto -o .test 2>/dev/null && echo 1 || echo 0 ; rm -f .test )" "1"
213        AFL_CLANG_FLTO ?= -flto
214  endif
215 endif
216endif
217
218ifeq "$(LLVM_LTO)" "1"
219  ifneq "$(AFL_CLANG_FLTO)" ""
220    ifeq "$(AFL_REAL_LD)" ""
221      ifneq "$(shell readlink $(LLVM_BINDIR)/ld.lld 2>&1)" ""
222        AFL_REAL_LD = $(LLVM_BINDIR)/ld.lld
223      else
224        $(warning ld.lld not found, cannot enable LTO mode)
225        LLVM_LTO = 0
226      endif
227    endif
228  else
229    $(warning clang option -flto is not working - maybe LLVMgold.so not found - cannot enable LTO mode)
230    LLVM_LTO = 0
231  endif
232endif
233
234AFL_CLANG_FUSELD=
235ifeq "$(LLVM_LTO)" "1"
236  ifeq "$(shell echo 'int main() {return 0; }' | $(CLANG_BIN) -x c - -fuse-ld=`command -v ld` -o .test 2>/dev/null && echo 1 || echo 0 ; rm -f .test )" "1"
237    AFL_CLANG_FUSELD=1
238    ifeq "$(shell echo 'int main() {return 0; }' | $(CLANG_BIN) -x c - -fuse-ld=ld.lld --ld-path=$(LLVM_BINDIR)/ld.lld -o .test 2>/dev/null && echo 1 || echo 0 ; rm -f .test )" "1"
239      AFL_CLANG_LDPATH=1
240    endif
241  else
242    $(warning -fuse-ld is not working, cannot enable LTO mode)
243    LLVM_LTO = 0
244  endif
245endif
246
247ifeq "$(shell echo 'int main() {return 0; }' | $(CLANG_BIN) -x c - -fdebug-prefix-map=$(CURDIR)=llvm_mode -o .test 2>/dev/null && echo 1 || echo 0 ; rm -f .test )" "1"
248        AFL_CLANG_DEBUG_PREFIX = -fdebug-prefix-map="$(CURDIR)=llvm_mode"
249else
250        AFL_CLANG_DEBUG_PREFIX =
251endif
252
253CFLAGS          ?= -O3 -funroll-loops -fPIC -D_FORTIFY_SOURCE=2
254CFLAGS_SAFE     := -Wall -g -Wno-cast-qual -Wno-variadic-macros -Wno-pointer-sign -I ./include/ -I ./instrumentation/ \
255                   -DAFL_PATH=\"$(HELPER_PATH)\" -DBIN_PATH=\"$(BIN_PATH)\" \
256                   -DLLVM_BINDIR=\"$(LLVM_BINDIR)\" -DVERSION=\"$(VERSION)\" \
257                   -DLLVM_LIBDIR=\"$(LLVM_LIBDIR)\" -DLLVM_VERSION=\"$(LLVMVER)\" \
258                   -Wno-deprecated -DAFL_CLANG_FLTO=\"$(AFL_CLANG_FLTO)\" \
259                   -DAFL_REAL_LD=\"$(AFL_REAL_LD)\" \
260                   -DAFL_CLANG_LDPATH=\"$(AFL_CLANG_LDPATH)\" \
261                   -DAFL_CLANG_FUSELD=\"$(AFL_CLANG_FUSELD)\" \
262                   -DCLANG_BIN=\"$(CLANG_BIN)\" -DCLANGPP_BIN=\"$(CLANGPP_BIN)\" -DUSE_BINDIR=$(USE_BINDIR) -Wno-unused-function \
263                   $(AFL_CLANG_DEBUG_PREFIX)
264override CFLAGS += $(CFLAGS_SAFE)
265
266ifdef AFL_TRACE_PC
267  $(info Compile option AFL_TRACE_PC is deprecated, just set AFL_LLVM_INSTRUMENT=PCGUARD to activate when compiling targets )
268endif
269
270CXXFLAGS          ?= -O3 -funroll-loops -fPIC -D_FORTIFY_SOURCE=2
271override CXXFLAGS += -Wall -g -I ./include/ \
272                     -DVERSION=\"$(VERSION)\" -Wno-variadic-macros \
273                     -DLLVM_MINOR=$(LLVM_MINOR) -DLLVM_MAJOR=$(LLVM_MAJOR)
274
275ifneq "$(shell $(LLVM_CONFIG) --includedir) 2> /dev/null" ""
276  CLANG_CFL  = -I$(shell $(LLVM_CONFIG) --includedir)
277endif
278ifneq "$(LLVM_CONFIG)" ""
279  CLANG_CFL += -I$(shell dirname $(LLVM_CONFIG))/../include
280endif
281CLANG_CPPFL  = `$(LLVM_CONFIG) --cxxflags` -fno-rtti -fPIC $(CXXFLAGS) -Wno-deprecated-declarations
282CLANG_LFL    = `$(LLVM_CONFIG) --ldflags` $(LDFLAGS)
283
284
285# User teor2345 reports that this is required to make things work on MacOS X.
286ifeq "$(SYS)" "Darwin"
287  CLANG_LFL += -Wl,-flat_namespace -Wl,-undefined,suppress
288  override LLVM_HAVE_LTO := 0
289  override LLVM_LTO := 0
290else
291  CLANG_CPPFL += -Wl,-znodelete
292endif
293
294ifeq "$(SYS)" "OpenBSD"
295  CLANG_LFL += `$(LLVM_CONFIG) --libdir`/libLLVM.so
296  CLANG_CPPFL += -mno-retpoline
297  CFLAGS += -mno-retpoline
298  # Needed for unwind symbols
299  LDFLAGS += -lc++abi -lpthread
300endif
301
302ifeq "$(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"
303        SHMAT_OK=1
304else
305        SHMAT_OK=0
306        CFLAGS_SAFE += -DUSEMMAP=1
307        LDFLAGS += -Wno-deprecated-declarations
308endif
309
310ifeq "$(TEST_MMAP)" "1"
311        SHMAT_OK=0
312        CFLAGS_SAFE += -DUSEMMAP=1
313        LDFLAGS += -Wno-deprecated-declarations
314endif
315
316PROGS_ALWAYS = ./afl-cc ./afl-compiler-rt.o ./afl-compiler-rt-32.o ./afl-compiler-rt-64.o
317PROGS        = $(PROGS_ALWAYS) ./afl-llvm-pass.so ./SanitizerCoveragePCGUARD.so ./split-compares-pass.so ./split-switches-pass.so ./cmplog-routines-pass.so ./cmplog-instructions-pass.so ./cmplog-switches-pass.so ./afl-llvm-dict2file.so ./compare-transform-pass.so ./afl-ld-lto ./afl-llvm-lto-instrumentlist.so ./SanitizerCoverageLTO.so
318
319# If prerequisites are not given, warn, do not build anything, and exit with code 0
320ifeq "$(LLVMVER)" ""
321  NO_BUILD = 1
322endif
323
324ifneq "$(LLVM_UNSUPPORTED)$(LLVM_APPLE_XCODE)" "00"
325  NO_BUILD = 1
326endif
327
328ifeq "$(NO_BUILD)" "1"
329  TARGETS = test_shm $(PROGS_ALWAYS) afl-cc.8
330else
331  TARGETS = test_shm test_deps $(PROGS) afl-cc.8 test_build all_done
332endif
333
334LLVM_MIN_4_0_1 = $(shell awk 'function tonum(ver, a) {split(ver,a,"."); return a[1]*1000000+a[2]*1000+a[3]} BEGIN { exit tonum(ARGV[1]) >= tonum(ARGV[2]) }' $(LLVMVER) 4.0.1; echo $$?)
335
336.PHONY: all
337all: $(TARGETS)
338
339.PHONY: test_shm
340ifeq "$(SHMAT_OK)" "1"
341test_shm:
342	@echo "[+] shmat seems to be working."
343	@rm -f .test2
344else
345test_shm:
346	@echo "[-] shmat seems not to be working, switching to mmap implementation"
347endif
348
349.PHONY: no_build
350no_build:
351	@printf "%b\\n" "\\033[0;31mPrerequisites are not met, skipping build llvm_mode\\033[0m"
352
353.PHONY: test_deps
354test_deps:
355	@echo "[*] Checking for working 'llvm-config'..."
356 ifneq "$(LLVM_APPLE_XCODE)" "1"
357	@type $(LLVM_CONFIG) >/dev/null 2>&1 || ( echo "[-] Oops, can't find 'llvm-config'. Install clang or set \$$LLVM_CONFIG or \$$PATH beforehand."; echo "    (Sometimes, the binary will be named llvm-config-11 or something like that.)"; exit 1 )
358 endif
359	@echo "[*] Checking for working '$(CC)'..."
360	@type $(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 )
361	@echo "[*] Checking for matching versions of '$(CC)' and '$(LLVM_CONFIG)'"
362ifneq "$(CLANGVER)" "$(LLVMVER)"
363	@echo "[!] WARNING: we have llvm-config version $(LLVMVER) and a clang version $(CLANGVER)"
364else
365	@echo "[*] We have llvm-config version $(LLVMVER) with a clang version $(CLANGVER), good."
366endif
367	@echo "[*] Checking for './afl-showmap'..."
368	@test -f ./afl-showmap || ( echo "[-] Oops, can't find './afl-showmap'. Be sure to compile AFL first."; exit 1 )
369	@echo "[+] All set and ready to build."
370
371instrumentation/afl-common.o: ./src/afl-common.c
372	$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ $(LDFLAGS)
373
374./afl-cc: src/afl-cc.c instrumentation/afl-common.o
375	$(CC) $(CLANG_CFL) $(CFLAGS) $(CPPFLAGS) $< instrumentation/afl-common.o -o $@ -DLLVM_MINOR=$(LLVM_MINOR) -DLLVM_MAJOR=$(LLVM_MAJOR) $(LDFLAGS) -DCFLAGS_OPT=\"$(CFLAGS_OPT)\" -lm
376	@ln -sf afl-cc ./afl-c++
377	@ln -sf afl-cc ./afl-gcc
378	@ln -sf afl-cc ./afl-g++
379	@ln -sf afl-cc ./afl-clang
380	@ln -sf afl-cc ./afl-clang++
381	@ln -sf afl-cc ./afl-clang-fast
382	@ln -sf afl-cc ./afl-clang-fast++
383ifneq "$(AFL_CLANG_FLTO)" ""
384ifeq "$(LLVM_LTO)" "1"
385	@ln -sf afl-cc ./afl-clang-lto
386	@ln -sf afl-cc ./afl-clang-lto++
387	@ln -sf afl-cc ./afl-lto
388	@ln -sf afl-cc ./afl-lto++
389endif
390endif
391
392instrumentation/afl-llvm-common.o: instrumentation/afl-llvm-common.cc instrumentation/afl-llvm-common.h
393	$(CXX) $(CFLAGS) $(CPPFLAGS) `$(LLVM_CONFIG) --cxxflags` -fno-rtti -fPIC -std=$(LLVM_STDCXX) -c $< -o $@
394
395./afl-llvm-pass.so: instrumentation/afl-llvm-pass.so.cc instrumentation/afl-llvm-common.o | test_deps
396ifeq "$(LLVM_MIN_4_0_1)" "0"
397	$(info [!] N-gram branch coverage instrumentation is not available for llvm version $(LLVMVER))
398endif
399	$(CXX) $(CLANG_CPPFL) -Wdeprecated -fno-rtti -fPIC -std=$(LLVM_STDCXX) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
400
401./SanitizerCoveragePCGUARD.so: instrumentation/SanitizerCoveragePCGUARD.so.cc instrumentation/afl-llvm-common.o | test_deps
402ifeq "$(LLVM_10_OK)" "1"
403	-$(CXX) $(CLANG_CPPFL) -fno-rtti -fPIC -std=$(LLVM_STDCXX) -shared $< -o $@ $(CLANG_LFL) -Wno-deprecated-copy-dtor -Wdeprecated instrumentation/afl-llvm-common.o
404endif
405
406./afl-llvm-lto-instrumentlist.so: instrumentation/afl-llvm-lto-instrumentlist.so.cc instrumentation/afl-llvm-common.o
407ifeq "$(LLVM_LTO)" "1"
408	$(CXX) $(CLANG_CPPFL) -fno-rtti -fPIC -std=$(LLVM_STDCXX) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
409endif
410
411./afl-ld-lto: src/afl-ld-lto.c
412ifeq "$(LLVM_LTO)" "1"
413	$(CC) $(CFLAGS) $(CPPFLAGS) $< -o $@
414endif
415
416./SanitizerCoverageLTO.so: instrumentation/SanitizerCoverageLTO.so.cc instrumentation/afl-llvm-common.o
417ifeq "$(LLVM_LTO)" "1"
418	$(CXX) $(CLANG_CPPFL) -Wno-writable-strings -fno-rtti -fPIC -std=$(LLVM_STDCXX) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
419	$(CLANG_BIN) $(CFLAGS_SAFE) $(CPPFLAGS) -Wno-unused-result -O0 $(AFL_CLANG_FLTO) -fPIC -c instrumentation/afl-llvm-rt-lto.o.c -o ./afl-llvm-rt-lto.o
420	@$(CLANG_BIN) $(CFLAGS_SAFE) $(CPPFLAGS) -Wno-unused-result -O0 $(AFL_CLANG_FLTO) -m64 -fPIC -c instrumentation/afl-llvm-rt-lto.o.c -o ./afl-llvm-rt-lto-64.o 2>/dev/null; if [ "$$?" = "0" ]; then : ; fi
421	@$(CLANG_BIN) $(CFLAGS_SAFE) $(CPPFLAGS) -Wno-unused-result -O0 $(AFL_CLANG_FLTO) -m32 -fPIC -c instrumentation/afl-llvm-rt-lto.o.c -o ./afl-llvm-rt-lto-32.o 2>/dev/null; if [ "$$?" = "0" ]; then : ; fi
422endif
423
424# laf
425./split-switches-pass.so:	instrumentation/split-switches-pass.so.cc instrumentation/afl-llvm-common.o | test_deps
426	$(CXX) $(CLANG_CPPFL) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
427./compare-transform-pass.so:	instrumentation/compare-transform-pass.so.cc instrumentation/afl-llvm-common.o | test_deps
428	$(CXX) $(CLANG_CPPFL) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
429./split-compares-pass.so:	instrumentation/split-compares-pass.so.cc instrumentation/afl-llvm-common.o | test_deps
430	$(CXX) $(CLANG_CPPFL) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
431# /laf
432
433./cmplog-routines-pass.so:	instrumentation/cmplog-routines-pass.cc instrumentation/afl-llvm-common.o | test_deps
434	$(CXX) $(CLANG_CPPFL) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
435
436./cmplog-instructions-pass.so:	instrumentation/cmplog-instructions-pass.cc instrumentation/afl-llvm-common.o | test_deps
437	$(CXX) $(CLANG_CPPFL) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
438
439./cmplog-switches-pass.so:	instrumentation/cmplog-switches-pass.cc instrumentation/afl-llvm-common.o | test_deps
440	$(CXX) $(CLANG_CPPFL) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
441
442afl-llvm-dict2file.so:	instrumentation/afl-llvm-dict2file.so.cc instrumentation/afl-llvm-common.o | test_deps
443	$(CXX) $(CLANG_CPPFL) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
444
445.PHONY: document
446document:
447	$(CLANG_BIN) -D_AFL_DOCUMENT_MUTATIONS $(CFLAGS_SAFE) $(CPPFLAGS) $(CLANG_CFL) -O3 -Wno-unused-result -fPIC -c instrumentation/afl-compiler-rt.o.c -o ./afl-compiler-rt.o
448	@$(CLANG_BIN) -D_AFL_DOCUMENT_MUTATIONS $(CFLAGS_SAFE) $(CPPFLAGS) $(CLANG_CFL) -O3 -Wno-unused-result -m32 -fPIC -c instrumentation/afl-compiler-rt.o.c -o ./afl-compiler-rt-32.o 2>/dev/null; if [ "$$?" = "0" ]; then echo "success!"; else echo "failed (that's fine)"; fi
449	@$(CLANG_BIN) -D_AFL_DOCUMENT_MUTATIONS $(CFLAGS_SAFE) $(CPPFLAGS) $(CLANG_CFL) -O3 -Wno-unused-result -m64 -fPIC -c instrumentation/afl-compiler-rt.o.c -o ./afl-compiler-rt-64.o 2>/dev/null; if [ "$$?" = "0" ]; then echo "success!"; else echo "failed (that's fine)"; fi
450
451./afl-compiler-rt.o: instrumentation/afl-compiler-rt.o.c
452	$(CC) $(CLANG_CFL) $(CFLAGS_SAFE) $(CPPFLAGS) -O3 -Wno-unused-result -fPIC -c $< -o $@
453
454./afl-compiler-rt-32.o: instrumentation/afl-compiler-rt.o.c
455	@printf "[*] Building 32-bit variant of the runtime (-m32)... "
456	@$(CC) $(CLANG_CFL) $(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
457
458./afl-compiler-rt-64.o: instrumentation/afl-compiler-rt.o.c
459	@printf "[*] Building 64-bit variant of the runtime (-m64)... "
460	@$(CC) $(CLANG_CFL) $(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
461
462.PHONY: test_build
463test_build: $(PROGS)
464	@echo "[*] Testing the CC wrapper and instrumentation output..."
465	unset AFL_USE_ASAN AFL_USE_MSAN AFL_INST_RATIO; ASAN_OPTIONS=detect_leaks=0 AFL_QUIET=1 AFL_PATH=. AFL_LLVM_LAF_ALL=1 ./afl-cc $(CFLAGS) $(CPPFLAGS) ./test-instr.c -o test-instr $(LDFLAGS)
466	ASAN_OPTIONS=detect_leaks=0 ./afl-showmap -m none -q -o .test-instr0 ./test-instr < /dev/null
467	echo 1 | ASAN_OPTIONS=detect_leaks=0 ./afl-showmap -m none -q -o .test-instr1 ./test-instr
468	@rm -f test-instr
469	@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
470	@echo "[+] All right, the instrumentation seems to be working!"
471
472.PHONY: all_done
473all_done: test_build
474	@echo "[+] All done! You can now use './afl-cc' to compile programs."
475
476.NOTPARALLEL: clean
477
478.PHONY: install
479install: all
480	@install -d -m 755 $${DESTDIR}$(BIN_PATH) $${DESTDIR}$(HELPER_PATH) $${DESTDIR}$(DOC_PATH) $${DESTDIR}$(MISC_PATH)
481	@if [ -f ./afl-cc ]; then set -e; install -m 755 ./afl-cc $${DESTDIR}$(BIN_PATH); ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-c++; fi
482	@rm -f $${DESTDIR}$(HELPER_PATH)/afl-llvm-rt*.o $${DESTDIR}$(HELPER_PATH)/afl-gcc-rt*.o
483	@if [ -f ./afl-compiler-rt.o ]; then set -e; install -m 755 ./afl-compiler-rt.o $${DESTDIR}$(HELPER_PATH); fi
484	@if [ -f ./afl-lto ]; then set -e; ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-lto; ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-lto++; ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-clang-lto; ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-clang-lto++; install -m 755 ./afl-llvm-rt-lto*.o ./afl-llvm-lto-instrumentlist.so $${DESTDIR}$(HELPER_PATH); fi
485	@if [ -f ./afl-ld-lto ]; then set -e; install -m 755 ./afl-ld-lto $${DESTDIR}$(BIN_PATH); fi
486	@if [ -f ./afl-compiler-rt-32.o ]; then set -e; install -m 755 ./afl-compiler-rt-32.o $${DESTDIR}$(HELPER_PATH); fi
487	@if [ -f ./afl-compiler-rt-64.o ]; then set -e; install -m 755 ./afl-compiler-rt-64.o $${DESTDIR}$(HELPER_PATH); fi
488	@if [ -f ./compare-transform-pass.so ]; then set -e; install -m 755 ./*.so $${DESTDIR}$(HELPER_PATH); fi
489	@if [ -f ./compare-transform-pass.so ]; then set -e; ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-clang-fast ; ln -sf ./afl-c++ $${DESTDIR}$(BIN_PATH)/afl-clang-fast++ ; ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-clang ; ln -sf ./afl-c++ $${DESTDIR}$(BIN_PATH)/afl-clang++ ; fi
490	@if [ -f ./SanitizerCoverageLTO.so ]; then set -e; ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-clang-lto ; ln -sf ./afl-c++ $${DESTDIR}$(BIN_PATH)/afl-clang-lto++ ; fi
491	set -e; install -m 644 ./dynamic_list.txt $${DESTDIR}$(HELPER_PATH)
492	install -m 644 instrumentation/README.*.md $${DESTDIR}$(DOC_PATH)/
493
494%.8: %
495	@echo .TH $* 8 $(BUILD_DATE) "afl++" > ./$@
496	@echo .SH NAME >> ./$@
497	@printf "%s" ".B $* \- " >> ./$@
498	@./$* -h 2>&1 | head -n 1 | sed -e "s/$$(printf '\e')[^m]*m//g" >> ./$@
499	@echo .B $* >> ./$@
500	@echo >> ./$@
501	@echo .SH SYNOPSIS >> ./$@
502	@./$* -h 2>&1 | head -n 3 | tail -n 1 | sed 's/^\.\///' >> ./$@
503	@echo >> ./$@
504	@echo .SH OPTIONS >> ./$@
505	@echo .nf >> ./$@
506	@./$* -h 2>&1 | tail -n +4 >> ./$@
507	@echo >> ./$@
508	@echo .SH AUTHOR >> ./$@
509	@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>" >> ./$@
510	@echo  The homepage of afl++ is: https://github.com/AFLplusplus/AFLplusplus >> ./$@
511	@echo >> ./$@
512	@echo .SH LICENSE >> ./$@
513	@echo Apache License Version 2.0, January 2004 >> ./$@
514	@ln -sf afl-cc.8 ./afl-c++.8
515	@ln -sf afl-cc.8 ./afl-clang-fast.8
516	@ln -sf afl-cc.8 ./afl-clang-fast++.8
517ifneq "$(AFL_CLANG_FLTO)" ""
518ifeq "$(LLVM_LTO)" "1"
519	@ln -sf afl-cc.8 ./afl-clang-lto.8
520	@ln -sf afl-cc.8 ./afl-clang-lto++.8
521	@ln -sf afl-cc.8 ./afl-lto.8
522	@ln -sf afl-cc.8 ./afl-lto++.8
523endif
524endif
525
526.PHONY: clean
527clean:
528	rm -f *.o *.so *~ a.out core core.[1-9][0-9]* .test2 test-instr .test-instr0 .test-instr1 *.dwo
529	rm -f $(PROGS) afl-common.o ./afl-c++ ./afl-lto ./afl-lto++ ./afl-clang-lto* ./afl-clang-fast* ./afl-clang*.8 ./ld ./afl-ld ./afl-compiler-rt*.o ./afl-llvm-rt*.o instrumentation/*.o
530