//! Which title memory changed since a host copy took it (decision 0007, //! "Copies of title memory" and "Change tracking"). Each host copy of title //! memory (`crate::shadow`) and each texture's staging (`crate::texture`) //! tracks its memory ([`Tracked`]): the 64 KiB pages it has not taken since //! they last changed. Taking pages makes their blocks read-only (guards); //! the first write to a guarded block faults into our vectored handler, //! which gives write access back and marks the block changed in every copy //! holding it. Writers that do not fault say so: our own and the kernel's //! writes into title memory ([`writing`], before they write), and the //! runtime mapping or unmapping memory ([`title_memory_remapped`]). So //! bringing a copy up to date costs what changed since, not how much memory //! it covers: no system call for memory nobody wrote. //! //! Only the runtime's GPU memory is tracked: its write-watched pools, its //! physical-page views and their placeholders. Other memory a copy takes //! (the C heap, where the kernel writes system call results into buffers a //! guard would make fail) is copied whole every time. //! //! Blocks that cannot be made read-only (placeholders, nothing mapped) //! read as zeros and count as taken: only a mapping changes them, which the //! runtime reports. //! //! Physical pages may be mapped at several addresses (the title fills //! pages through a pool view and maps them into its heaps), so a write at //! one is a change at all of them: the runtime says where else memory is //! mapped whenever it maps it ([`title_memory_aliased`]); taking a page //! guards every address showing it, and a write at any of them marks the //! trackers at all. (Asking the runtime at each take instead waited for //! its lock, which the title's page mappings hold: 4 s per world frame.) //! DirectStorage writing pages through views of the runtime's own reports //! the addresses showing them as written. //! //! shortcut: a system call writing into a guarded block fails; the known //! ones (file reads, DirectStorage) open their blocks first. Memory freed //! and allocated again while a tracker holds it is not seen. use std::collections::{BTreeMap, BTreeSet}; use std::sync::{Arc, Mutex}; /// The unit tracked and copied: 64 KiB, the console's page. Finer units /// made the checks of the world's big buffers cost more than the copies /// they saved. pub(crate) const PAGE: usize = 64 << 10; /// The pages a tracker has not taken since they changed, and what it saves /// of pages it armed. struct Changed(Mutex>, Mutex); /// Pages (page numbers) whose bytes a tracker keeps when they are first /// written after it took them, and those bytes: while shaders may be /// writing its copy of them, what it held before (`crate::shadow`'s /// copy-back of marked pages). #[derive(Default)] struct Saved { armed: BTreeSet, bytes: std::collections::HashMap>, } impl Changed { /// Page `page` was taken: what it saved of it is stale. fn taken(&self, page: usize) { self.1.lock().unwrap_or_else(|e| e.into_inner()).bytes.remove(&page); } /// Page `page` is about to change for the first time since it was /// taken: its bytes now, if armed for it. fn save(&self, page: usize) { let mut saved = self.1.lock().unwrap_or_else(|e| e.into_inner()); if !saved.armed.contains(&page) || saved.bytes.contains_key(&page) { return; } let mut bytes = vec![0u8; PAGE].into_boxed_slice(); if crate::host::read_into((page * PAGE) as *const u8, &mut bytes) { saved.bytes.insert(page, bytes); #[cfg(feature = "diagnostics")] crate::copystat::add(crate::copystat::C::SavedPages, 1); } } } /// Title memory a host copy or texture staging takes: bytes `[start, end)`. pub(crate) struct Tracked { start: usize, end: usize, changed: Arc, } struct Guards { /// Blocks (pages) we made read-only. protected: BTreeSet, /// The trackers holding each page. holders: BTreeMap>>, /// Pages `[first, last)` of writes the guards do not see that are under /// way ([`Writing`]): not guarded until they end, or the write fails. busy: Vec<(usize, usize)>, /// Pages showing the same physical pages elsewhere, both ways, as the /// runtime told when it mapped them: a write at one marks the others, /// with no question to the runtime (whose lock its page mappings hold /// for long). alias_of: BTreeMap>, /// Tracked pages written this frame and the frame before (diagnostics). #[cfg(feature = "diagnostics")] written: BTreeSet, #[cfg(feature = "diagnostics")] written_before: BTreeSet, } static GUARDS: Mutex = Mutex::new(Guards { protected: BTreeSet::new(), holders: BTreeMap::new(), busy: Vec::new(), alias_of: BTreeMap::new(), #[cfg(feature = "diagnostics")] written: BTreeSet::new(), #[cfg(feature = "diagnostics")] written_before: BTreeSet::new(), }); #[cfg(not(feature = "diagnostics"))] fn guards() -> std::sync::MutexGuard<'static, Guards> { GUARDS.lock().unwrap_or_else(|e| e.into_inner()) } /// The guard lock, timed while held by the line that took it /// (diagnostics: [`held_times`]). #[cfg(feature = "diagnostics")] #[track_caller] fn guards() -> Held { let line = std::panic::Location::caller().line(); let guard = GUARDS.lock().unwrap_or_else(|e| e.into_inner()); Held(Some(guard), std::time::Instant::now(), line) } #[cfg(feature = "diagnostics")] struct Held(Option>, std::time::Instant, u32); #[cfg(feature = "diagnostics")] impl std::ops::Deref for Held { type Target = Guards; fn deref(&self) -> &Guards { self.0.as_ref().expect("held until dropped") } } #[cfg(feature = "diagnostics")] impl std::ops::DerefMut for Held { fn deref_mut(&mut self) -> &mut Guards { self.0.as_mut().expect("held until dropped") } } /// Per line taking the guard lock: the line, microseconds held in all, the /// longest hold (no allocation: the fault handler takes the lock). #[cfg(feature = "diagnostics")] static HELD: [[std::sync::atomic::AtomicU64; 3]; 16] = [const { [const { std::sync::atomic::AtomicU64::new(0) }; 3] }; 16]; #[cfg(feature = "diagnostics")] impl Drop for Held { fn drop(&mut self) { use std::sync::atomic::Ordering::Relaxed; let us = self.1.elapsed().as_micros() as u64; drop(self.0.take()); let line = u64::from(self.2); if let Some(slot) = HELD .iter() .find(|s| s[0].load(Relaxed) == line || s[0].compare_exchange(0, line, Relaxed, Relaxed).is_ok()) { slot[1].fetch_add(us, Relaxed); slot[2].fetch_max(us, Relaxed); } } } /// Since the last call, per line of this file taking the guard lock: (line, /// milliseconds held, longest hold in milliseconds), most held first. #[cfg(feature = "diagnostics")] pub(crate) fn held_times() -> Vec<(u64, f64, f64)> { use std::sync::atomic::Ordering::Relaxed; let mut out: Vec<(u64, f64, f64)> = HELD .iter() .filter(|s| s[0].load(Relaxed) != 0) .map(|s| (s[0].load(Relaxed), s[1].swap(0, Relaxed) as f64 / 1e3, s[2].swap(0, Relaxed) as f64 / 1e3)) .collect(); out.sort_by(|a, b| b.1.total_cmp(&a.1)); out } /// (page, other page) for the pages (page numbers) showing the same memory /// as `pages` elsewhere. fn aliases(g: &Guards, pages: &[usize]) -> Vec<(usize, usize)> { pages .iter() .flat_map(|&p| g.alias_of.get(&p).into_iter().flatten().map(move |&a| (p, a))) .collect() } /// Guards the other mappings of taken pages. fn guard_aliases(g: &mut Guards, pairs: &[(usize, usize)]) { let others: Vec = pairs.iter().map(|&(_, a)| a).collect(); protect(g, &others); } /// Tracks `[start, start + len)` if it is the runtime's GPU memory (every /// page changed at first); `None` for other memory, and with `views` false /// for anything but its write-watched pools (physical-page views are /// written through other views showing the same pages: the title fills /// streamed textures and video planes through a pool view and maps the /// pages into its tiled heap, where a guard sees none of it). pub(crate) fn track(start: usize, len: usize, views: bool) -> Option { if len == 0 || !runtime_memory(start, views) { return None; } install(); let end = start + len; let pages = start / PAGE..end.div_ceil(PAGE); let changed = Arc::new(Changed(Mutex::new(pages.clone().collect()), Mutex::default())); let mut g = guards(); for page in pages { g.holders.entry(page).or_default().push(changed.clone()); } Some(Tracked { start, end, changed, }) } /// Whether memory at `at` is the runtime's GPU memory: a write-watched /// pool, or (with `views`) a view or a placeholder (anything but plain /// committed memory). fn runtime_memory(at: usize, views: bool) -> bool { use windows_sys::Win32::System::Memory::{ GetWriteWatch, MEM_COMMIT, MEM_PRIVATE, MEMORY_BASIC_INFORMATION, VirtualQuery, }; let page = at & !(4096 - 1); let (mut found, mut count, mut granularity) = (0usize, 1usize, 0u32); // SAFETY: room for one answer; no reset, so nothing changes. let watched = unsafe { GetWriteWatch(0, page as *const _, 4096, (&raw mut found).cast(), &mut count, &mut granularity) } == 0; if watched || !views { return watched; } // SAFETY: plain data filled in by the query. let mut info: MEMORY_BASIC_INFORMATION = unsafe { std::mem::zeroed() }; // SAFETY: a valid out pointer of the right size. let n = unsafe { VirtualQuery(page as *const _, &mut info, size_of_val(&info)) }; n != 0 && !(info.State == MEM_COMMIT && info.Type == MEM_PRIVATE) } impl Drop for Tracked { /// Pages no tracker holds any more get write access back, so that the /// guard set never names memory freed and allocated again. fn drop(&mut self) { let mut g = guards(); let mut free = Vec::new(); for page in self.start / PAGE..self.end.div_ceil(PAGE) { if let Some(holders) = g.holders.get_mut(&page) { holders.retain(|c| !Arc::ptr_eq(c, &self.changed)); if holders.is_empty() { g.holders.remove(&page); free.push(page); } } } for page in free { open(&mut g, page); } } } /// Gives write access back to page `page` if we guarded it. fn open(g: &mut Guards, page: usize) -> bool { use windows_sys::Win32::System::Memory::{PAGE_READWRITE, VirtualProtect}; if !g.protected.remove(&page) { return false; } let mut old = 0; // SAFETY: a block we made read-only (failing harmlessly if it is gone). unsafe { VirtualProtect((page * PAGE) as *const _, PAGE, PAGE_READWRITE, &mut old) }; #[cfg(feature = "diagnostics")] crate::copystat::add(crate::copystat::C::OpenPages, 1); true } impl Tracked { /// Takes the changed pages of `[from, to)`: guards them (writes from /// now on mark them again) and returns the byte ranges to copy, cut to /// `[from, to)`, in order. A page whose part in the tracked memory lies /// only partly in `[from, to)` is copied in part and stays changed. pub(crate) fn take(&self, from: usize, to: usize) -> Vec<(usize, usize)> { let (from, to) = (from.max(self.start), to.min(self.end)); if from >= to { return Vec::new(); } let pages: Vec = { #[cfg(feature = "diagnostics")] let waited = Timed(std::time::Instant::now(), crate::copystat::C::GuardWaitUs); let changed = self.changed.0.lock().unwrap_or_else(|e| e.into_inner()); #[cfg(feature = "diagnostics")] drop(waited); changed.range(from / PAGE..to.div_ceil(PAGE)).copied().collect() }; if pages.is_empty() { return Vec::new(); } let whole: Vec = pages .iter() .copied() .filter(|&p| (p * PAGE).max(self.start) >= from && ((p + 1) * PAGE).min(self.end) <= to) .collect(); // Every address showing the same pages is guarded too. A write in // between marks them changed again; the copy, made after, has it. #[cfg(feature = "diagnostics")] let waited = Timed(std::time::Instant::now(), crate::copystat::C::GuardWaitUs); let mut g = guards(); let mut changed = self.changed.0.lock().unwrap_or_else(|e| e.into_inner()); #[cfg(feature = "diagnostics")] drop(waited); #[cfg(feature = "diagnostics")] let asked = Timed(std::time::Instant::now(), crate::copystat::C::AliasUs); let others = aliases(&g, &whole); guard_aliases(&mut g, &others); #[cfg(feature = "diagnostics")] drop(asked); for page in protect(&mut g, &whole) { changed.remove(&page); self.changed.taken(page); } drop(changed); drop(g); let mut runs: Vec<(usize, usize)> = Vec::new(); for p in pages { let (a, b) = ((p * PAGE).max(from), ((p + 1) * PAGE).min(to)); match runs.last_mut() { Some((_, end)) if *end == a => *end = b, _ => runs.push((a, b)), } } runs } /// Whether page `page` (a page number) changed since it was taken. pub(crate) fn changed(&self, page: usize) -> bool { self.changed .0 .lock() .unwrap_or_else(|e| e.into_inner()) .contains(&page) } /// The caller made pages (page numbers) of its copy equal to title /// memory itself (what shaders wrote went back): taken and guarded. pub(crate) fn taken(&self, pages: &[usize]) { let mut g = guards(); let mut changed = self.changed.0.lock().unwrap_or_else(|e| e.into_inner()); let others = aliases(&g, pages); guard_aliases(&mut g, &others); for page in protect(&mut g, pages) { changed.remove(&page); self.changed.taken(page); } } /// Shaders may write the copy's bytes `[from, to)` until [`Tracked::disarm`]: /// pages there written next after they were taken have their bytes kept /// first ([`Tracked::saved`]). pub(crate) fn arm(&self, from: usize, to: usize) { let (from, to) = (from.max(self.start), to.min(self.end)); if from < to { self.changed.1.lock().unwrap_or_else(|e| e.into_inner()).armed.extend(from / PAGE..to.div_ceil(PAGE)); } } /// Page `page` (a page number) needs no saving any more: its copy-back /// is under way. pub(crate) fn unarm(&self, page: usize) { self.changed.1.lock().unwrap_or_else(|e| e.into_inner()).armed.remove(&page); } /// No shader writes the copy any more: nothing is armed or kept. pub(crate) fn disarm(&self) { let mut saved = self.changed.1.lock().unwrap_or_else(|e| e.into_inner()); saved.armed.clear(); saved.bytes.clear(); } /// The bytes page `page` (a page number) held when it was first written /// since it was taken, if it was armed then (and so changed). pub(crate) fn saved(&self, page: usize) -> Option> { self.changed.1.lock().unwrap_or_else(|e| e.into_inner()).bytes.remove(&page) } } /// Makes `pages` read-only where not already (in runs), and returns those /// now guarded or with nothing mapped (taken either way); a page that is /// mapped but cannot be guarded is left out (it stays changed). fn protect(g: &mut Guards, pages: &[usize]) -> Vec { #[cfg(feature = "diagnostics")] let counted = |calls: u64, pages: u64| { crate::copystat::add(crate::copystat::C::ProtectCalls, calls); crate::copystat::add(crate::copystat::C::ProtectPages, pages); }; use windows_sys::Win32::System::Memory::{ MEM_COMMIT, MEMORY_BASIC_INFORMATION, PAGE_READONLY, VirtualProtect, VirtualQuery, }; #[cfg(feature = "diagnostics")] let _timed = Timed(std::time::Instant::now(), crate::copystat::C::ProtectUs); let set = |from: usize, to: usize| { let mut old = 0; // SAFETY: title memory; only its protection changes. unsafe { VirtualProtect((from * PAGE) as *const _, (to - from) * PAGE, PAGE_READONLY, &mut old) != 0 } }; let unmapped = |page: usize| { // SAFETY: plain data filled in by the query. let mut info: MEMORY_BASIC_INFORMATION = unsafe { std::mem::zeroed() }; // SAFETY: a valid out pointer of the right size. let n = unsafe { VirtualQuery((page * PAGE) as *const _, &mut info, size_of_val(&info)) }; n == 0 || info.State != MEM_COMMIT }; let mut taken = Vec::with_capacity(pages.len()); let busy = |page: usize| g.busy.iter().any(|&(a, b)| (a..b).contains(&page)); let pages: Vec = pages.iter().copied().filter(|&p| !busy(p)).collect(); let mut i = 0; while i < pages.len() { if g.protected.contains(&pages[i]) { taken.push(pages[i]); i += 1; continue; } // A run of consecutive pages not guarded yet. let first = i; while i + 1 < pages.len() && pages[i + 1] == pages[i] + 1 && !g.protected.contains(&pages[i + 1]) { i += 1; } let run = &pages[first..=i]; i += 1; #[cfg(feature = "diagnostics")] counted(1, run.len() as u64); if set(run[0], run[run.len() - 1] + 1) { g.protected.extend(run); taken.extend(run); continue; } for &page in run { if set(page, page + 1) { g.protected.insert(page); taken.push(page); } else if unmapped(page) { taken.push(page); } } } taken } /// Pages `[first, last)` changed, and so the pages showing the same memory /// elsewhere: gives write access back where we guarded them and marks them /// in every tracker holding them. True if one of `[first, last)` was /// guarded. fn changed(g: &mut Guards, first: usize, last: usize, save: bool) -> bool { let guarded = changed_here(g, first, last, save); let others: Vec = g.alias_of.range(first..last).flat_map(|(_, o)| o.iter().copied()).collect(); #[cfg(feature = "diagnostics")] { let held = others.iter().filter(|p| g.holders.contains_key(p)).count(); crate::copystat::add(crate::copystat::C::AliasMarks, held as u64); } for page in others { changed_here(g, page, page + 1, save); } guarded } /// [`changed`] of `[first, last)` alone. With `save`, before the write /// lands: trackers armed for a page they had taken keep its bytes. fn changed_here(g: &mut Guards, first: usize, last: usize, save: bool) -> bool { let guarded: Vec = g.protected.range(first..last).copied().collect(); for &page in &guarded { open(g, page); } for (&page, holders) in g.holders.range(first..last) { for c in holders { let first_write = c.0.lock().unwrap_or_else(|e| e.into_inner()).insert(page); if first_write && save { c.save(page); } } #[cfg(feature = "diagnostics")] g.written.insert(page); } !guarded.is_empty() } /// At each presented frame (diagnostics): counts the tracked pages written /// in it, and those written the frame before too. #[cfg(feature = "diagnostics")] pub(crate) fn frame_done() { let mut g = guards(); let written = std::mem::take(&mut g.written); let again = written.intersection(&g.written_before).count(); crate::copystat::add(crate::copystat::C::WrittenPages, written.len() as u64); crate::copystat::add(crate::copystat::C::WrittenAgain, again as u64); g.written_before = written; } /// A write into title memory the guards do not see, under way: its pages /// stay open (a tracker taking them meanwhile would guard them, and the /// kernel's write would fail) until it is dropped, which marks them changed /// once more. #[must_use = "the write must happen while this lives"] pub struct Writing { first: usize, last: usize, } impl Drop for Writing { fn drop(&mut self) { let mut g = guards(); if let Some(i) = g.busy.iter().position(|&r| r == (self.first, self.last)) { g.busy.swap_remove(i); } changed(&mut g, self.first, self.last, false); } } /// Before our own write into title memory `[at, at + len)` through the /// kernel (which does not fault into our handler) or another write the /// guards do not see: opens its guarded blocks, marks them changed and /// keeps them open while the result lives. The runtime calls it (as /// `kernel_will_write`) around the title's kernel reads into its own /// memory and DirectStorage's writes to addresses. pub fn writing(at: usize, len: usize) -> Writing { // The length is the title's (`fread`'s size times count, saturated). let (first, last) = (at / PAGE, at.saturating_add(len).div_ceil(PAGE)); let mut g = guards(); g.busy.push((first, last)); changed(&mut g, first, last, true); Writing { first, last } } /// The runtime mapped or unmapped title memory `[at, at + len)`: what it /// shows is new, and read-write. pub fn title_memory_remapped(at: usize, len: usize) { let (first, last) = (at / PAGE, at.saturating_add(len).div_ceil(PAGE)); let mut g = guards(); // What it showed is gone already: nothing to keep. changed(&mut g, first, last, false); // What it showed is not what it shows now: its links go, both ways. let stale: Vec = g.alias_of.range(first..last).map(|(&p, _)| p).collect(); for page in stale { for other in g.alias_of.remove(&page).unwrap_or_default() { if let Some(links) = g.alias_of.get_mut(&other) { links.retain(|&p| p != page); if links.is_empty() { g.alias_of.remove(&other); } } } } } /// The runtime mapped the page at `at` where `others` show it already: the /// pages are linked both ways, and if we guard it there, it is guarded here /// too (a write through the new mapping must not go unseen by the trackers /// there). pub fn title_memory_aliased(at: usize, others: &[usize]) { let mut g = guards(); let page = at / PAGE; for &other in others { for (x, y) in [(other / PAGE, page), (page, other / PAGE)] { let links = g.alias_of.entry(x).or_default(); if !links.contains(&y) { links.push(y); } } } if others.iter().any(|&o| g.protected.contains(&(o / PAGE))) { protect(&mut g, &[page]); } } /// Registers the handler that takes writes to guarded blocks, once, ahead /// of the crash reporter's. fn install() { use windows_sys::Win32::Foundation::{EXCEPTION_ACCESS_VIOLATION, NTSTATUS}; use windows_sys::Win32::System::Diagnostics::Debug::{ AddVectoredExceptionHandler, EXCEPTION_CONTINUE_EXECUTION, EXCEPTION_CONTINUE_SEARCH, EXCEPTION_POINTERS, }; unsafe extern "system" fn on_write(info: *mut EXCEPTION_POINTERS) -> i32 { // SAFETY: the system passes the exception being dispatched. let r = unsafe { &*(*info).ExceptionRecord }; const WRITE: usize = 1; if r.ExceptionCode != EXCEPTION_ACCESS_VIOLATION as NTSTATUS || r.NumberParameters < 2 || r.ExceptionInformation[0] != WRITE { return EXCEPTION_CONTINUE_SEARCH; } let at = r.ExceptionInformation[1]; #[cfg(feature = "diagnostics")] let _timed = Timed(std::time::Instant::now(), crate::copystat::C::HandlerUs); // Held across both checks: another thread may have opened the block // since this one faulted, and a copy may have guarded it again since // (then it is ours again, not read-only for some other reason). let mut g = guards(); if changed(&mut g, at / PAGE, at / PAGE + 1, true) { #[cfg(feature = "diagnostics")] crate::copystat::add(crate::copystat::C::GuardFaults, 1); return EXCEPTION_CONTINUE_EXECUTION; } if writable(at) { EXCEPTION_CONTINUE_EXECUTION } else { EXCEPTION_CONTINUE_SEARCH } } static ONCE: std::sync::Once = std::sync::Once::new(); // SAFETY: the handler only touches our guard state and protections. ONCE.call_once(|| unsafe { AddVectoredExceptionHandler(1, Some(on_write)); }); } /// Whether `at` is committed read-write memory now. fn writable(at: usize) -> bool { use windows_sys::Win32::System::Memory::{ MEM_COMMIT, MEMORY_BASIC_INFORMATION, PAGE_READWRITE, VirtualQuery, }; // SAFETY: plain data filled in by the query. let mut info: MEMORY_BASIC_INFORMATION = unsafe { std::mem::zeroed() }; // SAFETY: a valid out pointer of the right size. let n = unsafe { VirtualQuery(at as *const _, &mut info, size_of_val(&info)) }; n != 0 && info.State == MEM_COMMIT && info.Protect == PAGE_READWRITE } /// Adds the time since it was made to a counter when dropped (diagnostics). #[cfg(feature = "diagnostics")] pub(crate) struct Timed(pub(crate) std::time::Instant, pub(crate) crate::copystat::C); #[cfg(feature = "diagnostics")] impl Drop for Timed { fn drop(&mut self) { let c = std::mem::replace(&mut self.1, crate::copystat::C::Submissions); crate::copystat::add(c, self.0.elapsed().as_micros() as u64); } } #[cfg(test)] pub(crate) mod tests { use super::*; use windows_sys::Win32::System::Memory::{ MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE, VirtualAlloc, VirtualFree, }; pub(crate) const MEM_WRITE_WATCH: u32 = 0x20_0000; /// A write-watched allocation of `pages` pages (as the runtime's pools), /// on a 64 KiB boundary (the allocation granularity). pub(crate) fn pool(pages: usize) -> usize { // SAFETY: a fresh allocation, freed by the test. let p = unsafe { VirtualAlloc( std::ptr::null(), pages * PAGE, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE, ) } as usize; assert!(p != 0 && p.is_multiple_of(PAGE)); p } pub(crate) fn free(p: usize) { // SAFETY: an allocation of ours. unsafe { VirtualFree(p as *mut _, 0, MEM_RELEASE) }; } /// Everything is changed at first; after taking, only what is written /// (by faults, or reported) is, and every tracker of a page sees it. #[test] fn writes_mark_every_tracker_of_their_page() { let p = pool(4); let a = track(p, 4 * PAGE, true).expect("pool memory is tracked"); let b = track(p + PAGE, 2 * PAGE, true).expect("tracked"); assert_eq!(a.take(p, p + 4 * PAGE), [(p, p + 4 * PAGE)]); assert_eq!(b.take(p, p + 4 * PAGE), [(p + PAGE, p + 3 * PAGE)]); assert!(a.take(p, p + 4 * PAGE).is_empty() && b.take(p, p + 4 * PAGE).is_empty()); // SAFETY: committed pages of ours, now read-only: the handler opens // the block; the second write needs no fault. unsafe { ((p + PAGE + 8) as *mut u8).write_volatile(1); ((p + PAGE + 9) as *mut u8).write_volatile(2); } assert_eq!(a.take(p, p + 4 * PAGE), [(p + PAGE, p + 2 * PAGE)]); // `a` guarded it again; `b` still has it changed. assert!(b.changed(p / PAGE + 1)); assert_eq!(b.take(p, p + 4 * PAGE), [(p + PAGE, p + 2 * PAGE)]); // SAFETY: as above: a fault again, for both. unsafe { ((p + PAGE + 10) as *mut u8).write_volatile(3) }; assert!(a.changed(p / PAGE + 1) && b.changed(p / PAGE + 1)); assert_eq!(a.take(p, p + 4 * PAGE), [(p + PAGE, p + 2 * PAGE)]); drop(writing(p + 3 * PAGE + 5, 1)); assert_eq!(a.take(p, p + 4 * PAGE), [(p + 3 * PAGE, p + 4 * PAGE)]); title_memory_remapped(p, PAGE); assert!(a.changed(p / PAGE) && !a.changed(p / PAGE + 1)); drop((a, b)); free(p); } /// A page only partly in the range asked is copied in part and stays /// changed; edge pages count whole when the tracked part is covered. #[test] fn partial_pages_stay_changed() { let p = pool(3); let t = track(p + 100, 2 * PAGE, true).expect("tracked"); assert_eq!(t.take(p + 100, p + PAGE + 10), [(p + 100, p + PAGE + 10)]); assert!(!t.changed(p / PAGE) && t.changed(p / PAGE + 1)); assert_eq!(t.take(p, p + 3 * PAGE), [(p + PAGE, p + 2 * PAGE + 100)]); assert!(t.take(p, p + 3 * PAGE).is_empty()); drop(t); free(p); } /// Plain committed memory (the C heap) is not tracked; reserved memory /// is, reads as taken, and changes when the runtime maps it. #[test] fn heap_memory_is_not_tracked_and_placeholders_wait_for_mappings() { let plain = vec![0u8; 2 * PAGE]; assert!(track(plain.as_ptr() as usize, plain.len(), true).is_none()); // SAFETY: a reservation of ours, freed below. let r = unsafe { VirtualAlloc(std::ptr::null(), 2 * PAGE, MEM_RESERVE, PAGE_READWRITE) } as usize; assert_ne!(r, 0); let t = track(r, 2 * PAGE, true).expect("reserved memory is tracked"); assert_eq!(t.take(r, r + 2 * PAGE).len(), 1, "copied once (as zeros)"); assert!(t.take(r, r + 2 * PAGE).is_empty()); title_memory_remapped(r, PAGE); assert_eq!(t.take(r, r + 2 * PAGE), [(r, r + PAGE)]); drop(t); // SAFETY: our reservation. unsafe { VirtualFree(r as *mut _, 0, MEM_RELEASE) }; } /// A kernel read into a guarded view block fails; opened first, it /// lands and the block counts as changed. #[test] fn kernel_reads_into_guarded_blocks_after_opening() { use std::io::{Read, Seek, Write}; use windows_sys::Win32::System::Memory::{ CreateFileMappingW, FILE_MAP_WRITE, MapViewOfFile, UnmapViewOfFile, }; // SAFETY: a pagefile section of two blocks, mapped read-write. let view = unsafe { let section = CreateFileMappingW( -1isize as _, std::ptr::null(), PAGE_READWRITE, 0, (2 * PAGE) as u32, std::ptr::null(), ); assert!(!section.is_null()); MapViewOfFile(section, FILE_MAP_WRITE, 0, 0, 2 * PAGE).Value as usize }; assert_ne!(view, 0); assert!(track(view, 2 * PAGE, false).is_none(), "views only where asked"); let t = track(view, 2 * PAGE, true).expect("views are tracked"); assert_eq!(t.take(view, view + 2 * PAGE), [(view, view + 2 * PAGE)]); // now read-only let mut file = tempfile::tempfile().unwrap(); file.write_all(b"from the disk").unwrap(); // `ReadFile` inside: the kernel writes the view. let read = |file: &mut std::fs::File| { file.rewind().unwrap(); // SAFETY: 13 bytes of our view, nothing else refers to them. let into = unsafe { std::slice::from_raw_parts_mut((view + 100) as *mut u8, 13) }; file.read(into).is_ok_and(|n| n == 13) }; assert!(!read(&mut file), "the kernel does not fault into our handler"); let write = writing(view + 100, 13); // A tracker taking the page meanwhile leaves it open. assert_eq!(t.take(view, view + 2 * PAGE), [(view, view + PAGE)]); assert!(read(&mut file)); drop(write); // SAFETY: the bytes just read. assert_eq!(unsafe { std::slice::from_raw_parts((view + 100) as *const u8, 13) }, b"from the disk"); assert_eq!(t.take(view, view + 2 * PAGE), [(view, view + PAGE)]); drop(t); // SAFETY: our view. unsafe { UnmapViewOfFile(windows_sys::Win32::System::Memory::MEMORY_MAPPED_VIEW_ADDRESS { Value: view as *mut _ }) }; } /// `pages` pages of a fresh pagefile section mapped twice (as the /// title's pool view and tiled heap show the same physical pages), /// reported as the runtime does when it maps them. pub(crate) fn twin_views(pages: usize) -> (usize, usize) { use windows_sys::Win32::System::Memory::{CreateFileMappingW, FILE_MAP_WRITE, MapViewOfFile}; let len = pages * PAGE; // SAFETY: a pagefile section mapped read-write twice; the test // keeps both views. let (a, b) = unsafe { let section = CreateFileMappingW( -1isize as _, std::ptr::null(), PAGE_READWRITE, 0, len as u32, std::ptr::null(), ); assert!(!section.is_null()); ( MapViewOfFile(section, FILE_MAP_WRITE, 0, 0, len).Value as usize, MapViewOfFile(section, FILE_MAP_WRITE, 0, 0, len).Value as usize, ) }; assert!(a != 0 && b != 0); for k in (0..len).step_by(PAGE) { title_memory_aliased(b + k, &[a + k]); } (a, b) } /// A write through another mapping of the same pages marks the tracker /// (the guard there faults, the runtime says where else it is mapped); /// so does a write the runtime reports at the other mapping. #[test] fn writes_through_another_mapping_mark_the_tracker() { let (a, b) = twin_views(2); let t = track(a, 2 * PAGE, true).expect("views are tracked"); assert_eq!(t.take(a, a + 2 * PAGE), [(a, a + 2 * PAGE)]); // SAFETY: the second view of the pages, guarded by the take. unsafe { ((b + PAGE + 4) as *mut u32).write_volatile(7) }; assert_eq!(t.take(a, a + 2 * PAGE), [(a + PAGE, a + 2 * PAGE)]); // SAFETY: the tracked view, which shows the write. assert_eq!(unsafe { ((a + PAGE + 4) as *const u32).read_volatile() }, 7); drop(writing(b, 8)); assert_eq!(t.take(a, a + 2 * PAGE), [(a, a + PAGE)]); assert!(t.take(a, a + 2 * PAGE).is_empty()); // A third mapping made later starts guarded, so its writes count. let (c, _) = twin_views(1); title_memory_aliased(c, &[a]); // SAFETY: a view of ours (another section: only the tracking // takes it for `a`'s pages here). unsafe { (c as *mut u32).write_volatile(1) }; assert_eq!(t.take(a, a + 2 * PAGE), [(a, a + PAGE)]); } /// 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; drop(writing(top, PAGE)); title_memory_remapped(top, usize::MAX); } }