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