]> git.lizzy.rs Git - rust.git/blob - src/test/ui/process/process-panic-after-fork.rs
Rollup merge of #90498 - joshtriplett:target-tier-policy-draft-updates, r=Mark-Simulacrum
[rust.git] / src / test / ui / process / process-panic-after-fork.rs
1 // run-pass
2 // no-prefer-dynamic
3 // ignore-wasm32-bare no libc
4 // ignore-windows
5 // ignore-sgx no libc
6 // ignore-emscripten no processes
7 // ignore-sgx no processes
8 // ignore-android: FIXME(#85261)
9
10 #![feature(bench_black_box)]
11 #![feature(rustc_private)]
12 #![feature(never_type)]
13 #![feature(panic_always_abort)]
14
15 extern crate libc;
16
17 use std::alloc::{GlobalAlloc, Layout};
18 use std::fmt;
19 use std::panic::{self, panic_any};
20 use std::os::unix::process::{CommandExt, ExitStatusExt};
21 use std::process::{self, Command, ExitStatus};
22 use std::sync::atomic::{AtomicU32, Ordering};
23
24 use libc::c_int;
25
26 /// This stunt allocator allows us to spot heap allocations in the child.
27 struct PidChecking<A> {
28     parent: A,
29     require_pid: AtomicU32,
30 }
31
32 #[global_allocator]
33 static ALLOCATOR: PidChecking<std::alloc::System> = PidChecking {
34     parent: std::alloc::System,
35     require_pid: AtomicU32::new(0),
36 };
37
38 impl<A> PidChecking<A> {
39     fn engage(&self) {
40         let parent_pid = process::id();
41         eprintln!("engaging allocator trap, parent pid={}", parent_pid);
42         self.require_pid.store(parent_pid, Ordering::Release);
43     }
44     fn check(&self) {
45         let require_pid = self.require_pid.load(Ordering::Acquire);
46         if require_pid != 0 {
47             let actual_pid = process::id();
48             if require_pid != actual_pid {
49                 unsafe {
50                     libc::raise(libc::SIGUSR1);
51                 }
52             }
53         }
54     }
55 }
56
57 unsafe impl<A:GlobalAlloc> GlobalAlloc for PidChecking<A> {
58     unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
59         self.check();
60         self.parent.alloc(layout)
61     }
62
63     unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
64         self.check();
65         self.parent.dealloc(ptr, layout)
66     }
67
68     unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
69         self.check();
70         self.parent.alloc_zeroed(layout)
71     }
72
73     unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
74         self.check();
75         self.parent.realloc(ptr, layout, new_size)
76     }
77 }
78
79 fn expect_aborted(status: ExitStatus) {
80     dbg!(status);
81     let signal = status.signal().expect("expected child process to die of signal");
82     assert!(signal == libc::SIGABRT || signal == libc::SIGILL || signal == libc::SIGTRAP);
83 }
84
85 fn main() {
86     ALLOCATOR.engage();
87
88     fn run(do_panic: &dyn Fn()) -> ExitStatus {
89         let child = unsafe { libc::fork() };
90         assert!(child >= 0);
91         if child == 0 {
92             panic::always_abort();
93             do_panic();
94             process::exit(0);
95         }
96         let mut status: c_int = 0;
97         let got = unsafe { libc::waitpid(child, &mut status, 0) };
98         assert_eq!(got, child);
99         let status = ExitStatus::from_raw(status.into());
100         status
101     }
102
103     fn one(do_panic: &dyn Fn()) {
104         let status = run(do_panic);
105         expect_aborted(status);
106     }
107
108     one(&|| panic!());
109     one(&|| panic!("some message"));
110     one(&|| panic!("message with argument: {}", 42));
111
112     #[derive(Debug)]
113     struct Wotsit { }
114     one(&|| panic_any(Wotsit { }));
115
116     let mut c = Command::new("echo");
117     unsafe {
118         c.pre_exec(|| panic!("{}", "crash now!"));
119     }
120     let st = c.status().expect("failed to get command status");
121     expect_aborted(st);
122
123     struct DisplayWithHeap;
124     impl fmt::Display for DisplayWithHeap {
125         fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
126             let s = vec![0; 100];
127             let s = std::hint::black_box(s);
128             write!(f, "{:?}", s)
129         }
130     }
131
132     // Some panics in the stdlib that we want not to allocate, as
133     // otherwise these facilities become impossible to use in the
134     // child after fork, which is really quite awkward.
135
136     one(&|| { None::<DisplayWithHeap>.unwrap(); });
137     one(&|| { None::<DisplayWithHeap>.expect("unwrapped a none"); });
138     one(&|| { std::str::from_utf8(b"\xff").unwrap(); });
139     one(&|| {
140         let x = [0, 1, 2, 3];
141         let y = x[std::hint::black_box(4)];
142         let _z = std::hint::black_box(y);
143     });
144
145     // Finally, check that our stunt allocator can actually catch an allocation after fork.
146     // ie, that our test is effective.
147
148     let status = run(&|| panic!("allocating to display... {}", DisplayWithHeap));
149     dbg!(status);
150     assert_eq!(status.signal(), Some(libc::SIGUSR1));
151 }