]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/stack-probes.rs
Auto merge of #47203 - varkor:output-filename-conflicts-with-directory, r=estebank
[rust.git] / src / test / run-pass / stack-probes.rs
1 // Copyright 2017 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-arm
12 // ignore-aarch64
13 // ignore-wasm
14 // ignore-cloudabi no processes
15 // ignore-emscripten no processes
16 // ignore-musl FIXME #31506
17 // min-system-llvm-version 5.0
18
19 use std::mem;
20 use std::process::Command;
21 use std::thread;
22 use std::env;
23
24 #[link(name = "rust_test_helpers", kind = "static")]
25 extern {
26     #[link_name = "rust_dbg_extern_identity_u64"]
27     fn black_box(u: u64);
28 }
29
30 fn main() {
31     let args = env::args().skip(1).collect::<Vec<_>>();
32     if args.len() > 0 {
33         match &args[0][..] {
34             "main-thread" => recurse(&[]),
35             "child-thread" => thread::spawn(|| recurse(&[])).join().unwrap(),
36             _ => panic!(),
37         }
38         return
39     }
40
41     let me = env::current_exe().unwrap();
42
43     // The linux kernel has some different behavior for the main thread because
44     // the main thread's stack can typically grow. We can't always guarantee
45     // that we report stack overflow on the main thread, see #43052 for some
46     // details
47     if cfg!(not(target_os = "linux")) {
48         assert_overflow(Command::new(&me).arg("main-thread"));
49     }
50     assert_overflow(Command::new(&me).arg("child-thread"));
51 }
52
53 #[allow(unconditional_recursion)]
54 fn recurse(array: &[u64]) {
55     unsafe { black_box(array.as_ptr() as u64); }
56     let local: [_; 1024] = unsafe { mem::uninitialized() };
57     recurse(&local);
58 }
59
60 fn assert_overflow(cmd: &mut Command) {
61     let output = cmd.output().unwrap();
62     assert!(!output.status.success());
63     let stdout = String::from_utf8_lossy(&output.stdout);
64     let stderr = String::from_utf8_lossy(&output.stderr);
65     println!("status: {}", output.status);
66     println!("stdout: {}", stdout);
67     println!("stderr: {}", stderr);
68     assert!(stdout.is_empty());
69     assert!(stderr.contains("has overflowed its stack\n"));
70 }