diff --git a/runtime/main.c b/runtime/main.c index d2c68575..e3428719 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -228,23 +228,34 @@ static void crash_handler(const PlatCrash *x) { rt_logf(", %s address %#llx", x->access == 1 ? "writing" : x->access == 8 ? "executing" : "reading", (unsigned long long)x->addr); rt_log_raw("\n", 1); + if (x->cxx_type) + rt_logf(" C++ type %s%s%s\n", x->cxx_type, x->cxx_what ? ", what(): " : "", x->cxx_what ? x->cxx_what : ""); uint64_t f = x->module ? 0 : lifted_function(rip); if (f) rt_logf(" in lifted function f_%llx = %s\n", (unsigned long long)f, ldr_describe(f, d, sizeof d)); - // abort and invalid parameter: where the host code called it; a fault in another module: the host calls that led - // into it + // abort and invalid parameter: where the host code called it; a fault in another module: the driver code and the + // host calls that led into it uintptr_t exe = plat_exe_base(); for (int i = 0; i < x->nframes; i++) { + const char *m = x->frame_module[i]; + if (m) { + rt_logf(" host frame %2d: %s+%#llx\n", i, *m ? m : "outside every module", + (unsigned long long)x->frame_offset[i]); + continue; + } uint64_t g = lifted_function(x->frames[i]); rt_logf(" host frame %2d: totk+%#llx", i, (unsigned long long)(x->frames[i] - exe)); if (g) rt_logf(" in lifted function f_%llx", (unsigned long long)g); rt_log_raw("\n", 1); } + if (x->frames_left_out) rt_logf(" (%d frames in other modules left out)\n", x->frames_left_out); Cpu *c = kern_cpu(); if (c) { for (int i = 0; i < 31; i++) rt_logf(" x%-2d %016llx%s", i, (unsigned long long)c->x[i], i % 4 == 3 ? "\n" : ""); - rt_logf(" sp %016llx\n pc %016llx lr %s\n", (unsigned long long)c->sp, (unsigned long long)c->pc, - ldr_describe(c->x[30], d, sizeof d)); + // pc: the guest code running, or the host function a guest call entered (its name tells the host call apart) + rt_logf(" sp %016llx\n pc %016llx", (unsigned long long)c->sp, (unsigned long long)c->pc); + if (hostfn_name(c->pc)) rt_logf(" (%s)", ldr_describe(c->pc, d, sizeof d)); + rt_logf(" lr %s\n", ldr_describe(c->x[30], d, sizeof d)); uint64_t fp = c->x[29]; for (int n = 0; n < 32 && fp && !(fp & 7) && readable(fp, 16); n++) { uint64_t next = LD64(fp), lr = LD64(fp + 8); diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index eea834e0..97911d27 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -222,9 +222,10 @@ _Noreturn void plat_reexec(void); // - A fault in this executable's own code (an access violation, an illegal instruction, a division by zero, a stack // overflow) is reported as it happens. // - A fault in another module (a GPU driver, a system library) is reported only when nothing handles it: on Windows -// from the unhandled-exception filter, with the return addresses in this executable on the faulting thread's stack, -// so the report shows which host call ran into the driver; on Linux from the signal handler, without frames. Drivers -// that raise and catch their own exceptions never reach fn. +// from the unhandled-exception filter, with the return addresses on the faulting thread's stack in every module, so +// the report shows which driver code failed and which host call ran into it; on Linux from the signal handler, +// without frames. Drivers that raise and catch their own exceptions never reach fn. For a C++ exception thrown by +// MSVC-built code (a driver) the report names its type and, for a std::exception, its what() text. // - 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. @@ -234,9 +235,14 @@ typedef struct { uintptr_t rip, offset; // the faulting instruction, and its offset in its module 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; // return addresses in this executable, innermost first: for abort and invalid parameter - uintptr_t frames[16]; // (rip is frames[0]) and for a fault in another module; 0 for a fault in this one + int nframes; // return addresses, innermost first: for abort and invalid parameter (rip is frames[0]) + uintptr_t frames[32]; // and for a fault in another module; 0 for a fault in this one + const char *frame_module[32]; // the module of each frame: NULL for this executable, "" for none + uintptr_t frame_offset[32]; // each frame's offset in its module + int frames_left_out; // frames in other modules past the limit the walk keeps, so this executable's still fit const char *module; // the module holding rip: NULL for this executable, "" for none (a jump to address 0) + const char *cxx_type; // a C++ exception from MSVC-built code: the thrown type's decorated name (.?AV...@@) + const char *cxx_what; // and what() when the type derives from std::exception; NULL when unknown } 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 4f17994f..5dda4538 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -674,16 +674,20 @@ static const char *image_name(const char *base) { return base + ((const IMAGE_EXPORT_DIRECTORY *)(base + d->VirtualAddress))->Name; } -// The return addresses in [lo, lo + size) on the crashed thread's stack, innermost first: unwinds the faulting context +// The return addresses on the crashed thread's stack, innermost first, with their modules: unwinds the faulting context // with the x64 unwind tables; a function without a table entry (a leaf, or address 0 after a call through a null // pointer) has its return address on top of the stack. The walk stops at return address 0 (the thread's first frame) -// and when the stack pointer leaves the thread's stack, so a damaged stack ends it instead of faulting. -static int frames_in(const CONTEXT *at, uintptr_t lo, uintptr_t size, uintptr_t *out, int max) { +// and when the stack pointer leaves the thread's stack, so a damaged stack ends it instead of faulting. A driver can +// nest deeper than the report holds, so frames in other modules stop being kept after the first 12 (counted in +// frames_left_out) and the frames in this executable, which name the host call that ran into the driver, still fit. +static void frames_of(const CONTEXT *at, PlatCrash *x) { + enum { MAX = sizeof x->frames / sizeof *x->frames, OTHER_MAX = 12 }; CONTEXT c = *at; ULONG_PTR low, high; GetCurrentThreadStackLimits(&low, &high); - int n = 0; - for (int i = 0; i < 256 && n < max && (c.Rip || !i) && c.Rsp >= low && c.Rsp + 8 <= high; i++) { + void *exe = GetModuleHandleW(NULL); + int other = 0; + for (int i = 0; i < 256 && x->nframes < MAX && (c.Rip || !i) && c.Rsp >= low && c.Rsp + 8 <= high; i++) { DWORD64 img; RUNTIME_FUNCTION *fe = RtlLookupFunctionEntry(c.Rip, &img, NULL); if (fe) { @@ -694,9 +698,64 @@ static int frames_in(const CONTEXT *at, uintptr_t lo, uintptr_t size, uintptr_t c.Rip = *(const DWORD64 *)c.Rsp; c.Rsp += 8; } - if (c.Rip - lo < size) out[n++] = (uintptr_t)c.Rip; + if (!c.Rip) break; + void *base = NULL; + RtlPcToFileHeader((void *)(uintptr_t)c.Rip, &base); + if (base != exe && other++ >= OTHER_MAX) { + x->frames_left_out++; + continue; + } + int n = x->nframes++; + x->frames[n] = (uintptr_t)c.Rip; + x->frame_module[n] = base == exe ? NULL : base ? image_name(base) : ""; + x->frame_offset[n] = (uintptr_t)c.Rip - (uintptr_t)base; } - return n; +} + +// MSVC's C++ throw (0xe06d7363) passes the thrown object in ExceptionInformation[1], its ThrowInfo in [2] and the image +// base the ThrowInfo's 32-bit offsets are relative to in [3] (magic 0x19930520..0x19930522 in [0]). The ThrowInfo lists +// the types a catch may match, the thrown type first; each type descriptor holds a vtable pointer, a spare pointer and +// the decorated name. A std::exception keeps its what() text in the pointer right after its vtable pointer (MSVC's +// __std_exception_data). Every read is checked: the exception may come from a damaged heap. +typedef struct { uint32_t attributes; int32_t unwind, forward_compat, catchable_types; } CxxThrowInfo; +typedef struct { uint32_t properties; int32_t type, this_mdisp, this_pdisp, this_vdisp, size, copy; } CxxCatchableType; + +// Copies the string at p into buf (cut to n - 1 characters), checking each page before reading it; NULL when the first +// byte cannot be read. +static const char *copy_cstr(uintptr_t p, char *buf, size_t n) { + size_t i = 0; + for (; p && i + 1 < n; i++) { + if ((!i || !((p + i) & 4095)) && !plat_mem_readable(p + i, 1)) break; + if (!(buf[i] = ((const char *)p)[i])) return buf; + } + buf[i] = 0; + return i ? buf : NULL; +} + +static void cxx_info(const EXCEPTION_RECORD *er, PlatCrash *x) { + if (er->NumberParameters < 4 || er->ExceptionInformation[0] - 0x19930520u > 2) return; + uintptr_t obj = er->ExceptionInformation[1], info = er->ExceptionInformation[2], + base = er->ExceptionInformation[3]; + if (!info || !plat_mem_readable(info, sizeof(CxxThrowInfo))) return; + const CxxThrowInfo *ti = (const void *)info; + uintptr_t arr = base + (uintptr_t)(intptr_t)ti->catchable_types; + if (!ti->catchable_types || !plat_mem_readable(arr, 4)) return; + int32_t n = *(const int32_t *)arr; + if (n <= 0 || n > 64 || !plat_mem_readable(arr + 4, 4u * (uint64_t)n)) return; + static char type[128], what[256]; // the crash report runs once: crash_fn ends the process + int exception = 0; + for (int32_t i = 0; i < n; i++) { + uintptr_t ct = base + (uintptr_t)(intptr_t)((const int32_t *)(arr + 4))[i]; + if (!plat_mem_readable(ct, sizeof(CxxCatchableType))) continue; + uintptr_t td = base + (uintptr_t)(intptr_t)((const CxxCatchableType *)ct)->type; + char name[64]; + if (!copy_cstr(td + 16, i ? name : type, i ? sizeof name : sizeof type)) // past vtable and spare pointers + continue; + if (!i) x->cxx_type = type; + if (!strcmp(i ? name : type, ".?AVexception@std@@")) exception = 1; + } + if (exception && obj && plat_mem_readable(obj + 8, 8)) + x->cxx_what = copy_cstr(*(const uintptr_t *)(obj + 8), what, sizeof what); } // The last-chance filter: an exception no handler took, in any module. crash_veh reports faults in this executable as @@ -723,14 +782,12 @@ static LONG WINAPI crash_filter(EXCEPTION_POINTERS *ep) { c.access = (int)er->ExceptionInformation[0], c.addr = er->ExceptionInformation[1]; void *base = NULL; RtlPcToFileHeader((void *)rip, &base); - MODULEINFO mi; uintptr_t exe = (uintptr_t)GetModuleHandleW(NULL); c.offset = rip - (uintptr_t)base; if (!base) c.module = ""; else if ((uintptr_t)base != exe) c.module = image_name(base); - if (c.module && GetModuleInformation(GetCurrentProcess(), (HMODULE)exe, &mi, sizeof mi)) - c.nframes = frames_in(ep->ContextRecord, exe, mi.SizeOfImage, c.frames, - (int)(sizeof c.frames / sizeof *c.frames)); + if (c.module) frames_of(ep->ContextRecord, &c); + if (code == 0xe06d7363u) cxx_info(er, &c); crash_fn(&c); return EXCEPTION_CONTINUE_SEARCH; }