Skip to content

Commit 8bb3d9e

Browse files
committed
other renames, introduction of BorrowTrackerMethod and AllocExtra
1 parent 3a01493 commit 8bb3d9e

File tree

5 files changed

+56
-76
lines changed

5 files changed

+56
-76
lines changed

src/tools/miri/src/bin/miri.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ fn main() {
317317
} else if arg == "-Zmiri-disable-validation" {
318318
miri_config.validate = false;
319319
} else if arg == "-Zmiri-disable-stacked-borrows" {
320-
miri_config.stacked_borrows = false;
320+
miri_config.borrow_tracker = None;
321321
} else if arg == "-Zmiri-disable-data-race-detector" {
322322
miri_config.data_race_detector = false;
323323
miri_config.weak_memory_emulation = false;

src/tools/miri/src/eval.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub struct MiriConfig {
8787
/// Determine if validity checking is enabled.
8888
pub validate: bool,
8989
/// Determines if Stacked Borrows is enabled.
90-
pub stacked_borrows: bool,
90+
pub borrow_tracker: Option<BorrowTrackerMethod>,
9191
/// Controls alignment checking.
9292
pub check_alignment: AlignmentCheck,
9393
/// Controls function [ABI](Abi) checking.
@@ -149,7 +149,7 @@ impl Default for MiriConfig {
149149
MiriConfig {
150150
env: vec![],
151151
validate: true,
152-
stacked_borrows: true,
152+
borrow_tracker: Some(BorrowTrackerMethod::StackedBorrows),
153153
check_alignment: AlignmentCheck::Int,
154154
check_abi: true,
155155
isolated_op: IsolatedOp::Reject(RejectOpWith::Abort),

src/tools/miri/src/intptrcast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ impl<'mir, 'tcx> GlobalStateInner {
112112
if global_state.provenance_mode != ProvenanceMode::Strict {
113113
trace!("Exposing allocation id {alloc_id:?}");
114114
global_state.exposed.insert(alloc_id);
115-
if ecx.machine.stacked_borrows.is_some() {
116-
ecx.expose_tag(alloc_id, sb)?;
115+
if ecx.machine.borrow_tracker.is_some() {
116+
ecx.expose_tag(alloc_id, tag)?;
117117
}
118118
}
119119
Ok(())

src/tools/miri/src/machine.rs

Lines changed: 46 additions & 62 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 stacked_borrows: Option<stacked_borrows::FrameExtra>,
42+
pub borrow_tracker: Option<borrow_tracker::FrameExtra>,
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,
@@ -61,20 +61,20 @@ pub struct FrameData<'tcx> {
6161
impl<'tcx> std::fmt::Debug for FrameData<'tcx> {
6262
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
6363
// Omitting `timing`, it does not support `Debug`.
64-
let FrameData { stacked_borrows, catch_unwind, timing: _, is_user_relevant: _ } = self;
64+
let FrameData { borrow_tracker, catch_unwind, timing: _, is_user_relevant: _ } = self;
6565
f.debug_struct("FrameData")
66-
.field("stacked_borrows", stacked_borrows)
66+
.field("borrow_tracker", borrow_tracker)
6767
.field("catch_unwind", catch_unwind)
6868
.finish()
6969
}
7070
}
7171

7272
impl VisitTags for FrameData<'_> {
73-
fn visit_tags(&self, visit: &mut dyn FnMut(SbTag)) {
74-
let FrameData { catch_unwind, stacked_borrows, timing: _, is_user_relevant: _ } = self;
73+
fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
74+
let FrameData { catch_unwind, borrow_tracker, timing: _, is_user_relevant: _ } = self;
7575

7676
catch_unwind.visit_tags(visit);
77-
stacked_borrows.visit_tags(visit);
77+
borrow_tracker.visit_tags(visit);
7878
}
7979
}
8080

@@ -254,8 +254,8 @@ impl ProvenanceExtra {
254254
/// Extra per-allocation data
255255
#[derive(Debug, Clone)]
256256
pub struct AllocExtra {
257-
/// Stacked Borrows state is only added if it is enabled.
258-
pub stacked_borrows: Option<stacked_borrows::AllocExtra>,
257+
/// Global state of the borrow tracker, if enabled.
258+
pub borrow_tracker: Option<borrow_tracker::AllocExtra>,
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>,
@@ -265,10 +265,10 @@ pub struct AllocExtra {
265265
}
266266

267267
impl VisitTags for AllocExtra {
268-
fn visit_tags(&self, visit: &mut dyn FnMut(SbTag)) {
269-
let AllocExtra { stacked_borrows, data_race, weak_memory } = self;
268+
fn visit_tags(&self, visit: &mut dyn FnMut(BorTag)) {
269+
let AllocExtra { borrow_tracker, data_race, weak_memory } = self;
270270

271-
stacked_borrows.visit_tags(visit);
271+
borrow_tracker.visit_tags(visit);
272272
data_race.visit_tags(visit);
273273
weak_memory.visit_tags(visit);
274274
}
@@ -350,8 +350,8 @@ pub struct MiriMachine<'mir, 'tcx> {
350350
// We carry a copy of the global `TyCtxt` for convenience, so methods taking just `&Evaluator` have `tcx` access.
351351
pub tcx: TyCtxt<'tcx>,
352352

353-
/// Stacked Borrows global data.
354-
pub stacked_borrows: Option<stacked_borrows::GlobalState>,
353+
/// Global data for borrow tracking.
354+
pub borrow_tracker: Option<borrow_tracker::GlobalState>,
355355

356356
/// Data race detector global data.
357357
pub data_race: Option<data_race::GlobalState>,
@@ -480,17 +480,11 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
480480
measureme::Profiler::new(out).expect("Couldn't create `measureme` profiler")
481481
});
482482
let rng = StdRng::seed_from_u64(config.seed.unwrap_or(0));
483-
let stacked_borrows = config.stacked_borrows.then(|| {
484-
RefCell::new(stacked_borrows::GlobalStateInner::new(
485-
config.tracked_pointer_tags.clone(),
486-
config.tracked_call_ids.clone(),
487-
config.retag_fields,
488-
))
489-
});
483+
let borrow_tracker = config.borrow_tracker.map(|bt| bt.instanciate_global_state(config));
490484
let data_race = config.data_race_detector.then(|| data_race::GlobalState::new(config));
491485
MiriMachine {
492486
tcx: layout_cx.tcx,
493-
stacked_borrows,
487+
borrow_tracker,
494488
data_race,
495489
intptrcast: RefCell::new(intptrcast::GlobalStateInner::new(config)),
496490
// `env_vars` depends on a full interpreter so we cannot properly initialize it yet.
@@ -668,7 +662,7 @@ impl VisitTags for MiriMachine<'_, '_> {
668662
cmd_line,
669663
extern_statics,
670664
dir_handler,
671-
stacked_borrows,
665+
borrow_tracker,
672666
data_race,
673667
intptrcast,
674668
file_handler,
@@ -706,7 +700,7 @@ impl VisitTags for MiriMachine<'_, '_> {
706700
dir_handler.visit_tags(visit);
707701
file_handler.visit_tags(visit);
708702
data_race.visit_tags(visit);
709-
stacked_borrows.visit_tags(visit);
703+
borrow_tracker.visit_tags(visit);
710704
intptrcast.visit_tags(visit);
711705
main_fn_ret_place.visit_tags(visit);
712706
argc.visit_tags(visit);
@@ -907,15 +901,12 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
907901
}
908902

909903
let alloc = alloc.into_owned();
910-
let stacks = ecx.machine.stacked_borrows.as_ref().map(|stacked_borrows| {
911-
stacked_borrows::Stacks::new_allocation(
912-
id,
913-
alloc.size(),
914-
stacked_borrows,
915-
kind,
916-
&ecx.machine,
917-
)
918-
});
904+
let borrow_tracker = ecx
905+
.machine
906+
.borrow_tracker
907+
.as_ref()
908+
.map(|bt| bt.borrow_mut().new_allocation(id, alloc.size(), kind, &ecx.machine));
909+
919910
let race_alloc = ecx.machine.data_race.as_ref().map(|data_race| {
920911
data_race::AllocExtra::new_allocation(
921912
data_race,
@@ -927,11 +918,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
927918
let buffer_alloc = ecx.machine.weak_memory.then(weak_memory::AllocExtra::new_allocation);
928919
let alloc: Allocation<Provenance, Self::AllocExtra> = alloc.adjust_from_tcx(
929920
&ecx.tcx,
930-
AllocExtra {
931-
stacked_borrows: stacks.map(RefCell::new),
932-
data_race: race_alloc,
933-
weak_memory: buffer_alloc,
934-
},
921+
AllocExtra { borrow_tracker, data_race: race_alloc, weak_memory: buffer_alloc },
935922
|ptr| ecx.global_base_pointer(ptr),
936923
)?;
937924
Ok(Cow::Owned(alloc))
@@ -955,8 +942,8 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
955942
}
956943
}
957944
let absolute_addr = intptrcast::GlobalStateInner::rel_ptr_to_addr(ecx, ptr);
958-
let sb_tag = if let Some(stacked_borrows) = &ecx.machine.stacked_borrows {
959-
stacked_borrows.borrow_mut().base_ptr_tag(ptr.provenance, &ecx.machine)
945+
let tag = if let Some(borrow_tracker) = &ecx.machine.borrow_tracker {
946+
borrow_tracker.borrow_mut().base_ptr_tag(ptr.provenance, &ecx.machine)
960947
} else {
961948
// Value does not matter, SB is disabled
962949
BorTag::default()
@@ -1018,10 +1005,8 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
10181005
if let Some(data_race) = &alloc_extra.data_race {
10191006
data_race.read(alloc_id, range, machine)?;
10201007
}
1021-
if let Some(stacked_borrows) = &alloc_extra.stacked_borrows {
1022-
stacked_borrows
1023-
.borrow_mut()
1024-
.before_memory_read(alloc_id, prov_extra, range, machine)?;
1008+
if let Some(borrow_tracker) = &alloc_extra.borrow_tracker {
1009+
borrow_tracker.before_memory_read(alloc_id, prov_extra, range, machine)?;
10251010
}
10261011
if let Some(weak_memory) = &alloc_extra.weak_memory {
10271012
weak_memory.memory_accessed(range, machine.data_race.as_ref().unwrap());
@@ -1040,8 +1025,8 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
10401025
if let Some(data_race) = &mut alloc_extra.data_race {
10411026
data_race.write(alloc_id, range, machine)?;
10421027
}
1043-
if let Some(stacked_borrows) = &mut alloc_extra.stacked_borrows {
1044-
stacked_borrows.get_mut().before_memory_write(alloc_id, prov_extra, range, machine)?;
1028+
if let Some(borrow_tracker) = &mut alloc_extra.borrow_tracker {
1029+
borrow_tracker.before_memory_write(alloc_id, prov_extra, range, machine)?;
10451030
}
10461031
if let Some(weak_memory) = &alloc_extra.weak_memory {
10471032
weak_memory.memory_accessed(range, machine.data_race.as_ref().unwrap());
@@ -1063,16 +1048,10 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
10631048
if let Some(data_race) = &mut alloc_extra.data_race {
10641049
data_race.deallocate(alloc_id, range, machine)?;
10651050
}
1066-
if let Some(stacked_borrows) = &mut alloc_extra.stacked_borrows {
1067-
stacked_borrows.get_mut().before_memory_deallocation(
1068-
alloc_id,
1069-
prove_extra,
1070-
range,
1071-
machine,
1072-
)
1073-
} else {
1074-
Ok(())
1051+
if let Some(borrow_tracker) = &mut alloc_extra.borrow_tracker {
1052+
borrow_tracker.before_memory_deallocation(alloc_id, prove_extra, range, machine)?;
10751053
}
1054+
Ok(())
10761055
}
10771056

10781057
#[inline(always)]
@@ -1081,7 +1060,10 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
10811060
kind: mir::RetagKind,
10821061
place: &PlaceTy<'tcx, Provenance>,
10831062
) -> InterpResult<'tcx> {
1084-
if ecx.machine.stacked_borrows.is_some() { ecx.retag(kind, place) } else { Ok(()) }
1063+
if ecx.machine.borrow_tracker.is_some() {
1064+
ecx.retag(kind, place)?;
1065+
}
1066+
Ok(())
10851067
}
10861068

10871069
#[inline(always)]
@@ -1104,10 +1086,10 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
11041086
None
11051087
};
11061088

1107-
let stacked_borrows = ecx.machine.stacked_borrows.as_ref();
1089+
let borrow_tracker = ecx.machine.borrow_tracker.as_ref();
11081090

11091091
let extra = FrameData {
1110-
stacked_borrows: stacked_borrows.map(|sb| sb.borrow_mut().new_frame(&ecx.machine)),
1092+
borrow_tracker: borrow_tracker.map(|bt| bt.borrow_mut().new_frame(&ecx.machine)),
11111093
catch_unwind: None,
11121094
timing,
11131095
is_user_relevant: ecx.machine.is_user_relevant(&frame),
@@ -1140,7 +1122,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
11401122
}
11411123
}
11421124

1143-
// Search for SbTags to find all live pointers, then remove all other tags from borrow
1125+
// Search for BorTags to find all live pointers, then remove all other tags from borrow
11441126
// stacks.
11451127
// When debug assertions are enabled, run the GC as often as possible so that any cases
11461128
// where it mistakenly removes an important tag become visible.
@@ -1166,8 +1148,10 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
11661148
let stack_len = ecx.active_thread_stack().len();
11671149
ecx.active_thread_mut().set_top_user_relevant_frame(stack_len - 1);
11681150
}
1169-
1170-
if ecx.machine.stacked_borrows.is_some() { ecx.retag_return_place() } else { Ok(()) }
1151+
if ecx.machine.borrow_tracker.is_some() {
1152+
ecx.retag_return_place()?;
1153+
}
1154+
Ok(())
11711155
}
11721156

11731157
#[inline(always)]
@@ -1184,8 +1168,8 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
11841168
ecx.active_thread_mut().recompute_top_user_relevant_frame();
11851169
}
11861170
let timing = frame.extra.timing.take();
1187-
if let Some(stacked_borrows) = &ecx.machine.stacked_borrows {
1188-
stacked_borrows.borrow_mut().end_call(&frame.extra);
1171+
if let Some(borrow_tracker) = &ecx.machine.borrow_tracker {
1172+
borrow_tracker.borrow_mut().end_call(&frame.extra);
11891173
}
11901174
let res = ecx.handle_stack_pop_unwind(frame.extra, unwinding);
11911175
if let Some(profiler) = ecx.machine.profiler.as_ref() {

src/tools/miri/src/tag_gc.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriInterpCxExt<'mir, 'tcx> {
154154
fn garbage_collect_tags(&mut self) -> InterpResult<'tcx> {
155155
let this = self.eval_context_mut();
156156
// No reason to do anything at all if stacked borrows is off.
157-
if this.machine.stacked_borrows.is_none() {
157+
if this.machine.borrow_tracker.is_none() {
158158
return Ok(());
159159
}
160160

@@ -167,17 +167,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriInterpCxExt<'mir, 'tcx> {
167167
Ok(())
168168
}
169169

170-
fn remove_unreachable_tags(&mut self, tags: FxHashSet<SbTag>) {
170+
fn remove_unreachable_tags(&mut self, tags: FxHashSet<BorTag>) {
171171
let this = self.eval_context_mut();
172172
this.memory.alloc_map().iter(|it| {
173173
for (_id, (_kind, alloc)) in it {
174-
alloc
175-
.extra
176-
.stacked_borrows
177-
.as_ref()
178-
.unwrap()
179-
.borrow_mut()
180-
.remove_unreachable_tags(&tags);
174+
if let Some(bt) = &alloc.extra.borrow_tracker {
175+
bt.remove_unreachable_tags(&tags);
176+
}
181177
}
182178
});
183179
}

0 commit comments

Comments
 (0)