# HG changeset patch # User Michael Pavone # Date 1435511557 25200 # Node ID b52cd6854c28c527f7e31c6a78f1543a0846532e # Parent 483f7e7926a6130f9c0c30b3f12b90747e31093b Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code diff -r 483f7e7926a6 -r b52cd6854c28 mem.c --- a/mem.c Sun Jun 28 09:53:17 2015 -0700 +++ b/mem.c Sun Jun 28 10:12:37 2015 -0700 @@ -8,33 +8,20 @@ #include #include #include + #include "mem.h" - -/* -void * alloc_code(size_t *size) -{ - *size += PAGE_SIZE - (*size & (PAGE_SIZE - 1)); - return mmap(NULL, *size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); -} -*/ - -/* -void * alloc_code(size_t *size) -{ - char * ret = malloc(*size); - char * base = (char *)(((intptr_t)ret) & (~(PAGE_SIZE-1))); - mprotect(base, (ret + *size) - base, PROT_EXEC | PROT_READ | PROT_WRITE); - return ret; -} -*/ +#ifndef MAP_ANONYMOUS +#define MAP_ANONYMOUS MAP_ANON +#endif void * alloc_code(size_t *size) { + //start at the 1GB mark to allow plenty of room for sbrk based malloc implementations + //while still keeping well within 32-bit displacement range for calling code compiled into the executable + static uint8_t *next = (uint8_t *)0x40000000; *size += PAGE_SIZE - (*size & (PAGE_SIZE - 1)); - void * ret = sbrk(*size); - if (ret == ((void *)-1)) { - return NULL; - } - mprotect(ret, *size, PROT_EXEC | PROT_READ | PROT_WRITE); + uint8_t *ret = mmap(NULL, *size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + next = ret + *size; return ret; } +