From e451ce6a0d8c6d8b9a65b15eb944203440d89914 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sat, 31 Oct 2026 19:57:09 +0300 Subject: [PATCH] Report abort and invalid C runtime parameters as crashes on Windows abort() (a failed assert, std::terminate) and an invalid parameter to a C runtime function raise no exception: the C runtime ends the process with 0xc0000409 and the log stops without a FATAL line. A SIGABRT handler and an invalid-parameter handler now send both to the crash hook with the host return addresses in totk.exe, and the report lists them as totk+offset frames after the guest registers. Checked with win32.c in a test program: assert, abort and fclose(NULL) each give one report whose innermost frame is the failing call. lifted_function no longer names the nearest lifted function for an address above the lifted code, so a host frame of the runtime is not reported as game code. Co-Authored-By: Claude Opus 5.5 --- runtime/main.c | 12 +++++++++++- runtime/platform/platform.h | 6 +++++- runtime/platform/win32.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/runtime/main.c b/runtime/main.c index 893a2764..137bb264 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -183,7 +183,8 @@ static int by_host(const void *a, const void *b) { return x < y ? -1 : x > y; } -// The lifted function that contains a host code address, by the nearest function start below it. +// The lifted function that contains a host code address, by the nearest function start below it; 0 outside the lifted +// code. static uint64_t lifted_function(uintptr_t rip) { if (!fn_rev) { static RtModule mods[MAX_MODULES]; @@ -197,6 +198,9 @@ static uint64_t lifted_function(uintptr_t rip) { fn_rev[fn_rev_n++] = (FnRev){(uintptr_t)mods[i].fns[k], mods[i].addrs[k]}; qsort(fn_rev, fn_rev_n, sizeof *fn_rev, by_host); } + // The lifted code is one range with the host code outside it (kernel.c ap_init checks that); an address above the + // last function start is taken for host code, which costs only the last function's body. + if (!fn_rev_n || rip > fn_rev[fn_rev_n - 1].host) return 0; uint64_t lo = 0, hi = fn_rev_n; while (lo < hi) { uint64_t mid = (lo + hi) / 2; @@ -220,6 +224,12 @@ static void crash_handler(const PlatCrash *x) { rt_log_raw("\n", 1); uint64_t f = lifted_function(rip); if (f) rt_logf(" in lifted function f_%llx = %s\n", (unsigned long long)f, ldr_describe(f, d, sizeof d)); + for (int i = 0; i < x->nframes; i++) { // abort and invalid parameter: where the host code called it + uint64_t g = lifted_function(x->frames[i]); + rt_logf(" host frame %2d: totk+%#llx", i, (unsigned long long)(x->frames[i] - (rip - x->offset))); + if (g) rt_logf(" in lifted function f_%llx", (unsigned long long)g); + rt_log_raw("\n", 1); + } Cpu *c = kern_cpu(); if (c) { for (int i = 0; i < 31; i++) diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index 5d2c9ef9..d5bf6da7 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -203,13 +203,17 @@ _Noreturn void plat_reexec(void); // A crash in this executable's own code (an access violation, an illegal instruction, a division by zero, a stack // overflow) goes to fn, which reports it and must end the process. Faults in other modules (drivers) are left to the -// OS. The write-fault hook above runs first. +// OS. The write-fault hook above runs first. On Windows abort() (a failed assert, std::terminate) and an invalid +// parameter to a C runtime function go there too, with the host frames in this executable: the C runtime ends the +// process for both with 0xc0000409 and no exception. typedef struct { const char *what; // "access violation", "illegal instruction", ... uint32_t code; // the Windows exception code or the signal number uintptr_t rip, offset; // the faulting instruction, and its offset in the executable int access; // the data access that faulted: 0 read, 1 write, 8 execute; -1 when not an access uint64_t addr; // its address when access >= 0 + int nframes; // abort and invalid parameter: return addresses in this executable, innermost first + uintptr_t frames[16]; // (rip is frames[0]); 0 for a fault, whose rip is exact } PlatCrash; void plat_crash_hook(void (*fn)(const PlatCrash *c)); int plat_mem_read(uint64_t addr, void *out, size_t n); // copies n bytes, 0 instead of faulting on unmapped memory diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index 7a65869d..0856db7d 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -582,9 +583,37 @@ static LONG CALLBACK crash_veh(EXCEPTION_POINTERS *ep) { return EXCEPTION_CONTINUE_SEARCH; } +// abort() and an invalid parameter to a C runtime function raise no exception: the C runtime ends the process with +// 0xc0000409 after calling the SIGABRT or invalid-parameter handler. The report names the host frames in this +// executable; the innermost is the caller of abort (assert) or of the C runtime function. Skipped frames: this function +// and the handler that called it. +__attribute__((noinline)) static void crash_from_runtime(const char *what, uint32_t code) { + PlatCrash c = {what, code, 0, 0, -1, 0}; + void *f[62]; + USHORT n = RtlCaptureStackBackTrace(2, 62, f, NULL); + MODULEINFO mi; + if (GetModuleInformation(GetCurrentProcess(), GetModuleHandleW(NULL), &mi, sizeof mi)) { + uintptr_t base = (uintptr_t)mi.lpBaseOfDll; + for (USHORT i = 0; i < n && c.nframes < (int)(sizeof c.frames / sizeof *c.frames); i++) + if ((uintptr_t)f[i] - base < mi.SizeOfImage) c.frames[c.nframes++] = (uintptr_t)f[i]; + if (c.nframes) c.rip = c.frames[0], c.offset = c.rip - base; + } + crash_fn(&c); +} + +static void abort_signal(int sig) { crash_from_runtime("abort", (uint32_t)sig); } + +static void invalid_parameter(const wchar_t *expr, const wchar_t *func, const wchar_t *file, unsigned line, + uintptr_t reserved) { + (void)expr, (void)func, (void)file, (void)line, (void)reserved; // NULL and 0 in the release C runtime + crash_from_runtime("invalid parameter to a C runtime function", 0xc0000417u); // STATUS_INVALID_CRUNTIME_PARAMETER +} + void plat_crash_hook(void (*fn)(const PlatCrash *c)) { crash_fn = fn; AddVectoredExceptionHandler(0, crash_veh); + signal(SIGABRT, abort_signal); + _set_invalid_parameter_handler(invalid_parameter); } // A fresh process gets a fresh layout: Windows places DLLs, heaps and thread stacks at random addresses. The parent