diff options
Diffstat (limited to 'src/lj_mcode.c')
-rw-r--r-- | src/lj_mcode.c | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/src/lj_mcode.c b/src/lj_mcode.c index 864da7fb..43694226 100644 --- a/src/lj_mcode.c +++ b/src/lj_mcode.c | |||
@@ -98,6 +98,14 @@ static int mcode_setprot(void *p, size_t sz, DWORD prot) | |||
98 | #define MAP_ANONYMOUS MAP_ANON | 98 | #define MAP_ANONYMOUS MAP_ANON |
99 | #endif | 99 | #endif |
100 | 100 | ||
101 | /* Check for macOS hardened runtime. */ | ||
102 | #if LUAJIT_SECURITY_MCODE != 0 && defined(MAP_JIT) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 110000 | ||
103 | #include <pthread.h> | ||
104 | #define MCMAP_CREATE MAP_JIT | ||
105 | #else | ||
106 | #define MCMAP_CREATE 0 | ||
107 | #endif | ||
108 | |||
101 | #define MCPROT_RW (PROT_READ|PROT_WRITE) | 109 | #define MCPROT_RW (PROT_READ|PROT_WRITE) |
102 | #define MCPROT_RX (PROT_READ|PROT_EXEC) | 110 | #define MCPROT_RX (PROT_READ|PROT_EXEC) |
103 | #define MCPROT_RWX (PROT_READ|PROT_WRITE|PROT_EXEC) | 111 | #define MCPROT_RWX (PROT_READ|PROT_WRITE|PROT_EXEC) |
@@ -109,10 +117,14 @@ static int mcode_setprot(void *p, size_t sz, DWORD prot) | |||
109 | 117 | ||
110 | static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, int prot) | 118 | static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, int prot) |
111 | { | 119 | { |
112 | void *p = mmap((void *)hint, sz, prot|MCPROT_CREATE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); | 120 | void *p = mmap((void *)hint, sz, prot|MCPROT_CREATE, MAP_PRIVATE|MAP_ANONYMOUS|MCMAP_CREATE, -1, 0); |
113 | if (p == MAP_FAILED) { | 121 | if (p == MAP_FAILED) { |
114 | if (!hint) lj_trace_err(J, LJ_TRERR_MCODEAL); | 122 | if (!hint) lj_trace_err(J, LJ_TRERR_MCODEAL); |
115 | p = NULL; | 123 | p = NULL; |
124 | #if MCMAP_CREATE | ||
125 | } else { | ||
126 | pthread_jit_write_protect_np(0); | ||
127 | #endif | ||
116 | } | 128 | } |
117 | return p; | 129 | return p; |
118 | } | 130 | } |
@@ -125,7 +137,12 @@ static void mcode_free(jit_State *J, void *p, size_t sz) | |||
125 | 137 | ||
126 | static int mcode_setprot(void *p, size_t sz, int prot) | 138 | static int mcode_setprot(void *p, size_t sz, int prot) |
127 | { | 139 | { |
140 | #if MCMAP_CREATE | ||
141 | pthread_jit_write_protect_np((prot & PROC_EXEC)); | ||
142 | return 0; | ||
143 | #else | ||
128 | return mprotect(p, sz, prot); | 144 | return mprotect(p, sz, prot); |
145 | #endif | ||
129 | } | 146 | } |
130 | 147 | ||
131 | #else | 148 | #else |