Skip to content

ensure we catch incorrectly unwinding calls #1744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/shims/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

trace!("miri_start_panic: {:?}", this.frame().instance);
// Make sure we only start unwinding when this matches our panic strategy.
assert_eq!(this.tcx.sess.panic_strategy(), PanicStrategy::Unwind);
if this.tcx.sess.panic_strategy() != PanicStrategy::Unwind {
throw_ub_format!("unwinding despite panic=abort");
}

// Get the raw pointer stored in arg[0] (the panic payload).
let &[ref payload] = check_arg_count(args)?;
Expand Down
16 changes: 16 additions & 0 deletions tests/compile-fail/panic/bad_unwind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// error-pattern: calling a function with ABI C-unwind using caller ABI C
#![feature(c_unwind)]

//! Unwinding when the caller ABI is "C" (without "-unwind") is UB.
//! Currently we detect the ABI mismatch; we could probably allow such calls in principle one day
//! but then we have to detect the unexpected unwinding.

extern "C-unwind" fn unwind() {
panic!();
}

fn main() {
let unwind: extern "C-unwind" fn() = unwind;
let unwind: extern "C" fn() = unsafe { std::mem::transmute(unwind) };
std::panic::catch_unwind(|| unwind()).unwrap_err();
}
11 changes: 11 additions & 0 deletions tests/compile-fail/panic/unwind_panic_abort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// compile-flags: -Cpanic=abort

//! Unwinding despite `-C panic=abort` is an error.

extern "Rust" {
fn miri_start_panic(payload: *mut u8) -> !;
}

fn main() {
unsafe { miri_start_panic(&mut 0); } //~ ERROR unwinding despite panic=abort
}