Skip to content

dxgi: return E_ACCESSDENIED for occupied or invalid HWNDs on swap chain creation - #8929

Closed
emirtugra-dag wants to merge 1 commit into
ProjectLeonida:mainfrom
emirtugra-dag:fix/dxgi-swapchain-accessdenied-1123-2240
Closed

emirtugra-dag wants to merge 1 commit into
ProjectLeonida:mainfrom
emirtugra-dag:fix/dxgi-swapchain-accessdenied-1123-2240

Conversation

@emirtugra-dag

Copy link
Copy Markdown

Claimed in the coordination issue as: #8683 (comment) (dxgi: return E_ACCESSDENIED from CreateSwapChainForHwnd / CreateSwapChain on a window with an existing swap chain, a destroyed window, or an invalid HWND, from @Maraco91's finding in #8683)
Depends on: none

What and why

  • In runtime/shims/dxgi_com.cpp, Factory::create_swap_chain previously passed invalid, destroyed, or already-occupied HWNDs straight through to VulkanSwapchain::create, returning E_FAIL (0x80004005) (or succeeding depending on Vulkan surface behavior) instead of host DXGI's E_ACCESSDENIED (0x80070005).
  • Added process-wide active-HWND tracking via HwndClaim (declared before SwapChain::swapchain_ so destructor teardown destroys VkSwapchainKHR/VkSurfaceKHR before freeing the HWND slot for reuse).
  • In Factory::create_swap_chain, after all descriptor/format/buffer/sample validations pass (preserving DXGI_ERROR_INVALID_CALL and E_INVALIDARG priority), checks !IsWindow(hwnd) and !sc->claim_hwnd(hwnd) and returns E_ACCESSDENIED with *out == nullptr. Because IDXGIFactory::CreateSwapChain delegates to CreateSwapChainForHwnd, both entry points (and CreateSwapChainForCoreWindow) match host DXGI.
  • Added host-runtime comparison cases (existing swap chain on window, destroyed window, garbage window, and descriptor-error precedence cases) and explicit assertions (including cross-factory collision, legacy CreateSwapChain, release-then-recreate, and *out == nullptr) in runtime/test/dxgi_swapchain_test.cpp.

Tested

  • Hardware & toolchain: Windows 11, MSVC 19.52, Vulkan SDK 1.4.363.1, AMD Radeon RX 7600 (validation off; no game files).
  • Red before fix (on upstream/main + new test): dxgi_swapchain_test.exe failed with 3 mismatches against host Windows dxgi.dll:
    • CreateSwapChainForHwnd existing swap chain on window: ours 0x80004005, host 0x80070005
    • CreateSwapChainForHwnd destroyed window: ours 0x80004005, host 0x80070005
    • CreateSwapChainForHwnd garbage window: ours 0x80004005, host 0x80070005
  • Green after fix (13d8cdab4):
    • runtime/build/dxgi_swapchain_test.exe: CreateSwapChainForHwnd: 52 descriptions compared with the host runtime, 0 mismatches; PASS.
    • runtime/build/dxgi_swapchain_count_test.exe: PASS (3 cases).
    • runtime/build/dxgi_device_test.exe: 3 backend(s) passed.
    • ctest --test-dir runtime/build -R "dxgi|present|frame" --output-on-failure: 11/11 passed (100%).
    • python -I scripts/check_no_game_data.py runtime/shims/dxgi_com.cpp runtime/test/dxgi_swapchain_test.cpp: exit 0.

AI-assisted: yes

Checklist

  • Synthetic tests only; no game data, keys, or online service calls
  • Verified fail-first against host dxgi.dll and green after fix on AMD Radeon RX 7600
  • Branch isolation preserved (unrelated open PR branches left untouched)

@altLov3r

Copy link
Copy Markdown

Tested 13d8cdab4 on Windows 11 with Intel Arc A770 Graphics (discrete Alchemist / Xe-HPG, driver 32.0.101.9150, Vulkan 1.4.359, MSVC 19.51.36257.0, Windows SDK 10.0.26100; synthetic tests only, AI-assisted):

Results (2/2 PASS)

  • dxgi_swapchain: PASS (1.20 s)
    • CreateSwapChainForHwnd: 52 descriptions compared with the host runtime, 0 mismatches
    • dxgi_swapchain_test: PASS (CreateSwapChain, CreateSwapChainForHwnd, CreateSwapChainForCoreWindow, occlusion status, GetBuffer+GetDesc, clear+readback, Present, ResizeBuffers; 0 validation errors)
    • Note: Vulkan validation layer (VK_LAYER_KHRONOS_validation) is not installed on this machine (validation off).
  • dxgi_device: PASS (0.25 s, 3 backend(s) passed: host WARP, host hardware Intel(R) Arc(TM) A770 Graphics, Vulkan Intel(R) Arc(TM) A770 Graphics)

@abnalstlll-byte abnalstlll-byte 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 Windows host review (commit \13d8cdab4337dd711bb7d23a16357b572a4f084e)

What changed:


  • untime/shims/dxgi_com.cpp:
    • Implements thread-safe \HwndClaim\ via process-wide active window set to track live swap chains across factories.
    • In \Factory::create_swap_chain, checks !IsWindow(hwnd)\ and returns \E_ACCESSDENIED\ (\

@Zipettte

Copy link
Copy Markdown

Tested on AMD Radeon RX 7700 XT (RDNA3), AMD Windows driver 32.0.32029.2010, Windows 11, MSVC 14.51 Release, at 13d8cda. Full ctest -j 8, out of 239 tests:

No regression. The only failures are the same 5 as on main dde82f2 on this card (vk_queue_query_sync, d3d12_vulkan_device tiles == 1, d3d12_texture_tiles, d3d12_depth_resolve, d3d12_pipeline_library_host; tracked in #8426). Every test this PR adds or touches passes.

AI-assisted (Claude Code).

@RAINighty

Copy link
Copy Markdown

Tested 13d8cdab4 merged with main 867d922f0 (clean merge) on Windows 11 (10.0.26300), Intel Core i7-14650HX, hybrid-GPU laptop (RTX 5060 + Intel UHD, no adapter forced), MSVC 19.44.35228, RelWithDebInfo (Ninja): pass.

  • dxgi_swapchain and dxgi_swapchain_count pass with this PR (2 of 2).
  • Negative control: this PR's dxgi_swapchain_test.cpp with main's runtime/shims/dxgi_com.cpp: dxgi_swapchain fails, dxgi_swapchain_count still passes, so the new test does catch the change.
  • Overlap: dxgi: resolve monitor ownership without creating a native factory per lookup #8620 edits the same two files.
  • Code not reviewed beyond running it. No game files.

AI-assisted (Claude Code).

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.

6 participants