xref: /PHP-8.1/ext/pcre/pcre2lib/sljit/sljitUtils.c (revision 5d429008)
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 /* ------------------------------------------------------------------------ */
28 /*  Locks                                                                   */
29 /* ------------------------------------------------------------------------ */
30 
31 /* Executable Allocator */
32 
33 #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR) \
34 	&& !(defined SLJIT_WX_EXECUTABLE_ALLOCATOR && SLJIT_WX_EXECUTABLE_ALLOCATOR)
35 #if (defined SLJIT_SINGLE_THREADED && SLJIT_SINGLE_THREADED)
36 #define SLJIT_ALLOCATOR_LOCK()
37 #define SLJIT_ALLOCATOR_UNLOCK()
38 #elif !(defined _WIN32)
39 #include <pthread.h>
40 
41 static pthread_mutex_t allocator_lock = PTHREAD_MUTEX_INITIALIZER;
42 
43 #define SLJIT_ALLOCATOR_LOCK() pthread_mutex_lock(&allocator_lock)
44 #define SLJIT_ALLOCATOR_UNLOCK() pthread_mutex_unlock(&allocator_lock)
45 #else /* windows */
46 static HANDLE allocator_lock;
47 
allocator_grab_lock(void)48 static SLJIT_INLINE void allocator_grab_lock(void)
49 {
50 	HANDLE lock;
51 	if (SLJIT_UNLIKELY(!InterlockedCompareExchangePointer(&allocator_lock, NULL, NULL))) {
52 		lock = CreateMutex(NULL, FALSE, NULL);
53 		if (InterlockedCompareExchangePointer(&allocator_lock, lock, NULL))
54 			CloseHandle(lock);
55 	}
56 	WaitForSingleObject(allocator_lock, INFINITE);
57 }
58 
59 #define SLJIT_ALLOCATOR_LOCK() allocator_grab_lock()
60 #define SLJIT_ALLOCATOR_UNLOCK() ReleaseMutex(allocator_lock)
61 #endif /* thread implementation */
62 #endif /* SLJIT_EXECUTABLE_ALLOCATOR && !SLJIT_WX_EXECUTABLE_ALLOCATOR */
63 
64 /* ------------------------------------------------------------------------ */
65 /*  Stack                                                                   */
66 /* ------------------------------------------------------------------------ */
67 
68 #if ((defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) \
69 	&& !(defined SLJIT_UTIL_SIMPLE_STACK_ALLOCATION && SLJIT_UTIL_SIMPLE_STACK_ALLOCATION)) \
70 	|| ((defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR) \
71 	&& !((defined SLJIT_PROT_EXECUTABLE_ALLOCATOR && SLJIT_PROT_EXECUTABLE_ALLOCATOR) \
72 	|| (defined SLJIT_WX_EXECUTABLE_ALLOCATOR && SLJIT_WX_EXECUTABLE_ALLOCATOR)))
73 
74 #ifndef _WIN32
75 /* Provides mmap function. */
76 #include <sys/types.h>
77 #include <sys/mman.h>
78 
79 #ifndef MAP_ANON
80 #ifdef MAP_ANONYMOUS
81 #define MAP_ANON MAP_ANONYMOUS
82 #endif /* MAP_ANONYMOUS */
83 #endif /* !MAP_ANON */
84 
85 #ifndef MAP_ANON
86 
87 #include <fcntl.h>
88 
89 #ifdef O_CLOEXEC
90 #define SLJIT_CLOEXEC	O_CLOEXEC
91 #else /* !O_CLOEXEC */
92 #define SLJIT_CLOEXEC	0
93 #endif /* O_CLOEXEC */
94 
95 /* Some old systems do not have MAP_ANON. */
96 static int dev_zero = -1;
97 
98 #if (defined SLJIT_SINGLE_THREADED && SLJIT_SINGLE_THREADED)
99 
open_dev_zero(void)100 static SLJIT_INLINE int open_dev_zero(void)
101 {
102 	dev_zero = open("/dev/zero", O_RDWR | SLJIT_CLOEXEC);
103 
104 	return dev_zero < 0;
105 }
106 
107 #else /* !SLJIT_SINGLE_THREADED */
108 
109 #include <pthread.h>
110 
111 static pthread_mutex_t dev_zero_mutex = PTHREAD_MUTEX_INITIALIZER;
112 
open_dev_zero(void)113 static SLJIT_INLINE int open_dev_zero(void)
114 {
115 	pthread_mutex_lock(&dev_zero_mutex);
116 	if (SLJIT_UNLIKELY(dev_zero < 0))
117 		dev_zero = open("/dev/zero", O_RDWR | SLJIT_CLOEXEC);
118 
119 	pthread_mutex_unlock(&dev_zero_mutex);
120 	return dev_zero < 0;
121 }
122 
123 #endif /* SLJIT_SINGLE_THREADED */
124 #undef SLJIT_CLOEXEC
125 #endif /* !MAP_ANON */
126 #endif /* !_WIN32 */
127 #endif /* open_dev_zero */
128 
129 #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) \
130 	|| (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
131 
132 #ifdef _WIN32
133 
get_page_alignment(void)134 static SLJIT_INLINE sljit_sw get_page_alignment(void) {
135 	SYSTEM_INFO si;
136 	static sljit_sw sljit_page_align;
137 	if (!sljit_page_align) {
138 		GetSystemInfo(&si);
139 		sljit_page_align = si.dwPageSize - 1;
140 	}
141 	return sljit_page_align;
142 }
143 
144 #else
145 
146 #include <unistd.h>
147 
get_page_alignment(void)148 static SLJIT_INLINE sljit_sw get_page_alignment(void) {
149 	static sljit_sw sljit_page_align = -1;
150 	if (sljit_page_align < 0) {
151 #ifdef _SC_PAGESIZE
152 		sljit_page_align = sysconf(_SC_PAGESIZE);
153 #else
154 		sljit_page_align = getpagesize();
155 #endif
156 		/* Should never happen. */
157 		if (sljit_page_align < 0)
158 			sljit_page_align = 4096;
159 		sljit_page_align--;
160 	}
161 	return sljit_page_align;
162 }
163 
164 #endif /* _WIN32 */
165 
166 #endif /* get_page_alignment() */
167 
168 #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK)
169 
170 #if (defined SLJIT_UTIL_SIMPLE_STACK_ALLOCATION && SLJIT_UTIL_SIMPLE_STACK_ALLOCATION)
171 
sljit_allocate_stack(sljit_uw start_size,sljit_uw max_size,void * allocator_data)172 SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_FUNC sljit_allocate_stack(sljit_uw start_size, sljit_uw max_size, void *allocator_data)
173 {
174 	struct sljit_stack *stack;
175 	void *ptr;
176 
177 	SLJIT_UNUSED_ARG(allocator_data);
178 
179 	if (start_size > max_size || start_size < 1)
180 		return NULL;
181 
182 	stack = (struct sljit_stack*)SLJIT_MALLOC(sizeof(struct sljit_stack), allocator_data);
183 	if (stack == NULL)
184 		return NULL;
185 
186 	ptr = SLJIT_MALLOC(max_size, allocator_data);
187 	if (ptr == NULL) {
188 		SLJIT_FREE(stack, allocator_data);
189 		return NULL;
190 	}
191 
192 	stack->min_start = (sljit_u8 *)ptr;
193  	stack->end = stack->min_start + max_size;
194  	stack->start = stack->end - start_size;
195 	stack->top = stack->end;
196 	return stack;
197 }
198 
sljit_free_stack(struct sljit_stack * stack,void * allocator_data)199 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_free_stack(struct sljit_stack *stack, void *allocator_data)
200 {
201 	SLJIT_UNUSED_ARG(allocator_data);
202 	SLJIT_FREE((void*)stack->min_start, allocator_data);
203 	SLJIT_FREE(stack, allocator_data);
204 }
205 
sljit_stack_resize(struct sljit_stack * stack,sljit_u8 * new_start)206 SLJIT_API_FUNC_ATTRIBUTE sljit_u8 *SLJIT_FUNC sljit_stack_resize(struct sljit_stack *stack, sljit_u8 *new_start)
207 {
208 	if ((new_start < stack->min_start) || (new_start >= stack->end))
209 		return NULL;
210 	stack->start = new_start;
211 	return new_start;
212 }
213 
214 #else /* !SLJIT_UTIL_SIMPLE_STACK_ALLOCATION */
215 
216 #ifdef _WIN32
217 
sljit_free_stack(struct sljit_stack * stack,void * allocator_data)218 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_free_stack(struct sljit_stack *stack, void *allocator_data)
219 {
220 	SLJIT_UNUSED_ARG(allocator_data);
221 	VirtualFree((void*)stack->min_start, 0, MEM_RELEASE);
222 	SLJIT_FREE(stack, allocator_data);
223 }
224 
225 #else /* !_WIN32 */
226 
sljit_free_stack(struct sljit_stack * stack,void * allocator_data)227 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_free_stack(struct sljit_stack *stack, void *allocator_data)
228 {
229 	SLJIT_UNUSED_ARG(allocator_data);
230 	munmap((void*)stack->min_start, stack->end - stack->min_start);
231 	SLJIT_FREE(stack, allocator_data);
232 }
233 
234 #endif /* _WIN32 */
235 
sljit_allocate_stack(sljit_uw start_size,sljit_uw max_size,void * allocator_data)236 SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_FUNC sljit_allocate_stack(sljit_uw start_size, sljit_uw max_size, void *allocator_data)
237 {
238 	struct sljit_stack *stack;
239 	void *ptr;
240 	sljit_sw page_align;
241 
242 	SLJIT_UNUSED_ARG(allocator_data);
243 
244 	if (start_size > max_size || start_size < 1)
245 		return NULL;
246 
247 	stack = (struct sljit_stack*)SLJIT_MALLOC(sizeof(struct sljit_stack), allocator_data);
248 	if (stack == NULL)
249 		return NULL;
250 
251 	/* Align max_size. */
252 	page_align = get_page_alignment();
253 	max_size = (max_size + page_align) & ~page_align;
254 
255 #ifdef _WIN32
256 	ptr = VirtualAlloc(NULL, max_size, MEM_RESERVE, PAGE_READWRITE);
257 	if (!ptr) {
258 		SLJIT_FREE(stack, allocator_data);
259 		return NULL;
260 	}
261 
262 	stack->min_start = (sljit_u8 *)ptr;
263 	stack->end = stack->min_start + max_size;
264 	stack->start = stack->end;
265 
266 	if (sljit_stack_resize(stack, stack->end - start_size) == NULL) {
267 		sljit_free_stack(stack, allocator_data);
268 		return NULL;
269 	}
270 #else /* !_WIN32 */
271 #ifdef MAP_ANON
272 	ptr = mmap(NULL, max_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
273 #else /* !MAP_ANON */
274 	if (SLJIT_UNLIKELY((dev_zero < 0) && open_dev_zero())) {
275 		SLJIT_FREE(stack, allocator_data);
276 		return NULL;
277 	}
278 	ptr = mmap(NULL, max_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, dev_zero, 0);
279 #endif /* MAP_ANON */
280 	if (ptr == MAP_FAILED) {
281 		SLJIT_FREE(stack, allocator_data);
282 		return NULL;
283 	}
284 	stack->min_start = (sljit_u8 *)ptr;
285 	stack->end = stack->min_start + max_size;
286 	stack->start = stack->end - start_size;
287 #endif /* _WIN32 */
288 
289 	stack->top = stack->end;
290 	return stack;
291 }
292 
sljit_stack_resize(struct sljit_stack * stack,sljit_u8 * new_start)293 SLJIT_API_FUNC_ATTRIBUTE sljit_u8 *SLJIT_FUNC sljit_stack_resize(struct sljit_stack *stack, sljit_u8 *new_start)
294 {
295 #if defined _WIN32 || defined(POSIX_MADV_DONTNEED)
296 	sljit_uw aligned_old_start;
297 	sljit_uw aligned_new_start;
298 	sljit_sw page_align;
299 #endif
300 
301 	if ((new_start < stack->min_start) || (new_start >= stack->end))
302 		return NULL;
303 
304 #ifdef _WIN32
305 	page_align = get_page_alignment();
306 
307 	aligned_new_start = (sljit_uw)new_start & ~page_align;
308 	aligned_old_start = ((sljit_uw)stack->start) & ~page_align;
309 	if (aligned_new_start != aligned_old_start) {
310 		if (aligned_new_start < aligned_old_start) {
311 			if (!VirtualAlloc((void*)aligned_new_start, aligned_old_start - aligned_new_start, MEM_COMMIT, PAGE_READWRITE))
312 				return NULL;
313 		}
314 		else {
315 			if (!VirtualFree((void*)aligned_old_start, aligned_new_start - aligned_old_start, MEM_DECOMMIT))
316 				return NULL;
317 		}
318 	}
319 #elif defined(POSIX_MADV_DONTNEED)
320 	if (stack->start < new_start) {
321 		page_align = get_page_alignment();
322 
323 		aligned_new_start = (sljit_uw)new_start & ~page_align;
324 		aligned_old_start = ((sljit_uw)stack->start) & ~page_align;
325 
326 		if (aligned_new_start > aligned_old_start) {
327 			posix_madvise((void*)aligned_old_start, aligned_new_start - aligned_old_start, POSIX_MADV_DONTNEED);
328 #ifdef MADV_FREE
329 			madvise((void*)aligned_old_start, aligned_new_start - aligned_old_start, MADV_FREE);
330 #endif /* MADV_FREE */
331 		}
332 	}
333 #endif /* _WIN32 */
334 
335 	stack->start = new_start;
336 	return new_start;
337 }
338 
339 #endif /* SLJIT_UTIL_SIMPLE_STACK_ALLOCATION */
340 
341 #endif /* SLJIT_UTIL_STACK */
342