diff options
| author | andretuta <andreviniciusmerlia@gmail.com> | 2026-11-25 00:58:58 -0300 |
|---|---|---|
| committer | andretuta <andreviniciusmerlia@gmail.com> | 2026-11-25 00:58:58 -0300 |
| commit | f9792cc56de10067f1bbdb233eaac23cd5b2074f (patch) | |
| tree | 29b081c088ccc77ddd184c9937d445ada25e30e2 /runtime/src/main.cpp | |
| parent | b358d9062d10e63fbd90bfbb08c4bac8b9ef0b30 (diff) | |
| parent | 5129be10d47e16ecf5d8c41cfec12a59a2f25512 (diff) | |
Merge remote-tracking branch 'ubiq/main' into feat/gdk-lifecycle-window-hook
# Conflicts:
# scripts/build_runtime.ps1
Diffstat (limited to 'runtime/src/main.cpp')
| -rw-r--r-- | runtime/src/main.cpp | 215 |
1 files changed, 212 insertions, 3 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 098a8ee40..8e3767740 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 | |||
| 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. 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. | ||
| 42 | constexpr std::uint32_t kWatchdogTrapRva = 0x2215c35; | ||
| 43 | constexpr std::uint32_t kWatchdogKickRva = 0x71b6331; | ||
| 44 | constexpr unsigned char kWatchdogTrap[] = {0xC7, 0x04, 0x25, 0, 0, 0, 0, 0, 0, 0, 0}; // movl $0, 0x0 | ||
| 45 | |||
| 46 | void 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. | ||
| 78 | void 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,22 +151,76 @@ 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. | ||
| 158 | void __cdecl reserve_tls_index0_at_start() { rt::reserve_tls_index0(); } | ||
| 159 | #pragma section(".CRT$XCT", read) | ||
| 160 | extern "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 | |||
| 164 | // Select the settings used to display the offline first-run UI. Keep explicit caller settings. | ||
| 165 | bool configure_first_run_ui(const std::filesystem::path& package, std::filesystem::path& overlay) { | ||
| 166 | // A variable that is set but empty is not a setting: Windows cannot hold one, but a caller on Wine can pass one | ||
| 167 | // (NAME= in the environment), and asking for the size alone reports it as present there. | ||
| 168 | const auto set_default = [](const wchar_t* name, const wchar_t* value) { | ||
| 169 | wchar_t first[2]{}; | ||
| 170 | return GetEnvironmentVariableW(name, first, 2) != 0 || _wputenv_s(name, value) == 0; | ||
| 171 | }; | ||
| 172 | const struct { const wchar_t* name; const wchar_t* value; } settings[] = { | ||
| 173 | {L"LEONIDA_D3D12", L"vulkan"}, {L"LEONIDA_ARENA_MB", L"2048"}, | ||
| 174 | {L"LEONIDA_GRAPHICS_STAGE_LAYOUT", L"1"}, {L"LEONIDA_PIPELINE_COMPILE", L"on-demand"}, | ||
| 175 | {L"LEONIDA_GPU_LABELS", L"1"}, {L"LEONIDA_FRAME_PACING", L"1"}, | ||
| 176 | {L"LEONIDA_KEYBOARD_PAD", L"always"}, | ||
| 177 | }; | ||
| 178 | for (const auto& setting : settings) { | ||
| 179 | if (!set_default(setting.name, setting.value)) return false; | ||
| 180 | } | ||
| 181 | std::wstring executable(32768, L'\0'); | ||
| 182 | const DWORD length = GetModuleFileNameW(nullptr, executable.data(), static_cast<DWORD>(executable.size())); | ||
| 183 | if (!length || length >= executable.size()) return false; | ||
| 184 | executable.resize(length); | ||
| 185 | const auto state = std::filesystem::path(executable).parent_path() / L"first-run-ui"; | ||
| 186 | if (overlay.empty()) overlay = state / L"overlay"; | ||
| 187 | if (!GetEnvironmentVariableW(L"LEONIDA_XMEM_BACKING_DIR", nullptr, 0)) { | ||
| 188 | std::error_code error; | ||
| 189 | const auto backing = state / L"xmem"; | ||
| 190 | std::filesystem::create_directories(backing, error); | ||
| 191 | if (error || !set_default(L"LEONIDA_XMEM_BACKING_DIR", backing.c_str())) return false; | ||
| 192 | } | ||
| 193 | return SetCurrentDirectoryW(package.c_str()) != FALSE; | ||
| 194 | } | ||
| 195 | |||
| 55 | int usage() { | 196 | int usage() { |
| 56 | std::fprintf(stderr, | 197 | 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" | 198 | "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace FILE]\n" |
| 58 | " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE]\n" | 199 | " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE]\n" |
| 200 | " gta6rt --first-run-ui <exe> [--game-dir DIR] [--overlay DIR] [--trace FILE]\n" | ||
| 59 | " gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n"); | 201 | " gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n"); |
| 60 | return 2; | 202 | return 2; |
| 61 | } | 203 | } |
| 62 | 204 | ||
| 63 | } // namespace | 205 | } // namespace |
| 64 | 206 | ||
| 207 | #ifdef LEONIDA_VULKAN_DEVICE | ||
| 208 | static BOOL WINAPI show_package_window(HWND window, int command) { | ||
| 209 | const BOOL previous = ShowWindow(window, command); | ||
| 210 | const DWORD error = GetLastError(); | ||
| 211 | if (command != SW_HIDE && rt::package_splash::paint_window(window)) | ||
| 212 | rt::startup_trace::event("package_splash_window"); | ||
| 213 | SetLastError(error); | ||
| 214 | return previous; | ||
| 215 | } | ||
| 216 | #endif | ||
| 217 | |||
| 65 | int wmain(int argc, wchar_t** argv) { | 218 | int wmain(int argc, wchar_t** argv) { |
| 66 | std::filesystem::path image, game_dir, overlay_dir, mods_dir; | 219 | 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 | 220 | uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash |
| 68 | int archetype_lookup_arg = 0; | 221 | int archetype_lookup_arg = 0; |
| 69 | uint32_t view_mode_getter_rva = 0; // --view-mode-getter RVA: the game's view mode of a camera context | 222 | uint32_t view_mode_getter_rva = 0; // --view-mode-getter RVA: the game's view mode of a camera context |
| 70 | bool dry_run = false, init = false, run = false, trace_d3d12 = false; | 223 | bool dry_run = false, init = false, run = false, trace_d3d12 = false, first_run_ui = false; |
| 71 | std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84) | 224 | std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84) |
| 72 | std::wstring trace_file; | 225 | std::wstring trace_file; |
| 73 | for (int i = 1; i < argc; ++i) { | 226 | for (int i = 1; i < argc; ++i) { |
| @@ -78,6 +231,7 @@ int wmain(int argc, wchar_t** argv) { | |||
| 78 | } | 231 | } |
| 79 | if (a == L"--dry-run") dry_run = true; | 232 | if (a == L"--dry-run") dry_run = true; |
| 80 | else if (a == L"--init") init = true; | 233 | else if (a == L"--init") init = true; |
| 234 | else if (a == L"--first-run-ui") first_run_ui = true; | ||
| 81 | else if (a == L"--run") run = true; | 235 | else if (a == L"--run") run = true; |
| 82 | else if (a == L"--trace-d3d12") trace_d3d12 = true; | 236 | else if (a == L"--trace-d3d12") trace_d3d12 = true; |
| 83 | else if (a == L"--trace" && i + 1 < argc) trace_file = argv[++i]; | 237 | else if (a == L"--trace" && i + 1 < argc) trace_file = argv[++i]; |
| @@ -104,7 +258,8 @@ int wmain(int argc, wchar_t** argv) { | |||
| 104 | } | 258 | } |
| 105 | if (image.empty()) return usage(); | 259 | if (image.empty()) return usage(); |
| 106 | // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point. | 260 | // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point. |
| 107 | if (dry_run + init + run != 1) return usage(); | 261 | if (dry_run + init + run + first_run_ui != 1) return usage(); |
| 262 | run = run || first_run_ui; | ||
| 108 | // The game runs AVX code from its TLS callbacks on; without AVX that is a bare 0xC000001D (src/host_cpu.h). | 263 | // The game runs AVX code from its TLS callbacks on; without AVX that is a bare 0xC000001D (src/host_cpu.h). |
| 109 | if ((init || run) && !rt::host_cpu::check_skipped()) | 264 | if ((init || run) && !rt::host_cpu::check_skipped()) |
| 110 | if (const auto missing = rt::host_cpu::missing_host_features(); !missing.empty()) { | 265 | if (const auto missing = rt::host_cpu::missing_host_features(); !missing.empty()) { |
| @@ -119,7 +274,21 @@ int wmain(int argc, wchar_t** argv) { | |||
| 119 | std::filesystem::path a = std::filesystem::absolute(p, ec); | 274 | std::filesystem::path a = std::filesystem::absolute(p, ec); |
| 120 | return ec ? p : a; | 275 | return ec ? p : a; |
| 121 | }; | 276 | }; |
| 277 | if (first_run_ui) { | ||
| 278 | image = absolute_or_same(image); | ||
| 279 | if (!mods_dir.empty()) mods_dir = absolute_or_same(mods_dir); | ||
| 280 | if (!trace_file.empty()) trace_file = absolute_or_same(trace_file).wstring(); | ||
| 281 | } | ||
| 122 | game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir); | 282 | game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir); |
| 283 | // An unattended run can select a fresh write root even when its launcher supplies --overlay. | ||
| 284 | if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_WRITE_DIR", nullptr, 0)) { | ||
| 285 | std::wstring dir(size, 0); | ||
| 286 | const DWORD n = GetEnvironmentVariableW(L"LEONIDA_WRITE_DIR", dir.data(), size); | ||
| 287 | if (n && n < size) { | ||
| 288 | dir.resize(n); | ||
| 289 | overlay_dir = dir; | ||
| 290 | } | ||
| 291 | } | ||
| 123 | if (!overlay_dir.empty()) overlay_dir = absolute_or_same(overlay_dir); | 292 | 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. | 293 | // 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)) { | 294 | if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_SHADERS", nullptr, 0)) { |
| @@ -135,8 +304,24 @@ int wmain(int argc, wchar_t** argv) { | |||
| 135 | if (!rt::base_reservation::inherited()) | 304 | if (!rt::base_reservation::inherited()) |
| 136 | if (const auto range = rt::base_reservation::needed(image)) return rt::base_reservation::relaunch(*range); | 305 | if (const auto range = rt::base_reservation::needed(image)) return rt::base_reservation::relaunch(*range); |
| 137 | // GAME: paths read under --game-dir; whatever a title writes lands in the overlay, never in the package. | 306 | // GAME: paths read under --game-dir; whatever a title writes lands in the overlay, never in the package. |
| 307 | if (first_run_ui && !configure_first_run_ui(game_dir, overlay_dir)) { | ||
| 308 | std::fprintf(stderr, "cannot initialize the first-run UI paths or graphics settings\n"); | ||
| 309 | return 1; | ||
| 310 | } | ||
| 138 | if (overlay_dir.empty()) overlay_dir = game_dir / "overlay"; | 311 | if (overlay_dir.empty()) overlay_dir = game_dir / "overlay"; |
| 139 | rt::fs::configure(game_dir, overlay_dir); | 312 | rt::fs::configure(game_dir, overlay_dir); |
| 313 | #ifdef LEONIDA_VULKAN_DEVICE | ||
| 314 | if (run) { | ||
| 315 | rt::package_splash::configure(game_dir); | ||
| 316 | if (rt::package_splash::configured()) { | ||
| 317 | const rt::shims::Entry window_imports[] = { | ||
| 318 | {"ext-ms-win-rtcore-ntuser-window-l1-1-0.dll", "ShowWindow", reinterpret_cast<void*>(&show_package_window)}, | ||
| 319 | {"user32.dll", "ShowWindow", reinterpret_cast<void*>(&show_package_window)}, | ||
| 320 | }; | ||
| 321 | rt::shims::Table{window_imports, std::size(window_imports)}; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | #endif | ||
| 140 | // Mods (docs/modding.md): <folder of gta6rt.exe>\mods unless --mods names another. Loaded and reported here; | 325 | // 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. | 326 | // the archetype swaps take effect once the game's archetype lookup is hooked for this executable version. |
| 142 | if (mods_dir.empty()) { | 327 | if (mods_dir.empty()) { |
| @@ -168,6 +353,10 @@ int wmain(int argc, wchar_t** argv) { | |||
| 168 | rt::d3d12_trace::set_enabled(trace_d3d12); | 353 | 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. | 354 | // 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); | 355 | rt::d3d12_xs::set_placed_at_va_pages_hook(&rt::xmem::commit_physical_range); |
| 356 | // So do the file shims' reads, whose kernel writes do not fault them in: on ERROR_NOACCESS (title_page_watch.h). | ||
| 357 | leonida::geometry_writes::aliases.store(&rt::xmem::geometry_aliases); | ||
| 358 | leonida::kernel_writes::set_committer(&rt::xmem::commit_physical_range); | ||
| 359 | rt::d3d12_xs::set_texture_page_provider(&rt::xmem::pin_mapped_data); | ||
| 171 | if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) { | 360 | 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" | 361 | 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"); | 362 | : "error: cannot create the trace file\n"); |
| @@ -186,6 +375,20 @@ int wmain(int argc, wchar_t** argv) { | |||
| 186 | try { | 375 | try { |
| 187 | rt::Loader loader(game_dir); | 376 | rt::Loader loader(game_dir); |
| 188 | rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code | 377 | rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code |
| 378 | // An executable with TLS that did not get index 0: its /GA code would read and write the TLS in slot 0, which | ||
| 379 | // is gta6rt's own (or another executable's), so no code of it may run. | ||
| 380 | const auto* main_nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>( | ||
| 381 | main_module.base + reinterpret_cast<const IMAGE_DOS_HEADER*>(main_module.base)->e_lfanew); | ||
| 382 | const bool main_is_exe = (main_nt->FileHeader.Characteristics & IMAGE_FILE_DLL) == 0; | ||
| 383 | if ((init || run) && main_is_exe && main_module.tls_slot != rt::kNoTlsIndex && main_module.tls_index != 0) { | ||
| 384 | std::string why; | ||
| 385 | if (rt::reserve_tls_index0(&why)) why = "another mapped executable holds it"; | ||
| 386 | std::fprintf(stderr, | ||
| 387 | "error: %s cannot have module TLS index 0 (%s); code built with /GA would read and write " | ||
| 388 | "another module's TLS through slot 0, so it is not run\n", | ||
| 389 | main_module.name.c_str(), why.c_str()); | ||
| 390 | return 1; | ||
| 391 | } | ||
| 189 | // The mods' archetype swaps (docs/modding.md): hooked here, before any image code runs, when the lookup's RVA | 392 | // 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. | 393 | // for this executable is given. The count of redirected calls shows whether the RVA is the real lookup. |
| 191 | if (view_mode_getter_rva) { | 394 | if (view_mode_getter_rva) { |
| @@ -272,7 +475,13 @@ int wmain(int argc, wchar_t** argv) { | |||
| 272 | } | 475 | } |
| 273 | std::printf("running %s\n", main_module.name.c_str()); | 476 | std::printf("running %s\n", main_module.name.c_str()); |
| 274 | std::fflush(stdout); | 477 | std::fflush(stdout); |
| 275 | const int code = rt::process::run_entry(main_module.base + main_module.entry_rva); | 478 | keep_title_watchdog_kicked(main_module.base); |
| 479 | start_title_peek(main_module.base); | ||
| 480 | int code; | ||
| 481 | { | ||
| 482 | auto stacks = rt::start_thread_stacks(); | ||
| 483 | code = rt::process::run_entry(main_module.base + main_module.entry_rva); | ||
| 484 | } | ||
| 276 | std::fflush(stdout); | 485 | std::fflush(stdout); |
| 277 | std::printf("game exited with code %d\n", code); | 486 | std::printf("game exited with code %d\n", code); |
| 278 | rt::shims::report_called_unimplemented(); | 487 | rt::shims::report_called_unimplemented(); |