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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 /*
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 Package: dyncall
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 Library: dyncallback
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 File: dyncallback/dyncall_alloc_wx_mmap.c
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 Description: Allocate write/executable memory - Implementation for posix
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 License:
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 Tassilo Philipp <tphilipp@potion-studios.com>
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 Permission to use, copy, modify, and distribute this software for any
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 purpose with or without fee is hereby granted, provided that the above
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 copyright notice and this permission notice appear in all copies.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 */
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 #include "dyncall_alloc_wx.h"
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 #include <sys/types.h>
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 #include <sys/mman.h>
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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
030fbb70aa1b - changed allocwx code:
Tassilo Philipp
parents: 185
diff changeset
44 DCerror dcAllocWX(size_t size, void** pp)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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
030fbb70aa1b - changed allocwx code:
Tassilo Philipp
parents: 185
diff changeset
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
030fbb70aa1b - changed allocwx code:
Tassilo Philipp
parents: 185
diff changeset
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
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
66 *pp = p;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
67 return 0;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
68 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
69
202
030fbb70aa1b - changed allocwx code:
Tassilo Philipp
parents: 185
diff changeset
70 DCerror dcInitExecWX(void* p, size_t size)
030fbb70aa1b - changed allocwx code:
Tassilo Philipp
parents: 185
diff changeset
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
030fbb70aa1b - changed allocwx code:
Tassilo Philipp
parents: 185
diff changeset
77 return mprotect(p, size, PROT_READ|PROT_EXEC);
030fbb70aa1b - changed allocwx code:
Tassilo Philipp
parents: 185
diff changeset
78 }
030fbb70aa1b - changed allocwx code:
Tassilo Philipp
parents: 185
diff changeset
79
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
80 void dcFreeWX(void* p, size_t size)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
88 munmap(p, size);
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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