annotate dyncallback/dyncall_alloc_wx_mmap.c @ 357:d982a00c2177

- PPC64 asm syntax fix, specifying explicitly comparison mode for cmpi (newer toolchains complain, older ones took optional field of instruction which happened to be same value)
author Tassilo Philipp
date Tue, 25 Feb 2020 18:16:13 +0100
parents f5577f6bf97a
children
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
281
f5577f6bf97a - file header cleanups for release
Tassilo Philipp
parents: 255
diff changeset
9 Copyright (c) 2007-2018 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
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
030fbb70aa1b - changed allocwx code:
Tassilo Philipp
parents: 185
diff changeset
55 DCerror dcAllocWX(size_t size, void** pp)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
77 *pp = p;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
78 return 0;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
79 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
80
202
030fbb70aa1b - changed allocwx code:
Tassilo Philipp
parents: 185
diff changeset
81 DCerror dcInitExecWX(void* p, size_t size)
030fbb70aa1b - changed allocwx code:
Tassilo Philipp
parents: 185
diff changeset
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
030fbb70aa1b - changed allocwx code:
Tassilo Philipp
parents: 185
diff changeset
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
030fbb70aa1b - changed allocwx code:
Tassilo Philipp
parents: 185
diff changeset
94 }
030fbb70aa1b - changed allocwx code:
Tassilo Philipp
parents: 185
diff changeset
95
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
96 void dcFreeWX(void* p, size_t size)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
104 munmap(p, size);
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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