Commit Graph

2 Commits

Author SHA1 Message Date
b2360deeb6 loader_tls: a /GA executable beside a second TLS module, on the loading thread and on a new one (BL-1)
BL-1 (docs/boot_leads.md) says code built with /GA reads its TLS at module index 0 and that main stops there.
main already carries a fix (b7a3fed28 and 4710549af: load() rewrites the executable's gs:[58h] reads to a
per-thread view with the image's block at [0]), but no test maps a /GA executable and a second TLS module at
the same time, and no test shows that the fixture's code really has the index-0 shape: the CRT objects linked
into it read gs:[58h] through _tls_index, so "some reads were rewritten" holds for a non-/GA build too.

tiny_ga_tls gets ga_tls_address(), which returns where its code looks for its variable without touching it.
The new case uses it three ways. With LEONIDA_TLS_GA_PATCH=0 the address lies in this test EXE's own block at
index 0: the stop-4 shape, shown without reading or writing another module's TLS. By default it lies in the
fixture's own block, on the loading thread and on a thread started after load(), while tiny_tls.dll mapped
beside it keeps a block at its own index, gets its thread attach and detach, and this EXE's block and value
stay at index 0 of each thread's real TLS array. ~Loader takes the view out again.

No loader change: the case passes on main as it is, which is the finding. pe_loader.cpp and tls_host.cpp are
untouched.

Tested on Windows 11, MSVC 19.44 (VS 2022 Build Tools), Ninja, RelWithDebInfo, at main 8f05e09e2:
loader_tls_test OK (118 checks, 95 before), 40 runs in a row; with LEONIDA_TLS_GA_PATCH=0 set for the whole
process, 8 checks fail, as they should (7 existing ones and "by default load() rewrites the EXE's index-0
TLS reads"). MSVC emits the index-0 read for ga_tls_address at /Od and at /O2 (checked in the listing). Full
ctest: 304 of 306, the same two failures as main (d3d12_execute_indirect_validation, d3d12_depth_resolve).
Not run on the retail executable: that needs game files.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf
2026-11-24 10:24:51 -05:00
4710549aff pe_loader: a thread that existed before load() gets the /GA TLS view too (EGEN-GA-TLS-EXISTING-THREAD)
load() rewrites a /GA executable's `mov r64, gs:[58h]` reads to a TlsAlloc slot whose value is a per-thread
view with the image's block at [0] (pe_loader.h, tls_ga_slot). install_thread_block creates that view for
the loading thread and for threads started after load(), but install_on_existing_threads gave the threads
that were already running only the block in TEB->ThreadLocalStoragePointer[tls_index]. A rewritten index-0
read on such a thread then loaded null and dereferenced it: the task queue runs a title's callbacks on pool
workers that predate the load, so this is the same AV the main thread had at exe+0x104075, moved to a worker.

Each existing thread now also gets a view of its own in TEB->TlsSlots[ga_slot], and ~Loader, the thread's
detach event and a reused thread id free it with the block.

Tested: runtime/test/loader_tls_test.cpp maps a new handmade /GA EXE fixture (tiny_ga_tls) and reads its
__declspec(thread) value from the loading thread, from a thread that existed before load() and from a later
one, then again after a second load(). Without the pe_loader change that test dies with 0xC0000005 on the
existing thread; with it all 95 checks pass. ctest -R loader: 4/4 passed (Windows 11, MSVC 19.44,
RelWithDebInfo, no GPU needed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 22:13:50 +01:00