Mercurial > pub > dyncall > dyncall
annotate dyncallback/dyncall_alloc_wx_mmap.c @ 330:4e6f63b7020e
- stack layout typo for sparc doc
author | Tassilo Philipp |
---|---|
date | Fri, 22 Nov 2019 23:28:17 +0100 |
parents | f5577f6bf97a |
children |
rev | line source |
---|---|
0 | 1 /* |
2 | |
3 Package: dyncall | |
4 Library: dyncallback | |
5 File: dyncallback/dyncall_alloc_wx_mmap.c | |
6 Description: Allocate write/executable memory - Implementation for posix | |
7 License: | |
8 | |
281 | 9 Copyright (c) 2007-2018 Daniel Adler <dadler@uni-goettingen.de>, |
0 | 10 Tassilo Philipp <tphilipp@potion-studios.com> |
11 | |
12 Permission to use, copy, modify, and distribute this software for any | |
13 purpose with or without fee is hereby granted, provided that the above | |
14 copyright notice and this permission notice appear in all copies. | |
15 | |
16 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
17 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
18 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
19 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
21 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
22 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
23 | |
24 */ | |
25 | |
26 | |
27 #include "dyncall_alloc_wx.h" | |
28 | |
29 #include <sys/types.h> | |
30 #include <sys/mman.h> | |
31 | |
185
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
32 /* MAP_ANON is not POSIX, if lacking, we'll use a portable fallback on *nix systems */ |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
33 #if !defined(MAP_ANON) |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
34 # if defined(MAP_ANONYMOUS) |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
35 # define MAP_ANON MAP_ANONYMOUS |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
36 # elif defined(DC_UNIX) |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
37 # include <fcntl.h> |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
38 # else |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
39 # error "Platform implementation missing for anonymous rwx memory" |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
40 # endif |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
41 #endif |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
42 |
255
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
43 /* platforms without mprotect */ |
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
44 #if defined(DC__OS_Minix) |
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
45 # define NO_MPROTECT |
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
46 #endif |
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
47 |
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
48 /* if no mprotect() available/wanted, make mmap alloc pages as rwx */ |
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
49 #if defined(NO_MPROTECT) |
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
50 # define MMAP_PROT_INIT (PROT_READ|PROT_WRITE|PROT_EXEC) |
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
51 #else |
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
52 # define MMAP_PROT_INIT (PROT_READ|PROT_WRITE) |
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
53 #endif |
185
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
54 |
202 | 55 DCerror dcAllocWX(size_t size, void** pp) |
0 | 56 { |
185
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
57 void* p; |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
58 #if !defined(MAP_ANON) && defined(DC_UNIX) |
251
3f7c69fadfc3
- regression fix for changing writable to executable memory for platforms where
Tassilo Philipp
parents:
202
diff
changeset
|
59 /* Hack around not having POSIX' MAP_ANON by going through /dev/zero; store |
3f7c69fadfc3
- regression fix for changing writable to executable memory for platforms where
Tassilo Philipp
parents:
202
diff
changeset
|
60 file descr to close on dcFreeWX at beginning of memory, as tiny hack */ |
185
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
61 int fd = open("/dev/zero", O_RDWR); |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
62 if(fd == -1) |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
63 return -1; |
255
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
64 p = mmap(0, size+sizeof(int), MMAP_PROT_INIT, MAP_PRIVATE, fd, 0); |
185
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
65 if(p == MAP_FAILED) { |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
66 close(fd); |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
67 return -1; |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
68 } |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
69 *(int*)p = fd; |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
70 p += sizeof(int); |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
71 #else |
255
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
72 p = mmap(0, size, MMAP_PROT_INIT, MAP_PRIVATE|MAP_ANON, -1, 0); |
185
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
73 if(p == MAP_FAILED) |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
74 return -1; |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
75 #endif |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
76 |
0 | 77 *pp = p; |
78 return 0; | |
79 } | |
80 | |
202 | 81 DCerror dcInitExecWX(void* p, size_t size) |
82 { | |
255
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
83 #if defined(NO_MPROTECT) |
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
84 return 0; |
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
85 #else |
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
86 |
251
3f7c69fadfc3
- regression fix for changing writable to executable memory for platforms where
Tassilo Philipp
parents:
202
diff
changeset
|
87 #if !defined(MAP_ANON) && defined(DC_UNIX) |
3f7c69fadfc3
- regression fix for changing writable to executable memory for platforms where
Tassilo Philipp
parents:
202
diff
changeset
|
88 /* Fixup pointer for no-MAP_ANON workaround (see above) */ |
3f7c69fadfc3
- regression fix for changing writable to executable memory for platforms where
Tassilo Philipp
parents:
202
diff
changeset
|
89 p -= sizeof(int); |
252
047d2829bdf6
- see last commit - sloppy me forgot a part of that regression fix
Tassilo Philipp
parents:
251
diff
changeset
|
90 size += sizeof(int); |
251
3f7c69fadfc3
- regression fix for changing writable to executable memory for platforms where
Tassilo Philipp
parents:
202
diff
changeset
|
91 #endif |
202 | 92 return mprotect(p, size, PROT_READ|PROT_EXEC); |
255
704c28fd9428
- added "Unknown" type as executable format #define next to PE, Mach and ELF ones for cases like Minix < 3.2.0
Tassilo Philipp
parents:
252
diff
changeset
|
93 #endif |
202 | 94 } |
95 | |
0 | 96 void dcFreeWX(void* p, size_t size) |
97 { | |
185
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
98 #if !defined(MAP_ANON) && defined(DC_UNIX) |
251
3f7c69fadfc3
- regression fix for changing writable to executable memory for platforms where
Tassilo Philipp
parents:
202
diff
changeset
|
99 /* Close file descriptor for no-MAP_ANON workaround (see above) */ |
185
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
100 p -= sizeof(int); |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
101 size += sizeof(int); |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
102 close(*(int*)p); |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
103 #endif |
0 | 104 munmap(p, size); |
105 } | |
185
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
106 |