diff --git a/CMakeLists.txt b/CMakeLists.txt index a482dfd7ed..ed5c3ba214 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -498,7 +498,7 @@ set(ELFLOADER_SRC ) if(NOT STATICBUILD) list(APPEND ELFLOADER_SRC - "${BOX64_ROOT}/src/mallochook.c" + "${BOX64_ROOT}/src/guestalloc.c" "${BOX64_ROOT}/src/steam.c" "${BOX64_ROOT}/src/libtools/sdl1rwops.c" "${BOX64_ROOT}/src/libtools/sdl2rwops.c" diff --git a/docs/USAGE.md b/docs/USAGE.md index e6dfa29038..2a84838db9 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -376,21 +376,6 @@ Detect libjvm and apply conservative settings. * 0: Does nothing. * 1: Detect libjvm, and apply BOX64_DYNAREC_BIGBLOCK=0 BOX64_DYNAREC_STRONGMEM=1 BOX64_SSE42=0 when detected. [Default] -### BOX64_LIBCEF - -Detect libcef and apply BOX64_MALLOC_HACK settings. - - * 0: Does nothing. [Default] - * 1: Detect libcef, and apply BOX64_MALLOC_HACK=2 if detected. - -### BOX64_MALLOC_HACK - -Behaviour when hooking malloc operators. - - * 0: Don't allow malloc operator to be redirected, rewriting code to use regular function. [Default] - * 1: Allow malloc operator to be redirected. - * 2: Like 0, but track special mmap/free (some redirected functions are inlined and cannot be redirected). - ### BOX64_MAXCPU Maximum CPU cores exposed. Available in WowBox64. diff --git a/docs/USAGE_CN.md b/docs/USAGE_CN.md index eceaef8668..218c18f635 100644 --- a/docs/USAGE_CN.md +++ b/docs/USAGE_CN.md @@ -363,21 +363,6 @@ Skip N first CPU cores (Is apply before MAXCPU) 在 WowBox64 中可用。 * 0: 不执行任何操作。 * 1: 检测 libjvm,并在检测到时应用 BOX64_DYNAREC_BIGBLOCK=0 BOX64_DYNAREC_STRONGMEM=1 BOX64_SSE42=0。 [默认值] -### BOX64_LIBCEF - -检测 libcef 并应用 BOX64_MALLOC_HACK 设置。 - - * 0: 不执行任何操作。 [默认值] - * 1: 检测 libcef,并在检测到时应用 BOX64_MALLOC_HACK=2。 - -### BOX64_MALLOC_HACK - -挂钩 malloc 操作符时的行为。 - - * 0: 不允许重定向 malloc 操作符,重写代码使用常规函数。 [默认值] - * 1: 允许重定向 malloc 操作符。 - * 2: 与 0 类似,但追踪特殊的 mmap/free(某些内联的重定向函数无法被重定向)。 - ### BOX64_MAXCPU 暴露的最大 CPU 核心数。 在 WowBox64 中可用。 diff --git a/docs/box64.pod b/docs/box64.pod index 3b73cef763..6bcb3453be 100644 --- a/docs/box64.pod +++ b/docs/box64.pod @@ -577,14 +577,6 @@ Force loading libraries with the binary. * XXXX:YYYY : Force the loading of libraries XXXX and YYYY. -=item B =I<0|1> - -Detect libcef and apply BOX64_MALLOC_HACK settings. - - * 0 : Does nothing. [Default] - * 1 : Detect libcef, and apply BOX64_MALLOC_HACK=2 if detected. - - =item B =I Set the name for libGL. @@ -610,15 +602,6 @@ Enable or disable Box64 logs, default value is 0 if stdout is not terminal, 1 ot * 3 : Enable verbose level Box64 logs. -=item B =I<0|1|2> - -Behaviour when hooking malloc operators. - - * 0 : Don't allow malloc operator to be redirected, rewriting code to use regular function. [Default] - * 1 : Allow malloc operator to be redirected. - * 2 : Like 0, but track special mmap/free (some redirected functions are inlined and cannot be redirected). - - =item B =I<0|XXXX> Maximum CPU cores exposed. Available in WowBox64. diff --git a/docs/gen/usage.json b/docs/gen/usage.json index 931a49f4b3..b3cd86f6d7 100644 --- a/docs/gen/usage.json +++ b/docs/gen/usage.json @@ -1322,25 +1322,6 @@ } ] }, - { - "name": "BOX64_LIBCEF", - "description": "Detect libcef and apply BOX64_MALLOC_HACK settings.", - "category": "Compatibility", - "wine": false, - "configurator": false, - "options": [ - { - "key": "0", - "description": "Does nothing.", - "default": true - }, - { - "key": "1", - "description": "Detect libcef, and apply BOX64_MALLOC_HACK=2 if detected.", - "default": false - } - ] - }, { "name": "BOX64_LIBGL", "description": "Set the name for libGL.", @@ -1403,30 +1384,6 @@ } ] }, - { - "name": "BOX64_MALLOC_HACK", - "description": "Behaviour when hooking malloc operators.", - "category": "Compatibility", - "wine": false, - "configurator": true, - "options": [ - { - "key": "0", - "description": "Don't allow malloc operator to be redirected, rewriting code to use regular function.", - "default": true - }, - { - "key": "1", - "description": "Allow malloc operator to be redirected.", - "default": false - }, - { - "key": "2", - "description": "Like 0, but track special mmap/free (some redirected functions are inlined and cannot be redirected).", - "default": false - } - ] - }, { "name": "BOX64_MAXCPU", "description": "Maximum CPU cores exposed.", diff --git a/docs/gen/usage_cn.json b/docs/gen/usage_cn.json index 63f223b730..43ffa474f8 100644 --- a/docs/gen/usage_cn.json +++ b/docs/gen/usage_cn.json @@ -462,13 +462,6 @@ "XXXX:YYYY": "强制加载库 XXXX 和 YYYY。" } }, - "BOX64_LIBCEF": { - "description": "检测 libcef 并应用 BOX64_MALLOC_HACK 设置。", - "options": { - "0": "不执行任何操作。", - "1": "检测 libcef,并在检测到时应用 BOX64_MALLOC_HACK=2。" - } - }, "BOX64_LIBGL": { "description": "设置 libGL 的名称。", "options": { @@ -491,14 +484,6 @@ "3": "启用详细级别的 Box64 日志。" } }, - "BOX64_MALLOC_HACK": { - "description": "挂钩 malloc 操作符时的行为。", - "options": { - "0": "不允许重定向 malloc 操作符,重写代码使用常规函数。", - "1": "允许重定向 malloc 操作符。", - "2": "与 0 类似,但追踪特殊的 mmap/free(某些内联的重定向函数无法被重定向)。" - } - }, "BOX64_MAXCPU": { "description": "暴露的最大 CPU 核心数。", "options": { diff --git a/src/core.c b/src/core.c index f8722d7348..b121e1af0b 100644 --- a/src/core.c +++ b/src/core.c @@ -714,7 +714,7 @@ void setupTrace() #endif } #ifndef STATICBUILD -void endMallocHook(); +void guest_alloc_fini(void); #endif void finiPendingDLOpenedNoUnload(x64emu_t* emu); @@ -767,7 +767,7 @@ void endBox64() DecRefCount(&needed->libs[i], emu); // all done, free context #ifndef STATICBUILD - endMallocHook(); + guest_alloc_fini(); #endif SerializeAllMapping(); // to be safe FreeBox64Context(&my_context); @@ -819,7 +819,7 @@ extern char** environ; int initialize(int argc, const char **argv, char** env, x64emu_t** emulator, elfheader_t** elfheader, int exec) { #ifndef STATICBUILD - init_malloc_hook(); + init_guestalloc(); #endif init_auxval(argc, argv, environ?environ:env); // analogue to QEMU_VERSION in qemu-user-mode emulation diff --git a/src/custommem.c b/src/custommem.c index aff6e114fa..24de072c25 100644 --- a/src/custommem.c +++ b/src/custommem.c @@ -3,6 +3,10 @@ #include #include #include +#ifndef _WIN32 +#include +#include +#endif #include "os.h" #include "backtrace.h" @@ -86,6 +90,9 @@ static int inited = 0; void lockMmapMutex(void) { mutex_lock(&mutex_mmap); } void unlockMmapMutex(void) { mutex_unlock(&mutex_mmap); } +#define MEM_GUEST_BIT 64 +#define MEM_GUEST (MEM_GUEST_BIT | MEM_ALLOCATED) + typedef enum { MEM_UNUSED = 0, MEM_ALLOCATED = 1, @@ -94,7 +101,8 @@ typedef enum { MEM_BOX = 5, MEM_STACK = 9, MEM_EXTERNAL = 17, - MEM_ELF = 33 + MEM_ELF = 33, + MEM_GUEST_FLAG = MEM_GUEST } mem_flag_t; rbtree_t* mapallmem = NULL; static rbtree_t* blockstree = NULL; @@ -2777,6 +2785,16 @@ void setProtection_mmap(uintptr_t addr, size_t size, uint32_t prot) setProtection_generic(addr, size, prot, MEM_MMAP); } +void setProtection_guest(uintptr_t addr, size_t size, uint32_t prot) +{ + setProtection_generic(addr, size, prot, MEM_GUEST_FLAG); +} + +void setProtection_flag(uintptr_t addr, size_t size, uint32_t prot, uint32_t flag) +{ + setProtection_generic(addr, size, prot, (mem_flag_t)flag); +} + void setProtection_box(uintptr_t addr, size_t size, uint32_t prot) { setProtection_generic(addr, size, prot, MEM_BOX); @@ -2985,6 +3003,12 @@ int getMmapped(uintptr_t addr) return (rb_get(mapallmem, addr)&MEM_MMAP) == MEM_MMAP; } +int isGuestHeap(uintptr_t addr) +{ + uint32_t f = rb_get(mapallmem, addr); + return (f & MEM_GUEST_BIT) || (f == MEM_MMAP); +} + int getMAllocated(uintptr_t addr) { return (rb_get(mapallmem, addr)&MEM_ALLOCATED); @@ -3012,6 +3036,126 @@ static void catchup_brk_protection(void) } } +#ifndef _WIN32 +static uintptr_t guest_brk_base = 0; +static uintptr_t guest_brk_end = 0; +static uintptr_t guest_brk_cur = 0; +static int guest_brk_state = 0; +static volatile int guest_brk_initializing = 0; + +static void init_guest_brk(void) +{ + if(guest_brk_state) + return; + if(box64_is32bits) { + guest_brk_state = -1; + return; + } + if(__atomic_exchange_n(&guest_brk_initializing, 1, __ATOMIC_ACQ_REL)) { + while(!__atomic_load_n(&guest_brk_state, __ATOMIC_ACQUIRE)) + sched_yield(); + return; + } + uintptr_t size = (uintptr_t)1 << 30; + void* p = box_mmap(NULL, size, PROT_READ|PROT_WRITE, + MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0); + if(p==MAP_FAILED || !p) { + printf_log(LOG_INFO, "Warning: cannot reserve guest brk arena (%s), using host program break\n", strerror(errno)); + __atomic_store_n(&guest_brk_state, -1, __ATOMIC_RELEASE); + return; + } + guest_brk_base = (uintptr_t)p; + guest_brk_end = guest_brk_base + size; + guest_brk_cur = guest_brk_base; + setProtection_guest(guest_brk_base, size, PROT_READ|PROT_WRITE); + printf_log(LOG_DEBUG, "Guest brk arena %p-%p\n", (void*)guest_brk_base, (void*)guest_brk_end); + __atomic_store_n(&guest_brk_state, 1, __ATOMIC_RELEASE); +} + +int guest_brk_is_active(void) +{ + if(!guest_brk_state) + init_guest_brk(); + return guest_brk_state == 1; +} + +int guest_brk_set(uintptr_t addr, uintptr_t* ret) +{ + if(!guest_brk_is_active()) + return -1; + if(!addr) { + if(ret) *ret = __atomic_load_n(&guest_brk_cur, __ATOMIC_RELAXED); + return 0; + } + if(addr < guest_brk_base || addr > guest_brk_end) + return -1; + __atomic_store_n(&guest_brk_cur, addr, __ATOMIC_RELAXED); + if(ret) *ret = addr; + return 0; +} + +uintptr_t guest_syscall_brk(uintptr_t addr) +{ + uintptr_t ret; + if(guest_brk_is_active()) { + if(guest_brk_set(addr, &ret)) + return __atomic_load_n(&guest_brk_cur, __ATOMIC_RELAXED); + return ret; + } + return (uintptr_t)syscall(__NR_brk, addr); +} + +void* guest_sbrk(intptr_t inc) +{ + if(!guest_brk_is_active()) + return sbrk(inc); + uintptr_t old, newc; + do { + old = __atomic_load_n(&guest_brk_cur, __ATOMIC_RELAXED); + if(inc >= 0) { + if((uintptr_t)inc > guest_brk_end - old) { + errno = ENOMEM; + return (void*)-1; + } + newc = old + (uintptr_t)inc; + } else { + uintptr_t dec = (uintptr_t)(-(inc+1)) + 1; + if(old < guest_brk_base + dec) { + errno = ENOMEM; + return (void*)-1; + } + newc = old - dec; + } + } while(!__atomic_compare_exchange_n(&guest_brk_cur, &old, newc, 0, + __ATOMIC_RELAXED, __ATOMIC_RELAXED)); + return (void*)old; +} +#else +int guest_brk_is_active(void) +{ + return 0; +} + +int guest_brk_set(uintptr_t addr, uintptr_t* ret) +{ + (void)addr; + if(ret) + *ret = 0; + return -1; +} + +uintptr_t guest_syscall_brk(uintptr_t addr) +{ + return addr; +} + +void* guest_sbrk(intptr_t inc) +{ + (void)inc; + return (void*)-1; +} +#endif + void* find31bitBlockNearHint(void* hint_, size_t size, uintptr_t mask) { // first, check if program break as changed diff --git a/src/elfs/elfloader.c b/src/elfs/elfloader.c index 255e06c21c..bf2874fb41 100644 --- a/src/elfs/elfloader.c +++ b/src/elfs/elfloader.c @@ -48,11 +48,7 @@ #include "../tools/bridge_private.h" #include "x64tls.h" -#ifndef STATICBUILD -void startMallocHook(); -#else -void startMallocHook() {} -#endif +#include "guestalloc.h" void* my__IO_2_1_stderr_ = (void*)1; void* my__IO_2_1_stdin_ = (void*)2; @@ -1328,14 +1324,6 @@ void RefreshElfTLS(elfheader_t* h, x64emu_t* emu) } } -void MallocHookRun(elfheader_t* h) -{ - if (!h) - return; - if(h->malloc_hook_2) - startMallocHook(); -} - void MarkElfInitDone(elfheader_t* h) { if(h) @@ -1400,9 +1388,6 @@ void RunElfInit(elfheader_t* h, x64emu_t *emu) } } - if(h->malloc_hook_2) - startMallocHook(); - h->fini_done = 0; // can be fini'd now (in case it was re-inited) printf_dump(LOG_DEBUG, "All Init Done for %s\n", ElfName(h)); return; diff --git a/src/elfs/elfloader_private.h b/src/elfs/elfloader_private.h index 9458859228..ce0d048be5 100644 --- a/src/elfs/elfloader_private.h +++ b/src/elfs/elfloader_private.h @@ -127,7 +127,6 @@ typedef struct elfheader_s { int init_done; int fini_done; int refcnt; // ref count for the elf - int malloc_hook_2; // this elf hook malloc, hacking it int gnuunique; // set if contains some STB_GNU_UNIQUE binding, preventing dlclose to unload the lib int dynamic_patched; diff --git a/src/emu/entrypoint.c b/src/emu/entrypoint.c index d0670fa3a2..4ef0d0e9bb 100644 --- a/src/emu/entrypoint.c +++ b/src/emu/entrypoint.c @@ -4,6 +4,7 @@ #include "box64cpu.h" #include "box64cpu_util.h" #include "elfloader.h" +#include "guestalloc.h" #include "box32.h" #ifdef ANDROID @@ -35,8 +36,7 @@ EXPORT int32_t my___libc_start_main(x64emu_t* emu, int (*main) (int, char * *, c { (void)argc; (void)ubp_av; (void)fini; (void)rtld_fini; (void)stack_end; - if(my_context->elfs[0]) - MallocHookRun(my_context->elfs[0]); + guest_alloc_run(); if(init) { uintptr_t old_rsp = GetRSP(emu); @@ -114,8 +114,7 @@ int32_t EXPORT my32___libc_start_main(x64emu_t* emu, int *(main) (int, char * *, Push_32(emu, my_context->envv32); Push_32(emu, my_context->argv32); Push_32(emu, my_context->argc); - if(my_context->elfs[0]) - MallocHookRun(my_context->elfs[0]); + guest_alloc_run(); if(init) { PushExit_32(emu); R_EIP=to_ptrv(*init); diff --git a/src/emu/x64printer.c b/src/emu/x64printer.c index 131a6d7469..2ed48d2dfe 100644 --- a/src/emu/x64printer.c +++ b/src/emu/x64printer.c @@ -1538,6 +1538,8 @@ void x64Print(x64emu_t* emu, char* buff, size_t buffsz, const char* func, int ti snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIu32 ", %" PRIp ")", tid, *(void**)(R_RSP), func, (uint32_t)R_RDI, (void*)(R_RSP + 8)); } else if (w == pFEuA) { snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIu32 ", %" PRIp ")", tid, *(void**)(R_RSP), func, (uint32_t)R_RDI, (void*)R_RSI); + } else if (w == pFELL) { + snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIu64 ", %" PRIu64 ")", tid, *(void**)(R_RSP), func, (uintptr_t)R_RDI, (uintptr_t)R_RSI); } else if (w == pFEpi) { snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIp ", %" PRIi32 ")", tid, *(void**)(R_RSP), func, (void*)R_RDI, (int32_t)R_RSI); } else if (w == pFEpu) { @@ -2098,6 +2100,8 @@ void x64Print(x64emu_t* emu, char* buff, size_t buffsz, const char* func, int ti snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIp ", %" PRIu64 ", %" PRIi32 ")", tid, *(void**)(R_RSP), func, (void*)R_RDI, (uint64_t)R_RSI, (int32_t)R_RDX); } else if (w == iFEpLi) { snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIp ", %" PRIu64 ", %" PRIi32 ")", tid, *(void**)(R_RSP), func, (void*)R_RDI, (uintptr_t)R_RSI, (int32_t)R_RDX); + } else if (w == iFEpLL) { + snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIp ", %" PRIu64 ", %" PRIu64 ")", tid, *(void**)(R_RSP), func, (void*)R_RDI, (uintptr_t)R_RSI, (uintptr_t)R_RDX); } else if (w == iFEpLp) { snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIp ", %" PRIu64 ", %" PRIp ")", tid, *(void**)(R_RSP), func, (void*)R_RDI, (uintptr_t)R_RSI, (void*)R_RDX); } else if (w == iFEppi) { @@ -2832,6 +2836,8 @@ void x64Print(x64emu_t* emu, char* buff, size_t buffsz, const char* func, int ti snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIp ", %" PRIu32 ", %" PRIp ")", tid, *(void**)(R_RSP), func, (void*)R_RDI, (uint32_t)R_RSI, (void*)R_RDX); } else if (w == pFEplp) { snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIp ", %" PRIi64 ", %" PRIp ")", tid, *(void**)(R_RSP), func, (void*)R_RDI, (intptr_t)R_RSI, (void*)R_RDX); + } else if (w == pFEpLL) { + snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIp ", %" PRIu64 ", %" PRIu64 ")", tid, *(void**)(R_RSP), func, (void*)R_RDI, (uintptr_t)R_RSI, (uintptr_t)R_RDX); } else if (w == pFEppi) { snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIp ", %" PRIp ", %" PRIi32 ")", tid, *(void**)(R_RSP), func, (void*)R_RDI, (void*)R_RSI, (int32_t)R_RDX); } else if (w == pFEppu) { @@ -7797,10 +7803,14 @@ void x64Print(x64emu_t* emu, char* buff, size_t buffsz, const char* func, int ti snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIi32 ", %" PRIp ", %" PRIi32 ", %" PRIp ", %" PRIp ")", tid, *(void**)(R_RSP), func, (int32_t)R_RDI, (void*)R_RSI, (int32_t)R_RDX, (void*)R_RCX, (void*)R_R8); #endif #if !defined(STATICBUILD) + } else if (w == vFEpL) { + snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIp ", %" PRIu64 ")", tid, *(void**)(R_RSP), func, (void*)R_RDI, (uintptr_t)R_RSI); } else if (w == iFlip) { snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIi64 ", %" PRIi32 ", %" PRIp ")", tid, *(void**)(R_RSP), func, (intptr_t)R_RDI, (int32_t)R_RSI, (void*)R_RDX); } else if (w == iFLLi) { snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIu64 ", %" PRIu64 ", %" PRIi32 ")", tid, *(void**)(R_RSP), func, (uintptr_t)R_RDI, (uintptr_t)R_RSI, (int32_t)R_RDX); + } else if (w == vFEpLL) { + snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIp ", %" PRIu64 ", %" PRIu64 ")", tid, *(void**)(R_RSP), func, (void*)R_RDI, (uintptr_t)R_RSI, (uintptr_t)R_RDX); } else if (w == iFLLii) { snprintf(buff, buffsz, "%04d|%p: Calling %s(%" PRIu64 ", %" PRIu64 ", %" PRIi32 ", %" PRIi32 ")", tid, *(void**)(R_RSP), func, (uintptr_t)R_RDI, (uintptr_t)R_RSI, (int32_t)R_RDX, (int32_t)R_RCX); } else if (w == WFpLLu) { diff --git a/src/emu/x64syscall.c b/src/emu/x64syscall.c index 5c0f54a7b5..fb1eb7ef71 100644 --- a/src/emu/x64syscall.c +++ b/src/emu/x64syscall.c @@ -43,6 +43,7 @@ #include "x64int_private.h" #include "syscall_user_dispatch.h" #include "cpumask.h" +#include "custommem.h" typedef struct x64_sigaction_s x64_sigaction_t; typedef struct x64_stack_s x64_stack_t; @@ -111,7 +112,6 @@ static const scwrap_t syscallwrap[] = { //[9] = {__NR_mmap, 6}, // wrapped to track mmap //[10] = {__NR_mprotect, 3}, // same //[11] = {__NR_munmap, 2}, // same - [12] = {__NR_brk, 1}, //[13] = {__NR_rt_sigaction, 4}, // wrapped to use my_ version [14] = {__NR_rt_sigprocmask, 4}, [15] = {__NR_rt_sigreturn, 1}, @@ -672,6 +672,9 @@ void EXPORT x64Syscall_linux(x64emu_t *emu) if(S_RAX==-1) S_RAX = -errno; break; + case 12: + R_RAX = guest_syscall_brk(R_RDI); + break; case 13: // sys_rt_sigaction #if 1 S_RAX = my_syscall_rt_sigaction(emu, S_EDI, (const x64_sigaction_restorer_t *)R_RSI, (x64_sigaction_restorer_t *)R_RDX, (size_t)R_R10); @@ -1158,6 +1161,8 @@ long EXPORT my_syscall(x64emu_t *emu) return my_mprotect(emu, (void*)R_RSI, R_RDX, S_ECX); case 11: // sys_munmap return my_munmap(emu, (void*)R_RSI, R_RDX); + case 12: + return (intptr_t)guest_syscall_brk(R_RSI); case 13: // sys_rt_sigaction #if 1 return my_syscall_rt_sigaction(emu, S_ESI, (const x64_sigaction_restorer_t *)R_RDX, (x64_sigaction_restorer_t *)R_RCX, (size_t)R_R8); diff --git a/src/guestalloc.c b/src/guestalloc.c new file mode 100644 index 0000000000..35dc8608f3 --- /dev/null +++ b/src/guestalloc.c @@ -0,0 +1,766 @@ +// SPDX-License-Identifier: MIT +#include +#include +#include +#include +#include +#include +#include +#include + +#include "box64context.h" +#include "debug.h" +#include "callback.h" +#include "librarian.h" +#include "elfs/elfloader_private.h" +#include "elfloader.h" +#include "custommem.h" +#include "env.h" +#include "bridge.h" +#include "alternate.h" +#include "guestalloc.h" +#ifdef BOX32 +#include "box32.h" +#endif + +extern int box64_quit; + +#if defined(BOX32) && defined(PPC64LE) +static void checkOggHookedSymbols32(elfheader_t* h); +#endif + +size_t (*box_malloc_usable_size)(void*) = NULL; + +#ifndef ANDROID + +char* box_strdup(const char* s) +{ + char* ret = box_calloc(1, strlen(s)+1); + memcpy(ret, s, strlen(s)); + return ret; +} + +char* box_realpath(const char* path, char* ret) +{ + if(ret) + return realpath(path, ret); +#ifdef PATH_MAX + size_t path_max = PATH_MAX; +#else + size_t path_max = pathconf(path, _PC_PATH_MAX); + if (path_max <= 0) + path_max = 4096; +#endif + char tmp[path_max]; + char* p = realpath(path, tmp); + if(!p) + return NULL; + return box_strdup(tmp); +} + +static size_t pot(size_t l) +{ + size_t ret = 0; + while (l>(1u<maplib) + return 0; + lockLibLoader(); + found = GetGlobalSymbolStartEnd(my_context->maplib, name, &start, &end, (void*)1, -1, NULL, 0, &elfsym); + unlockLibLoader(); + if(!found || !start || !elfsym) + return 0; + return start; +} + +void guest_alloc_refresh(void) +{ + if(box64_is32bits || box64_quit) + return; + if(!my_context || !my_context->maplib) + return; + uintptr_t m = ga_resolve_guest("malloc"); + uintptr_t f = ga_resolve_guest("free"); + if(!m || !f) { + if(ga_active) + printf_log(LOG_INFO, "Guest allocator: no globally bound override anymore, using host allocator\n"); + ga_active = 0; + ga_malloc_f = ga_free_f = ga_calloc_f = ga_realloc_f = 0; + ga_memalign_f = ga_posix_memalign_f = ga_aligned_alloc_f = ga_usable_f = 0; + return; + } + int changed = (!ga_active || ga_malloc_f!=m || ga_free_f!=f); + ga_malloc_f = m; + ga_free_f = f; + ga_calloc_f = ga_resolve_guest("calloc"); + ga_realloc_f = ga_resolve_guest("realloc"); + ga_memalign_f = ga_resolve_guest("memalign"); + ga_posix_memalign_f = ga_resolve_guest("posix_memalign"); + ga_aligned_alloc_f = ga_resolve_guest("aligned_alloc"); + ga_usable_f = ga_resolve_guest("malloc_usable_size"); + ga_active = 1; + if(changed) + printf_log(LOG_INFO, "Guest allocator detected (malloc=%p, free=%p), guest heap enabled\n", + (void*)m, (void*)f); +} + +int guest_alloc_is_active(void) +{ + return ga_active; +} + +int guest_alloc_owns(const void* p) +{ + return ga_active && p && isGuestHeap((uintptr_t)p); +} + +static int ga_enter(void) +{ + if(ga_in_guest) { + printf_log(LOG_INFO, "Guest allocator: recursive allocation call, refusing\n"); + return 0; + } + ga_in_guest = 1; + return 1; +} + +static void ga_leave(void) +{ + ga_in_guest = 0; +} + +static void* ga_call_malloc(size_t n) +{ + if(!ga_enter()) + return NULL; + void* ret = (void*)RunFunctionFmtNoAlt(ga_malloc_f, "L", (uint64_t)n); + ga_leave(); + return ret; +} + +static void* ga_call_calloc(size_t n, size_t s) +{ + if(!ga_enter()) + return NULL; + void* ret = (void*)RunFunctionFmtNoAlt(ga_calloc_f, "LL", (uint64_t)n, (uint64_t)s); + ga_leave(); + return ret; +} + +static void* ga_call_realloc(void* p, size_t n) +{ + if(!ga_enter()) + return NULL; + void* ret = (void*)RunFunctionFmtNoAlt(ga_realloc_f, "pL", p, (uint64_t)n); + ga_leave(); + return ret; +} + +static void ga_call_free(void* p) +{ + if(!ga_enter()) + return; + RunFunctionFmtNoAlt(ga_free_f, "p", p); + ga_leave(); +} + +static void* ga_call_memalign(size_t align, size_t size) +{ + if(!ga_enter()) + return NULL; + void* ret = (void*)RunFunctionFmtNoAlt(ga_memalign_f, "LL", (uint64_t)align, (uint64_t)size); + ga_leave(); + return ret; +} + +static int ga_call_posix_memalign(void** memptr, size_t align, size_t size) +{ + if(!ga_enter()) + return ENOMEM; + int ret = (int)RunFunctionFmtNoAlt(ga_posix_memalign_f, "pLL", memptr, (uint64_t)align, (uint64_t)size); + ga_leave(); + return ret; +} + +static void* ga_call_aligned_alloc(size_t align, size_t size) +{ + if(!ga_enter()) + return NULL; + void* ret = (void*)RunFunctionFmtNoAlt(ga_aligned_alloc_f, "LL", (uint64_t)align, (uint64_t)size); + ga_leave(); + return ret; +} + +static size_t ga_call_usable(void* p) +{ + if(!ga_enter()) + return 0; + size_t ret = (size_t)RunFunctionFmtNoAlt(ga_usable_f, "p", p); + ga_leave(); + return ret; +} + +static size_t ga_ptr_size(void* p) +{ + if(ga_usable_f && thread_emu_ready()) + return ga_call_usable(p); + uintptr_t a = (uintptr_t)p; + size_t s = 0; + while(s < ((size_t)1<<30) && (getProtection(a+s) & PROT_READ)) + s += box64_pagesize; + return s; +} + +void* guest_malloc(size_t size) +{ + if(!ga_active) + return box_malloc(size); + if(!thread_emu_ready()) { + printf_log(LOG_INFO, "Guest allocator: malloc(%zu) requested before guest TLS is ready, failing\n", size); + return NULL; + } + return ga_call_malloc(size); +} + +void* guest_calloc(size_t n, size_t size) +{ + if(!ga_active) + return box_calloc(n, size); + if(n && size > (size_t)-1 / n) { + errno = ENOMEM; + return NULL; + } + if(!thread_emu_ready()) { + printf_log(LOG_INFO, "Guest allocator: calloc(%zu,%zu) requested before guest TLS is ready, failing\n", n, size); + return NULL; + } + if(ga_calloc_f) + return ga_call_calloc(n, size); + void* p = ga_call_malloc(n*size); + if(p) + memset(p, 0, n*size); + return p; +} + +void* guest_realloc(void* p, size_t size) +{ + if(!ga_active) + return box_realloc(p, size); + if(!p) + return guest_malloc(size); + if(!isGuestHeap((uintptr_t)p)) + return box_realloc(p, size); + if(!thread_emu_ready()) { + printf_log(LOG_INFO, "Guest allocator: realloc(%p) requested before guest TLS is ready, failing\n", p); + return NULL; + } + if(ga_realloc_f) + return ga_call_realloc(p, size); + size_t old = ga_ptr_size(p); + if(old > size) + old = size; + void* ret = guest_malloc(size); + if(!ret) + return NULL; + if(old) + memcpy(ret, p, old); + guest_free(p); + return ret; +} + +void guest_free(void* p) +{ + if(!p) + return; + if(!ga_active || !isGuestHeap((uintptr_t)p)) { + box_free(p); + return; + } + if(!thread_emu_ready() || !ga_free_f) { + printf_log(LOG_INFO, "Guest allocator: cannot free %p before guest TLS is ready, leaking\n", p); + return; + } + ga_call_free(p); +} + +void* guest_memalign(size_t align, size_t size) +{ + if(!ga_active) + return box_memalign(align, size); + if(!thread_emu_ready()) { + printf_log(LOG_INFO, "Guest allocator: aligned allocation requested before guest TLS is ready, failing\n"); + return NULL; + } + if(ga_memalign_f) + return ga_call_memalign(align, size); + if(ga_posix_memalign_f) { + void* ret = NULL; + if(ga_call_posix_memalign(&ret, align, size)==0) + return ret; + return NULL; + } + if(ga_aligned_alloc_f) + return ga_call_aligned_alloc(align, size); + printf_log(LOG_INFO, "Guest allocator: no aligned allocation primitive for align=%zu size=%zu\n", align, size); + errno = ENOMEM; + return NULL; +} + +int guest_posix_memalign(void** memptr, size_t align, size_t size) +{ + if(!ga_active) { + if(!memptr) + return EINVAL; + if((align%sizeof(void*)) || pot(align)!=align) + return EINVAL; + void* ret = box_memalign(align, size); + if(!ret) + return ENOMEM; + *memptr = ret; + return 0; + } + if(!memptr) + return EINVAL; + if(!thread_emu_ready()) { + printf_log(LOG_INFO, "Guest allocator: posix_memalign requested before guest TLS is ready, failing\n"); + return ENOMEM; + } + if(ga_posix_memalign_f) + return ga_call_posix_memalign(memptr, align, size); + if(ga_memalign_f) { + void* ret = ga_call_memalign(align, size); + if(!ret) + return ENOMEM; + *memptr = ret; + return 0; + } + if(ga_aligned_alloc_f) { + void* ret = ga_call_aligned_alloc(align, size); + if(!ret) + return ENOMEM; + *memptr = ret; + return 0; + } + return ENOMEM; +} + +size_t guest_usable_size(void* p) +{ + if(!p) + return 0; + if(!ga_active || !isGuestHeap((uintptr_t)p)) + return box_malloc_usable_size ? box_malloc_usable_size(p) : 0; + if(ga_usable_f && thread_emu_ready()) + return ga_call_usable(p); + return 0; +} + +EXPORT void* malloc(size_t l) +{ + return actual_calloc(1, l); +} + +EXPORT void free(void* p) +{ + if(p && ga_active && isGuestHeap((uintptr_t)p)) { + guest_free(p); + return; + } + actual_free(p); +} + +EXPORT void* calloc(size_t n, size_t s) +{ + return actual_calloc(n, s); +} + +EXPORT void* realloc(void* p, size_t s) +{ + if(p && ga_active && isGuestHeap((uintptr_t)p)) + return guest_realloc(p, s); + return actual_realloc(p, s); +} + +EXPORT void* memalign(size_t align, size_t size) +{ + if(box64_is32bits && align==4) + align = sizeof(void*); + return actual_memalign(align, size); +} + +EXPORT void* aligned_alloc(size_t align, size_t size) +{ + if(box64_is32bits && align==4) + return memalign(align, size); + return actual_memalign(align, size); +} + +EXPORT int posix_memalign(void** p, size_t align, size_t size) +{ + if(box64_is32bits && align==4) + align = sizeof(void*); + if((align%sizeof(void*)) || (pot(align)!=align)) + return EINVAL; + void* ret = actual_memalign(align, size); + if(!ret) + return ENOMEM; + *p = ret; + return 0; +} + +EXPORT void* valloc(size_t size) +{ + return actual_memalign(box64_pagesize, size); +} + +EXPORT void* pvalloc(size_t size) +{ + return actual_memalign(box64_pagesize, ALIGN(size)); +} + +EXPORT void cfree(void* p) +{ + if(p && ga_active && isGuestHeap((uintptr_t)p)) { + guest_free(p); + return; + } + actual_free(p); +} + +EXPORT size_t malloc_usable_size(void* p) +{ + if(p && ga_active && isGuestHeap((uintptr_t)p)) + return guest_usable_size(p); + return actual_malloc_usable_size(p); +} + +static int elf_defines_allocator(elfheader_t* h) +{ + if(!h || !h->DynSym._64 || !h->DynStr) + return 0; + for(size_t i=0; inumDynSym; ++i) { + int type = ELF64_ST_TYPE(h->DynSym._64[i].st_info); + int bind = ELF64_ST_BIND(h->DynSym._64[i].st_info); + int vis = h->DynSym._64[i].st_other & 0x3; + if(type != STT_FUNC) + continue; + if(h->DynSym._64[i].st_shndx == 0 || h->DynSym._64[i].st_shndx > 65521) + continue; + if(bind == STB_LOCAL) + continue; + if(vis != STV_DEFAULT && vis != STV_PROTECTED) + continue; + const char* n = h->DynStr + h->DynSym._64[i].st_name; + if(!strcmp(n, "malloc") || !strcmp(n, "free") || !strcmp(n, "calloc") || !strcmp(n, "realloc")) + return 1; + } + return 0; +} + +void checkHookedSymbols(elfheader_t* h) +{ + if(box64_is32bits) { +#if defined(BOX32) && defined(PPC64LE) + checkOggHookedSymbols32(h); +#else + (void)h; +#endif + return; + } + if(ga_active || elf_defines_allocator(h)) + guest_alloc_refresh(); +} + +void init_guestalloc(void) +{ + box_malloc_usable_size = dlsym(RTLD_NEXT, "malloc_usable_size"); +} + +void guest_alloc_run(void) +{ + guest_alloc_refresh(); +} + +void guest_alloc_fini(void) +{ + ga_active = 0; + ga_malloc_f = ga_free_f = ga_calloc_f = ga_realloc_f = 0; + ga_memalign_f = ga_posix_memalign_f = ga_aligned_alloc_f = ga_usable_f = 0; +} + +void* my_tbb_malloc_proxy(unsigned long size); + +EXPORT void* my__Znwm(size_t size) +{ + return my_tbb_malloc_proxy(size); +} +EXPORT void* my__ZnwmRKSt9nothrow_t(size_t size, void* nt) +{ + (void)nt; + return my_tbb_malloc_proxy(size); +} +EXPORT void* my__Znam(size_t size) +{ + return my_tbb_malloc_proxy(size); +} +EXPORT void* my__ZnamRKSt9nothrow_t(size_t size, void* nt) +{ + (void)nt; + return my_tbb_malloc_proxy(size); +} +EXPORT void my__ZdlPv(void* p) +{ + free(p); +} +EXPORT void my__ZdlPvRKSt9nothrow_t(void* p, void* nt) +{ + (void)nt; + free(p); +} +EXPORT void my__ZdaPv(void* p) +{ + free(p); +} +EXPORT void my__ZdaPvRKSt9nothrow_t(void* p, void* nt) +{ + (void)nt; + free(p); +} + +static void* ga_original_allocator(const char* name) +{ + if(!strcmp(name, "malloc") || !strcmp(name, "__libc_malloc")) return (void*)malloc; + if(!strcmp(name, "free") || !strcmp(name, "__libc_free")) return (void*)free; + if(!strcmp(name, "calloc") || !strcmp(name, "__libc_calloc")) return (void*)calloc; + if(!strcmp(name, "realloc") || !strcmp(name, "__libc_realloc")) return (void*)realloc; + if(!strcmp(name, "memalign") || !strcmp(name, "__libc_memalign")) return (void*)memalign; + if(!strcmp(name, "posix_memalign")) return (void*)posix_memalign; + if(!strcmp(name, "aligned_alloc")) return (void*)aligned_alloc; + if(!strcmp(name, "valloc") || !strcmp(name, "__libc_valloc")) return (void*)valloc; + if(!strcmp(name, "pvalloc") || !strcmp(name, "__libc_pvalloc")) return (void*)pvalloc; + if(!strcmp(name, "cfree") || !strcmp(name, "__libc_cfree")) return (void*)cfree; + if(!strcmp(name, "malloc_usable_size")) return (void*)malloc_usable_size; + return NULL; +} + +EXPORT int my___TBB_internal_find_original_malloc(int n, char* names[], void* ptr[]) +{ + int ret = 1; + for(int i=0; i 0 && og) { + ogg_page_32_t* page = (ogg_page_32_t*)og; + + if(ogg_page_header_copy) { actual_free(ogg_page_header_copy); ogg_page_header_copy = NULL; } + if(ogg_page_body_copy) { actual_free(ogg_page_body_copy); ogg_page_body_copy = NULL; } + + if(page->header && page->header_len > 0) { + ogg_page_header_copy = actual_malloc(page->header_len); + if(ogg_page_header_copy) { + memcpy(ogg_page_header_copy, from_ptrv(page->header), page->header_len); + page->header = to_ptrv(ogg_page_header_copy); + } + } + + if(page->body && page->body_len > 0) { + ogg_page_body_copy = actual_malloc(page->body_len); + if(ogg_page_body_copy) { + memcpy(ogg_page_body_copy, from_ptrv(page->body), page->body_len); + page->body = to_ptrv(ogg_page_body_copy); + } + } + } + + return ret; +} + +static void checkOggHookedSymbols32(elfheader_t* h) +{ + if(!h->numSymTab || !h->SymTab._32) + return; + + for (size_t i = 0; i < h->numSymTab; ++i) { + int type = ELF32_ST_TYPE(h->SymTab._32[i].st_info); + if(type == STT_FUNC && h->SymTab._32[i].st_shndx != 0 + && h->SymTab._32[i].st_shndx <= 65521) { + const char* symname = h->StrTab + h->SymTab._32[i].st_name; + if(!strcmp(symname, "ogg_sync_pageout")) { + uintptr_t offs = h->SymTab._32[i].st_value + h->delta; + if(!real_ogg_sync_pageout_32) { + real_ogg_sync_pageout_32 = offs; + uintptr_t alt = AddCheckBridge(my_context->system, iFpp_32, (void*)my_ogg_sync_pageout_32, 0, "my_ogg_sync_pageout_32"); + addAlternate((void*)offs, (void*)alt); + printf_log(LOG_INFO, "Hooked statically-linked ogg_sync_pageout at %p in %s (ogg realloc fix)\n", (void*)offs, ElfName(h)); + } + break; + } + } + } +} +#endif + +#else + +void init_guestalloc(void) {} +void guest_alloc_refresh(void) {} +void guest_alloc_run(void) {} +void guest_alloc_fini(void) {} +int guest_alloc_is_active(void) { return 0; } +int guest_alloc_owns(const void* p) { (void)p; return 0; } +void* guest_malloc(size_t size) { return malloc(size); } +void* guest_calloc(size_t n, size_t size) { return calloc(n, size); } +void* guest_realloc(void* p, size_t size) { return realloc(p, size); } +void guest_free(void* p) { free(p); } +void* guest_memalign(size_t align, size_t size) { return memalign(align, size); } +int guest_posix_memalign(void** memptr, size_t align, size_t size) { return posix_memalign(memptr, align, size); } +size_t guest_usable_size(void* p) { return malloc_usable_size(p); } +void checkHookedSymbols(elfheader_t* h) { (void)h; } + +#endif diff --git a/src/include/box64context.h b/src/include/box64context.h index 49c0659046..0374839031 100644 --- a/src/include/box64context.h +++ b/src/include/box64context.h @@ -302,6 +302,7 @@ void thread_set_emu(x64emu_t* emu); void thread_forget_emu(); x64emu_t* thread_get_emu_no_create(void); x64emu_t* thread_get_emu(void); +int thread_emu_ready(void); // relock the muxtex that were unlocked void relockMutex(int locks); diff --git a/src/include/custommem.h b/src/include/custommem.h index 0403bc5416..3a63d8abe5 100644 --- a/src/include/custommem.h +++ b/src/include/custommem.h @@ -124,6 +124,8 @@ uintptr_t getJumpAddress64(uintptr_t addr); void updateProtection(uintptr_t addr, size_t size, uint32_t prot); void setProtection(uintptr_t addr, size_t size, uint32_t prot); void setProtection_mmap(uintptr_t addr, size_t size, uint32_t prot); +void setProtection_guest(uintptr_t addr, size_t size, uint32_t prot); +void setProtection_flag(uintptr_t addr, size_t size, uint32_t prot, uint32_t flag); void setProtection_box(uintptr_t addr, size_t size, uint32_t prot); void setProtection_stack(uintptr_t addr, size_t size, uint32_t prot); void setProtection_elf(uintptr_t addr, size_t size, uint32_t prot); @@ -136,8 +138,14 @@ void updateDBHostProtectionForGuestRange(uintptr_t addr, size_t size); uint32_t getProtection(uintptr_t addr); uint32_t getProtection_fast(uintptr_t addr); int getMmapped(uintptr_t addr); +int isGuestHeap(uintptr_t addr); int getMAllocated(uintptr_t addr); int memExist(uintptr_t addr); + +int guest_brk_is_active(void); +int guest_brk_set(uintptr_t addr, uintptr_t* ret); +uintptr_t guest_syscall_brk(uintptr_t addr); +void* guest_sbrk(intptr_t inc); void loadProtectionFromMap(void); #ifdef DYNAREC void protectDB(uintptr_t addr, size_t size); diff --git a/src/include/debug.h b/src/include/debug.h index f6b3b8673d..d571cf1d55 100644 --- a/src/include/debug.h +++ b/src/include/debug.h @@ -109,7 +109,7 @@ extern sysinfo_t box64_sysinfo; #ifndef _WIN32 // TODO: better wow64 support? #ifndef STATICBUILD -void init_malloc_hook(void); +void init_guestalloc(void); #endif #if defined(ANDROID) || defined(STATICBUILD) #define box_malloc malloc diff --git a/src/include/elfloader.h b/src/include/elfloader.h index ef564b7222..a70763d413 100644 --- a/src/include/elfloader.h +++ b/src/include/elfloader.h @@ -64,7 +64,6 @@ void RunElfInit(elfheader_t* h, x64emu_t *emu); void RunElfFini(elfheader_t* h, x64emu_t *emu); void RunDeferredElfInit(x64emu_t *emu); void MarkElfInitDone(elfheader_t* h); -void MallocHookRun(elfheader_t* h); void* GetBaseAddress(elfheader_t* h); void* GetElfDelta(elfheader_t* h); uint32_t GetBaseSize(elfheader_t* h); diff --git a/src/include/env.h b/src/include/env.h index 16f7626811..4e39006592 100644 --- a/src/include/env.h +++ b/src/include/env.h @@ -99,11 +99,9 @@ extern char* ftrace_name; BOOLEAN(BOX64_JVM, jvm, 1, 0, 0) \ STRING(BOX64_LD_LIBRARY_PATH, ld_library_path, 0, 0) \ STRING(BOX64_LD_PRELOAD, ld_preload, 0, 0) \ - BOOLEAN(BOX64_LIBCEF, libcef, 0, 0, 0) \ STRING(BOX64_LIBGL, libgl, 0, 0) \ ADDRESS(BOX64_LOAD_ADDR, load_addr, 0, 0) \ INTEGER(BOX64_LOG, log, DEFAULT_LOG_LEVEL, 0, 3, 1, 0) \ - INTEGER(BOX64_MALLOC_HACK, malloc_hack, 0, 0, 2, 0, 0) \ INTEGER(BOX64_MAXCPU, new_maxcpu, 0, 0, 100, 1, 0) \ INTEGER(BOX64_SKIPCPU, new_skipcpu, 0, 0, 100, 1, 0) \ BOOLEAN(BOX64_MMAP32, mmap32, 1, 0, 0) \ diff --git a/src/include/guestalloc.h b/src/include/guestalloc.h new file mode 100644 index 0000000000..a01c22705a --- /dev/null +++ b/src/include/guestalloc.h @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: MIT +#ifndef __GUESTALLOC_H__ +#define __GUESTALLOC_H__ + +#include +#include + +typedef struct elfheader_s elfheader_t; + +#ifdef STATICBUILD +#include +#include +static inline void init_guestalloc(void) {} +static inline void guest_alloc_refresh(void) {} +static inline void guest_alloc_run(void) {} +static inline void guest_alloc_fini(void) {} +static inline int guest_alloc_is_active(void) { return 0; } +static inline int guest_alloc_owns(const void* p) { (void)p; return 0; } +static inline void* guest_malloc(size_t size) { return malloc(size); } +static inline void* guest_calloc(size_t n, size_t size) { return calloc(n, size); } +static inline void* guest_realloc(void* p, size_t size) { return realloc(p, size); } +static inline void guest_free(void* p) { free(p); } +static inline void* guest_memalign(size_t align, size_t size) { return memalign(align, size); } +static inline int guest_posix_memalign(void** p, size_t align, size_t size) { return posix_memalign(p, align, size); } +static inline size_t guest_usable_size(void* p) { return malloc_usable_size(p); } +static inline void checkHookedSymbols(elfheader_t* h) { (void)h; } +#else + +void init_guestalloc(void); +void guest_alloc_refresh(void); +void guest_alloc_run(void); +void guest_alloc_fini(void); +int guest_alloc_is_active(void); +int guest_alloc_owns(const void* p); +void* guest_malloc(size_t size); +void* guest_calloc(size_t n, size_t size); +void* guest_realloc(void* p, size_t size); +void guest_free(void* p); +void* guest_memalign(size_t align, size_t size); +int guest_posix_memalign(void** memptr, size_t align, size_t size); +size_t guest_usable_size(void* p); + +void checkHookedSymbols(elfheader_t* h); + +#endif + +#endif diff --git a/src/librarian/library.c b/src/librarian/library.c index 45676eb4c9..b26242c7d1 100644 --- a/src/librarian/library.c +++ b/src/librarian/library.c @@ -395,11 +395,6 @@ static int loadEmulatedLib(const char* libname, library_t *lib, box64context_t* SET_BOX64ENV(sse42, 0); env_changed = 1; } - if(libname && BOX64ENV(libcef) && strstr(libname, "libcef.so")) { - printf_dump(LOG_INFO, "libcef detected, using malloc_hack=2\n"); - SET_BOX64ENV(malloc_hack, 2); - env_changed = 1; - } if (env_changed) PrintEnvVariables(&box64env, LOG_INFO); return 1; } diff --git a/src/libtools/threads.c b/src/libtools/threads.c index 08317ed40c..c84b6efcfd 100644 --- a/src/libtools/threads.c +++ b/src/libtools/threads.c @@ -260,6 +260,11 @@ void thread_set_emu(x64emu_t* emu) pthread_setspecific(thread_key, et); } +int thread_emu_ready(void) +{ + return thread_key_ready; +} + x64emu_t* thread_get_emu_no_create(void) { if(!thread_key_ready) return NULL; diff --git a/src/mallochook.c b/src/mallochook.c deleted file mode 100644 index ad8d28646e..0000000000 --- a/src/mallochook.c +++ /dev/null @@ -1,1101 +0,0 @@ -// SPDX-License-Identifier: MIT -#include -#include -#include -#include -#include -#include -#include -#include - -#include "box64context.h" -#include "debug.h" -#include "callback.h" -#include "librarian.h" -#include "elfs/elfloader_private.h" -#include "custommem.h" -#include "symbols.h" -#include "alternate.h" -#ifdef BOX32 -#include "box32.h" -#endif - -/* - This file here is for handling overriding of malloc functions - - Libraries like tcmalloc overrides all malloc/free/new/delete function and implement a custom version. - Problem is, box64 is already loaded in memoery, and already using system libc malloc for many of it's things - before those lib are even loaded in memory. - Also, those override function can be define in the main executable, or in a lib loaded directly by the exectable - or even in a lib loaded later using dlsym. - - The 2 different strategies to handle this can be defined as "Embrace" and "Exterminate" (as it cannot simply be ignored, - or you end up with mixing free/realloc from one lib and malloc/free from libc) - - In the "Embrace" strategy, the overridden malloc function are taken into account as soon as possible, and are used for all loaded lib, including native - In the "Exterminate" strategy, the overridden malloc function are erased, and replaced with libc malloc as soon as they are defined. - - The advantage of "Embrace" is that the system will run the function it supposed to be using, and potential side-effect and staticaly linked functions - will run as intended. - The downside of "Embrace" is that is makes it impossible to load a library with dlsym that override malloc function, especialy - if it loads natively wrapped function - - The advantage of "Exterminate" is that you wont be emulating basic malloc and friend, wich are used extensively in every program. Also, loading lib - with dlopen will not a be a problem. - The downside of "Exterminate" is that side effect are less well controled. Staticaly linked stuff and anonymous symbols might put this strategy in trouble. - - This is the Exterminate strategy implementation -*/ -#ifndef ANDROID -#include "bridge.h" -#include "wrapper.h" - -#define SUPER() \ -GO(malloc, pFL) \ -GO(free, vFp) \ -GO(calloc, pFLL) \ -GO(realloc, pFpL) \ -GO(aligned_alloc, pFLL) \ -GO(memalign, pFLL) \ -GO(posix_memalign, iFpLL) \ -GO(pvalloc, pFL) \ -GO(valloc, pFL) \ -GO(cfree, vFp) \ -GO(malloc_usable_size, LFp) \ -GO2(_Znwm, pFL) \ -GO2(_ZnwmRKSt9nothrow_t, pFLp) \ -GO2(_Znam, pFL) \ -GO2(_ZnamRKSt9nothrow_t, pFLp) \ -GO2(_ZdaPv, vFp) \ -GO2(_ZdaPvm, vFpL) \ -GO2(_ZdaPvmSt11align_val_t, vFpLL) \ -GO2(_ZdlPv, vFp) \ -GO2(_ZdlPvm, vFpL) \ -GO2(_ZnwmSt11align_val_t, pFLL) \ -GO2(_ZnwmSt11align_val_tRKSt9nothrow_t, pFLLp) \ -GO2(_ZnamSt11align_val_t, pFLL) \ -GO2(_ZnamSt11align_val_tRKSt9nothrow_t, pFLLp) \ -GO2(_ZdlPvRKSt9nothrow_t, vFpp) \ -GO2(_ZdaPvSt11align_val_tRKSt9nothrow_t, vFpLp) \ -GO2(_ZdlPvmSt11align_val_t, vFpLL) \ -GO2(_ZdaPvRKSt9nothrow_t, vFpp) \ -GO2(_ZdaPvSt11align_val_t, vFpL) \ -GO2(_ZdlPvSt11align_val_t, vFpL) \ -GO2(_ZdlPvSt11align_val_tRKSt9nothrow_t, vFpLp) \ -GO2(tc_calloc, pFLL) \ -GO2(tc_cfree, vFp) \ -GO2(tc_delete, vFp) \ -GO2(tc_deletearray, vFp) \ -GO2(tc_deletearray_nothrow, vFpp) \ -GO2(tc_delete_nothrow, vFpp) \ -GO2(tc_free, vFp) \ -GO2(tc_malloc, pFL) \ -GO2(tc_malloc_size, LFp) \ -GO2(tc_new, pFL) \ -GO2(tc_new_nothrow, pFLp) \ -GO2(tc_newarray, pFL) \ -GO2(tc_newarray_nothrow, pFLp) \ -GO2(tc_pvalloc, pFL) \ -GO2(tc_valloc, pFL) \ -GO2(tc_memalign, pFLL) \ -GO2(tc_malloc_skip_new_handler_weak, pFL) \ -GO2(tc_mallocopt, iFii) \ -GO2(tc_malloc_stats, vFv) \ -GO2(tc_malloc_skip_new_handler, pFL) \ -GO2(tc_mallinfo, pFp) \ -GO2(tc_posix_memalign, iFpLL) \ -GO2(tc_realloc, pFpL) \ -GO2(safer_scalable_aligned_realloc, pFpLLp) \ -GO2(safer_scalable_free, vFpp) \ -GO2(safer_scalable_msize, LFpp) \ -GO2(safer_scalable_realloc, pFpLp) \ -GO2(scalable_aligned_free, vFp) \ -GO2(scalable_aligned_malloc, pFLL) \ -GO2(scalable_msize, LFp) \ - - -//GO2(tc_set_new_mode, iFi) -//GO2(tc_version, iFi) - -typedef void (vFv_t) (void); -typedef int (iFv_t) (void); -typedef int (iFi_t) (int); -typedef void* (*pFL_t) (size_t); -typedef void* (*pFLp_t) (size_t, void* p); -typedef void (*vFp_t) (void*); -typedef void* (*pFp_t) (void*); -typedef size_t(*LFp_t) (void*); -typedef int (*iFii_t) (int, int); -typedef void (*vFpp_t) (void*, void*); -typedef size_t(*LFpp_t) (void*, void*); -typedef void (*vFpL_t) (void*, size_t); -typedef void* (*pFLL_t) (size_t, size_t); -typedef void* (*pFLLp_t)(size_t, size_t, void* p); -typedef void (*vFpLp_t)(void*, size_t, void*); -typedef void* (*pFpLp_t)(void*, size_t, void*); -typedef void (*vFpLL_t)(void*, size_t, size_t); -typedef void* (*pFpLLp_t)(void*, size_t, size_t, void*); - -size_t(*box_malloc_usable_size)(void*) = NULL; - -int GetTID(); -uint32_t getProtection(uintptr_t addr); -// malloc_hack "2" handling -// mmap history -static int malloc_hack_2 = 0; - -#define ALLOC 0 -#define FREE 1 - -char* box_strdup(const char* s) { - char* ret = box_calloc(1, strlen(s)+1); - memcpy(ret, s, strlen(s)); - return ret; -} - -char* box_realpath(const char* path, char* ret) -{ - if(ret) - return realpath(path, ret); -#ifdef PATH_MAX - size_t path_max = PATH_MAX; -#else - size_t path_max = pathconf(path, _PC_PATH_MAX); - if (path_max <= 0) - path_max = 4096; -#endif - char tmp[path_max]; - char* p = realpath(path, tmp); - if(!p) - return NULL; - return box_strdup(tmp); -} - -static size_t pot(size_t l) { - size_t ret = 0; - while (l>(1u<box64_pagesize) s-=box64_pagesize; else s=0;} - memcpy(ret, p, s); - printf_log(LOG_DEBUG, " -> %p (copied %zu from old)\n", ret, s); - // Mmaped, free with original function - if(real_free && FREE) - RunFunctionFmtNoAlt(real_free, "p", p); - } - return ret; - } - return actual_realloc(p, s); -} - -EXPORT void* memalign(size_t align, size_t size) -{ - if(malloc_hack_2 && ALLOC && real_aligned_alloc) { - return (void*)RunFunctionFmtNoAlt(real_aligned_alloc, "LL", align, size); - } - if(box64_is32bits && align==4) - align = sizeof(void*); - return actual_memalign(align, size); -} - -EXPORT void* aligned_alloc(size_t align, size_t size) -{ - if(malloc_hack_2 && ALLOC && real_aligned_alloc) { - return (void*)RunFunctionFmtNoAlt(real_aligned_alloc, "LL", align, size); - } - if(box64_is32bits && align==4) { - return memalign(align, size); - } - return actual_memalign(align, size); -} - -EXPORT int posix_memalign(void** p, size_t align, size_t size) -{ - if(malloc_hack_2 && ALLOC && real_posix_memalign) { - return RunFunctionFmtNoAlt(real_posix_memalign, "pLL", p, align, size); - } - if(box64_is32bits && align==4) - align = sizeof(void*); - if((align%sizeof(void*)) || (pot(align)!=align)) - return EINVAL; - void* ret = actual_memalign(align, size); - if(!ret) - return ENOMEM; - *p = ret; - return 0; -} - -EXPORT void* valloc(size_t size) -{ - if(malloc_hack_2 && ALLOC && real_valloc) { - return (void*)RunFunctionFmtNoAlt(real_valloc, "L", size); - } - return actual_memalign(box64_pagesize, size); -} - -EXPORT void* pvalloc(size_t size) -{ - if(malloc_hack_2 && ALLOC && real_pvalloc) { - return (void*)RunFunctionFmtNoAlt(real_pvalloc, "L", size); - } - return actual_memalign(box64_pagesize, ALIGN(size)); -} - -EXPORT void cfree(void* p) -{ - if(malloc_hack_2 && p) { - if(getMmapped((uintptr_t)p)) { - printf_log(LOG_DEBUG, "%04d|Malloc_Hack_2: not freeing %p\n", GetTID(), p); - // Mmaped, free with original function - if(real_free && FREE) - RunFunctionFmtNoAlt(real_free, "p", p); - return; - } - } - actual_free(p); -} - -EXPORT size_t malloc_usable_size(void* p) -{ - if(malloc_hack_2 && real_malloc_usable_size) { - if(getMmapped((uintptr_t)p)) - return RunFunctionFmtNoAlt(real_malloc_usable_size, "p", p); - } - return actual_malloc_usable_size(p); -} - -EXPORT void* my__Znwm(size_t sz) //operator new(size_t) -{ - if(malloc_hack_2 && real__Znwm) { - return (void*)RunFunctionFmtNoAlt(real__Znwm, "L", sz); - } - return actual_malloc(sz); -} - -EXPORT void* my__ZnwmRKSt9nothrow_t(size_t sz, void* p) //operator new(size_t, std::nothrow_t const&) -{ - if(malloc_hack_2 && real__ZnwmRKSt9nothrow_t) { - return (void*)RunFunctionFmtNoAlt(real__ZnwmRKSt9nothrow_t, "Lp", sz, p); - } - return actual_malloc(sz); -} - -EXPORT void* my__Znam(size_t sz) //operator new[](size_t) -{ - if(malloc_hack_2 && real__Znam) { - return (void*)RunFunctionFmtNoAlt(real__Znam, "L", sz); - } - return actual_malloc(sz); -} - -EXPORT void* my__ZnamRKSt9nothrow_t(size_t sz, void* p) //operator new[](size_t, std::nothrow_t const&) -{ - if(malloc_hack_2 && real__ZnamRKSt9nothrow_t) { - return (void*)RunFunctionFmtNoAlt(real__ZnamRKSt9nothrow_t, "Lp", sz, p); - } - return actual_malloc(sz); -} - - -EXPORT void my__ZdaPv(void* p) //operator delete[](void*) -{ - if(malloc_hack_2 && p) { - if(getMmapped((uintptr_t)p)) { - printf_log(LOG_DEBUG, "%04d|Malloc_Hack_2: not freeing %p\n", GetTID(), p); - // Mmaped, free with original function - if(real__ZdaPv) - RunFunctionFmtNoAlt(real__ZdaPv, "p", p); - return; - } - } - actual_free(p); -} - -EXPORT void my__ZdaPvm(void* p, size_t sz) //operator delete[](void*, size_t) -{ - if(malloc_hack_2 && p) { - if(getMmapped((uintptr_t)p)) { - printf_log(LOG_DEBUG, "%04d|Malloc_Hack_2: not freeing %p\n", GetTID(), p); - // Mmaped, free with original function - if(real__ZdaPvm) - RunFunctionFmtNoAlt(real__ZdaPvm, "pL", p, sz); - return; - } - } - actual_free(p); -} - -EXPORT void my__ZdaPvmSt11align_val_t(void* p, size_t sz, size_t align) //operator delete[](void*, unsigned long, std::align_val_t) -{ - if(malloc_hack_2 && p) { - if(getMmapped((uintptr_t)p)) { - printf_log(LOG_DEBUG, "%04d|Malloc_Hack_2: not freeing %p\n", GetTID(), p); - // Mmaped, free with original function - if(real__ZdaPvmSt11align_val_t) - RunFunctionFmtNoAlt(real__ZdaPvmSt11align_val_t, "pLL", p, sz, align); - return; - } - } - actual_free(p); -} - -EXPORT void my__ZdlPv(void* p) //operator delete(void*) -{ - if(malloc_hack_2 && p) { - if(getMmapped((uintptr_t)p)) { - printf_log(LOG_DEBUG, "%04d|Malloc_Hack_2: not freeing %p\n", GetTID(), p); - // Mmaped, free with original function - if(real__ZdlPv) - RunFunctionFmtNoAlt(real__ZdlPv, "p", p); - return; - } - } - actual_free(p); -} - -EXPORT void my__ZdlPvm(void* p, size_t sz) //operator delete(void*, size_t) -{ - if(malloc_hack_2 && p) { - if(getMmapped((uintptr_t)p)) { - printf_log(LOG_DEBUG, "%04d|Malloc_Hack_2: not freeing %p\n", GetTID(), p); - // Mmaped, free with original function - if(real__ZdlPvm) - RunFunctionFmtNoAlt(real__ZdlPvm, "pL", p, sz); - return; - } - } - actual_free(p); -} - -EXPORT void* my__ZnwmSt11align_val_t(size_t sz, size_t align) //// operator new(unsigned long, std::align_val_t) -{ - if(malloc_hack_2 && real__ZnwmSt11align_val_t) { - return (void*)RunFunctionFmtNoAlt(real__ZnwmSt11align_val_t, "LL", sz, align); - } - return actual_memalign(align, sz); -} - -EXPORT void* my__ZnwmSt11align_val_tRKSt9nothrow_t(size_t sz, size_t align, void* p) //// operator new(unsigned long, std::align_val_t, std::nothrow_t const&) -{ - if(malloc_hack_2 && real__ZnwmSt11align_val_tRKSt9nothrow_t) { - return (void*)RunFunctionFmtNoAlt(real__ZnwmSt11align_val_tRKSt9nothrow_t, "LLp", sz, align, p); - } - return actual_memalign(align, sz); -} - -EXPORT void* my__ZnamSt11align_val_t(size_t sz, size_t align) //// operator new[](unsigned long, std::align_val_t) -{ - if(malloc_hack_2 && real__ZnamSt11align_val_t) { - return (void*)RunFunctionFmtNoAlt(real__ZnamSt11align_val_t, "LL", sz, align); - } - return actual_memalign(align, sz); -} - -EXPORT void* my__ZnamSt11align_val_tRKSt9nothrow_t(size_t sz, size_t align, void* p) //// operator new[](unsigned long, std::align_val_t, std::nothrow_t const&) -{ - if(malloc_hack_2 && real__ZnamSt11align_val_tRKSt9nothrow_t) { - return (void*)RunFunctionFmtNoAlt(real__ZnamSt11align_val_tRKSt9nothrow_t, "LLp", sz, align, p); - } - return actual_memalign(align, sz); -} - -EXPORT void my__ZdlPvRKSt9nothrow_t(void* p, void* n) //operator delete(void*, std::nothrow_t const&) -{ - if(malloc_hack_2 && p) { - if(getMmapped((uintptr_t)p)) { - printf_log(LOG_DEBUG, "%04d|Malloc_Hack_2: not freeing %p\n", GetTID(), p); - // Mmaped, free with original function - if(real__ZdlPvRKSt9nothrow_t) - RunFunctionFmtNoAlt(real__ZdlPvRKSt9nothrow_t, "pp", p, n); - return; - } - } - actual_free(p); -} - -EXPORT void my__ZdaPvSt11align_val_tRKSt9nothrow_t(void* p, size_t align, void* n) //operator delete[](void*, std::align_val_t, std::nothrow_t const&) -{ - if(malloc_hack_2 && p) { - printf_log(LOG_INFO, "%04d|Malloc_Hack_2: my__ZdaPvSt11align_val_tRKSt9nothrow_t(%p, %d, %p)\n", GetTID(), p, align, n); - if(getMmapped((uintptr_t)p)) { - printf_log(LOG_DEBUG, "%04d|Malloc_Hack_2: not freeing %p\n", GetTID(), p); - // Mmaped, free with original function - if(real__ZdaPvSt11align_val_tRKSt9nothrow_t) - RunFunctionFmtNoAlt(real__ZdaPvSt11align_val_tRKSt9nothrow_t, "p", p); - return; - } - } - actual_free(p); -} - -EXPORT void my__ZdlPvmSt11align_val_t(void* p, size_t sz, size_t align) //operator delete(void*, unsigned long, std::align_val_t) -{ - if(malloc_hack_2 && p) { - if(getMmapped((uintptr_t)p)) { - printf_log(LOG_DEBUG, "%04d|Malloc_Hack_2: not freeing %p\n", GetTID(), p); - // Mmaped, free with original function - if(real__ZdlPvmSt11align_val_t) - RunFunctionFmtNoAlt(real__ZdlPvmSt11align_val_t, "p", p); - return; - } - } - actual_free(p); -} - -EXPORT void my__ZdaPvRKSt9nothrow_t(void* p, void* n) //operator delete[](void*, std::nothrow_t const&) -{ - if(malloc_hack_2 && p) { - if(getMmapped((uintptr_t)p)) { - printf_log(LOG_DEBUG, "%04d|Malloc_Hack_2: not freeing %p\n", GetTID(), p); - // Mmaped, free with original function - if(real__ZdaPvRKSt9nothrow_t) - RunFunctionFmtNoAlt(real__ZdaPvRKSt9nothrow_t, "p", p); - return; - } - } - actual_free(p); -} - -EXPORT void my__ZdaPvSt11align_val_t(void* p, size_t align) //operator delete[](void*, std::align_val_t) -{ - if(malloc_hack_2 && p) { - if(getMmapped((uintptr_t)p)) { - printf_log(LOG_DEBUG, "%04d|Malloc_Hack_2: not freeing %p\n", GetTID(), p); - // Mmaped, free with original function - if(real__ZdaPvSt11align_val_t) - RunFunctionFmtNoAlt(real__ZdaPvSt11align_val_t, "p", p); - return; - } - } - actual_free(p); -} - -EXPORT void my__ZdlPvSt11align_val_t(void* p, size_t align) //operator delete(void*, std::align_val_t) -{ - if(malloc_hack_2 && p) { - if(getMmapped((uintptr_t)p)) { - printf_log(LOG_DEBUG, "%04d|Malloc_Hack_2: not freeing %p\n", GetTID(), p); - // Mmaped, free with original function - if(real__ZdlPvSt11align_val_t) - RunFunctionFmtNoAlt(real__ZdlPvSt11align_val_t, "p", p); - return; - } - } - actual_free(p); -} - -EXPORT void my__ZdlPvSt11align_val_tRKSt9nothrow_t(void* p, size_t align, void* n) //operator delete(void*, std::align_val_t, std::nothrow_t const&) -{ - if(malloc_hack_2 && p) { - if(getMmapped((uintptr_t)p)) { - printf_log(LOG_DEBUG, "%04d|Malloc_Hack_2: not freeing %p\n", GetTID(), p); - // Mmaped, free with original function - if(real__ZdlPvSt11align_val_tRKSt9nothrow_t) - RunFunctionFmtNoAlt(real__ZdlPvSt11align_val_tRKSt9nothrow_t, "pLp", p, align, n); - return; - } - } - actual_free(p); -} - -EXPORT void* my_tc_calloc(size_t n, size_t s) -{ - return actual_calloc(n, s); -} - -EXPORT void my_tc_cfree(void* p) -{ - actual_free(p); -} - -EXPORT void my_tc_delete(void* p) -{ - actual_free(p); -} - -EXPORT void my_tc_deletearray(void* p) -{ - actual_free(p); -} - -EXPORT void my_tc_deletearray_nothrow(void* p, void* n) -{ - actual_free(p); -} - -EXPORT void my_tc_delete_nothrow(void* p, void* n) -{ - actual_free(p); -} - -EXPORT void my_tc_free(void* p) -{ - actual_free(p); -} - -EXPORT void* my_tc_malloc(size_t s) -{ - return actual_calloc(1, s); -} - -EXPORT size_t my_tc_malloc_size(void* p) -{ - return actual_malloc_usable_size(p); -} - -EXPORT void* my_tc_new(size_t s) -{ - return actual_calloc(1, s); -} - -EXPORT void* my_tc_new_nothrow(size_t s, void* n) -{ - return actual_calloc(1, s); -} - -EXPORT void* my_tc_newarray(size_t s) -{ - return actual_calloc(1, s); -} - -EXPORT void* my_tc_newarray_nothrow(size_t s, void* n) -{ - return actual_calloc(1, s); -} - -EXPORT void* my_tc_pvalloc(size_t size) -{ - return actual_memalign(box64_pagesize, ALIGN(size)); -} - -EXPORT void* my_tc_valloc(size_t size) -{ - return actual_memalign(box64_pagesize, size); -} - -EXPORT void* my_tc_memalign(size_t align, size_t size) -{ - return actual_memalign(align, size); -} - -EXPORT void* my_tc_malloc_skip_new_handler_weak(size_t s) -{ - return actual_calloc(1, s); -} - -EXPORT int my_tc_mallocopt(int param, int value) -{ - // ignoring... - return 1; -} - -EXPORT void my_tc_malloc_stats() -{ - // ignoring -} -/* -EXPORT int my_tc_set_new_mode(int mode) -{ - // ignoring - static int old = 0; - int ret = old; - old = mode; - return ret; -} -*/ -EXPORT void* my_tc_malloc_skip_new_handler(size_t s) -{ - return actual_calloc(1, s); -} - -EXPORT void* my_tc_mallinfo(void* p) -{ - // ignored, returning null stuffs - memset(p, 0, (box64_is32bits?sizeof(ptr_t):sizeof(size_t))*10); - return p; -} - -EXPORT int my_tc_posix_memalign(void** p, size_t align, size_t size) -{ - if(align%sizeof(void*) || pot(align)!=align) - return EINVAL; - void* ret = actual_memalign(align, size); - if(!ret) - return ENOMEM; - *p = ret; - return 0; -} - -EXPORT void* my_tc_realloc(void* p, size_t s) -{ - return actual_realloc(p, s); -} -/* -EXPORT int my_tc_version(int i) -{ - return 2; -} -*/ - -EXPORT void* my_safer_scalable_aligned_realloc(void* p, size_t size, size_t align, void *old) -{ - if(!ispot(align)) { - errno = EINVAL; - return NULL; - } - if(align <= 8) - return actual_realloc(p, size); - size_t old_size = actual_malloc_usable_size(p); - if(old_size>=size) - return p; - void* new_p = actual_memalign(align, size); - memcpy(new_p, p, (old_size=size) - return p; - void* new_p = actual_memalign(align, size); - memcpy(new_p, p, (old_size 0 && og) { - // ogg_sync_pageout succeeded — og now has pointers into oy->data - // which may be invalidated by a later ogg_sync_buffer/realloc. - // Copy the page data to stable allocations. - ogg_page_32_t* page = (ogg_page_32_t*)og; - - // Free previous copies - if(ogg_page_header_copy) { actual_free(ogg_page_header_copy); ogg_page_header_copy = NULL; } - if(ogg_page_body_copy) { actual_free(ogg_page_body_copy); ogg_page_body_copy = NULL; } - - if(page->header && page->header_len > 0) { - ogg_page_header_copy = actual_malloc(page->header_len); - if(ogg_page_header_copy) { - memcpy(ogg_page_header_copy, from_ptrv(page->header), page->header_len); - page->header = to_ptrv(ogg_page_header_copy); - } - } - - if(page->body && page->body_len > 0) { - ogg_page_body_copy = actual_malloc(page->body_len); - if(ogg_page_body_copy) { - memcpy(ogg_page_body_copy, from_ptrv(page->body), page->body_len); - page->body = to_ptrv(ogg_page_body_copy); - } - } - } - - return ret; -} - -static void checkOggHookedSymbols32(elfheader_t* h) -{ - if(!h->numSymTab || !h->SymTab._32) - return; - - // Scan for ogg_sync_pageout in the 32-bit symbol table - for (size_t i = 0; i < h->numSymTab; ++i) { - int type = ELF32_ST_TYPE(h->SymTab._32[i].st_info); - if(type == STT_FUNC && h->SymTab._32[i].st_shndx != 0 - && h->SymTab._32[i].st_shndx <= 65521) { - const char* symname = h->StrTab + h->SymTab._32[i].st_name; - if(!strcmp(symname, "ogg_sync_pageout")) { - uintptr_t offs = h->SymTab._32[i].st_value + h->delta; - if(!real_ogg_sync_pageout_32) { - real_ogg_sync_pageout_32 = offs; - uintptr_t alt = AddCheckBridge(my_context->system, iFpp_32, (void*)my_ogg_sync_pageout_32, 0, "my_ogg_sync_pageout_32"); - addAlternate((void*)offs, (void*)alt); - printf_log(LOG_INFO, "Hooked statically-linked ogg_sync_pageout at %p in %s (ogg realloc fix)\n", (void*)offs, ElfName(h)); - } - break; - } - } - } -} -#endif // BOX32 && PPC64LE - -void checkHookedSymbols(elfheader_t* h) -{ - int hooked = 0; - int hooked_symtab = 0; - if(BOX64ENV(malloc_hack)==1 || box64_nolibs) - return; - if(box64_is32bits) { -#if defined(BOX32) && defined(PPC64LE) - checkOggHookedSymbols32(h); -#endif - return; - } - for (size_t i=0; inumSymTab; ++i) { - int type = ELF64_ST_TYPE(h->SymTab._64[i].st_info); - int sz = ELF64_ST_TYPE(h->SymTab._64[i].st_size); - if((type==STT_FUNC) && sz && (h->SymTab._64[i].st_shndx!=0 && h->SymTab._64[i].st_shndx<=65521)) { - const char * symname = h->StrTab+h->SymTab._64[i].st_name; - #define GO(A, B) if(!strcmp(symname, #A)) ++hooked; else if(!strcmp(symname, "__libc_" #A)) ++hooked; - #define GO2(A, B) - SUPER() - #undef GO - #undef GO2 - } - } - if(hooked<2) { - for (size_t i=0; inumDynSym && hooked<2; ++i) { - const char * symname = h->DynStr+h->DynSym._64[i].st_name; - int bind = ELF64_ST_BIND(h->DynSym._64[i].st_info); - int type = ELF64_ST_TYPE(h->DynSym._64[i].st_info); - int vis = h->DynSym._64[i].st_other&0x3; - if((type==STT_FUNC) - && (vis==STV_DEFAULT || vis==STV_PROTECTED) && (h->DynSym._64[i].st_shndx!=0 && h->DynSym._64[i].st_shndx<=65521)) { - uintptr_t offs = h->DynSym._64[i].st_value + h->delta; - size_t sz = h->DynSym._64[i].st_size; - if(bind!=STB_LOCAL && bind!=STB_WEAK && sz>=sizeof(reloc_jmp_t)) { - #define GO(A, B) if(!strcmp(symname, #A)) ++hooked; else if(!strcmp(symname, "__libc_" #A)) ++hooked; - #define GO2(A, B) - SUPER() - #undef GO - #undef GO2 - } - } - } - } else - hooked_symtab = 1; - if(hooked<2) - return; // only redirect on lib that hooked / redefined the operators - printf_log(LOG_INFO, "Redirecting overridden malloc%s from %s function for %s\n", malloc_hack_2?" with hack":"", hooked_symtab?"symtab":"dynsym", ElfName(h)); - if(hooked_symtab) { - for (size_t i=0; inumSymTab; ++i) { - int type = ELF64_ST_TYPE(h->SymTab._64[i].st_info); - if(type==STT_FUNC) { - const char * symname = h->StrTab+h->SymTab._64[i].st_name; - uintptr_t offs = h->SymTab._64[i].st_value + h->delta; - size_t sz = h->SymTab._64[i].st_size; - #define GO(A, B) if(!strcmp(symname, "__libc_" #A)) {uintptr_t alt = AddCheckBridge(my_context->system, B, A, 0, #A); printf_log(LOG_DEBUG, "Redirecting %s function from %p (%s)\n", symname, (void*)offs, ElfName(h)); addRelocJmp((void*)offs, (void*)alt, sz, "__libc_" #A, h, NULL);} - #define GO2(A, B) - SUPER() - #undef GO - #undef GO2 - #define GO(A, B) if(!strcmp(symname, #A)) {uintptr_t alt = AddCheckBridge(my_context->system, B, A, 0, #A); printf_log(LOG_DEBUG, "Redirecting %s function from %p (%s)\n", symname, (void*)offs, ElfName(h)); addRelocJmp((void*)offs, (void*)alt, sz, #A, h, &real_##A);} - #define GO2(A, B) if(!strcmp(symname, #A) && (BOX64ENV(malloc_hack)>1)) {uintptr_t alt = AddCheckBridge(my_context->system, B, my_##A, 0, "my_" #A); printf_log(LOG_DEBUG, "Redirecting %s function from %p (%s)\n", symname, (void*)offs, ElfName(h)); addRelocJmp((void*)offs, (void*)alt, sz, "my_" #A, h, &real_##A);} - SUPER() - #undef GO - #undef GO2 - } - } - } else { - for (size_t i=0; inumDynSym; ++i) { - const char * symname = h->DynStr+h->DynSym._64[i].st_name; - int bind = ELF64_ST_BIND(h->DynSym._64[i].st_info); - int type = ELF64_ST_TYPE(h->DynSym._64[i].st_info); - int vis = h->DynSym._64[i].st_other&0x3; - if((type==STT_FUNC) - && (vis==STV_DEFAULT || vis==STV_PROTECTED) && (h->DynSym._64[i].st_shndx!=0 && h->DynSym._64[i].st_shndx<=65521)) { - uintptr_t offs = h->DynSym._64[i].st_value + h->delta; - size_t sz = h->DynSym._64[i].st_size; - if(bind!=STB_LOCAL && bind!=STB_WEAK) { - #define GO(A, B) if(!strcmp(symname, "__libc_" #A)) {uintptr_t alt = AddCheckBridge(my_context->system, B, A, 0, #A); printf_log(LOG_DEBUG, "Redirecting %s function from %p (%s)\n", symname, (void*)offs, ElfName(h)); addRelocJmp((void*)offs, (void*)alt, sz, "__libc_" #A, h, NULL);} - #define GO2(A, B) - SUPER() - #undef GO - #undef GO2 - #define GO(A, B) if(!strcmp(symname, #A)) {uintptr_t alt = AddCheckBridge(my_context->system, B, A, 0, #A); printf_log(LOG_DEBUG, "Redirecting %s function from %p (%s)\n", symname, (void*)offs, ElfName(h)); addRelocJmp((void*)offs, (void*)alt, sz, #A, h, &real_##A);} - #define GO2(A, B) if(!strcmp(symname, #A) && (BOX64ENV(malloc_hack)>1)) {uintptr_t alt = AddCheckBridge(my_context->system, B, my_##A, 0, "my_" #A); printf_log(LOG_DEBUG, "Redirecting %s function from %p (%s)\n", symname, (void*)offs, ElfName(h)); addRelocJmp((void*)offs, (void*)alt, sz, "my_" #A, h, &real_##A);} - SUPER() - #undef GO - #undef GO2 - } - } - } - } - if(BOX64ENV(malloc_hack)==2) - h->malloc_hook_2 = 1; -} - -void startMallocHook() -{ - malloc_hack_2 = 1; -} -void endMallocHook() -{ - malloc_hack_2 = 0; -} - -EXPORT int my___TBB_internal_find_original_malloc(int n, char* names[], void* ptr[]) -{ - int ret = 1; - #define GO(A, B) else if(!strcmp(names[i], #A)) {ptr[i] = A;} - #define GO2(A, B) - for (int i=0; ixmm[0].d[0]); } @@ -5833,6 +5839,7 @@ void lFEipL(x64emu_t *emu, uintptr_t fcn) { lFEipL_t fn = (lFEipL_t)fcn; R_RAX=( void lFEipV(x64emu_t *emu, uintptr_t fcn) { lFEipV_t fn = (lFEipV_t)fcn; R_RAX=(intptr_t)fn(emu, (int32_t)R_RDI, (void*)R_RSI, (void*)(R_RSP + 8)); } void lFEpip(x64emu_t *emu, uintptr_t fcn) { lFEpip_t fn = (lFEpip_t)fcn; R_RAX=(intptr_t)fn(emu, (void*)R_RDI, (int32_t)R_RSI, (void*)R_RDX); } void lFEppL(x64emu_t *emu, uintptr_t fcn) { lFEppL_t fn = (lFEppL_t)fcn; R_RAX=(intptr_t)fn(emu, (void*)R_RDI, (void*)R_RSI, (uintptr_t)R_RDX); } +void lFEppS(x64emu_t *emu, uintptr_t fcn) { lFEppS_t fn = (lFEppS_t)fcn; R_RAX=(intptr_t)fn(emu, (void*)R_RDI, (void*)R_RSI, io_convert((void*)R_RDX)); } void lFiiLu(x64emu_t *emu, uintptr_t fcn) { lFiiLu_t fn = (lFiiLu_t)fcn; R_RAX=(intptr_t)fn((int32_t)R_RDI, (int32_t)R_RSI, (uintptr_t)R_RDX, (uint32_t)R_RCX); } void lFiipL(x64emu_t *emu, uintptr_t fcn) { lFiipL_t fn = (lFiipL_t)fcn; R_RAX=(intptr_t)fn((int32_t)R_RDI, (int32_t)R_RSI, (void*)R_RDX, (uintptr_t)R_RCX); } void lFipiI(x64emu_t *emu, uintptr_t fcn) { lFipiI_t fn = (lFipiI_t)fcn; R_RAX=(intptr_t)fn((int32_t)R_RDI, (void*)R_RSI, (int32_t)R_RDX, (int64_t)R_RCX); } @@ -5855,7 +5862,6 @@ void lFplpp(x64emu_t *emu, uintptr_t fcn) { lFplpp_t fn = (lFplpp_t)fcn; R_RAX=( void lFpLpp(x64emu_t *emu, uintptr_t fcn) { lFpLpp_t fn = (lFpLpp_t)fcn; R_RAX=(intptr_t)fn((void*)R_RDI, (uintptr_t)R_RSI, (void*)R_RDX, (void*)R_RCX); } void lFppii(x64emu_t *emu, uintptr_t fcn) { lFppii_t fn = (lFppii_t)fcn; R_RAX=(intptr_t)fn((void*)R_RDI, (void*)R_RSI, (int32_t)R_RDX, (int32_t)R_RCX); } void lFppip(x64emu_t *emu, uintptr_t fcn) { lFppip_t fn = (lFppip_t)fcn; R_RAX=(intptr_t)fn((void*)R_RDI, (void*)R_RSI, (int32_t)R_RDX, (void*)R_RCX); } -void lFppiS(x64emu_t *emu, uintptr_t fcn) { lFppiS_t fn = (lFppiS_t)fcn; R_RAX=(intptr_t)fn((void*)R_RDI, (void*)R_RSI, (int32_t)R_RDX, io_convert((void*)R_RCX)); } void lFpplp(x64emu_t *emu, uintptr_t fcn) { lFpplp_t fn = (lFpplp_t)fcn; R_RAX=(intptr_t)fn((void*)R_RDI, (void*)R_RSI, (intptr_t)R_RDX, (void*)R_RCX); } void lFppLi(x64emu_t *emu, uintptr_t fcn) { lFppLi_t fn = (lFppLi_t)fcn; R_RAX=(intptr_t)fn((void*)R_RDI, (void*)R_RSI, (uintptr_t)R_RDX, (int32_t)R_RCX); } void lFppLp(x64emu_t *emu, uintptr_t fcn) { lFppLp_t fn = (lFppLp_t)fcn; R_RAX=(intptr_t)fn((void*)R_RDI, (void*)R_RSI, (uintptr_t)R_RDX, (void*)R_RCX); } @@ -5905,6 +5911,7 @@ void pFEpiV(x64emu_t *emu, uintptr_t fcn) { pFEpiV_t fn = (pFEpiV_t)fcn; R_RAX=( void pFEpCL(x64emu_t *emu, uintptr_t fcn) { pFEpCL_t fn = (pFEpCL_t)fcn; R_RAX=(uintptr_t)fn(emu, (void*)R_RDI, (uint8_t)R_RSI, (uintptr_t)R_RDX); } void pFEpup(x64emu_t *emu, uintptr_t fcn) { pFEpup_t fn = (pFEpup_t)fcn; R_RAX=(uintptr_t)fn(emu, (void*)R_RDI, (uint32_t)R_RSI, (void*)R_RDX); } void pFEplp(x64emu_t *emu, uintptr_t fcn) { pFEplp_t fn = (pFEplp_t)fcn; R_RAX=(uintptr_t)fn(emu, (void*)R_RDI, (intptr_t)R_RSI, (void*)R_RDX); } +void pFEpLL(x64emu_t *emu, uintptr_t fcn) { pFEpLL_t fn = (pFEpLL_t)fcn; R_RAX=(uintptr_t)fn(emu, (void*)R_RDI, (uintptr_t)R_RSI, (uintptr_t)R_RDX); } void pFEppi(x64emu_t *emu, uintptr_t fcn) { pFEppi_t fn = (pFEppi_t)fcn; R_RAX=(uintptr_t)fn(emu, (void*)R_RDI, (void*)R_RSI, (int32_t)R_RDX); } void pFEppu(x64emu_t *emu, uintptr_t fcn) { pFEppu_t fn = (pFEppu_t)fcn; R_RAX=(uintptr_t)fn(emu, (void*)R_RDI, (void*)R_RSI, (uint32_t)R_RDX); } void pFEppp(x64emu_t *emu, uintptr_t fcn) { pFEppp_t fn = (pFEppp_t)fcn; R_RAX=(uintptr_t)fn(emu, (void*)R_RDI, (void*)R_RSI, (void*)R_RDX); } @@ -6541,6 +6548,7 @@ void dFppddd(x64emu_t *emu, uintptr_t fcn) { dFppddd_t fn = (dFppddd_t)fcn; emu- void dFppppL(x64emu_t *emu, uintptr_t fcn) { dFppppL_t fn = (dFppppL_t)fcn; emu->xmm[0].d[0]=fn((void*)R_RDI, (void*)R_RSI, (void*)R_RDX, (void*)R_RCX, (uintptr_t)R_R8); } void lFEippL(x64emu_t *emu, uintptr_t fcn) { lFEippL_t fn = (lFEippL_t)fcn; R_RAX=(intptr_t)fn(emu, (int32_t)R_RDI, (void*)R_RSI, (void*)R_RDX, (uintptr_t)R_RCX); } void lFEuipp(x64emu_t *emu, uintptr_t fcn) { lFEuipp_t fn = (lFEuipp_t)fcn; R_RAX=(intptr_t)fn(emu, (uint32_t)R_RDI, (int32_t)R_RSI, (void*)R_RDX, (void*)R_RCX); } +void lFEppiS(x64emu_t *emu, uintptr_t fcn) { lFEppiS_t fn = (lFEppiS_t)fcn; R_RAX=(intptr_t)fn(emu, (void*)R_RDI, (void*)R_RSI, (int32_t)R_RDX, io_convert((void*)R_RCX)); } void lFEppLL(x64emu_t *emu, uintptr_t fcn) { lFEppLL_t fn = (lFEppLL_t)fcn; R_RAX=(intptr_t)fn(emu, (void*)R_RDI, (void*)R_RSI, (uintptr_t)R_RDX, (uintptr_t)R_RCX); } void lFipiIi(x64emu_t *emu, uintptr_t fcn) { lFipiIi_t fn = (lFipiIi_t)fcn; R_RAX=(intptr_t)fn((int32_t)R_RDI, (void*)R_RSI, (int32_t)R_RDX, (int64_t)R_RCX, (int32_t)R_R8); } void lFipili(x64emu_t *emu, uintptr_t fcn) { lFipili_t fn = (lFipili_t)fcn; R_RAX=(intptr_t)fn((int32_t)R_RDI, (void*)R_RSI, (int32_t)R_RDX, (intptr_t)R_RCX, (int32_t)R_R8); } @@ -8636,8 +8644,10 @@ void iFEipipp(x64emu_t *emu, uintptr_t fcn) { iFEipipp_t fn = (iFEipipp_t)fcn; R #if !defined(STATICBUILD) void lFiN(x64emu_t *emu, uintptr_t fcn) { lFiN_t fn = (lFiN_t)fcn; R_RAX=(intptr_t)fn((int32_t)R_RDI, (void*)R_RSI); } +void vFEpL(x64emu_t *emu, uintptr_t fcn) { vFEpL_t fn = (vFEpL_t)fcn; fn(emu, (void*)R_RDI, (uintptr_t)R_RSI); } void iFlip(x64emu_t *emu, uintptr_t fcn) { iFlip_t fn = (iFlip_t)fcn; R_RAX=(int)fn((intptr_t)R_RDI, (int32_t)R_RSI, (void*)R_RDX); } void iFLLi(x64emu_t *emu, uintptr_t fcn) { iFLLi_t fn = (iFLLi_t)fcn; R_RAX=(int)fn((uintptr_t)R_RDI, (uintptr_t)R_RSI, (int32_t)R_RDX); } +void vFEpLL(x64emu_t *emu, uintptr_t fcn) { vFEpLL_t fn = (vFEpLL_t)fcn; fn(emu, (void*)R_RDI, (uintptr_t)R_RSI, (uintptr_t)R_RDX); } void iFLLii(x64emu_t *emu, uintptr_t fcn) { iFLLii_t fn = (iFLLii_t)fcn; R_RAX=(int)fn((uintptr_t)R_RDI, (uintptr_t)R_RSI, (int32_t)R_RDX, (int32_t)R_RCX); } void WFpLLu(x64emu_t *emu, uintptr_t fcn) { WFpLLu_t fn = (WFpLLu_t)fcn; R_RAX=(unsigned short)fn((void*)R_RDI, (uintptr_t)R_RSI, (uintptr_t)R_RDX, (uint32_t)R_RCX); } void pFpLiS(x64emu_t *emu, uintptr_t fcn) { pFpLiS_t fn = (pFpLiS_t)fcn; R_RAX=(uintptr_t)fn((void*)R_RDI, (uintptr_t)R_RSI, (int32_t)R_RDX, io_convert((void*)R_RCX)); } diff --git a/src/wrapped/generated/wrapper.h b/src/wrapped/generated/wrapper.h index 180a881a5a..146f04386f 100644 --- a/src/wrapped/generated/wrapper.h +++ b/src/wrapped/generated/wrapper.h @@ -866,7 +866,6 @@ void lFppu(x64emu_t *emu, uintptr_t fnc); void lFppl(x64emu_t *emu, uintptr_t fnc); void lFppL(x64emu_t *emu, uintptr_t fnc); void lFppp(x64emu_t *emu, uintptr_t fnc); -void lFppS(x64emu_t *emu, uintptr_t fnc); void lFSpl(x64emu_t *emu, uintptr_t fnc); void LFEpW(x64emu_t *emu, uintptr_t fnc); void LFEpu(x64emu_t *emu, uintptr_t fnc); @@ -909,6 +908,7 @@ void pFEiV(x64emu_t *emu, uintptr_t fnc); void pFEup(x64emu_t *emu, uintptr_t fnc); void pFEuV(x64emu_t *emu, uintptr_t fnc); void pFEuA(x64emu_t *emu, uintptr_t fnc); +void pFELL(x64emu_t *emu, uintptr_t fnc); void pFEpi(x64emu_t *emu, uintptr_t fnc); void pFEpu(x64emu_t *emu, uintptr_t fnc); void pFEpL(x64emu_t *emu, uintptr_t fnc); @@ -1218,6 +1218,7 @@ void iFEpup(x64emu_t *emu, uintptr_t fnc); void iFEpuV(x64emu_t *emu, uintptr_t fnc); void iFEpUi(x64emu_t *emu, uintptr_t fnc); void iFEpLi(x64emu_t *emu, uintptr_t fnc); +void iFEpLL(x64emu_t *emu, uintptr_t fnc); void iFEpLp(x64emu_t *emu, uintptr_t fnc); void iFEppi(x64emu_t *emu, uintptr_t fnc); void iFEppd(x64emu_t *emu, uintptr_t fnc); @@ -1551,6 +1552,7 @@ void lFEipL(x64emu_t *emu, uintptr_t fnc); void lFEipV(x64emu_t *emu, uintptr_t fnc); void lFEpip(x64emu_t *emu, uintptr_t fnc); void lFEppL(x64emu_t *emu, uintptr_t fnc); +void lFEppS(x64emu_t *emu, uintptr_t fnc); void lFiiLu(x64emu_t *emu, uintptr_t fnc); void lFiipL(x64emu_t *emu, uintptr_t fnc); void lFipiI(x64emu_t *emu, uintptr_t fnc); @@ -1573,7 +1575,6 @@ void lFplpp(x64emu_t *emu, uintptr_t fnc); void lFpLpp(x64emu_t *emu, uintptr_t fnc); void lFppii(x64emu_t *emu, uintptr_t fnc); void lFppip(x64emu_t *emu, uintptr_t fnc); -void lFppiS(x64emu_t *emu, uintptr_t fnc); void lFpplp(x64emu_t *emu, uintptr_t fnc); void lFppLi(x64emu_t *emu, uintptr_t fnc); void lFppLp(x64emu_t *emu, uintptr_t fnc); @@ -1623,6 +1624,7 @@ void pFEpiV(x64emu_t *emu, uintptr_t fnc); void pFEpCL(x64emu_t *emu, uintptr_t fnc); void pFEpup(x64emu_t *emu, uintptr_t fnc); void pFEplp(x64emu_t *emu, uintptr_t fnc); +void pFEpLL(x64emu_t *emu, uintptr_t fnc); void pFEppi(x64emu_t *emu, uintptr_t fnc); void pFEppu(x64emu_t *emu, uintptr_t fnc); void pFEppp(x64emu_t *emu, uintptr_t fnc); @@ -2260,6 +2262,7 @@ void dFppddd(x64emu_t *emu, uintptr_t fnc); void dFppppL(x64emu_t *emu, uintptr_t fnc); void lFEippL(x64emu_t *emu, uintptr_t fnc); void lFEuipp(x64emu_t *emu, uintptr_t fnc); +void lFEppiS(x64emu_t *emu, uintptr_t fnc); void lFEppLL(x64emu_t *emu, uintptr_t fnc); void lFipiIi(x64emu_t *emu, uintptr_t fnc); void lFipili(x64emu_t *emu, uintptr_t fnc); @@ -4280,8 +4283,10 @@ void iFEipipp(x64emu_t *emu, uintptr_t fnc); #if !defined(STATICBUILD) void lFiN(x64emu_t *emu, uintptr_t fnc); +void vFEpL(x64emu_t *emu, uintptr_t fnc); void iFlip(x64emu_t *emu, uintptr_t fnc); void iFLLi(x64emu_t *emu, uintptr_t fnc); +void vFEpLL(x64emu_t *emu, uintptr_t fnc); void iFLLii(x64emu_t *emu, uintptr_t fnc); void WFpLLu(x64emu_t *emu, uintptr_t fnc); void pFpLiS(x64emu_t *emu, uintptr_t fnc); diff --git a/src/wrapped/wrappedlibc.c b/src/wrapped/wrappedlibc.c index 65f41f1596..915090aae6 100644 --- a/src/wrapped/wrappedlibc.c +++ b/src/wrapped/wrappedlibc.c @@ -99,6 +99,7 @@ extern const char *const sys_siglist[] __asm__("__sys_siglist"); #include "random.h" #include "cpumask.h" #include "x64tls.h" +#include "guestalloc.h" #ifndef LOG_INFO #define LOG_INFO 1 #endif @@ -116,6 +117,504 @@ EXPORT ssize_t my_write(x64emu_t* emu, int fd, const void* buf, size_t count) return write(fd, buf, count); } +EXPORT void* my_malloc(x64emu_t* emu, size_t size) +{ + (void)emu; + if(guest_alloc_is_active()) + return guest_malloc(size); + return actual_calloc(1, size); +} + +EXPORT void* my_calloc(x64emu_t* emu, size_t n, size_t size) +{ + (void)emu; + if(guest_alloc_is_active()) + return guest_calloc(n, size); + return actual_calloc(n, size); +} + +EXPORT void* my_realloc(x64emu_t* emu, void* p, size_t size) +{ + (void)emu; + if(guest_alloc_is_active()) { + if(!p) + return guest_malloc(size); + if(guest_alloc_owns(p)) + return guest_realloc(p, size); + } + return actual_realloc(p, size); +} + +EXPORT void my_free(x64emu_t* emu, void* p) +{ + (void)emu; + if(p && guest_alloc_is_active() && guest_alloc_owns(p)) { + guest_free(p); + return; + } + actual_free(p); +} + +EXPORT void my_cfree(x64emu_t* emu, void* p) +{ + my_free(emu, p); +} + +EXPORT void* my_reallocarray(x64emu_t* emu, void* p, size_t n, size_t size) +{ + if(n && size > (size_t)-1 / n) { + errno = ENOMEM; + return NULL; + } + return my_realloc(emu, p, n*size); +} + +EXPORT void* my_memalign(x64emu_t* emu, size_t align, size_t size) +{ + (void)emu; + if(guest_alloc_is_active()) + return guest_memalign(align, size); + if(box64_is32bits && align==4) + align = sizeof(void*); + return actual_memalign(align, size); +} + +EXPORT void* my_aligned_alloc(x64emu_t* emu, size_t align, size_t size) +{ + (void)emu; + if(guest_alloc_is_active()) + return guest_memalign(align, size); + if(box64_is32bits && align==4) + return my_memalign(emu, align, size); + return actual_memalign(align, size); +} + +EXPORT int my_posix_memalign(x64emu_t* emu, void** memptr, size_t align, size_t size) +{ + (void)emu; + if(guest_alloc_is_active()) + return guest_posix_memalign(memptr, align, size); + if(box64_is32bits && align==4) + align = sizeof(void*); + if(!align || (align%sizeof(void*)) || (align & (align-1))) + return EINVAL; + void* ret = actual_memalign(align, size); + if(!ret) + return ENOMEM; + *memptr = ret; + return 0; +} + +EXPORT void* my_valloc(x64emu_t* emu, size_t size) +{ + (void)emu; + if(guest_alloc_is_active()) + return guest_memalign(box64_pagesize, size); + return actual_memalign(box64_pagesize, size); +} + +EXPORT void* my_pvalloc(x64emu_t* emu, size_t size) +{ + (void)emu; + if(guest_alloc_is_active()) + return guest_memalign(box64_pagesize, ALIGN(size)); + return actual_memalign(box64_pagesize, ALIGN(size)); +} + +EXPORT size_t my_malloc_usable_size(x64emu_t* emu, void* p) +{ + (void)emu; + if(p && guest_alloc_is_active() && guest_alloc_owns(p)) + return guest_usable_size(p); +#if defined(ANDROID) || defined(STATICBUILD) + return malloc_usable_size(p); +#else + return actual_malloc_usable_size(p); +#endif +} + +EXPORT void my_free_sized(x64emu_t* emu, void* p, size_t size) +{ + (void)size; + my_free(emu, p); +} + +EXPORT void my_free_aligned_sized(x64emu_t* emu, void* p, size_t align, size_t size) +{ + (void)align; (void)size; + my_free(emu, p); +} + +EXPORT int my_brk(x64emu_t* emu, void* addr) +{ + (void)emu; + if(guest_brk_is_active()) { + uintptr_t ret; + if(guest_brk_set((uintptr_t)addr, &ret)) { + errno = ENOMEM; + return -1; + } + return 0; + } + return brk(addr); +} + +EXPORT void* my_sbrk(x64emu_t* emu, intptr_t inc) +{ + (void)emu; + if(guest_brk_is_active()) + return guest_sbrk(inc); + return sbrk(inc); +} + +EXPORT void* my___libc_malloc(x64emu_t* emu, size_t size) __attribute__((alias("my_malloc"))); +EXPORT void* my___libc_calloc(x64emu_t* emu, size_t n, size_t size) __attribute__((alias("my_calloc"))); +EXPORT void* my___libc_realloc(x64emu_t* emu, void* p, size_t size) __attribute__((alias("my_realloc"))); +EXPORT void my___libc_free(x64emu_t* emu, void* p) __attribute__((alias("my_free"))); +EXPORT void my___libc_cfree(x64emu_t* emu, void* p) __attribute__((alias("my_cfree"))); +EXPORT void* my___libc_memalign(x64emu_t* emu, size_t align, size_t size) __attribute__((alias("my_memalign"))); +EXPORT void* my___libc_valloc(x64emu_t* emu, size_t size) __attribute__((alias("my_valloc"))); +EXPORT void* my___libc_pvalloc(x64emu_t* emu, size_t size) __attribute__((alias("my_pvalloc"))); +EXPORT void* my___sbrk(x64emu_t* emu, intptr_t inc) __attribute__((alias("my_sbrk"))); + +static char* guest_copy_string(const char* s) +{ + size_t l = strlen(s) + 1; + char* p = guest_malloc(l); + if(p) + memcpy(p, s, l); + return p; +} + +EXPORT char* my_strdup(x64emu_t* emu, const char* s) +{ + (void)emu; + if(!guest_alloc_is_active()) + return strdup(s); + return guest_copy_string(s); +} +EXPORT char* my___strdup(x64emu_t* emu, const char* s) __attribute__((alias("my_strdup"))); + +EXPORT char* my_strndup(x64emu_t* emu, const char* s, size_t n) +{ + (void)emu; + if(!guest_alloc_is_active()) + return strndup(s, n); + size_t l = strnlen(s, n); + char* p = guest_malloc(l+1); + if(!p) + return NULL; + memcpy(p, s, l); + p[l] = 0; + return p; +} +EXPORT char* my___strndup(x64emu_t* emu, const char* s, size_t n) __attribute__((alias("my_strndup"))); + +EXPORT wchar_t* my_wcsdup(x64emu_t* emu, const wchar_t* s) +{ + (void)emu; + if(!guest_alloc_is_active()) + return wcsdup(s); + size_t l = (wcslen(s)+1) * sizeof(wchar_t); + wchar_t* p = guest_malloc(l); + if(p) + memcpy(p, s, l); + return p; +} + +EXPORT char* my_getcwd(x64emu_t* emu, char* buf, size_t size) +{ + (void)emu; + if(buf || !guest_alloc_is_active()) + return getcwd(buf, size); + char* host = getcwd(NULL, 0); + if(!host) + return NULL; + char* ret = guest_copy_string(host); + box_free(host); + return ret; +} + +EXPORT char* my___getcwd_chk(x64emu_t* emu, char* buf, size_t size, size_t buflen) +{ + if(size > buflen) { + errno = ERANGE; + return NULL; + } + return my_getcwd(emu, buf, size); +} + +EXPORT char* my_get_current_dir_name(x64emu_t* emu) +{ + (void)emu; +#ifndef ANDROID + if(!guest_alloc_is_active()) + return get_current_dir_name(); + char* host = get_current_dir_name(); +#else + char* host = getcwd(NULL, 0); +#endif + if(!host || !guest_alloc_is_active()) + return host; + char* ret = guest_copy_string(host); + box_free(host); + return ret; +} + +EXPORT char* my_canonicalize_file_name(x64emu_t* emu, const char* name) +{ + (void)emu; + char* host = realpath(name, NULL); + if(!host || !guest_alloc_is_active()) + return host; + char* ret = guest_copy_string(host); + box_free(host); + return ret; +} + +#ifndef ANDROID +EXPORT char* my_tempnam(x64emu_t* emu, const char* dir, const char* pfx) +{ + (void)emu; + char* host = tempnam(dir, pfx); + if(!host || !guest_alloc_is_active()) + return host; + char* ret = guest_copy_string(host); + box_free(host); + return ret; +} +#endif + +static long guest_getdelim(x64emu_t* emu, char** lineptr, size_t* n, int delim, FILE* stream) +{ + (void)emu; + if(!lineptr || !n || !stream) { + errno = EINVAL; + return -1; + } + char* buf = *lineptr; + size_t cap = *n; + int created = 0; + if(!buf || !cap) { + cap = 128; + buf = guest_malloc(cap); + if(!buf) { + errno = ENOMEM; + return -1; + } + created = 1; + } + size_t len = 0; + int c = EOF; + for(;;) { + c = fgetc(stream); + if(c==EOF) + break; + if(len+1 >= cap) { + size_t ncap = cap * 2; + char* nbuf = guest_realloc(buf, ncap); + if(!nbuf) { + errno = ENOMEM; + if(created) { + guest_free(buf); + *lineptr = NULL; + *n = 0; + } + return -1; + } + buf = nbuf; + cap = ncap; + } + buf[len++] = (char)c; + if(c==delim) + break; + } + if(len==0 && c==EOF) { + if(created) { + guest_free(buf); + *lineptr = NULL; + *n = 0; + } + return -1; + } + buf[len] = 0; + *lineptr = buf; + *n = cap; + return (long)len; +} + +EXPORT long my_getdelim(x64emu_t* emu, char** lineptr, size_t* n, int delim, FILE* stream) +{ + if(!guest_alloc_is_active()) + return getdelim(lineptr, n, delim, stream); + return guest_getdelim(emu, lineptr, n, delim, stream); +} +EXPORT long my___getdelim(x64emu_t* emu, char** lineptr, size_t* n, int delim, FILE* stream) __attribute__((alias("my_getdelim"))); + +EXPORT long my_getline(x64emu_t* emu, char** lineptr, size_t* n, FILE* stream) +{ + if(!guest_alloc_is_active()) + return getline(lineptr, n, stream); + return guest_getdelim(emu, lineptr, n, '\n', stream); +} + +#ifndef ANDROID +typedef struct my_memstream_s { + char* buf; + size_t size; + size_t cap; + size_t pos; + char** bufp; + size_t* sizep; +} my_memstream_t; + +static int my_memstream_grow(my_memstream_t* ms, size_t need) +{ + if(ms->cap >= need) + return 0; + size_t ncap = ms->cap ? ms->cap : 128; + while(ncap < need) { + if(ncap > ((size_t)-1)/2) + return -1; + ncap *= 2; + } + char* nbuf = guest_realloc(ms->buf, ncap); + if(!nbuf) + return -1; + ms->buf = nbuf; + ms->cap = ncap; + return 0; +} + +static void my_memstream_publish(my_memstream_t* ms) +{ + if(ms->bufp) + *ms->bufp = ms->buf; + if(ms->sizep) + *ms->sizep = ms->size; +} + +static ssize_t my_memstream_write(void* cookie, const char* buf, size_t size) +{ + my_memstream_t* ms = (my_memstream_t*)cookie; + if(size > (size_t)-1 - ms->pos - 1) { + errno = ENOMEM; + return -1; + } + if(my_memstream_grow(ms, ms->pos + size + 1)) { + errno = ENOMEM; + return -1; + } + memcpy(ms->buf + ms->pos, buf, size); + ms->pos += size; + if(ms->pos > ms->size) + ms->size = ms->pos; + ms->buf[ms->size] = 0; + my_memstream_publish(ms); + return (ssize_t)size; +} + +static int my_memstream_seek(void* cookie, off_t* offset, int whence) +{ + my_memstream_t* ms = (my_memstream_t*)cookie; + off_t base; + switch(whence) { + case SEEK_SET: base = 0; break; + case SEEK_CUR: base = (off_t)ms->pos; break; + case SEEK_END: base = (off_t)ms->size; break; + default: errno = EINVAL; return -1; + } + off_t np = base + *offset; + if(np < 0) { + errno = EINVAL; + return -1; + } + if(my_memstream_grow(ms, (size_t)np + 1)) { + errno = ENOMEM; + return -1; + } + if((size_t)np > ms->size) + memset(ms->buf + ms->size, 0, (size_t)np - ms->size); + ms->pos = (size_t)np; + if(ms->pos > ms->size) + ms->size = ms->pos; + ms->buf[ms->size] = 0; + *offset = np; + my_memstream_publish(ms); + return 0; +} + +static int my_memstream_close(void* cookie) +{ + my_memstream_t* ms = (my_memstream_t*)cookie; + if(my_memstream_grow(ms, ms->size + 1)) { + box_free(ms); + return -1; + } + ms->buf[ms->size] = 0; + my_memstream_publish(ms); + box_free(ms); + return 0; +} + +EXPORT FILE* my_open_memstream(x64emu_t* emu, char** bufp, size_t* sizep) +{ + (void)emu; + if(!guest_alloc_is_active()) + return open_memstream(bufp, sizep); + my_memstream_t* ms = (my_memstream_t*)box_calloc(1, sizeof(my_memstream_t)); + if(!ms) + return NULL; + ms->bufp = bufp; + ms->sizep = sizep; + cookie_io_functions_t io = { NULL, my_memstream_write, my_memstream_seek, my_memstream_close }; + FILE* f = fopencookie(ms, "w", io); + if(!f) { + box_free(ms); + return NULL; + } + return f; +} +#endif + +static void free_host_dirent_list(struct dirent** list, int n) +{ + if(!list) + return; + for(int i=0; id_reclen); + if(!entry) { + for(int j=0; jd_reclen); + guestlist[i] = entry; + } + guestlist[n] = NULL; + *namelist = guestlist; + return n; +} #define LIBNAME libc const char* libcName = "libc.so.6"; @@ -1269,7 +1768,20 @@ EXPORT int my___sprintf_chk(x64emu_t* emu, void* buff, int flag, size_t l, void EXPORT int my_asprintf(x64emu_t* emu, void** buff, void * fmt, uint64_t * b) { myStackAlign(emu, (const char*)fmt, b, emu->scratch, R_EAX, 2); PREPARE_VALIST; - return vasprintf((char**)buff, (char*)fmt, VARARGS); + char* host = NULL; + int r = vasprintf(&host, (char*)fmt, VARARGS); + if(r<0 || !host || !buff) { + if(buff) *buff = NULL; + return -1; + } + if(!guest_alloc_is_active()) { + *buff = host; + return r; + } + char* ret = guest_copy_string(host); + box_free(host); + *buff = ret; + return ret ? r : -1; } EXPORT int my___asprintf(x64emu_t* emu, void** buff, void * fmt, uint64_t * b) __attribute__((alias("my_asprintf"))); @@ -1281,7 +1793,20 @@ EXPORT int my_vasprintf(x64emu_t* emu, char** buff, void* fmt, x64_va_list_t b) myStackAlignValist(emu, (const char*)fmt, emu->scratch, b); PREPARE_VALIST; #endif - return vasprintf(buff, fmt, VARARGS); + char* host = NULL; + int r = vasprintf(&host, fmt, VARARGS); + if(r<0 || !host || !buff) { + if(buff) *buff = NULL; + return -1; + } + if(!guest_alloc_is_active()) { + *buff = host; + return r; + } + char* ret = guest_copy_string(host); + box_free(host); + *buff = ret; + return ret ? r : -1; } EXPORT int my_vsprintf(x64emu_t* emu, void* buff, void * fmt, x64_va_list_t b) { @@ -1489,14 +2014,39 @@ EXPORT int my___vasprintf_chk(x64emu_t* emu, void* buff, int flags, void* fmt, x myStackAlignValist(emu, (const char*)fmt, emu->scratch, b); PREPARE_VALIST; #endif - int r = vasprintf(buff, fmt, VARARGS); - return r; + char* host = NULL; + int r = vasprintf(&host, fmt, VARARGS); + if(r<0 || !host || !buff) { + if(buff) *(char**)buff = NULL; + return -1; + } + if(!guest_alloc_is_active()) { + *(char**)buff = host; + return r; + } + char* ret = guest_copy_string(host); + box_free(host); + *(char**)buff = ret; + return ret ? r : -1; } EXPORT int my___asprintf_chk(x64emu_t* emu, void* result_ptr, int flags, void* fmt, void* b) { myStackAlign(emu, (const char*)fmt, b, emu->scratch, R_EAX, 3); PREPARE_VALIST; - return vasprintf((char**)result_ptr, (char*)fmt, VARARGS); + char* host = NULL; + int r = vasprintf(&host, (char*)fmt, VARARGS); + if(r<0 || !host || !result_ptr) { + if(result_ptr) *(char**)result_ptr = NULL; + return -1; + } + if(!guest_alloc_is_active()) { + *(char**)result_ptr = host; + return r; + } + char* ret = guest_copy_string(host); + box_free(host); + *(char**)result_ptr = ret; + return ret ? r : -1; } EXPORT int my_vswprintf(x64emu_t* emu, void* buff, size_t s, void * fmt, x64_va_list_t b) { (void)emu; @@ -2667,20 +3217,62 @@ EXPORT int32_t my_glob(x64emu_t *emu, void* pat, int32_t flags, void* errfnc, vo EXPORT int my_scandir64(x64emu_t *emu, void* dir, void* namelist, void* sel, void* comp) { (void)emu; - return scandir(dir, namelist, findfilter64Fct(sel), findcompare64Fct(comp)); + if(!namelist) { + errno = EINVAL; + return -1; + } + struct dirent** hostlist = NULL; + int n = scandir(dir, &hostlist, findfilter64Fct(sel), findcompare64Fct(comp)); + if(n<0) + return n; + if(!guest_alloc_is_active()) { + *(struct dirent***)namelist = hostlist; + return n; + } + int ret = guest_scandir_copy((struct dirent***)namelist, hostlist, n); + free_host_dirent_list(hostlist, n); + return ret; } EXPORT int my_scandir(x64emu_t *emu, void* dir, void* namelist, void* sel, void* comp) __attribute__((alias("my_scandir64"))); EXPORT int my_scandirat(x64emu_t *emu, int dirfd, void* dirp, void* namelist, void* sel, void* comp) { (void)emu; - return scandirat(dirfd, dirp, namelist, findfilter64Fct(sel), findcompare64Fct(comp)); + if(!namelist) { + errno = EINVAL; + return -1; + } + struct dirent** hostlist = NULL; + int n = scandirat(dirfd, dirp, &hostlist, findfilter64Fct(sel), findcompare64Fct(comp)); + if(n<0) + return n; + if(!guest_alloc_is_active()) { + *(struct dirent***)namelist = hostlist; + return n; + } + int ret = guest_scandir_copy((struct dirent***)namelist, hostlist, n); + free_host_dirent_list(hostlist, n); + return ret; } EXPORT int my_scandirat64(x64emu_t *emu, int dirfd, void* dirp, void* namelist, void* sel, void* comp) { (void)emu; - return scandirat(dirfd, dirp, namelist, findfilter64Fct(sel), findcompare64Fct(comp)); + if(!namelist) { + errno = EINVAL; + return -1; + } + struct dirent** hostlist = NULL; + int n = scandirat(dirfd, dirp, &hostlist, findfilter64Fct(sel), findcompare64Fct(comp)); + if(n<0) + return n; + if(!guest_alloc_is_active()) { + *(struct dirent***)namelist = hostlist; + return n; + } + int ret = guest_scandir_copy((struct dirent***)namelist, hostlist, n); + free_host_dirent_list(hostlist, n); + return ret; } EXPORT int my_ftw64(x64emu_t* emu, void* filename, void* func, int descriptors) @@ -3621,10 +4213,15 @@ EXPORT void my___explicit_bzero_chk(x64emu_t* emu, void* dst, uint32_t len, uint EXPORT void* my_realpath(x64emu_t* emu, void* path, void* resolved_path) { - if(path && isProcSelf(path, "exe")) { - return realpath(emu->context->fullpath, resolved_path); - } - return realpath(path, resolved_path); + const char* p = (path && isProcSelf(path, "exe")) ? emu->context->fullpath : (const char*)path; + if(resolved_path || !guest_alloc_is_active()) + return realpath(p, resolved_path); + char* host = realpath(p, NULL); + if(!host) + return NULL; + char* ret = guest_copy_string(host); + box_free(host); + return ret; } EXPORT int my_readlinkat(x64emu_t* emu, int fd, void* path, void* buf, size_t bufsize) @@ -3938,6 +4535,14 @@ EXPORT void* my_mmap64(x64emu_t* emu, void *addr, size_t length, int prot, int f } EXPORT void* my_mmap(x64emu_t* emu, void *addr, size_t length, int prot, int flags, int fd, ssize_t offset) __attribute__((alias("my_mmap64"))); +static void setMremapProtection(uintptr_t addr, size_t size, uint32_t prot, uint32_t flag) +{ + if(flag) + setProtection_flag(addr, size, prot, flag); + else + setProtection_mmap(addr, size, prot); +} + EXPORT void* my_mremap(x64emu_t* emu, void* old_addr, size_t old_size, size_t new_size, int flags, void* new_addr) { #ifdef DYNAREC @@ -3945,6 +4550,7 @@ EXPORT void* my_mremap(x64emu_t* emu, void* old_addr, size_t old_size, size_t ne last_mmap_0_len = 0; #endif (void)emu; + uint32_t oldflag = (uint32_t)memExist((uintptr_t)old_addr); lockMmapMutex(); if((emu || box64_is32bits) && (BOX64ENV(log)>=LOG_DEBUG || BOX64ENV(dynarec_log)>=LOG_DEBUG)) {printf_log(LOG_NONE, "mremap(%p, %lu, %lu, %d, %p)=>", old_addr, old_size, new_size, flags, new_addr);} void* ret = mremap(old_addr, old_size, new_size, flags, new_addr); @@ -3953,7 +4559,7 @@ EXPORT void* my_mremap(x64emu_t* emu, void* old_addr, size_t old_size, size_t ne uint32_t prot = getProtection((uintptr_t)old_addr)&~PROT_CUSTOM; if(ret==old_addr) { if(old_size && old_size +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void (*real_free)(void*); +static void* (*real_realloc)(void*, size_t); + +#define MAGIC 0x6761616c6c6f6331ULL +typedef struct { + uint64_t magic; + size_t size; +} hdr_t; + +static char* arena_brk; +static char* arena_mmap; +static size_t used_brk; +static size_t used_mmap; +static size_t cap_brk = 2u << 20; +static size_t cap_mmap = 1u << 20; + +static void init_allocator(void) __attribute__((constructor)); +static void init_allocator(void) +{ + real_free = (void (*)(void*))dlsym(RTLD_NEXT, "free"); + real_realloc = (void* (*)(void*, size_t))dlsym(RTLD_NEXT, "realloc"); + arena_brk = (char*)sbrk(cap_brk); + if(arena_brk==(void*)-1) + arena_brk = NULL; + arena_mmap = (char*)mmap(NULL, cap_mmap, PROT_READ|PROT_WRITE, + MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + if(arena_mmap==MAP_FAILED) + arena_mmap = NULL; +} + +static int in_range(const void* p, const char* base, size_t used) +{ + return base && (const char*)p >= base && (const char*)p < base + used; +} + +static int guest_pointer(const void* p) +{ + return in_range(p, arena_brk, used_brk) || in_range(p, arena_mmap, used_mmap); +} + +static void* bump(size_t size) +{ + size_t total = (size + sizeof(hdr_t) + 15) & ~(size_t)15; + size_t* used; + char* base; + size_t cap; + if(arena_brk && used_brk + total <= cap_brk) { + used = &used_brk; + base = arena_brk; + cap = cap_brk; + } else if(arena_mmap && used_mmap + total <= cap_mmap) { + used = &used_mmap; + base = arena_mmap; + cap = cap_mmap; + } else { + errno = ENOMEM; + return NULL; + } + size_t off = __atomic_fetch_add(used, total, __ATOMIC_RELAXED); + if(off + total > cap) { + errno = ENOMEM; + return NULL; + } + hdr_t* h = (hdr_t*)(base + off); + h->magic = MAGIC; + h->size = size; + return h + 1; +} + +static inline void* fast_alloc(size_t size) __attribute__((always_inline)); +static inline void* fast_alloc(size_t size) +{ + return bump(size); +} + +static inline void fast_free(void* p) __attribute__((always_inline)); +static inline void fast_free(void* p) +{ + if(!p) + return; + if(guest_pointer(p)) { + hdr_t* h = ((hdr_t*)p) - 1; + if(h->magic != MAGIC) { + printf("BAD_FREE\n"); + _exit(2); + } + h->magic = 0; + return; + } + real_free(p); +} + +void* malloc(size_t size) +{ + return bump(size); +} + +void* calloc(size_t n, size_t size) +{ + if(n && size > (size_t)-1 / n) { + errno = ENOMEM; + return NULL; + } + void* p = bump(n * size); + if(p) + memset(p, 0, n * size); + return p; +} + +void free(void* p) +{ + fast_free(p); +} + +void* realloc(void* p, size_t size) +{ + if(!p) + return bump(size); + if(!guest_pointer(p)) + return real_realloc(p, size); + hdr_t* h = ((hdr_t*)p) - 1; + if(h->magic != MAGIC) { + printf("BAD_REALLOC\n"); + _exit(2); + } + size_t old = h->size; + void* ret = bump(size); + if(!ret) + return NULL; + memcpy(ret, p, old < size ? old : size); + return ret; +} + +size_t malloc_usable_size(void* p) +{ + if(!p) + return 0; + if(guest_pointer(p)) { + hdr_t* h = ((hdr_t*)p) - 1; + return h->magic == MAGIC ? h->size : 0; + } + return 0; +} + +static int no_dots(const struct dirent* d) +{ + return d->d_name[0] != '.'; +} + +static void* thread_work(void* arg) +{ + (void)arg; + for(int i=0; i<2000; ++i) { + char* p = (i & 1) ? (char*)malloc(32) : (char*)fast_alloc(48); + char* s = strdup("thread-string"); + if(!p || !s) + return (void*)1; + if(!guest_pointer(s)) + return (void*)1; + if(i & 1) + free(p); + else + fast_free(p); + free(s); + } + return NULL; +} + +int main(void) +{ + char* s = strdup("hello"); + printf("strdup guest=%d\n", guest_pointer(s)); + free(s); + + s = strndup("abcdef", 3); + printf("strndup guest=%d val=%s\n", guest_pointer(s), s ? s : "(null)"); + free(s); + + wchar_t* ws = wcsdup(L"wide"); + printf("wcsdup guest=%d\n", guest_pointer(ws)); + free(ws); + + char* a = NULL; + int r = asprintf(&a, "%s-%d", "asprintf", 42); + printf("asprintf guest=%d val=%s\n", guest_pointer(a), (r >= 0 && a) ? a : "(null)"); + free(a); + + char* line = (char*)fast_alloc(4); + size_t linecap = 4; + FILE* f = fmemopen("abcdefghij\n", 11, "r"); + long l = getline(&line, &linecap, f); + printf("getline guest=%d len=%ld val=%.*s\n", guest_pointer(line), l, + (int)(l > 0 ? l - 1 : 0), line ? line : ""); + fclose(f); + free(line); + + char cwd[512]; + char* gcw = getcwd(NULL, 0); + int same = gcw && getcwd(cwd, sizeof(cwd)) && !strcmp(gcw, cwd); + printf("getcwd guest=%d same=%d\n", guest_pointer(gcw), same); + free(gcw); + + char* cdn = get_current_dir_name(); + printf("curdir guest=%d\n", guest_pointer(cdn)); + free(cdn); + + char* canon = canonicalize_file_name("/tmp"); + printf("canon guest=%d\n", guest_pointer(canon)); + free(canon); + + char* rp = realpath("/tmp", NULL); + printf("realpath guest=%d\n", guest_pointer(rp)); + free(rp); + + char* tmp = tempnam("/tmp", "ga38"); + printf("tempnam guest=%d\n", guest_pointer(tmp)); + free(tmp); + + char* msbuf = (char*)fast_alloc(16); + size_t mslen = 0; + FILE* ms = open_memstream(&msbuf, &mslen); + fputs("memstream", ms); + fclose(ms); + printf("memstream guest=%d len=%zu val=%s\n", guest_pointer(msbuf), mslen, msbuf); + free(msbuf); + + char tmpl[] = "/tmp/ga38XXXXXX"; + char* dir = mkdtemp(tmpl); + if(dir) { + const char* names[] = {"b", "a", "c"}; + for(int i=0; i<3; ++i) { + char path[256]; + snprintf(path, sizeof(path), "%s/%s", dir, names[i]); + int fd = open(path, O_CREAT|O_WRONLY, 0600); + if(fd>=0) + close(fd); + } + struct dirent** list = NULL; + int n = scandir(dir, &list, no_dots, alphasort); + int g = (n==3) && guest_pointer(list); + char joined[16] = {0}; + for(int i=0; id_name); + } + printf("scandir n=%d guest=%d names=%s\n", n, g, joined); + for(int i=0; i