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

Closed
github-bot wants to merge 2 commits from gh-pr/31/head into gh-pr/31/base
Owner

@⁠AndyLatimer on GitHub · 2026-11-23 23:27 UTC · original
AndyLatimer:fix/hle-com-release-underflow → main · +101 −12 · 5 files · 2 commits · closed without merging

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

**@⁠AndyLatimer** on GitHub · 2026-11-23 23:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/31) `AndyLatimer:fix/hle-com-release-underflow` → `main` · +101 −12 · 5 files · 2 commits · closed without merging 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)
Release at a count of 0 in dstorage, gameinput, media and xg did fetch_sub(1) - 1: a panic in debug builds and, in release builds, a second free. A shared release_ref now keeps the count at 0, frees nothing and warns. One test per module.
# Conflicts:
#	crates/victory-hle/src/modules/dstorage.rs
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:47:15 +00:00
github-bot closed this pull request 2026-11-26 03:47:15 +00:00
Author
Owner

@⁠Sunkiller-qc on GitHub · 2026-11-23 23:46 UTC · original

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.

**@⁠Sunkiller-qc** on GitHub · 2026-11-23 23:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/31#issuecomment-7117354684) 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.
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 00:08 UTC · original

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

**@⁠DaviTostes** on GitHub · 2026-11-24 00:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/31#issuecomment-7117748449) 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).
Author
Owner

@⁠lordon1a on GitHub · 2026-11-24 00:20 UTC · original

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.

**@⁠lordon1a** on GitHub · 2026-11-24 00:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/31#issuecomment-7117957614) 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.
Author
Owner

@⁠AndyLatimer on GitHub · 2026-11-24 01:30 UTC · original

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.

**@⁠AndyLatimer** on GitHub · 2026-11-24 01:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/31#issuecomment-7119169599) 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.
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 01:34 UTC · original

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.

**@⁠DaviTostes** on GitHub · 2026-11-24 01:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/31#issuecomment-7119233755) 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.

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