|
| 1 | +use crate::*; |
| 2 | +use rustc_data_structures::fx::FxHashSet; |
| 3 | + |
| 4 | +pub fn garbage_collect_tags<'mir, 'tcx>( |
| 5 | + ecx: &mut MiriEvalContext<'mir, 'tcx>, |
| 6 | +) -> InterpResult<'tcx> { |
| 7 | + // No reason to do anything at all if stacked borrows is off. |
| 8 | + if ecx.machine.stacked_borrows.is_none() { |
| 9 | + return Ok(()); |
| 10 | + } |
| 11 | + |
| 12 | + let mut tags = FxHashSet::default(); |
| 13 | + |
| 14 | + ecx.machine.tls.iter(|scalar| { |
| 15 | + if let Scalar::Ptr(Pointer { provenance: Provenance::Concrete { sb, .. }, .. }, _) = scalar |
| 16 | + { |
| 17 | + tags.insert(*sb); |
| 18 | + } |
| 19 | + }); |
| 20 | + |
| 21 | + // Normal memory |
| 22 | + ecx.memory.alloc_map().iter(|it| { |
| 23 | + for (_id, (_kind, alloc)) in it { |
| 24 | + let stacked_borrows = alloc.extra.stacked_borrows.as_ref().unwrap(); |
| 25 | + // Base tags: TODO: Do we need this? |
| 26 | + for stack in stacked_borrows.borrow_mut().iter_all() { |
| 27 | + if let Some(item) = stack.get(0) { |
| 28 | + tags.insert(item.tag()); |
| 29 | + } |
| 30 | + } |
| 31 | + for (_size, prov) in alloc.relocations().iter() { |
| 32 | + if let Provenance::Concrete { sb, .. } = prov { |
| 33 | + tags.insert(*sb); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + // Locals |
| 40 | + // This code is a mess because... |
| 41 | + // force_allocation only works on locals from the current thread. This is because locals don't |
| 42 | + // remember what thread they are from, they're just an index. So attempting to use a local on |
| 43 | + // the wrong thread is simply an error. |
| 44 | + // Additionally, InterpCx::force_allocation takes out a mutable borrow of the whole InterpCx. |
| 45 | + // So we need to do this whole dance with indexes and a clone of the return place in order to |
| 46 | + // avoid holding a shared borrow of anything during the force_allocation call. |
| 47 | + let tids = |
| 48 | + ecx.machine.threads.threads.iter_enumerated().map(|(tid, _thread)| tid).collect::<Vec<_>>(); |
| 49 | + let original_tid = ecx.machine.threads.active_thread; |
| 50 | + |
| 51 | + for tid in tids { |
| 52 | + ecx.machine.threads.active_thread = tid; |
| 53 | + for f in 0..ecx.active_thread_stack().len() { |
| 54 | + // Handle the return place of each frame |
| 55 | + let frame = &ecx.active_thread_stack()[f]; |
| 56 | + let return_place = ecx.force_allocation(&frame.return_place.clone())?; |
| 57 | + return_place.map_provenance(|p| { |
| 58 | + if let Some(Provenance::Concrete { sb, .. }) = p { |
| 59 | + tags.insert(sb); |
| 60 | + } |
| 61 | + p |
| 62 | + }); |
| 63 | + |
| 64 | + let frame = &ecx.active_thread_stack()[f]; |
| 65 | + for local in frame.locals.iter() { |
| 66 | + if let LocalValue::Live(Operand::Immediate(Immediate::Scalar( |
| 67 | + ScalarMaybeUninit::Scalar(Scalar::Ptr(ptr, _)), |
| 68 | + ))) = local.value |
| 69 | + { |
| 70 | + if let Provenance::Concrete { sb, .. } = ptr.provenance { |
| 71 | + tags.insert(sb); |
| 72 | + } |
| 73 | + } |
| 74 | + if let LocalValue::Live(Operand::Immediate(Immediate::ScalarPair(s1, s2))) = |
| 75 | + local.value |
| 76 | + { |
| 77 | + if let ScalarMaybeUninit::Scalar(Scalar::Ptr(ptr, _)) = s1 { |
| 78 | + if let Provenance::Concrete { sb, .. } = ptr.provenance { |
| 79 | + tags.insert(sb); |
| 80 | + } |
| 81 | + } |
| 82 | + if let ScalarMaybeUninit::Scalar(Scalar::Ptr(ptr, _)) = s2 { |
| 83 | + if let Provenance::Concrete { sb, .. } = ptr.provenance { |
| 84 | + tags.insert(sb); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if let LocalValue::Live(Operand::Indirect(MemPlace { ptr, .. })) = local.value { |
| 90 | + if let Some(Provenance::Concrete { sb, .. }) = ptr.provenance { |
| 91 | + tags.insert(sb); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + ecx.machine.threads.active_thread = original_tid; |
| 98 | + |
| 99 | + ecx.memory.alloc_map().iter(|it| { |
| 100 | + for (id, (_kind, alloc)) in it { |
| 101 | + // Don't GC any allocation which has been exposed |
| 102 | + if ecx.machine.intptrcast.borrow().is_exposed(*id) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + let stacked_borrows = alloc.extra.stacked_borrows.as_ref().unwrap(); |
| 107 | + for stack in stacked_borrows.borrow_mut().iter_all() { |
| 108 | + stack.retain(&tags); |
| 109 | + } |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + Ok(()) |
| 114 | +} |
0 commit comments