runtime: harden COM refcount ordering, private-data updates, and reentrant release in d3d12_com_object #2047

Closed
leonida/mandy
quietfold wants to merge 0 commits from rt/d3d12-com-object-atomic-and-private-data-safety into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

quietfold commented 2026-11-25 18:57:17 +00:00

Claim ID: RT-D3D12-COM-OBJECT-ATOMIC-AND-PRIVATE-DATA-SAFETY

Summary

Hardens the shared COM base classes (rt::d3d12::Object, rt::d3d12::Child, and rt::d3d12::hand_out) in runtime/shims/d3d12_com_object.h:

  1. Standard COM atomic refcount ordering: AddRef() and try_add_ref() now use std::memory_order_relaxed and Release() uses std::memory_order_acq_rel instead of seq_cst defaults, matching std::shared_ptr, xg_xs.cpp, and d3d12_xs_rootsig.cpp while keeping acquire-release synchronization before deletion / last_release().
  2. In-place private-data overwrite & exception safety: replace() now updates an existing key in place (avoiding vector shifting and reallocation on overwrite), releases the evicted interface outside data_mutex_, and SetPrivateData() / SetPrivateDataInterface() catch std::bad_alloc and return E_OUTOFMEMORY instead of unwinding across STDMETHODCALLTYPE (releasing the added reference if SetPrivateDataInterface fails to grow data_).
  3. Guard SetName length truncation: SetName() now checks wcslen(name) < UINT_MAX / sizeof(wchar_t) before casting (length + 1) * sizeof(wchar_t) to UINT.
  4. Reentrancy-safe release in Child::attach_device(), Child::~Child(), and Object::~Object(): attach_device() and ~Child() update device_ via std::exchange before calling old->Release(), and ~Object() swaps data_ out under data_mutex_ before releasing stored interfaces. Previously, if an evicted device's Release() queried GetDevice(), device_ still pointed to the old device (causing infinite recursion / SIGSEGV in the negative control).
  5. Null guard in hand_out(): Returns E_OUTOFMEMORY and clears *out instead of dereferencing a null object pointer.
  6. Regression test: Extends runtime/test/d3d12_private_data_test.cpp (after the recorded Microsoft answer-table comparison so kPrivateDataAnswers is unchanged) with a reentrant Release() probe verifying lock-free visibility of the replacement entry.

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind)
  • GPUs tested: none (verified locally on macOS arm64 with Apple Clang 17 and minimal Win32/D3D12 COM stubs; Windows MSVC / Vulkan GPU CI verification of d3d12_private_data_test pending)
  • Sanitizers & tests (standalone harness exercising d3d12_com_object.h):
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror: PASS (failures=0)
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror (4 threads x 15,000 concurrent AddRef/try_add_ref/Release + concurrent SetPrivateData/GetPrivateData/SetPrivateDataInterface + deferred last_release() + reentrant attach_device & private-data overwrite + hand_out(nullptr)): PASS (failures=0)
    • -O2 negative control on main: reentrant attach_device() crashed with SIGSEGV (exit 139); fixed branch passed with failures=0.
    • -O2 A/B check (40M AddRef/Release + 3M SetPrivateData overwrites): bit-exact acc=120000000, last=2999999, throughput parity (ref_ms=125.6 ms vs 125.6 ms).
  • Repository checks: python3 -I scripts/check_no_game_data.py clean; python3 -I tests/test_text_write_line_endings.py (3/3 OK).
Claim ID: `RT-D3D12-COM-OBJECT-ATOMIC-AND-PRIVATE-DATA-SAFETY` ### Summary Hardens the shared COM base classes (`rt::d3d12::Object`, `rt::d3d12::Child`, and `rt::d3d12::hand_out`) in `runtime/shims/d3d12_com_object.h`: 1. **Standard COM atomic refcount ordering:** `AddRef()` and `try_add_ref()` now use `std::memory_order_relaxed` and `Release()` uses `std::memory_order_acq_rel` instead of `seq_cst` defaults, matching `std::shared_ptr`, `xg_xs.cpp`, and `d3d12_xs_rootsig.cpp` while keeping acquire-release synchronization before deletion / `last_release()`. 2. **In-place private-data overwrite & exception safety:** `replace()` now updates an existing key in place (avoiding vector shifting and reallocation on overwrite), releases the evicted interface outside `data_mutex_`, and `SetPrivateData()` / `SetPrivateDataInterface()` catch `std::bad_alloc` and return `E_OUTOFMEMORY` instead of unwinding across `STDMETHODCALLTYPE` (releasing the added reference if `SetPrivateDataInterface` fails to grow `data_`). 3. **Guard `SetName` length truncation:** `SetName()` now checks `wcslen(name) < UINT_MAX / sizeof(wchar_t)` before casting `(length + 1) * sizeof(wchar_t)` to `UINT`. 4. **Reentrancy-safe release in `Child::attach_device()`, `Child::~Child()`, and `Object::~Object()`:** `attach_device()` and `~Child()` update `device_` via `std::exchange` before calling `old->Release()`, and `~Object()` swaps `data_` out under `data_mutex_` before releasing stored interfaces. Previously, if an evicted device's `Release()` queried `GetDevice()`, `device_` still pointed to the old device (causing infinite recursion / SIGSEGV in the negative control). 5. **Null guard in `hand_out()`:** Returns `E_OUTOFMEMORY` and clears `*out` instead of dereferencing a null object pointer. 6. **Regression test:** Extends `runtime/test/d3d12_private_data_test.cpp` (after the recorded Microsoft answer-table comparison so `kPrivateDataAnswers` is unchanged) with a reentrant `Release()` probe verifying lock-free visibility of the replacement entry. ### Verification - **AI-assisted:** yes (Antigravity agent by Google DeepMind) - **GPUs tested:** none (verified locally on macOS arm64 with Apple Clang 17 and minimal Win32/D3D12 COM stubs; Windows MSVC / Vulkan GPU CI verification of `d3d12_private_data_test` pending) - **Sanitizers & tests (standalone harness exercising `d3d12_com_object.h`):** - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror`: PASS (`failures=0`) - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror` (4 threads x 15,000 concurrent `AddRef`/`try_add_ref`/`Release` + concurrent `SetPrivateData`/`GetPrivateData`/`SetPrivateDataInterface` + deferred `last_release()` + reentrant `attach_device` & private-data overwrite + `hand_out(nullptr)`): PASS (`failures=0`) - `-O2` negative control on `main`: reentrant `attach_device()` crashed with `SIGSEGV` (exit `139`); fixed branch passed with `failures=0`. - `-O2` A/B check (40M `AddRef`/`Release` + 3M `SetPrivateData` overwrites): bit-exact `acc=120000000`, `last=2999999`, throughput parity (`ref_ms=125.6 ms` vs `125.6 ms`). - **Repository checks:** `python3 -I scripts/check_no_game_data.py` clean; `python3 -I tests/test_text_write_line_endings.py` (3/3 OK).
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 18:57:31 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
zmasi commented 2026-11-28 11:57:58 +00:00

#2047 | head 7d496dde4479bde46dab03a319b64703254c37b6 (merged cleanly with main 4150b1a1cd9de753749e0417ff71a450b6ce92cc) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^d3d12_private_data$" --output-on-failure (LEONIDA_VK_VALIDATION=1) + direct Child::attach_device / hand_out(nullptr) probe | red on main: direct Child::attach_device(nullptr) reentrancy probe fails on main (old->Release() observes stale non-null attached_device(), and hand_out(nullptr) dereferences null; note: replace() on main already called released->Release() after data_.push_back outside data_mutex_, so unmodified d3d12_private_data passes on both main and PR+main) | green on PR+main: d3d12_private_data passes (667/667 host + recorded steps, 0 mismatches, both unmodified and with the attach_device / hand_out probe) on RTX 3090 with 0 Khronos validation errors; check_no_game_data.py 0 violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

#2047 | head 7d496dde4479bde46dab03a319b64703254c37b6 (merged cleanly with main 4150b1a1cd9de753749e0417ff71a450b6ce92cc) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^d3d12_private_data$" --output-on-failure` (`LEONIDA_VK_VALIDATION=1`) + direct `Child::attach_device` / `hand_out(nullptr)` probe | red on main: direct `Child::attach_device(nullptr)` reentrancy probe fails on `main` (`old->Release()` observes stale non-null `attached_device()`, and `hand_out(nullptr)` dereferences null; note: `replace()` on `main` already called `released->Release()` after `data_.push_back` outside `data_mutex_`, so unmodified `d3d12_private_data` passes on both `main` and `PR+main`) | green on PR+main: `d3d12_private_data` passes (`667/667` host + recorded steps, `0` mismatches, both unmodified and with the `attach_device` / `hand_out` probe) on RTX 3090 with `0` Khronos validation errors; `check_no_game_data.py` `0` violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes
OreoFR commented 2026-11-28 21:58:53 +00:00

Verified by OreoFR

  • Base: main c1ecf04ed + PR head 7d496dde4; green run in a merge package of 16 PRs, no new ctest failure vs main
  • Test: ctest -R ^d3d12_private_data$
  • Red on main: OK
  • Green with the PR: OK
  • Verdict: NO RED (COM refcount order / reentrancy: not reproduced on main)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

**Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `7d496dde4`; green run in a merge package of 16 PRs, no new ctest failure vs main - Test: `ctest -R ^d3d12_private_data$` - Red on main: OK - Green with the PR: OK - Verdict: NO RED (COM refcount order / reentrancy: not reproduced on main) - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 16:41:33 +00:00

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.
Sign in to join this conversation.
No description provided.