From ab3a1ec936e42de18cf0a0e3d30b8a1290930555 Mon Sep 17 00:00:00 2001 From: Afonso Pereia Date: Tue, 24 Nov 2026 15:00:00 +0000 Subject: [PATCH] Memory: preserve XMem graphics alignment in the Windows fallback XMemAllocDefault computed graphics alignment but discarded it when sparse and coherent allocations failed. Keep the 64 KiB path unchanged and use VirtualAlloc2 address requirements for larger alignments, without overallocating or changing release semantics. The extracted original fallback failed the Windows regression: a 128 KiB request returned address 0x29abfa30000, offset 65536 from the required boundary. Afterward the test passed all five alignment cases (64 KiB through 2 GiB), region size, protection, zeroing and release checks: 1 passed / 0 failed / 0 ignored / 719 filtered out. Windows 11 ARM64 UTM, x86_64 MSVC. Full suite and real-title main/head smoke not run. Assisted-by: omp (openai-codex/gpt-6-astra) --- README.md | 2 ++ crates/xcl/src/hle/kernelx.rs | 47 ++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0710608d4d..ccf7c864b9 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,8 @@ xcl rpf D:\data\shaders.rpf --extract NAME --out folder - Title data: `%LOCALAPPDATA%\XCL\\` (`%XCL_DATA_ROOT%`): `xcl.toml`, `overlay\`, `saves\`, `pls\` (the title's `S:\`), `temp\` (`T:\`), `cache\`, `logs\xcl.log`, `logs\report.json` (the previous run's: `xcl.prev.log`, `report.prev.json`). +- `XMemAllocDefault` preserves graphics-allocation address alignment when falling back to + Windows memory, including the 4 MiB alignment requested by console page-size attributes. ## Tests diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index 8aa3bc2002..cd4d852ded 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -531,6 +531,21 @@ fn xmem_graphics_layout(size: usize, a: XAllocAttrs) -> Option<(usize, usize)> { Some((rounded, align)) } +unsafe fn windows_graphics_alloc(size: usize, align: usize) -> *mut c_void { + if align <= crate::gpu::uma::GRAIN { + return mem::VirtualAlloc(std::ptr::null(), size, mem::MEM_COMMIT | mem::MEM_RESERVE, mem::PAGE_READWRITE); + } + let mut requirements = mem::MEM_ADDRESS_REQUIREMENTS { Alignment: align, ..Default::default() }; + let mut parameter = mem::MEM_EXTENDED_PARAMETER { + Anonymous1: mem::MEM_EXTENDED_PARAMETER_0 { _bitfield: mem::MemExtendedParameterAddressRequirements as u64 }, + Anonymous2: mem::MEM_EXTENDED_PARAMETER_1 { Pointer: (&mut requirements as *mut mem::MEM_ADDRESS_REQUIREMENTS).cast() }, + }; + mem::VirtualAlloc2( + std::ptr::null_mut(), std::ptr::null(), size, + mem::MEM_COMMIT | mem::MEM_RESERVE, mem::PAGE_READWRITE, &mut parameter, 1, + ) +} + pub unsafe extern "system" fn XMemAllocDefault(size: usize, attrs: u64) -> *mut c_void { let a = XAllocAttrs::decode(attrs); if a.memory_type == 0 { @@ -551,7 +566,7 @@ pub unsafe extern "system" fn XMemAllocDefault(size: usize, attrs: u64) -> *mut trace_mem("XMemAlloc (graphics, UMA)", 0, rounded, attrs, 0, q as usize); return q as *mut c_void; } - mem::VirtualAlloc(std::ptr::null(), rounded, mem::MEM_COMMIT | mem::MEM_RESERVE, mem::PAGE_READWRITE) + windows_graphics_alloc(rounded, align) } pub unsafe extern "system" fn XMemFreeDefault(p: *mut c_void, attrs: u64) { @@ -2633,6 +2648,36 @@ mod tests { } } + #[test] + fn windows_graphics_allocations_preserve_alignment() { + // Test the native fallback directly so initialized UMA cannot hide it. + for (page_size, alignment_bits, expected_align) in [ + (0u64, 0u64, 1usize << 16), + (0, 17, 1 << 17), + (2, 0, 4 << 20), + (2, 25, 1 << 25), + (0, 31, 1 << 31), + ] { + let attrs = (1 << 29) | (page_size << 14) | (alignment_bits << 24); + let (size, align) = xmem_graphics_layout(0x1_0001, XAllocAttrs::decode(attrs)).unwrap(); + unsafe { + let p = windows_graphics_alloc(size, align); + assert!(!p.is_null(), "attrs {attrs:#x}: error {}", fnd::GetLastError()); + let mut info: mem::MEMORY_BASIC_INFORMATION = std::mem::zeroed(); + let queried = mem::VirtualQuery(p, &mut info, std::mem::size_of_val(&info)); + let zeroed = std::slice::from_raw_parts(p.cast::<u8>(), size).iter().all(|&v| v == 0); + assert_ne!(mem::VirtualFree(p, 0, mem::MEM_RELEASE), 0); + assert_ne!(queried, 0); + assert_eq!(p as usize % expected_align, 0, "attrs {attrs:#x}, address {p:p}"); + assert_eq!(info.AllocationBase, p); + assert_eq!(info.RegionSize, if page_size == 2 { 4 << 20 } else { 0x2_0000 }); + assert_eq!(info.State, mem::MEM_COMMIT); + assert_eq!(info.Protect, mem::PAGE_READWRITE); + assert!(zeroed, "attrs {attrs:#x}: allocation was not zeroed"); + } + } + } + #[test] fn xmem_and_physical_page_overflows_fail_cleanly() { unsafe {