diff options
Diffstat (limited to '')
-rw-r--r-- | tests/ui/process-termination/process-termination-blocking-io.rs (renamed from src/test/ui/process-termination/process-termination-blocking-io.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process-termination/process-termination-simple.rs (renamed from src/test/ui/process-termination/process-termination-simple.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/core-run-destroy.rs (renamed from src/test/ui/process/core-run-destroy.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/fds-are-cloexec.rs (renamed from src/test/ui/process/fds-are-cloexec.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/issue-13304.rs (renamed from src/test/ui/process/issue-13304.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/issue-14456.rs (renamed from src/test/ui/process/issue-14456.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/issue-14940.rs (renamed from src/test/ui/process/issue-14940.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/issue-16272.rs (renamed from src/test/ui/process/issue-16272.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/issue-20091.rs (renamed from src/test/ui/process/issue-20091.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/multi-panic.rs (renamed from src/test/ui/process/multi-panic.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/no-stdio.rs (renamed from src/test/ui/process/no-stdio.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/nofile-limit.rs (renamed from src/test/ui/process/nofile-limit.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/process-envs.rs (renamed from src/test/ui/process/process-envs.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/process-exit.rs (renamed from src/test/ui/process/process-exit.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/process-panic-after-fork.rs | 197 | ||||
-rw-r--r-- | tests/ui/process/process-remove-from-env.rs (renamed from src/test/ui/process/process-remove-from-env.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/process-sigpipe.rs (renamed from src/test/ui/process/process-sigpipe.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/process-spawn-nonexistent.rs (renamed from src/test/ui/process/process-spawn-nonexistent.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/process-spawn-with-unicode-params.rs (renamed from src/test/ui/process/process-spawn-with-unicode-params.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/process-status-inherits-stdin.rs (renamed from src/test/ui/process/process-status-inherits-stdin.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/signal-exit-status.rs (renamed from src/test/ui/process/signal-exit-status.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/sigpipe-should-be-ignored.rs (renamed from src/test/ui/process/sigpipe-should-be-ignored.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/tls-exit-status.rs (renamed from src/test/ui/process/tls-exit-status.rs) | 0 | ||||
-rw-r--r-- | tests/ui/process/try-wait.rs (renamed from src/test/ui/process/try-wait.rs) | 0 |
24 files changed, 197 insertions, 0 deletions
diff --git a/src/test/ui/process-termination/process-termination-blocking-io.rs b/tests/ui/process-termination/process-termination-blocking-io.rs index b2dab5c93..b2dab5c93 100644 --- a/src/test/ui/process-termination/process-termination-blocking-io.rs +++ b/tests/ui/process-termination/process-termination-blocking-io.rs diff --git a/src/test/ui/process-termination/process-termination-simple.rs b/tests/ui/process-termination/process-termination-simple.rs index 8f2e5b94c..8f2e5b94c 100644 --- a/src/test/ui/process-termination/process-termination-simple.rs +++ b/tests/ui/process-termination/process-termination-simple.rs diff --git a/src/test/ui/process/core-run-destroy.rs b/tests/ui/process/core-run-destroy.rs index d0e97bf01..d0e97bf01 100644 --- a/src/test/ui/process/core-run-destroy.rs +++ b/tests/ui/process/core-run-destroy.rs diff --git a/src/test/ui/process/fds-are-cloexec.rs b/tests/ui/process/fds-are-cloexec.rs index 4482b7032..4482b7032 100644 --- a/src/test/ui/process/fds-are-cloexec.rs +++ b/tests/ui/process/fds-are-cloexec.rs diff --git a/src/test/ui/process/issue-13304.rs b/tests/ui/process/issue-13304.rs index b10f6d572..b10f6d572 100644 --- a/src/test/ui/process/issue-13304.rs +++ b/tests/ui/process/issue-13304.rs diff --git a/src/test/ui/process/issue-14456.rs b/tests/ui/process/issue-14456.rs index 52a56eb77..52a56eb77 100644 --- a/src/test/ui/process/issue-14456.rs +++ b/tests/ui/process/issue-14456.rs diff --git a/src/test/ui/process/issue-14940.rs b/tests/ui/process/issue-14940.rs index 98a4af0c4..98a4af0c4 100644 --- a/src/test/ui/process/issue-14940.rs +++ b/tests/ui/process/issue-14940.rs diff --git a/src/test/ui/process/issue-16272.rs b/tests/ui/process/issue-16272.rs index 5cf3fd949..5cf3fd949 100644 --- a/src/test/ui/process/issue-16272.rs +++ b/tests/ui/process/issue-16272.rs diff --git a/src/test/ui/process/issue-20091.rs b/tests/ui/process/issue-20091.rs index 86cc79d6b..86cc79d6b 100644 --- a/src/test/ui/process/issue-20091.rs +++ b/tests/ui/process/issue-20091.rs diff --git a/src/test/ui/process/multi-panic.rs b/tests/ui/process/multi-panic.rs index a1887c218..a1887c218 100644 --- a/src/test/ui/process/multi-panic.rs +++ b/tests/ui/process/multi-panic.rs diff --git a/src/test/ui/process/no-stdio.rs b/tests/ui/process/no-stdio.rs index 24985386a..24985386a 100644 --- a/src/test/ui/process/no-stdio.rs +++ b/tests/ui/process/no-stdio.rs diff --git a/src/test/ui/process/nofile-limit.rs b/tests/ui/process/nofile-limit.rs index 3ddf8d6ef..3ddf8d6ef 100644 --- a/src/test/ui/process/nofile-limit.rs +++ b/tests/ui/process/nofile-limit.rs diff --git a/src/test/ui/process/process-envs.rs b/tests/ui/process/process-envs.rs index f3a469791..f3a469791 100644 --- a/src/test/ui/process/process-envs.rs +++ b/tests/ui/process/process-envs.rs diff --git a/src/test/ui/process/process-exit.rs b/tests/ui/process/process-exit.rs index d193e073e..d193e073e 100644 --- a/src/test/ui/process/process-exit.rs +++ b/tests/ui/process/process-exit.rs diff --git a/tests/ui/process/process-panic-after-fork.rs b/tests/ui/process/process-panic-after-fork.rs new file mode 100644 index 000000000..da2683121 --- /dev/null +++ b/tests/ui/process/process-panic-after-fork.rs @@ -0,0 +1,197 @@ +// run-pass +// no-prefer-dynamic +// ignore-wasm32-bare no libc +// ignore-windows +// ignore-sgx no libc +// ignore-emscripten no processes +// ignore-sgx no processes +// ignore-fuchsia no fork + +#![feature(rustc_private)] +#![feature(never_type)] +#![feature(panic_always_abort)] + +extern crate libc; + +use std::alloc::{GlobalAlloc, Layout}; +use std::fmt; +use std::panic::{self, panic_any}; +use std::os::unix::process::{CommandExt, ExitStatusExt}; +use std::process::{self, Command, ExitStatus}; +use std::sync::atomic::{AtomicU32, Ordering}; + +use libc::c_int; + +/// This stunt allocator allows us to spot heap allocations in the child. +struct PidChecking<A> { + parent: A, + require_pid: AtomicU32, +} + +#[global_allocator] +static ALLOCATOR: PidChecking<std::alloc::System> = PidChecking { + parent: std::alloc::System, + require_pid: AtomicU32::new(0), +}; + +impl<A> PidChecking<A> { + fn engage(&self) { + let parent_pid = process::id(); + eprintln!("engaging allocator trap, parent pid={}", parent_pid); + self.require_pid.store(parent_pid, Ordering::Release); + } + fn check(&self) { + let require_pid = self.require_pid.load(Ordering::Acquire); + if require_pid != 0 { + let actual_pid = process::id(); + if require_pid != actual_pid { + unsafe { + libc::raise(libc::SIGUSR1); + } + } + } + } +} + +unsafe impl<A:GlobalAlloc> GlobalAlloc for PidChecking<A> { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + self.check(); + self.parent.alloc(layout) + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + self.check(); + self.parent.dealloc(ptr, layout) + } + + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { + self.check(); + self.parent.alloc_zeroed(layout) + } + + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { + self.check(); + self.parent.realloc(ptr, layout, new_size) + } +} + +fn expect_aborted(status: ExitStatus) { + dbg!(status); + let signal = status.signal().expect("expected child process to die of signal"); + + #[cfg(not(target_os = "android"))] + assert!(signal == libc::SIGABRT || signal == libc::SIGILL || signal == libc::SIGTRAP); + + #[cfg(target_os = "android")] + { + assert!(signal == libc::SIGABRT || signal == libc::SIGSEGV); + + if signal == libc::SIGSEGV { + // Pre-KitKat versions of Android signal an abort() with SIGSEGV at address 0xdeadbaad + // See e.g. https://groups.google.com/g/android-ndk/c/laW1CJc7Icc + // + // This behavior was changed in KitKat to send a standard SIGABRT signal. + // See: https://r.android.com/60341 + // + // Additional checks performed: + // 1. Find last tombstone (similar to coredump but in text format) from the + // same executable (path) as we are (must be because of usage of fork): + // This ensures that we look into the correct tombstone. + // 2. Cause of crash is a SIGSEGV with address 0xdeadbaad. + // 3. libc::abort call is in one of top two functions on callstack. + // The last two steps distinguish between a normal SIGSEGV and one caused + // by libc::abort. + + let this_exe = std::env::current_exe().unwrap().into_os_string().into_string().unwrap(); + let exe_string = format!(">>> {this_exe} <<<"); + let tombstone = (0..100) + .map(|n| format!("/data/tombstones/tombstone_{n:02}")) + .filter(|f| std::path::Path::new(&f).exists()) + .map(|f| std::fs::read_to_string(&f).expect("Cannot read tombstone file")) + .filter(|f| f.contains(&exe_string)) + .last() + .expect("no tombstone found"); + + println!("Content of tombstone:\n{tombstone}"); + + assert!(tombstone + .contains("signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr deadbaad")); + let abort_on_top = tombstone + .lines() + .skip_while(|l| !l.contains("backtrace:")) + .skip(1) + .take_while(|l| l.starts_with(" #")) + .take(2) + .any(|f| f.contains("/system/lib/libc.so (abort")); + assert!(abort_on_top); + } + } +} + +fn main() { + ALLOCATOR.engage(); + + fn run(do_panic: &dyn Fn()) -> ExitStatus { + let child = unsafe { libc::fork() }; + assert!(child >= 0); + if child == 0 { + panic::always_abort(); + do_panic(); + process::exit(0); + } + let mut status: c_int = 0; + let got = unsafe { libc::waitpid(child, &mut status, 0) }; + assert_eq!(got, child); + let status = ExitStatus::from_raw(status.into()); + status + } + + fn one(do_panic: &dyn Fn()) { + let status = run(do_panic); + expect_aborted(status); + } + + one(&|| panic!()); + one(&|| panic!("some message")); + one(&|| panic!("message with argument: {}", 42)); + + #[derive(Debug)] + struct Wotsit { } + one(&|| panic_any(Wotsit { })); + + let mut c = Command::new("echo"); + unsafe { + c.pre_exec(|| panic!("{}", "crash now!")); + } + let st = c.status().expect("failed to get command status"); + expect_aborted(st); + + struct DisplayWithHeap; + impl fmt::Display for DisplayWithHeap { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + let s = vec![0; 100]; + let s = std::hint::black_box(s); + write!(f, "{:?}", s) + } + } + + // Some panics in the stdlib that we want not to allocate, as + // otherwise these facilities become impossible to use in the + // child after fork, which is really quite awkward. + + one(&|| { None::<DisplayWithHeap>.unwrap(); }); + one(&|| { None::<DisplayWithHeap>.expect("unwrapped a none"); }); + one(&|| { std::str::from_utf8(b"\xff").unwrap(); }); + one(&|| { + let x = [0, 1, 2, 3]; + let y = x[std::hint::black_box(4)]; + let _z = std::hint::black_box(y); + }); + + // Finally, check that our stunt allocator can actually catch an allocation after fork. + // ie, that our test is effective. + + let status = run(&|| panic!("allocating to display... {}", DisplayWithHeap)); + dbg!(status); + assert_eq!(status.signal(), Some(libc::SIGUSR1)); +} diff --git a/src/test/ui/process/process-remove-from-env.rs b/tests/ui/process/process-remove-from-env.rs index ad027d685..ad027d685 100644 --- a/src/test/ui/process/process-remove-from-env.rs +++ b/tests/ui/process/process-remove-from-env.rs diff --git a/src/test/ui/process/process-sigpipe.rs b/tests/ui/process/process-sigpipe.rs index 107eba45d..107eba45d 100644 --- a/src/test/ui/process/process-sigpipe.rs +++ b/tests/ui/process/process-sigpipe.rs diff --git a/src/test/ui/process/process-spawn-nonexistent.rs b/tests/ui/process/process-spawn-nonexistent.rs index 9dd608986..9dd608986 100644 --- a/src/test/ui/process/process-spawn-nonexistent.rs +++ b/tests/ui/process/process-spawn-nonexistent.rs diff --git a/src/test/ui/process/process-spawn-with-unicode-params.rs b/tests/ui/process/process-spawn-with-unicode-params.rs index 16dba6292..16dba6292 100644 --- a/src/test/ui/process/process-spawn-with-unicode-params.rs +++ b/tests/ui/process/process-spawn-with-unicode-params.rs diff --git a/src/test/ui/process/process-status-inherits-stdin.rs b/tests/ui/process/process-status-inherits-stdin.rs index 7719dd9ad..7719dd9ad 100644 --- a/src/test/ui/process/process-status-inherits-stdin.rs +++ b/tests/ui/process/process-status-inherits-stdin.rs diff --git a/src/test/ui/process/signal-exit-status.rs b/tests/ui/process/signal-exit-status.rs index 9519ed7b4..9519ed7b4 100644 --- a/src/test/ui/process/signal-exit-status.rs +++ b/tests/ui/process/signal-exit-status.rs diff --git a/src/test/ui/process/sigpipe-should-be-ignored.rs b/tests/ui/process/sigpipe-should-be-ignored.rs index 144eeca23..144eeca23 100644 --- a/src/test/ui/process/sigpipe-should-be-ignored.rs +++ b/tests/ui/process/sigpipe-should-be-ignored.rs diff --git a/src/test/ui/process/tls-exit-status.rs b/tests/ui/process/tls-exit-status.rs index 6296e5042..6296e5042 100644 --- a/src/test/ui/process/tls-exit-status.rs +++ b/tests/ui/process/tls-exit-status.rs diff --git a/src/test/ui/process/try-wait.rs b/tests/ui/process/try-wait.rs index 692197210..692197210 100644 --- a/src/test/ui/process/try-wait.rs +++ b/tests/ui/process/try-wait.rs |