HLE: leave a released-out COM count at zero #31

Closed
AndyLatimer wants to merge 2 commits from upstream-pr/31 into main
AndyLatimer commented 2026-11-23 23:27:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AndyLatimer on 2026-11-23T23:27:52Z.


In dstorage, gameinput, media and xg, Release computed the new COM reference count as refs.fetch_sub(1, Ordering::AcqRel) - 1. When the count is already 0, that subtraction underflows u32: a panic in debug builds, and in release builds a wrapped count with a second free on the next call that reaches 0.

This adds a shared release_ref helper in crates/victory-hle/src/modules/mod.rs, built on fetch_update + checked_sub(1). At a count of 0 it leaves the count at 0, logs a warning and returns None, and the caller frees nothing.

Scope (thanks to the review): these objects are freed in the call that takes their count to 0. A title that releases once too often therefore makes its next Release read freed memory before release_ref runs, and this change does not detect or fix that use-after-free. What it does guarantee is that our own release paths never underflow or double-free on a count of 0. The tests reach that state by setting the count to 0 on a live object. Catching real double releases would need a freed-object marker (or a quarantine for released objects), which is not part of this PR.

Testing

  • Merged with current main (d2a4bae) in 79bb95e. The conflict was two tests added at the end of dstorage's test module; both are kept.
  • cargo test --workspace: 268 passed, 4 ignored (need external files). This includes 4 regression tests, one per module.
  • cargo clippy -p victory-hle --all-targets: no new warnings (the xmem.rs one is on main).
  • cargo xtask policy: pass.
  • Tested with synthetic unit tests only (not run against a title).

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6570992695 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/31). Originally posted by [AndyLatimer](https://github.com/AndyLatimer) on 2026-11-23T23:27:52Z. --- In `dstorage`, `gameinput`, `media` and `xg`, `Release` computed the new COM reference count as `refs.fetch_sub(1, Ordering::AcqRel) - 1`. When the count is already 0, that subtraction underflows `u32`: a panic in debug builds, and in release builds a wrapped count with a second free on the next call that reaches 0. This adds a shared `release_ref` helper in `crates/victory-hle/src/modules/mod.rs`, built on `fetch_update` + `checked_sub(1)`. At a count of 0 it leaves the count at 0, logs a warning and returns `None`, and the caller frees nothing. **Scope** (thanks to the review): these objects are freed in the call that takes their count to 0. A title that releases once too often therefore makes its next `Release` read freed memory before `release_ref` runs, and this change does not detect or fix that use-after-free. What it does guarantee is that our own release paths never underflow or double-free on a count of 0. The tests reach that state by setting the count to 0 on a live object. Catching real double releases would need a freed-object marker (or a quarantine for released objects), which is not part of this PR. ## Testing - Merged with current `main` (`d2a4bae`) in `79bb95e`. The conflict was two tests added at the end of `dstorage`'s test module; both are kept. - `cargo test --workspace`: 268 passed, 4 ignored (need external files). This includes 4 regression tests, one per module. - `cargo clippy -p victory-hle --all-targets`: no new warnings (the `xmem.rs` one is on `main`). - `cargo xtask policy`: pass. - Tested with synthetic unit tests only (not run against a title). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sunkiller-qc commented 2026-11-24 06:54:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Sunkiller-qc on 2026-11-23T23:46:49Z.


Thanks for this. One thing to be aware of: in all four modules, when the count reaches 0 the object is freed in the same call, so a title releasing "once too often" makes its next Release read a freed object before release_ref (modules/mod.rs:52) sees anything. The count-at-0 case it guards only exists for an object whose count is 0 but that was never freed, which these constructors don't create (the tests reach it by storing 0 by hand). The change is harmless, but the description may overstate it. If double releases are a real concern, logging them would need a freed-object marker or a quarantine instead. Static review, not built or run.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7117354684 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/31#issuecomment-7117354684). Originally posted by [Sunkiller-qc](https://github.com/Sunkiller-qc) on 2026-11-23T23:46:49Z. --- Thanks for this. One thing to be aware of: in all four modules, when the count reaches 0 the object is freed in the same call, so a title releasing "once too often" makes its next `Release` read a freed object before `release_ref` (`modules/mod.rs:52`) sees anything. The count-at-0 case it guards only exists for an object whose count is 0 but that was never freed, which these constructors don't create (the tests reach it by storing 0 by hand). The change is harmless, but the description may overstate it. If double releases are a real concern, logging them would need a freed-object marker or a quarantine instead. Static review, not built or run.
DaviTostes commented 2026-11-24 06:54:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T00:08:34Z.


Tested b3ecede stacked on #39 on Arch Linux x86_64, rustc 1.98.1: pass (no regression only).

  • cargo test --workspace: 155 passed, 0 failed, 3 ignored; merge with #39 clean.
  • Review note: the diff against main is much wider than the COM-count change — it also carries changes in victory-formats (texture.rs, xma.rs), victory-gpu (descriptor/host.rs, draw.rs) and victory-hle (dstorage.rs, gameinput.rs). If those are not intended, the branch needs rebasing; if they are prerequisites, they should be named in the description.
  • No game data used.

AI-assisted (DeepSeek V4.1 Flash coding agent).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7117748449 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/31#issuecomment-7117748449). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T00:08:34Z. --- Tested `b3ecede` stacked on **#39** on **Arch Linux x86_64, rustc 1.98.1**: **pass (no regression only)**. - `cargo test --workspace`: **155 passed, 0 failed, 3 ignored**; merge with #39 clean. - Review note: the diff against `main` is much wider than the COM-count change — it also carries changes in `victory-formats` (`texture.rs`, `xma.rs`), `victory-gpu` (`descriptor/host.rs`, `draw.rs`) and `victory-hle` (`dstorage.rs`, `gameinput.rs`). If those are not intended, the branch needs rebasing; if they are prerequisites, they should be named in the description. - No game data used. AI-assisted (DeepSeek V4.1 Flash coding agent).
lordon1a commented 2026-11-24 06:54:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by lordon1a on 2026-11-24T00:20:02Z.


Verified b3ecede92 on Windows 11 (x86_64-pc-windows-gnullvm, rustc 1.100.0, applied cleanly to main@35b054ec0): pass.

  • cargo xtask policy: 150 files ok
  • cargo test -p victory-hle: 85 passed + 1 integration test (no_dll_planting), 0 failed (+4 new COM refcount saturation tests).

AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7117957614 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/31#issuecomment-7117957614). Originally posted by [lordon1a](https://github.com/lordon1a) on 2026-11-24T00:20:02Z. --- Verified `b3ecede92` on Windows 11 (`x86_64-pc-windows-gnullvm`, `rustc 1.100.0`, applied cleanly to `main@35b054ec0`): **pass**. - `cargo xtask policy`: 150 files ok - `cargo test -p victory-hle`: **85 passed + 1 integration test (`no_dll_planting`), 0 failed** (+4 new COM refcount saturation tests). AI-assisted.
AndyLatimer commented 2026-11-24 06:54:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AndyLatimer on 2026-11-24T01:30:38Z.


Thanks for the reviews. The description now states the scope accurately: this keeps our own release paths from underflowing or double-freeing at a count of 0, but a title's extra Release after the object was freed is still a use-after-free that this does not catch. The branch is merged with current main (79bb95e, conflict only between two tests added at the end of dstorage's test module); the diff against main is the five HLE files only. cargo test --workspace: 268 passed.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7119169599 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/31#issuecomment-7119169599). Originally posted by [AndyLatimer](https://github.com/AndyLatimer) on 2026-11-24T01:30:38Z. --- Thanks for the reviews. The description now states the scope accurately: this keeps our own release paths from underflowing or double-freeing at a count of 0, but a title's extra Release after the object was freed is still a use-after-free that this does not catch. The branch is merged with current main (`79bb95e`, conflict only between two tests added at the end of dstorage's test module); the diff against main is the five HLE files only. `cargo test --workspace`: 268 passed.
DaviTostes commented 2026-11-24 06:55:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T01:34:24Z.


Tested the new head 79bb95e (merge with main) under Wine 11.22 + vkd3d-proton 3.1 (Arch Linux x86_64, AMD RX 9060 XT, RADV 26.3.1), Windows build of victory-hle: pass.

  • victory_hle: 89 passed, 1 failed — the failure is modules::net::tests::only_loopback_is_reachable (Wine lacks WSAConnectByList); with #195 on top the suite is green.
  • The 4 tests this PR adds for the released-out COM count pass.
  • The COM release path is mostly reference-count behaviour in the HLE, so the unit tests are the right check; no host device is needed.

No game data used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7119233755 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/31#issuecomment-7119233755). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T01:34:24Z. --- Tested the new head `79bb95e` (merge with main) under **Wine 11.22 + vkd3d-proton 3.1** (Arch Linux x86_64, AMD RX 9060 XT, RADV 26.3.1), Windows build of `victory-hle`: **pass**. - `victory_hle`: **89 passed, 1 failed** — the failure is `modules::net::tests::only_loopback_is_reachable` (Wine lacks `WSAConnectByList`); with #195 on top the suite is green. - The 4 tests this PR adds for the released-out COM count pass. - The COM release path is mostly reference-count behaviour in the HLE, so the unit tests are the right check; no host device is needed. No game data used.
upstream-sync closed this pull request 2026-11-24 09:04:54 +00:00

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