Add support for Android
MODIFY
afl-analyze.c
afl-analyze.c
#define AFL_MAIN
+#ifdef __ANDROID__
+ #include "android-ashmem.h"
+#endif
#include "config.h"
#include "types.h"
#include "debug.h"

MODIFY
afl-fuzz.c
afl-fuzz.c
#endif
#define _FILE_OFFSET_BITS 64
+#ifdef __ANDROID__
+ #include "android-ashmem.h"
+#endif
#include "config.h"
#include "types.h"
#include "debug.h"
@@ -11318,6 +11321,7 @@ static void check_term_size(void) {
if (ioctl(1, TIOCGWINSZ, &ws)) return;
+ if (ws.ws_row == 0 || ws.ws_col == 0) return;
if (ws.ws_row < 24 || ws.ws_col < 79) term_too_small = 1;
}
@@ -12371,7 +12375,7 @@ int main(int argc, char** argv) {
}
- if (index(argv[optind], '/') == NULL) WARNF(cLRD "Target binary called without a prefixed path, make sure you are fuzzing the right binary: " cRST "%s", argv[optind]);
+ if (strchr(argv[optind], '/') == NULL) WARNF(cLRD "Target binary called without a prefixed path, make sure you are fuzzing the right binary: " cRST "%s", argv[optind]);
OKF("afl++ is maintained by Marc \"van Hauser\" Heuse, Heiko \"hexcoder\" Eissfeldt and Andrea Fioraldi");
OKF("afl++ is open source, get it at https://github.com/vanhauser-thc/AFLplusplus");

MODIFY
afl-gotcpu.c
afl-gotcpu.c
#define AFL_MAIN
#define _GNU_SOURCE
+#ifdef __ANDROID__
+ #include "android-ashmem.h"
+#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

MODIFY
afl-showmap.c
afl-showmap.c
#define AFL_MAIN
+#ifdef __ANDROID__
+ #include "android-ashmem.h"
+#endif
#include "config.h"
#include "types.h"
#include "debug.h"

MODIFY
afl-tmin.c
afl-tmin.c
#define AFL_MAIN
+#ifdef __ANDROID__
+ #include "android-ashmem.h"
+#endif
#include "config.h"
#include "types.h"
#include "debug.h"

MODIFY
afl-whatsup
afl-whatsup
@@ -54,7 +54,7 @@ fi
CUR_TIME=`date +%s`
-TMP=`mktemp -t .afl-whatsup-XXXXXXXX` || exit 1
+TMP=`mktemp -t .afl-whatsup-XXXXXXXX` || TMP=`mktemp -p /data/local/tmp .afl-whatsup-XXXXXXXX` || exit 1
ALIVE_CNT=0
DEAD_CNT=0

ADD
android-ashmem.h
+#ifndef _ANDROID_ASHMEM_H
+#define _ANDROID_ASHMEM_H
+
+#include <fcntl.h>
+#include <linux/shm.h>
+#include <linux/ashmem.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+
+#if __ANDROID_API__ >= 26
+#define shmat bionic_shmat
+#define shmctl bionic_shmctl
+#define shmdt bionic_shmdt
+#define shmget bionic_shmget
+#endif
+ #include <sys/shm.h>
+#undef shmat
+#undef shmctl
+#undef shmdt
+#undef shmget
+#include <stdio.h>
+
+#define ASHMEM_DEVICE "/dev/ashmem"
+
+static inline int shmctl(int __shmid, int __cmd, struct shmid_ds *__buf)
+{
+ int ret = 0;
+ if (__cmd == IPC_RMID) {
+ int length = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
+ struct ashmem_pin pin = {0, length};
+ ret = ioctl(__shmid, ASHMEM_UNPIN, &pin);
+ close(__shmid);
+ }
+
+ return ret;
+}
+
+static inline int shmget (key_t __key, size_t __size, int __shmflg)
+{
+ int fd,ret;
+ char ourkey[11];
+
+ fd = open(ASHMEM_DEVICE, O_RDWR);
+ if (fd < 0)
+ return fd;
+
+ sprintf(ourkey,"%d",__key);
+ ret = ioctl(fd, ASHMEM_SET_NAME, ourkey);
+ if (ret < 0)
+ goto error;
+
+ ret = ioctl(fd, ASHMEM_SET_SIZE, __size);
+ if (ret < 0)
+ goto error;
+
+ return fd;
+
+error:
+ close(fd);
+ return ret;
+}
+
+static inline void *shmat (int __shmid, const void *__shmaddr, int __shmflg)
+{
+ int size;
+ void *ptr;
+
+ size = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
+ if (size < 0) {
+ return NULL;
+ }
+
+ ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, __shmid, 0);
+ if (ptr == MAP_FAILED) {
+ return NULL;
+ }
+
+ return ptr;
+}
+
+#endif

MODIFY
llvm_mode/afl-llvm-pass.so.cc
llvm_mode/afl-llvm-pass.so.cc
@@ -129,9 +129,14 @@ bool AFLCoverage::runOnModule(Module &M) {
new GlobalVariable(M, PointerType::get(Int8Ty, 0), false,
GlobalValue::ExternalLinkage, 0, "__afl_area_ptr");
+#ifdef __ANDROID__
+ GlobalVariable *AFLPrevLoc = new GlobalVariable(
+ M, Int32Ty, false, GlobalValue::ExternalLinkage, 0, "__afl_prev_loc");
+#else
GlobalVariable *AFLPrevLoc = new GlobalVariable(
M, Int32Ty, false, GlobalValue::ExternalLinkage, 0, "__afl_prev_loc",
0, GlobalVariable::GeneralDynamicTLSModel, 0, false);
+#endif
/* Instrument all the things! */

MODIFY
llvm_mode/afl-llvm-rt.o.c
llvm_mode/afl-llvm-rt.o.c
*/
+#ifdef __ANDROID__
+ #include "android-ashmem.h"
+#endif
#include "../config.h"
#include "../types.h"
u8 __afl_area_initial[MAP_SIZE];
u8* __afl_area_ptr = __afl_area_initial;
+#ifdef __ANDROID__
+u32 __afl_prev_loc;
+#else
__thread u32 __afl_prev_loc;
+#endif
/* Running in persistent mode? */

MODIFY
sharedmem.c
sharedmem.c
#define AFL_MAIN
+#ifdef __ANDROID__
+ #include "android-ashmem.h"
+#endif
#include "config.h"
#include "types.h"
#include "debug.h"

Add support for Android