D3D12.X: keep first scale_indirect_pipeline entry alive across races #3711
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3711"
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 Kalmarv on 2026-12-02T17:52:23Z.
What is in it
Fixes a use-after-free race in
scale_indirect_pipeline(crates/xcl/src/hle/d3d12x/bind.rs) that crashed the retail title at the first world two-passExecuteIndirect(DispatchMesh)submits with0xc0000005insidenvwgf2umx.dll(read at address 0xe6b8,rax=0x0inSetPipelineState).Root cause
ExecuteIndirect(DispatchMesh)within a few milliseconds of each other and concurrently enterscale_indirect_pipeline(&dev)on a coldCACHE.CACHE, compile/fetchSCALE_ARGS_HLSL, and calldev.CreateComputePipelineState, producing(rs_A, pso_A)and(rs_B, pso_B).(rs_A, pso_A)intoCACHE, returns toprepare_scaled_args, extracts raw pointers intoRewrite { rs, pso }, and drops its local COM handles assumingCACHEkeeps the pair alive for the life of the device (DeferredWork::Indirectlikewise retains those raw pointers for replay).CACHE.insert(key, (rs_B, pso_B)), overwritingCACHE[key]and releasing the last COM reference to(rs_A, pso_A).pso_Apointer tolist.SetPipelineState(ID3D12PipelineState::from_raw_borrowed(&pso_raw))→0xc0000005(or destroys a live PSO/root signature already recorded into a command list).Fix
cache_scale_pipelineand useentry(key).or_insert(built).clone()instead of.insert(...)so the first cached(ID3D12RootSignature, ID3D12PipelineState)for a device is never overwritten or dropped, and every racing caller receives that same canonical pair (while any losing duplicate is dropped unused).hle::d3d12x::bind::tests::scale_indirect_pipeline_race_keeps_first_entry_alive) proving both racing callers receive the same canonical entry, the cached entry stays alive after callers drop their local handles, and the unused duplicate is dropped cleanly.Tested
rustc 1.99.0, DXC1.9.2609.5, release & debug builds.cargo build --release -p xcl: ok (39 warnings, matchesorigin/main)..\scripts\test.ps1: passed with 0 failures (includinghle::d3d12x::bind::tests::scale_indirect_pipeline_race_keeps_first_entry_alive; 24 GPU tests skipped on this adapter: 22 no wave64, 2 no heap windows).scripts/smoke.ps1, same isolatedXCL_DATA_ROOT, DXC 1.9): clean, 0 device removals, 0 crashes.Regression run
Not tested
XCL_HEAP_WINDOWS=1(2 tests skipped).vkd3d-proton).AI use
Antigravity with Gemini 3.1 Pro analyzed the crash log and minidump, identified the
scale_indirect_pipelinecache-overwrite UAF race, implemented the fix and regression test, ran the build/test/smoke verification, and drafted this PR description.Imported from ivall/xcl. Originally posted by capy-ai[bot] on 2026-12-02T19:58:23Z.
Code review (head
69391983bf, diff read only — not built or tested on Windows/GPU, not a merge-check)Verdict: no blocking issues found. The diagnosis matches the code on
mainand the fix is the minimal correct one.Root cause checks out. On
main,scale_indirect_pipelinereturns the caller's own freshly built(rs, pso)and doesCACHE...insert(key, ...), so a second racer'sinsertdrops the first racer's cached pair.prepare_scaled_argsthen keeps onlyrs.as_raw()/pso.as_raw()inRewriteand drops its owned handles on return (the comment there says "the pair is the device's for good"), somake_scaled_args_buffer'sSetComputeRootSignature/SetPipelineStateviafrom_raw_borrowedcan see a freed object. That is a real UAF, consistent with the reportedSetPipelineStatefault.The fix is correct. In
cache_scale_pipeline:entry(key).or_insert(built).clone()returns the canonical entry to every caller, and the loser's duplicate is dropped while only its builder holds it, before any raw pointer to it escapes. TheRewriteinvariant now holds.build()(DXC compile + PSO create), so there is no new contention or lock-ordering exposure. A panic orErrinbuildleaves the cache untouched.dev.as_raw()cannot be reused for another device while the entry exists.origin/main(5ad98adb).Test.
scale_indirect_pipeline_race_keeps_first_entry_aliveplaces the barrier insidebuild, so both threads are guaranteed to miss before either inserts. That makes the race deterministic. If you revertor_inserttoinsert, ther0 == r1and "canonical not dropped" assertions fail, so the test does guard the fix. It exercises the extracted generic helper rather thanscale_indirect_pipelineitself, but that helper is the whole fix, so the coverage is adequate. It is not a GPU test, so it runs in the no-GPU CI job.Still needs Windows/GPU verification: a world-load repro on the crashing title, ideally past the first two-pass
ExecuteIndirect(DispatchMesh). The posted 60 s smoke covers launch and menu, which the PR itself notes may not reach that path. An AMD run would also help (gpu:both).AI use: Capy, Claude Opus 5.5 (static code review only; not built or tested on Windows/GPU).
Imported from ivall/xcl. Originally posted by fromthemoonx on 2026-12-02T21:21:09Z.
merge-check
base:
5ad98adbc3head:
69391983bfmerge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 3006 passed, 0 failed, 52 ignored; SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps); failing: none
review: read the whole diff (1 file, +76 -15). No problem found.
scale_indirect_pipelineat once with nothing cached, one process per try, 30 tries each:insert: in 30 of 30 tries, 7 of the 8 callers got a pipeline that is not the one the cache keeps.prepare_scaled_argsthen holds only its raw pointer, so it is freed. The root signature was the same object for all 8, so only the pipeline is affected here;insertput back and passes with the PR. It checks the helper with made-up entries, not the real pipeline; the scratch test above covers that.performance: a cached call does what it did before. Threads that race on the first call still each build a pipeline, as on main.
verdict: safe to merge
hardware / tool and model: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300 / Claude Code (Claude Opus 5.5)
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-03T04:56:52Z.
merge-check
base:
078358a058head:
69391983bfmerge: clean
build: ok
tests: 3007 passed, 0 failed, 52 ignored; 28 skipped, the same as on main
review: 1 high, 1 medium, 2 nits (below)
performance: 60 s smoke run: 1669 → 1673 frames (100 % of main), peak commit 17577 → 18013 MB, no device removal, no crash
title run: retail title on the RTX 5070 Ti (driver 621.14), into the world by scripted presses: 232 s there, 0 device removals, 0 crashes, 2.8 presents/s; run with
XCL_SKIP_CS=95cc13288a27a96e(the NVIDIA hang at the end of loading, #3541) and NVIDIA's 2048 MB shader heap (#2590)verdict: not yet: 1 blocking finding (critical or high, below)
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5
In short: Fixes the first-use race in
scale_indirect_pipeline: two threads that miss the cache both build a pair, and the secondinsertdrops the pair the first caller is about to use through raw pointers. The first-wins fix (entry().or_insert) is correct and the new test fails with the oldinsert. It blocks only because the older open #3317 makes the same fix and the two conflict.High
Medium
Nit
scale_indirect_pipeline_race_keeps_first_entry_alivedrivescache_scale_pipelinewith made-up entries. That is acceptable since the helper is the whole fix, and fromthemoonx's scratch test covered the real pipeline.cache_scale_pipelinereturns the error instead of the published pair (#3317 handles this). Rare: only on a DXC or PSO failure.Checked and fine
prepare_scaled_argskeeps onlyrs.as_raw()/pso.as_raw()(~3317), and main'sinsertdrops the first pairbuildforces both threads to miss; with main'sinsertther0 == r1and "not dropped" assertions fail; no GPU neededHow this was tested
078358a0, thencargo build --workspaceandcargo test --workspace --no-fail-fast -- --test-threads=4.scripts/smoke.ps1: 60 s of a retail title on main078358a0and on this merge; presents per 2 s at the end: 60 60 60.XCL_RUN_SECONDS=300and scripted A presses counted from the first frame (XCL_PRESS_BASE=frame, #3712) into the world from an existing save; time in the world is counted from the first in-world draw call to the end of the run.Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-03T05:00:48Z.
Thanks @Foniass for the thorough merge-check (and @fromthemoonx for the earlier check) — closing this in favor of the older, more complete #3317.
Pull request closed