From 3ae08413426eee45cc95f2c3c7e3d2b169ed1142 Mon Sep 17 00:00:00 2001 From: notdxbi <69725883+notdxbi@users.noreply.github.com> Date: Tue, 24 Nov 2026 00:59:32 +0000 Subject: [PATCH] kernelx: XMemMapPhysicalPagesScatter with no page array unmaps A retail game unmaps addresses while it streams its world with XMemMapPhysicalPagesScatter(addresses, n, NULL); it checks the result and runs into its own int3 when the call fails (+0xb18de7, then its crash reporter). XCL refused a NULL page array with ERROR_INVALID_PARAMETER. Page 0 already means "unmap this address" in a scatter, so no array is taken as page 0 for every address. Test scatter_without_pages_unmaps: two pages mapped and written, unmapped with no array (the call succeeds, the addresses are no longer committed), mapped again they read what was written. It fails without the change ("refused"). Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/kernelx.rs | 44 ++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index 69f13e05c..088ba04b7 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -1221,9 +1221,11 @@ pub unsafe extern "system" fn XMemMapPhysicalPages(va: *mut c_void, n: usize, ar } /// `BOOL XMemMapPhysicalPagesScatter(void** addresses, SIZE_T count, ULONG64* pages)`: one 64 KiB -/// page at each address (page 0: the address is unmapped) +/// page at each address (page 0: the address is unmapped). No page array unmaps every address: +/// a retail game unmaps that way while it streams its world and stops on its own `int3` when the +/// call fails. pub unsafe extern "system" fn XMemMapPhysicalPagesScatter(addresses: *const *mut c_void, n: usize, pages: *mut u64) -> BOOL { - if addresses.is_null() || pages.is_null() { + if addresses.is_null() { fnd::SetLastError(fnd::ERROR_INVALID_PARAMETER); return 0; } @@ -1233,7 +1235,12 @@ pub unsafe extern "system" fn XMemMapPhysicalPagesScatter(addresses: *const *mut } let mut g = AWAY.lock().unwrap(); let away = g.get_or_insert_with(Away::default); - let ok = (0..n).all(|i| !map_pages(away, *addresses.add(i), 1, pages.add(i) as *mut usize).is_null()); + // no page array: page 0 (unmap) for every address + let ok = (0..n).all(|i| { + let mut none = 0usize; + let page = if pages.is_null() { &mut none as *mut usize } else { pages.add(i) as *mut usize }; + !map_pages(away, *addresses.add(i), 1, page).is_null() + }); give_back_homes(away); ok as BOOL } @@ -2008,6 +2015,37 @@ mod tests { } } + /// `XMemMapPhysicalPagesScatter` with no page array unmaps each address, as page 0 does (a + /// retail game unmaps so while it streams; refused, it stopped on its own `int3`). + #[test] + fn scatter_without_pages_unmaps() { + unsafe { + let Some(_) = crate::gpu::uma::get() else { return }; + let mut n = 2; + let mut pages = [0usize; 2]; + assert_eq!(AllocateTitlePhysicalPages(std::ptr::null_mut(), 0, &mut n, pages.as_mut_ptr()), 1); + let r = mem::VirtualAlloc(std::ptr::null(), 2 * PHYS_PAGE, mem::MEM_RESERVE, mem::PAGE_READWRITE) as usize; + assert!(r != 0); + let addrs = [r as *mut c_void, (r + PHYS_PAGE) as *mut c_void]; + let mut ids = [pages[0] as u64, pages[1] as u64]; + assert_eq!(XMemMapPhysicalPagesScatter(addrs.as_ptr(), 2, ids.as_mut_ptr()), 1); + (r as *mut u32).write_volatile(7); + ((r + PHYS_PAGE) as *mut u32).write_volatile(9); + assert_eq!(XMemMapPhysicalPagesScatter(addrs.as_ptr(), 2, std::ptr::null_mut()), 1, "no page array: unmapped, not refused"); + for a in addrs { + assert!(!crate::gpu::shader::readable(a as usize, 4), "{a:p} still mapped"); + } + // the pages kept what was written: mapped again, they read it + assert_eq!(XMemMapPhysicalPagesScatter(addrs.as_ptr(), 2, ids.as_mut_ptr()), 1); + assert_eq!(((r as *const u32).read_volatile(), ((r + PHYS_PAGE) as *const u32).read_volatile()), (7, 9)); + assert_eq!(XMemMapPhysicalPagesScatter(addrs.as_ptr(), 2, std::ptr::null_mut()), 1); + // no addresses: the same answer with and without a page array + let mut none = [0u64; 1]; + assert_eq!(XMemMapPhysicalPagesScatter(addrs.as_ptr(), 0, std::ptr::null_mut()), XMemMapPhysicalPagesScatter(addrs.as_ptr(), 0, none.as_mut_ptr())); + mem::VirtualFree(r as *mut c_void, 0, mem::MEM_RELEASE); + } + } + /// `XMemMapPhysicalPagesScatter` against a model of physical pages: random scatters of /// pages to addresses (page 0: unmap), writes through the addresses and maps of the whole /// allocation at its home. After each step every page reads what was last written to it