]> git.lizzy.rs Git - rust.git/blob - src/test/ui/runtime/out-of-stack.rs
Auto merge of #98051 - davidtwco:split-dwarf-stabilization, r=wesleywiser
[rust.git] / src / test / ui / runtime / out-of-stack.rs
1 // run-pass
2
3 #![allow(unused_must_use)]
4 #![allow(unconditional_recursion)]
5 // ignore-android: FIXME (#20004)
6 // ignore-emscripten no processes
7 // ignore-sgx no processes
8
9 #![feature(core_intrinsics)]
10 #![feature(rustc_private)]
11
12 #[cfg(unix)]
13 extern crate libc;
14
15 use std::env;
16 use std::process::Command;
17 use std::thread;
18
19 // Inlining to avoid llvm turning the recursive functions into tail calls,
20 // which doesn't consume stack.
21 #[inline(always)]
22 pub fn black_box<T>(dummy: T) { std::intrinsics::black_box(dummy); }
23
24 fn silent_recurse() {
25     let buf = [0u8; 1000];
26     black_box(buf);
27     silent_recurse();
28 }
29
30 fn loud_recurse() {
31     println!("hello!");
32     loud_recurse();
33     black_box(()); // don't optimize this into a tail call. please.
34 }
35
36 #[cfg(unix)]
37 fn check_status(status: std::process::ExitStatus)
38 {
39     use std::os::unix::process::ExitStatusExt;
40
41     assert!(!status.success());
42     assert_eq!(status.signal(), Some(libc::SIGABRT));
43 }
44
45 #[cfg(not(unix))]
46 fn check_status(status: std::process::ExitStatus)
47 {
48     assert!(!status.success());
49 }
50
51
52 fn main() {
53     let args: Vec<String> = env::args().collect();
54     if args.len() > 1 && args[1] == "silent" {
55         silent_recurse();
56     } else if args.len() > 1 && args[1] == "loud" {
57         loud_recurse();
58     } else if args.len() > 1 && args[1] == "silent-thread" {
59         thread::spawn(silent_recurse).join();
60     } else if args.len() > 1 && args[1] == "loud-thread" {
61         thread::spawn(loud_recurse).join();
62     } else {
63         let mut modes = vec![
64             "silent-thread",
65             "loud-thread",
66         ];
67
68         // On linux it looks like the main thread can sometimes grow its stack
69         // basically without bounds, so we only test the child thread cases
70         // there.
71         if !cfg!(target_os = "linux") {
72             modes.push("silent");
73             modes.push("loud");
74         }
75         for mode in modes {
76             println!("testing: {}", mode);
77
78             let silent = Command::new(&args[0]).arg(mode).output().unwrap();
79
80             check_status(silent.status);
81
82             let error = String::from_utf8_lossy(&silent.stderr);
83             assert!(error.contains("has overflowed its stack"),
84                     "missing overflow message: {}", error);
85         }
86     }
87 }