✅ Leave no core of a death test's child

A death test's child dies on purpose, and systemd-coredump keeps every
core it is given, so AGENTS.md runs the Linux tests at ulimit -c 0. A
leg that lifts the limit to catch a test process's own crash (the
NVIDIA exit SIGSEGV, which leaves nothing to read at 0) would then keep
a core of every child as well.

Each child now sets its own soft RLIMIT_CORE to 0 when it enters the
harness (Linux and macOS; Windows has no core limit). The test process
keeps whatever limit it was given. a_child_leaves_no_core raises its
own limit first, so it fails if the child inherits it. Nothing changes
for the guest or the host: this is the testkit alone.
This commit is contained in:
Anees Iqbal committed 2026-11-18 09:17:43 -08:00
1 parent aad99b5f8b
commit 3eba3d087b
3 files changed
+100 -1

No files matched your search

@@ -476,7 +476,8 @@ fn child_main(body: impl FnOnce(&Child)) {
child.finish();
}
/// Makes this process a child of [`spawn_command`]: maps the scratch file [`ENV_FILE`] names, turns
/// Makes this process a child of [`spawn_command`]: on Unix sets its soft core limit to 0 (a death
/// leaves no core, `sys::dump_no_core`), maps the scratch file [`ENV_FILE`] names, turns
/// the capture off (a death aborts as in production), gives the calling thread an alternate signal
/// stack its nested handlers fit on (`sys::widen_alternate_stack`), arms the death marker's
/// signal handlers, and records the calling thread as the body's. A child that is not a libtest test (a tool's binary)
@@ -484,6 +485,8 @@ fn child_main(body: impl FnOnce(&Child)) {
/// [`Child::finish`] if the body returns: a body that panics unwinds past the `Child`, whose `Drop`
/// records it (the test then fails with `f`'s panic).
pub fn enter() -> Child {
#[cfg(unix)]
sys::dump_no_core();
let Some(path) = std::env::var_os(ENV_FILE) else {
panic!("death harness: {ENV_FILE} is not set");
};
@@ -1688,6 +1691,26 @@ mod tests {
}
);
/// A child leaves no core when it dies: its soft core limit is 0 though its parent's is not.
/// The parent first sets its own to 1 KiB, or to its hard limit where that is lower: not 0,
/// so the child's 0 is the harness's doing, and too small for a core of the parent's own.
#[cfg(unix)]
#[test]
fn a_child_leaves_no_core() {
sys::set_core_limit(1024);
run(
Expect::Death,
Options::default(),
module_path!(),
"a_child_leaves_no_core",
|t| {
t.op(&format!("core limit {:?}", sys::core_limit()));
abort()
},
|r| assert_eq!(r.died().op.as_deref(), Some("core limit Some(0)")),
);
}
death_test!(
/// A death under a held lock never lets a blocked second thread record
a_death_under_a_held_lock_never_lets_a_blocked_thread_record,
@@ -19,6 +19,8 @@ pub const SA_ONSTACK: c_int = 0x0800_0000;
pub const SA_SIGINFO: c_int = 0x0004;
pub const SIG_DFL: usize = 0;
pub const SIG_IGN: usize = 1;
/// `RLIMIT_CORE`: the largest core file the process may leave.
const RLIMIT_CORE: c_int = 4;
/// `struct sigaction`'s handler union.
#[repr(C)]
@@ -55,6 +57,13 @@ impl SigAction {
}
}
/// glibc's `struct rlimit` (`rlim_t` is 64 bits).
#[repr(C)]
struct Rlimit {
cur: u64,
max: u64,
}
/// glibc's `stack_t`.
#[repr(C)]
pub struct StackT {
@@ -81,6 +90,35 @@ unsafe extern "C" {
pub safe fn _exit(status: c_int) -> !;
safe fn gettid() -> c_int;
fn sigaltstack(stack: *const StackT, old: *mut StackT) -> c_int;
fn getrlimit(resource: c_int, limit: *mut Rlimit) -> c_int;
fn setrlimit(resource: c_int, limit: *const Rlimit) -> c_int;
}
/// Leaves no core file when this process dies: the soft `RLIMIT_CORE` set to 0, the hard limit
/// kept. A harness child dies on purpose, and systemd-coredump keeps every core it is given, so a
/// run that lifts the limit to catch a test process's own crash gets no core of a child's.
pub fn dump_no_core() {
set_core_limit(0);
}
/// Sets the soft `RLIMIT_CORE` to `soft` bytes, or to the hard limit where that is lower.
pub fn set_core_limit(soft: u64) {
let mut limit = Rlimit { cur: 0, max: 0 };
// SAFETY: the call writes one `Rlimit`.
if unsafe { getrlimit(RLIMIT_CORE, &raw mut limit) } != 0 {
return;
}
limit.cur = soft.min(limit.max);
// SAFETY: `limit` is an `Rlimit` whose hard limit is the process's own.
let _ = unsafe { setrlimit(RLIMIT_CORE, &raw const limit) };
}
/// The soft `RLIMIT_CORE`, or `None` where the call fails.
#[cfg(test)]
pub fn core_limit() -> Option<u64> {
let mut limit = Rlimit { cur: 0, max: 0 };
// SAFETY: the call writes one `Rlimit`.
(unsafe { getrlimit(RLIMIT_CORE, &raw mut limit) } == 0).then_some(limit.cur)
}
/// The calling thread's system-wide id (`gettid`), without allocating.
@@ -16,6 +16,8 @@ pub const SA_ONSTACK: c_int = 0x0001;
pub const SA_SIGINFO: c_int = 0x0040;
pub const SIG_DFL: usize = 0;
pub const SIG_IGN: usize = 1;
/// `RLIMIT_CORE`: the largest core file the process may leave.
const RLIMIT_CORE: c_int = 4;
/// `struct sigaction`'s handler union.
#[repr(C)]
@@ -50,6 +52,13 @@ impl SigAction {
}
}
/// Darwin's `struct rlimit` (`rlim_t` is 64 bits).
#[repr(C)]
struct Rlimit {
cur: u64,
max: u64,
}
unsafe extern "C" {
pub fn mmap(
addr: *mut c_void,
@@ -66,6 +75,35 @@ unsafe extern "C" {
pub fn pthread_threadid_np(thread: *mut c_void, id: *mut u64) -> c_int;
pub fn pthread_getname_np(thread: *mut c_void, name: *mut c_char, len: usize) -> c_int;
pub safe fn _exit(status: c_int) -> !;
fn getrlimit(resource: c_int, limit: *mut Rlimit) -> c_int;
fn setrlimit(resource: c_int, limit: *const Rlimit) -> c_int;
}
/// Leaves no core file when this process dies: the soft `RLIMIT_CORE` set to 0, the hard limit
/// kept. A harness child dies on purpose, and systemd-coredump keeps every core it is given, so a
/// run that lifts the limit to catch a test process's own crash gets no core of a child's.
pub fn dump_no_core() {
set_core_limit(0);
}
/// Sets the soft `RLIMIT_CORE` to `soft` bytes, or to the hard limit where that is lower.
pub fn set_core_limit(soft: u64) {
let mut limit = Rlimit { cur: 0, max: 0 };
// SAFETY: the call writes one `Rlimit`.
if unsafe { getrlimit(RLIMIT_CORE, &raw mut limit) } != 0 {
return;
}
limit.cur = soft.min(limit.max);
// SAFETY: `limit` is an `Rlimit` whose hard limit is the process's own.
let _ = unsafe { setrlimit(RLIMIT_CORE, &raw const limit) };
}
/// The soft `RLIMIT_CORE`, or `None` where the call fails.
#[cfg(test)]
pub fn core_limit() -> Option<u64> {
let mut limit = Rlimit { cur: 0, max: 0 };
// SAFETY: the call writes one `Rlimit`.
(unsafe { getrlimit(RLIMIT_CORE, &raw mut limit) } == 0).then_some(limit.cur)
}
/// The calling thread's system-wide id (`pthread_threadid_np`), without allocating.