rust/src/test/ui/panics/abort-on-panic.rs

77 lines
1.9 KiB
Rust
Raw Normal View History

// run-pass
#![allow(unused_must_use)]
#![feature(unwind_attributes)]
// Since we mark some ABIs as "nounwind" to LLVM, we must make sure that
// we never unwind through them.
// ignore-emscripten no processes
2019-04-24 16:26:33 +00:00
// ignore-sgx no processes
use std::{env, panic};
use std::io::prelude::*;
use std::io;
use std::process::{Command, Stdio};
2019-10-12 19:02:38 +00:00
#[unwind(aborts)] // FIXME(#58794) should work even without the attribute
extern "C" fn panic_in_ffi() {
panic!("Test");
}
2019-10-12 19:02:38 +00:00
#[unwind(aborts)]
extern "Rust" fn panic_in_rust_abi() {
panic!("TestRust");
}
fn should_have_aborted() {
io::stdout().write(b"This should never be printed.\n");
let _ = io::stdout().flush();
}
fn bomb_out_but_not_abort(msg: &str) {
eprintln!("bombing out: {}", msg);
exit(1);
}
fn test() {
let _ = panic::catch_unwind(|| { panic_in_ffi(); });
should_have_aborted();
}
2019-10-12 19:02:38 +00:00
fn testrust() {
let _ = panic::catch_unwind(|| { panic_in_rust_abi(); });
should_have_aborted();
2019-10-12 19:02:38 +00:00
}
fn main() {
let tests: &[(_, fn())] = &[
("test", test),
("testrust", testrust),
];
let args: Vec<String> = env::args().collect();
2019-10-12 19:02:38 +00:00
if args.len() > 1 {
// This is inside the self-executed command.
for (a,f) in tests {
if &args[1] == a { return f() }
2019-10-12 19:02:38 +00:00
}
bomb_out_but_not_abort("bad test");
}
let execute_self_expecting_abort = |arg| {
let mut p = Command::new(&args[0])
.stdout(Stdio::piped())
.stdin(Stdio::piped())
.arg(arg).spawn().unwrap();
let status = p.wait().unwrap();
assert!(!status.success());
// Any reasonable platform can distinguish a process which
// called exit(1) from one which panicked.
assert_ne!(status.code(), Some(1));
};
for (a,_f) in tests {
execute_self_expecting_abort(a);
}
}