Kernel reads, remapped views, freed memory and lost writes of title memory

- What the kernel is writing (file reads) is not guarded by a refresh before
  the call is done (`kernel_writing`): the read would have failed.
- A title thread that uses a page while the runtime remaps another page of
  the same view waits for that in the fault handler; it faulted on a page
  the title never gave up.
- Blocks being unmapped are not guarded again, memory the title frees leaves
  no notes of its blocks (its VirtualFree is taken over), and other memory at
  an address once guarded or absent keeps its protection.
- The second part of a submission's one asking has a number of its own.
- What shaders wrote waits for a change of mapping instead of being lost;
  what could not be written is counted and said; zeros into title memory
  that went are no fault.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 23:10:44 -06:00
1 parent 9f22d574bc
commit 8a475da9cf
9 files changed
+803 -32

No files matched your search

+49 -4
View File
@@ -704,6 +704,22 @@ fn warp_adapter() -> windows::core::Result<IDXGIAdapter> {
#[cfg(test)]
mod tests {
/// Zeros for a title destination that went meanwhile are no fault.
#[test]
fn zeros_where_nothing_is_are_lost() {
use windows_sys::Win32::System::Memory::*;
// SAFETY: a reservation of ours, its first page committed.
unsafe {
let at = VirtualAlloc(std::ptr::null(), 1 << 16, MEM_RESERVE, PAGE_NOACCESS).cast::<u8>();
assert!(!VirtualAlloc(at.cast(), 4096, MEM_COMMIT, PAGE_READWRITE).is_null());
at.write_bytes(7, 4096);
super::zero(at, 4096);
assert!(std::slice::from_raw_parts(at, 4096).iter().all(|&b| b == 0));
super::zero(at.add(4096), 4096);
VirtualFree(at.cast(), 0, MEM_RELEASE);
}
}
#[test]
fn sparse_copies_preserve_islands_and_observe_remaps() {
use windows_sys::Win32::System::Memory::*;
@@ -6407,7 +6423,10 @@ pub(crate) fn copy_mapped(dst: *mut u8, src: *const u8, len: usize, title_dst: b
}
#[cfg(feature = "diagnostics")]
let moved = std::time::Instant::now();
let whole = read(src, dst, len);
let since = crate::watch::changes();
// (A title destination that is away only while the runtime changes a
// mapping beside it is written when that is done.)
let whole = read(src, dst, len) || title_dst && crate::watch::again_after_change(since, || read(src, dst, len));
#[cfg(feature = "diagnostics")]
crate::copystat::add(crate::copystat::C::MoveUs, moved.elapsed().as_micros() as u64);
if whole {
@@ -6417,6 +6436,7 @@ pub(crate) fn copy_mapped(dst: *mut u8, src: *const u8, len: usize, title_dst: b
// not all mapped. Then the destination stays as it is (the kernel may
// have written its first pages).
if title_dst && !ask_first && !crate::texture::committed(dst, len) {
crate::watch::lost("a copy of the title's", dst as usize, len);
return;
}
#[cfg(feature = "diagnostics")]
@@ -6476,14 +6496,29 @@ fn read_pieces(src: *const u8, dst: *mut u8, len: usize) {
if !read(at as *const u8, to, n) {
#[cfg(feature = "diagnostics")]
crate::copystat::add(crate::copystat::C::Unmapped, n as u64);
// SAFETY: inside the destination, which is mapped.
unsafe { to.write_bytes(0, n) };
zero(to, n);
}
});
}
});
}
/// Zeros at `to`, in one 4 KiB page or two. It may be title memory (the
/// destination of a title's copy), which can go between the question whether
/// it is there and this.
fn zero(to: *mut u8, n: usize) {
static ZEROS: [u8; 4096] = [0; 4096];
if n <= ZEROS.len() && crate::safecopy::on() {
// SAFETY: a destination that is not there fails the copy.
if !unsafe { crate::safecopy::copy(to, ZEROS.as_ptr(), n) } {
crate::watch::lost("zeros", to as usize, n);
}
return;
}
// SAFETY: inside the destination, which is mapped.
unsafe { to.write_bytes(0, n) };
}
/// Reads title memory at `src` into `dst`; false if a page of it is not
/// readable.
pub(crate) fn read_into(src: *const u8, dst: &mut [u8]) -> bool {
@@ -6537,6 +6572,7 @@ pub(crate) fn write_mapped(dst: *mut u8, src: *const u8, len: usize) {
use windows_sys::Win32::System::Diagnostics::Debug::WriteProcessMemory;
use windows_sys::Win32::System::Threading::GetCurrentProcess;
let direct = direct_writes();
let since = crate::watch::changes();
let write = |at: usize, n: usize| {
let from = src.wrapping_add(at - dst as usize);
if crate::safecopy::on() {
@@ -6563,7 +6599,16 @@ pub(crate) fn write_mapped(dst: *mut u8, src: *const u8, len: usize) {
for_pieces(dst as usize, len, 64 << 10, |at, n| {
if !write(at, n) {
for_pieces(at, n, 4 << 10, |at, n| {
write(at, n);
if write(at, n) {
return;
}
// The kernel's write (`VICTORY_SAFE_COPY=0`) fails where a
// refresh guarded the block again since the caller opened
// it; and any where the runtime was changing a mapping.
crate::watch::writing(at, n);
if !write(at, n) && !crate::watch::again_after_change(since, || write(at, n)) {
crate::watch::lost("a host copy's bytes", at, n);
}
});
}
});