diff options
| author | mariuslozo-boop <254631011+mariuslozo-boop@users.noreply.github.com> | 2026-11-23 00:56:19 +0100 |
|---|---|---|
| committer | mariuslozo-boop <254631011+mariuslozo-boop@users.noreply.github.com> | 2026-11-23 01:38:19 +0100 |
| commit | a2542ccd6f394ab418daa39074f3d394b3daf6e7 (patch) | |
| tree | 8890b896176dbd818ab7d96c544983711abbdd94 /runtime/src/main.cpp | |
| parent | 66a7dbc1cd49cc29b34ceeab3020d42ca85f03d2 (diff) | |
Loader: reserve TLS index 0 at start, with a home template that copies the EXE's
Fixes from the review of the index-0 change:
- The lazy move at the first executable map stopped other threads and swapped
their slots, guarded by a "thread is inside gta6rt.exe" check. That is not
enough: MSVC keeps &ThreadLocalStoragePointer[_tls_index] across calls
(zlib_inflate saves it at [rbp-80h] across memset, push_back and
inflate_codes, then reads _Init_thread_epoch through it), so a thread
stopped in the heap or memset still reads the wrong slot afterwards. The
move is now rt::reserve_tls_index0(), done once at process start: gta6rt
calls it from a .CRT$XCT initializer, before its own static initializers
and before any of its threads; the CRT startup frames above it hold no TLS
slot address (checked in the disassembly). It swaps slots 0 and home on the
calling thread only; no thread enumeration, suspension, TEB writes or
TlsAlloc mark are left.
- The home clone's block was the helper's 514 bytes, smaller than
gta6rt.exe's 1398-byte TLS. The home clone is now written from
gta6rt_tls_host.dll with its TLS directory pointed at a new 16 KiB export,
gta6rt_tls_home_template, filled with a copy of the EXE's template and its
alignment (LdrpAllocateTls copies End - Start and ignores SizeOfZeroFill,
so the zero fill is part of the range). Every new thread then has a fresh
copy of gta6rt.exe's TLS at the home index from its first instruction, so
no adoption step or window before the helper's DLL_THREAD_ATTACH is left.
The mapped template is checked against the EXE's after the load.
- The owner is GetModuleHandleW(nullptr) with _tls_index 0, not any module
whose index reads 0.
- A primary helper without gta6rt_tls_host_set_thread_event is refused.
- An executable that does not get index 0 (no reservation, or another
executable holds it) is refused by --run and --init with an error; its /GA
code would read and write another module's TLS through slot 0. The README
no longer says it "reads the first one's slot 0".
- Thread attach installs every module's copy before any TLS callback runs,
as Windows allocates all blocks before the first DllMain.
- Tests: loader_tls_index0 (runs) checks this EXE's block moving from slot 0
to the home index by identity, the home template's size and content, and
that the getter reads slot 0 with its _tls_index pointing elsewhere (fails
for a non-/GA build of the fixture); loader_tls_index0_threads reserves
while threads are being created, with one suspended thread that has no TLS
array yet and tiny_thread_hook.dll running test code before the helper's
DLL_THREAD_ATTACH; loader_tls_index0_noreserve maps without the
reservation. The tiny_tls_ga.cpp comment names the real test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/main.cpp')
| -rw-r--r-- | runtime/src/main.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 49917bf18..6c4d0e641 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp | |||
| @@ -44,6 +44,16 @@ const char* binding_name(rt::Binding b) { | |||
| 44 | } | 44 | } |
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | // Module TLS index 0 for the game's executable (rt::reserve_tls_index0), from the CRT's initializer table: .CRT$XCT | ||
| 48 | // runs after the C runtime is set up and before every C++ dynamic initializer of gta6rt (.CRT$XCU), so before any of | ||
| 49 | // gta6rt's code can have started a thread. The frames above it (wmainCRTStartup, __scrt_common_main_seh, _initterm) | ||
| 50 | // hold no TLS slot address. The result is read again where an image is run. | ||
| 51 | void __cdecl reserve_tls_index0_at_start() { rt::reserve_tls_index0(); } | ||
| 52 | #pragma section(".CRT$XCT", read) | ||
| 53 | extern "C" __declspec(allocate(".CRT$XCT")) void(__cdecl* const gta6rt_reserve_tls_index0)() = | ||
| 54 | reserve_tls_index0_at_start; | ||
| 55 | #pragma comment(linker, "/INCLUDE:gta6rt_reserve_tls_index0") | ||
| 56 | |||
| 47 | int usage() { | 57 | int usage() { |
| 48 | std::fprintf(stderr, | 58 | std::fprintf(stderr, |
| 49 | "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--trace FILE]\n" | 59 | "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--trace FILE]\n" |
| @@ -105,6 +115,20 @@ int wmain(int argc, wchar_t** argv) { | |||
| 105 | try { | 115 | try { |
| 106 | rt::Loader loader(game_dir); | 116 | rt::Loader loader(game_dir); |
| 107 | rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code | 117 | rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code |
| 118 | // An executable with TLS that did not get index 0: its /GA code would read and write the TLS in slot 0, which | ||
| 119 | // is gta6rt's own (or another executable's), so no code of it may run. | ||
| 120 | const auto* main_nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>( | ||
| 121 | main_module.base + reinterpret_cast<const IMAGE_DOS_HEADER*>(main_module.base)->e_lfanew); | ||
| 122 | const bool main_is_exe = (main_nt->FileHeader.Characteristics & IMAGE_FILE_DLL) == 0; | ||
| 123 | if ((init || run) && main_is_exe && main_module.tls_slot != rt::kNoTlsIndex && main_module.tls_index != 0) { | ||
| 124 | std::string why; | ||
| 125 | if (rt::reserve_tls_index0(&why)) why = "another mapped executable holds it"; | ||
| 126 | std::fprintf(stderr, | ||
| 127 | "error: %s cannot have module TLS index 0 (%s); code built with /GA would read and write " | ||
| 128 | "another module's TLS through slot 0, so it is not run\n", | ||
| 129 | main_module.name.c_str(), why.c_str()); | ||
| 130 | return 1; | ||
| 131 | } | ||
| 108 | if (run) rt::process::set_command_line(image.filename().wstring(), game_args); | 132 | if (run) rt::process::set_command_line(image.filename().wstring(), game_args); |
| 109 | if (init || run) { | 133 | if (init || run) { |
| 110 | loader.initialise(); // TLS callbacks, then DllMain(DLL_PROCESS_ATTACH) for DLLs | 134 | loader.initialise(); // TLS callbacks, then DllMain(DLL_PROCESS_ATTACH) for DLLs |