Преглед на файлове

Merge pull request #88 from AndyLatimer/fix/title-size-overflows

Audio, watch: no overflow on huge title sizes (XMA read advance, fread)
Marvis преди 2 дни
родител
ревизия
2005e35afd
променени са 2 файла, в които са добавени 22 реда и са изтрити 3 реда
  1. 13 2
      crates/victory-gpu/src/watch.rs
  2. 9 1
      crates/victory-hle/src/modules/audio.rs

+ 13 - 2
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<usize> = 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);
+    }
 }

+ 9 - 1
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);