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 /// This stunt allocator allows us to spot heap allocations in the child.
27 struct PidChecking<A> {
29 require_pid: AtomicU32,
33 static ALLOCATOR: PidChecking<std::alloc::System> = PidChecking {
34 parent: std::alloc::System,
35 require_pid: AtomicU32::new(0),
38 impl<A> PidChecking<A> {
40 let parent_pid = process::id();
41 eprintln!("engaging allocator trap, parent pid={}", parent_pid);
42 self.require_pid.store(parent_pid, Ordering::Release);
45 let require_pid = self.require_pid.load(Ordering::Acquire);
47 let actual_pid = process::id();
48 if require_pid != actual_pid {
50 libc::raise(libc::SIGUSR1);
57 unsafe impl<A:GlobalAlloc> GlobalAlloc for PidChecking<A> {
58 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
60 self.parent.alloc(layout)
63 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
65 self.parent.dealloc(ptr, layout)
68 unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
70 self.parent.alloc_zeroed(layout)
73 unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
75 self.parent.realloc(ptr, layout, new_size)
79 fn expect_aborted(status: ExitStatus) {
81 let signal = status.signal().expect("expected child process to die of signal");
82 assert!(signal == libc::SIGABRT || signal == libc::SIGILL || signal == libc::SIGTRAP);
88 fn run(do_panic: &dyn Fn()) -> ExitStatus {
89 let child = unsafe { libc::fork() };
92 panic::always_abort();
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());
103 fn one(do_panic: &dyn Fn()) {
104 let status = run(do_panic);
105 expect_aborted(status);
109 one(&|| panic!("some message"));
110 one(&|| panic!("message with argument: {}", 42));
114 one(&|| panic_any(Wotsit { }));
116 let mut c = Command::new("echo");
118 c.pre_exec(|| panic!("{}", "crash now!"));
120 let st = c.status().expect("failed to get command status");
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);
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.
136 one(&|| { None::<DisplayWithHeap>.unwrap(); });
137 one(&|| { None::<DisplayWithHeap>.expect("unwrapped a none"); });
138 one(&|| { std::str::from_utf8(b"\xff").unwrap(); });
140 let x = [0, 1, 2, 3];
141 let y = x[std::hint::black_box(4)];
142 let _z = std::hint::black_box(y);
145 // Finally, check that our stunt allocator can actually catch an allocation after fork.
146 // ie, that our test is effective.
148 let status = run(&|| panic!("allocating to display... {}", DisplayWithHeap));
150 assert_eq!(status.signal(), Some(libc::SIGUSR1));