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:
authorLuiz Neves <luizgustavobenicio@gmail.com>2026-11-25 15:24:27 -0300
committerLuiz Neves <luizgustavobenicio@gmail.com>2026-11-25 15:24:27 -0300
commite68fd7f373dd2c6c6b027e53a2a62b24eaacc11d (patch)
treeea9c9fb1d540564927ed884653ce77b4639dd8b3 /runtime/src/main.cpp
parentfbde472118560c5e0e21d70333c2c5812533d3b4 (diff)
parentb9320a118d46b24de665e30e577425c131b4a2ee (diff)
Merge remote-tracking branch 'ubq/main' into HEAD
Diffstat (limited to 'runtime/src/main.cpp')
-rw-r--r--runtime/src/main.cpp261
1 files changed, 258 insertions, 3 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index 098a8ee40..f01997775 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,136 @@
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, at most 32; "RVA>OFF" reads through the pointer stored at the RVA, at
76// pointer + OFF, both hex) every LEONIDA_PEEK_SECONDS (default 10). For answering "what does this
77// global hold at run time" without a debugger: the reverse-engineering notes (docs/re/requests.md) ask for exactly
78// that. Reads are checked; nothing is written.
79void start_title_peek(std::uint8_t* base) {
80 static char spec[1024];
81 const DWORD n = GetEnvironmentVariableA("LEONIDA_PEEK", spec, sizeof(spec));
82 if (!n || n >= sizeof(spec)) return;
83 static std::uint8_t* image = base;
84 const HANDLE thread = CreateThread(
85 nullptr, 0,
86 [](LPVOID) -> DWORD {
87 char seconds[16]{};
88 DWORD period = 10;
89 if (GetEnvironmentVariableA("LEONIDA_PEEK_SECONDS", seconds, sizeof(seconds)) > 0)
90 period = std::max<DWORD>(1, static_cast<DWORD>(std::strtoul(seconds, nullptr, 10)));
91 for (unsigned tick = 0;; ++tick) {
92 std::string line = "[rt] peek t=" + std::to_string(tick * period) + "s:";
93 for (const char* at = spec; *at;) {
94 char* end = nullptr;
95 const std::uint64_t rva = std::strtoull(at, &end, 16);
96 if (end == at) break;
97 // "RVA>OFF": the 8-byte pointer at the RVA, then the values at pointer + OFF (both hex).
98 bool deref = false;
99 std::uint64_t offset = 0;
100 if (*end == '>') {
101 deref = true;
102 offset = std::strtoull(end + 1, &end, 16);
103 }
104 unsigned count = 1;
105 if (*end == ':') count = static_cast<unsigned>(std::strtoul(end + 1, &end, 10));
106 char text[48];
107 if (deref)
108 std::snprintf(text, sizeof(text), " 0x%llx>%llx=", static_cast<unsigned long long>(rva),
109 static_cast<unsigned long long>(offset));
110 else
111 std::snprintf(text, sizeof(text), " 0x%llx=", static_cast<unsigned long long>(rva));
112 line += text;
113 const std::uint8_t* from = image + rva;
114 if (deref) {
115 std::uint64_t pointer = 0;
116 SIZE_T got = 0;
117 if (!ReadProcessMemory(GetCurrentProcess(), from, &pointer, sizeof(pointer), &got) ||
118 got != sizeof(pointer) || !pointer)
119 count = 0, line += "?";
120 from = reinterpret_cast<const std::uint8_t*>(pointer + offset);
121 }
122 for (unsigned i = 0; i < count && i < 32; ++i) {
123 std::uint64_t value = 0;
124 SIZE_T got = 0;
125 const bool ok = ReadProcessMemory(GetCurrentProcess(), from + 8ull * i, &value,
126 sizeof(value), &got) && got == sizeof(value);
127 std::snprintf(text, sizeof(text), ok ? "%s%llx" : "%s?", i ? "," : "",
128 static_cast<unsigned long long>(value));
129 line += text;
130 }
131 at = *end == ',' ? end + 1 : end;
132 if (at == end && *end) break;
133 }
134 std::fprintf(stderr, "%s\n", line.c_str());
135 Sleep(period * 1000);
136 }
137 },
138 nullptr, 0, nullptr);
139 if (thread) CloseHandle(thread);
140}
141
142} // namespace
143
30#include "base_reservation.h" 144#include "base_reservation.h"
31#include "code_hooks.h" 145#include "code_hooks.h"
32#include "cpu_emulation.h" 146#include "cpu_emulation.h"
33#include "host_cpu.h" 147#include "host_cpu.h"
34#include "mods.h" 148#include "mods.h"
35#include "pe_loader.h" 149#include "pe_loader.h"
150#include "thread_stacks.h"
151#include "thread_sampler.h"
152#include "title_page_watch.h"
153#include "title_geometry_watch.h"
36#include "../shims/d3d12_trace.h" 154#include "../shims/d3d12_trace.h"
37#include "../shims/d3d12_xs_device.h" 155#include "../shims/d3d12_xs_device.h"
38#include "../shims/file_system.h" 156#include "../shims/file_system.h"
157#ifdef LEONIDA_VULKAN_DEVICE
158#include "../shims/d3d12_compile_exit.h"
159#include "../shims/package_splash.h"
160#endif
39#include "../shims/process_shim.h" 161#include "../shims/process_shim.h"
40#include "../shims/shims.h" 162#include "../shims/shims.h"
41#include "../shims/startup_trace.h" 163#include "../shims/startup_trace.h"
@@ -52,22 +174,88 @@ const char* binding_name(rt::Binding b) {
52 } 174 }
53} 175}
54 176
177// Module TLS index 0 for the game's executable (rt::reserve_tls_index0), from the CRT's initializer table: .CRT$XCT
178// runs after the C runtime is set up and before every C++ dynamic initializer of gta6rt (.CRT$XCU), so before any of
179// gta6rt's code can have started a thread. The frames above it (wmainCRTStartup, __scrt_common_main_seh, _initterm)
180// hold no TLS slot address. The result is read again where an image is run.
181void __cdecl reserve_tls_index0_at_start() { rt::reserve_tls_index0(); }
182#pragma section(".CRT$XCT", read)
183extern "C" __declspec(allocate(".CRT$XCT")) void(__cdecl* const gta6rt_reserve_tls_index0)() =
184 reserve_tls_index0_at_start;
185#pragma comment(linker, "/INCLUDE:gta6rt_reserve_tls_index0")
186
187// Select the settings used to display the offline first-run UI. Keep explicit caller settings.
188bool configure_first_run_ui(const std::filesystem::path& package, std::filesystem::path& overlay) {
189 // A variable that is set but empty is not a setting: Windows cannot hold one, but a caller on Wine can pass one
190 // (NAME= in the environment), and asking for the size alone reports it as present there.
191 const auto set_default = [](const wchar_t* name, const wchar_t* value) {
192 wchar_t first[2]{};
193 return GetEnvironmentVariableW(name, first, 2) != 0 || _wputenv_s(name, value) == 0;
194 };
195 const struct { const wchar_t* name; const wchar_t* value; } settings[] = {
196 {L"LEONIDA_D3D12", L"vulkan"}, {L"LEONIDA_ARENA_MB", L"2048"},
197 {L"LEONIDA_GRAPHICS_STAGE_LAYOUT", L"1"}, {L"LEONIDA_PIPELINE_COMPILE", L"on-demand"},
198 {L"LEONIDA_FRAME_PACING", L"1"},
199 {L"LEONIDA_KEYBOARD_PAD", L"always"},
200 };
201 for (const auto& setting : settings) {
202 if (!set_default(setting.name, setting.value)) return false;
203 }
204 std::wstring executable(32768, L'\0');
205 const DWORD length = GetModuleFileNameW(nullptr, executable.data(), static_cast<DWORD>(executable.size()));
206 if (!length || length >= executable.size()) return false;
207 executable.resize(length);
208 const auto state = std::filesystem::path(executable).parent_path() / L"first-run-ui";
209 if (overlay.empty()) overlay = state / L"overlay";
210 if (!GetEnvironmentVariableW(L"LEONIDA_XMEM_BACKING_DIR", nullptr, 0)) {
211 std::error_code error;
212 const auto backing = state / L"xmem";
213 std::filesystem::create_directories(backing, error);
214 if (error || !set_default(L"LEONIDA_XMEM_BACKING_DIR", backing.c_str())) return false;
215 }
216 return SetCurrentDirectoryW(package.c_str()) != FALSE;
217}
218
55int usage() { 219int usage() {
56 std::fprintf(stderr, 220 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" 221 "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" 222 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE]\n"
223 " 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"); 224 " 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; 225 return 2;
61} 226}
62 227
63} // namespace 228} // namespace
64 229
230#ifdef LEONIDA_VULKAN_DEVICE
231// The run ends with the title's Vulkan device alive, so no ~Device stops the background pipeline compiler: stop it
232// here, before ExitProcess ends its thread, maybe inside the driver's compiler (rt::d3d12::stop_pipeline_compiles).
233constexpr unsigned kExitCompileWaitMs = 2000; // a console close gives the whole process 5 s
234
235static BOOL WINAPI stop_compiles_on_console_exit(DWORD event) {
236 if (event == CTRL_C_EVENT || event == CTRL_BREAK_EVENT || event == CTRL_CLOSE_EVENT) {
237 rt::d3d12_xs::stop_list_values(500); // as at the run's end: no list-value thread cut off inside the layer
238 rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs);
239 }
240 return FALSE; // the next handler (the default one: ExitProcess) ends the process, as before
241}
242
243static BOOL WINAPI show_package_window(HWND window, int command) {
244 const BOOL previous = ShowWindow(window, command);
245 const DWORD error = GetLastError();
246 if (command != SW_HIDE && rt::package_splash::paint_window(window))
247 rt::startup_trace::event("package_splash_window");
248 SetLastError(error);
249 return previous;
250}
251#endif
252
65int wmain(int argc, wchar_t** argv) { 253int wmain(int argc, wchar_t** argv) {
66 std::filesystem::path image, game_dir, overlay_dir, mods_dir; 254 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 255 uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash
68 int archetype_lookup_arg = 0; 256 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 257 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; 258 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) 259 std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84)
72 std::wstring trace_file; 260 std::wstring trace_file;
73 for (int i = 1; i < argc; ++i) { 261 for (int i = 1; i < argc; ++i) {
@@ -78,6 +266,7 @@ int wmain(int argc, wchar_t** argv) {
78 } 266 }
79 if (a == L"--dry-run") dry_run = true; 267 if (a == L"--dry-run") dry_run = true;
80 else if (a == L"--init") init = true; 268 else if (a == L"--init") init = true;
269 else if (a == L"--first-run-ui") first_run_ui = true;
81 else if (a == L"--run") run = true; 270 else if (a == L"--run") run = true;
82 else if (a == L"--trace-d3d12") trace_d3d12 = true; 271 else if (a == L"--trace-d3d12") trace_d3d12 = true;
83 else if (a == L"--trace" && i + 1 < argc) trace_file = argv[++i]; 272 else if (a == L"--trace" && i + 1 < argc) trace_file = argv[++i];
@@ -104,7 +293,8 @@ int wmain(int argc, wchar_t** argv) {
104 } 293 }
105 if (image.empty()) return usage(); 294 if (image.empty()) return usage();
106 // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point. 295 // 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(); 296 if (dry_run + init + run + first_run_ui != 1) return usage();
297 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). 298 // 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()) 299 if ((init || run) && !rt::host_cpu::check_skipped())
110 if (const auto missing = rt::host_cpu::missing_host_features(); !missing.empty()) { 300 if (const auto missing = rt::host_cpu::missing_host_features(); !missing.empty()) {
@@ -119,7 +309,21 @@ int wmain(int argc, wchar_t** argv) {
119 std::filesystem::path a = std::filesystem::absolute(p, ec); 309 std::filesystem::path a = std::filesystem::absolute(p, ec);
120 return ec ? p : a; 310 return ec ? p : a;
121 }; 311 };
312 if (first_run_ui) {
313 image = absolute_or_same(image);
314 if (!mods_dir.empty()) mods_dir = absolute_or_same(mods_dir);
315 if (!trace_file.empty()) trace_file = absolute_or_same(trace_file).wstring();
316 }
122 game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir); 317 game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir);
318 // An unattended run can select a fresh write root even when its launcher supplies --overlay.
319 if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_WRITE_DIR", nullptr, 0)) {
320 std::wstring dir(size, 0);
321 const DWORD n = GetEnvironmentVariableW(L"LEONIDA_WRITE_DIR", dir.data(), size);
322 if (n && n < size) {
323 dir.resize(n);
324 overlay_dir = dir;
325 }
326 }
123 if (!overlay_dir.empty()) overlay_dir = absolute_or_same(overlay_dir); 327 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. 328 // 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)) { 329 if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_SHADERS", nullptr, 0)) {
@@ -135,8 +339,24 @@ int wmain(int argc, wchar_t** argv) {
135 if (!rt::base_reservation::inherited()) 339 if (!rt::base_reservation::inherited())
136 if (const auto range = rt::base_reservation::needed(image)) return rt::base_reservation::relaunch(*range); 340 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. 341 // GAME: paths read under --game-dir; whatever a title writes lands in the overlay, never in the package.
342 if (first_run_ui && !configure_first_run_ui(game_dir, overlay_dir)) {
343 std::fprintf(stderr, "cannot initialize the first-run UI paths or graphics settings\n");
344 return 1;
345 }
138 if (overlay_dir.empty()) overlay_dir = game_dir / "overlay"; 346 if (overlay_dir.empty()) overlay_dir = game_dir / "overlay";
139 rt::fs::configure(game_dir, overlay_dir); 347 rt::fs::configure(game_dir, overlay_dir);
348#ifdef LEONIDA_VULKAN_DEVICE
349 if (run) {
350 rt::package_splash::configure(game_dir);
351 if (rt::package_splash::configured()) {
352 const rt::shims::Entry window_imports[] = {
353 {"ext-ms-win-rtcore-ntuser-window-l1-1-0.dll", "ShowWindow", reinterpret_cast<void*>(&show_package_window)},
354 {"user32.dll", "ShowWindow", reinterpret_cast<void*>(&show_package_window)},
355 };
356 rt::shims::Table{window_imports, std::size(window_imports)};
357 }
358 }
359#endif
140 // Mods (docs/modding.md): <folder of gta6rt.exe>\mods unless --mods names another. Loaded and reported here; 360 // 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. 361 // the archetype swaps take effect once the game's archetype lookup is hooked for this executable version.
142 if (mods_dir.empty()) { 362 if (mods_dir.empty()) {
@@ -168,6 +388,11 @@ int wmain(int argc, wchar_t** argv) {
168 rt::d3d12_trace::set_enabled(trace_d3d12); 388 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. 389 // 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); 390 rt::d3d12_xs::set_placed_at_va_pages_hook(&rt::xmem::commit_physical_range);
391 // So do the file shims' reads, whose kernel writes do not fault them in: on ERROR_NOACCESS (title_page_watch.h).
392 leonida::geometry_writes::aliases.store(&rt::xmem::geometry_aliases);
393 leonida::kernel_writes::set_committer(&rt::xmem::commit_physical_range);
394 rt::d3d12_xs::set_texture_page_provider(&rt::xmem::pin_mapped_data);
395 leonida::kernel_writes::set_unguarder(&rt::xmem::reopen_unowned);
171 if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) { 396 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" 397 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"); 398 : "error: cannot create the trace file\n");
@@ -186,6 +411,20 @@ int wmain(int argc, wchar_t** argv) {
186 try { 411 try {
187 rt::Loader loader(game_dir); 412 rt::Loader loader(game_dir);
188 rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code 413 rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code
414 // An executable with TLS that did not get index 0: its /GA code would read and write the TLS in slot 0, which
415 // is gta6rt's own (or another executable's), so no code of it may run.
416 const auto* main_nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(
417 main_module.base + reinterpret_cast<const IMAGE_DOS_HEADER*>(main_module.base)->e_lfanew);
418 const bool main_is_exe = (main_nt->FileHeader.Characteristics & IMAGE_FILE_DLL) == 0;
419 if ((init || run) && main_is_exe && main_module.tls_slot != rt::kNoTlsIndex && main_module.tls_index != 0) {
420 std::string why;
421 if (rt::reserve_tls_index0(&why)) why = "another mapped executable holds it";
422 std::fprintf(stderr,
423 "error: %s cannot have module TLS index 0 (%s); code built with /GA would read and write "
424 "another module's TLS through slot 0, so it is not run\n",
425 main_module.name.c_str(), why.c_str());
426 return 1;
427 }
189 // The mods' archetype swaps (docs/modding.md): hooked here, before any image code runs, when the lookup's RVA 428 // 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. 429 // for this executable is given. The count of redirected calls shows whether the RVA is the real lookup.
191 if (view_mode_getter_rva) { 430 if (view_mode_getter_rva) {
@@ -272,7 +511,17 @@ int wmain(int argc, wchar_t** argv) {
272 } 511 }
273 std::printf("running %s\n", main_module.name.c_str()); 512 std::printf("running %s\n", main_module.name.c_str());
274 std::fflush(stdout); 513 std::fflush(stdout);
275 const int code = rt::process::run_entry(main_module.base + main_module.entry_rva); 514 keep_title_watchdog_kicked(main_module.base);
515 start_title_peek(main_module.base);
516 leonida::thread_sampler::start(main_module.base, main_module.size); // LEONIDA_SAMPLE_MS (diagnostic)
517#ifdef LEONIDA_VULKAN_DEVICE
518 SetConsoleCtrlHandler(&stop_compiles_on_console_exit, TRUE);
519#endif
520 int code;
521 {
522 auto stacks = rt::start_thread_stacks();
523 code = rt::process::run_entry(main_module.base + main_module.entry_rva);
524 }
276 std::fflush(stdout); 525 std::fflush(stdout);
277 std::printf("game exited with code %d\n", code); 526 std::printf("game exited with code %d\n", code);
278 rt::shims::report_called_unimplemented(); 527 rt::shims::report_called_unimplemented();
@@ -282,6 +531,12 @@ int wmain(int argc, wchar_t** argv) {
282 // Not `return`: a normal host exit runs the CRT's atexit table, which the game shares with gta6rt (its 531 // Not `return`: a normal host exit runs the CRT's atexit table, which the game shares with gta6rt (its
283 // handlers would run against a game that has stopped) and whose last word decides the exit status. 532 // handlers would run against a game that has stopped) and whose last word decides the exit status.
284 // ExitProcess also skips atexit, so the TLS clone cleanup registered there is run here instead. 533 // ExitProcess also skips atexit, so the TLS clone cleanup registered there is run here instead.
534 // The list-value threads end first: ExitProcess must not cut one off inside the layer (in a held submission,
535 // holding its queues' lock).
536 rt::d3d12_xs::stop_list_values(500);
537#ifdef LEONIDA_VULKAN_DEVICE
538 rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs);
539#endif
285 rt::cleanup_tls_clones(); 540 rt::cleanup_tls_clones();
286 ExitProcess(static_cast<UINT>(code)); 541 ExitProcess(static_cast<UINT>(code));
287 } catch (const std::exception& e) { 542 } catch (const std::exception& e) {