Mercurial > pub > dyncall > dyncall
comparison dyncallback/dyncall_alloc_wx_mmap.c @ 255:704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
- allow builds on platforms missing mprotect() (e.g. Minix)
author | Tassilo Philipp |
---|---|
date | Wed, 17 May 2017 02:39:41 +0200 |
parents | 047d2829bdf6 |
children | f5577f6bf97a |
comparison
equal
deleted
inserted
replaced
254:9d70178c1ded | 255:704c28fd9428 |
---|---|
38 # else | 38 # else |
39 # error "Platform implementation missing for anonymous rwx memory" | 39 # error "Platform implementation missing for anonymous rwx memory" |
40 # endif | 40 # endif |
41 #endif | 41 #endif |
42 | 42 |
43 /* platforms without mprotect */ | |
44 #if defined(DC__OS_Minix) | |
45 # define NO_MPROTECT | |
46 #endif | |
47 | |
48 /* if no mprotect() available/wanted, make mmap alloc pages as rwx */ | |
49 #if defined(NO_MPROTECT) | |
50 # define MMAP_PROT_INIT (PROT_READ|PROT_WRITE|PROT_EXEC) | |
51 #else | |
52 # define MMAP_PROT_INIT (PROT_READ|PROT_WRITE) | |
53 #endif | |
43 | 54 |
44 DCerror dcAllocWX(size_t size, void** pp) | 55 DCerror dcAllocWX(size_t size, void** pp) |
45 { | 56 { |
46 void* p; | 57 void* p; |
47 #if !defined(MAP_ANON) && defined(DC_UNIX) | 58 #if !defined(MAP_ANON) && defined(DC_UNIX) |
48 /* Hack around not having POSIX' MAP_ANON by going through /dev/zero; store | 59 /* Hack around not having POSIX' MAP_ANON by going through /dev/zero; store |
49 file descr to close on dcFreeWX at beginning of memory, as tiny hack */ | 60 file descr to close on dcFreeWX at beginning of memory, as tiny hack */ |
50 int fd = open("/dev/zero", O_RDWR); | 61 int fd = open("/dev/zero", O_RDWR); |
51 if(fd == -1) | 62 if(fd == -1) |
52 return -1; | 63 return -1; |
53 p = mmap(0, size+sizeof(int), PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); | 64 p = mmap(0, size+sizeof(int), MMAP_PROT_INIT, MAP_PRIVATE, fd, 0); |
54 if(p == MAP_FAILED) { | 65 if(p == MAP_FAILED) { |
55 close(fd); | 66 close(fd); |
56 return -1; | 67 return -1; |
57 } | 68 } |
58 *(int*)p = fd; | 69 *(int*)p = fd; |
59 p += sizeof(int); | 70 p += sizeof(int); |
60 #else | 71 #else |
61 p = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); | 72 p = mmap(0, size, MMAP_PROT_INIT, MAP_PRIVATE|MAP_ANON, -1, 0); |
62 if(p == MAP_FAILED) | 73 if(p == MAP_FAILED) |
63 return -1; | 74 return -1; |
64 #endif | 75 #endif |
65 | 76 |
66 *pp = p; | 77 *pp = p; |
67 return 0; | 78 return 0; |
68 } | 79 } |
69 | 80 |
70 DCerror dcInitExecWX(void* p, size_t size) | 81 DCerror dcInitExecWX(void* p, size_t size) |
71 { | 82 { |
83 #if defined(NO_MPROTECT) | |
84 return 0; | |
85 #else | |
86 | |
72 #if !defined(MAP_ANON) && defined(DC_UNIX) | 87 #if !defined(MAP_ANON) && defined(DC_UNIX) |
73 /* Fixup pointer for no-MAP_ANON workaround (see above) */ | 88 /* Fixup pointer for no-MAP_ANON workaround (see above) */ |
74 p -= sizeof(int); | 89 p -= sizeof(int); |
75 size += sizeof(int); | 90 size += sizeof(int); |
76 #endif | 91 #endif |
77 return mprotect(p, size, PROT_READ|PROT_EXEC); | 92 return mprotect(p, size, PROT_READ|PROT_EXEC); |
93 #endif | |
78 } | 94 } |
79 | 95 |
80 void dcFreeWX(void* p, size_t size) | 96 void dcFreeWX(void* p, size_t size) |
81 { | 97 { |
82 #if !defined(MAP_ANON) && defined(DC_UNIX) | 98 #if !defined(MAP_ANON) && defined(DC_UNIX) |