2019-11-19 07:51:08 -06:00
|
|
|
//! Panic runtime for Miri.
|
|
|
|
//!
|
|
|
|
//! The core pieces of the runtime are:
|
|
|
|
//! - An implementation of `__rust_maybe_catch_panic` that pushes the invoked stack frame with
|
|
|
|
//! some extra metadata derived from the panic-catching arguments of `__rust_maybe_catch_panic`.
|
|
|
|
//! - A hack in `libpanic_unwind` that calls the `miri_start_panic` intrinsic instead of the
|
|
|
|
//! target-native panic runtime. (This lives in the rustc repo.)
|
|
|
|
//! - An implementation of `miri_start_panic` that stores its argument (the panic payload), and then
|
|
|
|
//! immediately returns, but on the *unwind* edge (not the normal return edge), thus initiating unwinding.
|
|
|
|
//! - A hook executed each time a frame is popped, such that if the frame pushed by `__rust_maybe_catch_panic`
|
|
|
|
//! gets popped *during unwinding*, we take the panic payload and store it according to the extra
|
|
|
|
//! metadata we remembered when pushing said frame.
|
|
|
|
|
2020-03-30 04:07:32 -05:00
|
|
|
use log::trace;
|
|
|
|
|
2020-04-02 17:05:35 -05:00
|
|
|
use rustc_middle::{mir, ty};
|
2020-04-18 10:53:54 -05:00
|
|
|
use rustc_target::spec::PanicStrategy;
|
2021-03-14 09:30:37 -05:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2019-11-29 03:16:03 -06:00
|
|
|
|
|
|
|
use crate::*;
|
2020-04-02 16:09:20 -05:00
|
|
|
use helpers::check_arg_count;
|
2019-04-14 20:02:55 -05:00
|
|
|
|
2020-03-14 05:53:09 -05:00
|
|
|
/// Holds all of the relevant data for when unwinding hits a `try` frame.
|
2019-04-14 20:02:55 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct CatchUnwindData<'tcx> {
|
2020-03-14 05:53:09 -05:00
|
|
|
/// The `catch_fn` callback to call in case of a panic.
|
|
|
|
catch_fn: Scalar<Tag>,
|
|
|
|
/// The `data` argument for that callback.
|
|
|
|
data: Scalar<Tag>,
|
|
|
|
/// The return place from the original call to `try`.
|
|
|
|
dest: PlaceTy<'tcx, Tag>,
|
|
|
|
/// The return block from the original call to `try`.
|
|
|
|
ret: mir::BasicBlock,
|
2019-04-14 20:02:55 -05:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:55:52 -05:00
|
|
|
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
|
2019-04-14 20:02:55 -05:00
|
|
|
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
|
2020-03-14 05:53:09 -05:00
|
|
|
/// Handles the special `miri_start_panic` intrinsic, which is called
|
2019-11-19 07:51:08 -06:00
|
|
|
/// by libpanic_unwind to delegate the actual unwinding process to Miri.
|
2019-04-14 20:02:55 -05:00
|
|
|
fn handle_miri_start_panic(
|
|
|
|
&mut self,
|
|
|
|
args: &[OpTy<'tcx, Tag>],
|
2019-12-23 05:56:23 -06:00
|
|
|
unwind: Option<mir::BasicBlock>,
|
2019-04-14 20:02:55 -05:00
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
2020-03-30 15:54:49 -05:00
|
|
|
trace!("miri_start_panic: {:?}", this.frame().instance);
|
2020-09-18 06:10:18 -05:00
|
|
|
// Make sure we only start unwinding when this matches our panic strategy.
|
2021-03-14 11:10:45 -05:00
|
|
|
if this.tcx.sess.panic_strategy() != PanicStrategy::Unwind {
|
|
|
|
throw_ub_format!("unwinding despite panic=abort");
|
|
|
|
}
|
2019-04-14 20:02:55 -05:00
|
|
|
|
2019-11-19 14:31:37 -06:00
|
|
|
// Get the raw pointer stored in arg[0] (the panic payload).
|
2021-02-19 18:00:00 -06:00
|
|
|
let &[ref payload] = check_arg_count(args)?;
|
2020-07-23 10:50:45 -05:00
|
|
|
let payload = this.read_scalar(payload)?.check_init()?;
|
2020-08-31 19:32:14 -05:00
|
|
|
let thread = this.active_thread_mut();
|
|
|
|
assert!(
|
|
|
|
thread.panic_payload.is_none(),
|
|
|
|
"the panic runtime should avoid double-panics"
|
|
|
|
);
|
|
|
|
thread.panic_payload = Some(payload);
|
2019-04-14 20:02:55 -05:00
|
|
|
|
2019-11-19 07:51:08 -06:00
|
|
|
// Jump to the unwind block to begin unwinding.
|
2019-11-25 15:48:31 -06:00
|
|
|
this.unwind_to_block(unwind);
|
2019-12-23 05:56:23 -06:00
|
|
|
return Ok(());
|
2019-04-14 20:02:55 -05:00
|
|
|
}
|
|
|
|
|
2020-03-14 05:53:09 -05:00
|
|
|
/// Handles the `try` intrinsic, the underlying implementation of `std::panicking::try`.
|
|
|
|
fn handle_try(
|
2019-04-14 20:02:55 -05:00
|
|
|
&mut self,
|
|
|
|
args: &[OpTy<'tcx, Tag>],
|
2021-02-19 18:00:00 -06:00
|
|
|
dest: &PlaceTy<'tcx, Tag>,
|
2019-04-14 20:02:55 -05:00
|
|
|
ret: mir::BasicBlock,
|
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
2020-03-14 05:53:09 -05:00
|
|
|
// Signature:
|
|
|
|
// fn r#try(try_fn: fn(*mut u8), data: *mut u8, catch_fn: fn(*mut u8, *mut u8)) -> i32
|
|
|
|
// Calls `try_fn` with `data` as argument. If that executes normally, returns 0.
|
|
|
|
// If that unwinds, calls `catch_fn` with the first argument being `data` and
|
|
|
|
// then second argument being a target-dependent `payload` (i.e. it is up to us to define
|
|
|
|
// what that is), and returns 1.
|
|
|
|
// The `payload` is passed (by libstd) to `__rust_panic_cleanup`, which is then expected to
|
|
|
|
// return a `Box<dyn Any + Send + 'static>`.
|
|
|
|
// In Miri, `miri_start_panic` is passed exactly that type, so we make the `payload` simply
|
|
|
|
// a pointer to `Box<dyn Any + Send + 'static>`.
|
2019-11-19 07:51:08 -06:00
|
|
|
|
|
|
|
// Get all the arguments.
|
2021-02-19 18:00:00 -06:00
|
|
|
let &[ref try_fn, ref data, ref catch_fn] = check_arg_count(args)?;
|
2020-07-23 10:50:45 -05:00
|
|
|
let try_fn = this.read_scalar(try_fn)?.check_init()?;
|
|
|
|
let data = this.read_scalar(data)?.check_init()?;
|
|
|
|
let catch_fn = this.read_scalar(catch_fn)?.check_init()?;
|
2020-03-14 05:53:09 -05:00
|
|
|
|
|
|
|
// Now we make a function call, and pass `data` as first and only argument.
|
|
|
|
let f_instance = this.memory.get_fn(try_fn)?.as_instance()?;
|
|
|
|
trace!("try_fn: {:?}", f_instance);
|
2020-04-18 10:53:54 -05:00
|
|
|
let ret_place = MPlaceTy::dangling(this.machine.layouts.unit, this).into();
|
2019-11-28 16:42:10 -06:00
|
|
|
this.call_function(
|
2019-04-14 20:02:55 -05:00
|
|
|
f_instance,
|
2021-03-14 09:30:37 -05:00
|
|
|
Abi::Rust,
|
2020-03-14 05:53:09 -05:00
|
|
|
&[data.into()],
|
2021-02-19 18:00:00 -06:00
|
|
|
Some(&ret_place),
|
2019-04-14 20:02:55 -05:00
|
|
|
// Directly return to caller.
|
|
|
|
StackPopCleanup::Goto { ret: Some(ret), unwind: None },
|
|
|
|
)?;
|
|
|
|
|
2019-11-19 07:51:08 -06:00
|
|
|
// We ourselves will return `0`, eventually (will be overwritten if we catch a panic).
|
|
|
|
this.write_null(dest)?;
|
|
|
|
|
2020-03-14 05:53:09 -05:00
|
|
|
// In unwind mode, we tag this frame with the extra data needed to catch unwinding.
|
2019-04-14 20:02:55 -05:00
|
|
|
// This lets `handle_stack_pop` (below) know that we should stop unwinding
|
|
|
|
// when we pop this frame.
|
2020-03-14 05:53:09 -05:00
|
|
|
if this.tcx.sess.panic_strategy() == PanicStrategy::Unwind {
|
2021-02-19 18:00:00 -06:00
|
|
|
this.frame_mut().extra.catch_unwind = Some(CatchUnwindData { catch_fn, data, dest: *dest, ret });
|
2019-04-14 20:02:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_stack_pop(
|
|
|
|
&mut self,
|
|
|
|
mut extra: FrameData<'tcx>,
|
2019-12-23 05:56:23 -06:00
|
|
|
unwinding: bool,
|
2020-03-14 05:53:09 -05:00
|
|
|
) -> InterpResult<'tcx, StackPopJump> {
|
2019-04-14 20:02:55 -05:00
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
trace!("handle_stack_pop(extra = {:?}, unwinding = {})", extra, unwinding);
|
2020-04-12 03:32:36 -05:00
|
|
|
if let Some(stacked_borrows) = &this.memory.extra.stacked_borrows {
|
2020-03-14 05:53:09 -05:00
|
|
|
stacked_borrows.borrow_mut().end_call(extra.call_id);
|
|
|
|
}
|
2019-04-14 20:02:55 -05:00
|
|
|
|
|
|
|
// We only care about `catch_panic` if we're unwinding - if we're doing a normal
|
|
|
|
// return, then we don't need to do anything special.
|
2020-03-14 05:53:09 -05:00
|
|
|
if let (true, Some(catch_unwind)) = (unwinding, extra.catch_unwind.take()) {
|
|
|
|
// We've just popped a frame that was pushed by `try`,
|
2019-11-19 07:51:08 -06:00
|
|
|
// and we are unwinding, so we should catch that.
|
2020-03-30 15:54:49 -05:00
|
|
|
trace!("unwinding: found catch_panic frame during unwinding: {:?}", this.frame().instance);
|
2019-04-14 20:02:55 -05:00
|
|
|
|
2020-03-14 05:53:09 -05:00
|
|
|
// We set the return value of `try` to 1, since there was a panic.
|
2021-02-19 18:00:00 -06:00
|
|
|
this.write_scalar(Scalar::from_i32(1), &catch_unwind.dest)?;
|
2019-04-14 20:02:55 -05:00
|
|
|
|
2020-08-29 21:38:37 -05:00
|
|
|
// The Thread's `panic_payload` holds what was passed to `miri_start_panic`.
|
2020-03-14 05:53:09 -05:00
|
|
|
// This is exactly the second argument we need to pass to `catch_fn`.
|
2020-08-31 19:32:14 -05:00
|
|
|
let payload = this.active_thread_mut().panic_payload.take().unwrap();
|
2019-04-14 20:02:55 -05:00
|
|
|
|
2020-03-14 05:53:09 -05:00
|
|
|
// Push the `catch_fn` stackframe.
|
|
|
|
let f_instance = this.memory.get_fn(catch_unwind.catch_fn)?.as_instance()?;
|
|
|
|
trace!("catch_fn: {:?}", f_instance);
|
2020-04-18 10:53:54 -05:00
|
|
|
let ret_place = MPlaceTy::dangling(this.machine.layouts.unit, this).into();
|
2020-03-14 05:53:09 -05:00
|
|
|
this.call_function(
|
|
|
|
f_instance,
|
2021-03-14 09:30:37 -05:00
|
|
|
Abi::Rust,
|
2020-03-14 05:53:09 -05:00
|
|
|
&[catch_unwind.data.into(), payload.into()],
|
2021-02-19 18:00:00 -06:00
|
|
|
Some(&ret_place),
|
2020-03-14 05:53:09 -05:00
|
|
|
// Directly return to caller of `try`.
|
|
|
|
StackPopCleanup::Goto { ret: Some(catch_unwind.ret), unwind: None },
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// We pushed a new stack frame, the engine should not do any jumping now!
|
|
|
|
Ok(StackPopJump::NoJump)
|
2019-04-14 20:02:55 -05:00
|
|
|
} else {
|
2020-03-14 05:53:09 -05:00
|
|
|
Ok(StackPopJump::Normal)
|
2020-02-24 09:22:02 -06:00
|
|
|
}
|
2019-04-14 20:02:55 -05:00
|
|
|
}
|
2019-11-29 03:16:03 -06:00
|
|
|
|
2020-03-08 14:00:40 -05:00
|
|
|
/// Starta a panic in the interpreter with the given message as payload.
|
|
|
|
fn start_panic(
|
|
|
|
&mut self,
|
|
|
|
msg: &str,
|
|
|
|
unwind: Option<mir::BasicBlock>,
|
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
// First arg: message.
|
|
|
|
let msg = this.allocate_str(msg, MiriMemoryKind::Machine.into());
|
|
|
|
|
|
|
|
// Call the lang item.
|
|
|
|
let panic = this.tcx.lang_items().panic_fn().unwrap();
|
|
|
|
let panic = ty::Instance::mono(this.tcx.tcx, panic);
|
|
|
|
this.call_function(
|
|
|
|
panic,
|
2021-03-14 09:30:37 -05:00
|
|
|
Abi::Rust,
|
2020-03-08 14:00:40 -05:00
|
|
|
&[msg.to_ref()],
|
|
|
|
None,
|
|
|
|
StackPopCleanup::Goto { ret: None, unwind },
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-11-29 03:16:03 -06:00
|
|
|
fn assert_panic(
|
|
|
|
&mut self,
|
2020-02-13 07:01:35 -06:00
|
|
|
msg: &mir::AssertMessage<'tcx>,
|
2019-11-29 03:16:03 -06:00
|
|
|
unwind: Option<mir::BasicBlock>,
|
|
|
|
) -> InterpResult<'tcx> {
|
2020-03-30 04:07:32 -05:00
|
|
|
use rustc_middle::mir::AssertKind::*;
|
2019-11-29 03:16:03 -06:00
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
match msg {
|
2020-04-12 03:08:12 -05:00
|
|
|
BoundsCheck { index, len } => {
|
2019-11-29 04:17:44 -06:00
|
|
|
// Forward to `panic_bounds_check` lang item.
|
|
|
|
|
2020-03-11 14:05:44 -05:00
|
|
|
// First arg: index.
|
2021-02-19 18:00:00 -06:00
|
|
|
let index = this.read_scalar(&this.eval_operand(index, None)?)?;
|
2020-03-11 14:05:44 -05:00
|
|
|
// Second arg: len.
|
2021-02-19 18:00:00 -06:00
|
|
|
let len = this.read_scalar(&this.eval_operand(len, None)?)?;
|
2019-11-29 03:16:03 -06:00
|
|
|
|
|
|
|
// Call the lang item.
|
|
|
|
let panic_bounds_check = this.tcx.lang_items().panic_bounds_check_fn().unwrap();
|
|
|
|
let panic_bounds_check = ty::Instance::mono(this.tcx.tcx, panic_bounds_check);
|
|
|
|
this.call_function(
|
|
|
|
panic_bounds_check,
|
2021-03-14 09:30:37 -05:00
|
|
|
Abi::Rust,
|
2020-03-11 14:05:44 -05:00
|
|
|
&[index.into(), len.into()],
|
2019-11-29 04:17:44 -06:00
|
|
|
None,
|
|
|
|
StackPopCleanup::Goto { ret: None, unwind },
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Forward everything else to `panic` lang item.
|
2020-03-08 14:00:40 -05:00
|
|
|
this.start_panic(msg.description(), unwind)?;
|
2019-11-29 03:16:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-04-14 20:02:55 -05:00
|
|
|
}
|