TLS: installing an image leaves other threads' blocks to the loader #426
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/431"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-24T00:11:02Z.
cargo test -p xcl --libsometimes dies with0xC0000005before any test fails (only with several test threads). The crash is intls::install. After loading the carrier DLL, it filled every other thread's new TLS slot itself: a toolhelp snapshot, then each thread's TEB read and its TLS block written. A thread that exits in between has its TEB unmapped, which is the read AV, or its TLS block freed byLdrShutdownThread, which means heap corruption. Nothing outside the loader can hold a thread's TEB or TLS in place, so the walk can't be made safe:STILL_ACTIVEholds while the blocks are freed;The carrier now carries the image's template, so the loader fills every thread's block itself, under its own lock.
What is in it
build_carrier_dll_holding: the carrier's TLS initial data is the image's template (clamped to[16, MAX_CARRIER_TEMPLATE]to preserve #1403's overflow guard), with zeros after it.installuses it andbuild_carrier_dll_withdelegates to it.fill_existing_threadsis removed, and the module docs say who fills the blocks now.on_thread_attachstill fills new threads (now with the same bytes), andtake_slot_zerois untouched.carrier_holds_the_template;a_thread_running_during_install_gets_the_template: a thread started beforeinstallfinds the template in its own block, and nothing in XCL writes into it;install_while_threads_exit: 200 installs while 8 threads keep starting and ending short threads. Its carriers go to one folder,%TEMP%\xcl_tls_test_churn, cleared of earlier runs' carriers at the start (cleanup_cache).Tested
30378b241ece56458ce639e1e3a8bc33ffa03d1aonmain5ad98adbc360225ac8a57ad9661e16fe8df9d8dd:rustc 1.99.0 (b940084d7 2026-09-28), DXC1.9.2609.5, debug + release.cargo build --release -p xcl: clean, 39 warnings (0new vsmain5ad98adbcbaseline of 39 warnings;0warnings incrates/xcl/src/tls.rs).scripts/test.ps1(cargo build --workspace,cargo bench -p xcl --bench '*' -- --test,cargo test --workspace -- --test-threads=4): 3008 passed, 0 failed, 52 ignored across 43 test targets + doc-tests (xcllib:2856 passed, 0 failed, 32 ignored;tls::tests:8 passed, 0 failed, 1 ignoredincluding all 3 new tests andcarrier_size_and_build_clamp_without_overflow;tests/tls_slot_zero.rs:2 passed, 0 failed). SKIPPED GPU tests: 24 (22 no wave64,2 no heap windows).940f7cb(same TLS logic):main6da33fa,cargo test -p xcl --libcrashed in run 2 of 2 ontls::tests::install_without_an_index_variablereadingTEB+0x58intls_block_of←fill_existing_threads←install; with--test-threads=1it passed 6 of 6.install_while_threads_exit, debugger, CPU only):maind4f17cawith the test crashed 19 of 20 runs at the same read; this change passed 30 of 30.install_without_an_index_variable,windows_loads_carrier_and_allocates_full_block_for_all_threads, anda_thread_running_during_install_gets_the_template.windows-gnu, CPU only):tls::andpe::pass, and both new thread tests pass 10 of 10.mainvs 3/3 pass with this PR) and @srcamarda (RX 7800 XT, 90 s smoke pair clean).Regression run
Release builds on RTX 5070 Ti (
main5ad98adbcvs rebased head30378b241), no uncommitted changes.main:
PR:
Not tested
30378b241, native wave64 GPU tests (22skipped on RTX 5070 Ti), and opt-inXCL_HEAP_WINDOWS=1GPU tests (2skipped).XCL_SAMPLE_*unset).take_slot_zerois not changed here.AI use
main5ad98adbc(preserving #1403'sMAX_CARRIER_TEMPLATEclamp inbuild_carrier_dll_holding), ran the release build comparison,scripts/test.ps1, 60 s smoke pair, andscripts/pr_check.py --local, and updated this description.Imported from ivall/xcl. Originally posted by MesshM on 2026-11-24T00:59:57Z.
Test run of
526a17661f22431342f4a89c4f8925eb68fedd18on AMD (RDNA4); the PR was tested on NVIDIA only. The branch is based on9ab7d76, so I merged it onto currentmaina6f5f7alocally (clean merge, onlytls.rs) and tested that tree.Tested
stable-x86_64-pc-windows-msvc, debug.\scripts\test.ps1(cargo build --workspace+cargo test --workspace -- --test-threads=4) on the merge: 679 passed, 0 failed, 41 ignored, exit code 0;xcllib tests 639 passed, 0 failed, 21 ignored (mainon this machine: 636 / 676, the difference is the 3 new tests)SKIPPED; alltls::testspass, includinga_thread_running_during_install_gets_the_templateandinstall_while_threads_exitinstall_while_threads_exitcopied unchanged intomain'stls.rs(nothing else changed),cargo test -p xcl --lib tls::tests::install_while_threads_exit, 3 runs: 3 of 3 crashed with0xc0000005, STATUS_ACCESS_VIOLATIONSo on this 16-thread machine the race reproduces every time, not 1 run in 3.
Review of the change:
installruns in step 5 ofloader::map, after relocations (step 3), so the template the carrier now carries is the relocated one, the same bytesfill_blockused to copy; pasttemplate_lenthe block is zeros, as before.on_thread_attachstill fills new threads from the image's live template, so that path is unchanged. I found nothing to change.Not tested: no retail title or regression run (I have no unpacked game), no
XCL_SAMPLE_*tests, release build; the PR's head on its old base was not run, only its merge ontomain; the overlap with #196 was not checked.Run and report by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the code.
Imported from ivall/xcl. Originally posted by QemmHD on 2026-11-24T01:18:15Z.
NVIDIA run (only an AMD run so far) plus a short review.
Tested
526a17661merged onto maina6f5f7a74on Windows 11 Home 26200, RTX 4070 SUPER (driver 621.24, wave32), Rust 1.98, DXC 1.8.2502.11: pass.cargo test --workspace -- --test-threads=4: 679 passed, 0 failed, 41 ignored (main: 676 / 0 / 41). SKIPPED: only the 2no wave64tests.carrier_holds_the_template,a_thread_running_during_install_gets_the_templateandinstall_while_threads_exitran and passed; no TLS crash.Review – letting the loader seed every block from the carrier's template looks right; the template bytes sit outside the carrier's four DIR64 relocations (
tls.rs:163-165), and thetemplate_len == 0branch avoidsfrom_raw_parts(null).tls.rs:11-15(module docs) still say existing threads are filled "right after the carrier is loaded (via their TEB)". The loader now does that from the template; could the paragraph be updated?tls.rs:601-614: each run writes 200 carrier DLLs to%TEMP%\xcl_tls_test_churn_<pid>\and loads them into the shared--libprocess, so they can't be deleted and pile up across runs. Fewer installs, or a fixed directory cleared at the start, would avoid that.AI-assisted review (Claude Code, Claude Opus 5.5).
Discord: bigpeppa.
Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-24T02:02:27Z.
Thanks, @QemmHD. Both points were right, and both are fixed in
da285ce(squashed into the PR's commit).tls.rs:7-16) now say what the code does: the carrier's template is the game's, and ntdll starts every thread's block from it, both the threads running when the carrier loads and later ones.on_thread_attachcopies it again for new threads.install_while_threads_exitnow uses one folder,%TEMP%\xcl_tls_test_churn. It clears earlier runs' carriers at the start with the existingcleanup_cache, and the file names keep the process id, so runs at the same time don't clash. After each of 3 runs here the folder held 200 files, all from that run.🤖 Claude Opus 5.5
Imported from ivall/xcl. Originally posted by srcamarda on 2026-11-24T02:16:52Z.
Regression run on a retail title, as asked under Regression run.
Tested
scripts/smoke.ps1 -Seconds 90, no input (the title stays on its account screen at 30 fps). Each binary was built from a clean checkout of its commit and run twice; the first run of each has a cold translation cache. Blocks below are from the second run.main508a0c7: 2343 / 2361 frames, peak 15764 / 15674 MB. This PR (da285ce): 2322 / 2359 frames, peak 16486 / 16414 MB. No crash or device removal in any run.TLS: game_scarlett_master.exe: template 5248 B, block 5248 B, index 18, threads initialized: 3(alsoxboxbvhbuilder.dllandlibhttpclient.gdk.dll), and the title then runs normally.9ab7d76, 60 commits behindmain. Its peak commit is ~0.7 GB abovemain's here, most likely from the base:main's memory changes since then aren't in it. #443 and #470, both close tomain, matchmain. A rebase would settle it.Regression run
main:this PR's head:
gpu:in this PR's block names a virtual display adapter: the head predates97dc49c, which reads the adapter fromreport.json. XCL ran on the RX 7800 XT in every run.Not tested
AI use
Imported from ivall/xcl. Originally posted by raylitebull on 2026-11-24T10:40:33Z.
Code/CI review only (not independently runtime-tested by me): clean merge with both checks green (
Build and test+PR description). Cleanly fixes the intermittenttls::installcrash by leaving other threads' TLS blocks to the loader. Recommend merging, @ivall.Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-25T14:29:03Z.
Verification on RTX 3060 Ti (Windows 11), branch merged with current
main:cargo test -p xcl --lib: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.)Thanks!
Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-11-29T20:42:01Z.
Nothing has happened on this PR for 3 days, so it is marked
staleand will be closed in 4 days. A push or a comment takes the mark off: rebase it ontomainif it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T07:40:05Z.
merge-check
base:
7d86c5d2b7head:
940f7cb734merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2899 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read the whole diff. Still needed: main still fills existing threads by walking them (
fill_existing_threads, tls.rs ~553-602, called frominstall~653), and the only TLS change since this base (39ea43e9) is intake_slot_zero. Findings: (1)crates/xcl/tests/tls_slot_zero.rs, added after this PR, callsinstallwith 16 parked threads and asserts their slot-0 template (~122, ~176); with this change those blocks come from ntdll instead of the walk, so that test was the one to watch: both its tests (take_slot_zero_survives_concurrent_thread_exit_and_termination,reused_teb_after_terminate_thread_still_exchanges_slots) pass in the run above. (2)a_thread_running_during_install_gets_the_templatecan also pass without the fix if the thread's attach runs afterLoadLibrary(on_thread_attach then fills it), so it's weaker than described, though it won't fail by mistake. (3)install_while_threads_exitleaves 200 carriers loaded for the rest of the lib test process. Fine: the carrier layout change (raw size of.data, template copied atOFF_TEMPLATEafter the index sentinel),build_carrier_dll_withkept as a wrapper, the PE accessors the new test uses still exist on main, and nothing outside tls.rs reads the removed log line. #196 overlaps, as the author notes.performance: 60 s smoke of a retail title, main
7d86c5d2vs this merge (d83eb0b4): 1663 -> 1670 frames shown (100 %; last presents per 2 s: 60 60 60), peak commit 17153 -> 17818 MB, 0 device removals, 0 crashes.verdict: safe to merge (the new carrier test can also pass without the fix if the thread's attach runs after LoadLibrary, so it's weaker than described)
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5
AI-written and posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by Decodere on 2026-12-02T09:55:26Z.
merge-check
base:
1c8b551475head:
940f7cb734merge: clean (trial-merge commit 065990dd2a2baf7ffa6b2f29be4e09b91bb73fbe, git diff --check clean)
build: cargo build --workspace ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 --nocapture (43 targets): 2912 passed, 0 failed, 51 ignored (+3 new tests vs main baseline). Targeted tls::tests passed 7/7 (1 pre-existing ignored carrier_variants_probe), including all 3 new tests (carrier_holds_the_template, a_thread_running_during_install_gets_the_template, install_while_threads_exit) with 0 skips, and tests/tls_slot_zero.rs passed 2/2. 28 SKIPPED emissions (host environment only: 22 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 2 no heap windows, 1 cannot disable the driver cache (Windows Developer Mode required)).
review: Read the full merged diff (+88/-69 in crates/xcl/src/tls.rs). Embeds the image's TLS template bytes directly into the synthesized carrier DLL's
.datasection viabuild_carrier_dll_holding(sizingSizeOfRawDataandSizeOfInitializedDatatodata_raw = (OFF_TEMPLATE + template.len()).next_multiple_of(FILE_ALIGN)while keeping virtual.datasize atOFF_TEMPLATE + template_size), sontdllinitializes every existing and future thread's TLS block under the loader lock duringLoadLibraryW. Removes the racyfill_existing_threadsToolhelp/TEB walk that dereferenced TEBs/TLS blocks of concurrently exiting threads without loader synchronization, while preservingon_thread_attachandtemplate_len == 0handling. The three new unit tests verify carrier PE template layout, live-thread initialization on install, and 200 installs under 8-thread churn. No scope creep or UB.performance: not applicable (Tier 1 — PE loader / TLS carrier initialization only, runs once per loaded image; no hot runtime or GPU path changed).
verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 4060 8 GB (driver 621.40), Intel Core i5-11400F, 32 GB RAM, Windows 11 Build 26200, Rust 1.100.1 MSVC, DXC 1.9.2602.17 / Antigravity (Gemini 4 Neon)
Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-02T20:19:58Z.
Rebased onto
main(5ad98adbc) as30378b241to resolve thecrates/xcl/src/tls.rsconflict with #1403 (ce99e53ac,MAX_CARRIER_TEMPLATEclamp), preserving the[16, MAX_CARRIER_TEMPLATE]clamp insidebuild_carrier_dll_holdingand keeping the TLS behavior and tests unchanged.Still needed:
mainstill walks existing threads' TEBs infill_existing_threads, which races with thread exit intls::install.Verification at
30378b241(RTX 5070 Ti, driver 610.74,rustc 1.99.0, DXC1.9.2609.5):cargo build --release -p xcl: clean, 39 warnings (0 new vsmain5ad98adbc).scripts/test.ps1: 3008 passed, 0 failed, 52 ignored, 24 SKIPPED GPU tests (22 no wave64, 2 no heap windows);tls::tests8/8 passed (1 ignored) andtests/tls_slot_zero.rs2/2 passed.main5ad98adbcbaseline: 1460 frames, peak commit 15067 MB).scripts/pr_check.py --local: clean (description ok).Updated the PR description with the new head SHA and fresh
xcl-smokeblocks. Ready for re-check on30378b241.Imported from ivall/xcl. Originally posted by fromthemoonx on 2026-12-03T09:44:10Z.
merge-check
base:
16c2fd082ahead:
30378b241emerge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 3015 passed, 0 failed, 56 ignored in runs 2 and 3 (base: 3012 passed, 0 failed, 56 ignored, counted on
395405e4; only docs and scripts changed since); SKIPPED: 25, the same as base (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps); failing: none in runs 2 and 3. In run 1 the lib binary ended with 0xc0000005 during a thread dump in the GPU tests, before anytls::test had run. That is the pattern of #3586, which main has too (#3743 fixes it). After that I ran the whole lib binary in alternation: main 4 of 4 and this PR 3 of 3 without a crash.review: read the whole diff (1 file, +89 -69). No blocking finding.
fill_existing_threadsreads a TEB and writes a TLS block that only an open thread handle "protects", and a handle keeps neither alive. The PR removes the walk and lets the loader fill the blocks.install_while_threads_exitpassed 20 of 20 runs, and 10 of 10 with a 1 ms sleep between the TEB query and its use. In that widened build the walk reached exactly 10 threads in every install, so on this machine (Windows 11 26300) the short threads were probably gone before it could open them. So my runs do not show the crash being fixed; that evidence is the author's and MesshM's.a_thread_running_during_install_gets_the_template,install_without_an_index_variableandwindows_loads_carrier_and_allocates_full_block_for_all_threadsfail. As submitted,install_while_threads_exitpassed 30 of 30 andtls::tests30 of 30 at 8 threads. I agree with Foniass above that the first of these should wait until its thread is running: it caught the empty template in my one run, but it depends on timing.MAX_CARRIER_TEMPLATEbefore the size is computed, so the copy cannot run past the file buffer.performance: an install no longer takes a system-wide thread snapshot and opens each thread. The churn test's 200 installs took 11 s on main in one run, and 30 runs of it took 22 s with this PR. This happens at image load, not per frame.
verdict: safe to merge
hardware / tool and model: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300 / Claude Code (Claude Opus 5.5)
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-03T10:20:02Z.
merge-check
base:
16c2fd082ahead:
30378b241emerge: clean
build: ok
tests: 3015 passed, 0 failed, 56 ignored; 28 skipped, the same as on main
review: 1 medium, 2 nits (below)
performance: 60 s smoke run: 1683 → 1655 frames (98 % of main), peak commit 18505 → 17668 MB, no device removal, no crash
title run: retail title on the RTX 5070 Ti (driver 621.14), into the world by scripted presses: 228 s there, 0 device removals, 0 crashes, 3.9 presents/s; run with
XCL_SKIP_CS=95cc13288a27a96e(the NVIDIA hang at the end of loading, #3541) and NVIDIA's 2048 MB shader heap (#2590)verdict: safe to merge: no blocking finding (the points below are medium or nits), the suite passes and the smoke run is clean
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5
In short: Lets ntdll fill every thread's TLS block from a carrier DLL that now carries the image's template, and drops the racy walk over other threads' TEBs in install. Since my last merge-check the only change is the rebase onto #1403, which keeps its template clamp; the TLS logic and tests are the same. Nothing blocks.
Medium
Nit
Checked and fine
How this was tested
16c2fd08, thencargo build --workspaceandcargo test --workspace --no-fail-fast -- --test-threads=4.scripts/smoke.ps1: 60 s of a retail title on main16c2fd08and on this merge; presents per 2 s at the end: 60 60 60.XCL_RUN_SECONDS=300and scripted A presses counted from the first frame (XCL_PRESS_BASE=frame, #3712) into the world from an existing save; time in the world is counted from the first in-world draw call to the end of the run.Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.