Audio: an XMA read block past the ring's end wraps instead of filling forever #248

Closed
github-bot wants to merge 1 commit from gh-pr/248/head into gh-pr/248/base
Owner

@⁠AnoHunted on GitHub · 2026-11-24 01:48 UTC · original
AnoHunted:xma-read-block-wrap → main · +85 −1 · 1 files · 1 commits · closed without merging

What

  • pump in crates/victory-hle/src/modules/audio.rs compares the write block with the title's
    read block taken modulo the ring size (context.field(9, 0, 5) % size), as the write block
    ((write + 1) % size) and command 18 (advance_read) already are. One line and a comment.
  • New test xma_read_blocks_past_the_ring_end_wrap: a context set up like the one in
    xma_contexts_decode_into_their_output_rings (the synthetic silent stream, a ring of four
    blocks), but looping forever over the whole stream (loop count 255) and with read block 6. It
    runs an XMA pass on a thread of its own and requires it to end within 5 s, with blocks 0 and 1
    written, blocks 2 and 3 untouched, the write block on 2 and the ring no longer writable. If the
    pass does not end, the test clears the context's "writable" bit as a full ring would, so the
    stuck pass ends, the contexts are released, and the test fails without holding up the rest of
    the suite.

Why

A pass fills a voice's ring until the write block (word 0, bits 27-31) comes round to the read
block (word 9, bits 0-4). The write block wraps modulo the ring size (word 0, bits 22-26), but a
read block the title set up at or past the size was compared as it was: no write block ever
equalled it, and "writable" was never cleared. With an input that ends, the pass went round the
ring until the input ran out, writing over blocks the title had not read yet. With a stream that
loops forever it never returned. It holds that voice's lock while it runs, so:

  • the title's next command on that voice (advance the read block, disable, enable) waited for
    good; titles send them from their mixer thread;
  • the one decoding thread stayed on that voice, so no other voice was filled any more;
  • AcpHalReleaseShapeContexts waited for good for the context arrays' lock.

Why modulo, not a clamp or an error: the module already treats both offsets as positions on the
ring. The write block wraps, and command 18 already takes the read block modulo the size, so a
read block out of range is brought into range the first time the title advances it; the passes
before that now agree with it. Xenia's XMA context decoder, which reads the same fields, does
the same: it puts both offsets on its ring buffer, whose set_read_offset and set_write_offset
take them modulo the capacity. I know of no documented error status for this case. A pass now
writes at most one ring's worth of blocks (plus one, see below).

Also checked in pump, not changed: every turn of its loop writes a block and moves the write
block on, whatever the input does, so with the read block in range a pass ends within one ring
however the input buffers and loops are set up. A write block at or past the ring size is not
this bug (the pass still ends) but has a cost of its own: the first block of the pass is written
past the ring's end before the write block wraps.

Tested

Windows 11, NVIDIA GeForce RTX 5080, Rust 1.98.1.

  • Main with only the new test added: cargo test -p victory-hle -- modules::audio fails the new
    test after 5.0 s ("the XMA pass never ended"); the five other audio tests pass. All of
    cargo test -p victory-hle -- --test-threads=4: 86 passed, 1 failed (the new test), finished in
    5.02 s, so the stuck pass is ended and the rest of the crate runs.
  • This branch: cargo test -p victory-hle -- modules::audio gives 6 passed, 0 failed; the new test
    takes 0.003 s (--report-time).
  • cargo test --workspace -- --test-threads=4, run side by side: main 264 passed, 0 failed,
    4 ignored; this branch 265 passed (the new test), 0 failed, 4 ignored.
  • cargo clippy -p victory-hle --all-targets: the same single warning on main and here (in
    xmem.rs), nothing in audio.rs. rustfmt --check on audio.rs: nothing in the changed lines
    (the same older differences at Xma::pump, dump_input and in audio/capture.rs as on main).
    cargo xtask policy: 151 files ok.

Not tested

  • A title, and real XMA streams. What the console itself does with a read block out of range
    was not observed; the choice follows the module's write side and Xenia.
  • Linux, AMD and Intel GPUs (the change is CPU code only).

Open PRs on this file: #88 (advance_read), #112 (formatting), #130 (a profiler scope in
decode), #194 (the loop skip in decode), #237 (the heap lock in pump and decode) and the
draft integration #231 each merge with this branch without a textual conflict in audio.rs
(git merge-tree; not tested together); none of them changes the comparison in pump.

AI-assisted (Claude Code, Claude Opus 5.5). No game files were used.

**@⁠AnoHunted** on GitHub · 2026-11-24 01:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/248) `AnoHunted:xma-read-block-wrap` → `main` · +85 −1 · 1 files · 1 commits · closed without merging ## What - `pump` in `crates/victory-hle/src/modules/audio.rs` compares the write block with the title's read block taken modulo the ring size (`context.field(9, 0, 5) % size`), as the write block (`(write + 1) % size`) and command 18 (`advance_read`) already are. One line and a comment. - New test `xma_read_blocks_past_the_ring_end_wrap`: a context set up like the one in `xma_contexts_decode_into_their_output_rings` (the synthetic silent stream, a ring of four blocks), but looping forever over the whole stream (loop count 255) and with read block 6. It runs an XMA pass on a thread of its own and requires it to end within 5 s, with blocks 0 and 1 written, blocks 2 and 3 untouched, the write block on 2 and the ring no longer writable. If the pass does not end, the test clears the context's "writable" bit as a full ring would, so the stuck pass ends, the contexts are released, and the test fails without holding up the rest of the suite. ## Why A pass fills a voice's ring until the write block (word 0, bits 27-31) comes round to the read block (word 9, bits 0-4). The write block wraps modulo the ring size (word 0, bits 22-26), but a read block the title set up at or past the size was compared as it was: no write block ever equalled it, and "writable" was never cleared. With an input that ends, the pass went round the ring until the input ran out, writing over blocks the title had not read yet. With a stream that loops forever it never returned. It holds that voice's lock while it runs, so: - the title's next command on that voice (advance the read block, disable, enable) waited for good; titles send them from their mixer thread; - the one decoding thread stayed on that voice, so no other voice was filled any more; - `AcpHalReleaseShapeContexts` waited for good for the context arrays' lock. Why modulo, not a clamp or an error: the module already treats both offsets as positions on the ring. The write block wraps, and command 18 already takes the read block modulo the size, so a read block out of range is brought into range the first time the title advances it; the passes before that now agree with it. Xenia's XMA context decoder, which reads the same fields, does the same: it puts both offsets on its ring buffer, whose `set_read_offset` and `set_write_offset` take them modulo the capacity. I know of no documented error status for this case. A pass now writes at most one ring's worth of blocks (plus one, see below). Also checked in `pump`, not changed: every turn of its loop writes a block and moves the write block on, whatever the input does, so with the read block in range a pass ends within one ring however the input buffers and loops are set up. A write block at or past the ring size is not this bug (the pass still ends) but has a cost of its own: the first block of the pass is written past the ring's end before the write block wraps. ## Tested Windows 11, NVIDIA GeForce RTX 5080, Rust 1.98.1. - Main with only the new test added: `cargo test -p victory-hle -- modules::audio` fails the new test after 5.0 s ("the XMA pass never ended"); the five other audio tests pass. All of `cargo test -p victory-hle -- --test-threads=4`: 86 passed, 1 failed (the new test), finished in 5.02 s, so the stuck pass is ended and the rest of the crate runs. - This branch: `cargo test -p victory-hle -- modules::audio` gives 6 passed, 0 failed; the new test takes 0.003 s (`--report-time`). - `cargo test --workspace -- --test-threads=4`, run side by side: main 264 passed, 0 failed, 4 ignored; this branch 265 passed (the new test), 0 failed, 4 ignored. - `cargo clippy -p victory-hle --all-targets`: the same single warning on main and here (in `xmem.rs`), nothing in `audio.rs`. `rustfmt --check` on `audio.rs`: nothing in the changed lines (the same older differences at `Xma::pump`, `dump_input` and in `audio/capture.rs` as on main). `cargo xtask policy`: 151 files ok. ## Not tested - A title, and real XMA streams. What the console itself does with a read block out of range was not observed; the choice follows the module's write side and Xenia. - Linux, AMD and Intel GPUs (the change is CPU code only). Open PRs on this file: #88 (`advance_read`), #112 (formatting), #130 (a profiler scope in `decode`), #194 (the loop skip in `decode`), #237 (the heap lock in `pump` and `decode`) and the draft integration #231 each merge with this branch without a textual conflict in `audio.rs` (`git merge-tree`; not tested together); none of them changes the comparison in `pump`. AI-assisted (Claude Code, Claude Opus 5.5). No game files were used.
The decoder fills an XMA voice's output ring until the write block
(word 0, bits 27-31) comes round to the title's read block (word 9,
bits 0-4). The write block wraps modulo the ring size (word 0, bits
22-26), and so does a read block moved on by command 18, but a read block
the title set up at or past the ring's end was compared as it was: no
write block ever equalled it. With a stream that loops forever the pass
never ended, holding that voice's lock, so the title's next command on
the voice (advance, disable) waited for good, and releasing the context
arrays did too.

Take the read block modulo the ring size as well, as the write side
already does. A test sets up read block 6 on a ring of four with an
endless loop and requires the pass to end within seconds, with blocks 0
and 1 filled.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:56:32 +00:00
github-bot closed this pull request 2026-11-26 03:56:32 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 05:34 UTC · original

Tested current PR head 1054fe87de08038e887d6f7c6f44e5b07ac7a9f9.

  • cargo test --locked -p victory-hle --lib audio -- --test-threads=1: 6 passed, 81 filtered out. The new xma_read_blocks_past_the_ring_end_wrap test uses a four-block ring and read index 6, expecting the decoder to wrap to block 2. A looping two-packet silent stream must finish its pass within five seconds; the test checks that blocks 0–1 were written, blocks 2–3 stayed title-writable, and the sentinel bytes in the untouched half of the ring remain unchanged. The target also passed XMA ring decoding, queue-drop counting, and heap mapping/address reuse cases (ring-wrap regression test).

The test uses a synthetic silent XMA stream and in-memory ring, so it does not cover real title audio, media assets, or a host audio device.

**@⁠jeremygold02** on GitHub · 2026-11-24 05:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/248#issuecomment-7123070835) Tested current PR head `1054fe87de08038e887d6f7c6f44e5b07ac7a9f9`. - `cargo test --locked -p victory-hle --lib audio -- --test-threads=1`: 6 passed, 81 filtered out. The new `xma_read_blocks_past_the_ring_end_wrap` test uses a four-block ring and read index 6, expecting the decoder to wrap to block 2. A looping two-packet silent stream must finish its pass within five seconds; the test checks that blocks 0–1 were written, blocks 2–3 stayed title-writable, and the sentinel bytes in the untouched half of the ring remain unchanged. The target also passed XMA ring decoding, queue-drop counting, and heap mapping/address reuse cases ([ring-wrap regression test](https://github.com/Marvisak/VIctory/blob/1054fe87de08038e887d6f7c6f44e5b07ac7a9f9/crates/victory-hle/src/modules/audio.rs#L1001-L1079)). The test uses a synthetic silent XMA stream and in-memory ring, so it does not cover real title audio, media assets, or a host audio device.
Author
Owner

@⁠AnoHunted on GitHub · 2026-11-24 10:21 UTC · original

As @⁠Exorion1er noted, #536 contains this change and its test unchanged, plus the write-block wrap. Merging both would give two tests with the same name, so I'm closing this one in favour of #536. Thanks @⁠jeremygold02 and @⁠mercuryVM for testing it; those results apply to #536's first commit as is.

**@⁠AnoHunted** on GitHub · 2026-11-24 10:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/248#issuecomment-7128663745) As @⁠Exorion1er noted, #536 contains this change and its test unchanged, plus the write-block wrap. Merging both would give two tests with the same name, so I'm closing this one in favour of #536. Thanks @⁠jeremygold02 and @⁠mercuryVM for testing it; those results apply to #536's first commit as is.

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