GPU: saturate COM AddRef against a u32 wrap, with tests for object.rs #1100

Closed
github-bot wants to merge 1 commit from gh-pr/1100/head into gh-pr/1100/base
Owner

@⁠adamdotpy on GitHub · 2026-11-24 15:44 UTC · original
adamdotpy:gpu/object-addref-saturates → main · +376 −1 · 1 files · 1 commits · closed without merging

Carries @⁠geiyiliyabumo's commit from #931, verified here; AI-assisted.

The change is geiyiliyabumo@b82fb12, pushed unchanged with its authorship, because the issue says external PR creation is still restricted and offers the branch for cherry-pick.

The fix

object::add_ref did fetch_add(1).wrapping_add(1). At u32::MAX that wraps to 0: in a release build the object looks unreferenced, the next Release frees it while COM still holds it, and every later use is a use-after-free. It now saturates:

match refs.fetch_update(Relaxed, Relaxed, |n| n.checked_add(1)) {
    Ok(n) | Err(n) => n,   // Err: already at MAX
}
current.saturating_add(1)

AddRef returning u32::MAX for ever is what COM expects of a saturated count, and it never returns 0.

The tests

object.rs had none. This adds 17: the reference-count paths (AddRef saturation, Release never below zero, last-ref free and private-data cleanup, hand_out accept/reject/ask, answer), the ABI the title pins (Object's header bits and count, IUnknown at the head of a table, D3D12Object's methods at the PC offsets), is_matches over whole ids only, private data round-trip and its ownership, SetName, and parent_device answering only the device ids.

Verification here

Windows 11, Rust 1.98.1, on ffe310e with this commit applied:

  • cargo test -p victory-gpu: 67 passed, 1 ignored (50 before; 17 new).
  • cargo clippy -p victory-gpu --all-targets --all-features -- -D warnings: clean.
  • cargo build --release -p victory: builds.
  • Red/green: putting the old fetch_add back fails add_ref_saturates_instead_of_wrapping (in a debug test run it is an overflow panic at the + 1; in a release build the count wraps to 0, which the count assertion catches). The tests are not passing vacuously.

One note for whoever runs the suite: two of the tests lean on static objects (UNREFERENCED, SATURATED), each owned by exactly one test, so they do not race each other under the default threaded runner — but a future test that reuses either static would need to serialise against it.

Fixes #931.

**@⁠adamdotpy** on GitHub · 2026-11-24 15:44 UTC · [original](https://github.com/Marvisak/VIctory/pull/1100) `adamdotpy:gpu/object-addref-saturates` → `main` · +376 −1 · 1 files · 1 commits · closed without merging *Carries @⁠geiyiliyabumo's commit from #931, verified here; AI-assisted.* The change is [`geiyiliyabumo@b82fb12`](https://github.com/geiyiliyabumo/VIctory/tree/test/gpu-object-tests), pushed unchanged with its authorship, because the issue says external PR creation is still restricted and offers the branch for cherry-pick. ## The fix `object::add_ref` did `fetch_add(1).wrapping_add(1)`. At `u32::MAX` that wraps to 0: in a release build the object looks unreferenced, the next `Release` frees it while COM still holds it, and every later use is a use-after-free. It now saturates: ```rust match refs.fetch_update(Relaxed, Relaxed, |n| n.checked_add(1)) { Ok(n) | Err(n) => n, // Err: already at MAX } current.saturating_add(1) ``` `AddRef` returning `u32::MAX` for ever is what COM expects of a saturated count, and it never returns 0. ## The tests `object.rs` had none. This adds 17: the reference-count paths (`AddRef` saturation, `Release` never below zero, last-ref free and private-data cleanup, `hand_out` accept/reject/ask, `answer`), the ABI the title pins (`Object`'s header bits and count, `IUnknown` at the head of a table, `D3D12Object`'s methods at the PC offsets), `is_matches` over whole ids only, private data round-trip and its ownership, `SetName`, and `parent_device` answering only the device ids. ## Verification here Windows 11, Rust 1.98.1, on `ffe310e` with this commit applied: - `cargo test -p victory-gpu`: 67 passed, 1 ignored (50 before; 17 new). - `cargo clippy -p victory-gpu --all-targets --all-features -- -D warnings`: clean. - `cargo build --release -p victory`: builds. - Red/green: putting the old `fetch_add` back fails `add_ref_saturates_instead_of_wrapping` (in a debug test run it is an overflow panic at the `+ 1`; in a release build the count wraps to 0, which the count assertion catches). The tests are not passing vacuously. One note for whoever runs the suite: two of the tests lean on `static` objects (`UNREFERENCED`, `SATURATED`), each owned by exactly one test, so they do not race each other under the default threaded runner — but a future test that reuses either static would need to serialise against it. Fixes #931.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:19:51 +00:00
github-bot closed this pull request 2026-11-26 04:19:51 +00:00
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 16:29 UTC · original

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at b82fb12: cargo test -p victory-gpu passes, 67 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

**@⁠Zipettte** on GitHub · 2026-11-24 16:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/1100#issuecomment-7137748574) Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at b82fb12: `cargo test -p victory-gpu` passes, 67 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 17:10 UTC · original

Thanks for the run @⁠Zipettte — 67 passed / 0 failed on victory-gpu at b82fb12 with the main baseline green on the same machine. Noted; nothing actionable in the report.

**@⁠adamdotpy** on GitHub · 2026-11-24 17:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/1100#issuecomment-7138793354) Thanks for the run @⁠Zipettte — 67 passed / 0 failed on `victory-gpu` at `b82fb12` with the main baseline green on the same machine. Noted; nothing actionable in the report.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:32 UTC · original

Automated triage for the maintainers. Closing because it does not change behaviour the project needs (formatting, guards for cases that cannot occur, or notes and translations that cannot be kept current).

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 00:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/1100#issuecomment-7148609273) Automated triage for the maintainers. Closing because it does not change behaviour the project needs (formatting, guards for cases that cannot occur, or notes and translations that cannot be kept current). If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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!1100
No description provided.