D3D12.X: fix concurrent first-use UAF in scale_indirect_pipeline cache #3590

Closed
iggyvilla wants to merge 1 commit from upstream-pr/3590 into amd-dev
iggyvilla commented 2026-12-01 17:25:23 +00:00 (Migrated from github.com)

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 deterministic 0xc0000005 crashes in amdxc64.dll+0x38b335 (read at 0x220) when multiple recording threads hit a two-pass ExecuteIndirect for the first time simultaneously.

TL;DR — Before vs After (RX 6650 XT 8 GB, origin/amd-dev 41aa60ce)

Scenario Metric Before (41aa60ce) After (f44547ac) Delta
Title world-load entry (multi-threaded recording) amdxc64.dll+0x38b335 UAF crash at t=108.3–108.8 s 2 / 2 runs crashed (0xc0000005, 2 threads at once) 0 / 6 runs crashed 100% eliminated
60 s smoke (scripts/smoke.ps1) Frames shown / Peak commit 1,618 / 15,479 MB (main d8bd9a38) 1,651 / 15,568 MB (f44547ac) +2.0% frames / 1.006x commit

Root cause
prepare_scaled_args stores raw rs and pso pointers in Rewrite and drops its local ID3D12RootSignature/ID3D12PipelineState handles, relying on the invariant documented at bind.rs:3544:

(the pair is the device's for good: scale_indirect_pipeline keeps it)

Previously, scale_indirect_pipeline checked CACHE, compiled/created (rs, pso) unlocked, and then unconditionally called CACHE.insert(key, (rs.clone(), pso.clone())). When two recording threads raced on first use:

  1. Thread A inserted (rs_A, pso_A) into CACHE, returned them to prepare_scaled_args, extracted raw pointers into Rewrite, and dropped its local COM references.
  2. Thread B then called CACHE.insert(key, (rs_B, pso_B)), overwriting and releasing (rs_A, pso_A) (refcount → 0).
  3. Thread A called SetComputeRootSignature / SetPipelineState with the freed raw pointers → 0xc0000005 inside the driver.

What is in it

  • crates/xcl/src/hle/d3d12x/bind.rs:
    • Add cache_first_entry, which inserts into CACHE only when key is absent, always returns the winning cached entry, and drops any losing duplicate after releasing the cache mutex (so COM Release never runs under CACHE.lock()).
    • Use cache_first_entry in scale_indirect_pipeline so the first inserted (rs, pso) pair stays alive for the lifetime of the cache and every caller shares the same pointers.
    • Add unit test 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 test scale_indirect_pipeline_concurrent_calls_return_same_pair (4 barrier-synchronized threads receive identical non-null (rs, pso) pointers).

Tested

  • Hardware / OS / Rust: AMD Radeon RX 6650 XT (8 GB RDNA2, driver 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 and gpu_scale_args_reports_uncapped_group_count_feedback).
  • Retail title loading/world-entry verification on origin/amd-dev 41aa60ce: 2/2 deterministic 0xc0000005 crashes at t=108.3–108.8 s (amdxc64.dll+0x38b335, read at 0x220) before the fix; 0/6 crashes after applying the fix.

Regression run

Baseline (main):

xcl-smoke v1
commit: d8bd9a38204667f3ca7dd9eeffcc2f1c937b07b0
gpu: AMD Radeon RX 6650 XT
title: 1778c2d9
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 15479 MB

PR head (f44547ac):

xcl-smoke v1
commit: f44547acbebbfb887baa2901459006cc5e89febd
gpu: AMD Radeon RX 6650 XT
title: 1778c2d9
seconds: 60
exit: 0x0
frames shown: 1651 (PresentX 1651)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 15568 MB

Not tested

  • NVIDIA / Intel GPUs and multi-device setups (only tested on single-GPU AMD Radeon RX 6650 XT on Windows 11).

AI use

  • Antigravity (Gemini) diagnosed the race from crash logs, wrote the cache_first_entry fix and tests, and ran the local verification and smoke run.
<!-- greyfinch-sync:ivall/xcl:issue:5590873589 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3590). Originally posted by [iggyvilla](https://github.com/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 deterministic `0xc0000005` crashes in `amdxc64.dll+0x38b335` (read at `0x220`) when multiple recording threads hit a two-pass `ExecuteIndirect` for the first time simultaneously. ### TL;DR — Before vs After (RX 6650 XT 8 GB, `origin/amd-dev` `41aa60ce`) | Scenario | Metric | Before (`41aa60ce`) | After (`f44547ac`) | Delta | |---|---|---:|---:|---:| | **Title world-load entry (multi-threaded recording)** | `amdxc64.dll+0x38b335` UAF crash at `t=108.3–108.8 s` | **2 / 2 runs crashed** (`0xc0000005`, 2 threads at once) | **0 / 6 runs crashed** | **100% eliminated** | | **60 s smoke (`scripts/smoke.ps1`)** | Frames shown / Peak commit | 1,618 / 15,479 MB (`main` `d8bd9a38`) | **1,651 / 15,568 MB** (`f44547ac`) | **+2.0% frames / 1.006x commit** | **Root cause** `prepare_scaled_args` stores raw `rs` and `pso` pointers in `Rewrite` and drops its local `ID3D12RootSignature`/`ID3D12PipelineState` handles, relying on the invariant documented at `bind.rs:3544`: > `(the pair is the device's for good: scale_indirect_pipeline keeps it)` Previously, `scale_indirect_pipeline` checked `CACHE`, compiled/created `(rs, pso)` unlocked, and then unconditionally called `CACHE.insert(key, (rs.clone(), pso.clone()))`. When two recording threads raced on first use: 1. Thread A inserted `(rs_A, pso_A)` into `CACHE`, returned them to `prepare_scaled_args`, extracted raw pointers into `Rewrite`, and dropped its local COM references. 2. Thread B then called `CACHE.insert(key, (rs_B, pso_B))`, overwriting and releasing `(rs_A, pso_A)` (refcount → 0). 3. Thread A called `SetComputeRootSignature` / `SetPipelineState` with the freed raw pointers → `0xc0000005` inside the driver. **What is in it** - `crates/xcl/src/hle/d3d12x/bind.rs`: - Add `cache_first_entry`, which inserts into `CACHE` only when `key` is absent, always returns the winning cached entry, and drops any losing duplicate *after* releasing the cache mutex (so COM `Release` never runs under `CACHE.lock()`). - Use `cache_first_entry` in `scale_indirect_pipeline` so the first inserted `(rs, pso)` pair stays alive for the lifetime of the cache and every caller shares the same pointers. - Add unit test `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 test `scale_indirect_pipeline_concurrent_calls_return_same_pair` (4 barrier-synchronized threads receive identical non-null `(rs, pso)` pointers). **Tested** - Hardware / OS / Rust: AMD Radeon RX 6650 XT (8 GB RDNA2, driver `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 and `gpu_scale_args_reports_uncapped_group_count_feedback`). - Retail title loading/world-entry verification on `origin/amd-dev` `41aa60ce`: 2/2 deterministic `0xc0000005` crashes at `t=108.3–108.8 s` (`amdxc64.dll+0x38b335`, read at `0x220`) before the fix; 0/6 crashes after applying the fix. **Regression run** Baseline (`main`): ``` xcl-smoke v1 commit: d8bd9a38204667f3ca7dd9eeffcc2f1c937b07b0 gpu: AMD Radeon RX 6650 XT title: 1778c2d9 seconds: 60 exit: 0x0 frames shown: 1618 (PresentX 1618) presents per 2 s (last 3): 60 61 60 device removals: 0 crashes: 0 peak commit: 15479 MB ``` PR head (`f44547ac`): ``` xcl-smoke v1 commit: f44547acbebbfb887baa2901459006cc5e89febd gpu: AMD Radeon RX 6650 XT title: 1778c2d9 seconds: 60 exit: 0x0 frames shown: 1651 (PresentX 1651) presents per 2 s (last 3): 61 60 60 device removals: 0 crashes: 0 peak commit: 15568 MB ``` **Not tested** - NVIDIA / Intel GPUs and multi-device setups (only tested on single-GPU AMD Radeon RX 6650 XT on Windows 11). **AI use** - Antigravity (Gemini) diagnosed the race from crash logs, wrote the `cache_first_entry` fix and tests, and ran the local verification and smoke run.
Foniass commented 2026-12-01 20:24:48 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T20:24:48Z.


Review (head f44547ac, diff read, not a merge-check):

  • Same fix as open #3317. #3317 (against main) fixes the same last-insert-wins race in the scale_indirect_pipeline cache the same way, keeping the first pair (entry(key).or_insert(pair)). Neither main nor amd-dev has it yet, so either one of the two is chosen or this one is marked as the amd-dev port of #3317.
  • The concurrency test usually never reaches the race. scale_indirect_pipeline_concurrent_calls_return_same_pair (bind.rs ~11727) uses the process-wide CACHE and the shared runner::device(). gpu_scale_args_reports_uncapped_group_count_feedback (~11022) already calls scale_indirect_pipeline on 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 the cache_first_entry unit test reliably covers the change.

Fine: the root cause is right (prepare_scaled_args keeps only raw rs/pso pointers in Rewrite, ~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.

<!-- greyfinch-sync:ivall/xcl:comment:7324641620 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3590#issuecomment-7324641620). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T20:24:48Z. --- Review (head f44547ac, diff read, not a merge-check): - **Same fix as open #3317.** #3317 (against `main`) fixes the same last-insert-wins race in the `scale_indirect_pipeline` cache the same way, keeping the first pair (`entry(key).or_insert(pair)`). Neither `main` nor `amd-dev` has it yet, so either one of the two is chosen or this one is marked as the `amd-dev` port of #3317. - **The concurrency test usually never reaches the race.** `scale_indirect_pipeline_concurrent_calls_return_same_pair` (`bind.rs` ~11727) uses the process-wide `CACHE` and the shared `runner::device()`. `gpu_scale_args_reports_uncapped_group_count_feedback` (~11022) already calls `scale_indirect_pipeline` on 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 the `cache_first_entry` unit test reliably covers the change. Fine: the root cause is right (`prepare_scaled_args` keeps only raw `rs`/`pso` pointers in `Rewrite`, ~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.
Edgaras0404 commented 2026-12-02 02:30:38 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Edgaras0404 on 2026-12-02T02:30:38Z.


merge-check claim
head: f44547acbe

<!-- greyfinch-sync:ivall/xcl:comment:7332905508 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3590#issuecomment-7332905508). Originally posted by [Edgaras0404](https://github.com/Edgaras0404) on 2026-12-02T02:30:38Z. --- merge-check claim head: f44547acbebbfb887baa2901459006cc5e89febd
Edgaras0404 commented 2026-12-02 03:05:21 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Edgaras0404 on 2026-12-02T03:05:21Z.


merge-check
base: 41aa60ce84
head: f44547acbe
merge: 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) (no test 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 base 41aa60ce (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_args keeps 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 with entry().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's PR description check 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) Tested in 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 41aa60ce 1566 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

<!-- greyfinch-sync:ivall/xcl:comment:7333518803 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3590#issuecomment-7333518803). Originally posted by [Edgaras0404](https://github.com/Edgaras0404) on 2026-12-02T03:05:21Z. --- merge-check base: 41aa60ce842060a597b7e8724896f7bf6a031af5 head: f44547acbebbfb887baa2901459006cc5e89febd merge: 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)` (no `test 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 base 41aa60ce (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_args` keeps 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 with `entry().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's `PR description` check 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) `Tested` in 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 41aa60ce 1566 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
iggyvilla commented 2026-12-02 03:54:10 +00:00 (Migrated from github.com)

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!

<!-- greyfinch-sync:ivall/xcl:comment:7334353713 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3590#issuecomment-7334353713). Originally posted by [iggyvilla](https://github.com/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

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!3590
No description provided.