Skip to content

Commit b876d6d

Browse files
author
Mike Pall
committed
OpenBSD/x86: Better executable memory allocation for W^X mode.
1 parent 86913b9 commit b876d6d

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/lj_mcode.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static void mcode_free(jit_State *J, void *p, size_t sz)
145145

146146
/* -- MCode area protection ----------------------------------------------- */
147147

148-
/* Define this ONLY if the page protection twiddling becomes a bottleneck. */
148+
/* Define this ONLY if page protection twiddling becomes a bottleneck. */
149149
#ifdef LUAJIT_UNPROTECT_MCODE
150150

151151
/* It's generally considered to be a potential security risk to have
@@ -252,7 +252,20 @@ static void *mcode_alloc(jit_State *J, size_t sz)
252252
#else
253253

254254
/* All memory addresses are reachable by relative jumps. */
255-
#define mcode_alloc(J, sz) mcode_alloc_at((J), 0, (sz), MCPROT_GEN)
255+
static void *mcode_alloc(jit_State *J, size_t sz)
256+
{
257+
#ifdef __OpenBSD__
258+
/* Allow better executable memory allocation for OpenBSD W^X mode. */
259+
void *p = mcode_alloc_at(J, 0, sz, MCPROT_RUN);
260+
if (p && mcode_setprot(p, sz, MCPROT_GEN)) {
261+
mcode_free(J, p, sz);
262+
return NULL;
263+
}
264+
return p;
265+
#else
266+
return mcode_alloc_at(J, 0, sz, MCPROT_GEN);
267+
#endif
268+
}
256269

257270
#endif
258271

0 commit comments

Comments
 (0)