Skip to content

TLS: installing an image leaves other threads' blocks to the loader - #431

Open
Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/tls-fill-live-threads
Open

Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/tls-fill-live-threads

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

cargo test -p xcl --lib sometimes dies with 0xC0000005 before any test fails (only with several test threads). The crash is in tls::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 by LdrShutdownThread, 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:

  • a handle keeps only the thread object;
  • STILL_ACTIVE holds while the blocks are freed;
  • suspending doesn't stop a thread already exiting in the kernel.

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

Tested

  • RTX 5070 Ti, NVIDIA 610.74, Windows 11 Pro 26200; MSVC stable, debug.
  • cargo test --workspace -- --test-threads=4 on 940f7cb (this PR, on main 9ce2070): 687 passed, 0 failed, 41 ignored (the xcl library: 647 passed, 21 ignored). SKIPPED GPU tests: 2, both no wave64 (gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware).
  • The crash, named. On main 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 thread tls::tests::install_without_an_index_variable: a read of TEB+0x58 in tls_block_of ← fill_existing_threads ← install. With --test-threads=1 it passed 6 of 6.
  • The churn test (--exact, under the same debugger, CPU only):
    • main d4f17ca with the test added: 19 of 20 runs crashed, every one at the same read;
    • this change: 30 of 30 passed.
  • cargo test -p xcl --lib tls:: on 940f7cb, 3 runs: 7 passed, 1 ignored each. After each run, %TEMP%\xcl_tls_test_churn held 200 carriers, all from that run.
  • With the mutation (the carrier without the template, and no walk), three tests fail: install_without_an_index_variable, windows_loads_carrier_and_allocates_full_block_for_all_threads and a_thread_running_during_install_gets_the_template.
  • cargo test -p xcl --lib, 10 runs each, interleaved, main d4f17ca and this change:
    • neither side hit the TLS crash: it's too rare in the suite to show in 10 runs;
    • each side had 2 crashes from a different, unrelated race: a write to the same GPU-visible address in two kernelx tests (xmem_placeholder_split_replace_and_coalesce, console_alloc_flags_are_stripped).
    • The other runs: main 605 passed, 21 ignored; this change 608 passed, 20 ignored (it adds three tests).
  • Wine 11.21 Staging, windows-gnu, CPU only (run by a second agent on Ubuntu): tls:: and pe:: 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.
  • Independent run by @MesshM (TLS: installing an image leaves other threads' blocks to the loader #431 (comment)): AMD Radeon RX 9060 XT (RDNA4, driver 32.0.32029.2010), Ryzen 7 5700X (16 threads), Windows 11 26200, Rust 1.100.0, debug, 526a176 merged onto main a6f5f7a. scripts/test.ps1: 679 passed, 0 failed, 41 ignored, no SKIPPED GPU tests. install_while_threads_exit on main: 3 of 3 crashed (access violation); with this change 3 of 3 passed.
  • Regression run on a retail title by @srcamarda (TLS: installing an image leaves other threads' blocks to the loader #431 (comment)): AMD Radeon RX 7800 XT (RDNA3, driver 32.0.32029.2010), Ryzen 5 5600G, Windows 11 Pro 26300, release builds, scripts/smoke.ps1 -Seconds 90, main 508a0c7 against da285ce (this PR before the rebase onto 9ce2070), 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 names gpu: SudoMaker Virtual Display Adapter: da285ce has smoke.ps1 from before 97dc49c, which reads the adapter from report.json; XCL ran on the RX 7800 XT. They put the higher peak commit down to the old base, which the rebase replaces. Their main log shows install for three images with other threads alive (TLS: <executable>: … threads initialized: 3, the line main's install writes).

Regression run

Our own runs, one after the other on the machine under Tested (RTX 5070 Ti), release builds, no uncommitted changes.

main:

xcl-smoke v1
commit: 9ce2070eccf436d7bd38601c90407da447df34f3
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1588 (PresentX 1588)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14423 MB

PR:

xcl-smoke v1
commit: 940f7cb7341c80a4bf493163962d1c0c2a0ee506
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1562 (PresentX 1562)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14557 MB

Not tested

  • Whether the smoke runs above reach the loader's own call (loader::tls_install) with a real image's TLS: not checked. The only test that reaches it needs real console DLLs.
  • take_slot_zero walks the threads' TEBs the same way, once, for a title's executable. It isn't changed here.
  • macOS / Linux: not built.

AI use

  • Claude Code, Claude Opus 5.5: found the crash (debugger capture of the suite), wrote the change, the tests and this description; the runs above are its own, except the Wine run, which another of our agents made on Ubuntu.

🤖 Generated with Claude Code

@MesshM

MesshM commented Nov 24, 2026

Copy link
Copy Markdown

Test run of 526a17661f22431342f4a89c4f8925eb68fedd18 on AMD (RDNA4); the PR was tested on NVIDIA only. The branch is based on 9ab7d76, so I merged it onto current main a6f5f7a locally (clean merge, only tls.rs) and tested that tree.

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.32029.2010; Ryzen 7 5700X (8C/16T), 64 GB RAM, Windows 11 Pro build 26200, Rust 1.100.0 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; xcl lib tests 639 passed, 0 failed, 21 ignored (main on this machine: 636 / 676, the difference is the 3 new tests)
  • 0 SKIPPED; all tls::tests pass, including a_thread_running_during_install_gets_the_template and install_while_threads_exit
  • install_while_threads_exit copied unchanged into main's tls.rs (nothing else changed), cargo test -p xcl --lib tls::tests::install_while_threads_exit, 3 runs: 3 of 3 crashed with 0xc0000005, STATUS_ACCESS_VIOLATION
  • the same test on the merge, 3 runs: 3 of 3 passed (1.95–2.04 s)

So on this 16-thread machine the race reproduces every time, not 1 run in 3.

Review of the change: install runs in step 5 of loader::map, after relocations (step 3), so the template the carrier now carries is the relocated one, the same bytes fill_block used to copy; past template_len the block is zeros, as before. on_thread_attach still 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 onto main; 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

QemmHD commented Nov 24, 2026

Copy link
Copy Markdown

NVIDIA run (only an AMD run so far) plus a short review.

Tested 526a17661 merged onto main a6f5f7a74 on 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 2 no wave64 tests.
  • carrier_holds_the_template, a_thread_running_during_install_gets_the_template and install_while_threads_exit ran and passed; no TLS crash.
  • Not run: smoke.ps1 (no retail title on this machine), Wine.

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 the template_len == 0 branch avoids from_raw_parts(null).

  1. 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?
  2. Minor, tls.rs:601-614: each run writes 200 carrier DLLs to %TEMP%\xcl_tls_test_churn_<pid>\ and loads them into the shared --lib process, 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
Kalmarv force-pushed the fix/tls-fill-live-threads branch from 526a176 to da285ce Compare November 24, 2026 01:49
@Kalmarv

Kalmarv commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, @QemmHD. Both points were right, and both are fixed in da285ce (squashed into the PR's commit).

  1. The module docs (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_attach copies it again for new threads.
  2. install_while_threads_exit now uses one folder, %TEMP%\xcl_tls_test_churn. It clears earlier runs' carriers at the start with the existing cleanup_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

Copy link
Copy Markdown
Contributor

Regression run on a retail title, as asked under Regression run.

Tested

  • AMD Radeon RX 7800 XT (RDNA3), driver 32.0.32029.2010; Ryzen 5 5600G, 32 GB; Windows 11 Pro 26300; release builds
  • 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.
  • main 508a0c7: 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.
  • The changed path runs in this title: its log shows a TLS install for three images with other threads alive, e.g. TLS: game_scarlett_master.exe: template 5248 B, block 5248 B, index 18, threads initialized: 3 (also xboxbvhbuilder.dll and libhttpclient.gdk.dll), and the title then runs normally.
  • This head is based on 9ab7d76, 60 commits behind main. Its peak commit is ~0.7 GB above main's here, most likely from the base: main's memory changes since then aren't in it. D3D12.X: the sparse state is replaced outside its lock #443 and D3D12.X: map a one-slice title texture's packed mips where D3D12 addresses them #470, both close to main, match main. A rebase would settle it.

Regression run

main:

xcl-smoke v1
commit: 508a0c74d7c0625d6d2de8d24494e69dc2e1926f
gpu: AMD Radeon RX 7800 XT
title: RetailGame_Dump
seconds: 90
exit: 0x0
frames shown: 2361 (PresentX 2361)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15674 MB

this PR's head:

xcl-smoke v1
commit: da285ce79cbfc485e2a284da694c0d6dd41801b5
gpu: SudoMaker Virtual Display Adapter
title: RetailGame_Dump
seconds: 90
exit: 0x0
frames shown: 2359 (PresentX 2360)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16414 MB

gpu: in this PR's block names a virtual display adapter: the head predates 97dc49c, which reads the adapter from report.json. XCL ran on the RX 7800 XT in every run.

Not tested

  • Anything past the account screen.
  • The whole test suite at this head (only the title runs).

AI use

  • Claude Code with Claude Opus 5.5 built both commits, ran the smoke runs and wrote this report; the runs are from my machine.

tls::install filled the new slot of every other thread itself: a
toolhelp snapshot, then each thread's TEB and TLS block read and
written. A thread that exited in between had its TEB or block freed
under the access: the access violation that ends cargo test -p xcl --lib
now and then (tls_block_of <- fill_existing_threads <- install, from
install_without_an_index_variable).

The carrier DLL now holds the image's template, so the loader starts
every thread's block as it - the threads running at LoadLibrary under
its own lock, later ones as they start - and the walk goes.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@raylitebull

Copy link
Copy Markdown
Member

Code/CI review only (not independently runtime-tested by me): clean merge with both checks green (Build and test + PR description). Cleanly fixes the intermittent tls::install crash by leaving other threads' TLS blocks to the loader. Recommend merging, @ivall.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants