Skip to content

Commit 105dba7

Browse files
committed
Auto merge of rust-lang#2684 - RalfJung:stack-borrows-weak-protectors, r=saethlin
Stack borrows: weak protectors This addresses the issue described in rust-lang/unsafe-code-guidelines#376.
2 parents c043a0e + 32e9d00 commit 105dba7

20 files changed

+169
-78
lines changed

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_span::{Span, SpanData};
66
use rustc_target::abi::Size;
77

88
use crate::helpers::CurrentSpan;
9-
use crate::stacked_borrows::{err_sb_ub, AccessKind, GlobalStateInner, Permission};
9+
use crate::stacked_borrows::{err_sb_ub, AccessKind, GlobalStateInner, Permission, ProtectorKind};
1010
use crate::*;
1111

1212
use rustc_middle::mir::interpret::InterpError;
@@ -288,7 +288,11 @@ impl<'span, 'history, 'ecx, 'mir, 'tcx> DiagnosticCx<'span, 'history, 'ecx, 'mir
288288
}
289289
Operation::Access(AccessOp { kind, range, .. }) =>
290290
(*range, InvalidationCause::Access(*kind)),
291-
_ => unreachable!("Tags can only be invalidated during a retag or access"),
291+
Operation::Dealloc(_) => {
292+
// This can be reached, but never be relevant later since the entire allocation is
293+
// gone now.
294+
return;
295+
}
292296
};
293297
self.history.invalidations.push(Invalidation { tag, range, span, cause });
294298
}
@@ -369,7 +373,7 @@ impl<'span, 'history, 'ecx, 'mir, 'tcx> DiagnosticCx<'span, 'history, 'ecx, 'mir
369373

370374
/// Report a descriptive error when `new` could not be granted from `derived_from`.
371375
#[inline(never)] // This is only called on fatal code paths
372-
pub fn grant_error(&self, perm: Permission, stack: &Stack) -> InterpError<'tcx> {
376+
pub(super) fn grant_error(&self, perm: Permission, stack: &Stack) -> InterpError<'tcx> {
373377
let Operation::Retag(op) = &self.operation else {
374378
unreachable!("grant_error should only be called during a retag")
375379
};
@@ -389,7 +393,7 @@ impl<'span, 'history, 'ecx, 'mir, 'tcx> DiagnosticCx<'span, 'history, 'ecx, 'mir
389393

390394
/// Report a descriptive error when `access` is not permitted based on `tag`.
391395
#[inline(never)] // This is only called on fatal code paths
392-
pub fn access_error(&self, stack: &Stack) -> InterpError<'tcx> {
396+
pub(super) fn access_error(&self, stack: &Stack) -> InterpError<'tcx> {
393397
let Operation::Access(op) = &self.operation else {
394398
unreachable!("access_error should only be called during an access")
395399
};
@@ -408,7 +412,11 @@ impl<'span, 'history, 'ecx, 'mir, 'tcx> DiagnosticCx<'span, 'history, 'ecx, 'mir
408412
}
409413

410414
#[inline(never)] // This is only called on fatal code paths
411-
pub fn protector_error(&self, item: &Item) -> InterpError<'tcx> {
415+
pub(super) fn protector_error(&self, item: &Item, kind: ProtectorKind) -> InterpError<'tcx> {
416+
let protected = match kind {
417+
ProtectorKind::WeakProtector => "weakly protected",
418+
ProtectorKind::StrongProtector => "strongly protected",
419+
};
412420
let call_id = self
413421
.threads
414422
.all_stacks()
@@ -422,19 +430,15 @@ impl<'span, 'history, 'ecx, 'mir, 'tcx> DiagnosticCx<'span, 'history, 'ecx, 'mir
422430
match self.operation {
423431
Operation::Dealloc(_) =>
424432
err_sb_ub(
425-
format!(
426-
"deallocating while item {:?} is protected by call {:?}",
427-
item, call_id
428-
),
433+
format!("deallocating while item {item:?} is {protected} by call {call_id:?}",),
429434
None,
430435
None,
431436
),
432437
Operation::Retag(RetagOp { orig_tag: tag, .. })
433438
| Operation::Access(AccessOp { tag, .. }) =>
434439
err_sb_ub(
435440
format!(
436-
"not granting access to tag {:?} because that would remove {:?} which is protected because it is an argument of call {:?}",
437-
tag, item, call_id
441+
"not granting access to tag {tag:?} because that would remove {item:?} which is {protected} because it is an argument of call {call_id:?}",
438442
),
439443
None,
440444
tag.and_then(|tag| self.get_logs_relevant_to(tag, Some(item.tag()))),

0 commit comments

Comments
 (0)