aboutsummaryrefslogtreecommitdiff
path: root/llvm_mode
diff options
context:
space:
mode:
authorAndrea Fioraldi <andreafioraldi@gmail.com>2020-01-30 22:40:09 +0100
committerAndrea Fioraldi <andreafioraldi@gmail.com>2020-01-30 22:40:09 +0100
commit95a98fb3e805d94a3db82d5da21e9a00a79dbdf2 (patch)
tree4de4f17f898a4f0632098c9902614217fa2e53eb /llvm_mode
parentb8bad5a2273b0cddd0244a7f37c20150a08af475 (diff)
downloadafl++-95a98fb3e805d94a3db82d5da21e9a00a79dbdf2.tar.gz
cmplog runtime for LLVM
Diffstat (limited to 'llvm_mode')
-rw-r--r--llvm_mode/Makefile17
-rw-r--r--llvm_mode/README.cmplog.md42
-rw-r--r--llvm_mode/afl-clang-fast.c44
-rw-r--r--llvm_mode/afl-llvm-cmplog-rt.o.c412
-rw-r--r--llvm_mode/afl-llvm-cmplog.rt.o.c422
-rw-r--r--llvm_mode/afl-llvm-rt.o.c123
6 files changed, 921 insertions, 139 deletions
diff --git a/llvm_mode/Makefile b/llvm_mode/Makefile
index a455dc8a..9ee6fc8b 100644
--- a/llvm_mode/Makefile
+++ b/llvm_mode/Makefile
@@ -19,8 +19,6 @@
# For Heiko:
#TEST_MMAP=1
-AFL_TRACE_PC=1
-
PREFIX ?= /usr/local
HELPER_PATH = $(PREFIX)/lib/afl
BIN_PATH = $(PREFIX)/bin
@@ -133,9 +131,9 @@ ifeq "$(TEST_MMAP)" "1"
endif
ifndef AFL_TRACE_PC
- PROGS = ../afl-clang-fast ../afl-llvm-pass.so ../libLLVMInsTrim.so ../afl-llvm-rt.o ../afl-llvm-rt-32.o ../afl-llvm-rt-64.o ../compare-transform-pass.so ../split-compares-pass.so ../split-switches-pass.so
+ PROGS = ../afl-clang-fast ../afl-llvm-cmplog-rt.o ../afl-llvm-cmplog-rt-32.o ../afl-llvm-cmplog-rt-64.o ../afl-llvm-pass.so ../libLLVMInsTrim.so ../afl-llvm-rt.o ../afl-llvm-rt-32.o ../afl-llvm-rt-64.o ../compare-transform-pass.so ../split-compares-pass.so ../split-switches-pass.so
else
- PROGS = ../afl-clang-fast ../afl-llvm-rt.o ../afl-llvm-rt-32.o ../afl-llvm-rt-64.o ../compare-transform-pass.so ../split-compares-pass.so ../split-switches-pass.so
+ PROGS = ../afl-clang-fast ../afl-llvm-cmplog-rt.o ../afl-llvm-cmplog-rt-32.o ../afl-llvm-cmplog-rt-64.o ../afl-llvm-rt.o ../afl-llvm-rt-32.o ../afl-llvm-rt-64.o ../compare-transform-pass.so ../split-compares-pass.so ../split-switches-pass.so
endif
ifneq "$(CLANGVER)" "$(LLVMVER)"
@@ -228,6 +226,17 @@ endif
@printf "[*] Building 64-bit variant of the runtime (-m64)... "
@$(CC) $(CFLAGS) -m64 -fPIC -c $< -o $@ 2>/dev/null; if [ "$$?" = "0" ]; then echo "success!"; else echo "failed (that's fine)"; fi
+../afl-llvm-cmplog-rt.o: afl-llvm-cmplog-rt.o.c | test_deps
+ $(CC) $(CFLAGS) -fPIC -c $< -o $@
+
+../afl-llvm-cmplog-rt-32.o: afl-llvm-cmplog-rt.o.c | test_deps
+ @printf "[*] Building 32-bit variant of the CmpLog runtime (-m32)... "
+ @$(CC) $(CFLAGS) -m32 -fPIC -c $< -o $@ 2>/dev/null; if [ "$$?" = "0" ]; then echo "success!"; else echo "failed (that's fine)"; fi
+
+../afl-llvm-cmplog-rt-64.o: afl-llvm-cmplog-rt.o.c | test_deps
+ @printf "[*] Building 64-bit variant of the CmpLog runtime (-m64)... "
+ @$(CC) $(CFLAGS) -m64 -fPIC -c $< -o $@ 2>/dev/null; if [ "$$?" = "0" ]; then echo "success!"; else echo "failed (that's fine)"; fi
+
test_build: $(PROGS)
@echo "[*] Testing the CC wrapper and instrumentation output..."
unset AFL_USE_ASAN AFL_USE_MSAN AFL_INST_RATIO; AFL_QUIET=1 AFL_PATH=. AFL_CC=$(CC) AFL_LLVM_LAF_SPLIT_SWITCHES=1 AFL_LLVM_LAF_TRANSFORM_COMPARES=1 AFL_LLVM_LAF_SPLIT_COMPARES=1 ../afl-clang-fast $(CFLAGS) ../test-instr.c -o test-instr $(LDFLAGS)
diff --git a/llvm_mode/README.cmplog.md b/llvm_mode/README.cmplog.md
new file mode 100644
index 00000000..a0e838ad
--- /dev/null
+++ b/llvm_mode/README.cmplog.md
@@ -0,0 +1,42 @@
+# CmpLog instrumentation
+
+The CmpLog instrumentation enables the logging of the comparisons operands in a
+shared memory.
+
+These values can be used by variuous mutator built on top of it.
+At the moment we support the RedQueen mutator (input-2-state instructions only).
+
+## Build
+
+Tou use CmpLog, you have to build two versions of the instrumented target
+program.
+
+The first, using the regular AFL++ instrumentation.
+
+The second, the CmpLog binary, setting AFL_LLVM_CMPLOG during the compilation.
+
+For example:
+
+```
+./configure --cc=~/path/to/afl-clang-fast
+make
+cp ./program ./program.afl
+make clean
+export AFL_LLVM_CMPLOG=1
+./configure --cc=~/path/to/afl-clang-fast
+make
+cp ./program ./program.cmplog
+```
+
+## Use
+
+AFL++ have the -c option that can be used to specify a CmpLog binary (the second
+built).
+
+For example:
+
+```
+afl-fuzz -i input -o output -c ./program.cmplog -m none -- ./program.afl @@
+```
+
+Be careful to use -m none because CmpLog maps a lot of pages.
diff --git a/llvm_mode/afl-clang-fast.c b/llvm_mode/afl-clang-fast.c
index 939546d7..ddaa2388 100644
--- a/llvm_mode/afl-clang-fast.c
+++ b/llvm_mode/afl-clang-fast.c
@@ -40,6 +40,7 @@ static u8* obj_path; /* Path to runtime libraries */
static u8** cc_params; /* Parameters passed to the real CC */
static u32 cc_par_cnt = 1; /* Param count, including argv0 */
static u8 llvm_fullpath[PATH_MAX];
+static u8 cmplog_mode;
/* Try to find the runtime libraries. If that fails, abort. */
@@ -196,16 +197,20 @@ static void edit_params(u32 argc, char** argv) {
// /laf
-#ifdef USE_TRACE_PC
- if (getenv("AFL_CMPLOG"))
+ if (cmplog_mode) {
+
cc_params[cc_par_cnt++] = "-fsanitize-coverage=trace-pc-guard,trace-cmp";
- else
- cc_params[cc_par_cnt++] =
- "-fsanitize-coverage=trace-pc-guard"; // edge coverage by default
- // cc_params[cc_par_cnt++] = "-mllvm";
- // cc_params[cc_par_cnt++] =
- // "-fsanitize-coverage=trace-cmp,trace-div,trace-gep";
- // cc_params[cc_par_cnt++] = "-sanitizer-coverage-block-threshold=0";
+
+ } else {
+
+#ifdef USE_TRACE_PC
+
+ cc_params[cc_par_cnt++] =
+ "-fsanitize-coverage=trace-pc-guard"; // edge coverage by default
+ // cc_params[cc_par_cnt++] = "-mllvm";
+ // cc_params[cc_par_cnt++] =
+ // "-fsanitize-coverage=trace-cmp,trace-div,trace-gep";
+ // cc_params[cc_par_cnt++] = "-sanitizer-coverage-block-threshold=0";
#else
cc_params[cc_par_cnt++] = "-Xclang";
cc_params[cc_par_cnt++] = "-load";
@@ -216,6 +221,8 @@ static void edit_params(u32 argc, char** argv) {
cc_params[cc_par_cnt++] = alloc_printf("%s/afl-llvm-pass.so", obj_path);
#endif /* ^USE_TRACE_PC */
+ }
+
cc_params[cc_par_cnt++] = "-Qunused-arguments";
/* Detect stray -v calls from ./configure scripts. */
@@ -380,11 +387,17 @@ static void edit_params(u32 argc, char** argv) {
switch (bit_mode) {
case 0:
- cc_params[cc_par_cnt++] = alloc_printf("%s/afl-llvm-rt.o", obj_path);
+ if (cmplog_mode)
+ cc_params[cc_par_cnt++] = alloc_printf("%s/afl-llvm-cmplog-rt.o", obj_path);
+ else
+ cc_params[cc_par_cnt++] = alloc_printf("%s/afl-llvm-rt.o", obj_path);
break;
case 32:
- cc_params[cc_par_cnt++] = alloc_printf("%s/afl-llvm-rt-32.o", obj_path);
+ if (cmplog_mode)
+ cc_params[cc_par_cnt++] = alloc_printf("%s/afl-llvm-cmplog-rt-32.o", obj_path);
+ else
+ cc_params[cc_par_cnt++] = alloc_printf("%s/afl-llvm-rt-32.o", obj_path);
if (access(cc_params[cc_par_cnt - 1], R_OK))
FATAL("-m32 is not supported by your compiler");
@@ -392,7 +405,10 @@ static void edit_params(u32 argc, char** argv) {
break;
case 64:
- cc_params[cc_par_cnt++] = alloc_printf("%s/afl-llvm-rt-64.o", obj_path);
+ if (cmplog_mode)
+ cc_params[cc_par_cnt++] = alloc_printf("%s/afl-llvm-cmplog-rt-64.o", obj_path);
+ else
+ cc_params[cc_par_cnt++] = alloc_printf("%s/afl-llvm-rt-64.o", obj_path);
if (access(cc_params[cc_par_cnt - 1], R_OK))
FATAL("-m64 is not supported by your compiler");
@@ -463,6 +479,10 @@ int main(int argc, char** argv) {
#endif /* ^USE_TRACE_PC */
}
+
+ cmplog_mode = getenv("AFL_CMPLOG") || getenv("AFL_LLVM_CMPLOG");
+ if (cmplog_mode)
+ printf("CmpLog mode by <andreafioraldi@gmail.com>\n");
#ifndef __ANDROID__
find_obj(argv[0]);
diff --git a/llvm_mode/afl-llvm-cmplog-rt.o.c b/llvm_mode/afl-llvm-cmplog-rt.o.c
new file mode 100644
index 00000000..7a513c0d
--- /dev/null
+++ b/llvm_mode/afl-llvm-cmplog-rt.o.c
@@ -0,0 +1,412 @@
+/*
+ american fuzzy lop++ - LLVM instrumentation bootstrap
+ ---------------------------------------------------
+
+ Written by Laszlo Szekeres <lszekeres@google.com> and
+ Michal Zalewski
+
+ LLVM integration design comes from Laszlo Szekeres.
+
+ Copyright 2015, 2016 Google Inc. All rights reserved.
+ Copyright 2019-2020 AFLplusplus Project. All rights reserved.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at:
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ This code is the rewrite of afl-as.h's main_payload.
+
+*/
+
+#ifdef __ANDROID__
+#include "android-ashmem.h"
+#endif
+#include "config.h"
+#include "types.h"
+#include "cmplog.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <stdint.h>
+
+#include <sys/mman.h>
+#include <sys/shm.h>
+#include <sys/wait.h>
+#include <sys/types.h>
+
+/* This is a somewhat ugly hack for the experimental 'trace-pc-guard' mode.
+ Basically, we need to make sure that the forkserver is initialized after
+ the LLVM-generated runtime initialization pass, not before. */
+
+#ifdef USE_TRACE_PC
+#define CONST_PRIO 5
+#else
+#define CONST_PRIO 0
+#endif /* ^USE_TRACE_PC */
+
+#include <sys/mman.h>
+#include <fcntl.h>
+
+/* Globals needed by the injected instrumentation. The __afl_area_initial region
+ is used for instrumentation output before __afl_map_shm() has a chance to
+ run. It will end up as .comm, so it shouldn't be too wasteful. */
+
+// In CmpLog, the only usage of __afl_area_ptr is to report errors
+u8* __afl_area_ptr;
+
+struct cmp_map* __afl_cmp_map;
+__thread u32 __afl_cmp_counter;
+
+/* Running in persistent mode? */
+
+static u8 is_persistent;
+
+/* SHM setup. */
+
+static void __afl_map_shm(void) {
+
+ u8* id_str = getenv(SHM_ENV_VAR);
+
+ /* If we're running under AFL, attach to the appropriate region, replacing the
+ early-stage __afl_area_initial region that is needed to allow some really
+ hacky .init code to work correctly in projects such as OpenSSL. */
+
+ if (id_str) {
+
+#ifdef USEMMAP
+ const char* shm_file_path = id_str;
+ int shm_fd = -1;
+ unsigned char* shm_base = NULL;
+
+ /* create the shared memory segment as if it was a file */
+ shm_fd = shm_open(shm_file_path, O_RDWR, 0600);
+ if (shm_fd == -1) {
+
+ printf("shm_open() failed\n");
+ exit(1);
+
+ }
+
+ /* map the shared memory segment to the address space of the process */
+ shm_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
+ if (shm_base == MAP_FAILED) {
+
+ close(shm_fd);
+ shm_fd = -1;
+
+ printf("mmap() failed\n");
+ exit(2);
+
+ }
+
+ __afl_area_ptr = shm_base;
+#else
+ u32 shm_id = atoi(id_str);
+
+ __afl_area_ptr = shmat(shm_id, NULL, 0);
+#endif
+
+ /* Whooooops. */
+
+ if (__afl_area_ptr == (void*)-1) _exit(1);
+
+ /* Write something into the bitmap so that even with low AFL_INST_RATIO,
+ our parent doesn't give up on us. */
+
+ __afl_area_ptr[0] = 1;
+
+ }
+
+ id_str = getenv(CMPLOG_SHM_ENV_VAR);
+
+ if (id_str) {
+
+ u32 shm_id = atoi(id_str);
+
+ __afl_cmp_map = shmat(shm_id, NULL, 0);
+
+ if (__afl_cmp_map == (void*)-1) _exit(1);
+
+ }
+
+}
+
+/* Fork server logic. */
+
+static void __afl_start_forkserver(void) {
+
+ static u8 tmp[4];
+ s32 child_pid;
+
+ u8 child_stopped = 0;
+
+ void (*old_sigchld_handler)(int) = 0; // = signal(SIGCHLD, SIG_DFL);
+
+ /* Phone home and tell the parent that we're OK. If parent isn't there,
+ assume we're not running in forkserver mode and just execute program. */
+
+ if (write(FORKSRV_FD + 1, tmp, 4) != 4) return;
+
+ while (1) {
+
+ u32 was_killed;
+ int status;
+
+ /* Wait for parent by reading from the pipe. Abort if read fails. */
+
+ if (read(FORKSRV_FD, &was_killed, 4) != 4) _exit(1);
+
+ /* If we stopped the child in persistent mode, but there was a race
+ condition and afl-fuzz already issued SIGKILL, write off the old
+ process. */
+
+ if (child_stopped && was_killed) {
+
+ child_stopped = 0;
+ if (waitpid(child_pid, &status, 0) < 0) _exit(1);
+
+ }
+
+ if (!child_stopped) {
+
+ /* Once woken up, create a clone of our process. */
+
+ child_pid = fork();
+ if (child_pid < 0) _exit(1);
+
+ /* In child process: close fds, resume execution. */
+
+ if (!child_pid) {
+
+ signal(SIGCHLD, old_sigchld_handler);
+
+ close(FORKSRV_FD);
+ close(FORKSRV_FD + 1);
+ return;
+
+ }
+
+ } else {
+
+ /* Special handling for persistent mode: if the child is alive but
+ currently stopped, simply restart it with SIGCONT. */
+
+ kill(child_pid, SIGCONT);
+ child_stopped = 0;
+
+ }
+
+ /* In parent process: write PID to pipe, then wait for child. */
+
+ if (write(FORKSRV_FD + 1, &child_pid, 4) != 4) _exit(1);
+
+ if (waitpid(child_pid, &status, is_persistent ? WUNTRACED : 0) < 0)
+ _exit(1);
+
+ /* In persistent mode, the child stops itself with SIGSTOP to indicate
+ a successful run. In this case, we want to wake it up without forking
+ again. */
+
+ if (WIFSTOPPED(status)) child_stopped = 1;
+
+ /* Relay wait status to pipe, then loop back. */
+
+ if (write(FORKSRV_FD + 1, &status, 4) != 4) _exit(1);
+
+ }
+
+}
+
+/* A simplified persistent mode handler, used as explained in README.llvm. */
+
+int __afl_persistent_loop(unsigned int max_cnt) {
+
+ static u8 first_pass = 1;
+ static u32 cycle_cnt;
+
+ if (first_pass) {
+
+ /* Make sure that every iteration of __AFL_LOOP() starts with a clean slate.
+ On subsequent calls, the parent will take care of that, but on the first
+ iteration, it's our job to erase any trace of whatever happened
+ before the loop. */
+
+ if (is_persistent) {
+
+ // memset(__afl_area_ptr, 0, MAP_SIZE);
+ __afl_area_ptr[0] = 1;
+
+ }
+
+ cycle_cnt = max_cnt;
+ first_pass = 0;
+ return 1;
+
+ }
+
+ if (is_persistent) {
+
+ if (--cycle_cnt) {
+
+ raise(SIGSTOP);
+
+ __afl_area_ptr[0] = 1;
+
+ return 1;
+
+ } else {
+
+ /* When exiting __AFL_LOOP(), make sure that the subsequent code that
+ follows the loop is not traced. We do that by pivoting back to the
+ dummy output region. */
+
+ // __afl_area_ptr = __afl_area_initial;
+
+ }
+
+ }
+
+ return 0;
+
+}
+
+/* This one can be called from user code when deferred forkserver mode
+ is enabled. */
+
+void __afl_manual_init(void) {
+
+ static u8 init_done;
+
+ if (!init_done) {
+
+ __afl_map_shm();
+ __afl_start_forkserver();
+ init_done = 1;
+
+ }
+
+}
+
+/* Proper initialization routine. */
+
+__attribute__((constructor(CONST_PRIO))) void __afl_auto_init(void) {
+
+ is_persistent = !!getenv(PERSIST_ENV_VAR);
+
+ if (getenv(DEFER_ENV_VAR)) return;
+
+ __afl_manual_init();
+
+}
+
+///// CmpLog instrumentation
+
+void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
+
+ return;
+
+}
+
+void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
+
+ if (!__afl_cmp_map) return;
+
+ uintptr_t k = (uintptr_t)__builtin_return_address(0);
+ k = (k >> 4) ^ (k << 8);
+ k &= CMP_MAP_W - 1;
+
+ u32 hits = __afl_cmp_map->headers[k].hits;
+ __afl_cmp_map->headers[k].hits = hits + 1;
+ // if (!__afl_cmp_map->headers[k].cnt)
+ // __afl_cmp_map->headers[k].cnt = __afl_cmp_counter++;
+
+ __afl_cmp_map->headers[k].shape = 1;
+ //__afl_cmp_map->headers[k].type = CMP_TYPE_INS;
+
+ hits &= CMP_MAP_H - 1;
+ __afl_cmp_map->log[k][hits].v0 = Arg1;
+ __afl_cmp_map->log[k][hits].v1 = Arg2;
+
+}
+
+void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
+
+ if (!__afl_cmp_map) return;
+
+ uintptr_t k = (uintptr_t)__builtin_return_address(0);
+ k = (k >> 4) ^ (k << 8);
+ k &= CMP_MAP_W - 1;
+
+ u32 hits = __afl_cmp_map->headers[k].hits;
+ __afl_cmp_map->headers[k].hits = hits + 1;
+
+ __afl_cmp_map->headers[k].shape = 3;
+
+ hits &= CMP_MAP_H - 1;
+ __afl_cmp_map->log[k][hits].v0 = Arg1;
+ __afl_cmp_map->log[k][hits].v1 = Arg2;
+
+}
+
+void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) {
+
+ if (!__afl_cmp_map) return;
+
+ uintptr_t k = (uintptr_t)__builtin_return_address(0);
+ k = (k >> 4) ^ (k << 8);
+ k &= CMP_MAP_W - 1;
+
+ u32 hits = __afl_cmp_map->headers[k].hits;
+ __afl_cmp_map->headers[k].hits = hits + 1;
+
+ __afl_cmp_map->headers[k].shape = 7;
+
+ hits &= CMP_MAP_H - 1;
+ __afl_cmp_map->log[k][hits].v0 = Arg1;
+ __afl_cmp_map->log[k][hits].v1 = Arg2;
+
+}
+
+#if defined(__APPLE__)
+#pragma weak __sanitizer_cov_trace_const_cmp1 = __sanitizer_cov_trace_cmp1
+#pragma weak __sanitizer_cov_trace_const_cmp2 = __sanitizer_cov_trace_cmp2
+#pragma weak __sanitizer_cov_trace_const_cmp4 = __sanitizer_cov_trace_cmp4
+#pragma weak __sanitizer_cov_trace_const_cmp8 = __sanitizer_cov_trace_cmp8
+#else
+void __sanitizer_cov_trace_const_cmp1(uint8_t Arg1, uint8_t Arg2)
+ __attribute__((alias("__sanitizer_cov_trace_cmp1")));
+void __sanitizer_cov_trace_const_cmp2(uint16_t Arg1, uint16_t Arg2)
+ __attribute__((alias("__sanitizer_cov_trace_cmp2")));
+void __sanitizer_cov_trace_const_cmp4(uint32_t Arg1, uint32_t Arg2)
+ __attribute__((alias("__sanitizer_cov_trace_cmp4")));
+void __sanitizer_cov_trace_const_cmp8(uint64_t Arg1, uint64_t Arg2)
+ __attribute__((alias("__sanitizer_cov_trace_cmp8")));
+#endif /* defined(__APPLE__) */
+
+void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t* Cases) {
+
+ for (uint64_t i = 0; i < Cases[0]; i++) {
+
+ uintptr_t k = (uintptr_t)__builtin_return_address(0) + i;
+ k = (k >> 4) ^ (k << 8);
+ k &= CMP_MAP_W - 1;
+
+ u32 hits = __afl_cmp_map->headers[k].hits;
+ __afl_cmp_map->headers[k].hits = hits + 1;
+
+ __afl_cmp_map->headers[k].shape = 7;
+
+ hits &= CMP_MAP_H - 1;
+ __afl_cmp_map->log[k][hits].v0 = Val;
+ __afl_cmp_map->log[k][hits].v1 = Cases[i + 2];
+
+ }
+
+}
+
diff --git a/llvm_mode/afl-llvm-cmplog.rt.o.c b/llvm_mode/afl-llvm-cmplog.rt.o.c
new file mode 100644
index 00000000..ef944d4f
--- /dev/null
+++ b/llvm_mode/afl-llvm-cmplog.rt.o.c
@@ -0,0 +1,422 @@
+/*
+ american fuzzy lop++ - LLVM instrumentation bootstrap
+ ---------------------------------------------------
+
+ Written by Laszlo Szekeres <lszekeres@google.com> and
+ Michal Zalewski
+
+ LLVM integration design comes from Laszlo Szekeres.
+
+ Copyright 2015, 2016 Google Inc. All rights reserved.
+ Copyright 2019-2020 AFLplusplus Project. All rights reserved.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at:
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ This code is the rewrite of afl-as.h's main_payload.
+
+*/
+
+#ifdef __ANDROID__
+#include "android-ashmem.h"
+#endif
+#include "config.h"
+#include "types.h"
+#include "cmplog.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <stdint.h>
+
+#include <sys/mman.h>
+#include <sys/shm.h>
+#include <sys/wait.h>
+#include <sys/types.h>
+
+/* This is a somewhat ugly hack for the experimental 'trace-pc-guard' mode.
+ Basically, we need to make sure that the forkserver is initialized after
+ the LLVM-generated runtime initialization pass, not before. */
+
+#ifdef USE_TRACE_PC
+#define CONST_PRIO 5
+#else
+#define CONST_PRIO 0
+#endif /* ^USE_TRACE_PC */
+
+#include <sys/mman.h>
+#include <fcntl.h>
+
+/* Globals needed by the injected instrumentation. The __afl_area_initial region
+ is used for instrumentation output before __afl_map_shm() has a chance to
+ run. It will end up as .comm, so it shouldn't be too wasteful. */
+
+// In CmpLog, the only usage of __afl_area_ptr is to report errors
+u8* __afl_area_ptr;
+
+struct cmp_map* __afl_cmp_map;
+__thread u32 __afl_cmp_counter;
+
+/* Running in persistent mode? */
+
+static u8 is_persistent;
+
+/* SHM setup. */
+
+static void __afl_map_shm(void) {
+
+ u8* id_str = getenv(SHM_ENV_VAR);
+
+ /* If we're running under AFL, attach to the appropriate region, replacing the
+ early-stage __afl_area_initial region that is needed to allow some really
+ hacky .init code to work correctly in projects such as OpenSSL. */
+
+ if (id_str) {
+
+#ifdef USEMMAP
+ const char* shm_file_path = id_str;
+ int shm_fd = -1;
+ unsigned char* shm_base = NULL;
+
+ /* create the shared memory segment as if it was a file */
+ shm_fd = shm_open(shm_file_path, O_RDWR, 0600);
+ if (shm_fd == -1) {
+
+ printf("shm_open() failed\n");
+ exit(1);
+
+ }
+
+ /* map the shared memory segment to the address space of the process */
+ shm_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
+ if (shm_base == MAP_FAILED) {
+
+ close(shm_fd);
+ shm_fd = -1;
+
+ printf("mmap() failed\n");
+ exit(2);
+
+ }
+
+ __afl_area_ptr = shm_base;
+#else
+ u32 shm_id = atoi(id_str);
+
+ __afl_area_ptr = shmat(shm_id, NULL, 0);
+#endif
+
+ /* Whooooops. */
+
+ if (__afl_area_ptr == (void*)-1) _exit(1);
+
+ /* Write something into the bitmap so that even with low AFL_INST_RATIO,
+ our parent doesn't give up on us. */
+
+ __afl_area_ptr[0] = 1;
+
+ }
+
+ id_str = getenv(CMPLOG_SHM_ENV_VAR);
+
+ if (id_str) {
+
+ u32 shm_id = atoi(id_str);
+
+ __afl_cmp_map = shmat(shm_id, NULL, 0);
+
+ if (__afl_cmp_map == (void*)-1) _exit(1);
+
+ }
+
+}
+
+/* Fork server logic. */
+
+static void __afl_start_forkserver(void) {
+
+ static u8 tmp[4];
+ s32 child_pid;
+
+ u8 child_stopped = 0;
+
+ void (*old_sigchld_handler)(int) = 0; // = signal(SIGCHLD, SIG_DFL);
+
+ /* Phone home and tell the parent that we're OK. If parent isn't there,
+ assume we're not running in forkserver mode and just execute program. */
+
+ if (write(FORKSRV_FD + 1, tmp, 4) != 4) return;
+
+ while (1) {
+
+ u32 was_killed;
+ int status;
+
+ /* Wait for parent by reading from the pipe. Abort if read fails. */
+
+ if (read(FORKSRV_FD, &was_killed, 4) != 4) _exit(1);
+
+ /* If we stopped the child in persistent mode, but there was a race
+ condition and afl-fuzz already issued SIGKILL, write off the old
+ process. */
+
+ if (child_stopped && was_killed) {
+
+ child_stopped = 0;
+ if (waitpid(child_pid, &status, 0) < 0) _exit(1);
+
+ }
+
+ if (!child_stopped) {
+
+ /* Once woken up, create a clone of our process. */
+
+ child_pid = fork();
+ if (child_pid < 0) _exit(1);
+
+ /* In child process: close fds, resume execution. */
+
+ if (!child_pid) {
+
+ signal(SIGCHLD, old_sigchld_handler);
+
+ close(FORKSRV_FD);
+ close(FORKSRV_FD + 1);
+ return;
+
+ }
+
+ } else {
+
+ /* Special handling for persistent mode: if the child is alive but
+ currently stopped, simply restart it with SIGCONT. */
+
+ kill(child_pid, SIGCONT);
+ child_stopped = 0;
+
+ }
+
+ /* In parent process: write PID to pipe, then wait for child. */
+
+ if (write(FORKSRV_FD + 1, &child_pid, 4) != 4) _exit(1);
+
+ if (waitpid(child_pid, &status, is_persistent ? WUNTRACED : 0) < 0)
+ _exit(1);
+
+ /* In persistent mode, the child stops itself with SIGSTOP to indicate
+ a successful run. In this case, we want to wake it up without forking
+ again. */
+
+ if (WIFSTOPPED(status)) child_stopped = 1;
+
+ /* Relay wait status to pipe, then loop back. */
+
+ if (write(FORKSRV_FD + 1, &status, 4) != 4) _exit(1);
+
+ }
+
+}
+
+/* A simplified persistent mode handler, used as explained in README.llvm. */
+
+int __afl_persistent_loop(unsigned int max_cnt) {
+
+ static u8 first_pass = 1;
+ static u32 cycle_cnt;
+
+ if (first_pass) {
+
+ /* Make sure that every iteration of __AFL_LOOP() starts with a clean slate.
+ On subsequent calls, the parent will take care of that, but on the first
+ iteration, it's our job to erase any trace of whatever happened
+ before the loop. */
+
+ if (is_persistent) {
+
+ // memset(__afl_area_ptr, 0, MAP_SIZE);
+ __afl_area_ptr[0] = 1;
+
+ }
+
+ cycle_cnt = max_cnt;
+ first_pass = 0;
+ return 1;
+
+ }
+
+ if (is_persistent) {
+
+ if (--cycle_cnt) {
+
+ raise(SIGSTOP);
+
+ __afl_area_ptr[0] = 1;
+
+ return 1;
+
+ } else {
+
+ /* When exiting __AFL_LOOP(), make sure that the subsequent code that
+ follows the loop is not traced. We do that by pivoting back to the
+ dummy output region. */
+
+ // __afl_area_ptr = __afl_area_initial;
+
+ }
+
+ }
+
+ return 0;
+
+}
+
+/* This one can be called from user code when deferred forkserver mode
+ is enabled. */
+
+void __afl_manual_init(void) {
+
+ static u8 init_done;
+
+ if (!init_done) {
+
+ __afl_map_shm();
+ __afl_start_forkserver();
+ init_done = 1;
+
+ }
+
+}
+
+/* Proper initialization routine. */
+
+__attribute__((constructor(CONST_PRIO))) void __afl_auto_init(void) {
+
+ is_persistent = !!getenv(PERSIST_ENV_VAR);
+
+ if (getenv(DEFER_ENV_VAR)) return;
+
+ __afl_manual_init();
+
+}
+
+///// CmpLog instrumentation
+
+void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
+
+ return;
+
+}
+
+void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
+
+ if (!__afl_cmp_map) return;
+
+ uintptr_t k = (uintptr_t)__builtin_return_address(0);
+ k = (k >> 4) ^ (k << 8);
+ k &= CMP_MAP_W - 1;
+
+ u32 hits = __afl_cmp_map->headers[k].hits;
+ __afl_cmp_map->headers[k].hits = hits + 1;
+ // if (!__afl_cmp_map->headers[k].cnt)
+ // __afl_cmp_map->headers[k].cnt = __afl_cmp_counter++;
+
+ __afl_cmp_map->headers[k].shape = 1;
+ //__afl_cmp_map->headers[k].type = CMP_TYPE_INS;
+
+ hits &= CMP_MAP_H - 1;
+ __afl_cmp_map->log[k][hits].v0 = Arg1;
+ __afl_cmp_map->log[k][hits].v1 = Arg2;
+
+}
+
+void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
+
+ if (!__afl_cmp_map) return;
+
+ uintptr_t k = (uintptr_t)__builtin_return_address(0);
+ k = (k >> 4) ^ (k << 8);
+ k &= CMP_MAP_W - 1;
+
+ u32 hits = __afl_cmp_map->headers[k].hits;
+ __afl_cmp_map->headers[k].hits = hits + 1;
+
+ __afl_cmp_map->headers[k].shape = 3;
+
+ hits &= CMP_MAP_H - 1;
+ __afl_cmp_map->log[k][hits].v0 = Arg1;
+ __afl_cmp_map->log[k][hits].v1 = Arg2;
+
+}
+
+void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) {
+
+ if (!__afl_cmp_map) return;
+
+ uintptr_t k = (uintptr_t)__builtin_return_address(0);
+ k = (k >> 4) ^ (k << 8);
+ k &= CMP_MAP_W - 1;
+
+ u32 hits = __afl_cmp_map->headers[k].hits;
+ __afl_cmp_map->headers[k].hits = hits + 1;
+
+ __afl_cmp_map->headers[k].shape = 7;
+
+ hits &= CMP_MAP_H - 1;
+ __afl_cmp_map->log[k][hits].v0 = Arg1;
+ __afl_cmp_map->log[k][hits].v1 = Arg2;
+
+}
+
+#if defined(__APPLE__)
+#pragma weak __sanitizer_cov_trace_const_cmp1 = __sanitizer_cov_trace_cmp1
+#pragma weak __sanitizer_cov_trace_const_cmp2 = __sanitizer_cov_trace_cmp2
+#pragma weak __sanitizer_cov_trace_const_cmp4 = __sanitizer_cov_trace_cmp4
+#pragma weak __sanitizer_cov_trace_const_cmp8 = __sanitizer_cov_trace_cmp8
+#else
+void __sanitizer_cov_trace_const_cmp1(uint8_t Arg1, uint8_t Arg2)
+ __attribute__((alias("__sanitizer_cov_trace_cmp1")));
+void __sanitizer_cov_trace_const_cmp2(uint16_t Arg1, uint16_t Arg2)
+ __attribute__((alias("__sanitizer_cov_trace_cmp2")));
+void __sanitizer_cov_trace_const_cmp4(uint32_t Arg1, uint32_t Arg2)
+ __attribute__((alias("__sanitizer_cov_trace_cmp4")));
+void __sanitizer_cov_trace_const_cmp8(uint64_t Arg1, uint64_t Arg2)
+ __attribute__((alias("__sanitizer_cov_trace_cmp8")));
+#endif /* defined(__APPLE__) */
+
+void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t* Cases) {
+
+ for (uint64_t i = 0; i < Cases[0]; i++) {
+
+ uintptr_t k = (uintptr_t)__builtin_return_address(0) + i;
+ k = (k >> 4) ^ (k << 8);
+ k &= CMP_MAP_W - 1;
+
+ u32 hits = __afl_cmp_map->headers[k].hits;
+ __afl_cmp_map->headers[k].hits = hits + 1;
+
+ __afl_cmp_map->headers[k].shape = 7;
+
+ hits &= CMP_MAP_H - 1;
+ __afl_cmp_map->log[k][hits].v0 = Val;
+ __afl_cmp_map->log[k][hits].v1 = Cases[i + 2];
+
+ }
+
+}
+
+
+void __sanitizer_cov_trace_pc_guard(uint32_t* guard) {
+}
+
+void __sanitizer_cov_trace_pc_guard_init(uint32_t* start, uint32_t* stop) {
+}
+
+//// Library functions hooks
+
+// TODO
diff --git a/llvm_mode/afl-llvm-rt.o.c b/llvm_mode/afl-llvm-rt.o.c
index c3d1ba7d..9632844f 100644
--- a/llvm_mode/afl-llvm-rt.o.c
+++ b/llvm_mode/afl-llvm-rt.o.c
@@ -66,9 +66,6 @@ u32 __afl_prev_loc;
__thread u32 __afl_prev_loc;
#endif
-struct cmp_map* __afl_cmp_map;
-__thread u32 __afl_cmp_counter;
-
/* Running in persistent mode? */
static u8 is_persistent;
@@ -129,27 +126,6 @@ static void __afl_map_shm(void) {
}
- if (getenv("__AFL_CMPLOG_MODE__")) {
-
- id_str = getenv(CMPLOG_SHM_ENV_VAR);
-
- if (id_str) {
-
- u32 shm_id = atoi(id_str);
-
- __afl_cmp_map = shmat(shm_id, NULL, 0);
-
- if (__afl_cmp_map == (void*)-1) _exit(1);
-
- }
-
- } else if (getenv("AFL_CMPLOG")) {
-
- // during compilation, do this to avoid segfault
- __afl_cmp_map = calloc(sizeof(struct cmp_map), 1);
-
- }
-
}
/* Fork server logic. */
@@ -322,105 +298,6 @@ __attribute__((constructor(CONST_PRIO))) void __afl_auto_init(void) {
}
-///// CmpLog instrumentation
-
-void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
-
- return;
-
-}
-
-void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
-
- uintptr_t k = (uintptr_t)__builtin_return_address(0);
- k = (k >> 4) ^ (k << 8);
- k &= CMP_MAP_W - 1;
-
- u32 hits = __afl_cmp_map->headers[k].hits;
- __afl_cmp_map->headers[k].hits = hits + 1;
- // if (!__afl_cmp_map->headers[k].cnt)
- // __afl_cmp_map->headers[k].cnt = __afl_cmp_counter++;
-
- __afl_cmp_map->headers[k].shape = 1;
- //__afl_cmp_map->headers[k].type = CMP_TYPE_INS;
-
- hits &= CMP_MAP_H - 1;
- __afl_cmp_map->log[k][hits].v0 = Arg1;
- __afl_cmp_map->log[k][hits].v1 = Arg2;
-
-}
-
-void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
-
- uintptr_t k = (uintptr_t)__builtin_return_address(0);
- k = (k >> 4) ^ (k << 8);
- k &= CMP_MAP_W - 1;
-
- u32 hits = __afl_cmp_map->headers[k].hits;
- __afl_cmp_map->headers[k].hits = hits + 1;
-
- __afl_cmp_map->headers[k].shape = 3;
-
- hits &= CMP_MAP_H - 1;
- __afl_cmp_map->log[k][hits].v0 = Arg1;
- __afl_cmp_map->log[k][hits].v1 = Arg2;
-
-}
-
-void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) {
-
- uintptr_t k = (uintptr_t)__builtin_return_address(0);
- k = (k >> 4) ^ (k << 8);
- k &= CMP_MAP_W - 1;
-
- u32 hits = __afl_cmp_map->headers[k].hits;
- __afl_cmp_map->headers[k].hits = hits + 1;
-
- __afl_cmp_map->headers[k].shape = 7;
-
- hits &= CMP_MAP_H - 1;
- __afl_cmp_map->log[k][hits].v0 = Arg1;
- __afl_cmp_map->log[k][hits].v1 = Arg2;
-
-}
-
-#if defined(__APPLE__)
-#pragma weak __sanitizer_cov_trace_const_cmp1 = __sanitizer_cov_trace_cmp1
-#pragma weak __sanitizer_cov_trace_const_cmp2 = __sanitizer_cov_trace_cmp2
-#pragma weak __sanitizer_cov_trace_const_cmp4 = __sanitizer_cov_trace_cmp4
-#pragma weak __sanitizer_cov_trace_const_cmp8 = __sanitizer_cov_trace_cmp8
-#else
-void __sanitizer_cov_trace_const_cmp1(uint8_t Arg1, uint8_t Arg2)
- __attribute__((alias("__sanitizer_cov_trace_cmp1")));
-void __sanitizer_cov_trace_const_cmp2(uint16_t Arg1, uint16_t Arg2)
- __attribute__((alias("__sanitizer_cov_trace_cmp2")));
-void __sanitizer_cov_trace_const_cmp4(uint32_t Arg1, uint32_t Arg2)
- __attribute__((alias("__sanitizer_cov_trace_cmp4")));
-void __sanitizer_cov_trace_const_cmp8(uint64_t Arg1, uint64_t Arg2)
- __attribute__((alias("__sanitizer_cov_trace_cmp8")));
-#endif /* defined(__APPLE__) */
-
-void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t* Cases) {
-
- for (uint64_t i = 0; i < Cases[0]; i++) {
-
- uintptr_t k = (uintptr_t)__builtin_return_address(0) + i;
- k = (k >> 4) ^ (k << 8);
- k &= CMP_MAP_W - 1;
-
- u32 hits = __afl_cmp_map->headers[k].hits;
- __afl_cmp_map->headers[k].hits = hits + 1;
-
- __afl_cmp_map->headers[k].shape = 7;
-
- hits &= CMP_MAP_H - 1;
- __afl_cmp_map->log[k][hits].v0 = Val;
- __afl_cmp_map->log[k][hits].v1 = Cases[i + 2];
-
- }
-
-}
-
/* The following stuff deals with supporting -fsanitize-coverage=trace-pc-guard.
It remains non-operational in the traditional, plugin-backed LLVM mode.
For more info about 'trace-pc-guard', see README.llvm.