Audio heap: a block freed under the decoder's slice stays in memory until let go (#230) #248

Open
github-bot wants to merge 1 commit from github-bot/gh-248 into main AGit
Owner

@⁠sharan104 on GitHub · 2026-11-26 12:41 UTC · original
sharan104:fix/230-apu-free-deferred → main · +71 −3 · 1 files · 1 commits · open

Fixes #230.

heap_bytes returns slices of heap blocks whose lifetime isn't tied to the heap. The XMA decoder writes output blocks and reads input packets through them, while heap_free took the block out and deallocated it at once. So a title that freed a voice's ring or input while the voice was still decoding had the decoder write into freed host memory. That's a title bug, which corrupts memory on the console too, so this is insurance, as the issue says.

The issue's sketch drains the deferred frees on the decoder thread. pump() also runs on other threads, though (the tests' passes), and the diagnostics dump_input takes a slice too. So this uses a holder count instead:

  • Holding (an RAII guard) is held around each XMA pass and around dump_input's read.
  • heap_free still removes the block and gives its device range back immediately. ApuFree's range behaviour is unchanged, and no new slice can reach the block. It deallocates only when nothing holds slices; otherwise it pushes (ptr, layout) to FREED.
  • The last holder to let go drains FREED. It looks at the count again under FREED's lock, the same lock frees push under. So a holder that began after the first look, and may hold a slice of a block freed after it began, keeps that block until it lets go.
  • Ordering: SeqCst on the count. The HEAP mutex orders the removal against any later lookup, so a holder that starts after a block was removed can't reach it.

Test: a_block_freed_under_a_held_slice_stays_until_let_go. It allocates, takes a slice under a Holding and frees the block. It then checks that the block is gone from the heap, is still in FREED, and can still be written through the slice. After the holder lets go, the block is released unless another test's pass still holds slices; that last check is written to tolerate tests running in parallel.

Results:

  • cargo test -p victory-hle --locked passed 10 times out of 10 (156 tests each run).
  • The audio tests pass with --all-features --test-threads=1.
  • cargo clippy -p victory-hle --all-targets --locked --no-deps -- -D warnings is clean, with and without --all-features.

🤖 Generated with Claude Code

**@⁠sharan104** on GitHub · 2026-11-26 12:41 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/248) `sharan104:fix/230-apu-free-deferred` → `main` · +71 −3 · 1 files · 1 commits · open Fixes #230. `heap_bytes` returns slices of heap blocks whose lifetime isn't tied to the heap. The XMA decoder writes output blocks and reads input packets through them, while `heap_free` took the block out and deallocated it at once. So a title that freed a voice's ring or input while the voice was still decoding had the decoder write into freed host memory. That's a title bug, which corrupts memory on the console too, so this is insurance, as the issue says. The issue's sketch drains the deferred frees on the decoder thread. `pump()` also runs on other threads, though (the tests' passes), and the diagnostics `dump_input` takes a slice too. So this uses a holder count instead: - **`Holding`** (an RAII guard) is held around each XMA pass and around `dump_input`'s read. - **`heap_free`** still removes the block and gives its device range back immediately. `ApuFree`'s range behaviour is unchanged, and no new slice can reach the block. It deallocates only when nothing holds slices; otherwise it pushes `(ptr, layout)` to `FREED`. - **The last holder** to let go drains `FREED`. It looks at the count again under `FREED`'s lock, the same lock frees push under. So a holder that began after the first look, and may hold a slice of a block freed after it began, keeps that block until it lets go. - **Ordering:** `SeqCst` on the count. The `HEAP` mutex orders the removal against any later lookup, so a holder that starts after a block was removed can't reach it. Test: `a_block_freed_under_a_held_slice_stays_until_let_go`. It allocates, takes a slice under a `Holding` and frees the block. It then checks that the block is gone from the heap, is still in `FREED`, and can still be written through the slice. After the holder lets go, the block is released unless another test's pass still holds slices; that last check is written to tolerate tests running in parallel. Results: - `cargo test -p victory-hle --locked` passed 10 times out of 10 (156 tests each run). - The audio tests pass with `--all-features --test-threads=1`. - `cargo clippy -p victory-hle --all-targets --locked --no-deps -- -D warnings` is clean, with and without `--all-features`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
heap_bytes hands out slices of heap blocks with no lifetime tied to the
heap, and the XMA decoder writes output blocks and reads input packets
through them; heap_free took the block out and deallocated it at once. A
title that frees a voice's ring or input while the voice still decodes (a
title bug, which corrupts memory on the console too) had the decoder
write into freed host memory.

Now a Holding is held while slices are (each XMA pass, and the
diagnostics dump of an input). heap_free still takes the block out of the
heap and gives its device range back at once, so no new slice reaches
it, but deallocates it only when nothing holds slices; otherwise the last
holder to let go does. The holder count is looked at again under the
freed list's lock that frees push under, so one that began holding after
the first look keeps its block.

Fixes #230

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 12:46:57 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/248/head:github-bot/gh-248'
git switch 'github-bot/gh-248'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-248'
git switch 'github-bot/gh-248'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-248'
git switch 'github-bot/gh-248'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-248'
git switch 'main'
git merge --squash 'github-bot/gh-248'
git switch 'main'
git merge --ff-only 'github-bot/gh-248'
git switch 'main'
git merge 'github-bot/gh-248'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.