Bladeren bron

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 <[email protected]>
Manuel Pérez 3 dagen geleden
bovenliggende
commit
ec0d155925
1 gewijzigde bestanden met toevoegingen van 13 en 2 verwijderingen
  1. 13 2
      crates/victory-gpu/src/watch.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 {
@@ -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);
+    }
 }