Repository navigation
XMA: noise tests, and end a frame at its data instead of looping forever - #183
Conversation
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>
|
Tested
No game data used. |
…of looping forever Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
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).
Regression check: I kept the new tests and restored only Scope of the behaviour change, from reading the code: Not verified here: real streams ( |
…tead of looping forever
…tead of looping forever
…tead of looping forever
|
Automated triage for the maintainers. Merge candidate (mainly for the tests). CI: no check runs on the head commit. Generated by Claude Code |
What
Decoder::decodeends a frame's bits at the end of its data:end: bits.min(data.len().saturating_mul(8)). A frame fromread_framealways holds its bits(
data.len() == bits.div_ceil(8)), so its decoding does not change.xma.rs, like BCPack'snoise_never_panics: xorshift64, fixedseeds, bounded rounds, no new dependencies.
noise_packets_walk_and_decode: 60 buffers of 1 to 4 packets: pure noise, streams of noiseframes 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.rsdoes and decodes each,mono and stereo. Each
nextmust lie after its frame, the walk must stay withinbits / 16frames, 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 at24, 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 countspast the data, up to
usize::MAX.noise_offsets_read_a_frame_or_nothing:read_framefrom packet edges, the end of the buffer,usize::MAX / 2, the last packet-aligned bit belowusize::MAX,usize::MAXand random bits;packetat indices up tousize::MAX. A frame read must have a length of 16 to 32766 bits, thatmany bits of data, and a
nextafter its start and inside the buffer.Why
decodetook itsbitsargument as the end of the frame. Past the data the bit reader returnszeros. In a stereo tile header, zero bits give the subframe to no channel, so
min_channel_lenfalls back and the loop never moves on; its only way out is then
overread(), which a count past thedata never reaches. One byte of data with a count of about
usize::MAX / 2spun for good. A monotile header always moves on, so only stereo hung.
No caller passes such a count today:
audio.rshandsdecodetheFramethatread_framebuilt.The change makes
decodesafe 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.
cargo test -p victory-formats --lib xma::tests::noise_ -- --test-threads=1hung innoise_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.
cargo test -p victory-formats --lib xma::gives 9 passed, 2 ignored (they need filesoutside 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 --checkreports nothing in thechanged lines (the crate has older differences, which this does not touch).
cargo test -p victory-formats --lib xma::gives10 passed, 2 ignored.
Not tested
walks_saved_streams,matches_a_reference_decoderneed files outside the repository); a title.
Open PRs on this file: #96 (
next_frame), #112 (formatting ofxma.rs) and #172 (IMDCT tables)touch
xma.rs; each merges with this branch without a textual conflict (git merge-tree), and thetests here pass with #96 merged in (not run with #112 or #172). #88 changes only
audio.rs. Thepacketoffset fix of #50 is already on main.AI-assisted (Claude Code, Claude Opus 5.5). No game files were used.