]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/out-of-stack.rs
Auto merge of #28369 - ebfull:fix-higher-ranked, r=nikomatsakis
[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
14 #![feature(asm)]
15
16 use std::env;
17 use std::process::Command;
18 use std::thread;
19
20 // lifted from the test module
21 // Inlining to avoid llvm turning the recursive functions into tail calls,
22 // which doesn't consume stack.
23 #[inline(always)]
24 pub fn black_box<T>(dummy: T) { unsafe { asm!("" : : "r"(&dummy)) } }
25
26 fn silent_recurse() {
27     let buf = [0u8; 1000];
28     black_box(buf);
29     silent_recurse();
30 }
31
32 fn loud_recurse() {
33     println!("hello!");
34     loud_recurse();
35     black_box(()); // don't optimize this into a tail call. please.
36 }
37
38 fn main() {
39     let args: Vec<String> = env::args().collect();
40     if args.len() > 1 && args[1] == "silent" {
41         silent_recurse();
42     } else if args.len() > 1 && args[1] == "loud" {
43         loud_recurse();
44     } else if args.len() > 1 && args[1] == "silent-thread" {
45         thread::spawn(silent_recurse).join();
46     } else if args.len() > 1 && args[1] == "loud-thread" {
47         thread::spawn(loud_recurse).join();
48     } else {
49         let mut modes = vec![
50             "silent-thread",
51             "loud-thread",
52         ];
53
54         // On linux it looks like the main thread can sometimes grow its stack
55         // basically without bounds, so we only test the child thread cases
56         // there.
57         if !cfg!(target_os = "linux") {
58             modes.push("silent");
59             modes.push("loud");
60         }
61         for mode in modes {
62             println!("testing: {}", mode);
63
64             let silent = Command::new(&args[0]).arg(mode).output().unwrap();
65             assert!(!silent.status.success());
66             let error = String::from_utf8_lossy(&silent.stderr);
67             assert!(error.contains("has overflowed its stack"),
68                     "missing overflow message: {}", error);
69         }
70     }
71 }