From 3b5e702f838683ae654085044326e736fa9699a9 Mon Sep 17 00:00:00 2001 From: Kal0s-design <356578040+Kal0s-design@users.noreply.github.com> Date: Mon, 30 Nov 2026 12:33:11 +0100 Subject: [PATCH] d3d12-hle: arrange forgets the slice a page leaves for an unowned one In xmem's arrange (renderer port 1, the 32-bit heap's slices in address order), a page moving to a slice that no page has left its own slice still recorded as its own in the owner map. A later page of the same call moving into that slice then found the first page there and swapped it back, so the first page lost the place it had just been given. The slice it leaves now has no owner. Ported from ahmetbagci's fix in Vice-Tay (fddf8c7). Co-Authored-By: ahmetbagci <6625856+ahmetbagci@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- d3d12/crates/leonida-hle/src/modules/xmem.rs | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/d3d12/crates/leonida-hle/src/modules/xmem.rs b/d3d12/crates/leonida-hle/src/modules/xmem.rs index 99393503db..f047252174 100644 --- a/d3d12/crates/leonida-hle/src/modules/xmem.rs +++ b/d3d12/crates/leonida-hle/src/modules/xmem.rs @@ -1287,6 +1287,9 @@ fn arrange(base: usize, pages: &[usize]) { { slice.index = have; owner.insert(have, other); + } else { + // No page had `want`: none has `have` once this one leaves it. + owner.remove(&have); } if let Some(slice) = table.pages.get_mut(number) { slice.index = want; @@ -2558,6 +2561,26 @@ mod tests { assert_eq!(XMemFreePhysicalPages(4, pages.as_ptr()), 1); } + #[test] + fn arrange_forgets_the_slice_a_page_left_for_an_unowned_one() { + // A page that moves to a slice no page has leaves its own slice + // with no page either: a later page moved there in the same call + // must not move the first one back. + let base = XMemVirtualAlloc(std::ptr::null_mut(), 4 * PAGE, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, GRAPHICS | ADDRESS_32BIT, PAGE_NOACCESS); + assert!(!base.is_null() && (base as usize) < 1 << 32); + let pages = allocate(4); + let index = |number: usize| page_table().pages.get(&number).map(|s| s.index); + let at = |page: usize| base.wrapping_byte_add(page * PAGE); + // Slice 1 has no page any more. + assert_eq!(XMemFreePhysicalPages(1, &pages[1]), 1); + // pages[2] goes from slice 2 to the free slice 1, then pages[3] + // from slice 3 to slice 2, which pages[2] just left. + arrange(at(1) as usize, &pages[2..]); + assert_eq!([index(pages[2]), index(pages[3])], [Some(1), Some(2)]); + assert_eq!(XMemFreePhysicalPages(1, &pages[0]), 1); + assert_eq!(XMemFreePhysicalPages(2, pages[2..].as_ptr()), 1); + } + /// The two steps of a page-mapping copy: what the ranges are to show is looked up at the /// call; the pool mapped to other pages before the mapping is done changes nothing of it. A /// page that shows the same page already stays mapped, and a source of 0 unmaps.