2020-07-28 07:00:33 -05:00
|
|
|
// normalize-stderr-test "[^ ]*core/[a-z_/]+.rs[0-9:]*" -> "$$LOC"
|
2020-08-16 11:31:48 -05:00
|
|
|
// normalize-stderr-test "catch_panic\.rs:[0-9]{2}" -> "catch_panic.rs:LL"
|
|
|
|
// We test the `align_offset` panic below, make sure we test the interpreter impl and not the "real" one.
|
|
|
|
// compile-flags: -Zmiri-symbolic-alignment-check
|
2019-12-15 05:22:09 -06:00
|
|
|
#![feature(never_type)]
|
2021-07-07 02:32:58 -05:00
|
|
|
#![allow(unconditional_panic, non_fmt_panics)]
|
2020-06-27 07:43:37 -05:00
|
|
|
|
2019-11-24 03:24:41 -06:00
|
|
|
use std::panic::{catch_unwind, AssertUnwindSafe};
|
2019-04-14 20:02:55 -05:00
|
|
|
use std::cell::Cell;
|
|
|
|
|
|
|
|
thread_local! {
|
|
|
|
static MY_COUNTER: Cell<usize> = Cell::new(0);
|
|
|
|
static DROPPED: Cell<bool> = Cell::new(false);
|
|
|
|
static HOOK_CALLED: Cell<bool> = Cell::new(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DropTester;
|
|
|
|
impl Drop for DropTester {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
DROPPED.with(|c| {
|
|
|
|
c.set(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 03:24:41 -06:00
|
|
|
fn do_panic_counter(do_panic: impl FnOnce(usize) -> !) {
|
2019-04-14 20:02:55 -05:00
|
|
|
// If this gets leaked, it will be easy to spot
|
|
|
|
// in Miri's leak report
|
|
|
|
let _string = "LEAKED FROM do_panic_counter".to_string();
|
|
|
|
|
|
|
|
// When we panic, this should get dropped during unwinding
|
|
|
|
let _drop_tester = DropTester;
|
|
|
|
|
|
|
|
// Check for bugs in Miri's panic implementation.
|
|
|
|
// If do_panic_counter() somehow gets called more than once,
|
2019-11-24 03:24:41 -06:00
|
|
|
// we'll generate a different panic message and stderr will differ.
|
2019-04-14 20:02:55 -05:00
|
|
|
let old_val = MY_COUNTER.with(|c| {
|
|
|
|
let val = c.get();
|
|
|
|
c.set(val + 1);
|
|
|
|
val
|
|
|
|
});
|
2019-11-24 03:24:41 -06:00
|
|
|
do_panic(old_val);
|
2019-04-14 20:02:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2019-11-24 03:24:41 -06:00
|
|
|
let prev = std::panic::take_hook();
|
|
|
|
std::panic::set_hook(Box::new(move |panic_info| {
|
2019-04-14 20:02:55 -05:00
|
|
|
HOOK_CALLED.with(|h| h.set(true));
|
2019-11-24 03:24:41 -06:00
|
|
|
prev(panic_info)
|
2019-04-14 20:02:55 -05:00
|
|
|
}));
|
2019-11-24 03:24:41 -06:00
|
|
|
|
2019-11-29 04:19:15 -06:00
|
|
|
// Std panics
|
2020-03-08 14:00:40 -05:00
|
|
|
test(None, |_old_val| std::panic!("Hello from panic: std"));
|
|
|
|
test(None, |old_val| std::panic!(format!("Hello from panic: {:?}", old_val)));
|
|
|
|
test(None, |old_val| std::panic!("Hello from panic: {:?}", old_val));
|
|
|
|
test(None, |_old_val| std::panic!(1337));
|
2019-11-28 12:17:43 -06:00
|
|
|
|
2019-11-29 04:19:15 -06:00
|
|
|
// Core panics
|
2020-03-08 14:00:40 -05:00
|
|
|
test(None, |_old_val| core::panic!("Hello from panic: core"));
|
|
|
|
test(None, |old_val| core::panic!(&format!("Hello from panic: {:?}", old_val)));
|
|
|
|
test(None, |old_val| core::panic!("Hello from panic: {:?}", old_val));
|
|
|
|
|
|
|
|
// Built-in panics; also make sure the message is right.
|
|
|
|
test(
|
|
|
|
Some("index out of bounds: the len is 3 but the index is 4"),
|
|
|
|
|_old_val| { let _val = [0, 1, 2][4]; loop {} },
|
|
|
|
);
|
|
|
|
test(
|
|
|
|
Some("attempt to divide by zero"),
|
|
|
|
|_old_val| { let _val = 1/0; loop {} },
|
|
|
|
);
|
|
|
|
|
|
|
|
test(
|
|
|
|
Some("align_offset: align is not a power-of-two"),
|
|
|
|
|_old_val| { (0usize as *const u8).align_offset(3); loop {} },
|
|
|
|
);
|
2019-11-29 04:19:15 -06:00
|
|
|
|
2020-02-16 06:47:31 -06:00
|
|
|
// Assertion and debug assertion
|
2020-03-08 14:00:40 -05:00
|
|
|
test(None, |_old_val| { assert!(false); loop {} });
|
|
|
|
test(None, |_old_val| { debug_assert!(false); loop {} });
|
2020-02-16 06:47:31 -06:00
|
|
|
|
2019-11-24 03:24:41 -06:00
|
|
|
eprintln!("Success!"); // Make sure we get this in stderr
|
|
|
|
}
|
|
|
|
|
2020-03-08 14:00:40 -05:00
|
|
|
fn test(expect_msg: Option<&str>, do_panic: impl FnOnce(usize) -> !) {
|
2019-11-24 03:24:41 -06:00
|
|
|
// Reset test flags.
|
|
|
|
DROPPED.with(|c| c.set(false));
|
|
|
|
HOOK_CALLED.with(|c| c.set(false));
|
|
|
|
|
|
|
|
// Cause and catch a panic.
|
|
|
|
let res = catch_unwind(AssertUnwindSafe(|| {
|
2019-04-14 20:02:55 -05:00
|
|
|
let _string = "LEAKED FROM CLOSURE".to_string();
|
2019-11-24 03:24:41 -06:00
|
|
|
do_panic_counter(do_panic)
|
|
|
|
})).expect_err("do_panic() did not panic!");
|
2019-04-14 20:02:55 -05:00
|
|
|
|
2019-12-02 02:11:33 -06:00
|
|
|
// See if we can extract the panic message.
|
2020-03-08 14:00:40 -05:00
|
|
|
let msg = if let Some(s) = res.downcast_ref::<String>() {
|
2019-12-02 02:11:33 -06:00
|
|
|
eprintln!("Caught panic message (String): {}", s);
|
2020-03-08 14:00:40 -05:00
|
|
|
Some(s.as_str())
|
2019-12-02 02:11:33 -06:00
|
|
|
} else if let Some(s) = res.downcast_ref::<&str>() {
|
|
|
|
eprintln!("Caught panic message (&str): {}", s);
|
2020-03-08 14:00:40 -05:00
|
|
|
Some(*s)
|
2019-12-02 02:11:33 -06:00
|
|
|
} else {
|
2020-08-16 11:31:48 -05:00
|
|
|
eprintln!("Failed to get caught panic message.");
|
2020-03-08 14:00:40 -05:00
|
|
|
None
|
|
|
|
};
|
|
|
|
if let Some(expect_msg) = expect_msg {
|
|
|
|
assert_eq!(expect_msg, msg.unwrap());
|
2019-11-24 03:24:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test flags.
|
|
|
|
assert!(DROPPED.with(|c| c.get()));
|
|
|
|
assert!(HOOK_CALLED.with(|c| c.get()));
|
2019-04-14 20:02:55 -05:00
|
|
|
}
|