GPU: saturate COM AddRef at u32::MAX instead of wrapping #2057

Closed
fuzeofficlal wants to merge 1 commit from upstream-pr/2059 into main
fuzeofficlal commented 2026-11-25 04:17:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T04:17:01Z.


Closes #931.

Summary

  • Update object::add_ref to increment refs via fetch_update + checked_add(1) so COM reference counts saturate at u32::MAX instead of wrapping to 0 in release builds (which could allow a subsequent Release to drop a still-referenced object), matching the existing CAS floor in object::release and object::release_boxed.

Testing

  • Added object::tests::add_ref_saturates_at_u32_max_without_wrapping covering both heap-allocated (hand_out + release_boxed) and static (Object + release) COM objects across u32::MAX - 1 -> u32::MAX saturation, decrement without premature drop, and final 1 -> 0 destruction.
  • cargo test -p victory-gpu (debug & release), cargo clippy --workspace --all-targets --all-features -- -D warnings, and cargo xtask policy.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6597092418 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2059). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T04:17:01Z. --- Closes #931. ### Summary - Update `object::add_ref` to increment `refs` via `fetch_update` + `checked_add(1)` so COM reference counts saturate at `u32::MAX` instead of wrapping to `0` in release builds (which could allow a subsequent `Release` to drop a still-referenced object), matching the existing CAS floor in `object::release` and `object::release_boxed`. ### Testing - Added `object::tests::add_ref_saturates_at_u32_max_without_wrapping` covering both heap-allocated (`hand_out` + `release_boxed`) and static (`Object` + `release`) COM objects across `u32::MAX - 1 -> u32::MAX` saturation, decrement without premature drop, and final `1 -> 0` destruction. - `cargo test -p victory-gpu` (debug & release), `cargo clippy --workspace --all-targets --all-features -- -D warnings`, and `cargo xtask policy`.
catminou4 commented 2026-11-25 05:05:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T04:17:34Z.

Review: COMMENTED


Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.

COM AddRef saturates at u32::MAX instead of wrapping — refcount-overflow hardening; classic COM bug class, good.

<!-- greyfinch-sync:Marvisak/VIctory:review:6051789565 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2059#pullrequestreview-6051789565). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T04:17:34Z. **Review:** COMMENTED --- Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff. COM AddRef saturates at u32::MAX instead of wrapping — refcount-overflow hardening; classic COM bug class, good.
DenRakEiw commented 2026-11-25 05:05:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T04:44:48Z.

Review: APPROVED


Verified locally merged onto origin/main (940d32f, Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test -p victory-gpu --lib object:: — 5 passed, 0 failed (1 ignored), including add_ref_saturates_at_u32_max_without_wrapping
  • cargo clippy -p victory-gpu --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean saturation of COM AddRef at u32::MAX preventing reference-count wraparound. Looks good.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6051980670 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2059#pullrequestreview-6051980670). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T04:44:48Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (`940d32f`, Windows 11 x64, Rust 1.98.1 MSVC): - `cargo test -p victory-gpu --lib object::` — 5 passed, 0 failed (1 ignored), including `add_ref_saturates_at_u32_max_without_wrapping` - `cargo clippy -p victory-gpu --all-targets` — 0 warnings - `cargo xtask policy` — ok (`183 files ok`) Clean saturation of COM `AddRef` at `u32::MAX` preventing reference-count wraparound. Looks good. AI-assisted (Gemini 4 Neon).
jeremygold02 commented 2026-11-25 05:06:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-25T04:21:13Z.


Triage verdict — Keep the overflow fix, define ownership at saturation (37e69e7)

Evidence

  • @fuzeofficlal prevents the numeric wrap described in @geiyiliyabumo’s #931. At the maximum count, an interface query can still hand out a reference while the stored count stays unchanged, and the release paths still decrement it. The implementation needs an explicit lifetime policy for that state. AddRef and interface handout · Boxed release

Checks and limits

  • This is source review. The new test establishes that the counter does not wrap. Its final drop assertion resets the count to one, so it covers ordinary destruction separately from the saturation policy. Regression test

Next step

  • Make saturation sticky through release, or retain a wider internal count and clamp only the reported value. Test the chosen policy. Preserve credit for #931 and track its broader object-test coverage separately if it stays outside this patch.

This verification was AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7152765185 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2059#issuecomment-7152765185). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-25T04:21:13Z. --- **Triage verdict — Keep the overflow fix, define ownership at saturation** (`37e69e7`) **Evidence** - @fuzeofficlal prevents the numeric wrap described in @geiyiliyabumo’s #931. At the maximum count, an interface query can still hand out a reference while the stored count stays unchanged, and the release paths still decrement it. The implementation needs an explicit lifetime policy for that state. [AddRef and interface handout](https://github.com/Marvisak/VIctory/blob/37e69e79118fd8c34bf8ee21337804b4e741b854/crates/victory-gpu/src/object.rs#L69-L95) · [Boxed release](https://github.com/Marvisak/VIctory/blob/37e69e79118fd8c34bf8ee21337804b4e741b854/crates/victory-gpu/src/object.rs#L138-L153) **Checks and limits** - This is source review. The new test establishes that the counter does not wrap. Its final drop assertion resets the count to one, so it covers ordinary destruction separately from the saturation policy. [Regression test](https://github.com/Marvisak/VIctory/blob/37e69e79118fd8c34bf8ee21337804b4e741b854/crates/victory-gpu/src/object.rs#L570-L640) **Next step** - Make saturation sticky through release, or retain a wider internal count and clamp only the reported value. Test the chosen policy. Preserve credit for #931 and track its broader object-test coverage separately if it stays outside this patch. This verification was AI-assisted.
upipibi commented 2026-11-25 05:06:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-25T04:41:39Z.


Ran on Windows 11 with an RTX 3070 (Ampere, driver 616.56), debug build, at 37e69e7. victory-gpu: 142 passed, 2 failed, 2 ignored; the new add_ref_saturates_at_u32_max_without_wrapping passes. The 2 failures are #1744's heap-cap failures on main. Same as main on this hardware. Not run: a title.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7153078750 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2059#issuecomment-7153078750). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-25T04:41:39Z. --- Ran on Windows 11 with an RTX 3070 (Ampere, driver 616.56), debug build, at `37e69e7`. `victory-gpu`: 142 passed, 2 failed, 2 ignored; the new `add_ref_saturates_at_u32_max_without_wrapping` passes. The 2 failures are #1744's heap-cap failures on `main`. Same as `main` on this hardware. Not run: a title. AI-assisted (Claude Code).
fuzeofficlal commented 2026-11-25 05:06:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T05:00:07Z.


Updated in 57535164 — thanks @jeremygold02. Saturation at u32::MAX is now sticky across both object::release and object::release_boxed ((n != u32::MAX).then(|| n.checked_sub(1)).flatten()), so interface queries or AddRef calls at u32::MAX cannot be balanced down by later Release calls to drop a still-referenced object. Expanded the unit test to verify normal decrement at u32::MAX - 1, sticky saturation across answer + Release at u32::MAX, and ordinary 1 -> 0 destruction.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7153356802 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2059#issuecomment-7153356802). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T05:00:07Z. --- Updated in `57535164` — thanks @jeremygold02. Saturation at `u32::MAX` is now sticky across both `object::release` and `object::release_boxed` (`(n != u32::MAX).then(|| n.checked_sub(1)).flatten()`), so interface queries or `AddRef` calls at `u32::MAX` cannot be balanced down by later `Release` calls to drop a still-referenced object. Expanded the unit test to verify normal decrement at `u32::MAX - 1`, sticky saturation across `answer` + `Release` at `u32::MAX`, and ordinary `1 -> 0` destruction.
Marvisak commented 2026-11-25 10:07:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T09:24:14Z.


Automated triage for the maintainers.
Proposed close.

object::add_ref, object::release and object::release_boxed (crates/victory-gpu/src/object.rs) now stop the reference count at u32::MAX and then never drop the object. The count can only get there if a title holds about 4.29 billion references to one object at the same time. A title does not do that, so this is a guard for a case that cannot occur.

The guard also has a cost. Real COM lets the count wrap, so this differs from it. An object that did reach the limit would leak for good. The new test must free the saturated box by hand, which also skips the cleanup of the interface map in release_boxed. The same idea is already open in #1100 and #1105.

This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the label. A reproduction showing a title, or a VIctory code path, that actually reaches this count would change the verdict.

CI: passing (check).


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158282838 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2059#issuecomment-7158282838). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T09:24:14Z. --- <!-- victory-triage v1 head=5753516486cdf604bbfb82453f360b1abfbb8222 verdict=proposed-close at=2026-11-25T09:24:08Z --> Automated triage for the maintainers. **Proposed close.** `object::add_ref`, `object::release` and `object::release_boxed` (crates/victory-gpu/src/object.rs) now stop the reference count at `u32::MAX` and then never drop the object. The count can only get there if a title holds about 4.29 billion references to one object at the same time. A title does not do that, so this is a guard for a case that cannot occur. The guard also has a cost. Real COM lets the count wrap, so this differs from it. An object that did reach the limit would leak for good. The new test must free the saturated box by hand, which also skips the cleanup of the interface map in `release_boxed`. The same idea is already open in #1100 and #1105. This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the label. A reproduction showing a title, or a VIctory code path, that actually reaches this count would change the verdict. CI: passing (`check`). --- _Generated by [Claude Code](https://claude.ai/code)_
upstream-sync closed this pull request 2026-11-25 11:03:38 +00:00
fuzeofficlal commented 2026-11-25 11:05:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T10:17:54Z.


Agreed with the triage analysis — u32::MAX simultaneous live references cannot occur in practice and pinning the count diverges from COM cleanup. Closing.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7159503237 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2059#issuecomment-7159503237). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T10:17:54Z. --- Agreed with the triage analysis — `u32::MAX` simultaneous live references cannot occur in practice and pinning the count diverges from COM cleanup. Closing.

Pull request closed

Sign in to join this conversation.
No reviewers
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!2057
No description provided.