Skip to content

Commit 61e71ce

Browse files
saethlinRalfJung
authored andcommitted
Use VisitProvenance to factor allocation visiting better
1 parent a7153b5 commit 61e71ce

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

src/tools/miri/src/concurrency/weak_memory.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,19 @@ pub struct StoreBufferAlloc {
108108
store_buffers: RefCell<RangeObjectMap<StoreBuffer>>,
109109
}
110110

111-
impl StoreBufferAlloc {
112-
pub fn iter(&self, mut visitor: impl FnMut(&Scalar<Provenance>)) {
111+
impl VisitProvenance for StoreBufferAlloc {
112+
fn visit_provenance(&self, visitor: &mut impl FnMut(SbTag)) {
113113
for val in self
114114
.store_buffers
115115
.borrow()
116116
.iter()
117117
.flat_map(|buf| buf.buffer.iter().map(|element| &element.val))
118118
{
119-
visitor(val)
119+
if let Scalar::Ptr(ptr, _) = val {
120+
if let Provenance::Concrete { sb, .. } = ptr.provenance {
121+
visitor(sb);
122+
}
123+
}
120124
}
121125
}
122126
}

src/tools/miri/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub use crate::range_map::RangeMap;
112112
pub use crate::stacked_borrows::{
113113
CallId, EvalContextExt as StackedBorEvalContextExt, Item, Permission, SbTag, Stack, Stacks,
114114
};
115-
pub use crate::tag_gc::{EvalContextExt as _, VisitMachineValues};
115+
pub use crate::tag_gc::{EvalContextExt as _, VisitMachineValues, VisitProvenance};
116116

117117
/// Insert rustc arguments at the beginning of the argument list that Miri wants to be
118118
/// set per default, for maximal validation power.

src/tools/miri/src/stacked_borrows/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub struct Stacks {
7979
/// Stores past operations on this allocation
8080
history: AllocHistory,
8181
/// The set of tags that have been exposed inside this allocation.
82-
pub exposed_tags: FxHashSet<SbTag>,
82+
exposed_tags: FxHashSet<SbTag>,
8383
/// Whether this memory has been modified since the last time the tag GC ran
8484
modified_since_last_gc: bool,
8585
}
@@ -513,6 +513,14 @@ impl Stacks {
513513
}
514514
}
515515

516+
impl VisitProvenance for Stacks {
517+
fn visit_provenance(&self, visit: &mut impl FnMut(SbTag)) {
518+
for tag in self.exposed_tags.iter().copied() {
519+
visit(tag);
520+
}
521+
}
522+
}
523+
516524
/// Map per-stack operations to higher-level per-location-range operations.
517525
impl<'tcx> Stacks {
518526
/// Creates a new stack with an initial tag. For diagnostic purposes, we also need to know

src/tools/miri/src/tag_gc.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ pub trait VisitMachineValues {
66
fn visit_machine_values(&self, visit: &mut impl FnMut(&Operand<Provenance>));
77
}
88

9+
pub trait VisitProvenance {
10+
fn visit_provenance(&self, visit: &mut impl FnMut(SbTag));
11+
}
12+
913
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
1014
pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriInterpCxExt<'mir, 'tcx> {
1115
/// Generic GC helper to visit everything that can store a value. The `acc` offers some chance to
@@ -46,6 +50,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriInterpCxExt<'mir, 'tcx> {
4650
}
4751
};
4852

53+
let visit_provenance = |tags: &mut FxHashSet<SbTag>, tag: SbTag| {
54+
tags.insert(tag);
55+
};
56+
4957
this.visit_all_machine_values(
5058
&mut tags,
5159
|tags, op| {
@@ -71,21 +79,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriInterpCxExt<'mir, 'tcx> {
7179
tags.insert(*sb);
7280
}
7381
}
74-
let stacks = alloc
75-
.extra
76-
.stacked_borrows
77-
.as_ref()
78-
.expect("we should not even enter the GC if Stacked Borrows is disabled");
79-
tags.extend(&stacks.borrow().exposed_tags);
82+
83+
let stacks =
84+
alloc.extra.stacked_borrows.as_ref().expect(
85+
"we should not even enter the tag GC if Stacked Borrows is disabled",
86+
);
87+
stacks.borrow().visit_provenance(&mut |tag| visit_provenance(tags, tag));
8088

8189
if let Some(store_buffers) = alloc.extra.weak_memory.as_ref() {
82-
store_buffers.iter(|val| {
83-
if let Scalar::Ptr(ptr, _) = val {
84-
if let Provenance::Concrete { sb, .. } = ptr.provenance {
85-
tags.insert(sb);
86-
}
87-
}
88-
});
90+
store_buffers.visit_provenance(&mut |tag| visit_provenance(tags, tag));
8991
}
9092
},
9193
);

0 commit comments

Comments
 (0)