Skip to content

D3D12.X: deduplicate concurrent stand-in creation and harden shell COM methods - #2369

Open
Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-625a0f3-standin-race-com
Open

Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-625a0f3-standin-race-com

Conversation

@Decodere

@Decodere Decodere commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Part of #1333
Part of #1566

What is in it

Follow-up to 625a0f3 in shared stand-in pipelines (crates/xcl/src/hle/d3d12x/standin.rs):

  1. Deduplicate concurrent shell() misses (shell):
    • On re-locking SHARED after a miss and out-of-lock make(), re-check whether another thread already populated key. If so, drop the lock, immediately unref the redundant driver pipeline outside SHARED.lock(), and reuse the winning Arc<Shared> without double-AddRefing key.root_signature or emitting duplicate power-of-two log lines.
  2. Harden query_interface (is_pipeline_iid, query_interface):
    • Always zero *ppv when ppv is non-null, including when riid is null.
    • Accept only the five single-inheritance ID3D12PipelineState hierarchy IIDs implemented by Shell's vtable (IUnknown, ID3D12Object, ID3D12DeviceChild, ID3D12Pageable, ID3D12PipelineState): validate with the underlying driver pipeline's QueryInterface, unref(out) the driver pointer (zero refcount leak), and return this with add_ref(this). Do not require out as usize == pipeline, because real d3d12.dll pipeline objects return a distinct subobject pointer for IUnknown/ID3D12Object/ID3D12DeviceChild/ID3D12Pageable without the debug layer (caught by @fromthemoonx). Return E_NOINTERFACE for any non-pipeline interface.
  3. Fix set_private_data validation and implement per-shell set_name (set_private_data, set_private_data_interface, set_name):
    • Reject guid.is_null() or (size != 0 && data.is_null()) with E_INVALIDARG without erasing existing private data, and clear the entry when size == 0 (regardless of whether data is null).
    • Reject null name in set_name with E_INVALIDARG and store the NUL-terminated UTF-16 name per shell under WKPDID_D3DDebugObjectNameW (keeping addref under the data lock in get_private_data to avoid TOCTOU UAF).
  4. Regression tests (standin::tests):
    • concurrent_shell_miss_reuses_winner_and_releases_loser_pipeline: barrier-synchronized two-thread race confirming both shells share one driver pipeline, the loser pipeline drops to refs == 0, and root_signature refcount stays exact.
    • query_interface_rejects_secondary_interfaces_and_zeroes_out_on_null_riid: checks null-riid *ppv zeroing, all five single-inheritance IIDs (including distinct subobject pointer for IUnknown releasing the driver ref cleanly without leaking), and secondary IID rejection.
    • query_interface_on_real_d3d12_pipeline_accepts_hierarchy_and_rejects_other: builds a real D3D12 compute ID3D12PipelineState, wraps it with shell(), and verifies S_OK + out == s for all five hierarchy IIDs (IUnknown, ID3D12Object, ID3D12DeviceChild, ID3D12Pageable, ID3D12PipelineState) and E_NOINTERFACE + out.is_null() for ID3D12DebugCommandList (cleanly skips if no DXC or D3D12/WARP device).
    • set_private_data_validation_and_per_shell_set_name: checks (size != 0, null) preservation, size == 0 clearing with non-null pointer, null set_name rejection, and per-shell WKPDID_D3DDebugObjectNameW UTF-16 round-trip.

Tested

  • Pre-fix failing-first (cargo test -p xcl --lib hle::d3d12x::standin::tests -- --test-threads=1 --nocapture): 2 passed, 3 failed — concurrent_shell_miss_reuses_winner_and_releases_loser_pipeline kept both driver pipelines alive and double-logged 2 driver pipelines, query_interface_rejects_secondary_interfaces_and_zeroes_out_on_null_riid left *ppv non-null on null riid, and set_private_data_validation_and_per_shell_set_name returned S_OK instead of E_INVALIDARG for (size = 4, null).
  • Post-fix (cargo test --release -p xcl --lib hle::d3d12x::standin::tests -- --test-threads=1 --nocapture at 0738c30f atop 889ef3e8): 6 passed, 0 failed, 0 ignored on both NVIDIA GeForce RTX 4060 (0.35 s, 0 skipped) and WARP (0.20 s, 0 skipped), including query_interface_on_real_d3d12_pipeline_accepts_hierarchy_and_rejects_other.
  • Full release workspace (cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture on NVIDIA GeForce RTX 4060, driver 621.14, DXC 1.9.2609.5): 37 test binaries, 1417 passed, 0 failed, 46 ignored; 29 pre-existing hardware/debug-layer SKIPPED lines (0 in standin::tests): 21 no wave64 (RTX 4060 is wave32-only), 3 per-lane form: refused (lower_dxil::tests wave32 per-lane), 3 no D3D12 debug layer / no d3d12SDKLayers.dll, 1 cannot disable the driver cache (shaders::tests::pso_measurement_compiles_an_exact_synthetic_record), 1 no heap windows (heap_window_out_of_memory_falls_back_to_the_switch_form).

Regression run

Base (origin/main 889ef3e8aa7f267323ab4879e735e65da6bcef10):

xcl-smoke v1
commit: 889ef3e8aa7f267323ab4879e735e65da6bcef10
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1550 (PresentX 1550)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13981 MB

PR head (0738c30f4cf8705c953d6cab8f06dff1cca5d3ab):

xcl-smoke v1
commit: 0738c30f4cf8705c953d6cab8f06dff1cca5d3ab
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1540 (PresentX 1540)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13982 MB

Not tested

  • Wave64-capable hardware (RTX 4060 is wave32-only) and AMD/Intel GPUs.
  • D3D12 debug layer tests (d3d12SDKLayers.dll is not installed on this host).

AI use

Google Antigravity (Gemini 4 Neon) assisted with implementing the concurrency deduplication and COM method hardening in standin.rs, writing the mock-COM regression tests, and running the workspace and smoke verification suite.

@emirduran707

Copy link
Copy Markdown
Contributor

Independent AMD RDNA4 (native wave64) test report on exact head 9c48d84b46f80849611a7c44ae7c6a5adb381600 (clean detached checkout, no local changes).

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4, native wave64, driver 32.0.31044.16), Ryzen 5 5600, 32 GB RAM, Windows 11 Pro 26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2602.17; tests debug, smoke release.
  • cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4: 1285 passed, 2 failed, 46 ignored; 8 SKIPPED (6 in lower_dxil::tests::{f16,f32}_programs_on_the_gpu refused by per-lane form; heap_window_out_of_memory_falls_back_to_the_switch_form; pso_measurement_compiles_an_exact_synthetic_record). All native wave64 GPU tests ran and passed.
    • All 3 PR tests passed: hle::d3d12x::standin::tests::concurrent_shell_miss_reuses_winner_and_releases_loser_pipeline, query_interface_rejects_secondary_interfaces_and_zeroes_out_on_null_riid, and set_private_data_validation_and_per_shell_set_name.
    • The 2 failures (gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1 seed 722 and fuzz_validate_and_gpu_3 seed 1599) are pre-existing on unmodified main on this GPU.

Regression run (60 s retail-title smoke, XCL_NETWORK=0, release)

xcl-smoke v1
commit: 9c48d84b46f80849611a7c44ae7c6a5adb381600
gpu: AMD Radeon RX 9060 XT
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1601 (PresentX 1601)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14216 MB

(Matched main 68a284a baseline on the same machine today: 1573 frames, 14420 MB peak, 0 device removals, 0 crashes.)

Not tested

  • D3D12 debug layer; Intel GPUs.

AI use

  • Antigravity with Gemini 4 Neon ran the build, test suite, and smoke run and drafted this report.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

@Decodere Decodere closed this Nov 25, 2026
@Decodere Decodere reopened this Nov 25, 2026
@Decodere Decodere closed this Nov 25, 2026
@Decodere Decodere reopened this Nov 25, 2026
@SlopTart

Copy link
Copy Markdown

Tested: current PR head merged onto current origin/main (clean), Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT (NAVI31), cargo target x86_64-pc-windows-gnu run under wine-11.0 (Proton), rustc 1.94.0, vkd3d-proton 3.1 with the XCL patch, official MS DXC (binaries 2026-09-29).
suite: 145/3 = the three known baseline failures on this lane (fuzz_validate_and_gpu_2 seed 1003, gpu_nonfused_mad_and_mac_prevent_fma_contraction, gpu_depth_buffer_created_for_unordered_access_is_placed). Targeted runs of the three new shell tests (concurrent_shell_miss_reuses_winner_and_releases_loser_pipeline, query_interface_rejects_secondary_interfaces_and_zeroes_out_on_null_riid, set_private_data_validation_and_per_shell_set_name): 3 passed.

Not tested: retail titles (no title data on this lane, counts only).

AI use: Hermes agent (local Qwen-class) ran both machines and drafted this; I checked it before posting.

@fromthemoonx

fromthemoonx commented Nov 25, 2026 •

Copy link
Copy Markdown

Rerun on the new head 0738c30: fixed on NVIDIA. The stand-in answers all five pipeline IIDs on a real driver pipeline, and the new query_interface_on_real_d3d12_pipeline_accepts_hierarchy_and_rejects_other catches the old pointer check.

result
cargo test --workspace --no-fail-fast -- --test-threads=4, 0738c30 merged onto main 6d12f58 (clean) 1427 passed, 0 failed, 46 ignored; 25 SKIPPED lines; the new test ran (not skipped) and passed
same at the head 0738c30 (base 889ef3e) 1417 passed, 0 failed, 46 ignored; 25 SKIPPED
the new real-pipeline test with if out as usize != pipeline { return E_NOINTERFACE; } put back (scratch) fails 3 of 3: real pipeline QI failed for 00000000-0000-0000-C000-000000000046 (IUnknown)
the new test with a scratch EnableDebugLayer() and an info-queue dump passes, 0 stored messages
the new test, 3 runs; the race test alone, 40 runs; standin::tests:: (6 tests) at --test-threads=8, 40 runs 3 / 40 / 40, no failures

First run, on 9c48d84: On a real driver pipeline, query_interface refused IUnknown, ID3D12Object, ID3D12DeviceChild and ID3D12Pageable (E_NOINTERFACE), which main answers with S_OK. The suite passed on NVIDIA, and the new race test passed 40 of 40 and failed without the re-check.

Why: d3d12.dll's pipeline object answers those four IIDs with a pointer other than the pipeline's own, so the check if out as usize != pipeline { return E_NOINTERFACE; } (standin.rs:154) rejects them. The new tests use fake COM objects whose QI returns this, so they don't see it. I made a scratch test (not pushed) that builds a real compute PSO on the RTX 3060 and wraps it with shell(). For each IID it records the shell's QI result, and whether the driver's own QI returned the same pointer as the PSO:

IID PR, no debug layer PR, debug layer on main, either
IUnknown E_NOINTERFACE (driver: other pointer) E_NOINTERFACE (other pointer) S_OK
ID3D12Object / ID3D12DeviceChild / ID3D12Pageable E_NOINTERFACE (other pointer) S_OK (same pointer) S_OK
ID3D12PipelineState S_OK S_OK S_OK
ID3D12DebugCommandList (not a pipeline IID) E_NOINTERFACE E_NOINTERFACE E_NOINTERFACE

So without the debug layer, a title's QueryInterface for IUnknown, or for ID3D12Object (for SetName), on a stand-in pipeline now fails. Answering the five pipeline IIDs with this whenever the driver's QI succeeds, without comparing pointers, would keep main's behaviour and still refuse other interfaces. With the debug layer on, the info queue had 0 messages.

Tested: 9c48d84, and the same commit merged onto main 2898511 (clean). RTX 3060 12 GB (driver 610.88), Ryzen 7 5700X, Windows 11 Pro 26300, Rust 1.100.0 MSVC, debug, Graphics Tools (D3D12 debug layer) installed.

result
cargo test --workspace --no-fail-fast -- --test-threads=4, merged 1366 passed, 0 failed, 46 ignored; 25 SKIPPED lines (no wave64, refused per-lane forms)
same at the head 9c48d84 1287 passed, 0 failed, 46 ignored; 25 SKIPPED
concurrent_shell_miss_reuses_winner_and_releases_loser_pipeline alone, 40 runs; standin::tests:: (5 tests), 10 runs 40 of 40; 10 of 10
the same test with the re-check removed (scratch: if let Some(existing) = None::<Arc<Shared>>) fails 5 of 5 at standin.rs:399

Not tested: a title, a release build, AMD / Intel (another driver's objects may answer these IIDs differently).

AI use: Claude Code (Claude Opus 5.5) ran the tests and the scratch QI test, and wrote this report.

…M methods

Part of xcl-dev#1333
Part of xcl-dev#1566

Follow-up to 625a0f3 in shared stand-in pipelines (`hle/d3d12x/standin.rs`):

- In `shell()`, re-check `SHARED` on re-lock after concurrent `make()` misses
  so losing threads reuse the winning `Arc<Shared>`, skip double-AddRefing
  `root_signature`, suppress duplicate power-of-two log lines, and release the
  redundant driver pipeline outside `SHARED.lock()`.
- In `query_interface()`, zero `*ppv` even when `riid` is null, accept only the
  five single-inheritance `ID3D12PipelineState` IIDs (`IUnknown`,
  `ID3D12Object`, `ID3D12DeviceChild`, `ID3D12Pageable`, `ID3D12PipelineState`),
  validate via the underlying pipeline's `QueryInterface` and release its
  returned pointer (allowing real `d3d12.dll` subobject pointers distinct from
  `pipeline` for base IIDs), and return `E_NOINTERFACE` for any other interface.
- In `set_private_data()`, reject `(size != 0, data.is_null())` with
  `E_INVALIDARG` without erasing existing data and clear the entry when
  `size == 0`; in `set_name()`, reject null with `E_INVALIDARG` and store the
  NUL-terminated UTF-16 name per shell under `WKPDID_D3DDebugObjectNameW`.
- Add unit tests covering concurrent `shell()` races, `QueryInterface` on both
  synthetic tearoff/secondary mocks and a real D3D12 compute
  `ID3D12PipelineState`, `SetPrivateData` validation/clearing, and per-shell
  `SetName` round-trips.

Assisted-by: Google Antigravity (Gemini 4 Neon)

@bmjask bmjask left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: head 9c48d84 appears to make an existing unit test deterministically fail. is_pipeline_iid at standin.rs:137-139 allows only five D3D12 IIDs, and query_interface returns E_NOINTERFACE before forwarding any other IID (:141-146). But the existing a_shell_has_the_drivers_interfaces_and_its_own_references_and_private_data test still expects query_interface(s, &KNOWN, &mut out) == S_OK at :350-359; KNOWN (765a30f3-f624-4c6f-a828-ace948622445) is none of the five allowed IIDs. Please update/remove that legacy assertion to use a pipeline IID, or preserve the intended previous QI behavior, and rerun the tests from the current head.

@Decodere
Decodere force-pushed the fix-625a0f3-standin-race-com branch from 9c48d84 to 0738c30 Compare November 25, 2026 20:49
@Decodere

Copy link
Copy Markdown
Contributor Author

Great catch @fromthemoonx — fixed in 0738c30f!

query_interface still gates on the five single-inheritance pipeline IIDs (IUnknown, ID3D12Object, ID3D12DeviceChild, ID3D12Pageable, ID3D12PipelineState), validates with the driver pipeline, releases the returned driver pointer (unref(out)), and returns this (add_ref(this)) without requiring out == pipeline, so real d3d12.dll subobject pointers succeed while non-pipeline IIDs still return E_NOINTERFACE. Added query_interface_on_real_d3d12_pipeline_accepts_hierarchy_and_rejects_other testing a real D3D12 compute ID3D12PipelineState wrapped by shell() across all five hierarchy IIDs + ID3D12DebugCommandList (6/0 on both RTX 4060 and WARP; workspace 1417 passed, 0 failed; paired 60 s smoke 1550 vs 1540 frames, 0 crashes/removals).

(Re @bmjask: KNOWN = 765a30f3-f624-4c6f-a828-ace948622445 is ID3D12PipelineState::IID, so a_shell_has_the_drivers_interfaces_and_its_own_references_and_private_data passes on all runs.)

@ClosetPie107

Copy link
Copy Markdown

AMD RDNA4 / Linux (Proton Experimental) test report on head 0738c30f (merged cleanly onto main b49be183):

Hardware & environment

  • AMD Radeon RX 9070 16 GB (RDNA4, RADV GFX1201, Mesa 26.3.1, native wave64), Ryzen 5 9600X, 30 GiB RAM, CachyOS Linux
  • Proton Experimental (experimental-11.0-20261112), vkd3d-proton master ec207194 (3.2.0) + dxil-spirv b540c8a2 + relaxed-vtable patch (-Db_ndebug=true), DXVK v3.1.1-209, DXC 1.9.2609.5, Rust x86_64-pc-windows-gnu

Isolated unit tests (main b49be183 + #2369 0738c30f, debug, --test-threads=1):

  • xcl.exe hle::d3d12x::standin:: -- --test-threads=1: 6 passed, 0 failed, 0 ignored (0 skipped).

Combined 300 s retail sanity run (release, main b49be183 + #1540 + #1675 + #1844 + #1899 + #2123 + #2369 + #2422, retail 1.0.2.1 pressing A past menus):

  • Completed full 300 s (exit=0), 0 crashes, 0 device removals, 2,014 PresentX frames, 131,072 translated draws, 660,943 dispatches (44,546 chained split-compute dispatches), 102,631 Opus packets (0 errors).

Not tested

AI use

@oni-swr

oni-swr commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Independent amd-win test report for head 0738c30f4cf8705c953d6cab8f06dff1cca5d3ab (clean detached checkout, no local changes) against its exact parent 889ef3e8aa7f267323ab4879e735e65da6bcef10. Claimed in #1607.

Tested

  • AMD Radeon RX 7800 XT (driver 32.0.31035.1003), Windows 11 Pro 26200, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), debug build, -j 4, synthetic fixtures only.
  • cargo test -p xcl --lib standin -- --test-threads=1 at the head: 7 passed, 0 failed, 0 ignored, no SKIPPED lines. This includes query_interface_on_real_d3d12_pipeline_accepts_hierarchy_and_rejects_other (it ran on the real driver pipeline) and concurrent_shell_miss_reuses_winner_and_releases_loser_pipeline. The concurrent test also passed 20 of 20 repeated single-test runs.
  • cargo test --workspace --no-fail-fast -- --test-threads=4:
commit passed failed ignored SKIPPED lines
head 0738c30f 1416 1 46 7
parent 889ef3e8 1412 1 46 7
  • The only failure at both commits is gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1 (seed 722: res[30] = 0x1, CPU 0x7fffffff). It is a baseline failure unrelated to this diff, which touches only hle/d3d12x/standin.rs. The 4 extra passes at the head match the 4 tests the PR adds.
  • The 7 SKIPPED lines are identical on both sides: 6 in lower_dxil::tests::{f16,f32}_programs_on_the_gpu (per-lane form refused) and heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows). They are not coverage.

Not tested

  • No scripts/smoke.ps1 regression run and no retail title; no game files were used.
  • Other vendors and wave32-only GPUs.
  • No code review is included here.

AI use
Claude Code (Claude Sonnet 5.5) ran the tests and wrote this report; coordinated by Codex (gpt-6.2-sol).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants