Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
75 changes: 75 additions & 0 deletions runtime/src/fatal_report.h
Original file line number Diff line number Diff line change
@@ -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 <windows.h>

#include <cstdint>
#include <cstdio>
#include <string>

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<const void*>(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<std::uintptr_t>(info->ExceptionRecord->ExceptionAddress) : 0;
if (inside(at))
std::snprintf(buf, sizeof(buf), "[rt] fault at title +0x%llx\n", static_cast<unsigned long long>(at - base));
else
std::snprintf(buf, sizeof(buf), "[rt] fault at 0x%llx (outside the title image)\n",
static_cast<unsigned long long>(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<std::uintptr_t>(c.Rsp) + std::uintptr_t(i) * 8, v)) break;
if (inside(v) && v != base) {
std::snprintf(buf, sizeof(buf), " +0x%llx", static_cast<unsigned long long>(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<unsigned long long>(c.Rax), static_cast<unsigned long long>(c.Rbx),
static_cast<unsigned long long>(c.Rcx), static_cast<unsigned long long>(c.Rdx),
static_cast<unsigned long long>(c.Rsi), static_cast<unsigned long long>(c.Rdi),
static_cast<unsigned long long>(c.Rbp), static_cast<unsigned long long>(c.Rsp),
static_cast<unsigned long long>(c.R8), static_cast<unsigned long long>(c.R9),
static_cast<unsigned long long>(c.R12), static_cast<unsigned long long>(c.R13),
static_cast<unsigned long long>(c.R14), static_cast<unsigned long long>(c.R15));
out += buf;
std::uintptr_t vtable = 0;
if (known && read_qword(static_cast<std::uintptr_t>(c.Rbx), vtable) && inside(vtable)) {
std::snprintf(buf, sizeof(buf), "[rt] rbx points at an object with vtable title +0x%llx\n",
static_cast<unsigned long long>(vtable - base));
out += buf;
}
return out;
}

} // namespace rt::fatal_report
13 changes: 12 additions & 1 deletion runtime/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// (--overlay, default <game-dir>/overlay) so the package is never modified (shims/file_system.cpp).
#include <windows.h>

#include <atomic>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
Expand All @@ -38,6 +39,8 @@

#include <cstring>

#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
Expand Down Expand Up @@ -356,15 +359,21 @@ static LPTOP_LEVEL_EXCEPTION_FILTER WINAPI title_set_unhandled_exception_filter(
InterlockedExchangePointer(&title_fatal_filter, reinterpret_cast<void*>(filter)));
}

// The title's image, for rt::fatal_report (set once the title is loaded).
static std::atomic<std::uintptr_t> 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<unsigned long>(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);
Expand Down Expand Up @@ -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<std::uintptr_t>(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)
Expand Down
66 changes: 66 additions & 0 deletions runtime/test/fatal_report_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <windows.h>

#include <cstdint>
#include <cstdio>
#include <string>

#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<std::uintptr_t>(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<DWORD64>(stack);
ctx.Rbx = reinterpret_cast<DWORD64>(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<void*>(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;
}