diff --git a/crates/victory-gpu/src/watch.rs b/crates/victory-gpu/src/watch.rs index 1cbe2b7..7694609 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 { @@ -367,4 +368,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); + } } 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);