Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 0 additions & 15 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 0 additions & 15 deletions docs/USAGE_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 中可用。
Expand Down
17 changes: 0 additions & 17 deletions docs/box64.pod
Original file line number Diff line number Diff line change
Expand Up @@ -577,14 +577,6 @@ Force loading libraries with the binary.
* XXXX:YYYY : Force the loading of libraries XXXX and YYYY.


=item B<BOX64_LIBCEF> =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<BOX64_LIBGL> =I<libXXXX|/path/to/libXXXX>

Set the name for libGL.
Expand All @@ -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<BOX64_MALLOC_HACK> =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<BOX64_MAXCPU> =I<0|XXXX>

Maximum CPU cores exposed. Available in WowBox64.
Expand Down
43 changes: 0 additions & 43 deletions docs/gen/usage.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down
15 changes: 0 additions & 15 deletions docs/gen/usage_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down
6 changes: 3 additions & 3 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ void setupTrace()
#endif
}
#ifndef STATICBUILD
void endMallocHook();
void guest_alloc_fini(void);
#endif

void finiPendingDLOpenedNoUnload(x64emu_t* emu);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
146 changes: 145 additions & 1 deletion src/custommem.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifndef _WIN32
#include <sys/syscall.h>
#include <sched.h>
#endif

#include "os.h"
#include "backtrace.h"
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading