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.