Skip to content

dxgi: keep private-data AddRef outside the store lock and report out of memory - #6065

Open
l-CatDemon-l wants to merge 4 commits into
ItsNotRexed:mainfrom
l-CatDemon-l:catdemon/dxgi-private-data-reentry
Open

l-CatDemon-l wants to merge 4 commits into
ItsNotRexed:mainfrom
l-CatDemon-l:catdemon/dxgi-private-data-reentry

Conversation

@l-CatDemon-l

Copy link
Copy Markdown

Claimed in the coordination issue as: DXGI private data, the DXGI side of D3D-541

What and why

PrivateDataStore in runtime/shims/dxgi_com.cpp (IDXGIObject private data on the factory, adapter and swapchain, from #3280) had the problems #5290 fixes for the D3D12 objects:

  • GetPrivateData called the stored interface's AddRef while holding the store mutex, so an AddRef that touches the store (or a removal on another thread) waited on foreign code under the lock.
  • std::bad_alloc from copying bytes, creating the owner or growing the vector escaped SetPrivateData / SetPrivateDataInterface across the COM boundary, and inserting a new interface key could leak the reference already added.
  • Factory::RegisterAdaptersChangedEvent let the same bad_alloc escape from its map insertion.

Each stored interface now lives in a shared_ptr owner holding one COM reference. GetPrivateData copies the owner under the lock and calls AddRef outside it, so the object stays alive even if the entry is replaced or removed meanwhile; replaced references are released after unlocking. Allocation failures return E_OUTOFMEMORY and keep the existing entry; RegisterAdaptersChangedEvent returns E_OUTOFMEMORY and advances the cookie only after a successful insertion. Removal, size and DXGI_ERROR_MORE_DATA semantics are unchanged.

Measured native behaviour, for comparison: System32 DXGI (Windows 11 x64) also hands the interface back through AddRef, but does not hold its lock across it and does not pin the stored reference. A SetPrivateData on the same key from inside AddRef, or a concurrent removal from another thread (it completed in 0 ms while the reader was inside AddRef), drops the stored reference to 0 while AddRef is still running, and GetPrivateData then returns S_OK with data that is no longer the interface pointer, leaking the added reference. The layer keeps the object alive and returns the pointer it added a reference to, so the test's --native mode only checks that both scenarios complete.

Tested

Windows 11, VS 2022 (MSVC 19.44), RelWithDebInfo, NVIDIA GeForce RTX 4070 Ti, driver 616.56. The new dxgi_private_data_reentry test needs no GPU: it drives the shim's own factory, runs the reentry and concurrent-removal cases in child processes with a 10 s watchdog, and injects allocation failures with a test-local operator new.

  • Negative control, main (3faac55) with only the new test and its CMake entry: 11 of 12 checks fail. The reentrant child exits 0xC0000409, the concurrent-removal child fails, and bad_alloc escaped the DXGI call for every allocation case; only the four-thread reference-count check passes.
  • This branch: ctest -R ^dxgi_private_data_reentry$ --repeat until-fail:30 30/30, all 12 checks pass; dxgi_swapchain passes (45 swapchain descriptions compared with the host runtime, 0 mismatches).
  • dxgi_private_data_reentry_test.exe --native (System32 DXGI): exit 0, native: completes (layer-only guarantees not checked).
  • Independent re-run on a second build tree with separate probes (store extracted from dxgi_com.cpp, failure injected at each allocation, four threads): all pass on this branch, 20 of 20 runs; on main they show the escaping bad_alloc, a leaked reference, and the reentry failure.
  • Merges cleanly with current main, with D3D-541: avoid holding the private-data lock during AddRef #5290 and with Runtime: swapchain back buffers keep names and private data (D3D-451) #4317.

Synthetic COM objects only, no game files.

AI-assisted: yes (Codex and Claude Code)

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, no translator change)

l-CatDemon-l and others added 3 commits November 23, 2026 14:47
GetPrivateData calls AddRef under the store mutex, so a callback
that accesses the store can deadlock. An allocation failure during
an interface insertion can also leak the incoming COM reference
and let bad_alloc cross the API boundary.

Keep each stored reference in a shared owner and take a snapshot
before calling AddRef outside the lock. Release replaced references
after unlocking. Replace existing entries in place and report
E_OUTOFMEMORY without changing the old value.

Add a factory regression for reentry, concurrent removal and
replacement, and allocation failures on insertion and replacement.
Add an opt-in System32 DXGI comparison with bounded child runs.
Windows build and test runs are pending the shared machine slot.

Co-Authored-By: Codex <noreply@openai.com>
An allocation failure inserting an adapter notification into the
factory's event map can escape the COM method. Translate it to
E_OUTOFMEMORY and advance the cookie only after insertion succeeds.

Extend dxgi_private_data_reentry_test with an injected insertion
failure, an unchanged output cookie, and a successful retry and
unregister. Keep the native comparison focused on whether both
callbacks complete, and check reference ownership on the layer.

On RTX 4070 Ti (616.56), the Windows negative control failed as
expected. The fix passed 30 repeated regressions and dxgi_swapchain.
The updated native comparison still needs a Windows pass.

Co-Authored-By: Codex <noreply@openai.com>
Record that System32 DXGI calls AddRef without pinning the stored
reference, which the layer does, and note that the OOM fault-injection
counts follow the release build's allocation order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thehollowedone

Copy link
Copy Markdown

Independent Windows CPU check of d917d7bc, targeting 561b65a8. Windows 11 x64 build 26300, MSVC 19.38.33145, Windows SDK 10.0.26100, Vulkan headers 1.4.363, RelWithDebInfo.

Stock dxgi_private_data_reentry passed all 12 checks and 10 consecutive CTest repeats. The checks covered interface ownership during reentry and concurrent removal, allocation failures, and adapter-event registration retry.

This run used the app-local layer and synthetic COM objects. CPU only. AI-assisted.

@milas6669

Copy link
Copy Markdown

Checked head d917d7bca on Windows 11, MSVC 19.44, RelWithDebInfo, merged onto main 561b65a8 with no conflicts. dxgi_private_data_reentry passes. A full build and ctest -R "dxgi|d3d12_private|private_data|d3d12_com" is 7/7 (d3d12_private_data, dxgi_swapchain, dxgi_swapchain_count, d3d12_compute included). It also merges with #5290 without a conflict (git merge-tree; the only shared file is runtime/CMakeLists.txt).

New data: I mutated runtime/shims/dxgi_com.cpp one line at a time and rebuilt the test:

Mutation Result
lock.unlock() removed, so AddRef runs under the store lock caught: AddRef reentry and replacement and Concurrent removal during AddRef fail
stored reference not pinned in get() caught, same two tests
set, set_interface or RegisterAdaptersChangedEvent lets bad_alloc escape caught (all four OOM cases per function)
cookie advanced before the insert caught: Adapter event registration recovers from OOM
the replaced interface is released while the lock is still held (released declared inside the lock scope in store()) survives: exit 0

So the claim that "replaced references are released after unlocking" is not tested. The test covers AddRef outside the lock, but not Release of a replaced or removed entry: a stored object whose Release calls back into GetPrivateData / SetPrivateData on the same object would deadlock on std::mutex with that mutation, and nothing in the test does that. One case would pin it down: an interface whose Release calls SetPrivateData on the same store, then replace its entry from another key's call.

No defect in the PR itself. No game data, no GPU used. AI-assisted (Claude Code).

milas6669's mutation testing on ItsNotRexed#6065 found one mutation the test let
through: declaring `released` inside store()'s lock scope, so a replaced or
removed interface is released while the store's mutex is held. Add an
interface whose Release reads and writes the same store, replace its entry
with bytes for the same key and, separately, remove it. Each runs in a child
process under the watchdog like the AddRef cases, since with that mutation
the callback relocks the store's std::mutex on the same thread. The test
checks the callback's results, the entry afterwards and that the reference
count is back to the caller's own.

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@l-CatDemon-l

Copy link
Copy Markdown
Author

Thanks @milas6669, that was the gap. a6c9be2 adds an interface whose Release reads and writes the same store
(GetPrivateData of another key, SetPrivateData of a third). Its entry is replaced by bytes for the same key and,
separately, removed. Like the AddRef cases, each runs in a child process under the 10 s watchdog. The test checks
the callback's results, the entry afterwards and that the reference count is back to 1.

With your mutation (released declared inside store()'s lock scope), both new cases fail in 3/3 runs:
Release of a replaced interface reenters the store: FAIL and Release of a removed interface reenters the store: FAIL, each child exiting with 0xC0000409 (the reentrant lock of the store's std::mutex ends the process). Every
other case still passes there, so these two are the ones that catch it. Without the mutation the whole test passes
3/3 directly and 10/10 under ctest --repeat until-fail:10, and dxgi_swapchain passes (Windows 11, MSVC 19.44,
RelWithDebInfo, RTX 4070 Ti).

AI-assisted.

@Ingenioeren

Copy link
Copy Markdown
Contributor

Tested a6c9be2 on Windows 11 build 26200, MSVC 19.43, Windows SDK 10.0.26100.0, and Radeon RX 6750 XT (Windows driver 32.0.21045.29003). RelWithDebInfo, synthetic inputs, AI-assisted.

  • dxgi_private_data_reentry and dxgi_swapchain: 2/2 passed.
  • dxgi_private_data_reentry passed 30 consecutive runs on this head, including the new replacement-release coverage.
  • dxgi_private_data_reentry_test.exe --native: exit 0. The native mode checks completion and skips the layer-only guarantees.

@Alpaca-exe

Copy link
Copy Markdown
Contributor

AI-assisted Windows hardware verification on AMD Radeon RX 6750 XT (0x1002:0x73df, RDNA2, Windows 11, AMD proprietary driver 26.11.2 / 32.0.21045.29003, Vulkan 1.4.315, MSVC 19.44.35230.0, RelWithDebInfo, VK_LOADER_LAYERS_DISABLE=~implicit~):

Built and ran from commit a6c9be2b3 (rc=0):

  • dxgi_private_data_reentry_test.exe (rc=0): all 14 checks passed (AddRef reentry and replacement, Concurrent removal during AddRef, Release reentry on replaced/removed interfaces, all 8 OOM guards, Concurrent updates keep reference counts balanced, Adapter event registration recovers from OOM).
  • d3d12_private_data_test.exe (rc=0): 667 steps compared with the host (5 more the host faults on), 0 mismatches.

Synthetic tests only; no game files.

@trick-ye

Copy link
Copy Markdown

Tested a6c9be2b on AMD Radeon RX 7800 XT (RDNA3), AMD 26.11.2 (Windows driver 32.0.32029.2010), Windows 11 26200: pass.

  • The head is based on 3faac557, and I tested it as it is, not merged with current main.
  • Full build, then ctest -R d3d12_ -j 1 plus the new test: 69 tests, 67 pass, 2 skipped (d3d12_sparse_queue_family_contract, d3d12_first_light), 0 fail. The new dxgi_private_data_reentry passes.
  • I did not run the test's --native mode separately, and I have no run of 3faac557 itself on this card to compare with.
  • MSVC 14.42 (VS 2022), Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, llvm-mc 23.1.6, RelWithDebInfo, LEONIDA_VK_DEVICE=7800. A test run, not a code review. Synthetic tests only, 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