waterbox linux - rework to avoid possible memory leak

Bizhawk never would hit this because it only ever runs waterboxes in one host thread, but an application that spun up many threads and ran waterboxes in each would leak 32 bytes of heap for each native thread destroyed, which is super duper not really meaningful at all
This commit is contained in:
nattthebear 2020-07-08 07:21:54 -04:00
parent 7f28bc2caf
commit a67fa70632
2 changed files with 9 additions and 2 deletions

Binary file not shown.

View File

@ -68,6 +68,11 @@ pub struct Context {
pub extcall_slots: [Option<ExternalCallback>; 64],
}
#[cfg(unix)]
thread_local!(static TIB: Box<[usize; 4]> = Box::new([0usize; 4]));
/// Prepares this host thread to be allowed to call guest code. Noop if already called.
/// Only needs to happen once per host thread
pub fn prepare_thread() {
@ -84,8 +89,10 @@ pub fn prepare_thread() {
let mut gs = 0usize;
assert_eq!(syscall(SYS_arch_prctl, 0x1004 /*ARCH_GET_GS*/, &gs), 0);
if gs == 0 {
gs = Box::into_raw(Box::new([0usize; 4])) as usize;
assert_eq!(syscall(SYS_arch_prctl, 0x1001 /*ARCH_SET_GS*/, gs), 0);
TIB.with(|b| {
gs = b.as_ref()[0] as *const usize as usize;
assert_eq!(syscall(SYS_arch_prctl, 0x1001 /*ARCH_SET_GS*/, gs), 0);
});
}
}
}