TLS: harden take_slot_zero against TEB UAF and stale MOVED entries #1074
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/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?
Imported from ivall/xcl. Originally posted by real-alponzo on 2026-11-24T11:24:15Z.
Hardens
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.Imported from ivall/xcl. Originally posted by futurepro62 on 2026-11-24T14:25:16Z.
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
Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-25T08:36:49Z.
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 upipibi on 2026-11-25T20:59:02Z.
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.
Imported from ivall/xcl. Originally posted by throny on 2026-11-29T01:09:20Z.
merge-check claim
head:
39ea43e9d9Imported from ivall/xcl. Originally posted by throny on 2026-11-29T01:21:41Z.
merge-check
base:
fa204595head:
39ea43e9d9merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2610 passed, 0 failed, 49 ignored (27 SKIPPED lines). Base alone at
fa204595: 2608 passed, 0 failed, 49 ignored.review: no blocking finding; all hunks read with care for lifetimes and lock order. main's tls.rs is still the file this PR was written against. The stale entries are the thread blocks of threads ended by TerminateThread, which never run their detach; matching on block address plus thread id and keeping the thread's handle open (so its id cannot be reused) closes that. Other threads' blocks are now read through ReadProcessMemory, so such a read cannot fault; nothing allocates or takes a lock while a thread is suspended; the lock order is the one main has. Worth a follow-up, none worse than main: SuspendThread returns before the thread has really stopped (a GetThreadContext after it would make that certain); the two writes into another thread's TLS array are plain writes; and when suspending a live thread fails, that thread is skipped, which in the second phase would lose the title's block for it, where main would have written anyway (needs a failing suspend, which is rare). Costs: each thread attach and detach now does one wait per tracked entry. One of the new tests relies on Windows reusing a thread block within 2048 spawns, which could be flaky on some system. It will conflict textually with the open #464 on the same file.
performance: negligible (the slot work runs once per title; a short wait per tracked entry at thread attach and detach). No translator source.
verdict: safe to merge
hardware / tool and model: RTX 4080 16 GB (621.40), i9-12900K, Windows 11 26300, Rust 1.100.0, debug. Claude Code; merge, build and suite run by Claude Fable 5.2, diff review by Claude Opus 5.5 (every hunk read; nothing run on the GPU beyond the suite).
Imported from ivall/xcl. Originally posted by relativespace2K on 2026-11-30T02:52:25Z.
merge-check
base:
53e85fb1head:
39ea43e9d9merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2790 passed, 6 failed, 49 ignored; failing: gpu::dxil::compact_tests::gpu_compact_form_with_micro_blocks_and_literal_table_runs_as_the_plain_form, gpu::recomp::pso::shader_cache_tests::console_mesh_caches_hlsl_compute_pass_in_memory_and_on_disk, hle::d3d12x::standin::tests::concurrent_shell_miss_reuses_winner_and_releases_loser_pipeline, ram_budget::tests::windows_takes_a_working_set_maximum_and_lifts_it, and two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs (in
--test gpu_two_pass_cache_e2e, so 5 failing tests give 6 failures). The same five fail on base53e85fb1alone (2788 passed, 6 failed, 49 ignored there), so none comes from this PR; its two new tests incrates/xcl/tests/tls_slot_zero.rspassed, and they run in a child copy of the test binary so_tls_indexreally starts at 0 and the.CRT$XLYcallback sees real attach/detach. 13 SKIPPED lines, all environmental: no D3D12 debug layer / d3d12SDKLayers.dll, no heap windows, no GPU_UPLOAD, per-lane f16/f32 forms refused.review: no blocking finding; two files, every hunk read. The exchange now holds an open handle per peer, so a TID or TEB cannot be recycled under a stale
MOVEDentry, matches entries on TEB and tid, and prunes dead ones;SuspendThread/ResumeThreadbracket only raw syscalls andReadProcessMemorypointer reads, with no allocation or lock taken while a peer is suspended, so the "suspend then allocate" deadlock is not re-introduced; a peer that is mid-LdrpAllocateTlswith empty slots is skipped and left to its own attach.XCL_SLOTis published before the snapshot now, which is the right order for threads attaching during the swap.read_own_ptrgoing throughReadProcessMemoryrather than dereferencing is what makes a peer dying mid-read aNoneinstead of an access violation. The tests are aggressive on purpose (TerminateThread, TEB reuse, churn) but they are process-isolated and they assert host TLS isolation and heap validity afterwards, which is the invariant the fix is about. Notes, not blocking:MOVEDkeeps handles for moved threads until they detach or are pruned, and the test helper spawns a child copy of the test binary (guarded byXCL_TLS_SLOT_ZERO_CHILD) which is the only process it starts. No duplicate: nothing else changestake_slot_zero; #431 and #196 touch other TLS paths.performance: once per process, at start-up: one handle per thread, two short suspensions each, and the same 20 ms sleep as before; nothing per frame or per thread creation beyond a
WaitForSingleObject(0)whenMOVEDis touched.verdict: safe to merge
hardware / tool and model: AMD Radeon RX 6800 XT 16 GB (RDNA2, wave64), driver 32.0.21045.29003; Ryzen 5 5600, 32 GB RAM; Windows 10 IoT Enterprise LTSC 2024, 10.0.26100 build 26100.9974; Rust 1.100.0; DXC 1.9.2602.17; debug build. DeepSeek Harness coding agent, model DeepSeek V4.1 Flash.