Skip to content

d3d12: enforce cross-device ownership checks on queue, fence, and bundle paths - #9715

Closed
DenRakEiw wants to merge 1 commit into
ProjectLeonida:mainfrom
DenRakEiw:fix/d3d12-cross-device-ownership-checks
Closed

DenRakEiw wants to merge 1 commit into
ProjectLeonida:mainfrom
DenRakEiw:fix/d3d12-cross-device-ownership-checks

Conversation

@DenRakEiw

Copy link
Copy Markdown
Contributor

Claim: #9522 (comment)

Summary

Enforce cross-device ownership checks before passing D3D12 COM objects into Vulkan on five queue, fence, and command-list paths, and guard the blocking path of set_event_on_multiple_fence_completion against an already-lost device:

  • CommandQueue::Signal & CommandQueue::Wait (runtime/shims/d3d12_com.cpp): verify the fence belongs to the same VulkanContext and attached_device(); return E_INVALIDARG before claiming or submitting to Vulkan.
  • ID3D12Device1::SetEventOnMultipleFenceCompletion (runtime/shims/d3d12_com.h, runtime/shims/d3d12_com.cpp, runtime/shims/d3d12_vulkan_device.cpp): validate every fence in the array against context and device (E_INVALIDARG on any foreign fence, including mixed arrays); on the blocking (event == nullptr) path, return DXGI_ERROR_DEVICE_REMOVED immediately if context.is_device_lost() (and when vkWaitSemaphores returns VK_ERROR_DEVICE_LOST).
  • CommandQueue::ExecuteCommandLists (runtime/shims/d3d12_com.cpp): refuse the entire batch (ReplayResult::unsupported) if any list has a mismatched attached_device(), never submitting foreign handles to VulkanContext.
  • GraphicsCommandList::ExecuteBundle (runtime/shims/d3d12_com.cpp): invalidate the recording list via check(false) if bundle->attached_device() != attached_device(), so Close() returns E_INVALIDARG and no foreign bundle commands are replayed.

Standalone tests (attached_device() == nullptr on both objects) continue to pass cleanly.

Live Windows 11 D3D12 Verification

Empirically probed against two distinct host adapters (NVIDIA GeForce RTX 5090 + Microsoft Basic Render Driver / WARP):

  • queue_A->Signal(fence_B, 1) -> 0x80070057 (E_INVALIDARG), fence_B unchanged
  • queue_A->Wait(fence_B, 1) -> 0x80070057 (E_INVALIDARG)
  • dev_A->SetEventOnMultipleFenceCompletion({fence_B} / {fence_A, fence_B}, ALL/ANY, event/null) -> 0x80070057 (E_INVALIDARG)
  • Retail host ExecuteBundle / ExecuteCommandLists with foreign objects fault without the debug layer; the layer safely refuses both.

Testing

Added runtime/test/d3d12_cross_device_validation_test.cpp (registered as d3d12_cross_device_validation in runtime/CMakeLists.txt with SKIP_RETURN_CODE 77 and FAIL_REGULAR_EXPRESSION "Validation Error;VUID-").

Verified on NVIDIA GeForce RTX 5090 with LEONIDA_VK_VALIDATION=1 (0 VUIDs):

  • d3d12_cross_device_validation: PASS (40/40 checks, including live RTX 5090 + WARP host parity & device-lost blocking check)
  • d3d12_com: PASS
  • d3d12_fence_timing: PASS
  • d3d12_bundle: PASS

AI-assisted (Gemini 4 Neon).

@Grimkaaah Grimkaaah 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.

Independent review (Linux CPU) — PASS (code-only; dual-device Vulkan not run)

Checked

  • Cross-device ownership checks on ExecuteBundle, ExecuteCommandLists, Signal/Wait, and SetEventOnMultipleFenceCompletion (device pointer threaded through); device-lost → DXGI_ERROR_DEVICE_REMOVED on the blocking multi-fence path.
  • New d3d12_cross_device_validation_test.cpp covers foreign/mixed lists and fences; CMake entry present.
  • No Linux case-path issues.

Overlap: runtime/CMakeLists.txt + d3d12_vulkan_device.cpp with #9719.

Not executed here: dual Vulkan devices / live Windows D3D12 parity (host skipped under Wine per test comments).

No game files. Reviewer: Grimkeh (AI-assisted Cursor / Composer).

@adriaespii

Copy link
Copy Markdown
Contributor

Tested d175e11ae merged onto main 5d74c27 on Windows 11 Pro 24H2 (build 26100), AMD Radeon RX 9070 (RDNA4 / GFX1201), AMD Software 26.10.1 (driver 32.0.21045.29003, Vulkan 1.4.359), MSVC 14.44 (VS 2022 Community 17.14.40), RelWithDebInfo: pass.

  • d3d12_cross_device_validation_test (LEONIDA_VK_VALIDATION=1 and

@MatyEjsr

Copy link
Copy Markdown

AI-assisted code review and native Windows 11 / RTX 3090 verification (synthetic inputs only, no game data).

Depends on: none

Verification Summary — PASS

  • Base / head: origin/main (5d74c2723) fast-forwarded cleanly to PR d3d12: enforce cross-device ownership checks on queue, fence, and bundle paths #9715 head 3afcab2a8b5776e60c6e76b5e7ff8af43cb8f4a1.
  • Environment: Windows 11 (10.0.26200), MSVC 19.44.35230 (RelWithDebInfo, Ninja, 8 parallel jobs), Vulkan SDK 1.4.363.0 (instance 1.4.341), NVIDIA GeForce RTX 3090 (driver 617.42, Vulkan API 1.4.351) + Microsoft Basic Render Driver (WARP).

Checks Run

  1. No-game-data check:
    • python -I scripts/check_no_game_data.py on all 5 touched files (runtime/CMakeLists.txt, runtime/shims/d3d12_com.cpp, runtime/shims/d3d12_com.h, runtime/shims/d3d12_vulkan_device.cpp, runtime/test/d3d12_cross_device_validation_test.cpp): pass.
  2. Full runtime build (-j 8 / --parallel 8):
    • cmake -S runtime -B runtime/build + cmake --build runtime/build --config RelWithDebInfo --parallel 8: 188/188 targets built, 0 warnings in the touched files under /W4 /permissive- /utf-8.
  3. New test (d3d12_cross_device_validation) with Vulkan validation:
    • Executed directly and via CTest with LEONIDA_VK_VALIDATION=1 (and also with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation): 40/40 PASS (0 failure(s), 0 validation errors / VUIDs).
    • Verified all 5 layer cross-device rejection + same-device positive paths (CommandQueue::Signal, CommandQueue::Wait, ID3D12Device1::SetEventOnMultipleFenceCompletion for foreign-only and mixed {f_A, f_B} arrays across ALL/ANY and event/blocking modes, CommandQueue::ExecuteCommandLists single and mixed batches, and GraphicsCommandList::ExecuteBundle), the blocking lost-device DXGI_ERROR_DEVICE_REMOVED check, and live Windows 11 D3D12 host parity (RTX 3090 + WARP, 8/8 checks matching E_INVALIDARG).
  4. Focused D3D12 queue/fence/bundle/proxy CTests (LEONIDA_VK_VALIDATION=1, -j 8):
    • d3d12_cross_device_validation, d3d12_com, d3d12_fence_timing, d3d12_fence_events, d3d12_fence_device_lost_counter, d3d12_bundle, d3d12_bundle_forbidden, d3d12_queue_list_type, d3d12_queue_handover, d3d12_queue_wait, d3d12_queue_share, d3d12_close_parity, d3d12_set_descriptor_heaps, queue_proxy, allocator_proxy: 15/15 passed.
  5. Full runtime CTest suite (ctest --test-dir runtime/build -C RelWithDebInfo -j 8 --output-on-failure):
    • 263/265 non-skipped tests passed (12 skipped/disabled).
    • The 2 failures (d3d12_vulkan_device at line 6574 tiles == 1 and d3d12_depth_resolve) were re-run on unmodified origin/main (5d74c2723) and reproduce identically there; no regressions from this PR.

Code Review Notes

  • Ownership checks in runtime/shims/d3d12_com.cpp and runtime/shims/d3d12_vulkan_device.cpp cleanly reject foreign objects before claiming fence values or touching Vulkan handles, while preserving standalone (attached_device() == nullptr) test paths and still guarding cross-context standalone fences via f->context() != &context.
  • The context.is_device_lost() early return and VK_ERROR_DEVICE_LOST -> DXGI_ERROR_DEVICE_REMOVED mapping on the blocking set_event_on_multiple_fence_completion path prevent a hang when the device is already lost.
  • Non-blocking follow-up note: create_command_list (runtime/shims/d3d12_com.cpp:1958), GraphicsCommandList::Reset (runtime/shims/d3d12_com.cpp:225), and ID3D12Device3::EnqueueMakeResident (runtime/shims/d3d12_vulkan_device.cpp:5378) do not yet enforce owning-device checks on their allocator/fence arguments; those are outside this PR's five scoped paths and can be tracked separately if desired.

@corpse240

Copy link
Copy Markdown

Tested 3afcab2a8 (it already contains main 5d74c2723) on Intel Arc B580 (Battlemage, 0xe20b), Intel Windows driver 101.9150, Vulkan 1.4.359, Windows 11 Pro 26200; RelWithDebInfo, VS 2022 Build Tools, Vulkan SDK 1.4.363.0: pass.

AI-assisted (Claude Code); no game files.

@Dezmonius

Copy link
Copy Markdown

checked: #9715 at head 3afcab2a8b5776e60c6e76b5e7ff8af43cb8f4a1, tested on GitHub's merge commit 4a64a05e against main@5d74c272.

Windows 10 22H2 (19045.6466), RTX 5070 (NVIDIA 610.88, Vulkan subgroup 32), MSVC 19.51.36256, Windows SDK 10.0.26100.0, Ninja Release with static CRT.

  • d3d12_cross_device_validation: 40/40 checks passed, including the native D3D12 parity checks, lost-device path, and same-device controls.
  • d3d12_com and d3d12_fence_timing: both passed. Overall CTest result: 3/3 passed.
  • The successful run used the RTX 5070 with no game files and without VK_LAYER_KHRONOS_validation, which is unavailable on this machine.

AI-assisted: yes.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants