diff --git a/crates/xcl/src/hle/d3d12x/console_heap.rs b/crates/xcl/src/hle/d3d12x/console_heap.rs index 30dbf2c..efc4d80 100644 --- a/crates/xcl/src/hle/d3d12x/console_heap.rs +++ b/crates/xcl/src/hle/d3d12x/console_heap.rs @@ -275,12 +275,19 @@ 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| { - if crate::util::mac_host() { - if let Some(tags) = LAST_TAGS.lock().unwrap().as_mut() { - tags.retain(|(heap, _), _| *heap != obj); + // 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) @@ -632,6 +639,39 @@ 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::Uma, + }); + { + 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); + } + // 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.remove(&(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]