summaryrefslogtreecommitdiffstats
path: root/tests/ui/panics/abort-on-panic.rs
blob: 7fbee85ffd14faabc1b5130626e4dc923f404233 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// run-pass
// revisions: current next
//[next] compile-flags: -Ztrait-solver=next

#![allow(unused_must_use)]
#![feature(c_unwind)]
#![feature(panic_always_abort)]
// Since we mark some ABIs as "nounwind" to LLVM, we must make sure that
// we never unwind through them.

// ignore-emscripten no processes
// ignore-sgx no processes

use std::io;
use std::io::prelude::*;
use std::process::{exit, Command, Stdio};
use std::sync::{Arc, Barrier};
use std::thread;
use std::{env, panic};

extern "C" fn panic_in_ffi() {
    panic!("Test");
}

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();
}

fn test_always_abort() {
    panic::always_abort();
    let _ = panic::catch_unwind(|| {
        panic!();
    });
    should_have_aborted();
}

fn test_always_abort_thread() {
    let barrier = Arc::new(Barrier::new(2));
    let thr = {
        let barrier = barrier.clone();
        thread::spawn(move || {
            barrier.wait();
            panic!("in thread");
        })
    };
    panic::always_abort();
    barrier.wait();
    let _ = thr.join();
    bomb_out_but_not_abort("joined - but we were supposed to panic!");
}

fn main() {
    let tests: &[(_, fn())] = &[
        ("test", test),
        ("test_always_abort", test_always_abort),
        ("test_always_abort_thread", test_always_abort_thread),
    ];

    let args: Vec<String> = env::args().collect();
    if args.len() > 1 {
        // This is inside the self-executed command.
        for (a, f) in tests {
            if &args[1] == a {
                return f();
            }
        }
        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);
    }
}