Skip to content

XMA: noise tests, and end a frame at its data instead of looping forever - #183

Merged
Marvisak merged 1 commit into
Marvisak:mainfrom
AnoHunted:xma-noise-tests
Nov 25, 2026
Merged

Marvisak merged 1 commit into
Marvisak:mainfrom
AnoHunted:xma-noise-tests

Conversation

@AnoHunted

Copy link
Copy Markdown
Contributor

What

  • Decoder::decode ends a frame's bits at the end of its data:
    end: bits.min(data.len().saturating_mul(8)). A frame from read_frame always holds its bits
    (data.len() == bits.div_ceil(8)), so its decoding does not change.
  • Four deterministic noise tests in xma.rs, like BCPack's noise_never_panics: xorshift64, fixed
    seeds, bounded rounds, no new dependencies.
    • noise_packets_walk_and_decode: 60 buffers of 1 to 4 packets: pure noise, streams of noise
      frames with consistent headers (the test lays them out), and such streams with damaged headers
      and flipped bits. It walks the frames from the first packet as audio.rs does and decodes each,
      mono and stereo. Each next must lie after its frame, the walk must stay within bits / 16
      frames, every decode must append one frame of samples (flush half a frame), and a clean stream
      must give every frame it holds.
    • noise_frames_decode_to_a_frame_of_samples: noise frames of 0 to 4095 bytes, mono and stereo at
      24, 32, 44.1 and 48 kHz, with bit counts up to the end of the data.
    • noise_frames_with_bit_counts_past_the_data: short noise frames (0 to 300 bytes) with bit counts
      past the data, up to usize::MAX.
    • noise_offsets_read_a_frame_or_nothing: read_frame from packet edges, the end of the buffer,
      usize::MAX / 2, the last packet-aligned bit below usize::MAX, usize::MAX and random bits;
      packet at indices up to usize::MAX. A frame read must have a length of 16 to 32766 bits, that
      many bits of data, and a next after its start and inside the buffer.

Why

decode took its bits argument as the end of the frame. Past the data the bit reader returns
zeros. In a stereo tile header, zero bits give the subframe to no channel, so min_channel_len
falls back and the loop never moves on; its only way out is then overread(), which a count past the
data never reaches. One byte of data with a count of about usize::MAX / 2 spun for good. A mono
tile header always moves on, so only stereo hung.

No caller passes such a count today: audio.rs hands decode the Frame that read_frame built.
The change makes decode safe on its own, as a public entry point that takes a size.

Nothing else failed. The decoder and the frame walk also held up in a one-off run on main with 20
times the rounds and 400,000 mutated frames (281,000 of them decoded to non-silent samples, so the
scale factor, coefficient and channel transform paths ran). That run used a harness with a panic
catcher per case and a time limit, so a hang could not stall the run; it was for
finding failures only and is not part of this change.

Tested

Windows 11, NVIDIA GeForce RTX 5080, Rust 1.98.1.

  • Main with only the tests added (and the harness above): cargo test -p victory-formats --lib xma::tests::noise_ -- --test-threads=1 hung in noise_frames_with_bit_counts_past_the_data
    (2 channels, round 1: 1 byte, 9223372036855139009 bits); the three other tests passed, also at 20
    times the rounds.
  • This branch: cargo test -p victory-formats --lib xma:: gives 9 passed, 2 ignored (they need files
    outside the repository). The four noise tests take 0.14 s together in a debug build
    (--report-time: 0.058, 0.017, 0.002 and 0.067 s).
  • cargo test --workspace -- --test-threads=4, run side by side: main 264 passed, 0 failed,
    4 ignored; this branch 268 passed (the four new tests), 0 failed, 4 ignored.
  • cargo clippy -p victory-formats --tests: no warnings. cargo fmt --check reports nothing in the
    changed lines (the crate has older differences, which this does not touch).
  • With xma: start the frame after a continuation at the next packet that has frames #96 merged on top of this branch: cargo test -p victory-formats --lib xma:: gives
    10 passed, 2 ignored.

Not tested

  • Real XMA streams and the reference comparison (walks_saved_streams, matches_a_reference_decoder
    need files outside the repository); a title.
  • Linux, AMD and Intel GPUs (the change is CPU code only).

Open PRs on this file: #96 (next_frame), #112 (formatting of xma.rs) and #172 (IMDCT tables)
touch xma.rs; each merges with this branch without a textual conflict (git merge-tree), and the
tests here pass with #96 merged in (not run with #112 or #172). #88 changes only audio.rs. The
packet offset fix of #50 is already on main.

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

Decoder::decode took the caller's bit count as the end of the frame. Past
the data the bit reader returns zeros, and a stereo tile header reading
only zeros gives the subframe to no channel, so it never moves on; with a
count beyond the data it never overreads either, and looped for good.
Clamp the end to the bits the data holds. A frame from read_frame always
holds its bits, so its decoding does not change.

Add deterministic noise tests (xorshift64, fixed seeds, as BCPack's):
buffers of noise packets, streams of noise frames with clean and damaged
headers, frames in mono and stereo at each sample rate, bit counts past the
data, and read_frame and packet at any offset. Every walk is bounded and
every decode must give one frame of samples. The bit-count test hangs
without the fix; the others pass on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DaviTostes

Copy link
Copy Markdown

Tested d4d1822 on Arch Linux x86_64, rustc 1.98.1: pass.

  • cargo test -p victory-formats: 24 passed, 0 failed, 3 ignored (2 test binaries), including the 4 tests this PR adds.

No game data used.

edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
…of looping forever

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@yuruiwang233

Copy link
Copy Markdown

Independent verification on Windows 11, x86_64-pc-windows-gnu (rustc 1.98.1, MinGW-w64 15.2; GNU toolchain, not the MSVC one CI uses).

  • With the branch applied, cargo test -p victory-formats --lib --locked: 27 passed, 0 failed, 4 ignored.
  • cargo test -p victory-formats --lib xma::: 9 passed, 0 failed, 2 ignored (the two reference-file tests), in 0.25 s.
  • cargo clippy -p victory-formats --all-targets --locked -- -D warnings: clean.

Regression check: I kept the new tests and restored only end: bits. xma::tests::noise_frames_with_bit_counts_past_the_data was still running after 90 s and had to be killed; with the clamp it finishes in 0.25 s in the same binary. So the spin is real and this line is what ends it.

Scope of the behaviour change, from reading the code: read_frame builds data as bits.div_ceil(8) bytes, so bits <= data.len() * 8 holds for every frame the walker produces and the clamp is a no-op there. It only changes direct decode(data, bits, ...) calls whose bits exceed the buffer, where Bits::peek32 used to read zeros past the data. For those, overread() now trips at the data end instead of at bits, the frame returns Err, and the caller keeps the stream timing as it already did.

Not verified here: real streams (VICTORY_XMA_STREAMS data outside the repo) and the reference decoder comparison.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
@Marvisak
Marvisak merged commit 338808e into Marvisak:main Nov 25, 2026

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate (mainly for the tests). Decoder::decode in crates/victory-formats/src/xma.rs now clamps the bit reader's end to the data (bits.min(data.len() * 8)); before, a bit count past the data made a stereo tile header spin forever on zero bits. The only caller, decode in crates/victory-hle/src/modules/audio.rs, passes a Frame from read_frame whose data always holds its bits, so this does not change behaviour in a run; it makes the public entry point safe on its own. Most of the value is the four deterministic noise tests (noisy and damaged streams, all sample rates, bit counts past the data, read_frame/packet at edge offsets), with no new dependencies.

CI: no check runs on the head commit.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants