TLS: harden take_slot_zero against TEB UAF and stale MOVED entries #1083
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!1083
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1083"
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?
@AlpPesen on GitHub · 2026-11-24 11:24 UTC · original
AlpPesen:fix/tls-take-slot-zero-safety→main· +433 −30 · 2 files · 1 commits · openHardens
tls::take_slot_zeroandMOVEDtracking against two concurrency bugs from #106:ThreadLocalStoragePointerUAF duringtake_slot_zero: previouslythread_tebs()closed every thread handle immediately after readingThreadBasicInformation, thentake_slot_zerodereferenced the raw TEBs and wrote*arr = blockafterSleep(20)without verifying the thread was still alive. PublishingXCL_SLOTonly after the snapshot also left a window where concurrently starting or exiting threads bypassedMOVEDserialization.MOVEDentries afterTerminateThread:MOVEDstored only raw TEB addresses (Vec<usize>). A thread killed withoutDLL_THREAD_DETACHleft its TEB address inMOVEDpermanently, causingexchange_for_new_threadto skip swapping slot0and slotkwhen 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, andon_thread_detach, and places new tests in a separate integration test file.)What is in it
crates/xcl/src/tls.rs:MOVED: Mutex<Vec<usize>>withMovedTeb { teb, tid, handle: ThreadHandle }(Dropcloses the handle). Keeping the handle open prevents Windows from reusingtidwhile tracked and letsexchange_for_new_threadandon_thread_detachprune threads that died withoutDLL_THREAD_DETACHviaWaitForSingleObject(handle, 0) == WAIT_TIMEOUT.XCL_SLOT.store(k, Ordering::SeqCst)underMOVED.lock()before snapshotting peer threads so concurrent attach/detach serializes onMOVED.THREAD_QUERY_INFORMATION | THREAD_SUSPEND_RESUME | THREAD_SYNCHRONIZE) across both phases oftake_slot_zero, suspending each peer individually with a RAIISuspendGuardacross 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, orarr[k] == 0) so their upcomingon_thread_attachperforms the swap cleanly.MOVED.crates/xcl/tests/tls_slot_zero.rs:.CRT$XLYcallback forwarding realDLL_THREAD_ATTACH/DLL_THREAD_DETACHevents:reused_teb_after_terminate_thread_still_exchanges_slots— verified to fail deterministically on unmodifiedmain(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— exercisestake_slot_zerounder concurrent thread spawn/exit churn andTerminateThread, validating both survivor TLS integrity andHeapValidate.Regression run
Baseline (
main, warm shader/PSO cache):This PR (
fix/tls-take-slot-zero-safety, warm shader/PSO cache):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 skippedeach run (on unmodified47efa2e,reused_teb_after_terminate_thread_still_exchanges_slotsfailed withhost 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 RDNA2placed_title_textures_share_vram_and_preserve_pixels+ 2VRAM_TEST_LOCKPoisonErrorcascades on unmodifiedmain(tracked in #470)..\scripts\smoke.ps1 -Game '<game folder>\YouKnowWho'on39ea43e9d95d706aaa6ec679c7544d63c822a566(see Regression run above).Not tested
gnutarget builds (MSVC toolchain only).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.@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 base47efa2e).Tested
cargo build --workspace, thenXCL_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.reused_teb_after_terminate_thread_still_exchanges_slots,take_slot_zero_survives_concurrent_thread_exit_and_termination.Not tested
AI use
@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!
@upipibi on GitHub · 2026-11-25 20:59 UTC · original
NVIDIA test report for
39ea43e(head as is, base47efa2e), for #1607's "one more test report" list. RTX 3070 8 GB (Ampere, 32-lane waves), driver 616.56, Windows 11, debug, aftercargo 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 newreused_teb_after_terminate_thread_still_exchanges_slots(tests/tls_slot_zero.rs).hle::xaudio2, because its tests fail or hang on this machine onmaintoo. The PR doesn't touch it.no wave64on this card.AI-assisted (Claude Code, Claude Opus 5.5). Synthetic tests only; no title run.
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.