]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/out-of-stack.rs
run EndRegion when unwinding otherwise-empty scopes
[rust.git] / src / test / run-pass / out-of-stack.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // ignore-android: FIXME (#20004)
12 // ignore-musl
13 // ignore-emscripten
14
15 #![feature(asm)]
16 #![feature(libc)]
17
18 #[cfg(unix)]
19 extern crate libc;
20
21 use std::env;
22 use std::process::Command;
23 use std::thread;
24
25 // lifted from the test module
26 // Inlining to avoid llvm turning the recursive functions into tail calls,
27 // which doesn't consume stack.
28 #[inline(always)]
29 pub fn black_box<T>(dummy: T) { unsafe { asm!("" : : "r"(&dummy)) } }
30
31 fn silent_recurse() {
32     let buf = [0u8; 1000];
33     black_box(buf);
34     silent_recurse();
35 }
36
37 fn loud_recurse() {
38     println!("hello!");
39     loud_recurse();
40     black_box(()); // don't optimize this into a tail call. please.
41 }
42
43 #[cfg(unix)]
44 fn check_status(status: std::process::ExitStatus)
45 {
46     use libc;
47     use std::os::unix::process::ExitStatusExt;
48
49     assert!(!status.success());
50     assert_eq!(status.signal(), Some(libc::SIGABRT));
51 }
52
53 #[cfg(not(unix))]
54 fn check_status(status: std::process::ExitStatus)
55 {
56     assert!(!status.success());
57 }
58
59
60 fn main() {
61     let args: Vec<String> = env::args().collect();
62     if args.len() > 1 && args[1] == "silent" {
63         silent_recurse();
64     } else if args.len() > 1 && args[1] == "loud" {
65         loud_recurse();
66     } else if args.len() > 1 && args[1] == "silent-thread" {
67         thread::spawn(silent_recurse).join();
68     } else if args.len() > 1 && args[1] == "loud-thread" {
69         thread::spawn(loud_recurse).join();
70     } else {
71         let mut modes = vec![
72             "silent-thread",
73             "loud-thread",
74         ];
75
76         // On linux it looks like the main thread can sometimes grow its stack
77         // basically without bounds, so we only test the child thread cases
78         // there.
79         if !cfg!(target_os = "linux") {
80             modes.push("silent");
81             modes.push("loud");
82         }
83         for mode in modes {
84             println!("testing: {}", mode);
85
86             let silent = Command::new(&args[0]).arg(mode).output().unwrap();
87
88             check_status(silent.status);
89
90             let error = String::from_utf8_lossy(&silent.stderr);
91             assert!(error.contains("has overflowed its stack"),
92                     "missing overflow message: {}", error);
93         }
94     }
95 }