Mercurial > pub > dyncall > dyncall
annotate dyncallback/dyncall_alloc_wx_mmap.c @ 252:047d2829bdf6
- see last commit - sloppy me forgot a part of that regression fix
author | Tassilo Philipp |
---|---|
date | Sun, 14 May 2017 02:29:55 +0200 |
parents | 3f7c69fadfc3 |
children | 704c28fd9428 |
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 | |
185
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
9 Copyright (c) 2007-2017 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 |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
43 |
202 | 44 DCerror dcAllocWX(size_t size, void** pp) |
0 | 45 { |
185
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
46 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
|
47 #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
|
48 /* 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
|
49 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
|
50 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
|
51 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
|
52 return -1; |
202 | 53 p = mmap(0, size+sizeof(int), PROT_READ|PROT_WRITE, 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
|
54 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
|
55 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
|
56 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
|
57 } |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
58 *(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
|
59 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
|
60 #else |
202 | 61 p = mmap(0, size, PROT_READ|PROT_WRITE, 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
|
62 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
|
63 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
|
64 #endif |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
65 |
0 | 66 *pp = p; |
67 return 0; | |
68 } | |
69 | |
202 | 70 DCerror dcInitExecWX(void* p, size_t size) |
71 { | |
251
3f7c69fadfc3
- regression fix for changing writable to executable memory for platforms where
Tassilo Philipp
parents:
202
diff
changeset
|
72 #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
|
73 /* 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
|
74 p -= sizeof(int); |
252
047d2829bdf6
- see last commit - sloppy me forgot a part of that regression fix
Tassilo Philipp
parents:
251
diff
changeset
|
75 size += sizeof(int); |
251
3f7c69fadfc3
- regression fix for changing writable to executable memory for platforms where
Tassilo Philipp
parents:
202
diff
changeset
|
76 #endif |
202 | 77 return mprotect(p, size, PROT_READ|PROT_EXEC); |
78 } | |
79 | |
0 | 80 void dcFreeWX(void* p, size_t size) |
81 { | |
185
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
82 #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
|
83 /* 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
|
84 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
|
85 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
|
86 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
|
87 #endif |
0 | 88 munmap(p, size); |
89 } | |
185
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
90 |