Keep 32 KiB of every thread's and fiber's stack back for a stack overflow's crash report.

A stack overflow leaves the faulting thread only a guard page's worth of stack for the handler, and the crash
report needs more: main.c crash_handler's rt_log buffers are 2 KiB each, it walks the frames, and while the boot
builds pipelines its first call writes a file (plat_file_write's frame is about 4 KiB). Nothing in the runtime set
a stack guarantee, so the report faulted again; the verifier's probe ended with 0xC0000005 and no line once the
report's first call was the file write. The launcher starts the game with stderr on DEVNULL, so raw_note's stderr
line was all there was, and nobody saw it.

The new platform plat_stack_guarantee calls SetThreadStackGuarantee with PLAT_STACK_GUARANTEE (32 KiB) and runs
at the start of every thread win32.c thread_main starts, in fiber_start (a guest fiber's stack is its own and the
guarantee belongs to the running fiber), and on the main thread in main. Each thread and fiber commits that much
more of its stack. Linux's crash handling already runs on its signal stack (linux.c alt_stack), so it is empty
there.

Reproduced before the change by the new tests/crash_stack_check.py: a plat_thread_spawn thread and a plat_fiber_new
fiber recurse until their stacks run out, and the handler writes a file and three 2 KiB log lines. Without the
guarantee both ended with 0xC0000005 and no line; with it both write every line and end with 71.
thread_end_check, fatal_exit_check and the compile checks pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 17:18:05 -05:00
parent 790d201a59
commit 722a5dd1b0
6 changed files with 168 additions and 0 deletions

View File

@@ -44,8 +44,20 @@ struct PlatThread {
// ExitThread jumps back to the thread's start).
static void thread_handles_close(void);
// A stack overflow leaves the faulting thread a guard page's worth of stack, about 12 KiB of which a crash handler
// gets, and the report (main.c crash_handler) needs more: rt_log's 2 KiB buffers, the frame walk, and during the
// boot's builds a file write first (plat_file_write's frame is about 4 KiB). It faulted again inside the report, and
// the process ended with no FATAL line (the audit's probe: a 16 MiB-reserve thread recursing in 1 KiB frames). With
// 32 KiB kept back, the probe's report ran whole with room to spare. The guarantee belongs to the running fiber, so
// fiber_start asks for it too. Each thread and fiber commits that much more of its stack.
void plat_stack_guarantee(void) {
ULONG g = PLAT_STACK_GUARANTEE;
SetThreadStackGuarantee(&g);
}
static DWORD WINAPI thread_main(LPVOID p) {
PlatThread *t = p;
plat_stack_guarantee();
t->fn(t->arg);
plat_thread_end_run();
thread_handles_close();
@@ -336,6 +348,7 @@ void plat_fiber_unself(void) {
static void WINAPI fiber_start(void *p) {
PlatFiber *f = p;
plat_stack_guarantee(); // a guest fiber's stack is its own (kernel.c kern_fiber_init)
f->fn(f->arg);
}