diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 3c0112060b..36850b21ea 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -616,6 +616,11 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME) target_compile_definitions(title_write_watch_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX UNICODE _UNICODE) target_compile_options(title_write_watch_test PRIVATE /W4 /permissive- /utf-8) + # Fatal-exception report (src/fatal_report.h): the fault, stack and registers in the title's terms. + add_executable(fatal_report_test test/fatal_report_test.cpp) + target_compile_definitions(fatal_report_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX UNICODE _UNICODE) + target_compile_options(fatal_report_test PRIVATE /W4 /permissive- /utf-8) + # title_memory_map (src/title_page_watch.h): map changes by range, and the texture pass that probes only what they hit. add_executable(title_memory_map_test test/title_memory_map_test.cpp) target_compile_definitions(title_memory_map_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX UNICODE _UNICODE) @@ -1741,6 +1746,9 @@ if(BUILD_TESTING) if(WIN32 AND TARGET title_memory_map_test) add_test(NAME title_memory_map COMMAND title_memory_map_test) endif() + if(WIN32 AND TARGET fatal_report_test) + add_test(NAME fatal_report COMMAND fatal_report_test) + endif() if(WIN32 AND TARGET title_write_watch_test) add_test(NAME title_write_watch COMMAND title_write_watch_test) add_test(NAME title_write_watch_on COMMAND title_write_watch_test) diff --git a/runtime/src/fatal_report.h b/runtime/src/fatal_report.h new file mode 100644 index 0000000000..9f18d9b532 --- /dev/null +++ b/runtime/src/fatal_report.h @@ -0,0 +1,75 @@ +#pragma once +// Where a fatal exception happened, in the title's own terms, for the line gta6rt prints before the process ends: +// the faulting address as an RVA of the title's image, the values near the stack pointer that point into the image +// (likely return addresses, innermost first), the general registers, and when rbx holds an object whose first +// qword points into the image, that vtable's RVA. A title's soft assert (`int3` after a report call, e.g. its +// out-of-memory report) is then traceable from the log alone. Reads go through ReadProcessMemory, so a bad stack +// pointer ends the walk instead of faulting again inside the handler. +#include + +#include +#include +#include + +namespace rt::fatal_report { + +inline bool read_qword(std::uintptr_t at, std::uintptr_t& out) { + SIZE_T got = 0; + return at && ReadProcessMemory(GetCurrentProcess(), reinterpret_cast(at), &out, sizeof(out), &got) && + got == sizeof(out); +} + +// `base`/`size`: the title's image (0 when not known yet). `max_slots` stack qwords are looked at, at most +// `max_found` image addresses listed. +inline std::string describe(const EXCEPTION_POINTERS* info, std::uintptr_t base, std::size_t size, + int max_slots = 512, int max_found = 12) { + std::string out; + char buf[512]; + const bool known = base && size; + auto inside = [&](std::uintptr_t v) { return known && v >= base && v < base + size; }; + const std::uintptr_t at = + info && info->ExceptionRecord ? reinterpret_cast(info->ExceptionRecord->ExceptionAddress) : 0; + if (inside(at)) + std::snprintf(buf, sizeof(buf), "[rt] fault at title +0x%llx\n", static_cast(at - base)); + else + std::snprintf(buf, sizeof(buf), "[rt] fault at 0x%llx (outside the title image)\n", + static_cast(at)); + out += buf; + if (!info || !info->ContextRecord) return out; + const CONTEXT& c = *info->ContextRecord; + if (known) { + std::string calls; + int found = 0; + for (int i = 0; i < max_slots && found < max_found; ++i) { + std::uintptr_t v = 0; + if (!read_qword(static_cast(c.Rsp) + std::uintptr_t(i) * 8, v)) break; + if (inside(v) && v != base) { + std::snprintf(buf, sizeof(buf), " +0x%llx", static_cast(v - base)); + calls += buf; + ++found; + } + } + out += "[rt] title addresses on the stack (innermost first):" + (calls.empty() ? std::string(" none") : calls) + + "\n"; + } + std::snprintf(buf, sizeof(buf), + "[rt] registers: rax=%llx rbx=%llx rcx=%llx rdx=%llx rsi=%llx rdi=%llx rbp=%llx rsp=%llx r8=%llx " + "r9=%llx r12=%llx r13=%llx r14=%llx r15=%llx\n", + static_cast(c.Rax), static_cast(c.Rbx), + static_cast(c.Rcx), static_cast(c.Rdx), + static_cast(c.Rsi), static_cast(c.Rdi), + static_cast(c.Rbp), static_cast(c.Rsp), + static_cast(c.R8), static_cast(c.R9), + static_cast(c.R12), static_cast(c.R13), + static_cast(c.R14), static_cast(c.R15)); + out += buf; + std::uintptr_t vtable = 0; + if (known && read_qword(static_cast(c.Rbx), vtable) && inside(vtable)) { + std::snprintf(buf, sizeof(buf), "[rt] rbx points at an object with vtable title +0x%llx\n", + static_cast(vtable - base)); + out += buf; + } + return out; +} + +} // namespace rt::fatal_report diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index c8545f338e..669429fe34 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -25,6 +25,7 @@ // (--overlay, default /overlay) so the package is never modified (shims/file_system.cpp). #include +#include #include #include #include @@ -38,6 +39,8 @@ #include +#include "fatal_report.h" + namespace { // The retail title runs a hang watchdog thread: when the game's main loop has not set its "kick" byte for 30 @@ -356,15 +359,21 @@ static LPTOP_LEVEL_EXCEPTION_FILTER WINAPI title_set_unhandled_exception_filter( InterlockedExchangePointer(&title_fatal_filter, reinterpret_cast(filter))); } +// The title's image, for rt::fatal_report (set once the title is loaded). +static std::atomic title_image_base{0}, title_image_size{0}; + static LONG WINAPI save_caches_on_fatal_exception(EXCEPTION_POINTERS* info) { const DWORD code = info && info->ExceptionRecord ? info->ExceptionRecord->ExceptionCode : 0; if (code != EXCEPTION_STACK_OVERFLOW) { // Several title threads can fault at once (measured: eight within a second). The first writes; the others // wait here for it, so none of them reaches the title's filter, which may end the process, mid-write. static std::once_flag once; - std::call_once(once, [code] { + std::call_once(once, [code, info] { std::fprintf(stderr, "[rt] unhandled exception 0x%08lx: writing the session pipeline cache before the " "process ends\n", static_cast(code)); + const std::string where = rt::fatal_report::describe( + info, title_image_base.load(std::memory_order_relaxed), title_image_size.load(std::memory_order_relaxed)); + std::fputs(where.c_str(), stderr); rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs); rt::d3d12::save_pipeline_caches_at_exit(kExitCacheWaitMs); std::fflush(stderr); @@ -668,6 +677,8 @@ int wmain(int argc, wchar_t** argv) { std::printf("running %s\n", main_module.name.c_str()); std::fflush(stdout); keep_title_watchdog_kicked(main_module.base); + title_image_base.store(reinterpret_cast(main_module.base), std::memory_order_relaxed); + title_image_size.store(main_module.size, std::memory_order_relaxed); start_title_peek(main_module.base); leonida::dev_overlay::start(); // LEONIDA_DEV_OVERLAY=1 leonida::thread_sampler::start(main_module.base, main_module.size); // LEONIDA_SAMPLE_MS (diagnostic) diff --git a/runtime/test/fatal_report_test.cpp b/runtime/test/fatal_report_test.cpp new file mode 100644 index 0000000000..2e1025185b --- /dev/null +++ b/runtime/test/fatal_report_test.cpp @@ -0,0 +1,66 @@ +// rt::fatal_report::describe (runtime/src/fatal_report.h): the fatal-exception lines in the title's terms, from a +// made-up exception record, context and stack over a stand-in "image" buffer. No game data, no GPU. +// 1. a fault inside the image is printed as an RVA; +// 2. the stack's qwords that point into the image are listed innermost first, others skipped, up to the limit; +// 3. the registers line carries rbx and rdi; +// 4. rbx at an object whose first qword points into the image gives that vtable's RVA; +// 5. a fault outside the image, an unknown image and an unreadable stack pointer give a report without faulting. +#include + +#include +#include +#include + +#include "../src/fatal_report.h" + +namespace { + +int g_checks = 0; +int g_failures = 0; + +void check(bool condition, const char* what) { + ++g_checks; + std::printf("%s %s\n", condition ? "PASS" : "FAIL", what); + if (!condition) ++g_failures; +} + +bool has(const std::string& s, const char* part) { return s.find(part) != std::string::npos; } + +} // namespace + +int main() { + static std::uint8_t image[0x1000]; + const auto base = reinterpret_cast(image); + const std::size_t size = sizeof(image); + std::uintptr_t object[2] = {base + 0x800, 0}; // "vtable" at +0x800 + std::uintptr_t stack[8] = {0x1234, base + 0x10, 7, base + 0x20, base + 0x30, 0, base + 0x40, base + 0x50}; + + EXCEPTION_RECORD rec{}; + rec.ExceptionCode = EXCEPTION_BREAKPOINT; + rec.ExceptionAddress = image + 0x650; + CONTEXT ctx{}; + ctx.Rsp = reinterpret_cast(stack); + ctx.Rbx = reinterpret_cast(object); + ctx.Rdi = 0x21c000; + EXCEPTION_POINTERS info{&rec, &ctx}; + + const std::string r = rt::fatal_report::describe(&info, base, size, 8, 4); + std::printf("%s", r.c_str()); + check(has(r, "fault at title +0x650"), "1. fault as an RVA"); + check(has(r, "stack (innermost first): +0x10 +0x20 +0x30 +0x40\n"), "2. image addresses in order, limit 4"); + check(has(r, "rbx=") && has(r, "rdi=21c000"), "3. registers"); + check(has(r, "vtable title +0x800"), "4. rbx's vtable"); + + rec.ExceptionAddress = reinterpret_cast(0x1000); + ctx.Rsp = 0x10; // unreadable + ctx.Rbx = 0; + const std::string o = rt::fatal_report::describe(&info, base, size); + check(has(o, "outside the title image") && has(o, "stack (innermost first): none"), "5. outside, bad stack"); + const std::string u = rt::fatal_report::describe(&info, 0, 0); + check(has(u, "outside the title image") && !has(u, "stack (innermost first)") && has(u, "registers"), + "5. image not known"); + check(has(rt::fatal_report::describe(nullptr, base, size), "fault at 0x0"), "5. no exception pointers"); + + std::printf("%d checks, %d failures\n", g_checks, g_failures); + return g_failures ? 1 : 0; +}