diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index 2f3303a970..296c36c5f6 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -967,6 +967,36 @@ fn watch_ranges(regions: &[(usize, usize)], mut wanted: Vec<(usize, usize)>) -> out } +/// Copy `[start, end)` into `dst` (which must be pre-zeroed and at least `end - start` bytes), +/// leaving any page that is concurrently unmapped or made unreadable as zero instead of faulting. +unsafe fn copy_readable_range(start: usize, end: usize, dst: *mut u8) { + use windows_sys::Win32::System::Diagnostics::Debug::ReadProcessMemory; + use windows_sys::Win32::System::Threading::GetCurrentProcess; + const PAGE: usize = 0x1000; + if end <= start { + return; + } + let proc = GetCurrentProcess(); + let mut read = 0usize; + if ReadProcessMemory(proc, start as *const c_void, dst as *mut c_void, end - start, &mut read) != 0 { + return; + } + // A page inside `[start, end)` became unreadable between VirtualQuery and ReadProcessMemory + // (e.g. a streaming thread unmapped physical pages). On Windows, ReadProcessMemory may report + // ERROR_PARTIAL_COPY with `read == 0` even when leading pages are readable, so retry the + // uncopied remainder one page at a time to preserve every readable page. + let mut cur = start.saturating_add(read).min(end); + while cur < end { + let next = (cur & !(PAGE - 1)).saturating_add(PAGE).min(end); + let mut page_read = 0usize; + let _ = ReadProcessMemory(proc, cur as *const c_void, dst.add(cur - start) as *mut c_void, next - cur, &mut page_read); + if next <= cur { + break; + } + cur = next; + } +} + /// `len` bytes at `at`: the committed, readable pages copied, the rest zero (a streaming pool /// has only some of its pages mapped). unsafe fn read_committed(at: usize, len: usize) -> Vec { @@ -978,16 +1008,20 @@ unsafe fn read_committed(at: usize, len: usize) -> Vec { std::ptr::copy_nonoverlapping(at as *const u8, out.as_mut_ptr(), len); return out; } + let limit = at.saturating_add(len); let mut p = at; - while p < at + len { + while p < limit { let mut m: MEMORY_BASIC_INFORMATION = std::mem::zeroed(); if VirtualQuery(p as *const c_void, &mut m, std::mem::size_of_val(&m)) == 0 { break; } - let end = (m.BaseAddress as usize + m.RegionSize).min(at + len); + let end = (m.BaseAddress as usize).saturating_add(m.RegionSize).min(limit); + if end <= p { + break; + } let readable = m.State == MEM_COMMIT && m.Protect & 0xEE != 0 && m.Protect & 0x100 == 0; // R / RW / WC / X-R…, no guard if readable { - std::ptr::copy_nonoverlapping(p as *const u8, out.as_mut_ptr().add(p - at), end - p); + copy_readable_range(p, end, out.as_mut_ptr().add(p - at)); } p = end; } @@ -1632,4 +1666,66 @@ mod tests { uma.free(base); } } + + #[test] + fn read_committed_skips_unmapped_holes_and_survives_concurrent_decommit() { + use std::sync::atomic::{AtomicBool, Ordering}; + use windows_sys::Win32::System::Memory::{VirtualAlloc, VirtualFree, MEM_COMMIT, MEM_DECOMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE}; + const PAGES: usize = 32; + const PAGE: usize = 0x1000; + const SIZE: usize = PAGES * PAGE; + unsafe { + let base = VirtualAlloc(std::ptr::null(), SIZE, MEM_RESERVE, PAGE_READWRITE) as usize; + assert!(base != 0); + // Commit first and last 4 pages, leave the middle reserved (unmapped hole). + assert!(!VirtualAlloc(base as *mut c_void, 4 * PAGE, MEM_COMMIT, PAGE_READWRITE).is_null()); + assert!(!VirtualAlloc((base + (PAGES - 4) * PAGE) as *mut c_void, 4 * PAGE, MEM_COMMIT, PAGE_READWRITE).is_null()); + std::slice::from_raw_parts_mut(base as *mut u8, 4 * PAGE).fill(0x5A); + std::slice::from_raw_parts_mut((base + (PAGES - 4) * PAGE) as *mut u8, 4 * PAGE).fill(0xA5); + + // Non-page-aligned start/end crossing committed -> hole -> committed. + let got = read_committed(base + 123, SIZE - 246); + assert!(got[..4 * PAGE - 123].iter().all(|&b| b == 0x5A)); + assert!(got[4 * PAGE - 123..(PAGES - 4) * PAGE - 123].iter().all(|&b| b == 0)); + assert!(got[(PAGES - 4) * PAGE - 123..].iter().all(|&b| b == 0xA5)); + + // Direct copy_readable_range across a fixed hole (simulating a decommit after + // VirtualQuery): leading and trailing readable pages must both be preserved even when + // ReadProcessMemory returns ERROR_PARTIAL_COPY with read == 0. + for off in [0usize, 0x7b, 0x104d] { + let len = SIZE - off - 0x33; + let mut direct = vec![0u8; len]; + copy_readable_range(base + off, base + off + len, direct.as_mut_ptr()); + let lead = 4 * PAGE - off; + let tail_start = (PAGES - 4) * PAGE - off; + assert!(direct[..lead].iter().all(|&b| b == 0x5A), "leading pages zeroed at off={off:#x}"); + assert!(direct[lead..tail_start].iter().all(|&b| b == 0), "hole not zero at off={off:#x}"); + assert!(direct[tail_start..].iter().all(|&b| b == 0xA5), "trailing pages mismatch at off={off:#x}"); + } + + // Stress the TOCTOU window: one thread toggles commit/decommit on middle pages while + // read_committed reads the whole span. Previously this faulted with 0xC0000005. + let stop = Arc::new(AtomicBool::new(false)); + let stop_w = Arc::clone(&stop); + let mutator = std::thread::spawn(move || { + let mid = (base + 4 * PAGE) as *mut c_void; + let mid_len = (PAGES - 8) * PAGE; + while !stop_w.load(Ordering::Relaxed) { + if !VirtualAlloc(mid, mid_len, MEM_COMMIT, PAGE_READWRITE).is_null() { + std::ptr::write_bytes(mid as *mut u8, 0x3C, mid_len); + VirtualFree(mid, mid_len, MEM_DECOMMIT); + } + } + }); + for _ in 0..1000 { + let buf = read_committed(base, SIZE); + assert_eq!(buf.len(), SIZE); + assert!(buf[..4 * PAGE].iter().all(|&b| b == 0x5A)); + assert!(buf[(PAGES - 4) * PAGE..].iter().all(|&b| b == 0xA5)); + } + stop.store(true, Ordering::Relaxed); + mutator.join().unwrap(); + VirtualFree(base as *mut c_void, 0, MEM_RELEASE); + } + } }