Title views kept in the books alone until the title's CPU touches them (VICTORY_SOFT_VIEWS, off by default)

VICTORY_SOFT_VIEWS=1 (pool: the title's XMemMapPhysicalPages views alone;
copies: those of CopyPageMappingsBatchX alone): a view is noted in the
runtime's books and told to the GPU side as mapped, its addresses are one
placeholder of their own, and the host maps nothing. Each view of the host's
was a MapViewOfFile3 (45 us) and later an UnmapViewOfFile2 (93 us), both
behind a lock of the kernel's that the guards' protection changes take too:
445,503 maps (20.1 s) and 349,175 unmaps (32.6 s) in 143 s of run stream2,
359,969 mapping calls 23.8 s, 3,294 page-mapping copies 33.7 s, one of them
467 ms of a 608 ms frame (timeline tl.120).

Who gets the bytes of such a view, and how:
- DirectStorage writes pages by number through the section's own view, and
  page-mapping copies read the pool from the books: as before.
- The GPU side's reads of title memory (host::read, read_sparse) take them
  from the pages through the section's view (xmem::soft_read); a texture
  with such views is read through a copy, not in place; texture::committed
  takes such views for committed.
- The guards keep such a view in their books (watch::protect_here,
  protect_alias): no VirtualProtect, no question to the host. A write the
  runtime tells of, or one through another view of the page, opens it as
  before.
- Any touch of the memory itself (the title's CPU, a direct read of ours)
  faults; the GPU side's handler has the runtime map the view and the access
  is made again; the guards then open it and every other view of its page
  (title_memory_made_real). Our writes through the kernel, announced kernel
  writes, and the title's XMemVirtualQuery, XMemVirtualAlloc, VirtualFree
  and (with the switch) VirtualQuery and VirtualProtect ask first.

Why VICTORY_LAZY_VIEWS=1 ran at 2 fps (run xw-lazy, 2.4 to 3.4 GB of copies
a frame): a view guarded together with a lazy view of the same page was
never clean, because the host refuses to protect a placeholder
(protect_alias), so it was copied again at every asking; and every guarded
range made its lazy views real. Both are what the books replace here. Also
for both switches: a view is known as lazy before the GPU side is told of
it (a refresh in between noted the block absent), and its pages are
committed when it is noted (the GPU side writes them through its own view
of the section).

Counted: the streaming log's M line gets twelve more fields with either
switch (noted, let go untouched, made real; noted by mapping calls, by
copies; made real by a fault of the title's code, of other code, asked for
by the GPU side, by a call of the title's; reads served from the pages,
their KiB; such views now), and with VICTORY_SOFT_VIEWS the log has a line
of the same every 10 s, with the last faulting address and code.

Tests: xmem and host views_in_the_books_alone_are_read_from_their_pages, watch
a_view_in_the_books_alone_is_guarded_there. Not run in the game yet.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
readmet3xt-labandClaude Opus 5.5 committed 2026-11-27 00:32:01 +05:30
1 parent 4732b4676b
commit fb838e8251
5 files changed
+954 -66

No files matched your search

+98 -3
View File
@@ -1085,6 +1085,59 @@ mod tests {
}
}
/// Views the runtime keeps in its books alone (`VICTORY_SOFT_VIEWS`) are
/// placeholders to the host: their bytes are read from their pages,
/// beside memory read as before and memory that is not there (zeros),
/// and they count as committed.
#[test]
fn views_in_the_books_alone_are_read_from_their_pages() {
use crate::watch::test_books::{IN_BOOKS, books};
use windows_sys::Win32::System::Memory::*;
const K: usize = 64 << 10;
books();
let (pages, more) = (vec![0x22u8; K], vec![0x33u8; K]);
// SAFETY: a reservation of ours, of which the first block is
// committed and written; the second and the fourth are views in the
// books, the third is not there.
unsafe {
let at = VirtualAlloc(std::ptr::null(), 4 * K, MEM_RESERVE, PAGE_NOACCESS).cast::<u8>();
assert!(!at.is_null());
assert!(!VirtualAlloc(at.cast(), K, MEM_COMMIT, PAGE_READWRITE).is_null());
at.write_bytes(0x11, K);
let views = [(at as usize + K, K, pages.as_ptr() as usize), (at as usize + 3 * K, K, more.as_ptr() as usize)];
IN_BOOKS.lock().unwrap().extend(views);
let expected = |from: usize| match from / K {
0 => 0x11,
1 => 0x22,
3 => 0x33,
_ => 0,
};
// All there, from inside a page into the view in the books.
let mut out = vec![0xeeu8; 2 * K - 0x300];
assert!(super::read_into(at.add(0x300), &mut out));
assert!(out.iter().enumerate().all(|(i, &b)| b == expected(i + 0x300)));
// Over the block that is not there: no whole read; the copy has
// zeros there and the bytes around it, the views' from the pages.
let mut out = vec![0xeeu8; 4 * K];
assert!(!super::read_into(at, &mut out));
let mut out = vec![0xeeu8; 4 * K - 54];
super::copy_mapped(out.as_mut_ptr(), at.add(27), 4 * K - 54, false);
for (i, &b) in out.iter().enumerate() {
assert_eq!(b, expected(i + 27), "byte {:#x}", i + 27);
}
let mut out = vec![0xeeu8; 4 * K];
assert_eq!(super::read_there(at, &mut out), 3 * K);
assert!(out.iter().enumerate().all(|(i, &b)| b == expected(i)));
// Committed, as far as the title goes; the hole is not.
assert!(crate::texture::committed(at, 2 * K));
assert!(crate::texture::committed(at.add(K + 5), K - 5));
assert!(!crate::texture::committed(at, 3 * K));
assert!(crate::texture::committed(at.add(3 * K), K));
IN_BOOKS.lock().unwrap().retain(|view| !views.contains(view));
assert_ne!(VirtualFree(at.cast(), 0, MEM_RELEASE), 0);
}
}
/// Zeros for a title destination that went meanwhile are no fault.
#[test]
fn zeros_where_nothing_is_are_lost() {
@@ -8463,11 +8516,26 @@ fn read_sparse(src: *const u8, dst: *mut u8, len: usize) {
let at = src.wrapping_add(offset);
let to = dst.wrapping_add(offset);
let remaining = len - offset;
// A view in the runtime's books alone (`VICTORY_SOFT_VIEWS`) is a
// placeholder to the host, which would have it read as zeros: its
// bytes come from the pages, and the host is asked only about what
// lies before the next such view.
let mut plain = remaining;
if let Some(soft) = crate::watch::soft() {
let (from_pages, n) = (soft.read)(at as usize, to, remaining);
if from_pages && n != 0 {
offset += n;
continue;
}
if n != 0 {
plain = n;
}
}
// SAFETY: the query inspects an address and fills a local structure.
let mut info: MEMORY_BASIC_INFORMATION = unsafe { std::mem::zeroed() };
let size = unsafe { VirtualQuery(at.cast(), &mut info, size_of_val(&info)) };
let n = (info.BaseAddress as usize).saturating_add(info.RegionSize)
.saturating_sub(at as usize).min(remaining);
.saturating_sub(at as usize).min(plain);
if size != size_of_val(&info) || n == 0 {
read_pieces(at, to, remaining);
break;
@@ -8569,11 +8637,38 @@ pub(crate) fn read_there(src: *const u8, dst: &mut [u8]) -> usize {
/// Copies `len` bytes from `src` to `dst` unless a source page is not
/// readable.
fn read(src: *const u8, dst: *mut u8, len: usize) -> bool {
use windows_sys::Win32::System::Diagnostics::Debug::ReadProcessMemory;
use windows_sys::Win32::System::Threading::GetCurrentProcess;
// Views the runtime keeps in its books alone (`VICTORY_SOFT_VIEWS`) are
// read from their pages, and stay as they are: no view of the host's is
// made for our reading. What lies between them is read as before.
if let Some(soft) = crate::watch::soft() {
let mut done = 0;
while done < len {
let (at, to) = (src.wrapping_add(done), dst.wrapping_add(done));
let (mut from_pages, mut n) = (soft.read)(at as usize, to, len - done);
if !from_pages && n != 0 && !read_plain(at, to, n) {
// (Mapped so between the question and the read: once more.)
(from_pages, n) = (soft.read)(at as usize, to, len - done);
if !from_pages {
return false;
}
}
if n == 0 {
return false;
}
done += n;
}
return true;
}
// (The kernel's read does not fault into the handler that makes lazily
// mapped title memory real.)
crate::watch::materialize(src as usize, len);
read_plain(src, dst, len)
}
/// [`read`] of memory the host has mapped, or has not.
fn read_plain(src: *const u8, dst: *mut u8, len: usize) -> bool {
use windows_sys::Win32::System::Diagnostics::Debug::ReadProcessMemory;
use windows_sys::Win32::System::Threading::GetCurrentProcess;
if crate::safecopy::on() {
// SAFETY: the destination is mapped writable memory of `len` bytes;
// a source page that is not there fails the copy.
+3 -3
View File
@@ -119,7 +119,7 @@ pub use inplace::{title_memory_freeing, title_memory_section, title_memory_secti
pub use shadow::title_memory_write_combined;
#[cfg(windows)]
pub use watch::{
kernel_writing, title_memory_allocated, title_memory_freed, title_memory_reading, title_memory_remapped,
title_memory_remapping, title_memory_splitting, title_memory_unmapping, title_memory_written, use_lazy_views,
writing as kernel_will_write,
SoftViews, kernel_writing, soft_view_guards, title_memory_allocated, title_memory_freed, title_memory_made_real,
title_memory_reading, title_memory_remapped, title_memory_remapping, title_memory_splitting, title_memory_unmapping,
title_memory_written, use_lazy_views, use_soft_views, writing as kernel_will_write,
};
+27 -1
View File
@@ -1436,7 +1436,11 @@ impl Staging {
}
return size;
}
if through_copy() {
// (A texture with pages in views the runtime keeps in its books
// alone, `VICTORY_SOFT_VIEWS`, has no memory to read in place: its
// bytes come from the pages, through the copy. `_mapped` is held: no
// view becomes one of those before the read in place below is done.)
if through_copy() || !crate::watch::soft_runs(start, size).is_empty() {
return with_title(self.memory, size, |tiled| {
let hash = digest(tiled);
#[cfg(feature = "diagnostics")]
@@ -1790,9 +1794,31 @@ fn by_views() -> bool {
/// Whether `[at, at + len)` is committed memory (streamed textures may be
/// unmapped until their pages arrive).
pub(crate) fn committed(at: *const u8, len: usize) -> bool {
if crate::watch::soft().is_some() {
// Views the runtime keeps in its books alone (`VICTORY_SOFT_VIEWS`)
// are committed as far as the title goes, and stay as they are;
// Windows is asked about what lies between them.
let runs = crate::watch::soft_runs(at as usize, len);
if runs.is_empty() {
return committed_there(at, len);
}
let (mut from, end) = (at as usize, at as usize + len);
for (run, bytes) in runs {
if run > from && !committed_there(from as *const u8, run - from) {
return false;
}
from = run + bytes;
}
return from >= end || committed_there(from as *const u8, end - from);
}
// Memory the runtime mapped lazily is committed as far as the title
// goes: made real before Windows is asked.
crate::watch::materialize(at as usize, len);
committed_there(at, len)
}
/// [`committed`], of memory the host has mapped or has not.
fn committed_there(at: *const u8, len: usize) -> bool {
if by_query() {
return committed_by_query(at, len);
}
+267 -6
View File
@@ -673,8 +673,9 @@ pub(crate) fn asked(start: usize, end: usize) -> Option<(u64, Vec<(usize, u64)>)
/// with `guarded`, view memory, which is then guarded).
pub(crate) fn writes(start: usize, end: usize, guarded: bool) -> Option<(u64, Vec<(usize, u64)>)> {
// Guarding asks Windows about the pages and protects them: a lazy view
// is none to it yet.
if guarded {
// is none to it yet. (Not so a view in the books alone, which is
// guarded there: [`use_soft_views`].)
if guarded && soft().is_none() {
materialize(start, end.saturating_sub(start));
}
let (first, last) = (start / PAGE, end.div_ceil(PAGE));
@@ -2200,7 +2201,24 @@ fn protect_alias(g: &mut Guards, block: usize) -> bool {
if g.protected.contains(block) {
return true;
}
if g.left_alone(block) || g.rests(block, rest()) {
if g.left_alone(block) {
return false;
}
// A view in the runtime's books alone ([`use_soft_views`]): nothing
// writes through it before a fault makes it a mapping of the host's,
// which opens it and every other view of its page
// ([`title_memory_made_real`]). Guarded in the books, with no call to
// the host, which has a placeholder there and would refuse: then the
// view guarded with it was never clean and copied again at every asking
// (run xw-lazy, `VICTORY_LAZY_VIEWS=1`: 2.4 to 3.4 GB of copies a frame,
// 2 fps).
if soft_block(block) {
PROTECTED.set(block);
g.protected.insert(block);
SOFT_GUARDS[1].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
return true;
}
if g.rests(block, rest()) {
return false;
}
// Ahead of the protection.
@@ -2266,12 +2284,24 @@ fn protect_here(g: &mut Guards, from: usize, to: usize) {
};
// Ahead of the protection (`PROTECTED`).
(from..to).for_each(|block| PROTECTED.set(block));
if set(from, to) {
// Views in the runtime's books alone ([`use_soft_views`]) are guarded
// there: the host has a placeholder at their addresses, would refuse,
// and the block would count as not there. Nothing writes through one
// before a fault makes it real, which opens it
// ([`title_memory_made_real`]).
let soft = soft_runs(from * BLOCK, (to - from) * BLOCK);
let in_books = |block: usize| soft.iter().any(|&(at, n)| at <= block * BLOCK && (block + 1) * BLOCK <= at + n);
if soft.is_empty() && set(from, to) {
note(g, from, to);
return;
}
let mut uncommitted_until = from;
for block in from..to {
if in_books(block) {
note(g, block, block + 1);
SOFT_GUARDS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
continue;
}
// (Not one question a block of a region that is not there.)
if block >= uncommitted_until && set(block, block + 1) {
note(g, block, block + 1);
@@ -2931,7 +2961,90 @@ pub fn use_lazy_views(materialize: fn(usize, usize) -> bool) {
/// questions to Windows about the pages. (A fault in title memory, the
/// title's or ours, comes to [`install`]'s handler, which asks first.)
pub(crate) fn materialize(at: usize, len: usize) -> bool {
LAZY_VIEWS.get().is_some_and(|f| f(at, len.max(1)))
match soft() {
Some(soft) => (soft.materialize)(at, len.max(1)),
None => LAZY_VIEWS.get().is_some_and(|f| f(at, len.max(1))),
}
}
/// What the runtime gives for the views it keeps in its books alone
/// (`xmem`, `VICTORY_SOFT_VIEWS`): title memory it tells of as mapped
/// ([`title_memory_remapped`], `title_memory_view`) where the host has a
/// placeholder and no view, until the title's CPU touches it.
///
/// With these the GPU side leaves such a view as it is where it can: it
/// reads its bytes from the pages ([`SoftViews::read`], `crate::host`'s
/// reads; a texture all or partly in such views is read through a copy),
/// takes it for committed (`crate::texture::committed`) and guards it in
/// the books ([`protect_here`], [`protect_alias`]: no protection change, no
/// question to the host). Whatever else touches one makes it a mapping of
/// the host's first: a fault, the title's or ours (the handler below), and
/// what that handler does not see ([`materialize`]: our writes through the
/// kernel, the kernel's writes the runtime announces).
pub struct SoftViews {
/// `(address, bytes)`: makes every such view that overlaps the range a
/// mapping of the host's; whether there was one and all could be made.
pub materialize: fn(usize, usize) -> bool,
/// The same for an access that faulted: `(address, the code's address)`.
pub fault: fn(usize, usize) -> bool,
/// `(address, to, bytes)`: if such a view holds `address`, copies its
/// bytes from there on to `to`, to the view's end at most, and says
/// `(true, bytes copied)`; else `(false, n)`: the next `n` bytes (up to
/// `bytes`) are in no such view, and are read as other memory is.
pub read: fn(usize, *mut u8, usize) -> (bool, usize),
/// `(address, bytes)`: the parts of the range such views show, in
/// order, those that touch as one: `(address, bytes)`.
pub runs: fn(usize, usize) -> Vec<(usize, usize)>,
}
static SOFT_VIEWS: std::sync::OnceLock<SoftViews> = std::sync::OnceLock::new();
/// The runtime keeps views in its books alone ([`SoftViews`]).
pub fn use_soft_views(hooks: SoftViews) {
let _ = SOFT_VIEWS.set(hooks);
}
pub(crate) fn soft() -> Option<&'static SoftViews> {
SOFT_VIEWS.get()
}
/// The parts of `[at, at + len)` that views in the runtime's books alone
/// show ([`SoftViews::runs`]); none without such views.
pub(crate) fn soft_runs(at: usize, len: usize) -> Vec<(usize, usize)> {
match soft() {
Some(soft) if len != 0 => (soft.runs)(at, len),
_ => Vec::new(),
}
}
/// Whether a view in the runtime's books alone shows all of `block`.
fn soft_block(block: usize) -> bool {
soft_runs(block * BLOCK, BLOCK) == [(block * BLOCK, BLOCK)]
}
/// Since the start: blocks of views in the runtime's books alone guarded
/// there ([`protect_here`]), and such blocks guarded there as another view
/// of a page that is guarded ([`protect_alias`]).
static SOFT_GUARDS: [std::sync::atomic::AtomicU64; 2] = [const { std::sync::atomic::AtomicU64::new(0) }; 2];
/// [`SOFT_GUARDS`], for the runtime's line about its views.
pub fn soft_view_guards() -> [u64; 2] {
[0, 1].map(|i| SOFT_GUARDS[i].load(std::sync::atomic::Ordering::Relaxed))
}
/// The runtime made a view it had in its books alone a mapping of the
/// host's, read-write, at `[at, at + len)` ([`SoftViews`]): from now on the
/// title writes through it without a fault. What was guarded of it in the
/// books is opened, and with it every other view of its pages
/// ([`open`]): all of them count as written at the next asking. Called when
/// the view is there (as [`title_memory_remapped`] is), with the runtime's
/// lock on its views held.
pub fn title_memory_made_real(at: usize, len: usize) {
if len == 0 {
return;
}
let mut g = guards();
open(&mut g, at, at.saturating_add(len));
}
/// Registers the handler that takes writes to guarded blocks, once, ahead
@@ -2962,7 +3075,12 @@ pub(crate) fn install() {
// made again (any access, the title's or ours).
if r.ExceptionCode == EXCEPTION_ACCESS_VIOLATION as NTSTATUS
&& r.NumberParameters >= 2
&& materialize(r.ExceptionInformation[1], 1)
&& match soft() {
// (Counted by whose code it was: the title's own touch of a
// view is what the books cannot spare it.)
Some(soft) => (soft.fault)(r.ExceptionInformation[1], r.ExceptionAddress as usize),
None => materialize(r.ExceptionInformation[1], 1),
}
{
return EXCEPTION_CONTINUE_EXECUTION;
}
@@ -3079,6 +3197,48 @@ fn writable(at: usize) -> bool {
n != 0 && info.State == MEM_COMMIT && info.Protect == PAGE_READWRITE
}
/// A runtime that keeps views in its books alone, for tests
/// ([`use_soft_views`]; one set of hooks for the whole test process).
#[cfg(test)]
pub(crate) mod test_books {
/// What that runtime keeps in its books alone: address, bytes, and
/// where their pages are (0: nowhere, never read); apart from each other.
pub(crate) static IN_BOOKS: std::sync::Mutex<Vec<(usize, usize, usize)>> = std::sync::Mutex::new(Vec::new());
/// Its hooks: the views are [`IN_BOOKS`], read from their pages; it
/// makes none real itself.
pub(crate) fn books() {
super::use_soft_views(super::SoftViews {
materialize: |_, _| false,
fault: |_, _| false,
read: |at, to, len| {
let views = IN_BOOKS.lock().unwrap_or_else(|e| e.into_inner());
let readable = |view: &&(usize, usize, usize)| view.2 != 0 && (view.0..view.0 + view.1).contains(&at);
if let Some(&(base, bytes, pages)) = views.iter().find(readable) {
let n = (base + bytes - at).min(len);
// SAFETY: the test's pages hold the view's bytes, and
// its `to` as many as it asked for.
unsafe { std::ptr::copy_nonoverlapping((pages + (at - base)) as *const u8, to, n) };
return (true, n);
}
let next = views.iter().filter(|view| view.2 != 0).map(|view| view.0).filter(|&base| base > at).min();
(false, next.map_or(len, |next| (next - at).min(len)))
},
runs: |at, len| {
let views = IN_BOOKS.lock().unwrap_or_else(|e| e.into_inner());
let mut runs: Vec<(usize, usize)> = views
.iter()
.map(|&(base, bytes, _)| (base.max(at), (base + bytes).min(at + len)))
.filter(|&(from, to)| from < to)
.map(|(from, to)| (from, to - from))
.collect();
runs.sort_unstable();
runs
},
});
}
}
#[cfg(test)]
mod parts_tests {
use super::*;
@@ -3872,6 +4032,107 @@ mod tests {
drop_views(section, &[guarded, other], SIZE);
}
use super::test_books::{IN_BOOKS, books};
/// A view the runtime keeps in its books alone (`VICTORY_SOFT_VIEWS`): a
/// placeholder to the host. It is guarded in the books, on its own and
/// as another view of a guarded page, so that both it and that view come
/// clean; a write told of at it, or through the other view, is reported
/// at both; and made a view of the host's, it and the other view count
/// as written, and it is guarded as any view from then on.
#[test]
fn a_view_in_the_books_alone_is_guarded_there() {
use windows_sys::Win32::System::Memory::{
CreateFileMappingW, FILE_MAP_WRITE, MEM_REPLACE_PLACEHOLDER, MEM_RESERVE_PLACEHOLDER, MEMORY_MAPPED_VIEW_ADDRESS,
MapViewOfFile, MapViewOfFile3, PAGE_NOACCESS, UnmapViewOfFile, VirtualAlloc2,
};
use windows_sys::Win32::System::Threading::GetCurrentProcess;
const SIZE: usize = 2 * BLOCK;
books();
// SAFETY: a fresh placeholder of two blocks, and a pagefile section
// of two with one view of the host's.
let (soft, section, real) = unsafe {
let soft = VirtualAlloc2(
GetCurrentProcess(),
std::ptr::null(),
SIZE,
MEM_RESERVE | MEM_RESERVE_PLACEHOLDER,
PAGE_NOACCESS,
std::ptr::null_mut(),
0,
) as usize;
assert_ne!(soft, 0);
let section = CreateFileMappingW(-1isize as _, std::ptr::null(), PAGE_READWRITE, 0, SIZE as u32, std::ptr::null());
assert!(!section.is_null());
let real = MapViewOfFile(section, FILE_MAP_WRITE, 0, 0, SIZE).Value as usize;
assert_ne!(real, 0);
(soft, section, real)
};
// As the runtime tells of them: the one in the books first known as
// such, then told of as mapped.
IN_BOOKS.lock().unwrap().push((soft, SIZE, 0));
title_memory_remapped(soft, SIZE);
crate::inplace::title_memory_view(soft, SIZE, section as usize, 0);
title_memory_remapped(real, SIZE);
crate::inplace::title_memory_view(real, SIZE, section as usize, 0);
let blocks = |at: usize| vec![at / BLOCK, at / BLOCK + 1];
let block = |at: usize, k: usize| vec![at / BLOCK + k];
let none = Some(Vec::new());
// Guarded in the books: told once, then clean, with the host never
// asked to protect the placeholder (which it refuses).
assert_eq!(guard(soft, soft + SIZE), Some(blocks(soft)));
assert_eq!(guard(soft, soft + SIZE), none);
assert!(guards().absent.range(soft / BLOCK..soft / BLOCK + 2).next().is_none(), "there, as far as the title goes");
// The view of the host's comes clean beside it.
assert_eq!(guard(real, real + SIZE), Some(blocks(real)));
assert_eq!(guard(real, real + SIZE), none);
assert_eq!(guard(soft, soft + SIZE), none);
assert!(!writable(real));
// A write through the view of the host's: reported at both.
// SAFETY: the view, read-write once the handler has opened it.
unsafe { ((real + BLOCK + 8) as *mut u8).write_volatile(1) };
assert_eq!(guard(soft, soft + SIZE), Some(block(soft, 1)));
assert_eq!(guard(real, real + SIZE), Some(block(real, 1)));
assert_eq!(guard(soft, soft + SIZE), none);
assert_eq!(guard(real, real + SIZE), none);
// A write the runtime tells of at the view in its books (bytes
// DirectStorage put into the pages): reported at both.
writing(soft, BLOCK);
assert_eq!(guard(real, real + SIZE), Some(block(real, 0)));
assert_eq!(guard(soft, soft + SIZE), Some(block(soft, 0)));
assert_eq!(guard(real, real + SIZE), none);
assert_eq!(guard(soft, soft + SIZE), none);
// The title's CPU touched it: the runtime maps it, read-write, and
// tells. Both views count as written, whatever was written.
// SAFETY: the placeholder takes a view of the section.
let view = unsafe {
MapViewOfFile3(section, GetCurrentProcess(), soft as *const _, 0, SIZE, MEM_REPLACE_PLACEHOLDER, PAGE_READWRITE, std::ptr::null_mut(), 0)
};
assert_eq!(view.Value as usize, soft);
IN_BOOKS.lock().unwrap().retain(|view| view.0 != soft);
title_memory_made_real(soft, SIZE);
assert!(writable(soft));
assert_eq!(guard(real, real + SIZE), Some(blocks(real)));
assert_eq!(guard(soft, soft + SIZE), Some(blocks(soft)));
assert_eq!(guard(real, real + SIZE), none);
// A view as any other now: read-only by our hand, and a write
// through it faults and is reported at both.
assert!(!writable(soft));
// SAFETY: the view, read-write once the handler has opened it.
unsafe { ((soft + 8) as *mut u8).write_volatile(2) };
assert_eq!(guard(real, real + SIZE), Some(block(real, 0)));
assert_eq!(guard(soft, soft + SIZE), Some(block(soft, 0)));
// SAFETY: the bytes went into the section's page.
assert_eq!(unsafe { ((real + 8) as *const u8).read_volatile() }, 2);
for at in [soft, real] {
title_memory_remapped(at, SIZE);
// SAFETY: our views.
unsafe { UnmapViewOfFile(MEMORY_MAPPED_VIEW_ADDRESS { Value: at as *mut _ }) };
}
// SAFETY: the section's last reference.
unsafe { windows_sys::Win32::Foundation::CloseHandle(section) };
}
/// A write of the runtime's own, told at one view of the pages
/// (`writing`): the other views' next checks report the pages written.
#[test]