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

Open
opened 2026-11-26 21:19:52 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2047
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: rt/d3d12-com-object-atomic-and-private-data-safety


📝 Commits (1)

  • 7d496dd runtime: harden COM refcount ordering, private-data updates, and reentrant release in d3d12_com_object

📊 Changes

2 files changed (+90 additions, -22 deletions)

View changed files

📝 runtime/shims/d3d12_com_object.h (+47 -22)
📝 runtime/test/d3d12_private_data_test.cpp (+43 -0)

📄 Description

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).

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2047 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `rt/d3d12-com-object-atomic-and-private-data-safety` --- ### 📝 Commits (1) - [`7d496dd`](https://github.com/ubiquitarianism/leonida/commit/7d496dde4479bde46dab03a319b64703254c37b6) runtime: harden COM refcount ordering, private-data updates, and reentrant release in d3d12_com_object ### 📊 Changes **2 files changed** (+90 additions, -22 deletions) <details> <summary>View changed files</summary> 📝 `runtime/shims/d3d12_com_object.h` (+47 -22) 📝 `runtime/test/d3d12_private_data_test.cpp` (+43 -0) </details> ### 📄 Description 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). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#2053
No description provided.