From 2be62964c740d453cdfa1ce2f0758948b86ac1ff Mon Sep 17 00:00:00 2001 From: Mark Zhuang Date: Thu, 26 Feb 2026 20:06:50 +0800 Subject: [PATCH] Detect exit() from coroutine/fiber alternate stack and convert to emu quit When exit() is called with RSP outside the known main stack range (emu->init_stack/size_stack), convert to emu->quit instead of killing the process. This handles custom coroutine libraries that call exit() as a safety fallback from malloc'd alternate stacks. --- src/wrapped/wrappedlibc.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/wrapped/wrappedlibc.c b/src/wrapped/wrappedlibc.c index 83b3c1cce..7cfe69bd8 100644 --- a/src/wrapped/wrappedlibc.c +++ b/src/wrapped/wrappedlibc.c @@ -4099,6 +4099,19 @@ EXPORT void my_exit(x64emu_t* emu, int code) emu->flags.quitonexit = 2; return; } + if(emu->size_stack && emu->init_stack) { + uintptr_t sp = R_RSP; + uintptr_t stack_lo = (uintptr_t)emu->init_stack; + uintptr_t stack_hi = stack_lo + emu->size_stack; + if(sp < stack_lo || sp > stack_hi) { + printf_log(LOG_INFO, "exit(%d) called from alternate stack " + "(RSP=%p, main stack=%p--%p), converting to emu quit\n", + code, (void*)sp, (void*)stack_lo, (void*)stack_hi); + emu->quit = 1; + R_EAX = code; + return; + } + } emu->quit = 1; box64_exit_code = code; endBox64();