about summary refs log tree commit diff
path: root/src/afl-cc.c
diff options
context:
space:
mode:
authorSonic <50692172+SonicStark@users.noreply.github.com>2024-04-17 16:34:14 +0700
committerArnaud Rebillout <arnaudr@kali.org>2024-04-17 16:41:03 +0700
commit50839cf6e92c5f518ee2045452e7ff7a522c4d6f (patch)
tree7331d05dfb93572050dbe0ce1d7a175c35074ec9 /src/afl-cc.c
parent626a4434edc5c0cb381779f7e13d1b54c1ed1738 (diff)
downloadafl++-50839cf6e92c5f518ee2045452e7ff7a522c4d6f.tar.gz
afl-cc: Complete fix for afl-as
Look for afl-as, and then make sure that there's a 'as' binary in the
same directory, that seems to be either a symlink to, or a copy of,
afl-as.
Diffstat (limited to 'src/afl-cc.c')
-rw-r--r--src/afl-cc.c55
1 files changed, 54 insertions, 1 deletions
diff --git a/src/afl-cc.c b/src/afl-cc.c
index 15e0fcc7..dd4fb4ea 100644
--- a/src/afl-cc.c
+++ b/src/afl-cc.c
@@ -17,6 +17,10 @@
 
 #define AFL_MAIN
 
+#ifndef _GNU_SOURCE
+  #define _GNU_SOURCE 1
+#endif
+
 #include "common.h"
 #include "config.h"
 #include "types.h"
@@ -32,7 +36,9 @@
 #include <limits.h>
 #include <assert.h>
 #include <ctype.h>
+#include <fcntl.h>
 #include <sys/stat.h>
+#include <sys/mman.h>
 
 #if (LLVM_MAJOR - 0 == 0)
   #undef LLVM_MAJOR
@@ -520,7 +526,7 @@ void find_built_deps(aflcc_state_t *aflcc) {
   char *ptr = NULL;
 
 #if defined(__x86_64__)
-  if ((ptr = find_object(aflcc, "as")) != NULL) {
+  if ((ptr = find_object(aflcc, "afl-as")) != NULL) {
 
   #ifndef __APPLE__
     // on OSX clang masquerades as GCC
@@ -2482,6 +2488,53 @@ void add_assembler(aflcc_state_t *aflcc) {
   u8 *slash = strrchr(afl_as, '/');
   if (slash) *slash = 0;
 
+  // Search for 'as' may be unreliable in some cases (see #2058)
+  // so use 'afl-as' instead, because 'as' is usually a symbolic link,
+  // or can be a renamed copy of 'afl-as' created in the same dir.
+  // Now we should verify if the compiler can find the 'as' we need.
+
+#define AFL_AS_ERR "(should be a symlink or copy of 'afl-as')"
+
+  u8 *afl_as_dup = alloc_printf("%s/as", afl_as);
+
+  int fd = open(afl_as_dup, O_RDONLY);
+  if (fd < 0) { PFATAL("Unable to open '%s' " AFL_AS_ERR, afl_as_dup); }
+
+  struct stat st;
+  if (fstat(fd, &st) < 0) {
+
+    PFATAL("Unable to fstat '%s' " AFL_AS_ERR, afl_as_dup);
+
+  }
+
+  u32 f_len = st.st_size;
+
+  u8 *f_data = mmap(0, f_len, PROT_READ, MAP_PRIVATE, fd, 0);
+  if (f_data == MAP_FAILED) {
+
+    PFATAL("Unable to mmap file '%s' " AFL_AS_ERR, afl_as_dup);
+
+  }
+
+  close(fd);
+
+  // "AFL_AS" is a const str passed to getenv in afl-as.c
+  if (!memmem(f_data, f_len, "AFL_AS", strlen("AFL_AS") + 1)) {
+
+    FATAL(
+        "Looks like '%s' is not a valid symlink or copy of '%s/afl-as'. "
+        "It is a prerequisite to override system-wide 'as' for "
+        "instrumentation.",
+        afl_as_dup, afl_as);
+
+  }
+
+  if (munmap(f_data, f_len)) { PFATAL("unmap() failed"); }
+
+  ck_free(afl_as_dup);
+
+#undef AFL_AS_ERR
+
   insert_param(aflcc, "-B");
   insert_param(aflcc, afl_as);