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