Skip to content

dxgi_com: destroy VulkanSwapchain before releasing device_ in ~SwapChain (D3D-818) - #8176

Open
JGonzalezRodriguez wants to merge 1 commit into
ItsNotRexed:mainfrom
JGonzalezRodriguez:fix/d3d-818-swapchain-dtor
Open

JGonzalezRodriguez wants to merge 1 commit into
ItsNotRexed:mainfrom
JGonzalezRodriguez:fix/d3d-818-swapchain-dtor

Conversation

@JGonzalezRodriguez

Copy link
Copy Markdown

Claimed in the coordination issue as: D3D-818 (claim)

What and why

Fixes D3D-818 (found by @ABilenduke): in runtime/shims/dxgi_com.cpp, SwapChain::~SwapChain() called device_->Release() inside its destructor body before C++ ran member destructors. When SwapChain held the last reference to device_ (the D3D12 command queue, which holds Device -> VulkanContext including submit_mutex_, VkDevice, and VkInstance), releasing device_ destroyed VulkanContext and the Vulkan device before swapchain_.~VulkanSwapchain() ran—causing a use-after-free when ~VulkanSwapchain() locked submit_mutex_ and destroyed its swapchain/offscreen images, command pool, surface, and fences.

Changes:

  • runtime/shims/dxgi_swapchain.h: extract ~VulkanSwapchain()'s teardown into an idempotent void destroy() that nulls all Vulkan handles (swapchain_, blit_pool_, blit_cmd_, surface_, acquire_fence_, blit_fence_), resets vector/index state (including guarding !offscreen_memory_.empty()), and sets loaded_ = false, so ~VulkanSwapchain() becomes a no-op that touches neither Vulkan nor submit_mutex_.
  • runtime/shims/dxgi_com.cpp: call swapchain_.destroy() right after unregister_buffers() in ~SwapChain(), and release parent_ (which stores a VulkanContext& without holding a device ref) before device_->Release().
  • runtime/test/dxgi_swapchain_lifetime_test.cpp + runtime/CMakeLists.txt: add CTest target dxgi_swapchain_lifetime covering:
    • Synthetic COM tests (no GPU/display required): direct swapchain holding the last ref, offscreen + waitable + output-restricted swapchain holding the last ref, and partial-init teardown on vkCreateSwapchainKHR and vkAcquireNextImageKHR failures.
    • Real standalone-GPU tests: plain 128x128 present, offscreen + waitable + output-restricted R10G10B10A2_UNORM present, and RTV-cleared + ExecuteCommandLists + present, each releasing factory/device/queue before the final sc->Release().
  • runtime/test/dxgi_swapchain_count_test.cpp: add check_destroy_idempotent() verifying VulkanSwapchain::destroy() frees all handles and touches neither Vulkan nor a freed submit_mutex_ on subsequent destroy() / ~VulkanSwapchain().

(Note: #8033 was opened ~10 minutes after our claim on #7913 without a claim or tests; this PR includes the full fix, reproduction, and both synthetic and real-GPU regression tests.)

Tested

Build: Linux (CachyOS), cross-compiled with clang-cl 23.1.1 + lld-link + xwin 0.10.0 (Windows SDK 10.0.26100), run under wine-11.19. No game files.

Before fix (on main dde82f27e):

  • dxgi_swapchain_lifetime [synthetic]: FAIL: synthetic (direct): VulkanSwapchain called Vulkan after device_ was released (D3D-818)
  • dxgi_swapchain_lifetime [NVIDIA GeForce RTX 4090, driver 610.57.04]: ~Device() completed inside sc->Release() and immediately aborted with exit code 3 before returning from sc->Release().

After fix:

  • NVIDIA GeForce RTX 4090 (driver 610.57.04, Wine 11.19):
    • dxgi_swapchain_lifetime: PASS (4 synthetic + 3 standalone last-ref GPU cases; also PASS with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation)
    • dxgi_swapchain_count: PASS (3 count cases + destroy idempotency)
    • dxgi_device, d3d12_m3_frame, run_tiny_frame: PASS
  • AMD Radeon(TM) Graphics (Raphael RDNA2 / RADV, Mesa 26.2.4), Wine 11.19:
    • dxgi_swapchain_lifetime: PASS (4 synthetic + real-GPU partial-init teardown; WSI swapchain creation on NVIDIA-driven XWayland display returned VkResult -3 so GPU present cases skipped)
    • dxgi_swapchain_count, dxgi_device, d3d12_m3_frame, run_tiny_frame: PASS
  • python3 -I scripts/check_no_game_data.py on all 5 touched files: PASS.

AI-assisted: yes

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (N/A — DXGI/Vulkan runtime lifetime fix, no translator changes)

@lohant27-netizen

Copy link
Copy Markdown

Tested a914ce8ba on Windows 11 (10.0.26300), NVIDIA GeForce RTX 5070 (driver 32.0.16.2101, subgroup 32), MSVC with SDK 10.0.26100, Release: pass. Merged onto main 77fa308f9 together with #8187 and #8146 (no conflicts).

@memorypressure

Copy link
Copy Markdown

Heads tested: a914ce8 vs main dde82f2; recheck clean vs d612f65/867d922f0 (tree 23e19057d). C++/Windows — review-only; CI is the gate. The destructor-order fix matches the root cause: swapchain_.destroy() runs before device_->Release(); destroy() is idempotent and nulls every handle including blit_cmd_ with its pool and loaded_=false, so ~VulkanSwapchain() post-destroy touches neither Vulkan nor submit_mutex_; the destroy_swapchain() hardening (offscreen_memory_ wait, image_initialized_/index resets) is consistent. The synthetic lifetime test drives the real COM path over a fake Vulkan table with a mock last-reference queue — exactly the right shape; the GPU part skips 77. Before/after evidence (exit-3 abort on the 4090, green under the validation layer) is the good pattern. Overlap: conflicts with #8171 in dxgi_swapchain_count_test.cpp — later merger rebases. Flag: no CI checks reported. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

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.

4 participants