GPU: saturate COM AddRef against u32 wrap and add unit tests for object.rs #931

Open
opened 2026-11-26 04:14:23 +00:00 by github-bot · 5 comments
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-24 14:18 UTC · original

Summary

Fixes a COM reference-count wrap bug in crates/victory-gpu/src/object.rs (add_ref using fetch_add could wrap u32::MAX -> 0) and adds 17 deterministic synthetic unit tests (18 total in object.rs, previously 0).

Because external PR creation is currently restricted (collaborators_only), the CI-green branch is ready on my fork for cherry-pick / review:

  • Branch: geiyiliyabumo/VIctory@test/gpu-object-tests
  • Commit: geiyiliyabumo/VIctory@b82fb12 (based on main ffe310e)

Changes (crates/victory-gpu/src/object.rs)

  • Fix: add_ref now saturates at u32::MAX via fetch_update + checked_add, preserving the COM invariant that AddRef never wraps to 0.
  • Tests (+17 synthetic): covers AddRef saturation, Release underflow floor, last-ref free & private-data cleanup, hand_out accept/reject/null-out drop paths, answer result clearing/refcount, IID matching (incl. null), GetPrivateData/SetPrivateData, SetName, d3d12_object vtable slots, and parent_device.

Verification (Windows 11 x64 MSVC)

  • rustfmt --edition 2024 --check crates/victory-gpu/src/object.rs: PASS
  • cargo xtask policy: PASS (154 files ok)
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings (default + --features diagnostics): PASS (0 warnings)
  • cargo test -p victory-gpu --locked -- object::: 18 passed, 0 failed

AI-assisted; synthetic tests only, no game files.

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 14:18 UTC · [original](https://github.com/Marvisak/VIctory/issues/931) ### Summary Fixes a COM reference-count wrap bug in `crates/victory-gpu/src/object.rs` (`add_ref` using `fetch_add` could wrap `u32::MAX -> 0`) and adds 17 deterministic synthetic unit tests (18 total in `object.rs`, previously 0). Because external PR creation is currently restricted (`collaborators_only`), the CI-green branch is ready on my fork for cherry-pick / review: - **Branch**: `geiyiliyabumo/VIctory@test/gpu-object-tests` - **Commit**: `geiyiliyabumo/VIctory@b82fb12` (based on `main` `ffe310e`) ### Changes (`crates/victory-gpu/src/object.rs`) - **Fix**: `add_ref` now saturates at `u32::MAX` via `fetch_update` + `checked_add`, preserving the COM invariant that `AddRef` never wraps to `0`. - **Tests (+17 synthetic)**: covers `AddRef` saturation, `Release` underflow floor, last-ref free & private-data cleanup, `hand_out` accept/reject/null-out drop paths, `answer` result clearing/refcount, IID matching (incl. null), `GetPrivateData`/`SetPrivateData`, `SetName`, `d3d12_object` vtable slots, and `parent_device`. ### Verification (Windows 11 x64 MSVC) - `rustfmt --edition 2024 --check crates/victory-gpu/src/object.rs`: **PASS** - `cargo xtask policy`: **PASS** (`154 files ok`) - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` (default + `--features diagnostics`): **PASS** (0 warnings) - `cargo test -p victory-gpu --locked -- object::`: **18 passed, 0 failed** *AI-assisted; synthetic tests only, no game files.*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:23 +00:00
Author
Owner

@⁠DaniiL35 on GitHub · 2026-11-24 14:41 UTC · original

Independent Verification Review: geiyiliyabumo:test/gpu-object-tests (b82fb12)

Audited the COM reference-count saturation logic and object vtable tests in crates/victory-gpu/src/object.rs:


1. COM Lifetime Symmetry & Saturation Invariant

  • In release (line 77), reference decrements were already floored at zero using fetch_update(..., |n| n.checked_sub(1)).
  • In add_ref (line 71), previous code performed an unconstrained fetch_add(1). If refcounts wrapped u32::MAX -> 0, the object appeared zero-referenced and became vulnerable to premature drop or resource reclamation while active callers held references.
  • Symmetric Saturation:
    let current = match refs.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |n| n.checked_add(1)) {
        Ok(n) | Err(n) => n,
    };
    current.saturating_add(1)
    
    When n == u32::MAX, checked_add(1) returns None, preserving u32::MAX in the atomic without wrapping, and current.saturating_add(1) returns u32::MAX. This mirrors the standard COM lifetime convention that AddRef never wraps to 0.

2. Test Suite & Cross-Platform Execution

  • Expanded object.rs test coverage from 0 to 18 unit tests.
  • Uses Tracked wrapper with Rc<Cell<usize>> to verify destruction timing and prevent memory leaks.
  • Covers AddRef saturation, Release floor at zero, private-data cleanup on drop, hand_out drop safety on null pointers, and vtable slot offset alignments.
  • Cross-Platform: Because object.rs contains zero Windows D3D12 host device dependencies, all 18 tests run cleanly across non-Windows environments (macOS / Linux) as part of cargo test -p victory-gpu.
  • Policy Compliance: 100% synthetic in-memory fixtures, clean under Rust 2024 edition and Rule #1.

Ready to merge cleanly onto main.

**@⁠DaniiL35** on GitHub · 2026-11-24 14:41 UTC · [original](https://github.com/Marvisak/VIctory/issues/931#issuecomment-7134798988) ### Independent Verification Review: geiyiliyabumo:test/gpu-object-tests (`b82fb12`) Audited the COM reference-count saturation logic and object vtable tests in `crates/victory-gpu/src/object.rs`: --- ### 1. COM Lifetime Symmetry & Saturation Invariant - In `release` (line 77), reference decrements were already floored at zero using `fetch_update(..., |n| n.checked_sub(1))`. - In `add_ref` (line 71), previous code performed an unconstrained `fetch_add(1)`. If refcounts wrapped `u32::MAX -> 0`, the object appeared zero-referenced and became vulnerable to premature drop or resource reclamation while active callers held references. - **Symmetric Saturation**: ```rust let current = match refs.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |n| n.checked_add(1)) { Ok(n) | Err(n) => n, }; current.saturating_add(1) ``` When `n == u32::MAX`, `checked_add(1)` returns `None`, preserving `u32::MAX` in the atomic without wrapping, and `current.saturating_add(1)` returns `u32::MAX`. This mirrors the standard COM lifetime convention that `AddRef` never wraps to 0. --- ### 2. Test Suite & Cross-Platform Execution - Expanded `object.rs` test coverage from 0 to 18 unit tests. - Uses `Tracked` wrapper with `Rc<Cell<usize>>` to verify destruction timing and prevent memory leaks. - Covers `AddRef` saturation, `Release` floor at zero, private-data cleanup on drop, `hand_out` drop safety on null pointers, and vtable slot offset alignments. - **Cross-Platform**: Because `object.rs` contains zero Windows D3D12 host device dependencies, all 18 tests run cleanly across non-Windows environments (macOS / Linux) as part of `cargo test -p victory-gpu`. - **Policy Compliance**: 100% synthetic in-memory fixtures, clean under Rust 2024 edition and Rule #1. Ready to merge cleanly onto `main`.
Author
Owner

@⁠NethminaYasas on GitHub · 2026-11-24 15:31 UTC · original

Claiming this one: I'll mirror
elease's \checked_sub\ with a saturating \AddRef\ in \crates/victory-gpu/src/object.rs\ and add a focused unit test that sets the refcount to \u32::MAX, then open a PR. I saw the independent verification review of \geiyiliyabumo:test/gpu-object-tests\ above — if that branch is about to become an upstream PR, please say so and I'll defer to it. This work is AI-assisted (OpenCode) and uses no game data.

**@⁠NethminaYasas** on GitHub · 2026-11-24 15:31 UTC · [original](https://github.com/Marvisak/VIctory/issues/931#issuecomment-7136169399) Claiming this one: I'll mirror \ elease\'s \checked_sub\ with a saturating \AddRef\ in \crates/victory-gpu/src/object.rs\ and add a focused unit test that sets the refcount to \u32::MAX\, then open a PR. I saw the independent verification review of \geiyiliyabumo:test/gpu-object-tests\ above — if that branch is about to become an upstream PR, please say so and I'll defer to it. This work is AI-assisted (OpenCode) and uses no game data.
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 15:44 UTC · original

Carried to #1100 with @⁠geiyiliyabumo's commit b82fb12 unchanged and its authorship kept.

Checked here on ffe310e (Windows 11, Rust 1.98.1): cargo test -p victory-gpu 67 passed / 1 ignored (50 before), clippy clean with --all-features, cargo build --release -p victory builds.

Red/green on the fix: putting the old fetch_add(1) + 1 back fails add_ref_saturates_instead_of_wrapping — as an overflow panic in a debug test run, and by wrapping the count to 0 in a release build, which is the real failure the issue describes.

One thing to keep in mind if the tests are extended: UNREFERENCED and SATURATED are static objects, one per test, so they do not race today, but a second test on either static would need to serialise against the first.

**@⁠adamdotpy** on GitHub · 2026-11-24 15:44 UTC · [original](https://github.com/Marvisak/VIctory/issues/931#issuecomment-7136526551) Carried to #1100 with @⁠geiyiliyabumo's commit `b82fb12` unchanged and its authorship kept. Checked here on `ffe310e` (Windows 11, Rust 1.98.1): `cargo test -p victory-gpu` 67 passed / 1 ignored (50 before), clippy clean with `--all-features`, `cargo build --release -p victory` builds. Red/green on the fix: putting the old `fetch_add(1) + 1` back fails `add_ref_saturates_instead_of_wrapping` — as an overflow panic in a debug test run, and by wrapping the count to 0 in a release build, which is the real failure the issue describes. One thing to keep in mind if the tests are extended: `UNREFERENCED` and `SATURATED` are `static` objects, one per test, so they do not race today, but a second test on either static would need to serialise against the first.
Author
Owner

@⁠NethminaYasas on GitHub · 2026-11-24 15:47 UTC · original

Opened PR #1105 with the saturating \AddRef\ and the two synthetic unit tests.

**@⁠NethminaYasas** on GitHub · 2026-11-24 15:47 UTC · [original](https://github.com/Marvisak/VIctory/issues/931#issuecomment-7136624759) Opened PR #1105 with the saturating \AddRef\ and the two synthetic unit tests.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 06:59 UTC · original

Automated triage for the maintainers.

Confirmed on main: add_ref in crates/victory-gpu/src/object.rs is refs.fetch_add(1) + 1, so the count wraps past u32::MAX (and panics in a debug build), while release already stops at zero. It needs four billion outstanding references to matter. Open PR #2059 (fuzeofficlal) carries the saturating form; the earlier carries #1100 and #1105 were closed unmerged.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 06:59 UTC · [original](https://github.com/Marvisak/VIctory/issues/931#issuecomment-7155375885) <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-24T15:47:45Z at=2026-11-25T06:59:49Z --> Automated triage for the maintainers. Confirmed on `main`: `add_ref` in `crates/victory-gpu/src/object.rs` is `refs.fetch_add(1) + 1`, so the count wraps past `u32::MAX` (and panics in a debug build), while `release` already stops at zero. It needs four billion outstanding references to matter. Open PR #2059 (fuzeofficlal) carries the saturating form; the earlier carries #1100 and #1105 were closed unmerged. --- _Generated by [Claude Code](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
No milestone
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
github/Marvisak--VIctory#931
No description provided.