From c05da4b5de35bf7f74b3d8126927d4d0e331270e Mon Sep 17 00:00:00 2001 From: towiice Date: Tue, 24 Nov 2026 23:02:20 -0300 Subject: [PATCH 1/2] D3D12.X: drop a heap's resync tags after releasing the heap lock unregister removed the heap's LAST_TAGS entries while holding HEAPS, and only on a Mac host. The removal now runs after HEAPS is released, on every host. No failure was observed: resync_title_writes, which fills the tags, runs only on a Mac host, so elsewhere the map stays empty. Co-Authored-By: Claude Sonnet 5.5 --- crates/xcl/src/hle/d3d12x/console_heap.rs | 45 +++++++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/console_heap.rs b/crates/xcl/src/hle/d3d12x/console_heap.rs index 30dbf2c..245c0f9 100644 --- a/crates/xcl/src/hle/d3d12x/console_heap.rs +++ b/crates/xcl/src/hle/d3d12x/console_heap.rs @@ -277,16 +277,14 @@ pub fn register(obj: usize, n: u32, pc_cpu: u64, pc_gpu: u64, inc: u32, sampler: pub fn unregister(obj: usize) { let gone = { let mut heaps = HEAPS.lock().unwrap(); - heaps.iter().position(|h| h.obj == obj).map(|i| { - if crate::util::mac_host() { - if let Some(tags) = LAST_TAGS.lock().unwrap().as_mut() { - tags.retain(|(heap, _), _| *heap != obj); - } - } - heaps.swap_remove(i) - }) + heaps.iter().position(|h| h.obj == obj).map(|i| heaps.swap_remove(i)) }; if let Some(h) = gone { + if let Ok(mut tags) = LAST_TAGS.lock() { + if let Some(m) = tags.as_mut() { + m.retain(|&(heap, _), _| heap != obj); + } + } for (p, &f) in h.pages.iter().enumerate().filter(|&(_, &f)| f != UNMAPPED) { release_global(f, page_descriptors(h.n, p as u32)); } @@ -632,6 +630,37 @@ mod tests { assert!(cpu_mirror_srv(&heaps, 0).is_none()); // no assigned page } + #[test] + fn unregister_clears_cached_resync_tags_for_that_heap() { + let obj_a = 0xDEAD_1001usize; + let obj_b = 0xDEAD_1002usize; + HEAPS.lock().unwrap().push(Heap { + obj: obj_a, + shadow: 0, + n: 4, + pc_cpu: 0, + pc_gpu: 0, + inc: 32, + sampler: false, + pages: vec![UNMAPPED], + visible: true, + memory: Memory::Title, + }); + { + let mut lock = LAST_TAGS.lock().unwrap(); + let tags = lock.get_or_insert_with(HashMap::new); + tags.insert((obj_a, 0), 0x1111); + tags.insert((obj_a, 3), 0x2222); + tags.insert((obj_b, 0), 0x3333); + } + unregister(obj_a); + let lock = LAST_TAGS.lock().unwrap(); + let tags = lock.as_ref().unwrap(); + assert!(!tags.contains_key(&(obj_a, 0))); + assert!(!tags.contains_key(&(obj_a, 3))); + assert_eq!(tags.get(&(obj_b, 0)), Some(&0x3333)); + } + /// Ranges of the shared heap: released ones first (first fit, split), then new ones, and /// none past its size. #[test] From 33930d43ddaeb0af690bae51efe2ef9c0faab4b0 Mon Sep 17 00:00:00 2001 From: utkarshxdev <216722806+utkarshxdev@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:41:57 +0530 Subject: [PATCH 2/2] Keep heap tag cleanup under the heap lock --- crates/xcl/src/hle/d3d12x/console_heap.rs | 33 +++++++++++++++-------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/console_heap.rs b/crates/xcl/src/hle/d3d12x/console_heap.rs index 245c0f9..efc4d80 100644 --- a/crates/xcl/src/hle/d3d12x/console_heap.rs +++ b/crates/xcl/src/hle/d3d12x/console_heap.rs @@ -275,16 +275,25 @@ pub fn register(obj: usize, n: u32, pc_cpu: u64, pc_gpu: u64, inc: u32, sampler: /// The heap object was released for good: its slots go back to unified memory. pub fn unregister(obj: usize) { + unregister_with_resync(obj, crate::util::mac_host()); +} + +fn unregister_with_resync(obj: usize, track_resync: bool) { let gone = { let mut heaps = HEAPS.lock().unwrap(); - heaps.iter().position(|h| h.obj == obj).map(|i| heaps.swap_remove(i)) + heaps.iter().position(|h| h.obj == obj).map(|i| { + // Keep registration serialized with tag removal: COM addresses can be reused. + if track_resync { + if let Ok(mut tags) = LAST_TAGS.lock() { + if let Some(tags) = tags.as_mut() { + tags.retain(|&(heap, _), _| heap != obj); + } + } + } + heaps.swap_remove(i) + }) }; if let Some(h) = gone { - if let Ok(mut tags) = LAST_TAGS.lock() { - if let Some(m) = tags.as_mut() { - m.retain(|&(heap, _), _| heap != obj); - } - } for (p, &f) in h.pages.iter().enumerate().filter(|&(_, &f)| f != UNMAPPED) { release_global(f, page_descriptors(h.n, p as u32)); } @@ -644,7 +653,7 @@ mod tests { sampler: false, pages: vec![UNMAPPED], visible: true, - memory: Memory::Title, + memory: Memory::Uma, }); { let mut lock = LAST_TAGS.lock().unwrap(); @@ -653,12 +662,14 @@ mod tests { tags.insert((obj_a, 3), 0x2222); tags.insert((obj_b, 0), 0x3333); } - unregister(obj_a); - let lock = LAST_TAGS.lock().unwrap(); - let tags = lock.as_ref().unwrap(); + // Exercise the Mac cleanup path on any test host. + unregister_with_resync(obj_a, true); + assert!(start(obj_a).is_none()); + let mut lock = LAST_TAGS.lock().unwrap(); + let tags = lock.as_mut().unwrap(); assert!(!tags.contains_key(&(obj_a, 0))); assert!(!tags.contains_key(&(obj_a, 3))); - assert_eq!(tags.get(&(obj_b, 0)), Some(&0x3333)); + assert_eq!(tags.remove(&(obj_b, 0)), Some(0x3333)); } /// Ranges of the shared heap: released ones first (first fit, split), then new ones, and