Skip to content

Commit 6ff09af

Browse files
committed
Auto merge of #3802 - RalfJung:no-more-call-id, r=RalfJung
Borrow tracking: remove the concept of a call ID Turns out this is not needed any more ever since we started tracking the `protected_tags` list in the per-frame state. Also thanks to `@JoJoDeveloping` for inspiring me to even consider this possibility. :)
2 parents dd600ef + 7db942b commit 6ff09af

26 files changed

+46
-102
lines changed

src/tools/miri/README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,6 @@ to Miri failing to detect cases of undefined behavior in a program.
414414
being allocated or freed. This helps in debugging memory leaks and
415415
use after free bugs. Specifying this argument multiple times does not overwrite the previous
416416
values, instead it appends its values to the list. Listing an id multiple times has no effect.
417-
* `-Zmiri-track-call-id=<id1>,<id2>,...` shows a backtrace when the given call ids are
418-
assigned to a stack frame. This helps in debugging UB related to Stacked
419-
Borrows "protectors". Specifying this argument multiple times does not overwrite the previous
420-
values, instead it appends its values to the list. Listing an id multiple times has no effect.
421417
* `-Zmiri-track-pointer-tag=<tag1>,<tag2>,...` shows a backtrace when a given pointer tag
422418
is created and when (if ever) it is popped from a borrow stack (which is where the tag becomes invalid
423419
and any future use of it will error). This helps you in finding out why UB is

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -581,17 +581,6 @@ fn main() {
581581
show_error!("-Zmiri-track-pointer-tag requires nonzero arguments");
582582
}
583583
}
584-
} else if let Some(param) = arg.strip_prefix("-Zmiri-track-call-id=") {
585-
let ids: Vec<u64> = parse_comma_list(param).unwrap_or_else(|err| {
586-
show_error!("-Zmiri-track-call-id requires a comma separated list of valid `u64` arguments: {err}")
587-
});
588-
for id in ids.into_iter().map(miri::CallId::new) {
589-
if let Some(id) = id {
590-
miri_config.tracked_call_ids.insert(id);
591-
} else {
592-
show_error!("-Zmiri-track-call-id requires a nonzero argument");
593-
}
594-
}
595584
} else if let Some(param) = arg.strip_prefix("-Zmiri-track-alloc-id=") {
596585
let ids = parse_comma_list::<NonZero<u64>>(param).unwrap_or_else(|err| {
597586
show_error!("-Zmiri-track-alloc-id requires a comma separated list of valid non-zero `u64` arguments: {err}")

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use crate::*;
1212
pub mod stacked_borrows;
1313
pub mod tree_borrows;
1414

15-
pub type CallId = NonZero<u64>;
16-
1715
/// Tracking pointer provenance
1816
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
1917
pub struct BorTag(NonZero<u64>);
@@ -57,9 +55,6 @@ impl fmt::Debug for BorTag {
5755
/// Per-call-stack-frame data for borrow tracking
5856
#[derive(Debug)]
5957
pub struct FrameState {
60-
/// The ID of the call this frame corresponds to.
61-
call_id: CallId,
62-
6358
/// If this frame is protecting any tags, they are listed here. We use this list to do
6459
/// incremental updates of the global list of protected tags stored in the
6560
/// `stacked_borrows::GlobalState` upon function return, and if we attempt to pop a protected
@@ -93,18 +88,13 @@ pub struct GlobalStateInner {
9388
/// The root tag is the one used for the initial pointer.
9489
/// We need this in a separate table to handle cyclic statics.
9590
root_ptr_tags: FxHashMap<AllocId, BorTag>,
96-
/// Next unused call ID (for protectors).
97-
next_call_id: CallId,
9891
/// All currently protected tags.
99-
/// An item is protected if its tag is in this set, *and* it has the "protected" bit set.
10092
/// We add tags to this when they are created with a protector in `reborrow`, and
10193
/// we remove tags from this when the call which is protecting them returns, in
10294
/// `GlobalStateInner::end_call`. See `Stack::item_invalidated` for more details.
10395
protected_tags: FxHashMap<BorTag, ProtectorKind>,
10496
/// The pointer ids to trace
10597
tracked_pointer_tags: FxHashSet<BorTag>,
106-
/// The call ids to trace
107-
tracked_call_ids: FxHashSet<CallId>,
10898
/// Whether to recurse into datatypes when searching for pointers to retag.
10999
retag_fields: RetagFields,
110100
/// Whether `core::ptr::Unique` gets special (`Box`-like) handling.
@@ -168,18 +158,15 @@ impl GlobalStateInner {
168158
pub fn new(
169159
borrow_tracker_method: BorrowTrackerMethod,
170160
tracked_pointer_tags: FxHashSet<BorTag>,
171-
tracked_call_ids: FxHashSet<CallId>,
172161
retag_fields: RetagFields,
173162
unique_is_unique: bool,
174163
) -> Self {
175164
GlobalStateInner {
176165
borrow_tracker_method,
177166
next_ptr_tag: BorTag::one(),
178167
root_ptr_tags: FxHashMap::default(),
179-
next_call_id: NonZero::new(1).unwrap(),
180168
protected_tags: FxHashMap::default(),
181169
tracked_pointer_tags,
182-
tracked_call_ids,
183170
retag_fields,
184171
unique_is_unique,
185172
}
@@ -192,14 +179,8 @@ impl GlobalStateInner {
192179
id
193180
}
194181

195-
pub fn new_frame(&mut self, machine: &MiriMachine<'_>) -> FrameState {
196-
let call_id = self.next_call_id;
197-
trace!("new_frame: Assigning call ID {}", call_id);
198-
if self.tracked_call_ids.contains(&call_id) {
199-
machine.emit_diagnostic(NonHaltingDiagnostic::CreatedCallId(call_id));
200-
}
201-
self.next_call_id = NonZero::new(call_id.get() + 1).unwrap();
202-
FrameState { call_id, protected_tags: SmallVec::new() }
182+
pub fn new_frame(&mut self) -> FrameState {
183+
FrameState { protected_tags: SmallVec::new() }
203184
}
204185

205186
fn end_call(&mut self, frame: &machine::FrameExtra<'_>) {
@@ -252,7 +233,6 @@ impl BorrowTrackerMethod {
252233
RefCell::new(GlobalStateInner::new(
253234
self,
254235
config.tracked_pointer_tags.clone(),
255-
config.tracked_call_ids.clone(),
256236
config.retag_fields,
257237
config.unique_is_unique,
258238
))

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

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -429,30 +429,14 @@ impl<'history, 'ecx, 'tcx> DiagnosticCx<'history, 'ecx, 'tcx> {
429429
ProtectorKind::WeakProtector => "weakly protected",
430430
ProtectorKind::StrongProtector => "strongly protected",
431431
};
432-
let item_tag = item.tag();
433-
let call_id = self
434-
.machine
435-
.threads
436-
.all_stacks()
437-
.flat_map(|(_id, stack)| stack)
438-
.map(|frame| {
439-
frame.extra.borrow_tracker.as_ref().expect("we should have borrow tracking data")
440-
})
441-
.find(|frame| frame.protected_tags.iter().any(|(_, tag)| tag == &item_tag))
442-
.map(|frame| frame.call_id)
443-
.unwrap(); // FIXME: Surely we should find something, but a panic seems wrong here?
444432
match self.operation {
445433
Operation::Dealloc(_) =>
446-
err_sb_ub(
447-
format!("deallocating while item {item:?} is {protected} by call {call_id:?}",),
448-
vec![],
449-
None,
450-
),
434+
err_sb_ub(format!("deallocating while item {item:?} is {protected}",), vec![], None),
451435
Operation::Retag(RetagOp { orig_tag: tag, .. })
452436
| Operation::Access(AccessOp { tag, .. }) =>
453437
err_sb_ub(
454438
format!(
455-
"not granting access to tag {tag:?} because that would remove {item:?} which is {protected} because it is an argument of call {call_id:?}",
439+
"not granting access to tag {tag:?} because that would remove {item:?} which is {protected}",
456440
),
457441
vec![],
458442
tag.and_then(|tag| self.get_logs_relevant_to(tag, Some(item.tag()))),

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ use crate::borrow_tracker::BorTag;
77
pub struct Item(u64);
88

99
// An Item contains 3 bitfields:
10-
// * Bits 0-61 store a BorTag
11-
// * Bits 61-63 store a Permission
12-
// * Bit 64 stores a flag which indicates if we have a protector
10+
// * Bits 0-61 store a BorTag.
11+
// * Bits 61-63 store a Permission.
12+
// * Bit 64 stores a flag which indicates if we might have a protector.
13+
// This is purely an optimization: if the bit is set, the tag *might* be
14+
// in `protected_tags`, but if the bit is not set then the tag is definitely
15+
// not in `protected_tags`.
1316
const TAG_MASK: u64 = u64::MAX >> 3;
1417
const PERM_MASK: u64 = 0x3 << 61;
1518
const PROTECTED_MASK: u64 = 0x1 << 63;

src/tools/miri/src/diagnostics.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ pub enum NonHaltingDiagnostic {
116116
CreatedPointerTag(NonZero<u64>, Option<String>, Option<(AllocId, AllocRange, ProvenanceExtra)>),
117117
/// This `Item` was popped from the borrow stack. The string explains the reason.
118118
PoppedPointerTag(Item, String),
119-
CreatedCallId(CallId),
120119
CreatedAlloc(AllocId, Size, Align, MemoryKind),
121120
FreedAlloc(AllocId),
122121
AccessedAlloc(AllocId, AccessKind),
@@ -607,7 +606,6 @@ impl<'tcx> MiriMachine<'tcx> {
607606
("reborrow of reference to `extern type`".to_string(), DiagLevel::Warning),
608607
CreatedPointerTag(..)
609608
| PoppedPointerTag(..)
610-
| CreatedCallId(..)
611609
| CreatedAlloc(..)
612610
| AccessedAlloc(..)
613611
| FreedAlloc(..)
@@ -625,7 +623,6 @@ impl<'tcx> MiriMachine<'tcx> {
625623
"created tag {tag:?} with {perm} at {alloc_id:?}{range:?} derived from {orig_tag:?}"
626624
),
627625
PoppedPointerTag(item, cause) => format!("popped tracked tag for item {item:?}{cause}"),
628-
CreatedCallId(id) => format!("function call with id {id}"),
629626
CreatedAlloc(AllocId(id), size, align, kind) =>
630627
format!(
631628
"created {kind} allocation of {size} bytes (alignment {align} bytes) with id {id}",

src/tools/miri/src/eval.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ pub struct MiriConfig {
118118
pub seed: Option<u64>,
119119
/// The stacked borrows pointer ids to report about
120120
pub tracked_pointer_tags: FxHashSet<BorTag>,
121-
/// The stacked borrows call IDs to report about
122-
pub tracked_call_ids: FxHashSet<CallId>,
123121
/// The allocation ids to report about.
124122
pub tracked_alloc_ids: FxHashSet<AllocId>,
125123
/// For the tracked alloc ids, also report read/write accesses.
@@ -183,7 +181,6 @@ impl Default for MiriConfig {
183181
args: vec![],
184182
seed: None,
185183
tracked_pointer_tags: FxHashSet::default(),
186-
tracked_call_ids: FxHashSet::default(),
187184
tracked_alloc_ids: FxHashSet::default(),
188185
track_alloc_accesses: false,
189186
data_race_detector: true,

src/tools/miri/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ pub use crate::borrow_tracker::stacked_borrows::{
123123
EvalContextExt as _, Item, Permission, Stack, Stacks,
124124
};
125125
pub use crate::borrow_tracker::tree_borrows::{EvalContextExt as _, Tree};
126-
pub use crate::borrow_tracker::{
127-
BorTag, BorrowTrackerMethod, CallId, EvalContextExt as _, RetagFields,
128-
};
126+
pub use crate::borrow_tracker::{BorTag, BorrowTrackerMethod, EvalContextExt as _, RetagFields};
129127
pub use crate::clock::{Clock, Instant};
130128
pub use crate::concurrency::{
131129
cpu_affinity::MAX_CPUS,

src/tools/miri/src/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1368,7 +1368,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
13681368
let borrow_tracker = ecx.machine.borrow_tracker.as_ref();
13691369

13701370
let extra = FrameExtra {
1371-
borrow_tracker: borrow_tracker.map(|bt| bt.borrow_mut().new_frame(&ecx.machine)),
1371+
borrow_tracker: borrow_tracker.map(|bt| bt.borrow_mut().new_frame()),
13721372
catch_unwind: None,
13731373
timing,
13741374
is_user_relevant: ecx.machine.is_user_relevant(&frame),

src/tools/miri/tests/fail/both_borrows/aliasing_mut1.stack.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected because it is an argument of call ID
1+
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected
22
--> $DIR/aliasing_mut1.rs:LL:CC
33
|
44
LL | pub fn safe(x: &mut i32, y: &mut i32) {
5-
| ^ not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected because it is an argument of call ID
5+
| ^ not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected
66
|
77
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
88
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information

src/tools/miri/tests/fail/both_borrows/aliasing_mut2.stack.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [SharedReadOnly for <TAG>] which is strongly protected because it is an argument of call ID
1+
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [SharedReadOnly for <TAG>] which is strongly protected
22
--> $DIR/aliasing_mut2.rs:LL:CC
33
|
44
LL | pub fn safe(x: &i32, y: &mut i32) {
5-
| ^ not granting access to tag <TAG> because that would remove [SharedReadOnly for <TAG>] which is strongly protected because it is an argument of call ID
5+
| ^ not granting access to tag <TAG> because that would remove [SharedReadOnly for <TAG>] which is strongly protected
66
|
77
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
88
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information

src/tools/miri/tests/fail/both_borrows/aliasing_mut4.stack.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [SharedReadOnly for <TAG>] which is strongly protected because it is an argument of call ID
1+
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [SharedReadOnly for <TAG>] which is strongly protected
22
--> $DIR/aliasing_mut4.rs:LL:CC
33
|
44
LL | pub fn safe(x: &i32, y: &mut Cell<i32>) {
5-
| ^ not granting access to tag <TAG> because that would remove [SharedReadOnly for <TAG>] which is strongly protected because it is an argument of call ID
5+
| ^ not granting access to tag <TAG> because that would remove [SharedReadOnly for <TAG>] which is strongly protected
66
|
77
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
88
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information

src/tools/miri/tests/fail/both_borrows/box_noalias_violation.stack.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is weakly protected because it is an argument of call ID
1+
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is weakly protected
22
--> $DIR/box_noalias_violation.rs:LL:CC
33
|
44
LL | *y
5-
| ^^ not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is weakly protected because it is an argument of call ID
5+
| ^^ not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is weakly protected
66
|
77
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
88
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information

src/tools/miri/tests/fail/both_borrows/illegal_write6.stack.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected because it is an argument of call ID
1+
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected
22
--> $DIR/illegal_write6.rs:LL:CC
33
|
44
LL | unsafe { *y = 2 };
5-
| ^^^^^^ not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected because it is an argument of call ID
5+
| ^^^^^^ not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected
66
|
77
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
88
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information

src/tools/miri/tests/fail/both_borrows/invalidate_against_protector2.stack.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [SharedReadOnly for <TAG>] which is strongly protected because it is an argument of call ID
1+
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [SharedReadOnly for <TAG>] which is strongly protected
22
--> $DIR/invalidate_against_protector2.rs:LL:CC
33
|
44
LL | unsafe { *x = 0 };
5-
| ^^^^^^ not granting access to tag <TAG> because that would remove [SharedReadOnly for <TAG>] which is strongly protected because it is an argument of call ID
5+
| ^^^^^^ not granting access to tag <TAG> because that would remove [SharedReadOnly for <TAG>] which is strongly protected
66
|
77
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
88
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information

src/tools/miri/tests/fail/both_borrows/invalidate_against_protector3.stack.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [SharedReadOnly for <TAG>] which is strongly protected because it is an argument of call ID
1+
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [SharedReadOnly for <TAG>] which is strongly protected
22
--> $DIR/invalidate_against_protector3.rs:LL:CC
33
|
44
LL | unsafe { *x = 0 };
5-
| ^^^^^^ not granting access to tag <TAG> because that would remove [SharedReadOnly for <TAG>] which is strongly protected because it is an argument of call ID
5+
| ^^^^^^ not granting access to tag <TAG> because that would remove [SharedReadOnly for <TAG>] which is strongly protected
66
|
77
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
88
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information

src/tools/miri/tests/fail/both_borrows/newtype_pair_retagging.stack.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected because it is an argument of call ID
1+
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected
22
--> RUSTLIB/alloc/src/boxed.rs:LL:CC
33
|
44
LL | Box(unsafe { Unique::new_unchecked(raw) }, alloc)
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected because it is an argument of call ID
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected
66
|
77
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
88
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information

src/tools/miri/tests/fail/both_borrows/newtype_retagging.stack.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected because it is an argument of call ID
1+
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected
22
--> RUSTLIB/alloc/src/boxed.rs:LL:CC
33
|
44
LL | Box(unsafe { Unique::new_unchecked(raw) }, alloc)
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected because it is an argument of call ID
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected
66
|
77
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
88
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information

0 commit comments

Comments
 (0)