Skip to content

Commit e75894a

Browse files
JoeyJiaojonathanmetzman
authored andcommitted
Add Android Support (aflnet#46)
1 parent d80c830 commit e75894a

11 files changed

+115
-20
lines changed

afl-analyze.c

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*/
2929

3030
#define AFL_MAIN
31+
#include "android-ashmem.h"
3132

3233
#include "config.h"
3334
#include "types.h"

afl-as.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static u32 inst_ratio = 100, /* Instrumentation probability (%) */
7474
instrumentation for whichever mode we were compiled with. This is not
7575
perfect, but should do the trick for almost all use cases. */
7676

77-
#ifdef __x86_64__
77+
#ifdef WORD_SIZE_64
7878

7979
static u8 use_64bit = 1;
8080

@@ -86,7 +86,7 @@ static u8 use_64bit = 0;
8686
# error "Sorry, 32-bit Apple platforms are not supported."
8787
#endif /* __APPLE__ */
8888

89-
#endif /* ^__x86_64__ */
89+
#endif /* ^WORD_SIZE_64 */
9090

9191

9292
/* Examine and modify parameters to pass to 'as'. Note that the file name

afl-fuzz.c

+16-14
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030

3131
#define AFL_MAIN
32+
#include "android-ashmem.h"
3233
#define MESSAGES_TO_STDOUT
3334

3435
#ifndef _GNU_SOURCE
@@ -888,7 +889,7 @@ EXP_ST void read_bitmap(u8* fname) {
888889

889890
static inline u8 has_new_bits(u8* virgin_map) {
890891

891-
#ifdef __x86_64__
892+
#ifdef WORD_SIZE_64
892893

893894
u64* current = (u64*)trace_bits;
894895
u64* virgin = (u64*)virgin_map;
@@ -902,7 +903,7 @@ static inline u8 has_new_bits(u8* virgin_map) {
902903

903904
u32 i = (MAP_SIZE >> 2);
904905

905-
#endif /* ^__x86_64__ */
906+
#endif /* ^WORD_SIZE_64 */
906907

907908
u8 ret = 0;
908909

@@ -922,7 +923,7 @@ static inline u8 has_new_bits(u8* virgin_map) {
922923
/* Looks like we have not found any new bytes yet; see if any non-zero
923924
bytes in current[] are pristine in virgin[]. */
924925

925-
#ifdef __x86_64__
926+
#ifdef WORD_SIZE_64
926927

927928
if ((cur[0] && vir[0] == 0xff) || (cur[1] && vir[1] == 0xff) ||
928929
(cur[2] && vir[2] == 0xff) || (cur[3] && vir[3] == 0xff) ||
@@ -936,7 +937,7 @@ static inline u8 has_new_bits(u8* virgin_map) {
936937
(cur[2] && vir[2] == 0xff) || (cur[3] && vir[3] == 0xff)) ret = 2;
937938
else ret = 1;
938939

939-
#endif /* ^__x86_64__ */
940+
#endif /* ^WORD_SIZE_64 */
940941

941942
}
942943

@@ -1058,7 +1059,7 @@ static const u8 simplify_lookup[256] = {
10581059

10591060
};
10601061

1061-
#ifdef __x86_64__
1062+
#ifdef WORD_SIZE_64
10621063

10631064
static void simplify_trace(u64* mem) {
10641065

@@ -1115,7 +1116,7 @@ static void simplify_trace(u32* mem) {
11151116

11161117
}
11171118

1118-
#endif /* ^__x86_64__ */
1119+
#endif /* ^WORD_SIZE_64 */
11191120

11201121

11211122
/* Destructively classify execution counts in a trace. This is used as a
@@ -1152,7 +1153,7 @@ EXP_ST void init_count_class16(void) {
11521153
}
11531154

11541155

1155-
#ifdef __x86_64__
1156+
#ifdef WORD_SIZE_64
11561157

11571158
static inline void classify_counts(u64* mem) {
11581159

@@ -1204,7 +1205,7 @@ static inline void classify_counts(u32* mem) {
12041205

12051206
}
12061207

1207-
#endif /* ^__x86_64__ */
1208+
#endif /* ^WORD_SIZE_64 */
12081209

12091210

12101211
/* Get rid of shared memory (atexit handler). */
@@ -2441,11 +2442,11 @@ static u8 run_target(char** argv, u32 timeout) {
24412442

24422443
tb4 = *(u32*)trace_bits;
24432444

2444-
#ifdef __x86_64__
2445+
#ifdef WORD_SIZE_64
24452446
classify_counts((u64*)trace_bits);
24462447
#else
24472448
classify_counts((u32*)trace_bits);
2448-
#endif /* ^__x86_64__ */
2449+
#endif /* ^WORD_SIZE_64 */
24492450

24502451
prev_timed_out = child_timed_out;
24512452

@@ -3205,11 +3206,11 @@ static u8 save_if_interesting(char** argv, void* mem, u32 len, u8 fault) {
32053206

32063207
if (!dumb_mode) {
32073208

3208-
#ifdef __x86_64__
3209+
#ifdef WORD_SIZE_64
32093210
simplify_trace((u64*)trace_bits);
32103211
#else
32113212
simplify_trace((u32*)trace_bits);
3212-
#endif /* ^__x86_64__ */
3213+
#endif /* ^WORD_SIZE_64 */
32133214

32143215
if (!has_new_bits(virgin_tmout)) return keeping;
32153216

@@ -3269,11 +3270,11 @@ static u8 save_if_interesting(char** argv, void* mem, u32 len, u8 fault) {
32693270

32703271
if (!dumb_mode) {
32713272

3272-
#ifdef __x86_64__
3273+
#ifdef WORD_SIZE_64
32733274
simplify_trace((u64*)trace_bits);
32743275
#else
32753276
simplify_trace((u32*)trace_bits);
3276-
#endif /* ^__x86_64__ */
3277+
#endif /* ^WORD_SIZE_64 */
32773278

32783279
if (!has_new_bits(virgin_crash)) return keeping;
32793280

@@ -7071,6 +7072,7 @@ static void check_term_size(void) {
70717072

70727073
if (ioctl(1, TIOCGWINSZ, &ws)) return;
70737074

7075+
if (ws.ws_row == 0 && ws.ws_col == 0) return;
70747076
if (ws.ws_row < 25 || ws.ws_col < 80) term_too_small = 1;
70757077

70767078
}

afl-gotcpu.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
*/
3434

3535
#define AFL_MAIN
36+
#include "android-ashmem.h"
3637
#define _GNU_SOURCE
3738

3839
#include <stdio.h>
@@ -150,7 +151,7 @@ int main(int argc, char** argv) {
150151
CPU_SET(i, &c);
151152

152153
if (sched_setaffinity(0, sizeof(c), &c))
153-
PFATAL("sched_setaffinity failed");
154+
PFATAL("sched_setaffinity failed for cpu %d", i);
154155

155156
util_perc = measure_preemption(CTEST_CORE_TRG_MS);
156157

afl-showmap.c

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030

3131
#define AFL_MAIN
32+
#include "android-ashmem.h"
3233

3334
#include "config.h"
3435
#include "types.h"

afl-tmin.c

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828

2929
#define AFL_MAIN
30+
#include "android-ashmem.h"
3031

3132
#include "config.h"
3233
#include "types.h"

afl-whatsup

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fi
5454

5555
CUR_TIME=`date +%s`
5656

57-
TMP=`mktemp -t .afl-whatsup-XXXXXXXX` || exit 1
57+
TMP=`mktemp -t .afl-whatsup-XXXXXXXX` || TMP=`mktemp -p /data/local/tmp .afl-whatsup-XXXXXXXX` || exit 1
5858

5959
ALIVE_CNT=0
6060
DEAD_CNT=0

android-ashmem.h

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#ifdef __ANDROID__
2+
#ifndef _ANDROID_ASHMEM_H
3+
#define _ANDROID_ASHMEM_H
4+
5+
#include <fcntl.h>
6+
#include <linux/ashmem.h>
7+
#include <linux/shm.h>
8+
#include <sys/ioctl.h>
9+
#include <sys/mman.h>
10+
11+
#if __ANDROID_API__ >= 26
12+
#define shmat bionic_shmat
13+
#define shmctl bionic_shmctl
14+
#define shmdt bionic_shmdt
15+
#define shmget bionic_shmget
16+
#endif
17+
#include <sys/shm.h>
18+
#undef shmat
19+
#undef shmctl
20+
#undef shmdt
21+
#undef shmget
22+
#include <stdio.h>
23+
24+
#define ASHMEM_DEVICE "/dev/ashmem"
25+
26+
static inline int shmctl(int __shmid, int __cmd, struct shmid_ds *__buf) {
27+
int ret = 0;
28+
if (__cmd == IPC_RMID) {
29+
int length = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
30+
struct ashmem_pin pin = {0, length};
31+
ret = ioctl(__shmid, ASHMEM_UNPIN, &pin);
32+
close(__shmid);
33+
}
34+
35+
return ret;
36+
}
37+
38+
static inline int shmget(key_t __key, size_t __size, int __shmflg) {
39+
(void) __shmflg;
40+
int fd, ret;
41+
char ourkey[11];
42+
43+
fd = open(ASHMEM_DEVICE, O_RDWR);
44+
if (fd < 0)
45+
return fd;
46+
47+
sprintf(ourkey, "%d", __key);
48+
ret = ioctl(fd, ASHMEM_SET_NAME, ourkey);
49+
if (ret < 0)
50+
goto error;
51+
52+
ret = ioctl(fd, ASHMEM_SET_SIZE, __size);
53+
if (ret < 0)
54+
goto error;
55+
56+
return fd;
57+
58+
error:
59+
close(fd);
60+
return ret;
61+
}
62+
63+
static inline void *shmat(int __shmid, const void *__shmaddr, int __shmflg) {
64+
(void) __shmflg;
65+
int size;
66+
void *ptr;
67+
68+
size = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
69+
if (size < 0) {
70+
return NULL;
71+
}
72+
73+
ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, __shmid, 0);
74+
if (ptr == MAP_FAILED) {
75+
return NULL;
76+
}
77+
78+
return ptr;
79+
}
80+
81+
#endif /* !_ANDROID_ASHMEM_H */
82+
#endif /* !__ANDROID__ */

config.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,18 @@
5454

5555
#define EXEC_TM_ROUND 20
5656

57+
/* 64bit arch MACRO */
58+
#if (defined (__x86_64__) || defined (__arm64__) || defined (__aarch64__))
59+
#define WORD_SIZE_64 1
60+
#endif
61+
5762
/* Default memory limit for child process (MB): */
5863

59-
#ifndef __x86_64__
64+
#ifndef WORD_SIZE_64
6065
# define MEM_LIMIT 25
6166
#else
6267
# define MEM_LIMIT 50
63-
#endif /* ^!__x86_64__ */
68+
#endif /* ^!WORD_SIZE_64 */
6469

6570
/* Default memory limit when running in QEMU mode (MB): */
6671

llvm_mode/afl-clang-fast.c

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ static void edit_params(u32 argc, char** argv) {
147147
u8* cur = *(++argv);
148148

149149
if (!strcmp(cur, "-m32")) bit_mode = 32;
150+
if (!strcmp(cur, "armv7a-linux-androideabi")) bit_mode = 32;
150151
if (!strcmp(cur, "-m64")) bit_mode = 64;
151152

152153
if (!strcmp(cur, "-x")) x_set = 1;

llvm_mode/afl-llvm-rt.o.c

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
This code is the rewrite of afl-as.h's main_payload.
2727
*/
2828

29+
#include "../android-ashmem.h"
2930
#include "../config.h"
3031
#include "../types.h"
3132

0 commit comments

Comments
 (0)