upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src/main.cpp')
-rw-r--r--runtime/src/main.cpp49
1 files changed, 35 insertions, 14 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index 094aaa321..94e128d2a 100644
--- a/runtime/src/main.cpp
+++ b/runtime/src/main.cpp
@@ -33,24 +33,45 @@
33 33
34namespace { 34namespace {
35 35
36// The retail executable (1.0.2.1) runs a hang watchdog thread (function at RVA 0x2215a60): when the game's main loop 36// The retail title runs a hang watchdog thread: when the game's main loop has not set its "kick" byte for 30
37// has not set its "kick" byte (RVA 0x71b6331) for 30 one-second ticks, it calls a crash callback and then writes to 37// one-second ticks, it calls a crash callback and then writes to address 0 (`movl $0, 0`). Under the runtime the
38// address 0 (`movl $0, 0` at RVA 0x2215c35). Under the runtime the game thread can legitimately stall for longer than 38// game thread can legitimately stall for longer than that, e.g. while the driver compiles pipelines against a cold
39// that, e.g. while the driver compiles pipelines against a cold cache (first boot after a driver update), and the 39// cache (first boot after a driver update), and the title then kills itself. By default the watchdog stays armed, as
40// title then kills itself. By default the watchdog stays armed, as on the console. With LEONIDA_TITLE_WATCHDOG=0, and 40// on the console. The two places are measured locally and not kept in the repository:
41// only when that exact trap instruction sits where it does in this build, a runtime thread sets the kick byte twice a 41// LEONIDA_TITLE_WATCHDOG_KICK="<trap RVA>,<kick RVA>" (hex) names them, and only when that exact trap instruction
42// second so the title's watchdog never fires. 42// sits at the first one does a runtime thread set the byte at the second twice a second, so the title's watchdog
43constexpr std::uint32_t kWatchdogTrapRva = 0x2215c35; 43// never fires. Unset, or with LEONIDA_TITLE_WATCHDOG=1, the watchdog stays armed.
44constexpr std::uint32_t kWatchdogKickRva = 0x71b6331;
45constexpr unsigned char kWatchdogTrap[] = {0xC7, 0x04, 0x25, 0, 0, 0, 0, 0, 0, 0, 0}; // movl $0, 0x0 44constexpr unsigned char kWatchdogTrap[] = {0xC7, 0x04, 0x25, 0, 0, 0, 0, 0, 0, 0, 0}; // movl $0, 0x0
46 45
46bool watchdog_rvas(std::uint32_t& trap_rva, std::uint32_t& kick_rva) {
47 char value[64]{};
48 const DWORD n = GetEnvironmentVariableA("LEONIDA_TITLE_WATCHDOG_KICK", value, sizeof(value));
49 if (!n || n >= sizeof(value)) return false;
50 char* rest = nullptr;
51 const unsigned long long trap = std::strtoull(value, &rest, 16);
52 if (!rest || *rest != ',') return false;
53 const unsigned long long kick = std::strtoull(rest + 1, &rest, 16);
54 if (!rest || *rest || !trap || !kick || trap > 0xFFFFFFFFull || kick > 0xFFFFFFFFull) return false;
55 trap_rva = static_cast<std::uint32_t>(trap);
56 kick_rva = static_cast<std::uint32_t>(kick);
57 return true;
58}
59
47void keep_title_watchdog_kicked(std::uint8_t* base) { 60void keep_title_watchdog_kicked(std::uint8_t* base) {
48 char value[4]{}; 61 char keep[4]{};
49 if (GetEnvironmentVariableA("LEONIDA_TITLE_WATCHDOG", value, sizeof(value)) != 1 || value[0] != '0') return; 62 const DWORD keep_length = GetEnvironmentVariableA("LEONIDA_TITLE_WATCHDOG", keep, sizeof(keep));
63 if (keep_length == 1 && keep[0] == '1') return;
64 std::uint32_t kWatchdogTrapRva = 0, kWatchdogKickRva = 0;
65 if (!watchdog_rvas(kWatchdogTrapRva, kWatchdogKickRva)) {
66 if (keep_length == 1 && keep[0] == '0')
67 std::fprintf(stderr, "[rt] loader: LEONIDA_TITLE_WATCHDOG=0 needs LEONIDA_TITLE_WATCHDOG_KICK=<trap RVA>,"
68 "<kick RVA> (hex); the title's hang watchdog stays armed\n");
69 return;
70 }
50 MEMORY_BASIC_INFORMATION trap{}, kick{}; 71 MEMORY_BASIC_INFORMATION trap{}, kick{};
51 if (!VirtualQuery(base + kWatchdogTrapRva, &trap, sizeof(trap)) || trap.State != MEM_COMMIT || 72 if (!VirtualQuery(base + kWatchdogTrapRva, &trap, sizeof(trap)) || trap.State != MEM_COMMIT ||
52 std::memcmp(base + kWatchdogTrapRva, kWatchdogTrap, sizeof(kWatchdogTrap)) != 0) 73 std::memcmp(base + kWatchdogTrapRva, kWatchdogTrap, sizeof(kWatchdogTrap)) != 0)
53 return; // not the build this was measured on: leave the title's watchdog alone 74 return; // not the trap the RVAs name: leave the title's watchdog alone
54 const DWORD writable = PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY; 75 const DWORD writable = PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY;
55 if (!VirtualQuery(base + kWatchdogKickRva, &kick, sizeof(kick)) || kick.State != MEM_COMMIT || 76 if (!VirtualQuery(base + kWatchdogKickRva, &kick, sizeof(kick)) || kick.State != MEM_COMMIT ||
56 !(kick.Protect & writable)) 77 !(kick.Protect & writable))
@@ -68,11 +89,11 @@ void keep_title_watchdog_kicked(std::uint8_t* base) {
68 const_cast<std::uint8_t*>(byte), 0, nullptr); 89 const_cast<std::uint8_t*>(byte), 0, nullptr);
69 if (thread) { 90 if (thread) {
70 CloseHandle(thread); 91 CloseHandle(thread);
71 std::fprintf(stderr, "[rt] loader: keeping the title's hang watchdog kicked (LEONIDA_TITLE_WATCHDOG=0; unset or 1 keeps it armed)\n"); 92 std::fprintf(stderr, "[rt] loader: keeping the title's hang watchdog kicked (LEONIDA_TITLE_WATCHDOG_KICK)\n");
72 } 93 }
73} 94}
74 95
75// LEONIDA_PEEK="0x9e1dac0:3,0x7446510,0x98f828c" (diagnostic): a thread prints the 8-byte values at these RVAs of the 96// LEONIDA_PEEK="<rva>:3,<rva>,<rva>" (diagnostic): a thread prints the 8-byte values at these RVAs of the
76// title's image (":n" = n consecutive values, at most 32; "RVA>OFF" reads through the pointer stored at the RVA, at 97// title's image (":n" = n consecutive values, at most 32; "RVA>OFF" reads through the pointer stored at the RVA, at
77// pointer + OFF, both hex) every LEONIDA_PEEK_SECONDS (default 10). For answering "what does this 98// pointer + OFF, both hex) every LEONIDA_PEEK_SECONDS (default 10). For answering "what does this
78// global hold at run time" without a debugger: the reverse-engineering notes (docs/re/requests.md) ask for exactly 99// global hold at run time" without a debugger: the reverse-engineering notes (docs/re/requests.md) ask for exactly