From 49dfbee2119ce52600f7acbf61af690d6c9ef16c Mon Sep 17 00:00:00 2001 From: ptitSeb Date: Fri, 5 May 2023 13:49:06 +0200 Subject: Improved handling of x86_64 47bits memory (should help #763) --- src/custommem.c | 25 +++++++++++++++++++++++++ src/elfs/elfloader.c | 2 +- src/wrapped/wrappedlibc.c | 6 +++--- 3 files changed, 29 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/custommem.c b/src/custommem.c index f77b005c..7214aa8c 100644 --- a/src/custommem.c +++ b/src/custommem.c @@ -1364,6 +1364,30 @@ static void atfork_child_custommem(void) init_mutexes(); } +void reserveHighMem() +{ + intptr_t cur = 1LL<<47; + mapmem_t* m = mapmem; + while(m && (m->endnext; + } + while (m) { + uintptr_t addr = 0, end = 0; + if(m->begin>cur) { + void* ret = mmap64((void*)cur, m->begin-cur, 0, MAP_ANONYMOUS|MAP_FIXED|MAP_PRIVATE|MAP_NORESERVE, -1, 0); + printf_log(LOG_DEBUG, "Reserve %p(0x%zx) => %p (%s)\n", (void*)cur, m->begin-cur, ret, strerror(errno)); + if(ret!=(void*)-1) { + addr = cur; + end = m->begin; + } + } + cur = m->end + 1; + m = m->next; + if(addr) + addMapMem(addr, end); + } +} + void init_custommem_helper(box64context_t* ctx) { (void)ctx; @@ -1392,6 +1416,7 @@ void init_custommem_helper(box64context_t* ctx) mapmem->begin = 0x0; mapmem->end = (uintptr_t)LOWEST - 1; loadProtectionFromMap(); + reserveHighMem(); // check if PageSize is correctly defined if(box64_pagesize != (1<memsz); // limit to 47bits... printf_log(log_level, "Allocating 0x%zx memory @%p for Elf \"%s\"\n", head->memsz, (void*)offs, head->name); void* p = mmap((void*)offs, head->memsz diff --git a/src/wrapped/wrappedlibc.c b/src/wrapped/wrappedlibc.c index cc68c216..3f4e7a09 100755 --- a/src/wrapped/wrappedlibc.c +++ b/src/wrapped/wrappedlibc.c @@ -2590,7 +2590,7 @@ EXPORT void* my_mremap(x64emu_t* emu, void* old_addr, size_t old_size, size_t ne freeProtection((uintptr_t)ret+new_size, old_size-new_size); #ifdef DYNAREC if(box64_dynarec) - cleanDBFromAddressRange((uintptr_t)ret+new_size, new_size-old_size, 1); + cleanDBFromAddressRange((uintptr_t)ret+new_size, old_size-new_size, 1); #endif } else if(!old_size) { setProtection((uintptr_t)ret, new_size, prot); @@ -2644,14 +2644,14 @@ EXPORT int my_mprotect(x64emu_t* emu, void *addr, unsigned long len, int prot) prot|=PROT_READ; // PROT_READ is implicit with PROT_WRITE on x86_64 int ret = mprotect(addr, len, prot); #ifdef DYNAREC - if(box64_dynarec && !ret) { + if(box64_dynarec && !ret && len) { if(prot& PROT_EXEC) addDBFromAddressRange((uintptr_t)addr, len); else cleanDBFromAddressRange((uintptr_t)addr, len, 0); } #endif - if(!ret) + if(!ret && len) updateProtection((uintptr_t)addr, len, prot); return ret; } -- cgit 1.4.1