TLS: harden take_slot_zero against TEB UAF and stale MOVED entries #1083

Open
github-bot wants to merge 1 commit from github-bot/gh-1083 into main AGit
Owner

@⁠AlpPesen on GitHub · 2026-11-24 11:24 UTC · original
AlpPesen:fix/tls-take-slot-zero-safety → main · +433 −30 · 2 files · 1 commits · open

Hardens tls::take_slot_zero and MOVED tracking against two concurrency bugs from #106:

  1. TEB / ThreadLocalStoragePointer UAF during take_slot_zero: previously thread_tebs() closed every thread handle immediately after reading ThreadBasicInformation, then take_slot_zero dereferenced the raw TEBs and wrote *arr = block after Sleep(20) without verifying the thread was still alive. Publishing XCL_SLOT only after the snapshot also left a window where concurrently starting or exiting threads bypassed MOVED serialization.
  2. Stale MOVED entries after TerminateThread: MOVED stored only raw TEB addresses (Vec<usize>). A thread killed without DLL_THREAD_DETACH left its TEB address in MOVED permanently, causing exchange_for_new_thread to skip swapping slot 0 and slot k when Windows later recycled that TEB address for a new thread.

Part of #106. (Disjoint from open TLS PRs #196, #431, and #464: only touches MOVED, take_slot_zero, exchange_for_new_thread, and on_thread_detach, and places new tests in a separate integration test file.)

What is in it

  • crates/xcl/src/tls.rs:
    • Replace MOVED: Mutex<Vec<usize>> with MovedTeb { teb, tid, handle: ThreadHandle } (Drop closes the handle). Keeping the handle open prevents Windows from reusing tid while tracked and lets exchange_for_new_thread and on_thread_detach prune threads that died without DLL_THREAD_DETACH via WaitForSingleObject(handle, 0) == WAIT_TIMEOUT.
    • Publish XCL_SLOT.store(k, Ordering::SeqCst) under MOVED.lock() before snapshotting peer threads so concurrent attach/detach serializes on MOVED.
    • Keep peer thread handles open (THREAD_QUERY_INFORMATION | THREAD_SUSPEND_RESUME | THREAD_SYNCHRONIZE) across both phases of take_slot_zero, suspending each peer individually with a RAII SuspendGuard across only raw syscalls (WaitForSingleObject, NtQueryInformationThread, ReadProcessMemory) and pointer updates (no heap allocations or locks while suspended). Skip threads mid-LdrpAllocateTls (arr == null, arr[0] == 0, or arr[k] == 0) so their upcoming on_thread_attach performs the swap cleanly.
    • Handle the current thread directly without adding it to MOVED.
  • crates/xcl/tests/tls_slot_zero.rs:
    • Process-isolated integration tests with a .CRT$XLY callback forwarding real DLL_THREAD_ATTACH / DLL_THREAD_DETACH events:
      • reused_teb_after_terminate_thread_still_exchanges_slots — verified to fail deterministically on unmodified main (47efa2e) on the first recycled TEB (host TLS corrupted on new thread), and passes with this fix.
      • take_slot_zero_survives_concurrent_thread_exit_and_termination — exercises take_slot_zero under concurrent thread spawn/exit churn and TerminateThread, validating both survivor TLS integrity and HeapValidate.

Regression run

Baseline (main, warm shader/PSO cache):

xcl-smoke v1
commit: 09940bb87fa11bc394fd9e5de570c2b7cfd16323
gpu: AMD Radeon RX 6800
title: YouKnowWho
seconds: 60
exit: 0x0
frames shown: 405 (PresentX 405)
presents per 2 s (last 3): 11 12 11
device removals: 0
crashes: 0
peak commit: 15870 MB

This PR (fix/tls-take-slot-zero-safety, warm shader/PSO cache):

xcl-smoke v1
commit: 39ea43e9d95d706aaa6ec679c7544d63c822a566
gpu: AMD Radeon RX 6800
title: YouKnowWho
seconds: 60
exit: 0x0
frames shown: 489 (PresentX 489)
presents per 2 s (last 3): 17 16 17
device removals: 0
crashes: 0
peak commit: 15868 MB

Tested

Hardware: Windows 11 Pro for Workstations (Build 26300), Intel Core i9-9900K, 32 GB RAM, AMD Radeon RX 6800 (16337 MiB VRAM, Driver 32.0.21045.29003), rustc 1.100.0 (0ef1bc386 2026-11-09).

  • cargo test -p xcl --test tls_slot_zero (5 consecutive runs): 2 passed; 0 failed; 0 ignored; 0 skipped each run (on unmodified 47efa2e, reused_teb_after_terminate_thread_still_exchanges_slots failed with host TLS was corrupted on new thread (iter=234, reused=1)).
  • cargo test -p xcl --lib tls::: 4 passed; 0 failed; 1 ignored; 0 skipped.
  • cargo test -p xcl --test e2e: 3 passed; 0 failed; 0 ignored; 0 skipped.
  • XCL_REQUIRE_GPU=wave64,mesh cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4: 740 passed; 3 failed; 41 ignored; 0 skipped. All 3 failures are the pre-existing RDNA2 placed_title_textures_share_vram_and_preserve_pixels + 2 VRAM_TEST_LOCK PoisonError cascades on unmodified main (tracked in #470).
  • .\scripts\smoke.ps1 -Game '<game folder>\YouKnowWho' on 39ea43e9d95d706aaa6ec679c7544d63c822a566 (see Regression run above).

Not tested

  • MinGW / gnu target builds (MSVC toolchain only).
  • Non-AMD GPUs and Linux / Wine.

AI use

Antigravity (Gemini 4 Neon) audited crates/xcl/src/tls.rs, wrote the fix and process-isolated regression tests, and ran the local test suite and smoke runs.

**@⁠AlpPesen** on GitHub · 2026-11-24 11:24 UTC · [original](https://github.com/xcl-dev/xcl/pull/1083) `AlpPesen:fix/tls-take-slot-zero-safety` → `main` · +433 −30 · 2 files · 1 commits · open Hardens `tls::take_slot_zero` and `MOVED` tracking against two concurrency bugs from #106: 1. **TEB / `ThreadLocalStoragePointer` UAF during `take_slot_zero`**: previously `thread_tebs()` closed every thread handle immediately after reading `ThreadBasicInformation`, then `take_slot_zero` dereferenced the raw TEBs and wrote `*arr = block` after `Sleep(20)` without verifying the thread was still alive. Publishing `XCL_SLOT` only after the snapshot also left a window where concurrently starting or exiting threads bypassed `MOVED` serialization. 2. **Stale `MOVED` entries after `TerminateThread`**: `MOVED` stored only raw TEB addresses (`Vec<usize>`). A thread killed without `DLL_THREAD_DETACH` left its TEB address in `MOVED` permanently, causing `exchange_for_new_thread` to skip swapping slot `0` and slot `k` when Windows later recycled that TEB address for a new thread. Part of #106. (Disjoint from open TLS PRs #196, #431, and #464: only touches `MOVED`, `take_slot_zero`, `exchange_for_new_thread`, and `on_thread_detach`, and places new tests in a separate integration test file.) **What is in it** - **`crates/xcl/src/tls.rs`**: - Replace `MOVED: Mutex<Vec<usize>>` with `MovedTeb { teb, tid, handle: ThreadHandle }` (`Drop` closes the handle). Keeping the handle open prevents Windows from reusing `tid` while tracked and lets `exchange_for_new_thread` and `on_thread_detach` prune threads that died without `DLL_THREAD_DETACH` via `WaitForSingleObject(handle, 0) == WAIT_TIMEOUT`. - Publish `XCL_SLOT.store(k, Ordering::SeqCst)` under `MOVED.lock()` before snapshotting peer threads so concurrent attach/detach serializes on `MOVED`. - Keep peer thread handles open (`THREAD_QUERY_INFORMATION | THREAD_SUSPEND_RESUME | THREAD_SYNCHRONIZE`) across both phases of `take_slot_zero`, suspending each peer individually with a RAII `SuspendGuard` across only raw syscalls (`WaitForSingleObject`, `NtQueryInformationThread`, `ReadProcessMemory`) and pointer updates (no heap allocations or locks while suspended). Skip threads mid-`LdrpAllocateTls` (`arr == null`, `arr[0] == 0`, or `arr[k] == 0`) so their upcoming `on_thread_attach` performs the swap cleanly. - Handle the current thread directly without adding it to `MOVED`. - **`crates/xcl/tests/tls_slot_zero.rs`**: - Process-isolated integration tests with a `.CRT$XLY` callback forwarding real `DLL_THREAD_ATTACH` / `DLL_THREAD_DETACH` events: - `reused_teb_after_terminate_thread_still_exchanges_slots` — verified to fail deterministically on unmodified `main` (`47efa2e`) on the first recycled TEB (`host TLS corrupted on new thread`), and passes with this fix. - `take_slot_zero_survives_concurrent_thread_exit_and_termination` — exercises `take_slot_zero` under concurrent thread spawn/exit churn and `TerminateThread`, validating both survivor TLS integrity and `HeapValidate`. **Regression run** Baseline (`main`, warm shader/PSO cache): ``` xcl-smoke v1 commit: 09940bb87fa11bc394fd9e5de570c2b7cfd16323 gpu: AMD Radeon RX 6800 title: YouKnowWho seconds: 60 exit: 0x0 frames shown: 405 (PresentX 405) presents per 2 s (last 3): 11 12 11 device removals: 0 crashes: 0 peak commit: 15870 MB ``` This PR (`fix/tls-take-slot-zero-safety`, warm shader/PSO cache): ``` xcl-smoke v1 commit: 39ea43e9d95d706aaa6ec679c7544d63c822a566 gpu: AMD Radeon RX 6800 title: YouKnowWho seconds: 60 exit: 0x0 frames shown: 489 (PresentX 489) presents per 2 s (last 3): 17 16 17 device removals: 0 crashes: 0 peak commit: 15868 MB ``` **Tested** Hardware: Windows 11 Pro for Workstations (Build 26300), Intel Core i9-9900K, 32 GB RAM, AMD Radeon RX 6800 (16337 MiB VRAM, Driver `32.0.21045.29003`), `rustc 1.100.0 (0ef1bc386 2026-11-09)`. - `cargo test -p xcl --test tls_slot_zero` (5 consecutive runs): `2 passed; 0 failed; 0 ignored; 0 skipped` each run (on unmodified `47efa2e`, `reused_teb_after_terminate_thread_still_exchanges_slots` failed with `host TLS was corrupted on new thread (iter=234, reused=1)`). - `cargo test -p xcl --lib tls::`: `4 passed; 0 failed; 1 ignored; 0 skipped`. - `cargo test -p xcl --test e2e`: `3 passed; 0 failed; 0 ignored; 0 skipped`. - `XCL_REQUIRE_GPU=wave64,mesh cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4`: `740 passed; 3 failed; 41 ignored; 0 skipped`. All 3 failures are the pre-existing RDNA2 `placed_title_textures_share_vram_and_preserve_pixels` + 2 `VRAM_TEST_LOCK` `PoisonError` cascades on unmodified `main` (tracked in #470). - `.\scripts\smoke.ps1 -Game '<game folder>\YouKnowWho'` on `39ea43e9d95d706aaa6ec679c7544d63c822a566` (see Regression run above). **Not tested** - MinGW / `gnu` target builds (MSVC toolchain only). - Non-AMD GPUs and Linux / Wine. **AI use** Antigravity (Gemini 4 Neon) audited `crates/xcl/src/tls.rs`, wrote the fix and process-isolated regression tests, and ran the local test suite and smoke runs.
Keep peer thread handles open across both phases of take_slot_zero, suspend each peer across raw TEB/TLS-array syscalls and pointer swaps with WaitForSingleObject + ReadProcessMemory liveness checks, and publish XCL_SLOT before snapshotting threads so concurrent attach/detach serializes on MOVED.

Track MOVED entries by (teb, tid, open thread handle) and prune signaled handles in exchange_for_new_thread and on_thread_detach so threads terminated without DLL_THREAD_DETACH (e.g. TerminateThread) cannot poison a recycled TEB address and skip the slot 0 / slot k swap on future threads.

Part of #106.

Assisted-by: Antigravity (Gemini 4 Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:42:51 +00:00
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-24 14:25 UTC · original

Full-suite run, since this PR has no test report yet. Tested exact head 39ea43e (on its base 47efa2e).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 743 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: reused_teb_after_terminate_thread_still_exchanges_slots, take_slot_zero_survives_concurrent_thread_exit_and_termination.

Not tested

  • AMD, a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
**@⁠futurepro62** on GitHub · 2026-11-24 14:25 UTC · [original](https://github.com/xcl-dev/xcl/pull/1083#issuecomment-7134345475) Full-suite run, since this PR has no test report yet. Tested exact head `39ea43e` (on its base `47efa2e`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed. - `cargo build --workspace`, then `XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4`: **743 passed, 0 failed, 41 ignored**. SKIPPED (no wave64): `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`. - The new tests ran and passed: `reused_teb_after_terminate_thread_still_exchanges_slots`, `take_slot_zero_survives_concurrent_thread_exit_and_termination`. **Not tested** - AMD, a release build, a game (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 08:36 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!

**@⁠DavidM1911** on GitHub · 2026-11-25 08:36 UTC · [original](https://github.com/xcl-dev/xcl/pull/1083#issuecomment-7157254602) 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!
Author
Owner

@⁠upipibi on GitHub · 2026-11-25 20:59 UTC · original

NVIDIA test report for 39ea43e (head as is, base 47efa2e), for #1607's "one more test report" list. RTX 3070 8 GB (Ampere, 32-lane waves), driver 616.56, Windows 11, debug, after cargo build --workspace:

  • cargo test --workspace --no-fail-fast -- --test-threads=4 --skip hle::xaudio2: 734 passed, 0 failed, 41 ignored. The end-to-end title tests pass, and so does the new reused_teb_after_terminate_thread_still_exchanges_slots (tests/tls_slot_zero.rs).
  • Skipped hle::xaudio2, because its tests fail or hang on this machine on main too. The PR doesn't touch it.
  • SKIPPED lines: 2, both no wave64 on this card.

AI-assisted (Claude Code, Claude Opus 5.5). Synthetic tests only; no title run.

**@⁠upipibi** on GitHub · 2026-11-25 20:59 UTC · [original](https://github.com/xcl-dev/xcl/pull/1083#issuecomment-7174452005) NVIDIA test report for `39ea43e` (head as is, base `47efa2e`), for #1607's "one more test report" list. RTX 3070 8 GB (Ampere, 32-lane waves), driver 616.56, Windows 11, debug, after `cargo build --workspace`: - `cargo test --workspace --no-fail-fast -- --test-threads=4 --skip hle::xaudio2`: **734 passed, 0 failed, 41 ignored**. The end-to-end title tests pass, and so does the new `reused_teb_after_terminate_thread_still_exchanges_slots` (`tests/tls_slot_zero.rs`). - Skipped `hle::xaudio2`, because its tests fail or hang on this machine on `main` too. The PR doesn't touch it. - SKIPPED lines: 2, both `no wave64` on this card. AI-assisted (Claude Code, Claude Opus 5.5). Synthetic tests only; no title run.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1083/head:github-bot/gh-1083'
git switch 'github-bot/gh-1083'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1083'
git switch 'github-bot/gh-1083'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1083'
git switch 'github-bot/gh-1083'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1083'
git switch 'main'
git merge --squash 'github-bot/gh-1083'
git switch 'main'
git merge --ff-only 'github-bot/gh-1083'
git switch 'main'
git merge 'github-bot/gh-1083'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.