Avoid leaking memory on kernels with recalcitrant mmap() behavior.

This commit is contained in:
Mike Pall 2013-01-28 12:29:31 +01:00
parent 1651684417
commit a3db8f3562

View File

@ -99,8 +99,10 @@ static void mcode_setprot(void *p, size_t sz, DWORD prot)
static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, int prot) static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, int prot)
{ {
void *p = mmap((void *)hint, sz, prot, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); void *p = mmap((void *)hint, sz, prot, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (p == MAP_FAILED && !hint) if (p == MAP_FAILED) {
lj_trace_err(J, LJ_TRERR_MCODEAL); if (!hint) lj_trace_err(J, LJ_TRERR_MCODEAL);
p = NULL;
}
return p; return p;
} }
@ -220,11 +222,10 @@ static void *mcode_alloc(jit_State *J, size_t sz)
if (mcode_validptr(hint)) { if (mcode_validptr(hint)) {
void *p = mcode_alloc_at(J, hint, sz, MCPROT_GEN); void *p = mcode_alloc_at(J, hint, sz, MCPROT_GEN);
if (mcode_validptr(p)) { if (mcode_validptr(p) &&
if ((uintptr_t)p + sz - target < range || target - (uintptr_t)p < range) ((uintptr_t)p + sz - target < range || target - (uintptr_t)p < range))
return p; return p;
mcode_free(J, p, sz); /* Free badly placed area. */ if (p) mcode_free(J, p, sz); /* Free badly placed area. */
}
} }
/* Next try probing pseudo-random addresses. */ /* Next try probing pseudo-random addresses. */
do { do {