TLS: installing an image leaves other threads' blocks to the loader #431
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!431
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-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?
@Kalmarv on GitHub · 2026-11-24 00:11 UTC · original
Kalmarv:fix/tls-fill-live-threads→main· +88 −69 · 1 files · 1 commits · opencargo 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, with zeros after it.installuses 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).49d28fcconflicts in three places, where #196's aligned template offset meets the template copy. Resolved that way, all 11 tls tests pass, including #196's alignment-on-all-threads test. I can rebase either way.Tested
cargo test --workspace -- --test-threads=4on940f7cb(this PR, on main9ce2070): 687 passed, 0 failed, 41 ignored (thexcllibrary: 647 passed, 21 ignored). SKIPPED GPU tests: 2, bothno wave64(gpu_geometry_wave32_prefix_count_over_a_mask,gpu_wave32_program_in_wave64_hardware).6da33fa,cargo test -p xcl --lib(default threads) ran under a debugger that passes first-chance exceptions on and catches the second-chance one with its thread and stack. It crashed in run 2 of 2, on threadtls::tests::install_without_an_index_variable: a read ofTEB+0x58intls_block_of←fill_existing_threads←install. With--test-threads=1it passed 6 of 6.--exact, under the same debugger, CPU only):d4f17cawith the test added: 19 of 20 runs crashed, every one at the same read;cargo test -p xcl --lib tls::on940f7cb, 3 runs: 7 passed, 1 ignored each. After each run,%TEMP%\xcl_tls_test_churnheld 200 carriers, all from that run.install_without_an_index_variable,windows_loads_carrier_and_allocates_full_block_for_all_threadsanda_thread_running_during_install_gets_the_template.cargo test -p xcl --lib, 10 runs each, interleaved, maind4f17caand this change:xmem_placeholder_split_replace_and_coalesce,console_alloc_flags_are_stripped).tls::andpe::pass. The two new thread tests pass 10 of 10, so Wine's loader also fills running threads from the carrier's template. Main with the churn test also passes 10 of 10 there, so the race doesn't reproduce under Wine.526a176merged ontomaina6f5f7a.scripts/test.ps1: 679 passed, 0 failed, 41 ignored, no SKIPPED GPU tests.install_while_threads_exitonmain: 3 of 3 crashed (access violation); with this change 3 of 3 passed.scripts/smoke.ps1 -Seconds 90,main508a0c7againstda285ce(this PR before the rebase onto9ce2070), each run twice and the second run kept: 2361 and 2359 frames, peak commit 15674 and 16414 MB, no device removal or crash in any run. Their PR block namesgpu: SudoMaker Virtual Display Adapter:da285cehassmoke.ps1from before97dc49c, which reads the adapter fromreport.json; XCL ran on the RX 7800 XT. They put the higher peak commit down to the old base, which the rebase replaces. Theirmainlog showsinstallfor three images with other threads alive (TLS: <executable>: … threads initialized: 3, the linemain'sinstallwrites).Regression run
Our own runs, one after the other on the machine under Tested (RTX 5070 Ti), release builds, no uncommitted changes.
main:
PR:
Not tested
loader::tls_install) with a real image's TLS: not checked. The only test that reaches it needs real console DLLs.take_slot_zerowalks the threads' TEBs the same way, once, for a title's executable. It isn't changed here.AI use
🤖 Generated with Claude Code
@MesshM on GitHub · 2026-11-24 00:59 UTC · original
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.
@QemmHD on GitHub · 2026-11-24 01:18 UTC · original
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.
@Kalmarv on GitHub · 2026-11-24 02:02 UTC · original
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
@srcamarda on GitHub · 2026-11-24 02:16 UTC · original
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
@raylitebull on GitHub · 2026-11-24 10:40 UTC · original
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.@DavidM1911 on GitHub · 2026-11-25 14:29 UTC · original
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!
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.