Repository navigation
Conversation
…block too Continuation of ItsNotRexed#4313 by @mariuslozo-boop, closed unmerged with an invitation to take it over. Carries the PR's real intent onto current main, plus the correction @Roberto0953 posted in that discussion. @vakaobr found the divergence that made the correction necessary. The design and the fix are theirs. install_on_existing_threads() gives the loader's module-TLS block to threads already running when load() ran. Its guard read `m.tls_slot >= kMaxMappedTlsModules`, which is exactly the executable's slot (kMainImageSlot == kMaxMappedTlsModules), so the executable was skipped: a worker already running when the game was mapped kept whatever was in slot 0 and /GA code on it read the wrong block. The guard now uses kTlsSlotCount, the number of valid slots. The test change restores the contract main documents: load() itself gives an already-running thread its copy, and install_tls_for_thread()/uninstall_tls_for_thread() are no-ops there. The early-thread checks were still written for the previous contract and passed only because the guard skipped slot 0. Co-authored-by: mariuslozo-boop <mariuslozo-boop@users.noreply.github.com> Co-authored-by: Roberto0953 <Roberto0953@users.noreply.github.com> Co-authored-by: DerpSpears <61290890+DerpSpears@users.noreply.github.com>
|
Tested
|
|
Tested Before ( With this head: that fault is gone, all five startup reservations succeed, and the run goes on into the title's CRT and stops at AI-assisted. |
|
Closing this in favour of #8379 (@KevinPratap), which @lohant27-netizen is right to flag: it was opened 32 minutes earlier (18:36 vs 19:08), touches the same 13 files, is the same size (+1278/-77 vs +1269/-74), and — checked rather than assumed — it also carries the @lohant27-netizen and @evilclanker9000 — thank you both for testing it on hardware we do not have. Your two results together are the cleanest confirmation this fix has had: @evilclanker9000's before/after is exactly the mechanism — the read of One thing worth carrying over to #8379. The reason this branch is a correction of #4313 and not just a rebase is a specific defect @vakaobr found and @Roberto0953 fixed: So the useful evidence is the A/B: with the old guard and the corrected tests, Our branch is @mariuslozo-boop wrote the original; @Roberto0953 the correction; @vakaobr found the divergence. None of that changes with which PR carries it. AI-assisted. |
wavehs
left a comment
There was a problem hiding this comment.
Read the whole TLS path (no Windows here, so this is a code read only). The move is sound on the invariants that matter; two small things at the end.
What I checked
- Swap and free symmetry.
move_index0_on_this_threadswaps the two TEB pointers and then writeshomeinto the owner's_tls_index. The two blocks were allocated with the owner's size (the clone's TLS directory is rewritten to the owner'sSizeOfZeroFillbefore it is loaded, andreserve_index0refuses the move when the clone's raw+zero-fill does not match), so at thread exit the loader's per-module free takesslots[0](now the clone's block) for gta6rt.exe andslots[home](now the owner's live block) for the clone. Both pointers come from the same loader allocation path with the same size, so neither free is a size mismatch. - New threads.
gta6rt_tls_home_templateis a real 16 KiB array in the clone's file, filled with a copy of the owner's raw template bymake_home_clone_image, and the clone's TLS directory points at it, so a thread started later gets the owner's initial values, not zeros, athome— which is what the owner then reads from its first instruction. - Ordering. The only thread that can have run the owner's code before the move is the one doing it, and
move_index0_on_this_threadisnoinlinewith no slot address held across the swap, so the MSVC&ThreadLocalStoragePointer[_tls_index]caching you describe cannot bite on the current thread. - No
DllMainTLS.tls_host.cpp'sDllMainonly reads the plaing_thread_eventand calls out; loading the home clone runsDLL_PROCESS_ATTACHbefore any callback is installed, and the DLL creates no threads. That closes the one way a second thread could appear insidereserve_index0. - Static init order.
g_index0and thestd::array<std::atomic<...>>tables are constant-initialized, andstd::mutex/std::atomichave constexpr constructors, so the.CRT$XCTinitializer is safe ahead of this TU's dynamic initializers. Theg_tls_clonesheap allocation and the never-destroyed note are the right call given the atexit ordering.
Two nits
process_alive(pe_loader.cpp, the newGetExitCodeProcesscheck) is ambiguous for a process that exited with code 259: that isSTILL_ACTIVE, so such a clone owner is treated as alive forever and its file is never swept.WaitForSingleObject(p, 0) == WAIT_TIMEOUTanswers the question without the collision (and still covers the "handle held by a waiting parent" case the comment describes). Cheap to switch, and it makes the sweep exact.reserve_tls_index0can return a reason string on every start where the owner's template does not fit the 16 KiB array, andmain.cppturns that into a hard error for an EXE with TLS that did not get index 0 — correct — but the same string is what a title sees when the process simply was not started through the CRT initializer ("reserve_tls_index0 was not called at process start"). Worth wording the second case as a runtime bug ("the runtime's index-0 reservation did not run") rather than letting it read like the title's fault.
I could not run loader_tls_index0 here (Windows-only), so the above is a read; the test's own assertions (index 0 held by the mapped EXE, clones cleaned up) are the right shape.
AI-assisted. macOS 27.2.1 arm64; no Windows, no game files.
This is a continuation of #4313 by @mariuslozo-boop, which its author closed unmerged and invited someone to take over. It carries the PR's real intent onto current
mainand adds the correction @Roberto0953 posted in that discussion. @vakaobr found the divergence that made the correction necessary. The design and the fix are theirs; this branch only carries them forward.What this covers
reserve_tls_index0()movesgta6rt.exe's own static TLS to a helper "home" index so a mapped game executable can hold module TLS index 0, as a Windows process image does (RT-259). The executable takeskMainImageSlot(kMaxMappedTlsModules), not one of the helper slots.The correction
install_on_existing_threads()gives the loader's module-TLS block to threads that were already running whenload()ran; threads started later get theirs from the helper DLL'sDLL_THREAD_ATTACH. Its guard readm.tls_slot >= kMaxMappedTlsModules, which is exactly the executable's slot, so the executable was skipped: a thread-pool worker that was already running when the game was mapped kept whatever was in slot 0, and/GAcode on it read the wrong block. The guard now useskTlsSlotCount, the number of valid slots.The test change restores the contract
maindocuments:load()itself gives an already-running thread its copy, andinstall_tls_for_thread()/uninstall_tls_for_thread()are no-ops on such a thread. The early-thread checks were still written for the previous contract and passed only because the guard skipped slot 0.Contents
runtime/src/pe_loader.cpp- the guard coverskTlsSlotCount.runtime/test/loader_tls_index0_test.cpp- the early-thread checks assert theload()-installs-the-copy contract.66a7dbc1,a2542ccd,9366e54f) onto currentmain. The PR head28ea6b97was not used: its merge carries unrelated mods work.Testing
loader_tls_index0- 40 checks pass, including the new early-thread checks.loader_tls,loader_tls_index0_threads,loader_tls_index0_noreserve,run_tiny_tls_clone_cleanup- pass.run_tiny_tls_ga- the TLS phase passes (the/GAfixture reads 42 on its own thread and on a second thread with_tls_index == 0, and the trace records thetls_index0move). Its missing-TMPsubtest fails under Wine: Wine'sGetTempPathWfalls back toC:\users\...\TempwhenTMP/TEMPname a missing folder, so the reservation cannot fail as the check expects. Native Windows returns the missing path. Environmental, not the loader.tls_index0move, and the first blocking event after it is the title'sXGameRuntimeInitializefailure path (anint3; thexgameruntimeshim does not serveXErrorReport). That is the same stop the rebased branch reached before this correction, and it sits before any mapped/GAcode runs on a worker thread.Attribution
66a7dbc1,a2542ccd,9366e54f).b2902830in the Loader: the mapped game executable owns module TLS index 0 (RT-259) #4313 discussion).main's thread contract.No game data is included in this PR; the fixtures are ordinary test binaries. AI-assisted.