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/vulkan_aftermath.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/vulkan_aftermath.cpp')
0 files changed, 0 insertions, 0 deletions