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