From e9edd6a9d845bc65b8f535695ad7836e126bb23f Mon Sep 17 00:00:00 2001 From: spider <51022611+spidercraft@users.noreply.github.com> Date: Sun, 8 Nov 2026 17:44:15 +0100 Subject: [PATCH] Fix two Linux crashes before Ocarina reaches gameplay plat_frame_capture was a stub on Linux, so the font rasterizer's first setjmp ended the run (setjmp: no host frame). It now walks the stack with libgcc's _Unwind_Backtrace and fills a PlatJmp of its own (glibc's jmp_buf mangles rsp, rbp and rip, so it cannot be filled by hand); plat_jmp_save and plat_jmp_go are small SysV assembly routines like the Win32 ones. plat_fiber_entry called the fiber function with rsp 8 bytes off the 16-byte alignment the SysV ABI requires, so the first aligned SSE store on a fiber (movaps in ov_poll) faulted as an access violation at address 0. It now aligns rsp before the call. Co-Authored-By: Claude Opus 5.5 --- runtime/platform/linux.c | 73 +++++++++++++++++++++++++++++++++++-- runtime/platform/platform.h | 6 +++ 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index 4b96d39b..8912d02d 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -32,6 +32,7 @@ #include #include #include +#include #include @@ -313,6 +314,7 @@ __asm__(".text\n" ".type plat_fiber_entry, @function\n" ".p2align 4\n" "plat_fiber_entry:\n" + " andq $-16, %rsp\n" // fiber_swap's ret leaves rsp 8 off: the SysV ABI wants it aligned at the call " movq %r13, %rdi\n" " callq *%r12\n" " ud2\n" @@ -1045,9 +1047,74 @@ void plat_mem_fault_hook(int (*fn)(uint64_t addr)) { void plat_crash_context(const char *s) { (void)s; } // the Windows crash notes only -int plat_frame_capture(PlatJmp *out, int (*want)(uintptr_t rip, void *arg), void *arg) { // not on Linux yet - (void)out, (void)want, (void)arg; - return 0; +// plat_jmp_save / plat_jmp_go (platform.h): leaf code that does not move rsp before it leaves, as on Windows. +_Static_assert(sizeof(PlatJmp) == 72 && __builtin_offsetof(PlatJmp, mxcsr) == 64, "plat_jmp_save layout"); +__asm__(".text\n" + ".globl plat_jmp_save\n" + ".type plat_jmp_save, @function\n" + ".p2align 4\n" + "plat_jmp_save:\n" + " movq %rbx, 0(%rdi)\n" + " movq %rbp, 8(%rdi)\n" + " movq %r12, 16(%rdi)\n" + " movq %r13, 24(%rdi)\n" + " movq %r14, 32(%rdi)\n" + " movq %r15, 40(%rdi)\n" + " leaq 8(%rsp), %rdx\n" // the caller's rsp after this call returns + " movq %rdx, 48(%rdi)\n" + " movq (%rsp), %rdx\n" + " movq %rdx, 56(%rdi)\n" + " stmxcsr 64(%rdi)\n" + " fnstcw 68(%rdi)\n" + " xorl %eax, %eax\n" + " ret\n" + ".size plat_jmp_save, .-plat_jmp_save\n" + ".globl plat_jmp_go\n" + ".type plat_jmp_go, @function\n" + ".p2align 4\n" + "plat_jmp_go:\n" + " movq 0(%rdi), %rbx\n" + " movq 8(%rdi), %rbp\n" + " movq 16(%rdi), %r12\n" + " movq 24(%rdi), %r13\n" + " movq 32(%rdi), %r14\n" + " movq 40(%rdi), %r15\n" + " ldmxcsr 64(%rdi)\n" + " fldcw 68(%rdi)\n" + " movq 48(%rdi), %rsp\n" + " movl $1, %eax\n" + " jmpq *56(%rdi)\n" + ".size plat_jmp_go, .-plat_jmp_go\n"); + +// The DWARF unwinder of libgcc (already linked for C++), frame by frame from plat_frame_capture's caller. In each +// frame's callback the IP is its return address, the callee-saved registers are as that frame sees them, and +// _Unwind_GetCFA is its rsp once the call returns (libgcc's context holds the CFA of the frame just unwound). +typedef struct { + PlatJmp *out; + int (*want)(uintptr_t rip, void *arg); + void *arg; + int n, found; +} FrameWalk; + +static _Unwind_Reason_Code frame_step(struct _Unwind_Context *ctx, void *p) { + FrameWalk *w = p; + uintptr_t ip = _Unwind_GetIP(ctx), sp = _Unwind_GetCFA(ctx); + if (w->n++ == 0) return _URC_NO_REASON; // plat_frame_capture itself + if (!ip || w->n > 256) return _URC_END_OF_STACK; + if (!w->want(ip, w->arg)) return _URC_NO_REASON; + PlatJmp *o = w->out; + o->gpr[0] = _Unwind_GetGR(ctx, 3), o->gpr[1] = _Unwind_GetGR(ctx, 6); // DWARF rbx, rbp + for (int r = 12; r <= 15; r++) o->gpr[2 + r - 12] = _Unwind_GetGR(ctx, r); + o->gpr[6] = sp, o->gpr[7] = ip; + __asm__ volatile("stmxcsr %0\n\tfnstcw %1" : "=m"(o->mxcsr), "=m"(o->fpcw)); + w->found = 1; + return _URC_END_OF_STACK; +} + +__attribute__((noinline)) int plat_frame_capture(PlatJmp *out, int (*want)(uintptr_t rip, void *arg), void *arg) { + FrameWalk w = {out, want, arg, 0, 0}; + _Unwind_Backtrace(frame_step, &w); + return w.found; } void plat_crash_hook(void (*fn)(const PlatCrash *c)) { diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index 39071559..f1830c4d 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -76,6 +76,12 @@ void plat_fiber_free(PlatFiber *f); // a fiber nobody runs typedef struct { uint64_t gpr[10]; uint32_t mxcsr, fpcw; uint64_t pad; uint8_t xmm[160]; } PlatJmp; int plat_jmp_save(PlatJmp *j) __attribute__((returns_twice, nothrow)); _Noreturn void plat_jmp_go(PlatJmp *j); +#elif defined(__linux__) && defined(__x86_64__) +// SysV: rbx, rbp, r12-r15, the caller's rsp and the return address, then MXCSR and the x87 control word (linux.c). +// Not glibc's jmp_buf: it mangles rsp, rbp and rip, so plat_frame_capture could not fill one. +typedef struct { uint64_t gpr[8]; uint32_t mxcsr, fpcw; } PlatJmp; +int plat_jmp_save(PlatJmp *j) __attribute__((returns_twice, nothrow)); +_Noreturn void plat_jmp_go(PlatJmp *j); #else #include typedef struct { jmp_buf b; } PlatJmp;