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/mman.h>
28 #include <sys/procctl.h>
29
30 #ifdef PROC_WXMAP_CTL
sljit_is_wx_block(void)31 static SLJIT_INLINE int sljit_is_wx_block(void)
32 {
33 static int wx_block = -1;
34 if (wx_block < 0) {
35 int sljit_wx_enable = PROC_WX_MAPPINGS_PERMIT;
36 wx_block = !!procctl(P_PID, 0, PROC_WXMAP_CTL, &sljit_wx_enable);
37 }
38 return wx_block;
39 }
40
41 #define SLJIT_IS_WX_BLOCK sljit_is_wx_block()
42 #else /* !PROC_WXMAP_CTL */
43 #define SLJIT_IS_WX_BLOCK (1)
44 #endif /* PROC_WXMAP_CTL */
45
alloc_chunk(sljit_uw size)46 static SLJIT_INLINE void* alloc_chunk(sljit_uw size)
47 {
48 void *retval;
49 int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
50 int flags = MAP_PRIVATE;
51 int fd = -1;
52
53 #ifdef PROT_MAX
54 prot |= PROT_MAX(prot);
55 #endif
56
57 #ifdef MAP_ANON
58 flags |= MAP_ANON;
59 #else /* !MAP_ANON */
60 if (SLJIT_UNLIKELY((dev_zero < 0) && open_dev_zero()))
61 return NULL;
62
63 fd = dev_zero;
64 #endif /* MAP_ANON */
65
66 retry:
67 retval = mmap(NULL, size, prot, flags, fd, 0);
68 if (retval == MAP_FAILED) {
69 if (!SLJIT_IS_WX_BLOCK)
70 goto retry;
71
72 return NULL;
73 }
74
75 /* HardenedBSD's mmap lies, so check permissions again. */
76 if (mprotect(retval, size, PROT_READ | PROT_WRITE | PROT_EXEC) < 0) {
77 munmap(retval, size);
78 return NULL;
79 }
80
81 return retval;
82 }
83
free_chunk(void * chunk,sljit_uw size)84 static SLJIT_INLINE void free_chunk(void *chunk, sljit_uw size)
85 {
86 munmap(chunk, size);
87 }
88
89 #include "sljitExecAllocatorCore.c"
90