Mercurial > pub > dyncall > dyncall
annotate dyncallback/dyncall_alloc_wx_mmap.c @ 218:cb56f077fd37
- dynload_plain test fixes
author | Tassilo Philipp |
---|---|
date | Thu, 06 Apr 2017 13:35:50 +0200 |
parents | 030fbb70aa1b |
children | 3f7c69fadfc3 |
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) |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
48 // Hack around not having POSIX' MAP_ANON by going through /dev/zero; store |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
49 // file descriptor to close on dcFreeWX at beginning of memory, as tiny hack |
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 { | |
72 return mprotect(p, size, PROT_READ|PROT_EXEC); | |
73 } | |
74 | |
0 | 75 void dcFreeWX(void* p, size_t size) |
76 { | |
185
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
77 #if !defined(MAP_ANON) && 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
|
78 // Close file descriptor of no-MAP_ANON workaround (see above) |
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
79 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
|
80 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
|
81 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
|
82 #endif |
0 | 83 munmap(p, size); |
84 } | |
185
a41bc98e101c
- impl. for allocwx stuff on POSIX-strict systems where there is no MAP_ANON for mmap()
Tassilo Philipp
parents:
0
diff
changeset
|
85 |