1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use either::Either;

use rustc_data_structures::fx::FxHashSet;

use crate::*;

pub trait VisitTags {
    fn visit_tags(&self, visit: &mut dyn FnMut(BorTag));
}

impl<T: VisitTags> VisitTags for Option<T> {
    fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
        if let Some(x) = self {
            x.visit_tags(visit);
        }
    }
}

impl<T: VisitTags> VisitTags for std::cell::RefCell<T> {
    fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
        self.borrow().visit_tags(visit)
    }
}

impl VisitTags for BorTag {
    fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
        visit(*self)
    }
}

impl VisitTags for Provenance {
    fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
        if let Provenance::Concrete { tag, .. } = self {
            visit(*tag);
        }
    }
}

impl VisitTags for Pointer<Provenance> {
    fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
        let (prov, _offset) = self.into_parts();
        prov.visit_tags(visit);
    }
}

impl VisitTags for Pointer<Option<Provenance>> {
    fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
        let (prov, _offset) = self.into_parts();
        prov.visit_tags(visit);
    }
}

impl VisitTags for Scalar<Provenance> {
    fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
        match self {
            Scalar::Ptr(ptr, _) => ptr.visit_tags(visit),
            Scalar::Int(_) => (),
        }
    }
}

impl VisitTags for Immediate<Provenance> {
    fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
        match self {
            Immediate::Scalar(s) => {
                s.visit_tags(visit);
            }
            Immediate::ScalarPair(s1, s2) => {
                s1.visit_tags(visit);
                s2.visit_tags(visit);
            }
            Immediate::Uninit => {}
        }
    }
}

impl VisitTags for MemPlaceMeta<Provenance> {
    fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
        match self {
            MemPlaceMeta::Meta(m) => m.visit_tags(visit),
            MemPlaceMeta::None => {}
        }
    }
}

impl VisitTags for ImmTy<'_, Provenance> {
    fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
        (**self).visit_tags(visit)
    }
}

impl VisitTags for MPlaceTy<'_, Provenance> {
    fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
        self.ptr().visit_tags(visit);
        self.meta().visit_tags(visit);
    }
}

impl VisitTags for PlaceTy<'_, Provenance> {
    fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
        match self.as_mplace_or_local() {
            Either::Left(mplace) => mplace.visit_tags(visit),
            Either::Right(_) => (),
        }
    }
}

impl VisitTags for OpTy<'_, Provenance> {
    fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
        match self.as_mplace_or_imm() {
            Either::Left(mplace) => mplace.visit_tags(visit),
            Either::Right(imm) => imm.visit_tags(visit),
        }
    }
}

impl VisitTags for Allocation<Provenance, AllocExtra<'_>> {
    fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
        for prov in self.provenance().provenances() {
            prov.visit_tags(visit);
        }

        self.extra.visit_tags(visit);
    }
}

impl VisitTags for crate::MiriInterpCx<'_, '_> {
    fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
        // Memory.
        self.memory.alloc_map().iter(|it| {
            for (_id, (_kind, alloc)) in it {
                alloc.visit_tags(visit);
            }
        });

        // And all the other machine values.
        self.machine.visit_tags(visit);
    }
}

impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriInterpCxExt<'mir, 'tcx> {
    fn garbage_collect_tags(&mut self) -> InterpResult<'tcx> {
        let this = self.eval_context_mut();
        // No reason to do anything at all if stacked borrows is off.
        if this.machine.borrow_tracker.is_none() {
            return Ok(());
        }

        let mut tags = FxHashSet::default();
        this.visit_tags(&mut |tag| {
            tags.insert(tag);
        });
        self.remove_unreachable_tags(tags);

        Ok(())
    }

    fn remove_unreachable_tags(&mut self, tags: FxHashSet<BorTag>) {
        let this = self.eval_context_mut();
        this.memory.alloc_map().iter(|it| {
            for (_id, (_kind, alloc)) in it {
                if let Some(bt) = &alloc.extra.borrow_tracker {
                    bt.remove_unreachable_tags(&tags);
                }
            }
        });
    }
}