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 2edc41bfd0..043df0f1b6 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. diff --git a/kernel/pure/frames.rs b/kernel/pure/frames.rs index 937161c427..32eb6c5457 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; 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. //! @@ -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,14 @@ 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 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 an address space'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 +1362,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 +1462,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 an address space'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 +2109,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 a462a7ca04..ad271ce8a6 100644 --- a/kernel/pure/vm.rs +++ b/kernel/pure/vm.rs @@ -35,6 +35,21 @@ 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, 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); + if n > 0 { + read(from, 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 { @@ -390,6 +405,7 @@ impl Inventory { #[cfg(test)] mod tests { use super::*; + use std::format; use std::vec; use std::vec::Vec; @@ -432,6 +448,43 @@ mod tests { } } + /// 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; 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, 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; + 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"); + } + } + /// 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 17ea0a1971..099e3fcae1 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 6a14b7c68c..46135660c9 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/loader/mod.rs b/kernel/src/loader/mod.rs index 7fe14170cc..5582965f6f 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 8ae8c06a3a..b822f0b4dd 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; @@ -134,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)); @@ -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,15 +288,14 @@ 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. @@ -307,6 +306,13 @@ pub fn alloc_promised() -> Frame { 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`] or [`discard`]. +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 /// is held back: a process's tables are not promised. It goes back through /// [`free_held`]. @@ -445,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 16ba4c85db..3c5f07c0b1 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]); } @@ -427,6 +429,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 @@ -593,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 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. + /// 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; @@ -617,34 +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 => { - // 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); - } - true - } - }; + // maps now and no waiter is keyed on. + unsafe { pmm::discard(phys) }; } + freed += frames.len() as u64; }); self.usage.freed(freed); Ok(()) @@ -875,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 - // zeroed a pinned frame 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); @@ -929,35 +918,28 @@ 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. +/// 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().into_raw(); - if let Kind::Copy { source, offset, len } = &entry.kind { - copy_in(&**source, offset + (page - start), len.saturating_sub(page - start), phys); + let phys = pmm::alloc_promised_unzeroed(); + // SAFETY: a frame just drawn for 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) }; + match &entry.kind { + Kind::Private { .. } => frame.fill(0), + 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 } -/// 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; - } - // 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); -} - /// 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 2b5b1d577a..9fbf7e3352 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"; @@ -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,24 @@ 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, + 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"); + runs("after every refusal", &image::image_of(SELF)); println!("abuse_spawn_maps: PASS"); }