# HG changeset patch # User Michael Pavone # Date 1704258429 28800 # Node ID 8e86cd581620f5c862516286c95c1bd4e3c275db # Parent b50fa7602e39ba08293280770955f063ec7aef46 Better implementation of alloc_code for ASLR/libretro cases that also hopefully works on modern Mac OS diff -r b50fa7602e39 -r 8e86cd581620 mem.c --- a/mem.c Tue Jan 02 18:23:15 2024 -0800 +++ b/mem.c Tue Jan 02 21:07:09 2024 -0800 @@ -17,15 +17,11 @@ #define MAP_ANONYMOUS MAP_ANON #endif -#ifndef MAP_32BIT -#define MAP_32BIT 0 -#endif - void * alloc_code(size_t *size) { - //start at the 1GB mark to allow plenty of room for sbrk based malloc implementations + //start at 1GB above compiled code 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; + static uint8_t *next = ((uint8_t *)alloc_code) + 0x40000000; uint8_t *ret = try_alloc_arena(); if (ret) { return ret; @@ -33,11 +29,12 @@ if (*size & (PAGE_SIZE -1)) { *size += PAGE_SIZE - (*size & (PAGE_SIZE - 1)); } - ret = mmap(next, *size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0); + ret = mmap(next, *size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (ret == MAP_FAILED) { perror("alloc_code"); return NULL; } + printf("alloc_code next was %p, ret is %p, alloc_code address %p\n", next, ret, alloc_code); track_block(ret); next = ret + *size; return ret;