3 // ignore-wasm32-bare no libc
6 // ignore-emscripten no processes
7 // ignore-sgx no processes
8 // ignore-android: FIXME(#85261)
10 #![feature(bench_black_box)]
11 #![feature(rustc_private)]
12 #![feature(never_type)]
13 #![feature(panic_always_abort)]
17 use std::alloc::{GlobalAlloc, Layout};
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};
26 #[cfg(not(target_os = "linux"))]
31 /// We need to directly use the getpid syscall instead of using `process::id()`
32 /// because the libc wrapper might return incorrect values after a process was
34 #[cfg(target_os = "linux")]
37 libc::syscall(libc::SYS_getpid) as _
41 /// This stunt allocator allows us to spot heap allocations in the child.
42 struct PidChecking<A> {
44 require_pid: AtomicU32,
48 static ALLOCATOR: PidChecking<std::alloc::System> = PidChecking {
49 parent: std::alloc::System,
50 require_pid: AtomicU32::new(0),
53 impl<A> PidChecking<A> {
55 let parent_pid = process::id();
56 eprintln!("engaging allocator trap, parent pid={}", parent_pid);
57 self.require_pid.store(parent_pid, Ordering::Release);
60 let require_pid = self.require_pid.load(Ordering::Acquire);
62 let actual_pid = getpid();
63 if require_pid != actual_pid {
65 libc::raise(libc::SIGUSR1);
72 unsafe impl<A:GlobalAlloc> GlobalAlloc for PidChecking<A> {
73 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
75 self.parent.alloc(layout)
78 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
80 self.parent.dealloc(ptr, layout)
83 unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
85 self.parent.alloc_zeroed(layout)
88 unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
90 self.parent.realloc(ptr, layout, new_size)
94 fn expect_aborted(status: ExitStatus) {
96 let signal = status.signal().expect("expected child process to die of signal");
97 assert!(signal == libc::SIGABRT || signal == libc::SIGILL || signal == libc::SIGTRAP);
103 fn run(do_panic: &dyn Fn()) -> ExitStatus {
104 let child = unsafe { libc::fork() };
107 panic::always_abort();
111 let mut status: c_int = 0;
112 let got = unsafe { libc::waitpid(child, &mut status, 0) };
113 assert_eq!(got, child);
114 let status = ExitStatus::from_raw(status.into());
118 fn one(do_panic: &dyn Fn()) {
119 let status = run(do_panic);
120 expect_aborted(status);
124 one(&|| panic!("some message"));
125 one(&|| panic!("message with argument: {}", 42));
129 one(&|| panic_any(Wotsit { }));
131 let mut c = Command::new("echo");
133 c.pre_exec(|| panic!("{}", "crash now!"));
135 let st = c.status().expect("failed to get command status");
138 struct DisplayWithHeap;
139 impl fmt::Display for DisplayWithHeap {
140 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
141 let s = vec![0; 100];
142 let s = std::hint::black_box(s);
147 // Some panics in the stdlib that we want not to allocate, as
148 // otherwise these facilities become impossible to use in the
149 // child after fork, which is really quite awkward.
151 one(&|| { None::<DisplayWithHeap>.unwrap(); });
152 one(&|| { None::<DisplayWithHeap>.expect("unwrapped a none"); });
153 one(&|| { std::str::from_utf8(b"\xff").unwrap(); });
155 let x = [0, 1, 2, 3];
156 let y = x[std::hint::black_box(4)];
157 let _z = std::hint::black_box(y);
160 // Finally, check that our stunt allocator can actually catch an allocation after fork.
161 // ie, that our test is effective.
163 let status = run(&|| panic!("allocating to display... {}", DisplayWithHeap));
165 assert_eq!(status.signal(), Some(libc::SIGUSR1));