diff options
| author | Moriz82 <72836413+Moriz82@users.noreply.github.com> | 2026-11-24 11:42:12 -0600 |
|---|---|---|
| committer | Moriz82 <72836413+Moriz82@users.noreply.github.com> | 2026-11-24 11:42:12 -0600 |
| commit | 273b3bdf9d6e736442ad584ad464df6e86ee928b (patch) | |
| tree | 3886a0af7c74ffa7d9415d662d8dc0fb708f1c08 /runtime/src/main.cpp | |
| parent | ece4ba5b31ebc6f345c8e29eae1261f2f780057a (diff) | |
[integration] Linux/Proton and native Windows: boot through the story load (RT-PROTON-STORY-LOAD)
One squashed commit of a fork that was developed on a Linux/Proton machine and
tested on two native Windows machines. On all of them the retail 1.0.2.1
package now launches, shows its launch movie, first-run pages and landing
prompt, runs the story load to its end with the loading slideshow drawing its
artwork, and enters the world phase. A first world frame is not verified yet.
docs/linux_proton.md has the setup, a healthy boot's timeline, the machines
and where it stops.
Runtime: frames in the title's own window; DirectStorage pinned reads,
page-list scatter/gather, BCPack and unshuffle with a locally generated
predictor file; pooled, device-local and deferred image backing for streamed
and placed textures; enhanced barriers into LAYOUT_UNDEFINED; a bindless table
that degrades instead of refusing a submission; a descriptor hash finalizer
(off until a library carries it); directly indexed heap offsets for compute
dispatches and VS/PS/GS draws; title buffer windows outside a fragmented
arena; opt-in device-local copies of streamed buffer views; write-fault
retries under the tracker lock, shared trackers and write-watched title memory
for native Windows; device-loss, compute-input and memory probes.
Translator: loop bounds at structured exits, the eight FidelityFX programs
that use a packed-dot form the disassembler skips, buffer bounds modes per
view kind, a compaction option, the hash finalizer option.
Tools and scripts: Linux boot scripts (Wine/Proton, no network), a Windows
boot script, the paired compute translation script.
No game data. AI-assisted (Claude Code and Codex agents), reviewed and booted
by people on the machines named in docs/linux_proton.md.
Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/main.cpp')
| -rw-r--r-- | runtime/src/main.cpp | 165 |
1 files changed, 164 insertions, 1 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 098a8ee40..b346bc68a 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,112 @@ | |||
| 27 | #include <system_error> | 28 | #include <system_error> |
| 28 | #include <vector> | 29 | #include <vector> |
| 29 | 30 | ||
| 31 | #include <cstring> | ||
| 32 | |||
| 33 | namespace { | ||
| 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. Unless LEONIDA_TITLE_WATCHDOG=1, and only when that exact trap instruction sits where it | ||
| 40 | // does in this build, a runtime thread sets the kick byte twice a second so the title's watchdog never fires. | ||
| 41 | constexpr std::uint32_t kWatchdogTrapRva = 0x2215c35; | ||
| 42 | constexpr std::uint32_t kWatchdogKickRva = 0x71b6331; | ||
| 43 | constexpr unsigned char kWatchdogTrap[] = {0xC7, 0x04, 0x25, 0, 0, 0, 0, 0, 0, 0, 0}; // movl $0, 0x0 | ||
| 44 | |||
| 45 | void keep_title_watchdog_kicked(std::uint8_t* base) { | ||
| 46 | char keep[4]{}; | ||
| 47 | if (GetEnvironmentVariableA("LEONIDA_TITLE_WATCHDOG", keep, sizeof(keep)) == 1 && keep[0] == '1') return; | ||
| 48 | MEMORY_BASIC_INFORMATION trap{}, kick{}; | ||
| 49 | if (!VirtualQuery(base + kWatchdogTrapRva, &trap, sizeof(trap)) || trap.State != MEM_COMMIT || | ||
| 50 | std::memcmp(base + kWatchdogTrapRva, kWatchdogTrap, sizeof(kWatchdogTrap)) != 0) | ||
| 51 | return; // not the build this was measured on: leave the title's watchdog alone | ||
| 52 | const DWORD writable = PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY; | ||
| 53 | if (!VirtualQuery(base + kWatchdogKickRva, &kick, sizeof(kick)) || kick.State != MEM_COMMIT || | ||
| 54 | !(kick.Protect & writable)) | ||
| 55 | return; | ||
| 56 | volatile std::uint8_t* const byte = base + kWatchdogKickRva; | ||
| 57 | const HANDLE thread = CreateThread( | ||
| 58 | nullptr, 0, | ||
| 59 | [](LPVOID p) -> DWORD { | ||
| 60 | auto* b = static_cast<volatile std::uint8_t*>(p); | ||
| 61 | for (;;) { | ||
| 62 | *b = 1; | ||
| 63 | Sleep(500); | ||
| 64 | } | ||
| 65 | }, | ||
| 66 | const_cast<std::uint8_t*>(byte), 0, nullptr); | ||
| 67 | if (thread) { | ||
| 68 | CloseHandle(thread); | ||
| 69 | std::fprintf(stderr, "[rt] loader: keeping the title's hang watchdog kicked (LEONIDA_TITLE_WATCHDOG=1 keeps it armed)\n"); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | // LEONIDA_PEEK="0x9e1dac0:3,0x7446510,0x98f828c" (diagnostic): a thread prints the 8-byte values at these RVAs of the | ||
| 74 | // title's image (":n" = n consecutive values) every LEONIDA_PEEK_SECONDS (default 10). For answering "what does this | ||
| 75 | // global hold at run time" without a debugger: the reverse-engineering notes (docs/re/requests.md) ask for exactly | ||
| 76 | // that. Reads are checked; nothing is written. | ||
| 77 | void start_title_peek(std::uint8_t* base) { | ||
| 78 | static char spec[1024]; | ||
| 79 | const DWORD n = GetEnvironmentVariableA("LEONIDA_PEEK", spec, sizeof(spec)); | ||
| 80 | if (!n || n >= sizeof(spec)) return; | ||
| 81 | static std::uint8_t* image = base; | ||
| 82 | const HANDLE thread = CreateThread( | ||
| 83 | nullptr, 0, | ||
| 84 | [](LPVOID) -> DWORD { | ||
| 85 | char seconds[16]{}; | ||
| 86 | DWORD period = 10; | ||
| 87 | if (GetEnvironmentVariableA("LEONIDA_PEEK_SECONDS", seconds, sizeof(seconds)) > 0) | ||
| 88 | period = std::max<DWORD>(1, static_cast<DWORD>(std::strtoul(seconds, nullptr, 10))); | ||
| 89 | for (unsigned tick = 0;; ++tick) { | ||
| 90 | std::string line = "[rt] peek t=" + std::to_string(tick * period) + "s:"; | ||
| 91 | for (const char* at = spec; *at;) { | ||
| 92 | char* end = nullptr; | ||
| 93 | const std::uint64_t rva = std::strtoull(at, &end, 16); | ||
| 94 | if (end == at) break; | ||
| 95 | unsigned count = 1; | ||
| 96 | if (*end == ':') count = static_cast<unsigned>(std::strtoul(end + 1, &end, 10)); | ||
| 97 | char text[40]; | ||
| 98 | std::snprintf(text, sizeof(text), " 0x%llx=", static_cast<unsigned long long>(rva)); | ||
| 99 | line += text; | ||
| 100 | for (unsigned i = 0; i < count && i < 16; ++i) { | ||
| 101 | std::uint64_t value = 0; | ||
| 102 | SIZE_T got = 0; | ||
| 103 | const bool ok = ReadProcessMemory(GetCurrentProcess(), image + rva + 8ull * i, &value, | ||
| 104 | sizeof(value), &got) && got == sizeof(value); | ||
| 105 | std::snprintf(text, sizeof(text), ok ? "%s%llx" : "%s?", i ? "," : "", | ||
| 106 | static_cast<unsigned long long>(value)); | ||
| 107 | line += text; | ||
| 108 | } | ||
| 109 | at = *end == ',' ? end + 1 : end; | ||
| 110 | if (at == end && *end) break; | ||
| 111 | } | ||
| 112 | std::fprintf(stderr, "%s\n", line.c_str()); | ||
| 113 | Sleep(period * 1000); | ||
| 114 | } | ||
| 115 | }, | ||
| 116 | nullptr, 0, nullptr); | ||
| 117 | if (thread) CloseHandle(thread); | ||
| 118 | } | ||
| 119 | |||
| 120 | } // namespace | ||
| 121 | |||
| 30 | #include "base_reservation.h" | 122 | #include "base_reservation.h" |
| 31 | #include "code_hooks.h" | 123 | #include "code_hooks.h" |
| 32 | #include "cpu_emulation.h" | 124 | #include "cpu_emulation.h" |
| 33 | #include "host_cpu.h" | 125 | #include "host_cpu.h" |
| 34 | #include "mods.h" | 126 | #include "mods.h" |
| 35 | #include "pe_loader.h" | 127 | #include "pe_loader.h" |
| 128 | #include "thread_stacks.h" | ||
| 129 | #include "title_page_watch.h" | ||
| 130 | #include "title_geometry_watch.h" | ||
| 36 | #include "../shims/d3d12_trace.h" | 131 | #include "../shims/d3d12_trace.h" |
| 37 | #include "../shims/d3d12_xs_device.h" | 132 | #include "../shims/d3d12_xs_device.h" |
| 38 | #include "../shims/file_system.h" | 133 | #include "../shims/file_system.h" |
| 134 | #ifdef LEONIDA_VULKAN_DEVICE | ||
| 135 | #include "../shims/package_splash.h" | ||
| 136 | #endif | ||
| 39 | #include "../shims/process_shim.h" | 137 | #include "../shims/process_shim.h" |
| 40 | #include "../shims/shims.h" | 138 | #include "../shims/shims.h" |
| 41 | #include "../shims/startup_trace.h" | 139 | #include "../shims/startup_trace.h" |
| @@ -52,6 +150,16 @@ const char* binding_name(rt::Binding b) { | |||
| 52 | } | 150 | } |
| 53 | } | 151 | } |
| 54 | 152 | ||
| 153 | // Module TLS index 0 for the game's executable (rt::reserve_tls_index0), from the CRT's initializer table: .CRT$XCT | ||
| 154 | // runs after the C runtime is set up and before every C++ dynamic initializer of gta6rt (.CRT$XCU), so before any of | ||
| 155 | // gta6rt's code can have started a thread. The frames above it (wmainCRTStartup, __scrt_common_main_seh, _initterm) | ||
| 156 | // hold no TLS slot address. The result is read again where an image is run. | ||
| 157 | void __cdecl reserve_tls_index0_at_start() { rt::reserve_tls_index0(); } | ||
| 158 | #pragma section(".CRT$XCT", read) | ||
| 159 | extern "C" __declspec(allocate(".CRT$XCT")) void(__cdecl* const gta6rt_reserve_tls_index0)() = | ||
| 160 | reserve_tls_index0_at_start; | ||
| 161 | #pragma comment(linker, "/INCLUDE:gta6rt_reserve_tls_index0") | ||
| 162 | |||
| 55 | int usage() { | 163 | int usage() { |
| 56 | std::fprintf(stderr, | 164 | 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" | 165 | "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 +170,17 @@ int usage() { | |||
| 62 | 170 | ||
| 63 | } // namespace | 171 | } // namespace |
| 64 | 172 | ||
| 173 | #ifdef LEONIDA_VULKAN_DEVICE | ||
| 174 | static BOOL WINAPI show_package_window(HWND window, int command) { | ||
| 175 | const BOOL previous = ShowWindow(window, command); | ||
| 176 | const DWORD error = GetLastError(); | ||
| 177 | if (command != SW_HIDE && rt::package_splash::paint_window(window)) | ||
| 178 | rt::startup_trace::event("package_splash_window"); | ||
| 179 | SetLastError(error); | ||
| 180 | return previous; | ||
| 181 | } | ||
| 182 | #endif | ||
| 183 | |||
| 65 | int wmain(int argc, wchar_t** argv) { | 184 | int wmain(int argc, wchar_t** argv) { |
| 66 | std::filesystem::path image, game_dir, overlay_dir, mods_dir; | 185 | 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 | 186 | uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash |
| @@ -120,6 +239,15 @@ int wmain(int argc, wchar_t** argv) { | |||
| 120 | return ec ? p : a; | 239 | return ec ? p : a; |
| 121 | }; | 240 | }; |
| 122 | game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir); | 241 | game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir); |
| 242 | // An unattended run can select a fresh write root even when its launcher supplies --overlay. | ||
| 243 | if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_WRITE_DIR", nullptr, 0)) { | ||
| 244 | std::wstring dir(size, 0); | ||
| 245 | const DWORD n = GetEnvironmentVariableW(L"LEONIDA_WRITE_DIR", dir.data(), size); | ||
| 246 | if (n && n < size) { | ||
| 247 | dir.resize(n); | ||
| 248 | overlay_dir = dir; | ||
| 249 | } | ||
| 250 | } | ||
| 123 | if (!overlay_dir.empty()) overlay_dir = absolute_or_same(overlay_dir); | 251 | 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. | 252 | // 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)) { | 253 | if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_SHADERS", nullptr, 0)) { |
| @@ -137,6 +265,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. | 265 | // 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"; | 266 | if (overlay_dir.empty()) overlay_dir = game_dir / "overlay"; |
| 139 | rt::fs::configure(game_dir, overlay_dir); | 267 | rt::fs::configure(game_dir, overlay_dir); |
| 268 | #ifdef LEONIDA_VULKAN_DEVICE | ||
| 269 | if (run) { | ||
| 270 | rt::package_splash::configure(game_dir); | ||
| 271 | if (rt::package_splash::configured()) { | ||
| 272 | const rt::shims::Entry window_imports[] = { | ||
| 273 | {"ext-ms-win-rtcore-ntuser-window-l1-1-0.dll", "ShowWindow", reinterpret_cast<void*>(&show_package_window)}, | ||
| 274 | {"user32.dll", "ShowWindow", reinterpret_cast<void*>(&show_package_window)}, | ||
| 275 | }; | ||
| 276 | rt::shims::Table{window_imports, std::size(window_imports)}; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | #endif | ||
| 140 | // Mods (docs/modding.md): <folder of gta6rt.exe>\mods unless --mods names another. Loaded and reported here; | 280 | // 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. | 281 | // the archetype swaps take effect once the game's archetype lookup is hooked for this executable version. |
| 142 | if (mods_dir.empty()) { | 282 | if (mods_dir.empty()) { |
| @@ -168,6 +308,9 @@ int wmain(int argc, wchar_t** argv) { | |||
| 168 | rt::d3d12_trace::set_enabled(trace_d3d12); | 308 | 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. | 309 | // 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); | 310 | rt::d3d12_xs::set_placed_at_va_pages_hook(&rt::xmem::commit_physical_range); |
| 311 | // So do the file shims' reads, whose kernel writes do not fault them in: on ERROR_NOACCESS (title_page_watch.h). | ||
| 312 | leonida::geometry_writes::aliases.store(&rt::xmem::geometry_aliases); | ||
| 313 | 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)) { | 314 | 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" | 315 | 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"); | 316 | : "error: cannot create the trace file\n"); |
| @@ -186,6 +329,20 @@ int wmain(int argc, wchar_t** argv) { | |||
| 186 | try { | 329 | try { |
| 187 | rt::Loader loader(game_dir); | 330 | rt::Loader loader(game_dir); |
| 188 | rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code | 331 | rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code |
| 332 | // An executable with TLS that did not get index 0: its /GA code would read and write the TLS in slot 0, which | ||
| 333 | // is gta6rt's own (or another executable's), so no code of it may run. | ||
| 334 | const auto* main_nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>( | ||
| 335 | main_module.base + reinterpret_cast<const IMAGE_DOS_HEADER*>(main_module.base)->e_lfanew); | ||
| 336 | const bool main_is_exe = (main_nt->FileHeader.Characteristics & IMAGE_FILE_DLL) == 0; | ||
| 337 | if ((init || run) && main_is_exe && main_module.tls_slot != rt::kNoTlsIndex && main_module.tls_index != 0) { | ||
| 338 | std::string why; | ||
| 339 | if (rt::reserve_tls_index0(&why)) why = "another mapped executable holds it"; | ||
| 340 | std::fprintf(stderr, | ||
| 341 | "error: %s cannot have module TLS index 0 (%s); code built with /GA would read and write " | ||
| 342 | "another module's TLS through slot 0, so it is not run\n", | ||
| 343 | main_module.name.c_str(), why.c_str()); | ||
| 344 | return 1; | ||
| 345 | } | ||
| 189 | // The mods' archetype swaps (docs/modding.md): hooked here, before any image code runs, when the lookup's RVA | 346 | // 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. | 347 | // for this executable is given. The count of redirected calls shows whether the RVA is the real lookup. |
| 191 | if (view_mode_getter_rva) { | 348 | if (view_mode_getter_rva) { |
| @@ -272,7 +429,13 @@ int wmain(int argc, wchar_t** argv) { | |||
| 272 | } | 429 | } |
| 273 | std::printf("running %s\n", main_module.name.c_str()); | 430 | std::printf("running %s\n", main_module.name.c_str()); |
| 274 | std::fflush(stdout); | 431 | std::fflush(stdout); |
| 275 | const int code = rt::process::run_entry(main_module.base + main_module.entry_rva); | 432 | keep_title_watchdog_kicked(main_module.base); |
| 433 | start_title_peek(main_module.base); | ||
| 434 | int code; | ||
| 435 | { | ||
| 436 | auto stacks = rt::start_thread_stacks(); | ||
| 437 | code = rt::process::run_entry(main_module.base + main_module.entry_rva); | ||
| 438 | } | ||
| 276 | std::fflush(stdout); | 439 | std::fflush(stdout); |
| 277 | std::printf("game exited with code %d\n", code); | 440 | std::printf("game exited with code %d\n", code); |
| 278 | rt::shims::report_called_unimplemented(); | 441 | rt::shims::report_called_unimplemented(); |