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