Skip to content

Commit 8a18ee4

Browse files
authored
Add Android Ashmem stub header to libafl_targets forkserver.c (#1648)
* Add Android Ashmem stub header to libafl_targets forkserver.c * clang-format
1 parent 1aede04 commit 8a18ee4

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

libafl_targets/src/android-ashmem.h

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/// On Android, this file redirects all (unsupported) shared memory calls out to
2+
/// Android's Ashmem. Source:
3+
/// <https://github.com/AFLplusplus/AFLplusplus/blob/stable/include/android-ashmem.h>
4+
#ifdef __ANDROID__
5+
#ifndef _ANDROID_ASHMEM_H
6+
#define _ANDROID_ASHMEM_H
7+
8+
// Not supported on Android.
9+
#undef USEMMAP
10+
11+
#ifndef _GNU_SOURCE
12+
#define _GNU_SOURCE
13+
#endif
14+
#include <sys/syscall.h>
15+
#include <unistd.h>
16+
#include <fcntl.h>
17+
#include <linux/ashmem.h>
18+
#include <sys/ioctl.h>
19+
#include <sys/mman.h>
20+
#include <sys/shm.h>
21+
#include <stdio.h>
22+
#define ASHMEM_DEVICE "/dev/ashmem"
23+
24+
int shmdt(const void *address) {
25+
#if defined(SYS_shmdt)
26+
return syscall(SYS_shmdt, address);
27+
#else
28+
return syscall(SYS_ipc, SHMDT, 0, 0, 0, address, 0);
29+
#endif
30+
}
31+
32+
int shmctl(int __shmid, int __cmd, struct shmid_ds *__buf) {
33+
int ret = 0;
34+
if (__cmd == IPC_RMID) {
35+
int length = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
36+
struct ashmem_pin pin = {0, length};
37+
ret = ioctl(__shmid, ASHMEM_UNPIN, &pin);
38+
close(__shmid);
39+
}
40+
41+
return ret;
42+
}
43+
44+
int shmget(key_t __key, size_t __size, int __shmflg) {
45+
(void)__shmflg;
46+
int fd, ret;
47+
char ourkey[11];
48+
49+
fd = open(ASHMEM_DEVICE, O_RDWR);
50+
if (fd < 0) return fd;
51+
52+
sprintf(ourkey, "%d", __key);
53+
ret = ioctl(fd, ASHMEM_SET_NAME, ourkey);
54+
if (ret < 0) goto error;
55+
56+
ret = ioctl(fd, ASHMEM_SET_SIZE, __size);
57+
if (ret < 0) goto error;
58+
59+
return fd;
60+
61+
error:
62+
close(fd);
63+
return ret;
64+
}
65+
66+
void *shmat(int __shmid, const void *__shmaddr, int __shmflg) {
67+
(void)__shmflg;
68+
int size;
69+
void *ptr;
70+
71+
size = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
72+
if (size < 0) { return NULL; }
73+
74+
ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, __shmid, 0);
75+
if (ptr == MAP_FAILED) { return NULL; }
76+
77+
return ptr;
78+
}
79+
80+
#endif /* !_ANDROID_ASHMEM_H */
81+
#endif /* !__ANDROID__ */

libafl_targets/src/forkserver.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "common.h"
22

3+
#include "android-ashmem.h"
34
#include <stdio.h>
45
#include <stdlib.h>
56
#include <string.h>

0 commit comments

Comments
 (0)