1#
2# american fuzzy lop - LLVM instrumentation
3# -----------------------------------------
4#
5# Written by Laszlo Szekeres <lszekeres@google.com> and
6#            Michal Zalewski <lcamtuf@google.com>
7#
8# LLVM integration design comes from Laszlo Szekeres.
9#
10# Copyright 2015, 2016 Google Inc. All rights reserved.
11#
12# Licensed under the Apache License, Version 2.0 (the "License");
13# you may not use this file except in compliance with the License.
14# You may obtain a copy of the License at:
15#
16#   http://www.apache.org/licenses/LICENSE-2.0
17#
18
19PREFIX      ?= /usr/local
20HELPER_PATH  = $(PREFIX)/lib/afl
21BIN_PATH     = $(PREFIX)/bin
22
23VERSION     = $(shell grep '^\#define VERSION ' ../config.h | cut -d '"' -f2)
24
25LLVM_CONFIG ?= llvm-config
26
27CFLAGS      ?= -O3 -funroll-loops
28CFLAGS      += -Wall -D_FORTIFY_SOURCE=2 -g -Wno-pointer-sign \
29               -DAFL_PATH=\"$(HELPER_PATH)\" -DBIN_PATH=\"$(BIN_PATH)\" \
30               -DVERSION=\"$(VERSION)\"
31ifdef AFL_TRACE_PC
32  CFLAGS    += -DUSE_TRACE_PC=1
33endif
34
35CXXFLAGS    ?= -O3 -funroll-loops
36CXXFLAGS    += -Wall -D_FORTIFY_SOURCE=2 -g -Wno-pointer-sign \
37               -DVERSION=\"$(VERSION)\" -Wno-variadic-macros
38
39CLANG_CFL    = `$(LLVM_CONFIG) --cxxflags` -fno-rtti -fpic $(CXXFLAGS)
40CLANG_LFL    = `$(LLVM_CONFIG) --ldflags` $(LDFLAGS)
41
42# User teor2345 reports that this is required to make things work on MacOS X.
43
44ifeq "$(shell uname)" "Darwin"
45  CLANG_LFL += -Wl,-flat_namespace -Wl,-undefined,suppress
46endif
47
48# We were using llvm-config --bindir to get the location of clang, but
49# this seems to be busted on some distros, so using the one in $PATH is
50# probably better.
51
52ifeq "$(origin CC)" "default"
53  CC         = clang
54  CXX        = clang++
55endif
56
57ifndef AFL_TRACE_PC
58  PROGS      = ../afl-clang-fast ../afl-llvm-pass.so ../afl-llvm-rt.o ../afl-llvm-rt-32.o ../afl-llvm-rt-64.o
59else
60  PROGS      = ../afl-clang-fast ../afl-llvm-rt.o ../afl-llvm-rt-32.o ../afl-llvm-rt-64.o
61endif
62
63all: test_deps $(PROGS) all_done
64
65test_deps:
66ifndef AFL_TRACE_PC
67	@echo "[*] Checking for working 'llvm-config'..."
68	@which $(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-3.5 or something like that.)"; exit 1 )
69else
70	@echo "[!] Note: using -fsanitize=trace-pc mode (this will fail with older LLVM)."
71endif
72	@echo "[*] Checking for working '$(CC)'..."
73	@which $(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 )
74	@echo "[*] Checking for '../afl-showmap'..."
75	@test -f ../afl-showmap || ( echo "[-] Oops, can't find '../afl-showmap'. Be sure to compile AFL first."; exit 1 )
76	@echo "[+] All set and ready to build."
77
78../afl-clang-fast: afl-clang-fast.c | test_deps
79	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
80	ln -sf afl-clang-fast ../afl-clang-fast++
81
82../afl-llvm-pass.so: afl-llvm-pass.so.cc | test_deps
83	$(CXX) $(CLANG_CFL) -shared $< -o $@ $(CLANG_LFL)
84
85../afl-llvm-rt.o: afl-llvm-rt.o.c | test_deps
86	$(CC) $(CFLAGS) -fPIC -c $< -o $@
87
88../afl-llvm-rt-32.o: afl-llvm-rt.o.c | test_deps
89	@printf "[*] Building 32-bit variant of the runtime (-m32)... "
90	@$(CC) $(CFLAGS) -m32 -fPIC -c $< -o $@ 2>/dev/null; if [ "$$?" = "0" ]; then echo "success!"; else echo "failed (that's fine)"; fi
91
92../afl-llvm-rt-64.o: afl-llvm-rt.o.c | test_deps
93	@printf "[*] Building 64-bit variant of the runtime (-m64)... "
94	@$(CC) $(CFLAGS) -m64 -fPIC -c $< -o $@ 2>/dev/null; if [ "$$?" = "0" ]; then echo "success!"; else echo "failed (that's fine)"; fi
95
96test_build: $(PROGS)
97	@echo "[*] Testing the CC wrapper and instrumentation output..."
98	unset AFL_USE_ASAN AFL_USE_MSAN AFL_INST_RATIO; AFL_QUIET=1 AFL_PATH=. AFL_CC=$(CC) ../afl-clang-fast $(CFLAGS) ../test-instr.c -o test-instr $(LDFLAGS)
99	echo 0 | ../afl-showmap -m none -q -o .test-instr0 ./test-instr
100	echo 1 | ../afl-showmap -m none -q -o .test-instr1 ./test-instr
101	@rm -f test-instr
102	@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 ping <lcamtuf@google.com> to troubleshoot the issue."; echo; exit 1; fi
103	@echo "[+] All right, the instrumentation seems to be working!"
104
105all_done:
106	@echo "[+] All done! You can now use '../afl-clang-fast' to compile programs."
107
108.NOTPARALLEL: clean
109
110clean:
111	rm -f *.o *.so *~ a.out core core.[1-9][0-9]* test-instr .test-instr0 .test-instr1
112	rm -f $(PROGS) ../afl-clang-fast++
113