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

Open
opened 2026-11-24 14:18:00 +00:00 by geiyiliyabumo · 5 comments
geiyiliyabumo commented 2026-11-24 14:18:00 +00:00 (Migrated from github.com)
Contributor

Imported from Marvisak/VIctory. Originally posted by geiyiliyabumo on 2026-11-24T14:18:00Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6583838812 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/issues/931). Originally posted by [geiyiliyabumo](https://github.com/geiyiliyabumo) on 2026-11-24T14:18:00Z. --- ### 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.*
DaniiL35 commented 2026-11-24 15:04:10 +00:00 (Migrated from github.com)
Author
Contributor

Imported from Marvisak/VIctory. Originally posted by DaniiL35 on 2026-11-24T14:41:50Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7134798988 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/issues/931#issuecomment-7134798988). Originally posted by [DaniiL35](https://github.com/DaniiL35) on 2026-11-24T14:41:50Z. --- ### 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`.
NethminaYasas commented 2026-11-24 16:05:14 +00:00 (Migrated from github.com)
Author
Contributor

Imported from Marvisak/VIctory. Originally posted by NethminaYasas on 2026-11-24T15:31:20Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7136169399 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/issues/931#issuecomment-7136169399). Originally posted by [NethminaYasas](https://github.com/NethminaYasas) on 2026-11-24T15:31:20Z. --- 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.
adamdotpy commented 2026-11-24 16:05:21 +00:00 (Migrated from github.com)
Author
Contributor

Imported from Marvisak/VIctory. Originally posted by adamdotpy on 2026-11-24T15:44:15Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7136526551 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/issues/931#issuecomment-7136526551). Originally posted by [adamdotpy](https://github.com/adamdotpy) on 2026-11-24T15:44:15Z. --- 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.
NethminaYasas commented 2026-11-24 16:05:22 +00:00 (Migrated from github.com)
Author
Contributor

Imported from Marvisak/VIctory. Originally posted by NethminaYasas on 2026-11-24T15:47:45Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7136624759 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/issues/931#issuecomment-7136624759). Originally posted by [NethminaYasas](https://github.com/NethminaYasas) on 2026-11-24T15:47:45Z. --- Opened PR #1105 with the saturating \AddRef\ and the two synthetic unit tests.
Marvisak commented 2026-11-25 07:03:31 +00:00 (Migrated from github.com)
Author
Contributor

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T06:59:50Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7155375885 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/issues/931#issuecomment-7155375885). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T06:59:50Z. --- <!-- 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)_
Sign in to join this conversation.
No labels
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.

Reference
Marvisak/VIctory#931
No description provided.