Mercurial > pub > dyncall > dyncall
comparison dyncallback/dyncall_alloc_wx_mmap.c @ 202:030fbb70aa1b
- changed allocwx code:
* to introduce a call allowing to switch the written page to be executable (e.g. using mprotect)
* changed mmap based code so page is always W^X
author | Tassilo Philipp |
---|---|
date | Mon, 20 Mar 2017 23:12:58 +0100 |
parents | a41bc98e101c |
children | 3f7c69fadfc3 |
comparison
equal
deleted
inserted
replaced
201:136409adbdd1 | 202:030fbb70aa1b |
---|---|
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 | 43 |
44 int dcAllocWX(size_t size, void** pp) | 44 DCerror dcAllocWX(size_t size, void** pp) |
45 { | 45 { |
46 void* p; | 46 void* p; |
47 #if !defined(MAP_ANON) && defined(DC_UNIX) | 47 #if !defined(MAP_ANON) && defined(DC_UNIX) |
48 // Hack around not having POSIX' MAP_ANON by going through /dev/zero; store | 48 // Hack around not having POSIX' MAP_ANON by going through /dev/zero; store |
49 // file descriptor to close on dcFreeWX at beginning of memory, as tiny hack | 49 // file descriptor to close on dcFreeWX at beginning of memory, as tiny hack |
50 int fd = open("/dev/zero", O_RDWR); | 50 int fd = open("/dev/zero", O_RDWR); |
51 if(fd == -1) | 51 if(fd == -1) |
52 return -1; | 52 return -1; |
53 p = mmap(0, size+sizeof(int), PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); | 53 p = mmap(0, size+sizeof(int), PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
54 if(p == MAP_FAILED) { | 54 if(p == MAP_FAILED) { |
55 close(fd); | 55 close(fd); |
56 return -1; | 56 return -1; |
57 } | 57 } |
58 *(int*)p = fd; | 58 *(int*)p = fd; |
59 p += sizeof(int); | 59 p += sizeof(int); |
60 #else | 60 #else |
61 p = mmap(0, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); | 61 p = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); |
62 if(p == MAP_FAILED) | 62 if(p == MAP_FAILED) |
63 return -1; | 63 return -1; |
64 #endif | 64 #endif |
65 | 65 |
66 *pp = p; | 66 *pp = p; |
67 return 0; | 67 return 0; |
68 } | |
69 | |
70 DCerror dcInitExecWX(void* p, size_t size) | |
71 { | |
72 return mprotect(p, size, PROT_READ|PROT_EXEC); | |
68 } | 73 } |
69 | 74 |
70 void dcFreeWX(void* p, size_t size) | 75 void dcFreeWX(void* p, size_t size) |
71 { | 76 { |
72 #if !defined(MAP_ANON) && defined(DC_UNIX) | 77 #if !defined(MAP_ANON) && defined(DC_UNIX) |