diff options
Diffstat (limited to 'runtime/src/main.cpp')
| -rw-r--r-- | runtime/src/main.cpp | 143 |
1 files changed, 141 insertions, 2 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 8ea202fa0..b57bbca29 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" |
| @@ -71,7 +169,7 @@ bool configure_first_run_ui(const std::filesystem::path& package, std::filesyste | |||
| 71 | {L"LEONIDA_D3D12", L"vulkan"}, {L"LEONIDA_ARENA_MB", L"2048"}, | 169 | {L"LEONIDA_D3D12", L"vulkan"}, {L"LEONIDA_ARENA_MB", L"2048"}, |
| 72 | {L"LEONIDA_GRAPHICS_STAGE_LAYOUT", L"1"}, {L"LEONIDA_PIPELINE_COMPILE", L"on-demand"}, | 170 | {L"LEONIDA_GRAPHICS_STAGE_LAYOUT", L"1"}, {L"LEONIDA_PIPELINE_COMPILE", L"on-demand"}, |
| 73 | {L"LEONIDA_GPU_LABELS", L"1"}, {L"LEONIDA_FRAME_PACING", L"1"}, | 171 | {L"LEONIDA_GPU_LABELS", L"1"}, {L"LEONIDA_FRAME_PACING", L"1"}, |
| 74 | {L"LEONIDA_LOCAL_SPEECH", L"1"}, {L"LEONIDA_KEYBOARD_PAD", L"always"}, | 172 | {L"LEONIDA_KEYBOARD_PAD", L"always"}, |
| 75 | }; | 173 | }; |
| 76 | for (const auto& setting : settings) { | 174 | for (const auto& setting : settings) { |
| 77 | if (!set_default(setting.name, setting.value)) return false; | 175 | if (!set_default(setting.name, setting.value)) return false; |
| @@ -102,6 +200,17 @@ int usage() { | |||
| 102 | 200 | ||
| 103 | } // namespace | 201 | } // namespace |
| 104 | 202 | ||
| 203 | #ifdef LEONIDA_VULKAN_DEVICE | ||
| 204 | static BOOL WINAPI show_package_window(HWND window, int command) { | ||
| 205 | const BOOL previous = ShowWindow(window, command); | ||
| 206 | const DWORD error = GetLastError(); | ||
| 207 | if (command != SW_HIDE && rt::package_splash::paint_window(window)) | ||
| 208 | rt::startup_trace::event("package_splash_window"); | ||
| 209 | SetLastError(error); | ||
| 210 | return previous; | ||
| 211 | } | ||
| 212 | #endif | ||
| 213 | |||
| 105 | int wmain(int argc, wchar_t** argv) { | 214 | int wmain(int argc, wchar_t** argv) { |
| 106 | std::filesystem::path image, game_dir, overlay_dir, mods_dir; | 215 | std::filesystem::path image, game_dir, overlay_dir, mods_dir; |
| 107 | uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash | 216 | uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash |
| @@ -167,6 +276,15 @@ int wmain(int argc, wchar_t** argv) { | |||
| 167 | if (!trace_file.empty()) trace_file = absolute_or_same(trace_file).wstring(); | 276 | if (!trace_file.empty()) trace_file = absolute_or_same(trace_file).wstring(); |
| 168 | } | 277 | } |
| 169 | game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir); | 278 | game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir); |
| 279 | // An unattended run can select a fresh write root even when its launcher supplies --overlay. | ||
| 280 | if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_WRITE_DIR", nullptr, 0)) { | ||
| 281 | std::wstring dir(size, 0); | ||
| 282 | const DWORD n = GetEnvironmentVariableW(L"LEONIDA_WRITE_DIR", dir.data(), size); | ||
| 283 | if (n && n < size) { | ||
| 284 | dir.resize(n); | ||
| 285 | overlay_dir = dir; | ||
| 286 | } | ||
| 287 | } | ||
| 170 | if (!overlay_dir.empty()) overlay_dir = absolute_or_same(overlay_dir); | 288 | if (!overlay_dir.empty()) overlay_dir = absolute_or_same(overlay_dir); |
| 171 | // LEONIDA_SHADERS too: the D3D12 device reads it at the title's first pipeline state, after it may have moved. | 289 | // LEONIDA_SHADERS too: the D3D12 device reads it at the title's first pipeline state, after it may have moved. |
| 172 | if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_SHADERS", nullptr, 0)) { | 290 | if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_SHADERS", nullptr, 0)) { |
| @@ -188,6 +306,18 @@ int wmain(int argc, wchar_t** argv) { | |||
| 188 | } | 306 | } |
| 189 | if (overlay_dir.empty()) overlay_dir = game_dir / "overlay"; | 307 | if (overlay_dir.empty()) overlay_dir = game_dir / "overlay"; |
| 190 | rt::fs::configure(game_dir, overlay_dir); | 308 | rt::fs::configure(game_dir, overlay_dir); |
| 309 | #ifdef LEONIDA_VULKAN_DEVICE | ||
| 310 | if (run) { | ||
| 311 | rt::package_splash::configure(game_dir); | ||
| 312 | if (rt::package_splash::configured()) { | ||
| 313 | const rt::shims::Entry window_imports[] = { | ||
| 314 | {"ext-ms-win-rtcore-ntuser-window-l1-1-0.dll", "ShowWindow", reinterpret_cast<void*>(&show_package_window)}, | ||
| 315 | {"user32.dll", "ShowWindow", reinterpret_cast<void*>(&show_package_window)}, | ||
| 316 | }; | ||
| 317 | rt::shims::Table{window_imports, std::size(window_imports)}; | ||
| 318 | } | ||
| 319 | } | ||
| 320 | #endif | ||
| 191 | // Mods (docs/modding.md): <folder of gta6rt.exe>\mods unless --mods names another. Loaded and reported here; | 321 | // Mods (docs/modding.md): <folder of gta6rt.exe>\mods unless --mods names another. Loaded and reported here; |
| 192 | // the archetype swaps take effect once the game's archetype lookup is hooked for this executable version. | 322 | // the archetype swaps take effect once the game's archetype lookup is hooked for this executable version. |
| 193 | if (mods_dir.empty()) { | 323 | if (mods_dir.empty()) { |
| @@ -219,6 +349,9 @@ int wmain(int argc, wchar_t** argv) { | |||
| 219 | rt::d3d12_trace::set_enabled(trace_d3d12); | 349 | rt::d3d12_trace::set_enabled(trace_d3d12); |
| 220 | // Slot 133 imports the title's pages; xmem commits them on first touch, so they are committed first. | 350 | // Slot 133 imports the title's pages; xmem commits them on first touch, so they are committed first. |
| 221 | rt::d3d12_xs::set_placed_at_va_pages_hook(&rt::xmem::commit_physical_range); | 351 | rt::d3d12_xs::set_placed_at_va_pages_hook(&rt::xmem::commit_physical_range); |
| 352 | // So do the file shims' reads, whose kernel writes do not fault them in: on ERROR_NOACCESS (title_page_watch.h). | ||
| 353 | leonida::geometry_writes::aliases.store(&rt::xmem::geometry_aliases); | ||
| 354 | leonida::kernel_writes::set_committer(&rt::xmem::commit_physical_range); | ||
| 222 | rt::d3d12_xs::set_texture_page_provider(&rt::xmem::pin_mapped_data); | 355 | rt::d3d12_xs::set_texture_page_provider(&rt::xmem::pin_mapped_data); |
| 223 | if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) { | 356 | if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) { |
| 224 | std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n" | 357 | std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n" |
| @@ -338,7 +471,13 @@ int wmain(int argc, wchar_t** argv) { | |||
| 338 | } | 471 | } |
| 339 | std::printf("running %s\n", main_module.name.c_str()); | 472 | std::printf("running %s\n", main_module.name.c_str()); |
| 340 | std::fflush(stdout); | 473 | std::fflush(stdout); |
| 341 | const int code = rt::process::run_entry(main_module.base + main_module.entry_rva); | 474 | keep_title_watchdog_kicked(main_module.base); |
| 475 | start_title_peek(main_module.base); | ||
| 476 | int code; | ||
| 477 | { | ||
| 478 | auto stacks = rt::start_thread_stacks(); | ||
| 479 | code = rt::process::run_entry(main_module.base + main_module.entry_rva); | ||
| 480 | } | ||
| 342 | std::fflush(stdout); | 481 | std::fflush(stdout); |
| 343 | std::printf("game exited with code %d\n", code); | 482 | std::printf("game exited with code %d\n", code); |
| 344 | rt::shims::report_called_unimplemented(); | 483 | rt::shims::report_called_unimplemented(); |