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 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-10-31 19:57:09 +03:00
1 parent 637b98dea7
commit e451ce6a0d
3 files changed
+45 -2

No files matched your search

+11 -1
View File
@@ -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++)
+5 -1
View File
@@ -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
+29
View File
@@ -9,6 +9,7 @@
#include <psapi.h>
#include <shellapi.h>
#include <timeapi.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
@@ -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