Skip to content

Commit 796bcf9

Browse files
committed
Auto merge of rust-lang#2582 - RalfJung:sb-tracking, r=RalfJung
more details in stacked borrows tag tracking
2 parents 17cb715 + d1676b5 commit 796bcf9

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/tools/miri/src/diagnostics.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ impl MachineStopType for TerminationInfo {}
6060

6161
/// Miri specific diagnostics
6262
pub enum NonHaltingDiagnostic {
63-
CreatedPointerTag(NonZeroU64, Option<(AllocId, AllocRange)>),
63+
/// (new_tag, new_kind, (alloc_id, base_offset, orig_tag))
64+
///
65+
/// new_kind is `None` for base tags.
66+
CreatedPointerTag(NonZeroU64, Option<String>, Option<(AllocId, AllocRange, ProvenanceExtra)>),
6467
/// This `Item` was popped from the borrow stack, either due to an access with the given tag or
6568
/// a deallocation when the second argument is `None`.
6669
PoppedPointerTag(Item, Option<(ProvenanceExtra, AccessKind)>),
@@ -376,7 +379,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
376379
MiriInterpCx::generate_stacktrace_from_stack(self.threads.active_thread_stack());
377380
let (stacktrace, _was_pruned) = prune_stacktrace(stacktrace, self);
378381

379-
let (title, diag_level) = match e {
382+
let (title, diag_level) = match &e {
380383
RejectedIsolatedOp(_) => ("operation rejected by isolation", DiagLevel::Warning),
381384
Int2Ptr { .. } => ("integer-to-pointer cast", DiagLevel::Warning),
382385
CreatedPointerTag(..)
@@ -388,10 +391,13 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
388391
| WeakMemoryOutdatedLoad => ("tracking was triggered", DiagLevel::Note),
389392
};
390393

391-
let msg = match e {
392-
CreatedPointerTag(tag, None) => format!("created tag {tag:?}"),
393-
CreatedPointerTag(tag, Some((alloc_id, range))) =>
394-
format!("created tag {tag:?} at {alloc_id:?}{range:?}"),
394+
let msg = match &e {
395+
CreatedPointerTag(tag, None, _) => format!("created base tag {tag:?}"),
396+
CreatedPointerTag(tag, Some(kind), None) => format!("created {tag:?} for {kind}"),
397+
CreatedPointerTag(tag, Some(kind), Some((alloc_id, range, orig_tag))) =>
398+
format!(
399+
"created tag {tag:?} for {kind} at {alloc_id:?}{range:?} derived from {orig_tag:?}"
400+
),
395401
PoppedPointerTag(item, tag) =>
396402
match tag {
397403
None => format!("popped tracked tag for item {item:?} due to deallocation",),
@@ -418,7 +424,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
418424
format!("weak memory emulation: outdated value returned from load"),
419425
};
420426

421-
let notes = match e {
427+
let notes = match &e {
422428
ProgressReport { block_count } => {
423429
// It is important that each progress report is slightly different, since
424430
// identical diagnostics are being deduplicated.
@@ -427,7 +433,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
427433
_ => vec![],
428434
};
429435

430-
let helps = match e {
436+
let helps = match &e {
431437
Int2Ptr { details: true } =>
432438
vec![
433439
(

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

+19-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use log::trace;
55
use std::cell::RefCell;
66
use std::cmp;
77
use std::fmt;
8+
use std::fmt::Write;
89
use std::num::NonZeroU64;
910

1011
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -203,7 +204,7 @@ impl GlobalStateInner {
203204
self.base_ptr_tags.get(&id).copied().unwrap_or_else(|| {
204205
let tag = self.new_ptr();
205206
if self.tracked_pointer_tags.contains(&tag) {
206-
machine.emit_diagnostic(NonHaltingDiagnostic::CreatedPointerTag(tag.0, None));
207+
machine.emit_diagnostic(NonHaltingDiagnostic::CreatedPointerTag(tag.0, None, None));
207208
}
208209
trace!("New allocation {:?} has base tag {:?}", id, tag);
209210
self.base_ptr_tags.try_insert(id, tag).unwrap();
@@ -674,10 +675,26 @@ trait EvalContextPrivExt<'mir: 'ecx, 'tcx: 'mir, 'ecx>: crate::MiriInterpCxExt<'
674675
loc: Option<(AllocId, Size, ProvenanceExtra)>| // alloc_id, base_offset, orig_tag
675676
-> InterpResult<'tcx> {
676677
let global = this.machine.stacked_borrows.as_ref().unwrap().borrow();
678+
let ty = place.layout.ty;
677679
if global.tracked_pointer_tags.contains(&new_tag) {
680+
let mut kind_str = format!("{kind}");
681+
match kind {
682+
RefKind::Unique { two_phase: false }
683+
if !ty.is_unpin(this.tcx.at(DUMMY_SP), this.param_env()) =>
684+
{
685+
write!(kind_str, " (!Unpin pointee type {ty})").unwrap()
686+
},
687+
RefKind::Shared
688+
if !ty.is_freeze(this.tcx.at(DUMMY_SP), this.param_env()) =>
689+
{
690+
write!(kind_str, " (!Freeze pointee type {ty})").unwrap()
691+
},
692+
_ => write!(kind_str, " (pointee type {ty})").unwrap(),
693+
};
678694
this.emit_diagnostic(NonHaltingDiagnostic::CreatedPointerTag(
679695
new_tag.0,
680-
loc.map(|(alloc_id, base_offset, _)| (alloc_id, alloc_range(base_offset, size))),
696+
Some(kind_str),
697+
loc.map(|(alloc_id, base_offset, orig_tag)| (alloc_id, alloc_range(base_offset, size), orig_tag)),
681698
));
682699
}
683700
drop(global); // don't hold that reference any longer than we have to

0 commit comments

Comments
 (0)