From 7fa09f37b99fc8feef6a063f4f4d07cbb50b73cb Mon Sep 17 00:00:00 2001 From: b3IKK <278540256+b3IKK@users.noreply.github.com> Date: Tue, 24 Nov 2026 16:34:17 +0000 Subject: [PATCH] XAudio2: honor requested alignment for 32-bit APU addresses in ApuAlloc ApuAlloc aligned the host VA via alloc_aligned(size, alignment), but Apu::address / Apu::find_free_range hardcoded 4 KiB (0x1000) alignment for the returned 32-bit APU address. After any smaller allocation advanced `APU.next` off a larger power-of-two boundary, subsequent ApuAlloc calls with `alignment > 0x1000` returned a misaligned APU address. Pass `alignment` through `Apu::address_aligned` and `find_free_range`, aligning both candidate starts and overlap skips up to `alignment.max(1).next_power_of_two().max(0x1000)` with checked math. Assisted-by: OpenCode (Gemini 4 Neon) --- crates/xcl/src/hle/acphal.rs | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/crates/xcl/src/hle/acphal.rs b/crates/xcl/src/hle/acphal.rs index 2210066f1c..21f2553b5b 100644 --- a/crates/xcl/src/hle/acphal.rs +++ b/crates/xcl/src/hle/acphal.rs @@ -218,18 +218,22 @@ impl Apu { .max() } - fn find_free_range(&self, step: u64) -> Option { + fn find_free_range(&self, step: u64, align: u32) -> Option { if step == 0 || step > APU_LIMIT - APU_BASE { return None; } + let align = (align.max(1) as u64).checked_next_power_of_two()?.max(0x1000); + let align_up = |x: u64| x.checked_add(align - 1).map(|v| v & !(align - 1)); // Fast path from `self.next`, then wrap once to `APU_BASE` to reclaim freed ranges. for start_hint in [self.next as u64, APU_BASE] { - let mut cand = start_hint.max(APU_BASE); + let Some(mut cand) = align_up(start_hint.max(APU_BASE)) else { + continue; + }; while let Some(end) = cand.checked_add(step).filter(|&e| e <= APU_LIMIT) { match self.overlaps(cand, end) { None => return Some(cand as u32), Some(skip_to) => { - let next = (skip_to + 0xFFF) & !0xFFF; + let Some(next) = align_up(skip_to) else { break }; if next <= cand { break; } @@ -242,8 +246,8 @@ impl Apu { } /// The APU address of `va` – in a known region (for non-owned mappings covering the full span), - /// or a new non-overlapping 32-bit APU range of `size` bytes. - fn address(&mut self, va: usize, size: u32, owned: bool) -> Option { + /// or a new non-overlapping 32-bit APU range of `size` bytes aligned to `align`. + fn address_aligned(&mut self, va: usize, size: u32, align: u32, owned: bool) -> Option { let span = size.max(1) as usize; if !owned { if let Some(r) = self.regions.iter().find(|r| { @@ -253,12 +257,16 @@ impl Apu { } } let step = (size.max(1) as u64 + 0xFFF) & !0xFFF; - let apu = self.find_free_range(step)?; + let apu = self.find_free_range(step, align)?; let next = (apu as u64) + step; self.next = if next < APU_LIMIT { next as u32 } else { APU_BASE as u32 }; self.regions.push(Region { va, size, apu, owned }); Some(apu) } + + fn address(&mut self, va: usize, size: u32, owned: bool) -> Option { + self.address_aligned(va, size, 0x1000, owned) + } } /// `size` bytes aligned to `align` (zeroed; the allocation's base is `va` rounded down to @@ -306,7 +314,7 @@ pub unsafe extern "system" fn ApuAlloc(va: *mut *mut c_void, apu: *mut u32, size *va = std::ptr::null_mut(); return E_OUTOFMEMORY; }; - let Some(a) = APU.lock().unwrap().address(p, size, true) else { + let Some(a) = APU.lock().unwrap().address_aligned(p, size, alignment, true) else { VirtualFree(p as *mut c_void, 0, MEM_RELEASE); *va = std::ptr::null_mut(); return E_OUTOFMEMORY; @@ -1293,4 +1301,18 @@ mod tests { assert_eq!(mbi.State, MEM_FREE, "data buffer must be released when event creation fails"); } } + + #[test] + fn apu_alloc_honors_alignment_for_apu_address() { + unsafe { + let (mut va1, mut apu1) = (std::ptr::null_mut(), 0u32); + let (mut va2, mut apu2) = (std::ptr::null_mut(), 0u32); + assert_eq!(ApuAlloc(&mut va1, &mut apu1, 0x1000, 0x1000, 0), S_OK); + assert_eq!(ApuAlloc(&mut va2, &mut apu2, 0x3000, 0x2_0000, 0), S_OK); + assert_eq!((va2 as usize) % 0x2_0000, 0, "VA must be 0x20000-aligned"); + assert_eq!(apu2 % 0x2_0000, 0, "APU address must also be 0x20000-aligned"); + assert_eq!(ApuFree(va1), S_OK); + assert_eq!(ApuFree(va2), S_OK); + } + } }