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:
authoraslan2142 <jan.trencansky@protonmail.com>2026-11-25 02:37:19 +0100
committerGitHub <noreply@github.com>2026-11-25 02:37:19 +0100
commit18a22c1aefa227fd2eda047bab3385147d569f9d (patch)
tree74ad707a670f980da61b5b9e0bc9241dfd6ef181 /runtime/src/main.cpp
parent5a5df8e0b6bff458814c934e18f4216e69c40d36 (diff)
parent71a783e56fb6eb591849b5c9fb6618ed3af3f595 (diff)
Merge pull request #984 from Moriz82/integration/proton-story-load
[integration] Linux/Proton and native Windows: boot through the story load (RT-PROTON-STORY-LOAD)
Diffstat (limited to 'runtime/src/main.cpp')
-rw-r--r--runtime/src/main.cpp166
1 files changed, 165 insertions, 1 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index 098a8ee40..6357dd0b0 100644
--- a/runtime/src/main.cpp
+++ b/runtime/src/main.cpp
@@ -18,6 +18,7 @@
18// (--overlay, default <game-dir>/overlay) so the package is never modified (shims/file_system.cpp). 18// (--overlay, default <game-dir>/overlay) so the package is never modified (shims/file_system.cpp).
19#include <windows.h> 19#include <windows.h>
20 20
21#include <cstdint>
21#include <cstdio> 22#include <cstdio>
22#include <cstdlib> 23#include <cstdlib>
23#include <exception> 24#include <exception>
@@ -27,15 +28,113 @@
27#include <system_error> 28#include <system_error>
28#include <vector> 29#include <vector>
29 30
31#include <cstring>
32
33namespace {
34
35// The retail executable (1.0.2.1) runs a hang watchdog thread (function at RVA 0x2215a60): when the game's main loop
36// has not set its "kick" byte (RVA 0x71b6331) for 30 one-second ticks, it calls a crash callback and then writes to
37// address 0 (`movl $0, 0` at RVA 0x2215c35). Under the runtime the game thread can legitimately stall for longer than
38// that, e.g. while the driver compiles pipelines against a cold cache (first boot after a driver update), and the
39// title then kills itself. By default the watchdog stays armed, as on the console. With LEONIDA_TITLE_WATCHDOG=0, and
40// only when that exact trap instruction sits where it does in this build, a runtime thread sets the kick byte twice a
41// second so the title's watchdog never fires.
42constexpr std::uint32_t kWatchdogTrapRva = 0x2215c35;
43constexpr std::uint32_t kWatchdogKickRva = 0x71b6331;
44constexpr unsigned char kWatchdogTrap[] = {0xC7, 0x04, 0x25, 0, 0, 0, 0, 0, 0, 0, 0}; // movl $0, 0x0
45
46void keep_title_watchdog_kicked(std::uint8_t* base) {
47 char value[4]{};
48 if (GetEnvironmentVariableA("LEONIDA_TITLE_WATCHDOG", value, sizeof(value)) != 1 || value[0] != '0') return;
49 MEMORY_BASIC_INFORMATION trap{}, kick{};
50 if (!VirtualQuery(base + kWatchdogTrapRva, &trap, sizeof(trap)) || trap.State != MEM_COMMIT ||
51 std::memcmp(base + kWatchdogTrapRva, kWatchdogTrap, sizeof(kWatchdogTrap)) != 0)
52 return; // not the build this was measured on: leave the title's watchdog alone
53 const DWORD writable = PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY;
54 if (!VirtualQuery(base + kWatchdogKickRva, &kick, sizeof(kick)) || kick.State != MEM_COMMIT ||
55 !(kick.Protect & writable))
56 return;
57 volatile std::uint8_t* const byte = base + kWatchdogKickRva;
58 const HANDLE thread = CreateThread(
59 nullptr, 0,
60 [](LPVOID p) -> DWORD {
61 auto* b = static_cast<volatile std::uint8_t*>(p);
62 for (;;) {
63 *b = 1;
64 Sleep(500);
65 }
66 },
67 const_cast<std::uint8_t*>(byte), 0, nullptr);
68 if (thread) {
69 CloseHandle(thread);
70 std::fprintf(stderr, "[rt] loader: keeping the title's hang watchdog kicked (LEONIDA_TITLE_WATCHDOG=0; unset or 1 keeps it armed)\n");
71 }
72}
73
74// LEONIDA_PEEK="0x9e1dac0:3,0x7446510,0x98f828c" (diagnostic): a thread prints the 8-byte values at these RVAs of the
75// title's image (":n" = n consecutive values) every LEONIDA_PEEK_SECONDS (default 10). For answering "what does this
76// global hold at run time" without a debugger: the reverse-engineering notes (docs/re/requests.md) ask for exactly
77// that. Reads are checked; nothing is written.
78void start_title_peek(std::uint8_t* base) {
79 static char spec[1024];
80 const DWORD n = GetEnvironmentVariableA("LEONIDA_PEEK", spec, sizeof(spec));
81 if (!n || n >= sizeof(spec)) return;
82 static std::uint8_t* image = base;
83 const HANDLE thread = CreateThread(
84 nullptr, 0,
85 [](LPVOID) -> DWORD {
86 char seconds[16]{};
87 DWORD period = 10;
88 if (GetEnvironmentVariableA("LEONIDA_PEEK_SECONDS", seconds, sizeof(seconds)) > 0)
89 period = std::max<DWORD>(1, static_cast<DWORD>(std::strtoul(seconds, nullptr, 10)));
90 for (unsigned tick = 0;; ++tick) {
91 std::string line = "[rt] peek t=" + std::to_string(tick * period) + "s:";
92 for (const char* at = spec; *at;) {
93 char* end = nullptr;
94 const std::uint64_t rva = std::strtoull(at, &end, 16);
95 if (end == at) break;
96 unsigned count = 1;
97 if (*end == ':') count = static_cast<unsigned>(std::strtoul(end + 1, &end, 10));
98 char text[40];
99 std::snprintf(text, sizeof(text), " 0x%llx=", static_cast<unsigned long long>(rva));
100 line += text;
101 for (unsigned i = 0; i < count && i < 16; ++i) {
102 std::uint64_t value = 0;
103 SIZE_T got = 0;
104 const bool ok = ReadProcessMemory(GetCurrentProcess(), image + rva + 8ull * i, &value,
105 sizeof(value), &got) && got == sizeof(value);
106 std::snprintf(text, sizeof(text), ok ? "%s%llx" : "%s?", i ? "," : "",
107 static_cast<unsigned long long>(value));
108 line += text;
109 }
110 at = *end == ',' ? end + 1 : end;
111 if (at == end && *end) break;
112 }
113 std::fprintf(stderr, "%s\n", line.c_str());
114 Sleep(period * 1000);
115 }
116 },
117 nullptr, 0, nullptr);
118 if (thread) CloseHandle(thread);
119}
120
121} // namespace
122
30#include "base_reservation.h" 123#include "base_reservation.h"
31#include "code_hooks.h" 124#include "code_hooks.h"
32#include "cpu_emulation.h" 125#include "cpu_emulation.h"
33#include "host_cpu.h" 126#include "host_cpu.h"
34#include "mods.h" 127#include "mods.h"
35#include "pe_loader.h" 128#include "pe_loader.h"
129#include "thread_stacks.h"
130#include "title_page_watch.h"
131#include "title_geometry_watch.h"
36#include "../shims/d3d12_trace.h" 132#include "../shims/d3d12_trace.h"
37#include "../shims/d3d12_xs_device.h" 133#include "../shims/d3d12_xs_device.h"
38#include "../shims/file_system.h" 134#include "../shims/file_system.h"
135#ifdef LEONIDA_VULKAN_DEVICE
136#include "../shims/package_splash.h"
137#endif
39#include "../shims/process_shim.h" 138#include "../shims/process_shim.h"
40#include "../shims/shims.h" 139#include "../shims/shims.h"
41#include "../shims/startup_trace.h" 140#include "../shims/startup_trace.h"
@@ -52,6 +151,16 @@ const char* binding_name(rt::Binding b) {
52 } 151 }
53} 152}
54 153
154// Module TLS index 0 for the game's executable (rt::reserve_tls_index0), from the CRT's initializer table: .CRT$XCT
155// runs after the C runtime is set up and before every C++ dynamic initializer of gta6rt (.CRT$XCU), so before any of
156// gta6rt's code can have started a thread. The frames above it (wmainCRTStartup, __scrt_common_main_seh, _initterm)
157// hold no TLS slot address. The result is read again where an image is run.
158void __cdecl reserve_tls_index0_at_start() { rt::reserve_tls_index0(); }
159#pragma section(".CRT$XCT", read)
160extern "C" __declspec(allocate(".CRT$XCT")) void(__cdecl* const gta6rt_reserve_tls_index0)() =
161 reserve_tls_index0_at_start;
162#pragma comment(linker, "/INCLUDE:gta6rt_reserve_tls_index0")
163
55int usage() { 164int usage() {
56 std::fprintf(stderr, 165 std::fprintf(stderr,
57 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace FILE]\n" 166 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace FILE]\n"
@@ -62,6 +171,17 @@ int usage() {
62 171
63} // namespace 172} // namespace
64 173
174#ifdef LEONIDA_VULKAN_DEVICE
175static BOOL WINAPI show_package_window(HWND window, int command) {
176 const BOOL previous = ShowWindow(window, command);
177 const DWORD error = GetLastError();
178 if (command != SW_HIDE && rt::package_splash::paint_window(window))
179 rt::startup_trace::event("package_splash_window");
180 SetLastError(error);
181 return previous;
182}
183#endif
184
65int wmain(int argc, wchar_t** argv) { 185int wmain(int argc, wchar_t** argv) {
66 std::filesystem::path image, game_dir, overlay_dir, mods_dir; 186 std::filesystem::path image, game_dir, overlay_dir, mods_dir;
67 uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash 187 uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash
@@ -120,6 +240,15 @@ int wmain(int argc, wchar_t** argv) {
120 return ec ? p : a; 240 return ec ? p : a;
121 }; 241 };
122 game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir); 242 game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir);
243 // An unattended run can select a fresh write root even when its launcher supplies --overlay.
244 if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_WRITE_DIR", nullptr, 0)) {
245 std::wstring dir(size, 0);
246 const DWORD n = GetEnvironmentVariableW(L"LEONIDA_WRITE_DIR", dir.data(), size);
247 if (n && n < size) {
248 dir.resize(n);
249 overlay_dir = dir;
250 }
251 }
123 if (!overlay_dir.empty()) overlay_dir = absolute_or_same(overlay_dir); 252 if (!overlay_dir.empty()) overlay_dir = absolute_or_same(overlay_dir);
124 // LEONIDA_SHADERS too: the D3D12 device reads it at the title's first pipeline state, after it may have moved. 253 // LEONIDA_SHADERS too: the D3D12 device reads it at the title's first pipeline state, after it may have moved.
125 if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_SHADERS", nullptr, 0)) { 254 if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_SHADERS", nullptr, 0)) {
@@ -137,6 +266,18 @@ int wmain(int argc, wchar_t** argv) {
137 // GAME: paths read under --game-dir; whatever a title writes lands in the overlay, never in the package. 266 // GAME: paths read under --game-dir; whatever a title writes lands in the overlay, never in the package.
138 if (overlay_dir.empty()) overlay_dir = game_dir / "overlay"; 267 if (overlay_dir.empty()) overlay_dir = game_dir / "overlay";
139 rt::fs::configure(game_dir, overlay_dir); 268 rt::fs::configure(game_dir, overlay_dir);
269#ifdef LEONIDA_VULKAN_DEVICE
270 if (run) {
271 rt::package_splash::configure(game_dir);
272 if (rt::package_splash::configured()) {
273 const rt::shims::Entry window_imports[] = {
274 {"ext-ms-win-rtcore-ntuser-window-l1-1-0.dll", "ShowWindow", reinterpret_cast<void*>(&show_package_window)},
275 {"user32.dll", "ShowWindow", reinterpret_cast<void*>(&show_package_window)},
276 };
277 rt::shims::Table{window_imports, std::size(window_imports)};
278 }
279 }
280#endif
140 // Mods (docs/modding.md): <folder of gta6rt.exe>\mods unless --mods names another. Loaded and reported here; 281 // Mods (docs/modding.md): <folder of gta6rt.exe>\mods unless --mods names another. Loaded and reported here;
141 // the archetype swaps take effect once the game's archetype lookup is hooked for this executable version. 282 // the archetype swaps take effect once the game's archetype lookup is hooked for this executable version.
142 if (mods_dir.empty()) { 283 if (mods_dir.empty()) {
@@ -168,6 +309,9 @@ int wmain(int argc, wchar_t** argv) {
168 rt::d3d12_trace::set_enabled(trace_d3d12); 309 rt::d3d12_trace::set_enabled(trace_d3d12);
169 // Slot 133 imports the title's pages; xmem commits them on first touch, so they are committed first. 310 // Slot 133 imports the title's pages; xmem commits them on first touch, so they are committed first.
170 rt::d3d12_xs::set_placed_at_va_pages_hook(&rt::xmem::commit_physical_range); 311 rt::d3d12_xs::set_placed_at_va_pages_hook(&rt::xmem::commit_physical_range);
312 // So do the file shims' reads, whose kernel writes do not fault them in: on ERROR_NOACCESS (title_page_watch.h).
313 leonida::geometry_writes::aliases.store(&rt::xmem::geometry_aliases);
314 leonida::kernel_writes::set_committer(&rt::xmem::commit_physical_range);
171 if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) { 315 if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) {
172 std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n" 316 std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n"
173 : "error: cannot create the trace file\n"); 317 : "error: cannot create the trace file\n");
@@ -186,6 +330,20 @@ int wmain(int argc, wchar_t** argv) {
186 try { 330 try {
187 rt::Loader loader(game_dir); 331 rt::Loader loader(game_dir);
188 rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code 332 rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code
333 // An executable with TLS that did not get index 0: its /GA code would read and write the TLS in slot 0, which
334 // is gta6rt's own (or another executable's), so no code of it may run.
335 const auto* main_nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(
336 main_module.base + reinterpret_cast<const IMAGE_DOS_HEADER*>(main_module.base)->e_lfanew);
337 const bool main_is_exe = (main_nt->FileHeader.Characteristics & IMAGE_FILE_DLL) == 0;
338 if ((init || run) && main_is_exe && main_module.tls_slot != rt::kNoTlsIndex && main_module.tls_index != 0) {
339 std::string why;
340 if (rt::reserve_tls_index0(&why)) why = "another mapped executable holds it";
341 std::fprintf(stderr,
342 "error: %s cannot have module TLS index 0 (%s); code built with /GA would read and write "
343 "another module's TLS through slot 0, so it is not run\n",
344 main_module.name.c_str(), why.c_str());
345 return 1;
346 }
189 // The mods' archetype swaps (docs/modding.md): hooked here, before any image code runs, when the lookup's RVA 347 // The mods' archetype swaps (docs/modding.md): hooked here, before any image code runs, when the lookup's RVA
190 // for this executable is given. The count of redirected calls shows whether the RVA is the real lookup. 348 // for this executable is given. The count of redirected calls shows whether the RVA is the real lookup.
191 if (view_mode_getter_rva) { 349 if (view_mode_getter_rva) {
@@ -272,7 +430,13 @@ int wmain(int argc, wchar_t** argv) {
272 } 430 }
273 std::printf("running %s\n", main_module.name.c_str()); 431 std::printf("running %s\n", main_module.name.c_str());
274 std::fflush(stdout); 432 std::fflush(stdout);
275 const int code = rt::process::run_entry(main_module.base + main_module.entry_rva); 433 keep_title_watchdog_kicked(main_module.base);
434 start_title_peek(main_module.base);
435 int code;
436 {
437 auto stacks = rt::start_thread_stacks();
438 code = rt::process::run_entry(main_module.base + main_module.entry_rva);
439 }
276 std::fflush(stdout); 440 std::fflush(stdout);
277 std::printf("game exited with code %d\n", code); 441 std::printf("game exited with code %d\n", code);
278 rt::shims::report_called_unimplemented(); 442 rt::shims::report_called_unimplemented();