From 05a36f10ba99a461647a41433f199dd4ebc95e57 Mon Sep 17 00:00:00 2001 From: vanhauser-thc Date: Tue, 8 Feb 2022 18:33:59 +0100 Subject: import mozilla afl-cc patch --- src/afl-cc.c | 93 +++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 33 deletions(-) (limited to 'src/afl-cc.c') diff --git a/src/afl-cc.c b/src/afl-cc.c index 9197c74b..1f28b1f9 100644 --- a/src/afl-cc.c +++ b/src/afl-cc.c @@ -315,7 +315,7 @@ static void edit_params(u32 argc, char **argv, char **envp) { u8 fortify_set = 0, asan_set = 0, x_set = 0, bit_mode = 0, shared_linking = 0, preprocessor_only = 0, have_unroll = 0, have_o = 0, have_pic = 0, - have_c = 0, partial_linking = 0; + have_c = 0, partial_linking = 0, wasm_linking = 0; cc_params = ck_alloc((argc + 128) * sizeof(u8 *)); @@ -671,22 +671,6 @@ static void edit_params(u32 argc, char **argv, char **envp) { // cc_params[cc_par_cnt++] = "-Qunused-arguments"; - // in case LLVM is installed not via a package manager or "make install" - // e.g. compiled download or compiled from github then its ./lib directory - // might not be in the search path. Add it if so. - u8 *libdir = strdup(LLVM_LIBDIR); - if (plusplus_mode && strlen(libdir) && strncmp(libdir, "/usr", 4) && - strncmp(libdir, "/lib", 4)) { - - cc_params[cc_par_cnt++] = "-rpath"; - cc_params[cc_par_cnt++] = libdir; - - } else { - - free(libdir); - - } - if (lto_mode && argc > 1) { u32 idx; @@ -766,14 +750,21 @@ static void edit_params(u32 argc, char **argv, char **envp) { u8 *afllib = find_object("libAFLDriver.a", argv[0]); - if (!be_quiet) + if (!be_quiet) { + OKF("Found '-fsanitize=fuzzer', replacing with libAFLDriver.a"); + } + if (!afllib) { - WARNF( - "Cannot find 'libAFLDriver.a' to replace '-fsanitize=fuzzer' in " - "the flags - this will fail!"); + if (!be_quiet) { + + WARNF( + "Cannot find 'libAFLDriver.a' to replace '-fsanitize=fuzzer' in " + "the flags - this will fail!"); + + } } else { @@ -805,6 +796,13 @@ static void edit_params(u32 argc, char **argv, char **envp) { if (!strcmp(cur, "-x")) x_set = 1; if (!strcmp(cur, "-E")) preprocessor_only = 1; if (!strcmp(cur, "-shared")) shared_linking = 1; + if (!strcmp(cur, "--target=wasm32-wasi")) { + + if (!be_quiet) { WARNF("Found '%s'!", cur); } + wasm_linking = 1; + + } + if (!strcmp(cur, "-dynamiclib")) shared_linking = 1; if (!strcmp(cur, "-Wl,-r")) partial_linking = 1; if (!strcmp(cur, "-Wl,-i")) partial_linking = 1; @@ -820,6 +818,22 @@ static void edit_params(u32 argc, char **argv, char **envp) { } + // in case LLVM is installed not via a package manager or "make install" + // e.g. compiled download or compiled from github then its ./lib directory + // might not be in the search path. Add it if so. + u8 *libdir = strdup(LLVM_LIBDIR); + if (plusplus_mode && !wasm_linking && strlen(libdir) && + strncmp(libdir, "/usr", 4) && strncmp(libdir, "/lib", 4)) { + + cc_params[cc_par_cnt++] = "-rpath"; + cc_params[cc_par_cnt++] = libdir; + + } else { + + free(libdir); + + } + if (getenv("AFL_HARDEN")) { cc_params[cc_par_cnt++] = "-fstack-protector-all"; @@ -1056,7 +1070,7 @@ static void edit_params(u32 argc, char **argv, char **envp) { switch (bit_mode) { case 0: - if (!shared_linking && !partial_linking) + if (!shared_linking && !partial_linking && !wasm_linking) cc_params[cc_par_cnt++] = alloc_printf("%s/afl-compiler-rt.o", obj_path); if (lto_mode) @@ -1065,7 +1079,7 @@ static void edit_params(u32 argc, char **argv, char **envp) { break; case 32: - if (!shared_linking && !partial_linking) { + if (!shared_linking && !partial_linking && !wasm_linking) { cc_params[cc_par_cnt++] = alloc_printf("%s/afl-compiler-rt-32.o", obj_path); @@ -1086,7 +1100,7 @@ static void edit_params(u32 argc, char **argv, char **envp) { break; case 64: - if (!shared_linking && !partial_linking) { + if (!shared_linking && !partial_linking && !wasm_linking) { cc_params[cc_par_cnt++] = alloc_printf("%s/afl-compiler-rt-64.o", obj_path); @@ -1109,7 +1123,7 @@ static void edit_params(u32 argc, char **argv, char **envp) { } #if !defined(__APPLE__) && !defined(__sun) - if (!shared_linking && !partial_linking) + if (!shared_linking && !partial_linking && !wasm_linking) cc_params[cc_par_cnt++] = alloc_printf("-Wl,--dynamic-list=%s/dynamic_list.txt", obj_path); #endif @@ -1248,10 +1262,14 @@ int main(int argc, char **argv, char **envp) { if (compiler_mode) { - WARNF( - "\"AFL_CC_COMPILER\" is set but a specific compiler was already " - "selected by command line parameter or symlink, ignoring the " - "environment variable!"); + if (!be_quiet) { + + WARNF( + "\"AFL_CC_COMPILER\" is set but a specific compiler was already " + "selected by command line parameter or symlink, ignoring the " + "environment variable!"); + + } } else { @@ -1304,11 +1322,14 @@ int main(int argc, char **argv, char **envp) { } - if (compiler_mode) + if (compiler_mode && !be_quiet) { + WARNF( "--afl-... compiler mode supersedes the AFL_CC_COMPILER and " "symlink compiler selection!"); + } + ptr = argv[i]; ptr += 5; while (*ptr == '-') @@ -1390,7 +1411,7 @@ int main(int argc, char **argv, char **envp) { } - if (have_instr_env && getenv("AFL_DONT_OPTIMIZE")) { + if (have_instr_env && getenv("AFL_DONT_OPTIMIZE") && !be_quiet) { WARNF( "AFL_LLVM_ALLOWLIST/DENYLIST and AFL_DONT_OPTIMIZE cannot be combined " @@ -1970,10 +1991,13 @@ int main(int argc, char **argv, char **envp) { } else { - if (!be_quiet) + if (!be_quiet) { + WARNF("afl-clang-lto called with mode %s, using that mode instead", instrument_mode_string[instrument_mode]); + } + } } @@ -1985,11 +2009,14 @@ int main(int argc, char **argv, char **envp) { if (have_instr_env) { instrument_mode = INSTRUMENT_AFL; - if (!be_quiet) + if (!be_quiet) { + WARNF( "Switching to classic instrumentation because " "AFL_LLVM_ALLOWLIST/DENYLIST does not work with PCGUARD < 10.0.1."); + } + } else #endif -- cgit 1.4.1 From fa628865c1d3b8d0cc4bc04efc516fc7b48b6a69 Mon Sep 17 00:00:00 2001 From: vanhauser-thc Date: Tue, 8 Feb 2022 18:43:23 +0100 Subject: remove debug msg --- src/afl-cc.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'src/afl-cc.c') diff --git a/src/afl-cc.c b/src/afl-cc.c index 1f28b1f9..2d7e3d91 100644 --- a/src/afl-cc.c +++ b/src/afl-cc.c @@ -796,14 +796,8 @@ static void edit_params(u32 argc, char **argv, char **envp) { if (!strcmp(cur, "-x")) x_set = 1; if (!strcmp(cur, "-E")) preprocessor_only = 1; if (!strcmp(cur, "-shared")) shared_linking = 1; - if (!strcmp(cur, "--target=wasm32-wasi")) { - - if (!be_quiet) { WARNF("Found '%s'!", cur); } - wasm_linking = 1; - - } - if (!strcmp(cur, "-dynamiclib")) shared_linking = 1; + if (!strcmp(cur, "--target=wasm32-wasi")) wasm_linking = 1; if (!strcmp(cur, "-Wl,-r")) partial_linking = 1; if (!strcmp(cur, "-Wl,-i")) partial_linking = 1; if (!strcmp(cur, "-Wl,--relocatable")) partial_linking = 1; -- cgit 1.4.1 From 5e8da2b85c13eeaac245f94ef9232c674cd2e146 Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Wed, 9 Feb 2022 20:36:16 +0100 Subject: Adapt to my MacOS --- GNUmakefile | 10 ++++++++-- src/afl-cc.c | 4 ++-- utils/aflpp_driver/GNUmakefile | 8 +++++++- 3 files changed, 17 insertions(+), 5 deletions(-) (limited to 'src/afl-cc.c') diff --git a/GNUmakefile b/GNUmakefile index 1c5d992e..6392fceb 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -97,6 +97,12 @@ ifneq "$(SYS)" "Darwin" endif endif +ifeq "$(SYS)" "Darwin" + # On some odd MacOS system configurations, the Xcode sdk path is not set correctly + SDK_LD = -L$(shell xcrun --show-sdk-path)/usr/lib + LDFLAGS += $(SDK_LD) +endif + ifeq "$(SYS)" "SunOS" CFLAGS_OPT += -Wno-format-truncation LDFLAGS = -lkstat -lrt @@ -384,7 +390,7 @@ test_x86: @echo "[*] Testing the PATH environment variable..." @test "$${PATH}" != "$${PATH#.:}" && { echo "Please remove current directory '.' from PATH to avoid recursion of 'as', thanks!"; echo; exit 1; } || : @echo "[*] Checking for the ability to compile x86 code..." - @echo 'main() { __asm__("xorb %al, %al"); }' | $(CC) $(CFLAGS) -w -x c - -o .test1 || ( echo; echo "Oops, looks like your compiler can't generate x86 code."; echo; echo "Don't panic! You can use the LLVM or QEMU mode, but see docs/INSTALL first."; echo "(To ignore this error, set AFL_NO_X86=1 and try again.)"; echo; exit 1 ) + @echo 'main() { __asm__("xorb %al, %al"); }' | $(CC) $(CFLAGS) $(LDFLAGS) -w -x c - -o .test1 || ( echo; echo "Oops, looks like your compiler can't generate x86 code."; echo; echo "Don't panic! You can use the LLVM or QEMU mode, but see docs/INSTALL first."; echo "(To ignore this error, set AFL_NO_X86=1 and try again.)"; echo; exit 1 ) @rm -f .test1 else test_x86: @@ -528,7 +534,7 @@ code-format: ifndef AFL_NO_X86 test_build: afl-cc afl-gcc afl-as afl-showmap @echo "[*] Testing the CC wrapper afl-cc and its instrumentation output..." - @unset AFL_MAP_SIZE AFL_USE_UBSAN AFL_USE_CFISAN AFL_USE_LSAN AFL_USE_ASAN AFL_USE_MSAN; ASAN_OPTIONS=detect_leaks=0 AFL_INST_RATIO=100 AFL_PATH=. ./afl-cc test-instr.c -o test-instr 2>&1 || (echo "Oops, afl-cc failed"; exit 1 ) + @unset AFL_MAP_SIZE AFL_USE_UBSAN AFL_USE_CFISAN AFL_USE_LSAN AFL_USE_ASAN AFL_USE_MSAN; ASAN_OPTIONS=detect_leaks=0 AFL_INST_RATIO=100 AFL_PATH=. ./afl-cc test-instr.c $(LDFLAGS) -o test-instr 2>&1 || (echo "Oops, afl-cc failed"; exit 1 ) ASAN_OPTIONS=detect_leaks=0 ./afl-showmap -m none -q -o .test-instr0 ./test-instr < /dev/null echo 1 | ASAN_OPTIONS=detect_leaks=0 ./afl-showmap -m none -q -o .test-instr1 ./test-instr @rm -f test-instr diff --git a/src/afl-cc.c b/src/afl-cc.c index 2d7e3d91..ed57ca1e 100644 --- a/src/afl-cc.c +++ b/src/afl-cc.c @@ -931,7 +931,7 @@ static void edit_params(u32 argc, char **argv, char **envp) { } -#if defined(USEMMAP) && !defined(__HAIKU__) +#if defined(USEMMAP) && !defined(__HAIKU__) && !__APPLE__ if (!have_c) cc_params[cc_par_cnt++] = "-lrt"; #endif @@ -1136,7 +1136,7 @@ static void edit_params(u32 argc, char **argv, char **envp) { } - #if defined(USEMMAP) && !defined(__HAIKU__) + #if defined(USEMMAP) && !defined(__HAIKU__) && !__APPLE__ cc_params[cc_par_cnt++] = "-lrt"; #endif diff --git a/utils/aflpp_driver/GNUmakefile b/utils/aflpp_driver/GNUmakefile index c282a9f3..234a1c31 100644 --- a/utils/aflpp_driver/GNUmakefile +++ b/utils/aflpp_driver/GNUmakefile @@ -2,6 +2,12 @@ ifeq "" "$(LLVM_CONFIG)" LLVM_CONFIG=llvm-config endif +ifeq "$(shell uname -s)" "Darwin" + # On some odd MacOS system configurations, the Xcode sdk path is not set correctly + SDK_LD = -L$(shell xcrun --show-sdk-path)/usr/lib + LDFLAGS += $(SDK_LD) +endif + LLVM_BINDIR = $(shell $(LLVM_CONFIG) --bindir 2>/dev/null) ifneq "" "$(LLVM_BINDIR)" LLVM_BINDIR := $(LLVM_BINDIR)/ @@ -33,7 +39,7 @@ libAFLQemuDriver.a: aflpp_qemu_driver.o @-cp -vf libAFLQemuDriver.a ../../ aflpp_qemu_driver_hook.so: aflpp_qemu_driver_hook.o - @-test -e aflpp_qemu_driver_hook.o && $(LLVM_BINDIR)clang -shared aflpp_qemu_driver_hook.o -o aflpp_qemu_driver_hook.so || echo "Note: Optional aflpp_qemu_driver_hook.so not built." + @-test -e aflpp_qemu_driver_hook.o && $(LLVM_BINDIR)clang $(LDFLAGS) -shared aflpp_qemu_driver_hook.o -o aflpp_qemu_driver_hook.so || echo "Note: Optional aflpp_qemu_driver_hook.so not built." aflpp_qemu_driver_hook.o: aflpp_qemu_driver_hook.c @-test -e ../../qemu_mode/qemuafl/qemuafl/api.h && $(LLVM_BINDIR)clang $(CFLAGS) -funroll-loops -c aflpp_qemu_driver_hook.c || echo "Note: Optional aflpp_qemu_driver_hook.o not built." -- cgit 1.4.1