9 // ignore-emscripten no processes
10 // ignore-sgx no processes
11 // ignore-fuchsia no exception handler registered for segfault
14 use std::mem::MaybeUninit;
15 use std::process::Command;
18 #[link(name = "rust_test_helpers", kind = "static")]
20 #[link_name = "rust_dbg_extern_identity_u64"]
25 let args = env::args().skip(1).collect::<Vec<_>>();
28 "main-recurse" => overflow_recurse(),
29 "child-recurse" => thread::spawn(overflow_recurse).join().unwrap(),
30 "child-frame" => overflow_frame(),
36 let me = env::current_exe().unwrap();
38 // The linux kernel has some different behavior for the main thread because
39 // the main thread's stack can typically grow. We can't always guarantee
40 // that we report stack overflow on the main thread, see #43052 for some
42 if cfg!(not(target_os = "linux")) {
43 assert_overflow(Command::new(&me).arg("main-recurse"));
45 assert_overflow(Command::new(&me).arg("child-recurse"));
46 assert_overflow(Command::new(&me).arg("child-frame"));
49 #[allow(unconditional_recursion)]
50 fn recurse(array: &MaybeUninit<[u64; 1024]>) {
52 black_box(array.as_ptr() as u64);
54 let local: MaybeUninit<[u64; 1024]> = MaybeUninit::uninit();
59 fn overflow_recurse() {
60 recurse(&MaybeUninit::uninit());
64 // By using a 1MiB stack frame with only 512KiB stack, we'll jump over any
65 // guard page, even with 64K pages -- but stack probes should catch it.
66 const STACK_SIZE: usize = 512 * 1024;
67 thread::Builder::new().stack_size(STACK_SIZE).spawn(|| {
68 let local: MaybeUninit<[u8; 2 * STACK_SIZE]> = MaybeUninit::uninit();
70 black_box(local.as_ptr() as u64);
72 }).unwrap().join().unwrap();
75 fn assert_overflow(cmd: &mut Command) {
76 let output = cmd.output().unwrap();
77 assert!(!output.status.success());
78 let stdout = String::from_utf8_lossy(&output.stdout);
79 let stderr = String::from_utf8_lossy(&output.stderr);
80 println!("status: {}", output.status);
81 println!("stdout: {}", stdout);
82 println!("stderr: {}", stderr);
83 assert!(stdout.is_empty());
84 assert!(stderr.contains("has overflowed its stack\n"));