Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gnzlbg committed Sep 9, 2019
1 parent 08574db commit 801e1b7
Showing 1 changed file with 64 additions and 2 deletions.
66 changes: 64 additions & 2 deletions src/test/ui/abi/abort-on-c-abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ extern "C" fn panic_in_ffi() {
panic!("Test");
}

#[unwind(allowed)]
unsafe extern "C" fn unsafe_panic_allow_in_ffi() {
panic!("Test");
}

#[unwind(aborts)]
unsafe extern "C" fn unsafe_abort_in_ffi() {
panic!("Test");
}

#[unwind(allowed)]
extern "C" fn nopanic_in_ffi() {
panic!("Test");
}

#[unwind(aborts)]
extern "C" fn abort_in_ffi() {
panic!("Test");
}

fn test() {
// A safe extern "C" function that panics should abort the process:
let _ = panic::catch_unwind(|| panic_in_ffi() );
Expand All @@ -31,10 +51,36 @@ fn test() {
let _ = io::stdout().flush();
}

fn test2() {
// A safe extern "C" function that panics should abort the process:
let _ = panic::catch_unwind(|| abort_in_ffi() );

// If the process did not abort, the panic escaped FFI:
io::stdout().write(b"This should never be printed.\n");
let _ = io::stdout().flush();
}

fn test3() {
// An unsafe #[unwind(abort)] extern "C" function that panics should abort the process:
let _ = panic::catch_unwind(|| unsafe { unsafe_abort_in_ffi() });

// If the process did not abort, the panic escaped FFI:
io::stdout().write(b"This should never be printed.\n");
let _ = io::stdout().flush();
}

fn main() {
let args: Vec<String> = env::args().collect();
if args.len() > 1 && args[1] == "test" {
return test();
if args.len() > 1 {
if args[1] == "test" {
return test();
}
if args[1] == "test2" {
return test2();
}
if args[1] == "test3" {
return test3();
}
}

let mut p = Command::new(&args[0])
Expand All @@ -43,6 +89,22 @@ fn main() {
.arg("test").spawn().unwrap();
assert!(!p.wait().unwrap().success());

let mut p = Command::new(&args[0])
.stdout(Stdio::piped())
.stdin(Stdio::piped())
.arg("test2").spawn().unwrap();
assert!(!p.wait().unwrap().success());

let mut p = Command::new(&args[0])
.stdout(Stdio::piped())
.stdin(Stdio::piped())
.arg("test3").spawn().unwrap();
assert!(!p.wait().unwrap().success());

// An unsafe extern "C" function that panics should let the panic escape:
assert!(panic::catch_unwind(|| unsafe { unsafe_panic_in_ffi() }).is_err());
assert!(panic::catch_unwind(|| unsafe { unsafe_panic_allow_in_ffi() }).is_err());

// A safe extern "C" unwind(allows) that panics should let the panic escape:
assert!(panic::catch_unwind(|| nopanic_in_ffi()).is_err());
}

0 comments on commit 801e1b7

Please sign in to comment.