D3D12.X: fix concurrent first-use UAF in scale_indirect_pipeline cache #3590
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3590"
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 iggyvilla on 2026-12-01T17:25:23Z.
Fixes a concurrent first-use use-after-free crash in
scale_indirect_pipeline(crates/xcl/src/hle/d3d12x/bind.rs) that caused deterministic0xc0000005crashes inamdxc64.dll+0x38b335(read at0x220) when multiple recording threads hit a two-passExecuteIndirectfor the first time simultaneously.TL;DR — Before vs After (RX 6650 XT 8 GB,
origin/amd-dev41aa60ce)41aa60ce)f44547ac)amdxc64.dll+0x38b335UAF crash att=108.3–108.8 s0xc0000005, 2 threads at once)scripts/smoke.ps1)maind8bd9a38)f44547ac)Root cause
prepare_scaled_argsstores rawrsandpsopointers inRewriteand drops its localID3D12RootSignature/ID3D12PipelineStatehandles, relying on the invariant documented atbind.rs:3544:Previously,
scale_indirect_pipelinecheckedCACHE, compiled/created(rs, pso)unlocked, and then unconditionally calledCACHE.insert(key, (rs.clone(), pso.clone())). When two recording threads raced on first use:(rs_A, pso_A)intoCACHE, returned them toprepare_scaled_args, extracted raw pointers intoRewrite, and dropped its local COM references.CACHE.insert(key, (rs_B, pso_B)), overwriting and releasing(rs_A, pso_A)(refcount → 0).SetComputeRootSignature/SetPipelineStatewith the freed raw pointers →0xc0000005inside the driver.What is in it
crates/xcl/src/hle/d3d12x/bind.rs:cache_first_entry, which inserts intoCACHEonly whenkeyis absent, always returns the winning cached entry, and drops any losing duplicate after releasing the cache mutex (so COMReleasenever runs underCACHE.lock()).cache_first_entryinscale_indirect_pipelineso the first inserted(rs, pso)pair stays alive for the lifetime of the cache and every caller shares the same pointers.scale_indirect_cache_first_never_overwrites_existing_entry(verifies the first entry stays alive and the losing duplicate is dropped outside the lock) and GPU concurrency testscale_indirect_pipeline_concurrent_calls_return_same_pair(4 barrier-synchronized threads receive identical non-null(rs, pso)pointers).Tested
32.0.21045.29003), AMD Ryzen 7 5700X, Windows 11,rustc 1.100.0 (0ef1bc386 2026-11-09).cargo test -p xcl --lib -- hle::d3d12x::bind::tests --test-threads=1(debug): 123 passed, 0 failed, 0 ignored, 0 skipped (including both new regression tests andgpu_scale_args_reports_uncapped_group_count_feedback).origin/amd-dev41aa60ce: 2/2 deterministic0xc0000005crashes att=108.3–108.8 s(amdxc64.dll+0x38b335, read at0x220) before the fix; 0/6 crashes after applying the fix.Regression run
Baseline (
main):PR head (
f44547ac):Not tested
AI use
cache_first_entryfix and tests, and ran the local verification and smoke run.Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T20:24:48Z.
Review (head
f44547ac, diff read, not a merge-check):main) fixes the same last-insert-wins race in thescale_indirect_pipelinecache the same way, keeping the first pair (entry(key).or_insert(pair)). Neithermainnoramd-devhas it yet, so either one of the two is chosen or this one is marked as theamd-devport of #3317.scale_indirect_pipeline_concurrent_calls_return_same_pair(bind.rs~11727) uses the process-wideCACHEand the sharedrunner::device().gpu_scale_args_reports_uncapped_group_count_feedback(~11022) already callsscale_indirect_pipelineon that device and runs earlier in name order, so the four threads normally take the cached fast path and pass with or without the fix. Only thecache_first_entryunit test reliably covers the change.Fine: the root cause is right (
prepare_scaled_argskeeps only rawrs/psopointers inRewrite, ~3563, and relies on the cache owning the pair), the first insert now wins, and the losing pair is released outside the cache lock.AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by Edgaras0404 on 2026-12-02T02:30:38Z.
merge-check claim
head:
f44547acbeImported from ivall/xcl. Originally posted by Edgaras0404 on 2026-12-02T03:05:21Z.
merge-check
base:
41aa60ce84head:
f44547acbemerge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> non-lib test targets summed: 139 passed, 2 failed, 20 ignored (the 2 are the same known gpu_two_pass_cache_e2e::two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs parent/child cases); -p xcl --lib: 1993 ok, 3 failed, 23 ignored observed, then the binary aborted at exit 0x80004005 after
D3D12 queue drain: Signal failed: The parameter is incorrect. (0x80070057)(notest result:line). The 3 lib failures (gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu, gpu::recomp::tests::gpu_f32_compares_with_nan, gpu::recomp::tests::gpu_float_compares_random) and the e2e failure also fail identically on base41aa60ce(lib 1991 ok, 3 failed, 23 ignored, same abort; non-lib 139/2/20). SKIPPED (6, identical on base): 4x gpu::recomp::lower_dxil::tests::f16_programs_on_the_gpu (V_MUL_F16 / V_PK_MUL_F16 per-lane form refused), hle::d3d12x::bind::tests::two_pass_binds_set_the_shared_view_heap (no heap windows), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).review: The fix itself is correct: the first insert wins, the losing pair is released after the cache lock, and every caller gets a live clone, so the raw rs/pso pointers
prepare_scaled_argskeeps stay valid. Findings, most serious first: (1) duplicate of open #3317 (older, 29 Nov, against main): it makes the same first-wins change in the same function withentry().or_insert, and also keeps a device reference in each entry and fixes the per-device ARGUMENT_ORDER buffer; #3590 is the amd-dev port of the cache part. Pick one; #3317'sPR descriptioncheck is red, #3590's is green. (2) the new GPU test scale_indirect_pipeline_concurrent_calls_return_same_pair does not normally reach the race: it uses the process-wide cache and the shared device, and gpu_scale_args_reports_uncapped_group_count_feedback (earlier by name) already fills that cache, so the four threads take the hit path and the test passes with or without the fix; only the cache_first_entry unit test is a real regression test. (3)Testedin the description has only the bind module (123 passed), not whole-suite counts on a GPU as CONTRIBUTING requires for a runtime change. (4) the cache is still keyed by device address without holding the device (pre-existing; #3317 fixes it). Not tested by the author on NVIDIA/Intel.performance: 60 s smoke on the same retail title: base
41aa60ce1566 frames, peak 16335 MB, 0 removals, 0 crashes; merge a376a103 1578 frames (+0.8%), peak 17740 MB (108.6% of base), 0 removals, 0 crashes, presents 61/60/60. Within the 80% frames / 125% commit bounds. The fix adds one cache lookup and a clone on the miss path; no per-frame cost.verdict: not yet: duplicates open #3317 (older, same first-wins fix in the same function); choose one PR and fold the amd-dev port into it.
hardware / tool and model: AMD Radeon RX 6700 XT 12 GB (RDNA2), driver 32.0.21045.29003; AMD Ryzen 5 5600X; Windows 10 build 19045 / OpenCode, DeepSeek V4.1 Flash 1017
Imported from ivall/xcl. Originally posted by iggyvilla on 2026-12-02T03:54:10Z.
Closing as a duplicate of older open #3317 (same first-wins fix in
scale_indirect_pipeline). Thanks @Foniass and @Edgaras0404 for catching this during review and merge-check!Pull request closed