From 20d67ebbfff6377136d6eadc94c53e67da1ce585 Mon Sep 17 00:00:00 2001 From: japabu Date: Wed, 11 Nov 2026 16:22:09 +0200 Subject: [PATCH 1/7] A spawn reserves its stack once, and a copied page is written once `loader::spawn` reserved the guard and the stack as one range and then reserved the stack again over it, so every spawn removed a range, cut another and walked, to clear them, the 8 MiB of empty leaves it had just built tables for. The stack is now placed once with its guard (`AddressSpace::reserve_above_guard`): two ranges side by side, nothing replaced. Metal boot 2 timed the stack stage at 6.4-7.4 us of a tiny program's 17-22 us SYS_SPAWN on both benches. `space::owned_frame` zeroed a copied range's frame and then copied the program's bytes over it. It now takes the frame as its last owner left it (`pmm::alloc_promised_unzeroed`) and writes each byte once (`vm::copied_page`): the copied bytes, and zeros only past them. `CopySource::read` writes every byte it is asked for, so nothing the frame held survives; the host test checks that for none, some and all of a page. Co-Authored-By: Claude Opus 5.5 --- kernel/pure/vm.rs | 23 +++++++++++++++++++++ kernel/src/loader/mod.rs | 5 +---- kernel/src/mm/pmm.rs | 8 +++++++- kernel/src/mm/space.rs | 44 ++++++++++++++++++++++++---------------- 4 files changed, 57 insertions(+), 23 deletions(-) diff --git a/kernel/pure/vm.rs b/kernel/pure/vm.rs index a462a7ca04e..8256a19197a 100644 --- a/kernel/pure/vm.rs +++ b/kernel/pure/vm.rs @@ -35,6 +35,14 @@ pub fn around(start: u64, end: u64, page: u64, populated: impl Fn(u64) -> bool) (run.max(start), (run + AROUND).min(end)) } +/// Make `frame`, which holds what its last owner left, a copied range's page: +/// its first `n` bytes as `read` writes them, and zeros after. +pub fn copied_page(frame: &mut [u8], n: usize, read: impl FnOnce(&mut [u8])) { + let (bytes, rest) = frame.split_at_mut(n); + read(bytes); + rest.fill(0); +} + /// What a page may be used for. No variant is both writable and executable. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum Prot { @@ -432,6 +440,21 @@ mod tests { } } + /// A copied page holds its bytes and zeros after them, whatever the + /// frame held: none of them, some and all. + #[test] + fn a_copied_page_keeps_nothing_its_frame_held() { + for n in [0, 100, PAGE as usize] { + let mut frame = vec![0xa5u8; PAGE as usize]; + copied_page(&mut frame, n, |bytes| { + assert_eq!(bytes.len(), n); + bytes.fill(7); + }); + assert!(frame[..n].iter().all(|&b| b == 7), "{n} bytes copied"); + assert!(frame[n..].iter().all(|&b| b == 0), "zeros past {n} bytes"); + } + } + /// An object by number. #[derive(Clone, Copy, PartialEq, Eq, Debug)] struct Obj(u32); diff --git a/kernel/src/loader/mod.rs b/kernel/src/loader/mod.rs index 7fe14170cc7..5582965f6ff 100644 --- a/kernel/src/loader/mod.rs +++ b/kernel/src/loader/mod.rs @@ -186,16 +186,13 @@ pub fn spawn( return Err(SyscallError::ResourceExhausted.into()); }; let (stack_base, sp) = { - use crate::mm::space::At; use kernel::vm::Prot; let mut space = child_pt.lock(); crate::clock::map_page(&mut space); lay(&mut space, &maps)?; // Never executable, and a reserved guard below it, so running off the // stack's bottom faults instead of writing what lies there. - let stack = space.reserve(STACK_GUARD + USER_STACK_SIZE as u64, At::ANYWHERE, Prot::None)?; - let stack_base = stack + STACK_GUARD; - space.reserve(USER_STACK_SIZE as u64, At::Fixed(stack_base), Prot::ReadWrite)?; + let stack_base = space.reserve_above_guard(STACK_GUARD, USER_STACK_SIZE as u64, Prot::ReadWrite)?; (stack_base, stack_base.raw() + USER_STACK_SIZE as u64) }; diff --git a/kernel/src/mm/pmm.rs b/kernel/src/mm/pmm.rs index 8ae8c06a3aa..c1aca1fc53b 100644 --- a/kernel/src/mm/pmm.rs +++ b/kernel/src/mm/pmm.rs @@ -301,12 +301,18 @@ pub unsafe fn discard(phys: u64) -> Discarded { /// A zeroed frame a promise held, which the promise guarantees is free. pub fn alloc_promised() -> Frame { - let frame = Frame { phys: with(Frames::alloc_promised) }; + let frame = alloc_promised_unzeroed(); // SAFETY: just taken off the allocator, so unaliased, and inside the direct map. unsafe { core::ptr::write_bytes(frame.direct_map().as_mut_ptr::(), 0, FRAME as usize) }; frame } +/// [`alloc_promised`]'s frame holding what its last owner left: its caller +/// writes every byte of it before anything maps it. +pub fn alloc_promised_unzeroed() -> Frame { + Frame { phys: with(Frames::alloc_promised) } +} + /// A zeroed frame for a page table of `job`'s, refused rather than take what /// is held back: a process's tables are not promised. It goes back through /// [`free_held`]. diff --git a/kernel/src/mm/space.rs b/kernel/src/mm/space.rs index 16ba4c85dbe..69d141b837b 100644 --- a/kernel/src/mm/space.rs +++ b/kernel/src/mm/space.rs @@ -427,6 +427,20 @@ impl AddressSpace { self.private(len, at, prot, Kind::Private { committed: prot != Prot::None }) } + /// [`reserve`](Self::reserve)'s `len` bytes wherever they fit above + /// `guard` bytes reserved with them, which nothing backs: a stack, and + /// the fault below it. All or nothing. + pub fn reserve_above_guard(&mut self, guard: u64, len: u64, prot: Prot) -> Result { + let whole = guard.checked_add(len).ok_or(SyscallError::InvalidArgument)?; + let below = UserAddr::new(self.place(whole, At::ANYWHERE)?); + self.reserve(guard, At::Fixed(below), Prot::None)?; + let at = self.reserve(len, At::Fixed(below + guard), prot); + if at.is_err() { + self.release(below, guard); + } + at + } + /// `len` bytes of private memory, committed, whose pages start as a copy /// of `source` from `offset` as far as their first `copied` bytes go and /// zeros past them, each taken at its first touch: a program's segment @@ -930,26 +944,20 @@ fn copyable(entry: &SpaceEntry) -> bool { } /// A fresh frame for `page` of the committed range `entry` at `start`: zeros, -/// or its source's bytes for a copied range. +/// or for a copied range as many of its source's bytes as the range's copy +/// and the source both hold, and zeros after them. fn owned_frame(start: u64, entry: &SpaceEntry, page: u64) -> u64 { - let phys = pmm::alloc_promised().into_raw(); - if let Kind::Copy { source, offset, len } = &entry.kind { - copy_in(&**source, offset + (page - start), len.saturating_sub(page - start), phys); - } - phys -} - -/// Write `source`'s bytes from `offset` into the zeroed frame at `phys`, as -/// many of the page's as `len` and the source both hold. -fn copy_in(source: &dyn CopySource, offset: u64, len: u64, phys: u64) { - let n = len.min(PAGE_SIZE).min(source.size().saturating_sub(offset)) as usize; - if n == 0 { - return; - } + let Kind::Copy { source, offset, len } = &entry.kind else { + return pmm::alloc_promised().into_raw(); + }; + let (at, offset) = (page - start, offset + (page - start)); + let n = len.saturating_sub(at).min(PAGE_SIZE).min(source.size().saturating_sub(offset)) as usize; + let phys = pmm::alloc_promised_unzeroed().into_raw(); // SAFETY: a frame just allocated to this page, which no leaf maps yet, so - // nothing else reaches it; `n` bytes stay inside it. - let frame = unsafe { core::slice::from_raw_parts_mut(super::DirectMap::from_phys(phys).as_mut_ptr::(), n) }; - source.read(offset, frame); + // nothing else reaches it. + let frame = unsafe { core::slice::from_raw_parts_mut(super::DirectMap::from_phys(phys).as_mut_ptr::(), PAGE_SIZE as usize) }; + vm::copied_page(frame, n, |bytes| source.read(offset, bytes)); + phys } /// Every frame the space owns goes back; its tables go with `UserTables`. From a70411f77d1ce6ebead41cd59fbc1499ab973f5a Mon Sep 17 00:00:00 2001 From: japabu Date: Wed, 11 Nov 2026 17:29:21 +0200 Subject: [PATCH 2/7] A copy source writes every byte it is asked for A copied range's page now takes its frame unzeroed, so `CopySource::read` leaving any byte of `out` unwritten would hand the process what the frame's last owner left there. The trait states it. Co-Authored-By: Claude Opus 5.5 --- kernel/src/mm/space.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/src/mm/space.rs b/kernel/src/mm/space.rs index 69d141b837b..288d01b4f3c 100644 --- a/kernel/src/mm/space.rs +++ b/kernel/src/mm/space.rs @@ -46,7 +46,9 @@ type SpaceKind = Kind, Arc>; /// hold, and it cannot fail: what it reads was fixed when the range was made. pub trait CopySource: Send + Sync { fn size(&self) -> u64; - /// `out.len()` bytes from `offset`, all inside [`size`](Self::size). + /// Write every byte of `out` from `offset`, all inside + /// [`size`](Self::size): `out` is a frame that holds what its last owner + /// left. fn read(&self, offset: u64, out: &mut [u8]); } From e7a2c62593a509e3604daf38c06fbd54ca2647dc Mon Sep 17 00:00:00 2001 From: japabu Date: Wed, 11 Nov 2026 17:32:24 +0200 Subject: [PATCH 3/7] A pinned frame a discard keeps is filled as new, written once `AddressSpace::discard` kept a frame a user copy pins and zeroed it in place, then copied a copied range's source bytes over it through `copy_in`, which the previous commit removed while this caller still named it: the kernel did not build. Both a fresh frame (`owned_frame`) and a kept one now go through `fill_as_new`, which writes every byte of the frame once as its range starts the page: zeros for a private page, the source's bytes and zeros past them (`vm::copied_page`) for a copied one. A fresh private page's frame is zeroed there instead of in `pmm`, the same writes. A kept frame outside every range panics: `discard` checked that its range is covered. Co-Authored-By: Claude Opus 5.5 --- kernel/src/mm/space.rs | 56 +++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/kernel/src/mm/space.rs b/kernel/src/mm/space.rs index 288d01b4f3c..b8ce3e33480 100644 --- a/kernel/src/mm/space.rs +++ b/kernel/src/mm/space.rs @@ -611,10 +611,10 @@ impl AddressSpace { /// its source again, an object's page is mapped again from the object. /// /// A private page's frame is freed and the page promised a frame again; - /// one a user copy pins is zeroed in place instead and kept, since freed - /// it would come back only at its last unpin and the promise would be - /// backed by nothing meanwhile (`Frames::discard`). No pin is taken - /// meanwhile: a copy pins under this space's lock, which this holds. + /// one a user copy pins is filled as new in place instead and kept, + /// since freed it would come back only at its last unpin and the promise + /// would be backed by nothing meanwhile (`Frames::discard`). No pin is + /// taken meanwhile: a copy pins under this space's lock, which this holds. pub fn discard(&mut self, start: UserAddr, len: u64) -> Result<(), SyscallError> { let (start, end) = self.held(start, len)?; let mut present = false; @@ -649,14 +649,11 @@ impl AddressSpace { false } pmm::Discarded::Kept => { + let (s, entry) = inventory.find(va).unwrap_or_else(|| panic!("space: a frame owned at {va:#x} outside every range")); // SAFETY: the page's own frame, which a copy may be - // reading or writing as the page's contents; zeroing - // it is what the discard makes those contents. - unsafe { core::ptr::write_bytes(super::DirectMap::from_phys(phys).as_mut_ptr::(), 0, PAGE_SIZE as usize) }; - // A copied page's contents start as its source's again. - if let Some((s, Entry { kind: Kind::Copy { source, offset, len }, .. })) = inventory.find(va) { - copy_in(&**source, offset + (va - s), len.saturating_sub(va - s), phys); - } + // reading or writing as the page's contents; what the + // page starts as is what the discard makes them. + unsafe { fill_as_new(entry, va - s, phys) }; true } }; @@ -892,7 +889,7 @@ impl AddressSpace { let phys = match leaves.get(page) { Leaf::Mapped(m) => m.phys, // A frame the page keeps while no leaf maps it: a discard that - // zeroed a pinned frame in place. + // filled a pinned frame as new in place. Leaf::Held(phys) => { leaves.set(page, Leaf::Mapped(Mapping { phys, prot, owned: true, cache: CachePolicy::Normal })); phys @@ -945,23 +942,38 @@ fn copyable(entry: &SpaceEntry) -> bool { } } -/// A fresh frame for `page` of the committed range `entry` at `start`: zeros, -/// or for a copied range as many of its source's bytes as the range's copy -/// and the source both hold, and zeros after them. +/// A fresh frame for `page` of the committed range `entry` at `start`. fn owned_frame(start: u64, entry: &SpaceEntry, page: u64) -> u64 { - let Kind::Copy { source, offset, len } = &entry.kind else { - return pmm::alloc_promised().into_raw(); - }; - let (at, offset) = (page - start, offset + (page - start)); - let n = len.saturating_sub(at).min(PAGE_SIZE).min(source.size().saturating_sub(offset)) as usize; let phys = pmm::alloc_promised_unzeroed().into_raw(); // SAFETY: a frame just allocated to this page, which no leaf maps yet, so // nothing else reaches it. - let frame = unsafe { core::slice::from_raw_parts_mut(super::DirectMap::from_phys(phys).as_mut_ptr::(), PAGE_SIZE as usize) }; - vm::copied_page(frame, n, |bytes| source.read(offset, bytes)); + unsafe { fill_as_new(entry, page - start, phys) }; phys } +/// Write every byte of the frame at `phys` as the page `at` bytes into the +/// committed range `entry` starts: zeros, or for a copied range as many of +/// its source's bytes as the range's copy and the source both hold, and +/// zeros after them. +/// +/// # Safety +/// +/// `phys` is the page's own frame, which nothing reaches meanwhile but a +/// user copy that pins the page. +unsafe fn fill_as_new(entry: &SpaceEntry, at: u64, phys: u64) { + // SAFETY: the caller's; a frame is a page of RAM the direct map reaches. + let frame = unsafe { core::slice::from_raw_parts_mut(super::DirectMap::from_phys(phys).as_mut_ptr::(), PAGE_SIZE as usize) }; + match &entry.kind { + Kind::Private { .. } => frame.fill(0), + Kind::Copy { source, offset, len } => { + let offset = offset + at; + let n = len.saturating_sub(at).min(PAGE_SIZE).min(source.size().saturating_sub(offset)) as usize; + vm::copied_page(frame, n, |bytes| source.read(offset, bytes)); + } + Kind::Object { .. } => unreachable!("space: an object's page owns no frame"), + } +} + /// Every frame the space owns goes back; its tables go with `UserTables`. /// /// Not through [`AddressSpace::clear`]: no thread runs in a space that drops, From eafdc096f7fbe0156cb1cf4941970d1e49767e0f Mon Sep 17 00:00:00 2001 From: japabu Date: Wed, 11 Nov 2026 20:38:01 +0200 Subject: [PATCH 4/7] A page past its source reads nothing from it, and a page's frame is never kept Review of #1250, round 1. A copied range's page that takes no bytes, such as one wholly past its object's end, still called `CopySource::read` with an empty slice from past the object, whose bound panics the kernel: a spawn's range longer than its object reaches it, and so does a program whose .bss runs two or more pages past its file. `vm::copied_page` now works out how many bytes a page takes, from where, and reads only when there are some. Host tests cover pages of none, some and all, a source ending inside the page and a page wholly past it; `abuse_spawn_maps` spawns a child with such a range and reads zeros there. `AddressSpace::discard` kept a frame a user copy pinned, but user copies stopped pinning when they went through the process's own mapping, and a pin is taken only on a paged object's frames, which no space owns. A pinned frame discarded is now an invariant broken (`Frames::discard` panics), and `Discarded`, the kept arm, `discard_owned` on both architectures, `fill_as_new` and populate's held-frame arm go: `owned_frame` writes a fresh frame's every byte again. `pmm::alloc_promised_unzeroed` answers an address and is the module's own, so every `Frame` is zeroed as its doc says. Co-Authored-By: Claude Opus 5.5 --- kernel/pure/frames.rs | 59 +++++--------- kernel/pure/vm.rs | 64 +++++++++++---- kernel/src/arch/aarch64/paging.rs | 31 ------- kernel/src/arch/x86_64/paging.rs | 30 ------- kernel/src/mm/pmm.rs | 30 +++---- kernel/src/mm/space.rs | 80 +++++-------------- .../rust-tests/src/bin/abuse_spawn_maps.rs | 27 ++++++- 7 files changed, 128 insertions(+), 193 deletions(-) diff --git a/kernel/pure/frames.rs b/kernel/pure/frames.rs index 937161c4278..357ec9ca2ec 100644 --- a/kernel/pure/frames.rs +++ b/kernel/pure/frames.rs @@ -8,8 +8,9 @@ //! a free frame, or was split, which keeps 4 KiB traffic in few superframes. //! //! **A pinned frame is never reissued.** [`Pins`] counts, per frame and -//! without the allocator's lock, the user copies reading or writing it through -//! the direct map. A frame freed while pinned is deferred: the allocator does +//! without the allocator's lock, the kernel's copies reading or writing a +//! paged object's frame through the direct map; a page's frame is never +//! pinned. A frame freed while pinned is deferred: the allocator does //! not take it back until its last pin goes, and [`Pins::unpin`] says when //! that is. //! @@ -119,15 +120,6 @@ enum Which { Partial, } -/// What [`Frames::discard`] did with a frame. -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -pub enum Discarded { - /// Freed, and its page promised a frame again. - Freed, - /// Pinned by a copy, so kept for its page. - Kept, -} - /// One account's memory: what it holds, and the floor held back for it. /// Written only under the allocator's lock, by the [`Frames`] methods that /// take it, and read by anyone. @@ -733,22 +725,13 @@ impl<'m> Frames<'m> { } /// Free the frame at `phys` of a page that stays committed, promising - /// the page a frame again — or, where a user copy pins the frame, keep it - /// for the page: the caller zeroes it in place and leaves it in the - /// page's leaf, not present, for the next touch to map again. - /// - /// Sound only where no pin can be taken meanwhile: a copy pins a frame - /// under the lock of the space that maps it, which the caller holds, so a - /// frame read unpinned here stays unpinned. Then every promise is backed - /// by a frame that is free now, never by one deferred to a later unpin. - pub fn discard(&mut self, phys: u64, pins: &Pins) -> Discarded { - if pins.pinned(phys) { - return Discarded::Kept; - } + /// the page a frame again, which the frame backs at once: nothing pins a + /// page's frame, so it is free now and never deferred to a later unpin. + pub fn discard(&mut self, phys: u64, pins: &Pins) { + assert!(!pins.pinned(phys), "frames: {phys:#x} discarded pinned, and nothing pins a page's frame"); self.give_back(phys, pins); self.repromise(1); self.refill(); - Discarded::Freed } /// Promise `frames` again, which a commitment still holds and whose @@ -1378,7 +1361,7 @@ mod tests { // A frame a commitment still holds, given back, is promised to it // again; one given back by none is anyone's. - assert_eq!(m.frames.discard(drawn[0], &m.pins), Discarded::Freed); + m.frames.discard(drawn[0], &m.pins); assert_eq!(m.frames.promised(), 1); assert_eq!(held(&mut m.frames, &m.l), None, "a frame promised again went to another"); m.frames.free_drawn(&m.l, [drawn[1]], &m.pins); @@ -1478,23 +1461,17 @@ mod tests { assert_eq!((m.frames.charges(), m.frames.held_back()), ((0, 0), (1, 0))); } - /// A frame a copy pins is kept by a discard rather than freed: freed, it - /// would come back only at its last unpin, and the promise made for it - /// meanwhile would be backed by no free frame, so a touch of the page, or - /// any other promised page, could find none. + /// A discarded frame is promised to its page again, backed by itself: a + /// pinned one would come back only at its last unpin, leaving the promise + /// backed by no free frame, and nothing pins a page's frame. #[test] - fn a_discard_keeps_a_pinned_frame_and_promises_only_what_came_back() { + #[should_panic(expected = "discarded pinned")] + fn a_pinned_frame_discarded_is_a_panic() { let mut m = whole(1); - let all = PER_SUPER as u64; - assert!(m.frames.promise(&m.l, all)); - let drawn: Vec = (0..all).map(|_| m.frames.alloc_promised()).collect(); - assert!(m.pins.pin(drawn[0])); - assert_eq!(m.frames.discard(drawn[0], &m.pins), Discarded::Kept, "a pinned frame was freed"); - assert_eq!(m.frames.discard(drawn[1], &m.pins), Discarded::Freed); - assert_eq!((m.frames.promised(), m.frames.free_frames()), (1, 1)); - // The touch of the page whose frame went finds its frame. - assert_eq!(m.frames.alloc_promised(), drawn[1]); - assert_eq!(m.pins.unpin(drawn[0]), Unpinned::Kept, "a kept frame was deferred"); + assert!(m.frames.promise(&m.l, 1)); + let frame = m.frames.alloc_promised(); + assert!(m.pins.pin(frame)); + m.frames.discard(frame, &m.pins); } #[test] @@ -2131,7 +2108,7 @@ mod tests { 3 if !drawn.is_empty() => { // A discard keeps the page committed: its frame is promised again. let f = drawn.swap_remove(next() as usize % drawn.len()); - assert_eq!(m.frames.discard(f, &m.pins), Discarded::Freed); + m.frames.discard(f, &m.pins); promised += 1; } 4 if !drawn.is_empty() => { diff --git a/kernel/pure/vm.rs b/kernel/pure/vm.rs index 8256a19197a..be22e5d2ef6 100644 --- a/kernel/pure/vm.rs +++ b/kernel/pure/vm.rs @@ -35,11 +35,18 @@ pub fn around(start: u64, end: u64, page: u64, populated: impl Fn(u64) -> bool) (run.max(start), (run + AROUND).min(end)) } -/// Make `frame`, which holds what its last owner left, a copied range's page: -/// its first `n` bytes as `read` writes them, and zeros after. -pub fn copied_page(frame: &mut [u8], n: usize, read: impl FnOnce(&mut [u8])) { +/// Make `frame`, which holds what its last owner left, the page `at` bytes +/// into a range that copies `copied` bytes of a `size`-byte source from +/// `offset`: as many of the source's bytes as both go, which `read` writes +/// from the source offset it is given, and zeros after them. A page that +/// takes none, such as one wholly past the source's end, reads nothing. +pub fn copied_page(frame: &mut [u8], at: u64, offset: u64, copied: u64, size: u64, read: impl FnOnce(u64, &mut [u8])) { + let from = offset.saturating_add(at); + let n = copied.saturating_sub(at).min(frame.len() as u64).min(size.saturating_sub(from)) as usize; let (bytes, rest) = frame.split_at_mut(n); - read(bytes); + if n > 0 { + read(from, bytes); + } rest.fill(0); } @@ -398,6 +405,7 @@ impl Inventory { #[cfg(test)] mod tests { use super::*; + use std::format; use std::vec; use std::vec::Vec; @@ -440,21 +448,49 @@ mod tests { } } - /// A copied page holds its bytes and zeros after them, whatever the - /// frame held: none of them, some and all. + /// The page `at` of a range copying `copied` bytes of a `size`-byte + /// source from `offset`, from a frame that held other bytes: what it + /// asked the source for, and the page. + fn copy_page(at: u64, offset: u64, copied: u64, size: u64) -> (Option<(u64, usize)>, Vec) { + let mut frame = vec![0xa5u8; PAGE as usize]; + let mut asked = None; + copied_page(&mut frame, at, offset, copied, size, |from, bytes| { + asked = Some((from, bytes.len())); + bytes.fill(7); + }); + (asked, frame) + } + + /// A copied page holds the source's bytes as far as the range's copy and + /// the source both go, and zeros after them, whatever the frame held. #[test] fn a_copied_page_keeps_nothing_its_frame_held() { - for n in [0, 100, PAGE as usize] { - let mut frame = vec![0xa5u8; PAGE as usize]; - copied_page(&mut frame, n, |bytes| { - assert_eq!(bytes.len(), n); - bytes.fill(7); - }); - assert!(frame[..n].iter().all(|&b| b == 7), "{n} bytes copied"); - assert!(frame[n..].iter().all(|&b| b == 0), "zeros past {n} bytes"); + for (at, offset, copied, size, n) in [ + (PAGE, 0, 3 * PAGE, 4 * PAGE, PAGE), + (0, PAGE, 100, 2 * PAGE, 100), + (PAGE, 0, 3 * PAGE, PAGE + 50, 50), + (PAGE, 0, 100, 4 * PAGE, 0), + (0, 0, 0, PAGE, 0), + ] { + let (asked, frame) = copy_page(at, offset, copied, size); + let n = n as usize; + let what = format!("page {at:#x} of {copied:#x} bytes copied from {offset:#x} of {size:#x}"); + assert_eq!(asked, (n > 0).then_some((offset + at, n)), "{what}: what it read"); + assert!(frame[..n].iter().all(|&b| b == 7), "{what}: its {n} bytes"); + assert!(frame[n..].iter().all(|&b| b == 0), "{what}: zeros past {n} bytes"); } } + /// A page wholly past its source's end, which a range longer than its + /// object reaches, reads zeros and asks the source for nothing: a read + /// there is past the object, which no read may be. + #[test] + fn a_page_wholly_past_its_source_reads_nothing_from_it() { + let (asked, frame) = copy_page(2 * PAGE, 0, 3 * PAGE, PAGE); + assert_eq!(asked, None, "a read past the source's end"); + assert!(frame.iter().all(|&b| b == 0)); + } + /// An object by number. #[derive(Clone, Copy, PartialEq, Eq, Debug)] struct Obj(u32); diff --git a/kernel/src/arch/aarch64/paging.rs b/kernel/src/arch/aarch64/paging.rs index 17ea0a1971f..099e3fcae1b 100644 --- a/kernel/src/arch/aarch64/paging.rs +++ b/kernel/src/arch/aarch64/paging.rs @@ -716,37 +716,6 @@ impl UserTables { published(); } - /// [`take_owned`](Self::take_owned), where `decide` says of each frame of - /// a table's batch, with the address its leaf maps, whether the leaf - /// keeps it: a kept frame's leaf stays, every other is emptied. - pub fn discard_owned(&mut self, start: u64, end: u64, mut decide: impl FnMut(&[u64], &[u64], &mut [bool])) { - self.each_table(start, end, |base, table| { - let mut frames = [0u64; 512]; - let mut vas = [0u64; 512]; - let mut slot = [0usize; 512]; - let mut n = 0; - for i in slots(start, end, base, 12) { - if let Leaf::Held(phys) | Leaf::Mapped(Mapping { phys, owned: true, .. }) = Leaf::decode(table.0[i]) { - frames[n] = phys; - vas[n] = base + ((i as u64) << 12); - slot[n] = i; - n += 1; - } - } - if n == 0 { - return; - } - let mut kept = [false; 512]; - decide(&frames[..n], &vas[..n], &mut kept[..n]); - for k in 0..n { - if !kept[k] { - table.set(slot[k], Leaf::Empty.encode()); - } - } - }); - published(); - } - /// Empty every leaf of `[start, end)` holding a frame the space owns, one /// table of leaves at a time, handing each table's frames to `taken`: at /// most 512 at once, in a buffer of its own, so nothing is allocated. diff --git a/kernel/src/arch/x86_64/paging.rs b/kernel/src/arch/x86_64/paging.rs index 6a14b7c68ce..46135660c99 100644 --- a/kernel/src/arch/x86_64/paging.rs +++ b/kernel/src/arch/x86_64/paging.rs @@ -966,36 +966,6 @@ impl UserTables { }); } - /// [`take_owned`](Self::take_owned), where `decide` says of each frame of - /// a table's batch, with the address its leaf maps, whether the leaf - /// keeps it: a kept frame's leaf stays, every other is emptied. - pub fn discard_owned(&mut self, start: u64, end: u64, mut decide: impl FnMut(&[u64], &[u64], &mut [bool])) { - self.each_table(start, end, |base, table| { - let mut frames = [0u64; 512]; - let mut vas = [0u64; 512]; - let mut slot = [0usize; 512]; - let mut n = 0; - for i in slots(start, end, base, 12) { - if let Leaf::Held(phys) | Leaf::Mapped(Mapping { phys, owned: true, .. }) = Leaf::decode(table.0[i]) { - frames[n] = phys; - vas[n] = base + ((i as u64) << 12); - slot[n] = i; - n += 1; - } - } - if n == 0 { - return; - } - let mut kept = [false; 512]; - decide(&frames[..n], &vas[..n], &mut kept[..n]); - for k in 0..n { - if !kept[k] { - table.store(slot[k], Leaf::Empty.encode()); - } - } - }); - } - /// Empty every leaf of `[start, end)` holding a frame the space owns, one /// table of leaves at a time, handing each table's frames to `taken`: at /// most 512 at once, in a buffer of its own, so nothing is allocated. diff --git a/kernel/src/mm/pmm.rs b/kernel/src/mm/pmm.rs index c1aca1fc53b..65f42a686a6 100644 --- a/kernel/src/mm/pmm.rs +++ b/kernel/src/mm/pmm.rs @@ -18,7 +18,6 @@ use core::sync::atomic::{AtomicPtr, AtomicU32, AtomicU64, AtomicUsize, Ordering} use kernel::frames::{self, Frames, Pins, PoolSplit, Super, Unpinned, FRAME, SUPER}; use kernel::sched::job::Job; -pub use kernel::frames::Discarded; use super::{DirectMap, PAGE_2M}; use crate::sync::Lock; @@ -262,8 +261,9 @@ fn kept_supers(frames: &Frames) -> u64 { } /// Promise `frames` to a commitment of `job`'s, which draws each with -/// [`alloc_promised`] when it is first touched; `false`, and nothing -/// promised, where that would take what is held back. +/// [`alloc_promised`] or [`alloc_promised_unzeroed`] when it is first +/// touched; `false`, and nothing promised, where that would take what is +/// held back. pub fn promise(job: &Job, frames: u64) -> bool { frames == 0 || with(|f| f.promise(job.memory(), frames)) } @@ -288,29 +288,29 @@ pub fn unpromise(job: &Job, frames: u64) { } } -/// [`Frames::discard`] of `phys`, a committed page's frame the caller holds -/// the lock of the mapping space for. +/// Give back `phys`, the frame of a page that stays committed, and promise +/// the page a frame again ([`Frames::discard`]). /// /// # Safety -/// `phys` is an owned leaf's frame, which no leaf on any CPU maps any more, -/// unless this answers [`Discarded::Kept`]. -pub unsafe fn discard(phys: u64) -> Discarded { +/// `phys` is an owned leaf's frame, which no leaf on any CPU maps any more. +pub unsafe fn discard(phys: u64) { let pins = pins(); - with(|f| f.discard(phys, &pins)) + with(|f| f.discard(phys, &pins)); } /// A zeroed frame a promise held, which the promise guarantees is free. pub fn alloc_promised() -> Frame { - let frame = alloc_promised_unzeroed(); + let frame = Frame { phys: with(Frames::alloc_promised) }; // SAFETY: just taken off the allocator, so unaliased, and inside the direct map. unsafe { core::ptr::write_bytes(frame.direct_map().as_mut_ptr::(), 0, FRAME as usize) }; frame } -/// [`alloc_promised`]'s frame holding what its last owner left: its caller -/// writes every byte of it before anything maps it. -pub fn alloc_promised_unzeroed() -> Frame { - Frame { phys: with(Frames::alloc_promised) } +/// [`alloc_promised`]'s frame holding what its last owner left, by its +/// address: its caller writes every byte of it before anything maps it, and +/// gives it back through [`free_drawn`]. +pub(super) fn alloc_promised_unzeroed() -> u64 { + with(Frames::alloc_promised) } /// A zeroed frame for a page table of `job`'s, refused rather than take what @@ -451,7 +451,7 @@ pub fn alloc_frame() -> Option { Some(frame) } -/// Pin every managed frame `[phys, phys + len)` touches so none is reissued while a user-copy window covers it; `false`, and nothing pinned, on a pin-count overflow. +/// Pin every managed frame `[phys, phys + len)` touches so none is reissued while a copy reads or writes it; `false`, and nothing pinned, on a pin-count overflow. pub fn pin_range(phys: u64, len: usize) -> bool { let pins = pins(); let frames = frame_span(phys, len); diff --git a/kernel/src/mm/space.rs b/kernel/src/mm/space.rs index b8ce3e33480..3c5f07c0b1e 100644 --- a/kernel/src/mm/space.rs +++ b/kernel/src/mm/space.rs @@ -609,12 +609,7 @@ impl AddressSpace { /// Free every frame behind `[start, start + len)` and keep its ranges as /// they are: a private page reads zeros at its next touch, a copied one /// its source again, an object's page is mapped again from the object. - /// - /// A private page's frame is freed and the page promised a frame again; - /// one a user copy pins is filled as new in place instead and kept, - /// since freed it would come back only at its last unpin and the promise - /// would be backed by nothing meanwhile (`Frames::discard`). No pin is - /// taken meanwhile: a copy pins under this space's lock, which this holds. + /// Each freed frame's page is promised a frame again. pub fn discard(&mut self, start: UserAddr, len: u64) -> Result<(), SyscallError> { let (start, end) = self.held(start, len)?; let mut present = false; @@ -633,31 +628,15 @@ impl AddressSpace { self.shoot_down(start, end, &Unlinked::none()); } let mut freed = 0; - let inventory = &self.inventory; - self.tables.discard_owned(start, end, |frames, vas, kept| { - let mut sorted = [0u64; 512]; - let sorted = &mut sorted[..frames.len()]; - sorted.copy_from_slice(frames); - sorted.sort_unstable(); - crate::sched::futex::revoke_frames(&self.futex, sorted); - for ((&phys, &va), kept) in frames.iter().zip(vas).zip(kept.iter_mut()) { + self.tables.take_owned(start, end, |frames| { + frames.sort_unstable(); + crate::sched::futex::revoke_frames(&self.futex, frames); + for &phys in frames.iter() { // SAFETY: a frame an owned leaf held, which no leaf on any CPU - // maps now and no waiter is keyed on; one kept is the page's. - *kept = match unsafe { pmm::discard(phys) } { - pmm::Discarded::Freed => { - freed += 1; - false - } - pmm::Discarded::Kept => { - let (s, entry) = inventory.find(va).unwrap_or_else(|| panic!("space: a frame owned at {va:#x} outside every range")); - // SAFETY: the page's own frame, which a copy may be - // reading or writing as the page's contents; what the - // page starts as is what the discard makes them. - unsafe { fill_as_new(entry, va - s, phys) }; - true - } - }; + // maps now and no waiter is keyed on. + unsafe { pmm::discard(phys) }; } + freed += frames.len() as u64; }); self.usage.freed(freed); Ok(()) @@ -888,12 +867,9 @@ impl AddressSpace { let mut leaves = self.tables.leaf_table(page).unwrap_or_else(|| panic!("space: no tables under {page:#x}")); let phys = match leaves.get(page) { Leaf::Mapped(m) => m.phys, - // A frame the page keeps while no leaf maps it: a discard that - // filled a pinned frame as new in place. - Leaf::Held(phys) => { - leaves.set(page, Leaf::Mapped(Mapping { phys, prot, owned: true, cache: CachePolicy::Normal })); - phys - } + // Only a protection to `None` leaves a frame held where no leaf + // maps it, and its range allows no access. + Leaf::Held(_) => unreachable!("space: a held frame at {page:#x} in a range that allows an access"), Leaf::Empty => { // A neighbour in the next table down or up is another run's. let (lo, hi) = vm::around(start, entry.end, page, |at| leaves.covers(at) && leaves.get(at) != Leaf::Empty); @@ -942,44 +918,28 @@ fn copyable(entry: &SpaceEntry) -> bool { } } -/// A fresh frame for `page` of the committed range `entry` at `start`. +/// A fresh frame for `page` of the committed range `entry` at `start`, every +/// byte of it written: zeros, or for a copied range its source's bytes as far +/// as the range's copy and the source both go, and zeros after them. fn owned_frame(start: u64, entry: &SpaceEntry, page: u64) -> u64 { - let phys = pmm::alloc_promised_unzeroed().into_raw(); - // SAFETY: a frame just allocated to this page, which no leaf maps yet, so + let phys = pmm::alloc_promised_unzeroed(); + // SAFETY: a frame just drawn for this page, which no leaf maps yet, so // nothing else reaches it. - unsafe { fill_as_new(entry, page - start, phys) }; - phys -} - -/// Write every byte of the frame at `phys` as the page `at` bytes into the -/// committed range `entry` starts: zeros, or for a copied range as many of -/// its source's bytes as the range's copy and the source both hold, and -/// zeros after them. -/// -/// # Safety -/// -/// `phys` is the page's own frame, which nothing reaches meanwhile but a -/// user copy that pins the page. -unsafe fn fill_as_new(entry: &SpaceEntry, at: u64, phys: u64) { - // SAFETY: the caller's; a frame is a page of RAM the direct map reaches. let frame = unsafe { core::slice::from_raw_parts_mut(super::DirectMap::from_phys(phys).as_mut_ptr::(), PAGE_SIZE as usize) }; match &entry.kind { Kind::Private { .. } => frame.fill(0), - Kind::Copy { source, offset, len } => { - let offset = offset + at; - let n = len.saturating_sub(at).min(PAGE_SIZE).min(source.size().saturating_sub(offset)) as usize; - vm::copied_page(frame, n, |bytes| source.read(offset, bytes)); - } + Kind::Copy { source, offset, len } => vm::copied_page(frame, page - start, *offset, *len, source.size(), |from, bytes| source.read(from, bytes)), Kind::Object { .. } => unreachable!("space: an object's page owns no frame"), } + phys } /// Every frame the space owns goes back; its tables go with `UserTables`. /// /// Not through [`AddressSpace::clear`]: no thread runs in a space that drops, /// no CPU holds its root ([`Space::retire`]), and a private frame is mapped in -/// no other, so no CPU caches its leaves, no futex waiter is keyed on its -/// frames and no copy pins them. +/// no other, so no CPU caches its leaves and no futex waiter is keyed on its +/// frames. impl Drop for AddressSpace { fn drop(&mut self) { let committed = self.committed_pages(FLOOR, TOP); diff --git a/tests/guest/rust-tests/src/bin/abuse_spawn_maps.rs b/tests/guest/rust-tests/src/bin/abuse_spawn_maps.rs index 2b5b1d577a3..19138999edb 100644 --- a/tests/guest/rust-tests/src/bin/abuse_spawn_maps.rs +++ b/tests/guest/rust-tests/src/bin/abuse_spawn_maps.rs @@ -18,6 +18,12 @@ const SELF: &str = "/system/bin/test_rs_abuse_spawn_maps"; const NAME: &str = "test_rs_abuse_spawn_maps"; const CHILD: &str = "spawned-from-checked-ranges"; const NONE: u64 = toyos_abi::HANDLE_INVALID.0 as u64; +const PAGE: u64 = 0x1000; +/// The child that reads the last of three writable pages laid at [`PAST_AT`], +/// a copy whose last page lies wholly past its object's end. +const PAST: &str = "a range's page wholly past its object's end"; +/// Far from the program's ranges and the clock page, inside the user half. +const PAST_AT: u64 = 0x40_0000_0000; /// Spawn this program with `maps` and `entry`, its image laid out otherwise. fn spawn(maps: &[SpawnMap], entry: u64, image_base: u64) -> Result { @@ -51,8 +57,15 @@ fn runs(what: &str, image: &Image) { } fn main() { - if std::env::args().nth(1).as_deref() == Some(CHILD) { - return; + match std::env::args().nth(1).as_deref() { + Some(CHILD) => return, + Some(PAST) => { + // SAFETY: the parent laid three writable pages at `PAST_AT`. + let byte = unsafe { core::ptr::read_volatile((PAST_AT + 2 * PAGE) as *const u8) }; + assert_eq!(byte, 0, "{PAST}: read {byte}"); + return; + } + _ => {} } let image = image::image_of(SELF); let laid = image::laid(&image, &format!("{SELF}\0{CHILD}")); @@ -96,6 +109,16 @@ fn main() { // SAFETY: no type holds this number. unsafe { syscall::close(unmappable) }; + // Its first touch reads zeros and nothing from the object, whose every + // read is bounded by its size. + let size = (image.bytes().len() as u64).next_multiple_of(PAGE); + let past = image::laid(&image, &format!("{SELF}\0{PAST}")); + let mut maps = past.maps().to_vec(); + maps.push(SpawnMap { addr: PAST_AT, len: 3 * PAGE, object: honest[text].object, offset: size - PAGE, copied: PAGE, prot: VmProt::ReadWrite as u64 }); + let child = spawn(&maps, past.entry, past.image_base).unwrap_or_else(|e| panic!("{PAST}: spawn: {e:?}")); + assert_eq!(syscall::process_wait(child), Ok(End::Exited(0)), "{PAST}"); + println!("abuse_spawn_maps: {PAST}: zeros"); + runs("after every refusal", &image::image_of(SELF)); println!("abuse_spawn_maps: PASS"); } From 86c1627e90e9210b9eebf95af9e2339c814c6adc Mon Sep 17 00:00:00 2001 From: japabu Date: Wed, 11 Nov 2026 21:11:36 +0200 Subject: [PATCH 5/7] Review r2 of #1250: free_drawn's precondition, and the past-end page as a range the loader takes `free_drawn`'s Safety named where a frame came from (`Frame::into_raw`), which a space's frames, drawn by address, never meet; it now states the requirement. `alloc_promised_unzeroed`'s frames also go back through `discard`. "A page's frame is never pinned" says an address space's. The page wholly past its object is a row of the copied-page table, with a copy the loader accepts, in place of a test of one it refuses; every row is now a range `Map::checked` takes. Co-Authored-By: Claude Opus 5.5 --- kernel/pure/frames.rs | 13 +++++++------ kernel/pure/vm.rs | 18 ++++++------------ kernel/src/mm/pmm.rs | 6 +++--- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/kernel/pure/frames.rs b/kernel/pure/frames.rs index 357ec9ca2ec..32eb6c5457b 100644 --- a/kernel/pure/frames.rs +++ b/kernel/pure/frames.rs @@ -9,8 +9,8 @@ //! //! **A pinned frame is never reissued.** [`Pins`] counts, per frame and //! without the allocator's lock, the kernel's copies reading or writing a -//! paged object's frame through the direct map; a page's frame is never -//! pinned. A frame freed while pinned is deferred: the allocator does +//! paged object's frame through the direct map; an address space's frame is +//! never pinned. A frame freed while pinned is deferred: the allocator does //! not take it back until its last pin goes, and [`Pins::unpin`] says when //! that is. //! @@ -725,10 +725,11 @@ impl<'m> Frames<'m> { } /// Free the frame at `phys` of a page that stays committed, promising - /// the page a frame again, which the frame backs at once: nothing pins a - /// page's frame, so it is free now and never deferred to a later unpin. + /// the page a frame again, which the frame backs at once: nothing pins an + /// address space's frame, so it is free now and never deferred to a later + /// unpin. pub fn discard(&mut self, phys: u64, pins: &Pins) { - assert!(!pins.pinned(phys), "frames: {phys:#x} discarded pinned, and nothing pins a page's frame"); + assert!(!pins.pinned(phys), "frames: {phys:#x} discarded pinned, and nothing pins an address space's frame"); self.give_back(phys, pins); self.repromise(1); self.refill(); @@ -1463,7 +1464,7 @@ mod tests { /// A discarded frame is promised to its page again, backed by itself: a /// pinned one would come back only at its last unpin, leaving the promise - /// backed by no free frame, and nothing pins a page's frame. + /// backed by no free frame, and nothing pins an address space's frame. #[test] #[should_panic(expected = "discarded pinned")] fn a_pinned_frame_discarded_is_a_panic() { diff --git a/kernel/pure/vm.rs b/kernel/pure/vm.rs index be22e5d2ef6..ad271ce8a61 100644 --- a/kernel/pure/vm.rs +++ b/kernel/pure/vm.rs @@ -462,15 +462,19 @@ mod tests { } /// A copied page holds the source's bytes as far as the range's copy and - /// the source both go, and zeros after them, whatever the frame held. + /// the source both go, and zeros after them, whatever the frame held; a + /// page that takes none asks the source for nothing. #[test] fn a_copied_page_keeps_nothing_its_frame_held() { for (at, offset, copied, size, n) in [ (PAGE, 0, 3 * PAGE, 4 * PAGE, PAGE), (0, PAGE, 100, 2 * PAGE, 100), - (PAGE, 0, 3 * PAGE, PAGE + 50, 50), + (PAGE, 0, PAGE + 50, PAGE + 50, 50), (PAGE, 0, 100, 4 * PAGE, 0), (0, 0, 0, PAGE, 0), + // A range longer than its object: a page wholly past its end, + // where a read would be past the object, which no read may be. + (2 * PAGE, 0, 100, PAGE, 0), ] { let (asked, frame) = copy_page(at, offset, copied, size); let n = n as usize; @@ -481,16 +485,6 @@ mod tests { } } - /// A page wholly past its source's end, which a range longer than its - /// object reaches, reads zeros and asks the source for nothing: a read - /// there is past the object, which no read may be. - #[test] - fn a_page_wholly_past_its_source_reads_nothing_from_it() { - let (asked, frame) = copy_page(2 * PAGE, 0, 3 * PAGE, PAGE); - assert_eq!(asked, None, "a read past the source's end"); - assert!(frame.iter().all(|&b| b == 0)); - } - /// An object by number. #[derive(Clone, Copy, PartialEq, Eq, Debug)] struct Obj(u32); diff --git a/kernel/src/mm/pmm.rs b/kernel/src/mm/pmm.rs index 65f42a686a6..b822f0b4dd9 100644 --- a/kernel/src/mm/pmm.rs +++ b/kernel/src/mm/pmm.rs @@ -133,8 +133,8 @@ pub fn release_floor(job: &Job) { /// under one hold of the allocator's lock. /// /// # Safety -/// Each is a frame [`Frame::into_raw`] gave up, that no leaf on any CPU maps -/// any more and nothing else frees. +/// Each is a frame drawn for a committed page of `job`'s, which no leaf on +/// any CPU maps any more and nothing else frees. pub unsafe fn free_drawn(job: &Job, frames: &[u64]) { let pins = pins(); with(|f| f.free_drawn(job.memory(), frames.iter().copied(), &pins)); @@ -308,7 +308,7 @@ pub fn alloc_promised() -> Frame { /// [`alloc_promised`]'s frame holding what its last owner left, by its /// address: its caller writes every byte of it before anything maps it, and -/// gives it back through [`free_drawn`]. +/// gives it back through [`free_drawn`] or [`discard`]. pub(super) fn alloc_promised_unzeroed() -> u64 { with(Frames::alloc_promised) } From 783bb5ebe04d93b1fb673779a95304f4c387da30 Mon Sep 17 00:00:00 2001 From: japabu Date: Wed, 11 Nov 2026 21:58:38 +0200 Subject: [PATCH 6/7] issues: what #1250 owes on metal The benches are down, so #1250's A/B is owed rather than run: the spawn record names its rows, benches and what main must show. Co-Authored-By: Claude Opus 5.5 --- ...wning-a-process-costs-toyos-up-to-147-times-linuxs.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/issues/spawning-a-process-costs-toyos-up-to-147-times-linuxs.md b/issues/spawning-a-process-costs-toyos-up-to-147-times-linuxs.md index 2edc41bfd02..043df0f1b60 100644 --- a/issues/spawning-a-process-costs-toyos-up-to-147-times-linuxs.md +++ b/issues/spawning-a-process-costs-toyos-up-to-147-times-linuxs.md @@ -141,6 +141,15 @@ stays drawn whole and the window's size waits for the file-call work (the window's record). The rest's growth is read again after the first two. +- **Owed on metal:** #1250, a spawn that reserves its stack once and writes a + copied page once. An A/B of main before and after it on the T14 and the AMD + bench, of `spawn_cost` and `osbench procs` (`proc.spawn_wait.*`, + `proc.posix_spawn_wait.*`, `process.spawn_wait`). Main after must show + `proc.spawn_wait.tiny` and `proc.posix_spawn_wait.tiny` p50 lower by about the + stack stage it removed (7.0 µs on the T14 and 7.4 µs on the AMD bench, hot, + in the spawn diagnosis's boot 2), and no `proc` row slower beyond the before + run's spread. + **Exit**: these rows within Linux's on both benches in a labelled current-state comparison, or each remaining difference measured to its parts and justified. From e343eb551f6148fe49a182b651d435634b217b45 Mon Sep 17 00:00:00 2001 From: japabu Date: Thu, 12 Nov 2026 09:00:41 +0200 Subject: [PATCH 7/7] abuse_spawn_maps names its copy range's backing The past-the-object case pushed a `SpawnMap` without `backing`, a field the ABI gained with #1259 after the case was written; it is a copy range (`SpawnBacking::Copy`), as every range this test builds by hand is. Co-Authored-By: Claude Opus 5.5 --- tests/guest/rust-tests/src/bin/abuse_spawn_maps.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/guest/rust-tests/src/bin/abuse_spawn_maps.rs b/tests/guest/rust-tests/src/bin/abuse_spawn_maps.rs index 19138999edb..9fbf7e33527 100644 --- a/tests/guest/rust-tests/src/bin/abuse_spawn_maps.rs +++ b/tests/guest/rust-tests/src/bin/abuse_spawn_maps.rs @@ -10,7 +10,7 @@ mod image; use toyos::process::Image; use toyos_abi::end::End; use toyos_abi::handle::Rights; -use toyos_abi::syscall::{self, SpawnArgs, SpawnMap, SyscallError, VmProt, MAX_SPAWN_MAPS}; +use toyos_abi::syscall::{self, SpawnArgs, SpawnBacking, SpawnMap, SyscallError, VmProt, MAX_SPAWN_MAPS}; use toyos_abi::RawHandle; const SELF: &str = "/system/bin/test_rs_abuse_spawn_maps"; @@ -114,7 +114,15 @@ fn main() { let size = (image.bytes().len() as u64).next_multiple_of(PAGE); let past = image::laid(&image, &format!("{SELF}\0{PAST}")); let mut maps = past.maps().to_vec(); - maps.push(SpawnMap { addr: PAST_AT, len: 3 * PAGE, object: honest[text].object, offset: size - PAGE, copied: PAGE, prot: VmProt::ReadWrite as u64 }); + maps.push(SpawnMap { + addr: PAST_AT, + len: 3 * PAGE, + object: honest[text].object, + offset: size - PAGE, + copied: PAGE, + prot: VmProt::ReadWrite as u64, + backing: SpawnBacking::Copy as u64, + }); let child = spawn(&maps, past.entry, past.image_base).unwrap_or_else(|e| panic!("{PAST}: spawn: {e:?}")); assert_eq!(syscall::process_wait(child), Ok(End::Exited(0)), "{PAST}"); println!("abuse_spawn_maps: {PAST}: zeros");