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 #define SLJIT_HAS_CHUNK_HEADER
28 #define SLJIT_HAS_EXECUTABLE_OFFSET
29 
30 struct sljit_chunk_header {
31 	void *executable;
32 };
33 
34 /*
35  * MAP_REMAPDUP is a NetBSD extension available sinde 8.0, make sure to
36  * adjust your feature macros (ex: -D_NETBSD_SOURCE) as needed
37  */
alloc_chunk(sljit_uw size)38 static SLJIT_INLINE struct sljit_chunk_header* alloc_chunk(sljit_uw size)
39 {
40 	struct sljit_chunk_header *retval;
41 
42 	retval = (struct sljit_chunk_header *)mmap(NULL, size,
43 			PROT_READ | PROT_WRITE | PROT_MPROTECT(PROT_EXEC),
44 			MAP_ANON | MAP_SHARED, -1, 0);
45 
46 	if (retval == MAP_FAILED)
47 		return NULL;
48 
49 	retval->executable = mremap(retval, size, NULL, size, MAP_REMAPDUP);
50 	if (retval->executable == MAP_FAILED) {
51 		munmap((void *)retval, size);
52 		return NULL;
53 	}
54 
55 	if (mprotect(retval->executable, size, PROT_READ | PROT_EXEC) == -1) {
56 		munmap(retval->executable, size);
57 		munmap((void *)retval, size);
58 		return NULL;
59 	}
60 
61 	return retval;
62 }
63 
free_chunk(void * chunk,sljit_uw size)64 static SLJIT_INLINE void free_chunk(void *chunk, sljit_uw size)
65 {
66 	struct sljit_chunk_header *header = ((struct sljit_chunk_header *)chunk) - 1;
67 
68 	munmap(header->executable, size);
69 	munmap((void *)header, size);
70 }
71 
72 #include "sljitExecAllocatorCore.c"
73