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

Closed
adamdotpy wants to merge 1 commit from upstream-pr/1100 into main
adamdotpy commented 2026-11-24 15:44:09 +00:00 (Migrated from github.com)

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


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6585646559 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1100). Originally posted by [adamdotpy](https://github.com/adamdotpy) on 2026-11-24T15:44:09Z. --- *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.
Zipettte commented 2026-11-24 17:04:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Zipettte on 2026-11-24T16:29:31Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7137748574 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1100#issuecomment-7137748574). Originally posted by [Zipettte](https://github.com/Zipettte) on 2026-11-24T16:29:31Z. --- 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)._
adamdotpy commented 2026-11-24 18:05:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by adamdotpy on 2026-11-24T17:10:19Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7138793354 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1100#issuecomment-7138793354). Originally posted by [adamdotpy](https://github.com/adamdotpy) on 2026-11-24T17:10:19Z. --- 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.
mercuryVM commented 2026-11-24 20:01:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T19:58:53Z.

Review: APPROVED


Tested b82fb125 on Windows 11, RTX 5070, MSVC:

  • cargo test -p victory-gpu passes on the head (67 passed, 1 ignored), including the new object tests.
  • Merged into our fork: workspace tests, shader tests and clippy -D warnings (default and all features) are all clean.

The fix is right. fetch_update(checked_add) stops at u32::MAX, so a saturated object is never freed by a later Release. Leaking it is the safe outcome, and the use-after-free is gone. The returned count stays monotonic with saturating_add.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6046401114 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1100#pullrequestreview-6046401114). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T19:58:53Z. **Review:** APPROVED --- Tested b82fb125 on Windows 11, RTX 5070, MSVC: - `cargo test -p victory-gpu` passes on the head (67 passed, 1 ignored), including the new object tests. - Merged into our fork: workspace tests, shader tests and `clippy -D warnings` (default and all features) are all clean. The fix is right. `fetch_update(checked_add)` stops at `u32::MAX`, so a saturated object is never freed by a later `Release`. Leaking it is the safe outcome, and the use-after-free is gone. The returned count stays monotonic with `saturating_add`. AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-25 01:05:34 +00:00
Marvisak commented 2026-11-25 01:08:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:32:15Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148609273 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1100#issuecomment-7148609273). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:32:15Z. --- 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

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!1100
No description provided.