Skip to content

Commit 595490e

Browse files
committed
slight simplifications for borrow tracking
1 parent 90118a1 commit 595490e

File tree

3 files changed

+36
-58
lines changed

3 files changed

+36
-58
lines changed

src/tools/miri/src/borrow_tracker/mod.rs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ impl fmt::Debug for BorTag {
5555
}
5656
}
5757

58-
/// Per-frame data for borrow tracking
58+
/// Per-call-stack-frame data for borrow tracking
5959
#[derive(Debug)]
60-
pub struct FrameExtra {
60+
pub struct FrameState {
6161
/// The ID of the call this frame corresponds to.
6262
pub call_id: CallId,
6363

@@ -72,7 +72,7 @@ pub struct FrameExtra {
7272
pub protected_tags: SmallVec<[BorTag; 2]>,
7373
}
7474

75-
impl VisitTags for FrameExtra {
75+
impl VisitTags for FrameState {
7676
fn visit_tags(&self, _visit: &mut dyn FnMut(BorTag)) {
7777
// `protected_tags` are fine to GC.
7878
}
@@ -190,14 +190,14 @@ impl GlobalStateInner {
190190
id
191191
}
192192

193-
pub fn new_frame(&mut self, machine: &MiriMachine<'_, '_>) -> FrameExtra {
193+
pub fn new_frame(&mut self, machine: &MiriMachine<'_, '_>) -> FrameState {
194194
let call_id = self.next_call_id;
195195
trace!("new_frame: Assigning call ID {}", call_id);
196196
if self.tracked_call_ids.contains(&call_id) {
197197
machine.emit_diagnostic(NonHaltingDiagnostic::CreatedCallId(call_id));
198198
}
199199
self.next_call_id = NonZeroU64::new(call_id.get() + 1).unwrap();
200-
FrameExtra { call_id, protected_tags: SmallVec::new() }
200+
FrameState { call_id, protected_tags: SmallVec::new() }
201201
}
202202

203203
pub fn end_call(&mut self, frame: &machine::FrameData<'_>) {
@@ -253,10 +253,10 @@ impl GlobalStateInner {
253253
alloc_size: Size,
254254
kind: MemoryKind<machine::MiriMemoryKind>,
255255
machine: &MiriMachine<'_, '_>,
256-
) -> AllocExtra {
256+
) -> AllocState {
257257
match self.borrow_tracker_method {
258258
BorrowTrackerMethod::StackedBorrows =>
259-
AllocExtra::StackedBorrows(Box::new(RefCell::new(Stacks::new_allocation(
259+
AllocState::StackedBorrows(Box::new(RefCell::new(Stacks::new_allocation(
260260
id, alloc_size, self, kind, machine,
261261
)))),
262262
}
@@ -292,24 +292,30 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
292292

293293
/// Extra per-allocation data for borrow tracking
294294
#[derive(Debug, Clone)]
295-
pub enum AllocExtra {
295+
pub enum AllocState {
296296
/// Data corresponding to Stacked Borrows
297-
StackedBorrows(Box<RefCell<stacked_borrows::AllocExtra>>),
297+
StackedBorrows(Box<RefCell<stacked_borrows::AllocState>>),
298298
}
299299

300-
impl AllocExtra {
301-
pub fn assert_sb(&self) -> &RefCell<stacked_borrows::AllocExtra> {
302-
match self {
303-
AllocExtra::StackedBorrows(ref sb) => sb,
300+
impl machine::AllocExtra {
301+
#[track_caller]
302+
pub fn borrow_tracker_sb(&self) -> &RefCell<stacked_borrows::AllocState> {
303+
match self.borrow_tracker {
304+
Some(AllocState::StackedBorrows(ref sb)) => sb,
305+
_ => panic!("expected Stacked Borrows borrow tracking, got something else"),
304306
}
305307
}
306308

307-
pub fn assert_sb_mut(&mut self) -> &mut RefCell<stacked_borrows::AllocExtra> {
308-
match self {
309-
AllocExtra::StackedBorrows(ref mut sb) => sb,
309+
#[track_caller]
310+
pub fn borrow_tracker_sb_mut(&mut self) -> &mut RefCell<stacked_borrows::AllocState> {
311+
match self.borrow_tracker {
312+
Some(AllocState::StackedBorrows(ref mut sb)) => sb,
313+
_ => panic!("expected Stacked Borrows borrow tracking, got something else"),
310314
}
311315
}
316+
}
312317

318+
impl AllocState {
313319
pub fn before_memory_read<'tcx>(
314320
&self,
315321
alloc_id: AllocId,
@@ -318,7 +324,7 @@ impl AllocExtra {
318324
machine: &MiriMachine<'_, 'tcx>,
319325
) -> InterpResult<'tcx> {
320326
match self {
321-
AllocExtra::StackedBorrows(sb) =>
327+
AllocState::StackedBorrows(sb) =>
322328
sb.borrow_mut().before_memory_read(alloc_id, prov_extra, range, machine),
323329
}
324330
}
@@ -331,7 +337,7 @@ impl AllocExtra {
331337
machine: &mut MiriMachine<'_, 'tcx>,
332338
) -> InterpResult<'tcx> {
333339
match self {
334-
AllocExtra::StackedBorrows(sb) =>
340+
AllocState::StackedBorrows(sb) =>
335341
sb.get_mut().before_memory_write(alloc_id, prov_extra, range, machine),
336342
}
337343
}
@@ -344,22 +350,22 @@ impl AllocExtra {
344350
machine: &mut MiriMachine<'_, 'tcx>,
345351
) -> InterpResult<'tcx> {
346352
match self {
347-
AllocExtra::StackedBorrows(sb) =>
353+
AllocState::StackedBorrows(sb) =>
348354
sb.get_mut().before_memory_deallocation(alloc_id, prov_extra, range, machine),
349355
}
350356
}
351357

352358
pub fn remove_unreachable_tags(&self, tags: &FxHashSet<BorTag>) {
353359
match self {
354-
AllocExtra::StackedBorrows(sb) => sb.borrow_mut().remove_unreachable_tags(tags),
360+
AllocState::StackedBorrows(sb) => sb.borrow_mut().remove_unreachable_tags(tags),
355361
}
356362
}
357363
}
358364

359-
impl VisitTags for AllocExtra {
365+
impl VisitTags for AllocState {
360366
fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
361367
match self {
362-
AllocExtra::StackedBorrows(sb) => sb.visit_tags(visit),
368+
AllocState::StackedBorrows(sb) => sb.visit_tags(visit),
363369
}
364370
}
365371
}

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

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod stack;
2525
pub use stack::Stack;
2626
pub mod diagnostics;
2727

28-
pub type AllocExtra = Stacks;
28+
pub type AllocState = Stacks;
2929

3030
/// Extra per-allocation state.
3131
#[derive(Clone, Debug)]
@@ -500,10 +500,6 @@ impl Stacks {
500500
})?;
501501
Ok(())
502502
}
503-
504-
fn expose_tag(&mut self, tag: BorTag) {
505-
self.exposed_tags.insert(tag);
506-
}
507503
}
508504

509505
/// Retagging/reborrowing. There is some policy in here, such as which permissions
@@ -567,10 +563,7 @@ trait EvalContextPrivExt<'mir: 'ecx, 'tcx: 'mir, 'ecx>: crate::MiriInterpCxExt<'
567563
// uncovers a non-supported `extern static`.
568564
let extra = this.get_alloc_extra(alloc_id)?;
569565
let mut stacked_borrows = extra
570-
.borrow_tracker
571-
.as_ref()
572-
.expect("We should have borrow tracking data")
573-
.assert_sb()
566+
.borrow_tracker_sb()
574567
.borrow_mut();
575568
// Note that we create a *second* `DiagnosticCxBuilder` below for the actual retag.
576569
// FIXME: can this be done cleaner?
@@ -681,12 +674,7 @@ trait EvalContextPrivExt<'mir: 'ecx, 'tcx: 'mir, 'ecx>: crate::MiriInterpCxExt<'
681674
// We have to use shared references to alloc/memory_extra here since
682675
// `visit_freeze_sensitive` needs to access the global state.
683676
let alloc_extra = this.get_alloc_extra(alloc_id)?;
684-
let mut stacked_borrows = alloc_extra
685-
.borrow_tracker
686-
.as_ref()
687-
.expect("We should have borrow tracking data")
688-
.assert_sb()
689-
.borrow_mut();
677+
let mut stacked_borrows = alloc_extra.borrow_tracker_sb().borrow_mut();
690678
this.visit_freeze_sensitive(place, size, |mut range, frozen| {
691679
// Adjust range.
692680
range.start += base_offset;
@@ -736,12 +724,7 @@ trait EvalContextPrivExt<'mir: 'ecx, 'tcx: 'mir, 'ecx>: crate::MiriInterpCxExt<'
736724
// Note that this asserts that the allocation is mutable -- but since we are creating a
737725
// mutable pointer, that seems reasonable.
738726
let (alloc_extra, machine) = this.get_alloc_extra_mut(alloc_id)?;
739-
let stacked_borrows = alloc_extra
740-
.borrow_tracker
741-
.as_mut()
742-
.expect("We should have borrow tracking data")
743-
.assert_sb_mut()
744-
.get_mut();
727+
let stacked_borrows = alloc_extra.borrow_tracker_sb_mut().get_mut();
745728
let item = Item::new(new_tag, perm, protect.is_some());
746729
let range = alloc_range(base_offset, size);
747730
let global = machine.borrow_tracker.as_ref().unwrap().borrow();
@@ -993,13 +976,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
993976
// uncovers a non-supported `extern static`.
994977
let alloc_extra = this.get_alloc_extra(alloc_id)?;
995978
trace!("Stacked Borrows tag {tag:?} exposed in {alloc_id:?}");
996-
alloc_extra
997-
.borrow_tracker
998-
.as_ref()
999-
.expect("We should have borrow tracking data")
1000-
.assert_sb()
1001-
.borrow_mut()
1002-
.expose_tag(tag);
979+
alloc_extra.borrow_tracker_sb().borrow_mut().exposed_tags.insert(tag);
1003980
}
1004981
AllocKind::Function | AllocKind::VTable | AllocKind::Dead => {
1005982
// No stacked borrows on these allocations.
@@ -1011,12 +988,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
1011988
fn print_stacks(&mut self, alloc_id: AllocId) -> InterpResult<'tcx> {
1012989
let this = self.eval_context_mut();
1013990
let alloc_extra = this.get_alloc_extra(alloc_id)?;
1014-
let stacks = alloc_extra
1015-
.borrow_tracker
1016-
.as_ref()
1017-
.expect("We should have borrow tracking data")
1018-
.assert_sb()
1019-
.borrow();
991+
let stacks = alloc_extra.borrow_tracker_sb().borrow();
1020992
for (range, stack) in stacks.stacks.iter_all() {
1021993
print!("{range:?}: [");
1022994
if let Some(bottom) = stack.unknown_bottom() {

src/tools/miri/src/machine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub const STACK_SIZE: u64 = 16 * PAGE_SIZE; // whatever
3939
/// Extra data stored with each stack frame
4040
pub struct FrameData<'tcx> {
4141
/// Extra data for Stacked Borrows.
42-
pub borrow_tracker: Option<borrow_tracker::FrameExtra>,
42+
pub borrow_tracker: Option<borrow_tracker::FrameState>,
4343

4444
/// If this is Some(), then this is a special "catch unwind" frame (the frame of `try_fn`
4545
/// called by `try`). When this frame is popped during unwinding a panic,
@@ -255,7 +255,7 @@ impl ProvenanceExtra {
255255
#[derive(Debug, Clone)]
256256
pub struct AllocExtra {
257257
/// Global state of the borrow tracker, if enabled.
258-
pub borrow_tracker: Option<borrow_tracker::AllocExtra>,
258+
pub borrow_tracker: Option<borrow_tracker::AllocState>,
259259
/// Data race detection via the use of a vector-clock,
260260
/// this is only added if it is enabled.
261261
pub data_race: Option<data_race::AllocExtra>,

0 commit comments

Comments
 (0)