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

Open
github-bot wants to merge 1 commit from github-bot/gh-2047 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 18:57 UTC · original
IlyassRS3:rt/d3d12-com-object-atomic-and-private-data-safety → main · +90 −22 · 2 files · 1 commits · open

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).
**@⁠IlyassRS3** on GitHub · 2026-11-25 18:57 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2047) `IlyassRS3:rt/d3d12-com-object-atomic-and-private-data-safety` → `main` · +90 −22 · 2 files · 1 commits · open 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).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:16:00 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 18:57 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 18:57 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2047#pullrequestreview-6063283308) · **reviewed** 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2047/head:github-bot/gh-2047'
git switch 'github-bot/gh-2047'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2047'
git switch 'github-bot/gh-2047'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2047'
git switch 'github-bot/gh-2047'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2047'
git switch 'main'
git merge --squash 'github-bot/gh-2047'
git switch 'main'
git merge --ff-only 'github-bot/gh-2047'
git switch 'main'
git merge 'github-bot/gh-2047'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.