Skip to content

Commit

Permalink
Auto merge of rust-lang#3797 - zachs18:no-borrow-no-panic, r=RalfJung
Browse files Browse the repository at this point in the history
Don't panic on `miri_print_borrow_state()` under `-Zmiri-disable-stacked-borrows`.

Fixes rust-lang#3459

Instead, just print "attempted to print borrow state, but no borrow state is being tracked" directly to ~~stdout~~ stderr and return successfully. Alternately, the diagnostic machinery could be used to print a structured error/warning.

 (Does not address the comment about nothing being printed for `miri_print_borrow_state` with `alloc_id = 0`)
  • Loading branch information
bors committed Aug 9, 2024
2 parents ad8aa16 + aee5e91 commit 770c234
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/tools/miri/src/borrow_tracker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,11 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {

fn print_borrow_state(&mut self, alloc_id: AllocId, show_unnamed: bool) -> InterpResult<'tcx> {
let this = self.eval_context_mut();
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
let Some(borrow_tracker) = &this.machine.borrow_tracker else {
eprintln!("attempted to print borrow state, but no borrow state is being tracked");
return Ok(());
};
let method = borrow_tracker.borrow().borrow_tracker_method;
match method {
BorrowTrackerMethod::StackedBorrows => this.print_stacks(alloc_id),
BorrowTrackerMethod::TreeBorrows => this.print_tree(alloc_id, show_unnamed),
Expand Down
8 changes: 6 additions & 2 deletions src/tools/miri/src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,12 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
let [id, show_unnamed] = this.check_shim(abi, Abi::Rust, link_name, args)?;
let id = this.read_scalar(id)?.to_u64()?;
let show_unnamed = this.read_scalar(show_unnamed)?.to_bool()?;
if let Some(id) = std::num::NonZero::new(id) {
this.print_borrow_state(AllocId(id), show_unnamed)?;
if let Some(id) = std::num::NonZero::new(id).map(AllocId)
&& this.get_alloc_info(id).2 == AllocKind::LiveData
{
this.print_borrow_state(id, show_unnamed)?;
} else {
eprintln!("{id} is not the ID of a live data allocation");
}
}
"miri_pointer_name" => {
Expand Down

0 comments on commit 770c234

Please sign in to comment.