Repository navigation
loader: enumerate only this process's threads for startup TLS - #7884
sarveshgamers wants to merge 7 commits into
Conversation
Loader::install_on_existing_threads takes a TH32CS_SNAPTHREAD snapshot and walks every thread in the system, once per mapped TLS module. With gta6rt --dry-run on the retail executable (Windows 11, 5,650 system threads), that is 6-10 ms for the snapshot and 23-32 ms for the Thread32Next walk per module; the three startup TLS modules add about 90 ms. Prefer ntdll's NtGetNextThread, which returns one handle per thread of this process only (0.008 ms for the one-thread probe). It still runs per module, so a thread created between two module loads is seen; the calling thread is kept as the enumeration cursor and skipped by the install loop. A missing export or a call that does not reach STATUS_NO_MORE_ENTRIES falls back to the existing Toolhelp walk, which also keeps Wine and older Windows working.
… templates The enumeration in install_on_existing_threads runs per load, not once per process: a thread created after the first module has no DLL_THREAD_ATTACH for a module mapped later, so it still has to be found. The new case starts a thread between three loads and checks that it reads all three templates; a cached thread list would fail it.
A process always has its calling thread. An implementation that answers STATUS_NO_MORE_ENTRIES without yielding one is treated like a missing export and retried through Toolhelp, so a broken or stub ntdll cannot silently skip every other thread.
abe50d9 to
7880e0f
Compare
|
AI-assisted Windows 11 review of I reproduced this with the synthetic
struct Observe {
HANDLE value{};
~Observe() {
DWORD flags{};
if (value && GetHandleInformation(value, &flags)) {
std::fprintf(stderr, "retained thread handle after candidate cleanup: thread %lu\n", GetThreadId(value));
CloseHandle(value);
}
}
} probe;
The unmodified test passes. With the allocation failure, |
|
Independent native Windows validation of head Windows 11 Pro; MSVC 19.44.35230.0, Windows SDK 10.0.26100.0, CMake 3.31.6-msvc6; Visual Studio 2022 x64 RelWithDebInfo. Host GPU: NVIDIA RTX 5060 Ti, driver 32.0.16.2114 (these are CPU tests). Built
3/3 CTest entries passed. Only locally built synthetic PE/DLL fixtures were used. This does not validate retail startup performance, the forced-Toolhelp fallback, or the full runtime suite. AI-assisted with Codex. |
|
Tested 7880e0f on Windows 11 Pro 10.0.26300, MSVC 14.51 (VS Build Tools 2026), RTX 4060 / 621.24 (CPU tests only, no Vulkan SDK): pass. Merged onto main dde82f2 (clean): full runtime build, then Code read: the walk keeps each returned handle as the next call's cursor and in AI-assisted (Claude Code). No game files. |
|
Tested 7880e0f on NVIDIA GeForce RTX 5070 Ti, driver 617.42, Windows 11 build 26300, VS 2022 Build Tools (MSVC 19.44), Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, RelWithDebInfo: pass. Built Synthetic tests only, no game files. AI-assisted (Claude Code). |
…ot grow A failed candidates.push_back left the handle returned by NtGetNextThread open, because the candidate list is what owns handles. A local owner now closes it unless the vector takes it. Reproduced with the reviewer's std::pmr::null_memory_resource() synthetic: without the owner the observer saw the retained handle after cleanup; with it, none. The normal loader_tls suite still passes 86 checks.
|
Thanks, fixed in d9dbcd8. The handle returned by NtGetNextThread now sits in a local owner until candidates.push_back succeeds, and the owner closes it if the vector throws. Reproduced with the pmr null_memory_resource synthetic: without the owner the observer printed the retained handle after cleanup; with it, none. Normal loader_tls still passes 86/86. On the fallback gap: I rebuilt with the export name deliberately disabled and loader_tls passed 86/86 through the Toolhelp path. The Windows test only takes NtGetNextThread otherwise, as noted; the Wine job is the natural fallback coverage in CI. |
LEONIDA_LOADER_TOOLHELP=1 forces the documented fallback, and a second CTest entry runs loader_tls_test through it (86 checks). The variable is documented in runtime/README.md, so test_docs_env_vars stays green. The normal entry still covers NtGetNextThread.
|
The fallback gap is now covered too: LEONIDA_LOADER_TOOLHELP=1 forces the Toolhelp path, and a second CTest entry (loader_tls_toolhelp) runs the same 86 checks through it. Both entries pass locally; runtime/README.md documents the variable and tests/test_docs_env_vars.py stays green. |
…ead id Two review follow-ups: ThreadHandle now has an explicit constructor and deleted copy so it cannot be duplicated and double-close, and a returned handle whose GetThreadId is 0 makes the walk incomplete instead of being installed under id 0 (the calling thread would then not be skipped). loader_tls passes 86 checks on both the NtGetNextThread and Toolhelp paths.
|
Second review pass: ThreadHandle is now single-owner (explicit constructor, deleted copy), and a handle whose GetThreadId is 0 makes the walk incomplete and falls back, instead of installing the calling thread under id 0. loader_tls still passes 86 checks on both the NtGetNextThread and Toolhelp paths. The body now lists #7524, #5805 and #4313 as rebase conflicts and uses the current coordination thread #8047. |
|
Independent retest of Built from the exact head with no local source edits, then focused CTest:
4/4 CTest entries passed. This independently covers both the normal enumeration and the forced fallback on Windows. It does not inject allocation failure, unreadable thread IDs, or partial-enumeration failure. No game files; not a full runtime-suite or retail performance run. AI-assisted with Codex. |
NtGetNextThread returns the calling thread too, but its only use is as the next call's cursor: install_thread_block already gave it the module's copy. Keep that handle in a separate cursor list, so the install loop is unchanged and a single-thread process does not look like an empty walk. The Toolhelp fallback excludes the calling thread again, and loader_tls passes 86 checks on both paths.
|
Follow-up to the review: the calling thread's handle now stays out of the candidate list (it is only the next-call cursor), so the install loop is unchanged and a single-thread process counts as a complete walk. That also removes the direct hunk overlap with #7524/#5805. loader_tls is 86/86 on both the NtGetNextThread and Toolhelp entries at c2a116a. |
|
Independent build + test: PASS AI-assisted (Claude Code). Windows 11 x64, MSVC 19.44.35220, Ninja RelWithDebInfo. GPU (not used by these tests): NVIDIA GeForce RTX 3080 driver 621.24 / 32.0.16.2124. Head
The last row is expected: the new late-thread check is a guard that both enumeration paths have to satisfy, not a test that only passes with the fix. On this machine the NtGetNextThread path makes the whole suite about 3.6x faster than the Toolhelp path. Code notes (not blocking):
|
Claimed as a new RT item in the coordination thread (#7496 (comment)) and recorded as done with this PR in the current thread (#8047 (comment)). No RT- ID has been assigned yet; the item is "startup TLS thread enumeration".
What and why
Loader::install_on_existing_threadsgives every other running thread its copy of a mapped module's static TLS. To find them it takes aTH32CS_SNAPTHREADsnapshot and callsThread32Nextover every thread in the system, once per mapped TLS module. On Windows 11 with 5,650 threads in 297 processes, a dry run of the retail executable measured:CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD)Thread32First/Thread32NextwalkThe three startup TLS modules (the executable,
XboxBvhBuilder.dll,libHttpClient.GDK.dll) paid that three times.What changes
NtGetNextThread, which walks only this process's threads and returns a handle for each. A one-thread probe costs 0.008 ms.DLL_THREAD_ATTACHfor the later module, so it still has to be found and given that module's block.install_thread_block). That also means a single-thread process is a complete walk, not an empty one.STATUS_NO_MORE_ENTRIES, or an older/stub implementation without it falls back to the existing Toolhelp walk. Current Wine implementsNtGetNextThreadand takes the Nt path. The fallback also closes any handles from the partial ntdll walk.LEONIDA_LOADER_TOOLHELP=1forces that path, andloader_tls_toolhelpruns the same suite through it on CI.The same threads are given the same blocks and the same install/restore path runs, so behavior is unchanged;
~Loaderstill restores every block.Measurements
gta6rt --dry-run <retail executable> --game-dir <own 1.0.2.1 package>, Windows 11,maindde82f27e, clang-cl 23.1.6 + xwin, warm file cache, 12 alternating runs of a before/after binary pair:The instrumented component on the same code path (
main8575d36ed) was 6.0–10.5 ms snapshot + 22.5–31.6 ms walk per module;NtGetNextThreadmeasured 0.008 ms for the one-thread probe. This is startup CPU only; no game data or GPU work is involved.Tests
loader_tls_test <tiny_tls.dll>: 86 checks pass (includes threads that existed beforeload(), thread-pool workers, three modules at once, and a terminated thread). The new case starts a thread between three module loads and checks that it reads all three templates; a per-process cached thread list would fail it.loader_tls_toolhelp(LEONIDA_LOADER_TOOLHELP=1): the same 86 checks pass through the Toolhelp fallback.pe_loader_test <tiny_dll.dll> <tiny_exe.exe>: 79 checks pass without a package; 111 checks pass withLEONIDA_GAME_DIRset to my own 1.0.2.1 copy (optional package integration and unwind tables).loader_test: 88 checks, 0 failed.std::pmr::null_memory_resource()synthetic (candidate vector cannot grow) showed the retained NtGetNextThread handle before the fix and none after it; normalloader_tls_teststill passes 86 checks.tests/test_docs_env_vars.py: 4 tests pass after documentingLEONIDA_LOADER_TOOLHELPinruntime/README.md.Independent Windows checks on
7880e0f24(before the review fixes): 3/3 focused CTests passed, and a fullctest -C RelWithDebInforun passed 111/111. The current headc2a116ab8is locally checked on both enumeration paths; a re-check on the current head is welcome.gta6rt --dry-runon the retail executable completes with the same 29 unimplemented imports.AI-assisted. No game data, logs or traces are included.
Depends on: none in these hunks. #7524 and #5805 also edit
install_on_existing_threads, but not the candidate struct or the enumeration loop this changes; #4313 edits the same file elsewhere. A rebase may still be needed if one lands first. This branch is onmaindde82f27e.