diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index b8d284d6e..42eb1e6ba 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -4561,6 +4561,12 @@ if(TARGET Vulkan::Headers) target_compile_options(arena_blocks_test PRIVATE /W4 /permissive- /utf-8) endif() add_test(NAME arena_blocks COMMAND arena_blocks_test) + # Recognising the title's hang watchdog ending a run (src/title_watchdog.h): CPU only, no game file. + add_executable(title_watchdog_test test/title_watchdog_test.cpp) + if(WIN32 AND MSVC) + target_compile_options(title_watchdog_test PRIVATE /W4 /permissive- /utf-8) + endif() + add_test(NAME title_watchdog COMMAND title_watchdog_test) add_executable(title_window_plan_bounds_test test/title_window_plan_bounds_test.cpp) if(WIN32 AND MSVC) target_compile_options(title_window_plan_bounds_test PRIVATE /W4 /permissive- /utf-8) diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 4d483ed59..c3b1ef7e6 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -36,33 +36,37 @@ #include #include +#include #include +#include "title_watchdog.h" + namespace { -// The retail executable (1.0.2.1) runs a hang watchdog thread (function at RVA 0x2215a60): when the game's main loop -// has not set its "kick" byte (RVA 0x71b6331) for 30 one-second ticks, it calls a crash callback and then writes to -// address 0 (`movl $0, 0` at RVA 0x2215c35). Under the runtime the game thread can legitimately stall for longer than -// that, e.g. while the driver compiles pipelines against a cold cache (first boot after a driver update), and the -// title then kills itself. By default the watchdog stays armed, as on the console. With LEONIDA_TITLE_WATCHDOG=0, and -// only when that exact trap instruction sits where it does in this build, a runtime thread sets the kick byte twice a -// second so the title's watchdog never fires. -constexpr std::uint32_t kWatchdogTrapRva = 0x2215c35; -constexpr std::uint32_t kWatchdogKickRva = 0x71b6331; -constexpr unsigned char kWatchdogTrap[] = {0xC7, 0x04, 0x25, 0, 0, 0, 0, 0, 0, 0, 0}; // movl $0, 0x0 +// The title's hang watchdog (title_watchdog.h has what it is and where). This is the title's image base once the trap +// instruction has been found where this build has it, and null for any other build: nothing below touches the +// watchdog's addresses then. +std::uint8_t* volatile title_watchdog_image = nullptr; + +void find_title_watchdog(std::uint8_t* base, std::size_t image_bytes) { + namespace wd = leonida::title_watchdog; + MEMORY_BASIC_INFORMATION trap{}; + if (VirtualQuery(base + wd::kTrapRva, &trap, sizeof(trap)) && trap.State == MEM_COMMIT && + wd::trap_present(base, image_bytes)) + title_watchdog_image = base; +} void keep_title_watchdog_kicked(std::uint8_t* base) { + namespace wd = leonida::title_watchdog; char value[4]{}; if (GetEnvironmentVariableA("LEONIDA_TITLE_WATCHDOG", value, sizeof(value)) != 1 || value[0] != '0') return; - MEMORY_BASIC_INFORMATION trap{}, kick{}; - if (!VirtualQuery(base + kWatchdogTrapRva, &trap, sizeof(trap)) || trap.State != MEM_COMMIT || - std::memcmp(base + kWatchdogTrapRva, kWatchdogTrap, sizeof(kWatchdogTrap)) != 0) - return; // not the build this was measured on: leave the title's watchdog alone + if (title_watchdog_image != base) return; // not the build this was measured on: leave the title's watchdog alone + MEMORY_BASIC_INFORMATION kick{}; const DWORD writable = PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY; - if (!VirtualQuery(base + kWatchdogKickRva, &kick, sizeof(kick)) || kick.State != MEM_COMMIT || + if (!VirtualQuery(base + wd::kKickRva, &kick, sizeof(kick)) || kick.State != MEM_COMMIT || !(kick.Protect & writable)) return; - volatile std::uint8_t* const byte = base + kWatchdogKickRva; + volatile std::uint8_t* const byte = base + wd::kKickRva; const HANDLE thread = CreateThread( nullptr, 0, [](LPVOID p) -> DWORD { @@ -327,8 +331,40 @@ static LPTOP_LEVEL_EXCEPTION_FILTER WINAPI title_set_unhandled_exception_filter( InterlockedExchangePointer(&title_fatal_filter, reinterpret_cast(filter))); } +// RT-WATCHDOG-FIRED-NOTE: the title's hang watchdog ends a stalled run with a write to address 0, and the log then +// showed only an access violation in title code, which reads as a fault of the layer or of whatever came just before +// it. When the fault is that write (title_watchdog.h, fired()), say what it was, once. Nothing else changes: the +// exception goes on as before. +static void report_title_watchdog(const EXCEPTION_POINTERS* info) { + namespace wd = leonida::title_watchdog; + std::uint8_t* const image = title_watchdog_image; + const EXCEPTION_RECORD* const record = info->ExceptionRecord; + if (!image || !info->ContextRecord || record->NumberParameters < 2) return; + wd::Fault fault; + fault.image_base = reinterpret_cast(image); + fault.instruction = reinterpret_cast(record->ExceptionAddress); + fault.write = record->ExceptionInformation[0] == 1; + fault.address = record->ExceptionInformation[1]; + // The stack from the stack pointer up, as far as its committed region goes. + const auto sp = static_cast(info->ContextRecord->Rsp); + MEMORY_BASIC_INFORMATION region{}; + if (sp % sizeof(std::uint64_t) == 0 && VirtualQuery(reinterpret_cast(sp), ®ion, sizeof(region)) && + region.State == MEM_COMMIT && !(region.Protect & (PAGE_NOACCESS | PAGE_GUARD))) { + const auto end = reinterpret_cast(region.BaseAddress) + region.RegionSize; + fault.stack = reinterpret_cast(sp); + fault.stack_words = std::min(wd::kStackWords, (end - sp) / sizeof(std::uint64_t)); + } + if (!wd::fired(fault)) return; + static std::once_flag once; + std::call_once(once, [] { + std::fprintf(stderr, "[rt] %s\n", wd::kFiredNote); + std::fflush(stderr); + }); +} + static LONG WINAPI save_caches_on_fatal_exception(EXCEPTION_POINTERS* info) { const DWORD code = info && info->ExceptionRecord ? info->ExceptionRecord->ExceptionCode : 0; + if (code == EXCEPTION_ACCESS_VIOLATION) report_title_watchdog(info); 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. @@ -635,6 +671,7 @@ int wmain(int argc, wchar_t** argv) { } std::printf("running %s\n", main_module.name.c_str()); std::fflush(stdout); + find_title_watchdog(main_module.base, main_module.size); keep_title_watchdog_kicked(main_module.base); start_title_peek(main_module.base); leonida::dev_overlay::start(); // LEONIDA_DEV_OVERLAY=1 diff --git a/runtime/src/title_watchdog.h b/runtime/src/title_watchdog.h new file mode 100644 index 000000000..2fcaf4307 --- /dev/null +++ b/runtime/src/title_watchdog.h @@ -0,0 +1,64 @@ +// The retail title's hang watchdog, as far as the runtime needs to know it (main.cpp). +// +// The retail executable (1.0.2.1) runs a hang watchdog thread (function at RVA 0x2215a60): when the game's main loop +// has not set its "kick" byte (RVA 0x71b6331) for 30 one-second ticks, it calls a crash callback and then writes to +// address 0 (`movl $0, 0` at RVA 0x2215c35). Under the runtime the game thread can legitimately stall for longer than +// that, e.g. while the driver compiles pipelines against a cold cache (first boot after a driver update), and the +// title then kills itself. By default the watchdog stays armed, as on the console. With LEONIDA_TITLE_WATCHDOG=0, and +// only when that exact trap instruction sits where it does in this build, a runtime thread sets the kick byte twice a +// second so the title's watchdog never fires. +// +// When it does fire, the log used to show only an access violation writing address 0 in title code, which reads as a +// fault of the layer or of whatever happened just before. fired() recognises the case so the runtime can say what it +// was. Nothing here needs Windows: the caller supplies the image bytes, the fault and the stack words. +#pragma once +#include +#include +#include + +namespace leonida::title_watchdog { + +inline constexpr std::uint32_t kTrapRva = 0x2215c35; +inline constexpr std::uint32_t kKickRva = 0x71b6331; +inline constexpr unsigned char kTrap[] = {0xC7, 0x04, 0x25, 0, 0, 0, 0, 0, 0, 0, 0}; // movl $0, 0x0 + +// Whether `image` (its first `image_bytes` bytes readable) has the trap instruction where this build has it. Every +// other use of the two RVAs above is gated on this: another build may have anything there. +inline bool trap_present(const std::uint8_t* image, std::size_t image_bytes) { + return image && image_bytes >= kTrapRva + sizeof(kTrap) && + std::memcmp(image + kTrapRva, kTrap, sizeof(kTrap)) == 0; +} + +// An access violation, as the unhandled-exception filter sees it. +struct Fault { + std::uint64_t image_base = 0; // where the title's image is mapped + std::uint64_t instruction = 0; // the faulting instruction's address + bool write = false; // the access was a write + std::uint64_t address = 0; // the address it touched + const std::uint64_t* stack = nullptr; // the faulting thread's stack, from its stack pointer up + std::size_t stack_words = 0; +}; + +// How much of the stack the filter hands over: the crash callback's own frames sit between the fault and the +// watchdog's return address (measured: the return address is the second frame). +inline constexpr std::size_t kStackWords = 128; + +// True when the fault is the watchdog ending the run: a write to address 0, either by the trap instruction itself or +// below the watchdog's call of its crash callback, whose return address is the trap's address (the call is the +// instruction before it). The caller has checked trap_present(). +inline bool fired(const Fault& fault) { + if (!fault.write || fault.address != 0) return false; + const std::uint64_t trap = fault.image_base + kTrapRva; + if (fault.instruction == trap) return true; + for (std::size_t i = 0; fault.stack && i < fault.stack_words; ++i) + if (fault.stack[i] == trap) return true; + return false; +} + +inline constexpr const char* kFiredNote = + "the title's own hang watchdog ended this run: its main loop did not report progress for 30 one-second ticks, so " + "the title called its crash callback and wrote to address 0 on purpose. The access violation that follows is that " + "write, not a fault of its own; what to look at is what kept the main loop busy (pipeline builds, a long wait). " + "LEONIDA_TITLE_WATCHDOG=0 keeps the watchdog kicked"; + +} // namespace leonida::title_watchdog diff --git a/runtime/test/title_watchdog_test.cpp b/runtime/test/title_watchdog_test.cpp new file mode 100644 index 000000000..027f711d3 --- /dev/null +++ b/runtime/test/title_watchdog_test.cpp @@ -0,0 +1,92 @@ +// title_watchdog.h (RT-WATCHDOG-FIRED-NOTE): recognising the title's hang watchdog ending a run. CPU only, no game +// file: the "image" is a zeroed buffer with the trap bytes written where the header says they are. +#include "../src/title_watchdog.h" + +#include +#include +#include + +namespace wd = leonida::title_watchdog; + +static int failures = 0; +static void check(bool condition, const char* what) { + std::printf("%s %s\n", condition ? "PASS" : "FAIL", what); + if (!condition) ++failures; +} + +int main() { + // trap_present: the bytes, at the place, inside the image. + std::vector image(wd::kTrapRva + sizeof(wd::kTrap) + 16, 0xCC); + check(!wd::trap_present(image.data(), image.size()), "trap_present: other bytes at the RVA are not the trap"); + std::memcpy(image.data() + wd::kTrapRva, wd::kTrap, sizeof(wd::kTrap)); + check(wd::trap_present(image.data(), image.size()), "trap_present: the trap bytes at the RVA"); + check(!wd::trap_present(image.data(), wd::kTrapRva + sizeof(wd::kTrap) - 1), + "trap_present: an image that ends inside the trap does not have it"); + check(!wd::trap_present(image.data(), 16), "trap_present: an image smaller than the RVA does not have it"); + check(!wd::trap_present(nullptr, image.size()), "trap_present: no image"); + image[wd::kTrapRva + 3] = 1; + check(!wd::trap_present(image.data(), image.size()), "trap_present: one byte off is another instruction"); + + const std::uint64_t base = 0x140000000ull; + const std::uint64_t trap = base + wd::kTrapRva; + + // The trap instruction itself faults: the crash callback returned. + wd::Fault at_trap; + at_trap.image_base = base; + at_trap.instruction = trap; + at_trap.write = true; + at_trap.address = 0; + check(wd::fired(at_trap), "fired: a write to address 0 by the trap instruction, with no stack given"); + + // The usual case: the fault is inside the crash callback, and the watchdog's return address (the trap's own + // address) is a few words up the stack. + std::vector stack(wd::kStackWords, 0x1111); + stack[17] = trap; + wd::Fault in_callback; + in_callback.image_base = base; + in_callback.instruction = base + 0x1000; + in_callback.write = true; + in_callback.address = 0; + in_callback.stack = stack.data(); + in_callback.stack_words = stack.size(); + check(wd::fired(in_callback), "fired: a write to address 0 below the watchdog's call (return address on the stack)"); + + wd::Fault f = in_callback; + f.stack_words = 17; + check(!wd::fired(f), "fired: the return address past the words handed over is not seen"); + f.stack_words = 18; + check(wd::fired(f), "fired: the return address as the last word handed over is seen"); + + f = in_callback; + f.write = false; + check(!wd::fired(f), "fired: a read of address 0 is not the watchdog"); + f = in_callback; + f.address = 8; + check(!wd::fired(f), "fired: a write near address 0 is not the watchdog (the trap writes address 0 exactly)"); + f = at_trap; + f.address = 0x1000; + check(!wd::fired(f), "fired: the trap address with another target is not the watchdog"); + + // A null write somewhere else in the title, with nothing of the watchdog on the stack. + stack[17] = 0x1111; + check(!wd::fired(in_callback), "fired: a write to address 0 without the watchdog on the stack is an ordinary fault"); + stack[5] = trap + 1; + check(!wd::fired(in_callback), "fired: an address next to the trap is not its return address"); + + // The image mapped elsewhere: the return address moves with it. + wd::Fault moved = in_callback; + moved.image_base = 0x7ff600000000ull; + stack[40] = moved.image_base + wd::kTrapRva; + check(wd::fired(moved), "fired: follows the image base"); + check(!wd::fired(in_callback), "fired: and the old base no longer matches"); + + wd::Fault no_stack = in_callback; + no_stack.stack = nullptr; + check(!wd::fired(no_stack), "fired: a null stack with a word count reads nothing"); + + check(std::strstr(wd::kFiredNote, "hang watchdog") && std::strstr(wd::kFiredNote, "LEONIDA_TITLE_WATCHDOG=0"), + "note: names the watchdog and the variable"); + + std::printf("%d failures\n", failures); + return failures ? 1 : 0; +}