Skip to content

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

Closed
AndyLatimer wants to merge 2 commits into
Marvisak:mainfrom
AndyLatimer:fix/hle-com-release-underflow
Closed

AndyLatimer wants to merge 2 commits into
Marvisak:mainfrom
AndyLatimer:fix/hle-com-release-underflow

Conversation

@AndyLatimer

@AndyLatimer AndyLatimer commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

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

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.
@Sunkiller-qc

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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 Build: make workspace tests run on Linux #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

Copy link
Copy Markdown
Contributor

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.

# Conflicts:
#	crates/victory-hle/src/modules/dstorage.rs
@AndyLatimer

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown

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 Build on Linux: cross-compile for MSVC, run under Wine #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.

edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Marvisak Marvisak closed this Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants