1 /*
2  *    Stack-less Just-In-Time compiler
3  *
4  *    Copyright Zoltan Herczeg (hzmester@freemail.hu). 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_MAP_JIT	(get_map_jit_flag())
45 #define SLJIT_UPDATE_WX_FLAGS(from, to, enable_exec)
46 
get_map_jit_flag(void)47 static SLJIT_INLINE int get_map_jit_flag(void)
48 {
49 	size_t page_size;
50 	void *ptr;
51 	struct utsname name;
52 	static int map_jit_flag = -1;
53 
54 	if (map_jit_flag < 0) {
55 		map_jit_flag = 0;
56 		uname(&name);
57 
58 		/* Kernel version for 10.14.0 (Mojave) or later */
59 		if (atoi(name.release) >= 18) {
60 			page_size = get_page_alignment() + 1;
61 			/* Only use MAP_JIT if a hardened runtime is used */
62 			ptr = mmap(NULL, page_size, PROT_WRITE | PROT_EXEC,
63 					MAP_PRIVATE | MAP_ANON, -1, 0);
64 
65 			if (ptr != MAP_FAILED)
66 				munmap(ptr, page_size);
67 			else
68 				map_jit_flag = MAP_JIT;
69 		}
70 	}
71 	return map_jit_flag;
72 }
73 
74 #elif defined(SLJIT_CONFIG_ARM) && SLJIT_CONFIG_ARM
75 
76 #include <AvailabilityMacros.h>
77 #include <pthread.h>
78 
79 #define SLJIT_MAP_JIT	(MAP_JIT)
80 #define SLJIT_UPDATE_WX_FLAGS(from, to, enable_exec) \
81 		apple_update_wx_flags(enable_exec)
82 
apple_update_wx_flags(sljit_s32 enable_exec)83 static SLJIT_INLINE void apple_update_wx_flags(sljit_s32 enable_exec)
84 {
85 #if MAC_OS_X_VERSION_MIN_REQUIRED < 110000
86 	if (__builtin_available(macos 11, *))
87 #endif /* BigSur */
88 	pthread_jit_write_protect_np(enable_exec);
89 }
90 
91 #elif defined(SLJIT_CONFIG_PPC) && SLJIT_CONFIG_PPC
92 
93 #define SLJIT_MAP_JIT	(0)
94 #define SLJIT_UPDATE_WX_FLAGS(from, to, enable_exec)
95 
96 #else
97 #error "Unsupported architecture"
98 #endif /* SLJIT_CONFIG */
99 
100 #else /* !TARGET_OS_OSX */
101 
102 #ifdef MAP_JIT
103 #define SLJIT_MAP_JIT	(MAP_JIT)
104 #else
105 #define SLJIT_MAP_JIT	(0)
106 #endif
107 
108 #endif /* TARGET_OS_OSX */
109 
alloc_chunk(sljit_uw size)110 static SLJIT_INLINE void* alloc_chunk(sljit_uw size)
111 {
112 	void *retval;
113 	int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
114 	int flags = MAP_PRIVATE;
115 	int fd = -1;
116 
117 	flags |= MAP_ANON | SLJIT_MAP_JIT;
118 
119 	retval = mmap(NULL, size, prot, flags, fd, 0);
120 	if (retval == MAP_FAILED)
121 		return NULL;
122 
123 	SLJIT_UPDATE_WX_FLAGS(retval, (uint8_t *)retval + size, 0);
124 
125 	return retval;
126 }
127 
free_chunk(void * chunk,sljit_uw size)128 static SLJIT_INLINE void free_chunk(void *chunk, sljit_uw size)
129 {
130 	munmap(chunk, size);
131 }
132 
133 #include "sljitExecAllocatorCore.c"
134