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