1 /*
2 * Stack-less Just-In-Time compiler
3 *
4 * Copyright Zoltan Herczeg ([email protected]). All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 * of conditions and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/types.h>
28 #include <sys/mman.h>
29 /*
30 On macOS systems, returns MAP_JIT if it is defined _and_ we're running on a
31 version where it's OK to have more than one JIT block or where MAP_JIT is
32 required.
33 On non-macOS systems, returns MAP_JIT if it is defined.
34 */
35 #include <TargetConditionals.h>
36
37 #if (defined(TARGET_OS_OSX) && TARGET_OS_OSX) || (TARGET_OS_MAC && !TARGET_OS_IPHONE)
38
39 #if defined(SLJIT_CONFIG_X86) && SLJIT_CONFIG_X86
40
41 #include <sys/utsname.h>
42 #include <stdlib.h>
43
44 #define SLJIT_UPDATE_WX_FLAGS(from, to, enable_exec)
45
46 #ifdef MAP_JIT
47 #define SLJIT_MAP_JIT (get_map_jit_flag())
get_map_jit_flag(void)48 static SLJIT_INLINE int get_map_jit_flag(void)
49 {
50 size_t page_size;
51 void *ptr;
52 struct utsname name;
53 static int map_jit_flag = -1;
54
55 if (map_jit_flag < 0) {
56 map_jit_flag = 0;
57 uname(&name);
58
59 /* Kernel version for 10.14.0 (Mojave) or later */
60 if (atoi(name.release) >= 18) {
61 page_size = get_page_alignment() + 1;
62 /* Only use MAP_JIT if a hardened runtime is used */
63 ptr = mmap(NULL, page_size, PROT_WRITE | PROT_EXEC,
64 MAP_PRIVATE | MAP_ANON, -1, 0);
65
66 if (ptr != MAP_FAILED)
67 munmap(ptr, page_size);
68 else
69 map_jit_flag = MAP_JIT;
70 }
71 }
72 return map_jit_flag;
73 }
74 #else /* !defined(MAP_JIT) */
75 #define SLJIT_MAP_JIT (0)
76 #endif
77
78 #elif defined(SLJIT_CONFIG_ARM) && SLJIT_CONFIG_ARM
79
80 #include <AvailabilityMacros.h>
81 #include <pthread.h>
82
83 #define SLJIT_MAP_JIT (MAP_JIT)
84 #define SLJIT_UPDATE_WX_FLAGS(from, to, enable_exec) \
85 apple_update_wx_flags(enable_exec)
86
apple_update_wx_flags(sljit_s32 enable_exec)87 static SLJIT_INLINE void apple_update_wx_flags(sljit_s32 enable_exec)
88 {
89 #if MAC_OS_X_VERSION_MIN_REQUIRED < 110000
90 if (__builtin_available(macos 11, *))
91 #endif /* BigSur */
92 pthread_jit_write_protect_np(enable_exec);
93 }
94
95 #elif defined(SLJIT_CONFIG_PPC) && SLJIT_CONFIG_PPC
96
97 #define SLJIT_MAP_JIT (0)
98 #define SLJIT_UPDATE_WX_FLAGS(from, to, enable_exec)
99
100 #else
101 #error "Unsupported architecture"
102 #endif /* SLJIT_CONFIG */
103
104 #else /* !TARGET_OS_OSX */
105
106 #ifdef MAP_JIT
107 #define SLJIT_MAP_JIT (MAP_JIT)
108 #else
109 #define SLJIT_MAP_JIT (0)
110 #endif
111
112 #endif /* TARGET_OS_OSX */
113
alloc_chunk(sljit_uw size)114 static SLJIT_INLINE void* alloc_chunk(sljit_uw size)
115 {
116 void *retval;
117 int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
118 int flags = MAP_PRIVATE;
119 int fd = -1;
120
121 flags |= MAP_ANON | SLJIT_MAP_JIT;
122
123 retval = mmap(NULL, size, prot, flags, fd, 0);
124 if (retval == MAP_FAILED)
125 return NULL;
126
127 SLJIT_UPDATE_WX_FLAGS(retval, (uint8_t *)retval + size, 0);
128
129 return retval;
130 }
131
free_chunk(void * chunk,sljit_uw size)132 static SLJIT_INLINE void free_chunk(void *chunk, sljit_uw size)
133 {
134 munmap(chunk, size);
135 }
136
137 #include "sljitExecAllocatorCore.c"
138