1
Fork 0

interpret: have assert_* intrinsics call the panic machinery instead of a direct abort

This commit is contained in:
Ralf Jung 2023-08-19 14:20:41 +02:00
parent 788fd44a3b
commit ac3bca24b7
16 changed files with 114 additions and 43 deletions

View file

@ -18,7 +18,6 @@ pub enum ConstEvalErrKind {
ModifiedGlobal, ModifiedGlobal,
AssertFailure(AssertKind<ConstInt>), AssertFailure(AssertKind<ConstInt>),
Panic { msg: Symbol, line: u32, col: u32, file: Symbol }, Panic { msg: Symbol, line: u32, col: u32, file: Symbol },
Abort(String),
} }
impl MachineStopType for ConstEvalErrKind { impl MachineStopType for ConstEvalErrKind {
@ -30,7 +29,6 @@ impl MachineStopType for ConstEvalErrKind {
ModifiedGlobal => const_eval_modified_global, ModifiedGlobal => const_eval_modified_global,
Panic { .. } => const_eval_panic, Panic { .. } => const_eval_panic,
AssertFailure(x) => x.diagnostic_message(), AssertFailure(x) => x.diagnostic_message(),
Abort(msg) => msg.to_string().into(),
} }
} }
fn add_args( fn add_args(
@ -39,7 +37,7 @@ impl MachineStopType for ConstEvalErrKind {
) { ) {
use ConstEvalErrKind::*; use ConstEvalErrKind::*;
match *self { match *self {
ConstAccessesStatic | ModifiedGlobal | Abort(_) => {} ConstAccessesStatic | ModifiedGlobal => {}
AssertFailure(kind) => kind.add_args(adder), AssertFailure(kind) => kind.add_args(adder),
Panic { msg, line, col, file } => { Panic { msg, line, col, file } => {
adder("msg".into(), msg.into_diagnostic_arg()); adder("msg".into(), msg.into_diagnostic_arg());

View file

@ -464,6 +464,13 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
Ok(Some((ecx.load_mir(instance.def, None)?, orig_instance))) Ok(Some((ecx.load_mir(instance.def, None)?, orig_instance)))
} }
fn panic_nounwind(ecx: &mut InterpCx<'mir, 'tcx, Self>, msg: &str) -> InterpResult<'tcx> {
let msg = Symbol::intern(msg);
let span = ecx.find_closest_untracked_caller_location();
let (file, line, col) = ecx.location_triple_for_span(span);
return Err(ConstEvalErrKind::Panic { msg, file, line, col }.into());
}
fn call_intrinsic( fn call_intrinsic(
ecx: &mut InterpCx<'mir, 'tcx, Self>, ecx: &mut InterpCx<'mir, 'tcx, Self>,
instance: ty::Instance<'tcx>, instance: ty::Instance<'tcx>,
@ -584,10 +591,6 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
Err(ConstEvalErrKind::AssertFailure(err).into()) Err(ConstEvalErrKind::AssertFailure(err).into())
} }
fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>, msg: String) -> InterpResult<'tcx, !> {
Err(ConstEvalErrKind::Abort(msg).into())
}
fn binary_ptr_op( fn binary_ptr_op(
_ecx: &InterpCx<'mir, 'tcx, Self>, _ecx: &InterpCx<'mir, 'tcx, Self>,
_bin_op: mir::BinOp, _bin_op: mir::BinOp,

View file

@ -125,15 +125,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
) -> InterpResult<'tcx, bool> { ) -> InterpResult<'tcx, bool> {
let instance_args = instance.args; let instance_args = instance.args;
let intrinsic_name = self.tcx.item_name(instance.def_id()); let intrinsic_name = self.tcx.item_name(instance.def_id());
let Some(ret) = ret else {
// First handle intrinsics without return place. // We don't support any intrinsic without return place.
let ret = match ret { return Ok(false);
None => match intrinsic_name {
sym::abort => M::abort(self, "the program aborted execution".to_owned())?,
// Unsupported diverging intrinsic.
_ => return Ok(false),
},
Some(p) => p,
}; };
match intrinsic_name { match intrinsic_name {
@ -410,7 +404,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
ValidityRequirement::Uninit => bug!("assert_uninit_valid doesn't exist"), ValidityRequirement::Uninit => bug!("assert_uninit_valid doesn't exist"),
}; };
M::abort(self, msg)?; M::panic_nounwind(self, &msg)?;
// Skip the `go_to_block` at the end.
return Ok(true);
} }
} }
sym::simd_insert => { sym::simd_insert => {

View file

@ -218,10 +218,8 @@ pub trait Machine<'mir, 'tcx: 'mir>: Sized {
unwind: mir::UnwindAction, unwind: mir::UnwindAction,
) -> InterpResult<'tcx>; ) -> InterpResult<'tcx>;
/// Called to abort evaluation. /// Called to trigger a non-unwinding panic.
fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>, _msg: String) -> InterpResult<'tcx, !> { fn panic_nounwind(_ecx: &mut InterpCx<'mir, 'tcx, Self>, msg: &str) -> InterpResult<'tcx>;
throw_unsup_format!("aborting execution is not supported")
}
/// Called when unwinding reached a state where execution should be terminated. /// Called when unwinding reached a state where execution should be terminated.
fn unwind_terminate(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx>; fn unwind_terminate(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx>;

View file

@ -180,6 +180,10 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx>
throw_machine_stop_str!("calling functions isn't supported in ConstProp") throw_machine_stop_str!("calling functions isn't supported in ConstProp")
} }
fn panic_nounwind(_ecx: &mut InterpCx<'mir, 'tcx, Self>, _msg: &str) -> InterpResult<'tcx> {
throw_machine_stop_str!("panicking isn't supported in ConstProp")
}
fn find_mir_or_eval_fn( fn find_mir_or_eval_fn(
_ecx: &mut InterpCx<'mir, 'tcx, Self>, _ecx: &mut InterpCx<'mir, 'tcx, Self>,
_instance: ty::Instance<'tcx>, _instance: ty::Instance<'tcx>,

View file

@ -541,6 +541,13 @@ impl<'mir, 'tcx: 'mir> rustc_const_eval::interpret::Machine<'mir, 'tcx> for Dumm
unimplemented!() unimplemented!()
} }
fn panic_nounwind(
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
_msg: &str,
) -> interpret::InterpResult<'tcx> {
unimplemented!()
}
fn call_intrinsic( fn call_intrinsic(
_ecx: &mut InterpCx<'mir, 'tcx, Self>, _ecx: &mut InterpCx<'mir, 'tcx, Self>,
_instance: ty::Instance<'tcx>, _instance: ty::Instance<'tcx>,

View file

@ -971,9 +971,8 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
ecx.assert_panic(msg, unwind) ecx.assert_panic(msg, unwind)
} }
#[inline(always)] fn panic_nounwind(ecx: &mut InterpCx<'mir, 'tcx, Self>, msg: &str) -> InterpResult<'tcx> {
fn abort(_ecx: &mut MiriInterpCx<'mir, 'tcx>, msg: String) -> InterpResult<'tcx, !> { ecx.start_panic_nounwind(msg)
throw_machine_stop!(TerminationInfo::Abort(msg))
} }
fn unwind_terminate(ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> { fn unwind_terminate(ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {

View file

@ -34,10 +34,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
if this.emulate_intrinsic(instance, args, dest, ret)? { if this.emulate_intrinsic(instance, args, dest, ret)? {
return Ok(()); return Ok(());
} }
// All remaining supported intrinsics have a return place.
let intrinsic_name = this.tcx.item_name(instance.def_id()); let intrinsic_name = this.tcx.item_name(instance.def_id());
let intrinsic_name = intrinsic_name.as_str(); let intrinsic_name = intrinsic_name.as_str();
// Handle intrinsics without return place.
match intrinsic_name {
"abort" => {
throw_machine_stop!(TerminationInfo::Abort(
"the program aborted execution".to_owned()
))
}
_ => {},
}
// All remaining supported intrinsics have a return place.
let ret = match ret { let ret = match ret {
None => throw_unsup_format!("unimplemented (diverging) intrinsic: `{intrinsic_name}`"), None => throw_unsup_format!("unimplemented (diverging) intrinsic: `{intrinsic_name}`"),
Some(p) => p, Some(p) => p,
@ -393,7 +403,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
"breakpoint" => { "breakpoint" => {
let [] = check_arg_count(args)?; let [] = check_arg_count(args)?;
// normally this would raise a SIGTRAP, which aborts if no debugger is connected // normally this would raise a SIGTRAP, which aborts if no debugger is connected
throw_machine_stop!(TerminationInfo::Abort(format!("Trace/breakpoint trap"))) throw_machine_stop!(TerminationInfo::Abort(format!("trace/breakpoint trap")))
} }
name => throw_unsup_format!("unimplemented intrinsic: `{name}`"), name => throw_unsup_format!("unimplemented intrinsic: `{name}`"),

View file

@ -188,6 +188,25 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
) )
} }
/// Start a non-unwinding panic in the interpreter with the given message as payload.
fn start_panic_nounwind(&mut self, msg: &str) -> InterpResult<'tcx> {
let this = self.eval_context_mut();
// First arg: message.
let msg = this.allocate_str(msg, MiriMemoryKind::Machine.into(), Mutability::Not)?;
// Call the lang item.
let panic = this.tcx.lang_items().panic_nounwind().unwrap();
let panic = ty::Instance::mono(this.tcx.tcx, panic);
this.call_function(
panic,
Abi::Rust,
&[msg.to_ref(this)],
None,
StackPopCleanup::Goto { ret: None, unwind: mir::UnwindAction::Unreachable },
)
}
fn assert_panic( fn assert_panic(
&mut self, &mut self,
msg: &mir::AssertMessage<'tcx>, msg: &mir::AssertMessage<'tcx>,

View file

@ -2,6 +2,6 @@
fn main() { fn main() {
unsafe { unsafe {
core::intrinsics::breakpoint() //~ ERROR: Trace/breakpoint trap core::intrinsics::breakpoint() //~ ERROR: trace/breakpoint trap
}; };
} }

View file

@ -1,8 +1,8 @@
error: abnormal termination: Trace/breakpoint trap error: abnormal termination: trace/breakpoint trap
--> $DIR/breakpoint.rs:LL:CC --> $DIR/breakpoint.rs:LL:CC
| |
LL | core::intrinsics::breakpoint() LL | core::intrinsics::breakpoint()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Trace/breakpoint trap | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trace/breakpoint trap
| |
= note: inside `main` at $DIR/breakpoint.rs:LL:CC = note: inside `main` at $DIR/breakpoint.rs:LL:CC

View file

@ -1,7 +1,11 @@
//@normalize-stderr-test: "unsafe \{ libc::abort\(\) \}|crate::intrinsics::abort\(\);" -> "ABORT();"
//@normalize-stderr-test: "\| +\^+" -> "| ^"
//@normalize-stderr-test: "\n +[0-9]+:[^\n]+" -> "$1"
//@normalize-stderr-test: "\n at [^\n]+" -> "$1"
//@error-in-other-file: aborted execution
#![feature(never_type)] #![feature(never_type)]
#[allow(deprecated, invalid_value)] #[allow(deprecated, invalid_value)]
fn main() { fn main() {
let _ = unsafe { std::mem::uninitialized::<!>() }; let _ = unsafe { std::mem::uninitialized::<!>() };
//~^ ERROR: attempted to instantiate uninhabited type `!`
} }

View file

@ -1,10 +1,24 @@
error: abnormal termination: aborted execution: attempted to instantiate uninhabited type `!` thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
aborted execution: attempted to instantiate uninhabited type `!`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread caused non-unwinding panic. aborting.
error: abnormal termination: the program aborted execution
--> RUSTLIB/std/src/sys/PLATFORM/mod.rs:LL:CC
|
LL | ABORT();
| ^ the program aborted execution
|
= note: inside `std::sys::PLATFORM::abort_internal` at RUSTLIB/std/src/sys/PLATFORM/mod.rs:LL:CC
= note: inside `std::panicking::rust_panic_with_hook` at RUSTLIB/std/src/panicking.rs:LL:CC
= note: inside closure at RUSTLIB/std/src/panicking.rs:LL:CC
= note: inside `std::sys_common::backtrace::__rust_end_short_backtrace::<[closure@std::panicking::begin_panic_handler::{closure#0}], !>` at RUSTLIB/std/src/sys_common/backtrace.rs:LL:CC
= note: inside `std::panicking::begin_panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC
= note: inside `core::panicking::panic_nounwind` at RUSTLIB/core/src/panicking.rs:LL:CC
note: inside `main`
--> $DIR/uninit_uninhabited_type.rs:LL:CC --> $DIR/uninit_uninhabited_type.rs:LL:CC
| |
LL | let _ = unsafe { std::mem::uninitialized::<!>() }; LL | let _ = unsafe { std::mem::uninitialized::<!>() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to instantiate uninhabited type `!` | ^
|
= note: inside `main` at $DIR/uninit_uninhabited_type.rs:LL:CC
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

View file

@ -1,5 +1,10 @@
//@normalize-stderr-test: "unsafe \{ libc::abort\(\) \}|crate::intrinsics::abort\(\);" -> "ABORT();"
//@normalize-stderr-test: "\| +\^+" -> "| ^"
//@normalize-stderr-test: "\n +[0-9]+:[^\n]+" -> "$1"
//@normalize-stderr-test: "\n at [^\n]+" -> "$1"
//@error-in-other-file: aborted execution
#[allow(deprecated, invalid_value)] #[allow(deprecated, invalid_value)]
fn main() { fn main() {
let _ = unsafe { std::mem::zeroed::<fn()>() }; let _ = unsafe { std::mem::zeroed::<fn()>() };
//~^ ERROR: attempted to zero-initialize type `fn()`, which is invalid
} }

View file

@ -1,10 +1,24 @@
error: abnormal termination: aborted execution: attempted to zero-initialize type `fn()`, which is invalid thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
aborted execution: attempted to zero-initialize type `fn()`, which is invalid
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread caused non-unwinding panic. aborting.
error: abnormal termination: the program aborted execution
--> RUSTLIB/std/src/sys/PLATFORM/mod.rs:LL:CC
|
LL | ABORT();
| ^ the program aborted execution
|
= note: inside `std::sys::PLATFORM::abort_internal` at RUSTLIB/std/src/sys/PLATFORM/mod.rs:LL:CC
= note: inside `std::panicking::rust_panic_with_hook` at RUSTLIB/std/src/panicking.rs:LL:CC
= note: inside closure at RUSTLIB/std/src/panicking.rs:LL:CC
= note: inside `std::sys_common::backtrace::__rust_end_short_backtrace::<[closure@std::panicking::begin_panic_handler::{closure#0}], !>` at RUSTLIB/std/src/sys_common/backtrace.rs:LL:CC
= note: inside `std::panicking::begin_panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC
= note: inside `core::panicking::panic_nounwind` at RUSTLIB/core/src/panicking.rs:LL:CC
note: inside `main`
--> $DIR/zero_fn_ptr.rs:LL:CC --> $DIR/zero_fn_ptr.rs:LL:CC
| |
LL | let _ = unsafe { std::mem::zeroed::<fn()>() }; LL | let _ = unsafe { std::mem::zeroed::<fn()>() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to zero-initialize type `fn()`, which is invalid | ^
|
= note: inside `main` at $DIR/zero_fn_ptr.rs:LL:CC
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

View file

@ -2,19 +2,19 @@ error[E0080]: evaluation of constant value failed
--> $DIR/assert-type-intrinsics.rs:12:9 --> $DIR/assert-type-intrinsics.rs:12:9
| |
LL | MaybeUninit::<!>::uninit().assume_init(); LL | MaybeUninit::<!>::uninit().assume_init();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to instantiate uninhabited type `!` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'aborted execution: attempted to instantiate uninhabited type `!`', $DIR/assert-type-intrinsics.rs:12:36
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/assert-type-intrinsics.rs:16:9 --> $DIR/assert-type-intrinsics.rs:16:9
| |
LL | intrinsics::assert_mem_uninitialized_valid::<&'static i32>(); LL | intrinsics::assert_mem_uninitialized_valid::<&'static i32>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to leave type `&i32` uninitialized, which is invalid | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'aborted execution: attempted to leave type `&i32` uninitialized, which is invalid', $DIR/assert-type-intrinsics.rs:16:9
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/assert-type-intrinsics.rs:20:9 --> $DIR/assert-type-intrinsics.rs:20:9
| |
LL | intrinsics::assert_zero_valid::<&'static i32>(); LL | intrinsics::assert_zero_valid::<&'static i32>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to zero-initialize type `&i32`, which is invalid | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'aborted execution: attempted to zero-initialize type `&i32`, which is invalid', $DIR/assert-type-intrinsics.rs:20:9
error: aborting due to 3 previous errors error: aborting due to 3 previous errors