From adb608af1fda3a16e7d4e51727c383da2ca33c9e Mon Sep 17 00:00:00 2001 From: AnoHunted <360473103+AnoHunted@users.noreply.github.com> Date: Tue, 24 Nov 2026 04:38:44 +0000 Subject: [PATCH] Audio: an XMA write block past the ring's end wraps before the first write The write block (context word 0, bits 27-31) was used as it is for the first block of a pass, so one at or past the ring size (bits 22-26) was written past the ring's end, possibly into another APU allocation, before it wrapped with (write + 1) % size. Take it modulo the ring first, as the read block is taken, and as advance_read does. (cherry picked from commit 7a5d85e6a2867081ed54515ce7f33bae3fcb643d) --- crates/victory-hle/src/modules/audio.rs | 88 ++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/crates/victory-hle/src/modules/audio.rs b/crates/victory-hle/src/modules/audio.rs index 644e032..8da3346 100644 --- a/crates/victory-hle/src/modules/audio.rs +++ b/crates/victory-hle/src/modules/audio.rs @@ -520,7 +520,9 @@ fn pump(context: Context, voice: &mut Voice) { if voice.pcm.len() < SAMPLES_PER_BLOCK && !decode(context, voice) { return; } - let write = context.field(0, 27, 5); + // The write block is the title's too: taken modulo the ring before + // the first write, which would otherwise land past the ring's end. + let write = context.field(0, 27, 5) % size; let address = context.get(7) as usize + write as usize * BLOCK_BYTES; let Some(out) = heap_bytes(address as u32, BLOCK_BYTES) else { return; @@ -1091,6 +1093,90 @@ mod tests { assert!(ring[0x200..].iter().all(|&b| b == 0x55)); } + /// A write block past the end of the ring (6 on a ring of four) is block + /// 2 of it: blocks 2, 3 and 0 are filled, up to the read block (1). It + /// used to write block 6 first, past the ring's end. + #[test] + fn xma_write_blocks_past_the_ring_end_wrap() { + let _serial = lock(&CONTEXT_TESTS); + let mut request = ShapeContexts { + counts: [0, 0, 0, 0, 4, 0], + arrays: [std::ptr::null_mut(); 6], + apu: [0; 6], + }; + assert_eq!(AcpHalAllocateShapeContexts(&mut request), S_OK); + let context = Context(request.arrays[4].cast::().wrapping_add(16)); + let (mut input, mut input_apu) = (std::ptr::null_mut(), 0); + assert_eq!(ApuAlloc(&mut input, &mut input_apu, 0x1000, 0x800, 1), S_OK); + let stream = silent_stream(); + // SAFETY: two packets were allocated. + unsafe { std::ptr::copy_nonoverlapping(stream.as_ptr(), input.cast(), stream.len()) }; + let (mut output, mut output_apu) = (std::ptr::null_mut(), 0); + assert_eq!( + ApuAlloc(&mut output, &mut output_apu, 0x800, 0x100, 2), + S_OK + ); + // SAFETY: the allocation was just made: the ring (four blocks) and a guard + // area after it, which the pass must not touch. + unsafe { output.cast::().write_bytes(0x55, 0x800) }; + + // As in `xma_contexts_decode_into_their_output_rings`, but looping + // forever from the first frame to the last; write block 6, read block 1. + context.set(0, 2 | 255 << 12 | 1 << 20 | 4 << 22 | 6 << 27); + context.set(1, 1 << 31 | 3 << 27); + context.set(2, 32); + context.set(3, 32); + context.set(4, (xma::PACKET_BYTES * 8 + 32 + 2 * 31) as u32); + context.set(5, input_apu); + context.set(7, output_apu); + context.set(9, 1); + + let mut acp = std::ptr::null_mut(); + assert_eq!(AcpHalCreate(&mut acp), S_OK); + // SAFETY: just created. + let m = unsafe { (*acp).vtable }; + assert_eq!((m.connect)(acp, 0x1000, 0x1000), S_OK); + let submit = |command: u32, words: &[u32]| { + assert_eq!( + (m.submit)(acp, command, 0xf1, u32::MAX, words.as_ptr().cast(), 0), + S_OK + ); + }; + submit(ENABLE_XMA, &[1]); + // A pass on a thread of its own (the decoding thread runs them too), + // so that one that never ends fails this test instead of hanging it. + let (done, passed) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + xma().pump(); + let _ = done.send(()); + }); + let five = std::time::Duration::from_secs(5); + let wait = || passed.recv_timeout(five).is_ok(); + let ended = wait(); + if !ended { + // End it as a full ring would, to release the contexts below. + context.set_field(1, 31, 1, 0); + assert!(wait(), "the XMA pass did not end on a full ring either"); + } + let (write, writable) = (context.field(0, 27, 5), context.field(1, 31, 1)); + // SAFETY: the ring. + let ring = unsafe { std::slice::from_raw_parts(output.cast::(), 0x800) }.to_vec(); + + submit(DISABLE_XMA, &[1]); + (m.release)(acp); + ApuFree(input); + ApuFree(output); + assert_eq!(AcpHalReleaseShapeContexts(), S_OK); + assert!(ended, "the XMA pass never ended"); + // Blocks 2, 3 and 0 written (silence), 1 left for the title, and + // nothing past the ring. + assert_eq!((write, writable), (1, 0)); + assert!(ring[0x400..].iter().all(|&b| b == 0x55), "written past the ring"); + let block = |n: usize| &ring[n * 0x100..(n + 1) * 0x100]; + assert!([0, 2, 3].iter().all(|&n| block(n).iter().all(|&b| b == 0))); + assert!(block(1).iter().all(|&b| b == 0x55)); + } + #[test] fn xma_contexts_decode_into_their_output_rings() { let _serial = lock(&CONTEXT_TESTS);