From fba9d1d3a95eee1a3d597f68c80c3c0d5cb41ffd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9rez?= Date: Tue, 24 Nov 2026 01:21:41 +0100 Subject: [PATCH 1/2] Audio: reduce the title's XMA read advance before adding it ADVANCE_XMA_READ added the title's block count to the 5-bit read block in u32 before taking it modulo the ring size, so a count near u32::MAX overflowed (a panic in debug builds). The count is reduced modulo the ring first; the result is the same for every count that did not overflow. Co-Authored-By: Claude Opus 5.5 --- crates/victory-hle/src/modules/audio.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/victory-hle/src/modules/audio.rs b/crates/victory-hle/src/modules/audio.rs index 0a11195..4bd223a 100644 --- a/crates/victory-hle/src/modules/audio.rs +++ b/crates/victory-hle/src/modules/audio.rs @@ -471,7 +471,8 @@ impl Xma { // The decoder writes the same words. let mut slot = lock(&self.voices[index as usize]); let size = context.field(0, 22, 5).max(1); - let read = (context.field(9, 0, 5) + blocks) % size; + // The count is the title's: reduced first, so the sum stays small. + let read = (context.field(9, 0, 5) + blocks % size) % size; context.set_field(9, 0, 5, read); if blocks > 0 { context.set_field(1, 31, 1, 1); @@ -1062,6 +1063,13 @@ mod tests { assert_eq!(context.field(0, 20, 2), 0, "input used up"); assert_eq!(context.field(2, 26, 5), INPUT_INVALID); + // A count near `u32::MAX` wraps the read block like any other + // (3 + 0xffff_ffff blocks on a ring of four is block 2); the sum + // used to overflow. + context.set_field(9, 0, 5, 3); + submit(ADVANCE_XMA_READ, &[1, u32::MAX]); + assert_eq!(context.field(9, 0, 5), 2); + submit(DISABLE_XMA, &[1]); assert_eq!(produced(&xma()), None); (m.release)(acp); From d0577acc0a55b3ecf3fb1f9cfff1f211d70f6937 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9rez?= Date: Tue, 24 Nov 2026 01:21:42 +0100 Subject: [PATCH 2/2] Watch: saturate the end of announced writes and remaps kernel_will_write (fread's size times count, saturated) and title_memory_remapped added the length to the address unchecked, so a huge fread count overflowed (a panic in debug builds). The end now saturates at the top of the address space. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/watch.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/victory-gpu/src/watch.rs b/crates/victory-gpu/src/watch.rs index a64e3e3..2012acd 100644 --- a/crates/victory-gpu/src/watch.rs +++ b/crates/victory-gpu/src/watch.rs @@ -210,7 +210,8 @@ fn open(g: &mut Guards, start: usize, end: usize) -> bool { /// The runtime calls it (as `kernel_will_write`) before the title's kernel /// reads into its own memory. pub fn writing(at: usize, len: usize) { - open(&mut guards(), at, at + len); + // The length is the title's (`fread`'s size times count, saturated). + open(&mut guards(), at, at.saturating_add(len)); } /// The runtime mapped or unmapped title memory `[at, at + len)`: what it @@ -219,7 +220,7 @@ pub fn title_memory_remapped(at: usize, len: usize) { let mut g = guards(); let blocks: Vec = g .protected - .range(at / BLOCK..(at + len).div_ceil(BLOCK)) + .range(at / BLOCK..at.saturating_add(len).div_ceil(BLOCK)) .copied() .collect(); for block in blocks { @@ -364,4 +365,14 @@ mod tests { unsafe { UnmapViewOfFile(windows_sys::Win32::System::Memory::MEMORY_MAPPED_VIEW_ADDRESS { Value: view as *mut _ }) }; title_memory_remapped(view, 2 * BLOCK); } + + /// A write or mapping that runs past the end of the address space (a + /// title's `fread` with a huge count) covers up to the end; the sum used + /// to overflow. Up there no test has guarded blocks. + #[test] + fn ranges_past_the_address_space_end_do_not_overflow() { + let top = usize::MAX - 16; + writing(top, PAGE); + title_memory_remapped(top, usize::MAX); + } }