From f700b76f16c086a5ec40e5c6c26a299c197f1626 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 17:01:25 -0800 Subject: [PATCH 1/3] chore(quest): claim quest/m0/ffi-abort-cores.md Quest-Claim: f2f899f358f2841a From 0bcfbb7c2c47f141794a9c00478ca1a5188fa612 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 17:18:13 -0800 Subject: [PATCH 2/3] test(ffi): expected aborts leave no core dump Each test program marks itself non-dumpable at its broken-contract entry on Linux, so the kernel never starts the host's crash handler; the Kotlin JVM, which cannot call prctl on JDK 21, spawns its cases with RLIMIT_CORE at 0. Every broken case outside Kotlin now asserts its status reports no core. Co-Authored-By: Claude Opus 5.5 --- ffi/tests/api.c | 12 ++++ ffi/tests/api.cpp | 12 ++++ ffi/tests/api.dart | 10 +++ ffi/tests/api.py | 7 ++ ffi/tests/api.swift | 9 +++ ffi/tests/fixtures.rs | 109 ++++++++++++++++++++++++------ ffi/tests/nodump/module.modulemap | 4 ++ ffi/tests/nodump/nodump.h | 9 +++ quest/m0/ffi-abort-cores.md | 71 ------------------- 9 files changed, 152 insertions(+), 91 deletions(-) create mode 100644 ffi/tests/nodump/module.modulemap create mode 100644 ffi/tests/nodump/nodump.h delete mode 100644 quest/m0/ffi-abort-cores.md diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 1c9632dce..88e024611 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -7,6 +7,10 @@ #include #include +#ifdef __linux__ +#include +#endif + #include "api.h" static api_buf str(const char *s) { @@ -1780,6 +1784,14 @@ static int script(void) { /* Each breaks a contract, which must abort with a message naming the function. */ static int broken(const char *which) { +#ifdef __linux__ + /* Non-dumpable, the expected abort writes no core and never starts the host's crash + * handler. */ + if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) != 0) { + perror("prctl"); + return 1; + } +#endif if (strcmp(which, "moved") == 0) { api_Counter *c = api_Counter_new(str("c"), 0); api_Label label = api_Counter_into_label(c, (api_option_Point){0}); diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index aa1b518de..fcebab36f 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -33,6 +33,10 @@ #include #endif +#ifdef __linux__ +#include +#endif + namespace { // Print a returned buffer. @@ -1657,6 +1661,14 @@ int lifetimes() { // `tests/api_strict.cpp` refuses at compile time reach the object through a reference or // pointer, whose state Clang does not track. int broken(const char *which) { +#ifdef __linux__ + // Non-dumpable, the expected abort writes no core and never starts the host's crash + // handler. + if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) != 0) { + std::perror("prctl"); + return 1; + } +#endif if (std::strcmp(which, "moved") == 0) { api::Counter c = api::Counter::new_("c", 0); api::Counter &same = c; diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index d81077978..445246196 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -7,6 +7,7 @@ import 'dart:async'; import 'dart:convert'; +import 'dart:ffi' as ffi; import 'dart:io'; import 'dart:typed_data'; @@ -1255,6 +1256,15 @@ final class Thrower implements api.SeekableReader { /// Each breaks a contract, which must fail with a message naming it. void broken(String which) { + if (Platform.isLinux) { + // Non-dumpable, an expected abort writes no core and never starts the host's crash + // handler. PR_SET_DUMPABLE is 4. + final prctl = ffi.DynamicLibrary.process().lookupFunction< + ffi.Int Function(ffi.Int, + ffi.VarArgs<(ffi.UnsignedLong, ffi.UnsignedLong, ffi.UnsignedLong, ffi.UnsignedLong)>), + int Function(int, int, int, int, int)>('prctl'); + check(prctl(4, 0, 0, 0, 0) == 0, 'prctl'); + } switch (which) { case 'moved': final c = api.Counter('c', 0); diff --git a/ffi/tests/api.py b/ffi/tests/api.py index d76582b4b..d5103dcb4 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -1357,6 +1357,13 @@ def raises(f, message): def broken(which): """Each breaks a contract, which must fail with a message naming it.""" + if sys.platform == "linux": + # Non-dumpable, an expected abort writes no core and never starts the host's crash + # handler. PR_SET_DUMPABLE is 4. + import ctypes + + if ctypes.CDLL(None, use_errno=True).prctl(4, 0, 0, 0, 0) != 0: + raise OSError(ctypes.get_errno(), "prctl") match which: case "moved": c = api.Counter.new("c", 0) diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index ae6108a2a..de2991926 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -7,6 +7,9 @@ import Api import BasicContainers import Foundation +#if os(Linux) +import NoDump +#endif // The Log callback: keeps even levels. final class PrintLog: Log { @@ -1226,6 +1229,12 @@ func lentKept() { // Each breaks a contract, which must fail with a message naming it. func broken(_ which: String) { + #if os(Linux) + if nodump() != 0 { + perror("prctl") + exit(1) + } + #endif switch which { // A lent return outliving its owner's change, which Rust checks. case "outlived": diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index c8ea43af0..a1d772a71 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -231,7 +231,7 @@ fn api_c() { ("zero", "api_next_hop: a NonZero is zero"), ] { let output = Command::new(&exe).arg(case).output().unwrap(); - assert!(!output.status.success(), "{case} did not abort"); + failed(&output, case); let stderr = String::from_utf8_lossy(&output.stderr); assert!(stderr.contains(message), "{case} printed {stderr}"); } @@ -383,7 +383,7 @@ fn api_cpp() { ("outlived", OUTLIVED), ] { let output = Command::new(&exe).arg(case).output().unwrap(); - assert!(!output.status.success(), "C++{standard} {case} did not abort"); + failed(&output, &format!("C++{standard} {case}")); let stderr = String::from_utf8_lossy(&output.stderr); assert!(stderr.contains(message), "C++{standard} {case} printed {stderr}"); } @@ -407,17 +407,34 @@ fn api_cpp() { run(&mut cxx(&api.link(), 17, true, &src, &exe)); } - // A C++ program whose header names another contract than its library's aborts at load. + // A C++ program whose header names another contract than its library's aborts at load, + // non-dumpable from a constructor that runs before the header's check, as `broken` is. let mismatched = scratch().join("api").join("contract.cpp"); std::fs::write( &mismatched, - "#include \"api.h\"\n#undef API_CONTRACT\n#define API_CONTRACT ((uint64_t)0)\n#include \"api.hpp\"\nint main() {}\n", + r#"#ifdef __linux__ +#include +#include +#include +__attribute__((constructor(101))) static void nodump() { + if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) != 0) { + std::perror("prctl"); + std::_Exit(1); + } +} +#endif +#include "api.h" +#undef API_CONTRACT +#define API_CONTRACT ((uint64_t)0) +#include "api.hpp" +int main() {} +"#, ) .unwrap(); let contract = scratch().join("api").join(format!("contract{EXE_SUFFIX}")); run(&mut cxx(&api.link(), 17, false, &mismatched, &contract)); let output = Command::new(&contract).output().unwrap(); - assert!(!output.status.success(), "a mismatched contract did not abort"); + failed(&output, "a mismatched contract"); let stderr = String::from_utf8_lossy(&output.stderr); assert!( stderr.contains("api/rt.hpp and the library are from different builds"), @@ -569,7 +586,7 @@ fn api_kotlin() { std::fs::write(&native_class, &mismatched).unwrap(); let output = java(&[]).output().unwrap(); std::fs::write(&native_class, &bytes).unwrap(); - assert!(!output.status.success(), "a mismatched contract did not fail"); + failed(&output, "a mismatched contract"); let stderr = String::from_utf8_lossy(&output.stderr); assert!( stderr.contains("api.kt and the library are from different builds"), @@ -742,13 +759,58 @@ fn kotlin(native: &str, vm: &dyn Fn(&[&str]) -> Command) { ("stray", "MalformedInputException"), ("family", "an IPv6 address has no Rust SocketAddrV4"), ] { - let output = vm(&[case]).output().unwrap(); + // Under a piped core pattern the handler still runs at a limit of 0, so the + // status reports a core though none is written. + let output = no_core(&mut vm(&[case])).output().unwrap(); assert!(!output.status.success(), "Kotlin {case} did not fail"); let stderr = String::from_utf8_lossy(&output.stderr); assert!(stderr.contains(message), "Kotlin {case} printed {stderr}"); } } +/// Asserts the run of `what` failed, as a broken contract must, without dumping core: each +/// core costs the host a write and a run of its crash handler. +fn failed(output: &Output, what: &str) { + assert!(!output.status.success(), "{what} did not fail"); + #[cfg(unix)] + assert!( + !std::os::unix::process::ExitStatusExt::core_dumped(&output.status), + "{what} dumped core" + ); +} + +/// `cmd` with a core-size limit of 0 on Linux, so a JVM case that aborts writes no core. The +/// JVM cannot mark itself non-dumpable as the other test programs do, since JDK 21 calls C +/// only through a preview API, so the host's crash handler still starts and logs the crash. +fn no_core(cmd: &mut Command) -> &mut Command { + #[cfg(target_os = "linux")] + { + use std::ffi::{c_int, c_ulong}; + use std::os::unix::process::CommandExt; + + #[repr(C)] + struct Rlimit { + cur: c_ulong, + max: c_ulong, + } + unsafe extern "C" { + fn setrlimit(resource: c_int, limit: *const Rlimit) -> c_int; + } + const RLIMIT_CORE: c_int = 4; + // SAFETY: setrlimit is async-signal-safe and touches only the child. + unsafe { + cmd.pre_exec(|| { + if setrlimit(RLIMIT_CORE, &Rlimit { cur: 0, max: 0 }) == 0 { + Ok(()) + } else { + Err(std::io::Error::last_os_error()) + } + }); + } + } + cmd +} + /// The library `python/api.py` loads from beside itself. const PYTHON_LIB: &str = match (cfg!(windows), cfg!(target_os = "macos")) { (true, _) => "api.dll", @@ -800,7 +862,7 @@ fn api_python() { ("reader kept", "read: a reader kept a view of its buffer past the read"), ] { let output = python(&dir, &[case]).output().unwrap(); - assert!(!output.status.success(), "Python {case} did not fail"); + failed(&output, &format!("Python {case}")); let stderr = String::from_utf8_lossy(&output.stderr); assert!(stderr.contains(message), "Python {case} printed {stderr}"); } @@ -823,7 +885,7 @@ fn api_python() { let source = format!("{}{flipped}{}", &source[..at], &source[at + 1..]); std::fs::write(mismatched.join("api.py"), source).unwrap(); let output = python(&mismatched, &[]).output().unwrap(); - assert!(!output.status.success(), "a mismatched contract did not fail"); + failed(&output, "a mismatched contract"); let stderr = String::from_utf8_lossy(&output.stderr); assert!( stderr.contains("api.py and the library are from different builds"), @@ -943,7 +1005,7 @@ fn api_swift() { ("plain variant", "api_Family.Wasm: it is not on this library's target"), ] { let output = Command::new(&exe).arg(case).output().unwrap(); - assert!(!output.status.success(), "Swift {case} did not fail"); + failed(&output, &format!("Swift {case}")); let stderr = String::from_utf8_lossy(&output.stderr); assert!(stderr.contains(message), "Swift {case} printed {stderr}"); } @@ -961,7 +1023,12 @@ fn api_swift() { let contract = dir.join("contract"); std::fs::create_dir_all(&contract).unwrap(); std::fs::write(contract.join("api.swift"), mismatched).unwrap(); - std::fs::write(contract.join("main.swift"), "_ = dropped()\n").unwrap(); + // Non-dumpable before the check fails, as `broken` is. + std::fs::write( + contract.join("main.swift"), + "#if os(Linux)\nimport Glibc\nimport NoDump\n\nif nodump() != 0 {\n\tperror(\"prctl\")\n\texit(1)\n}\n#endif\n_ = dropped()\n", + ) + .unwrap(); let exe = contract.join("contract"); run(swiftc(&module, 13) .args(["-module-name", "Api"]) @@ -973,7 +1040,7 @@ fn api_swift() { .arg("-o") .arg(&exe)); let output = Command::new(&exe).output().unwrap(); - assert!(!output.status.success(), "a mismatched contract did not fail"); + failed(&output, "a mismatched contract"); let stderr = String::from_utf8_lossy(&output.stderr); assert!( stderr.contains("api.swift and the library are from different builds"), @@ -1032,7 +1099,7 @@ fn api_dart() { ("lent stray", "a path from Rust is not UTF-8"), ] { let output = dart_run(&exe, &[case]).output().unwrap(); - assert!(!output.status.success(), "Dart {case} did not fail"); + failed(&output, &format!("Dart {case}")); let stderr = String::from_utf8_lossy(&output.stderr); assert!(stderr.contains(message), "Dart {case} printed {stderr}"); } @@ -1055,7 +1122,7 @@ fn api_dart() { .unwrap(); let exe = dart_compile("api", &contract, &contract, &lib, &main); let output = dart_run(&exe, &[]).output().unwrap(); - assert!(!output.status.success(), "a mismatched contract did not fail"); + failed(&output, "a mismatched contract"); let stderr = String::from_utf8_lossy(&output.stderr); assert!( stderr.contains("api.dart and the library are from different builds"), @@ -1164,9 +1231,9 @@ fn collections_dir() -> PathBuf { manifest_dir().join("target").join("swift-collections").join("build") } -/// swiftc, with warnings as errors, finding the C header's module in `module` and -/// swift-collections' in [`collections_dir`], at this macOS deployment target on macOS: 13 -/// is the oldest the generated `Swift.Duration` allows. +/// swiftc, with warnings as errors, finding the C header's module in `module`, +/// swift-collections' in [`collections_dir`], and `tests/nodump`'s, at this macOS deployment +/// target on macOS: 13 is the oldest the generated `Swift.Duration` allows. fn swiftc(module: &Path, macos: u8) -> Command { let mut cmd = Command::new("swiftc"); if cfg!(target_os = "macos") { @@ -1179,7 +1246,9 @@ fn swiftc(module: &Path, macos: u8) -> Command { cmd.args(["-warnings-as-errors", "-I"]) .arg(module) .arg("-I") - .arg(collections_dir()); + .arg(collections_dir()) + .arg("-I") + .arg(manifest_dir().join("tests").join("nodump")); cmd } @@ -1239,7 +1308,7 @@ fn api_go() { ("plain variant", "api_Family.Wasm: it is not on this library's target"), ] { let output = Command::new(&exe).arg(case).output().unwrap(); - assert!(!output.status.success(), "Go {case} did not fail"); + failed(&output, &format!("Go {case}")); let stderr = String::from_utf8_lossy(&output.stderr); assert!(stderr.contains(message), "Go {case} printed {stderr}"); } @@ -1263,7 +1332,7 @@ fn api_go() { .unwrap(); run(go(&module, api, &["build", "-o"]).arg(&exe)); let output = Command::new(&exe).output().unwrap(); - assert!(!output.status.success(), "a mismatched contract did not fail"); + failed(&output, "a mismatched contract"); let stderr = String::from_utf8_lossy(&output.stderr); assert!( stderr.contains("the Go of api and the library are from different builds"), diff --git a/ffi/tests/nodump/module.modulemap b/ffi/tests/nodump/module.modulemap new file mode 100644 index 000000000..dfb3871b1 --- /dev/null +++ b/ffi/tests/nodump/module.modulemap @@ -0,0 +1,4 @@ +module NoDump { + header "nodump.h" + export * +} diff --git a/ffi/tests/nodump/nodump.h b/ffi/tests/nodump/nodump.h new file mode 100644 index 000000000..c60c76c7b --- /dev/null +++ b/ffi/tests/nodump/nodump.h @@ -0,0 +1,9 @@ +/* `prctl` for the Swift test, which cannot call it: Swift imports no variadic C function. */ + +#include + +/* Marks the process non-dumpable, so an expected abort writes no core and never starts the + * host's crash handler. */ +static inline int nodump(void) { + return prctl(PR_SET_DUMPABLE, 0, 0, 0, 0); +} diff --git a/quest/m0/ffi-abort-cores.md b/quest/m0/ffi-abort-cores.md deleted file mode 100644 index 3503f243b..000000000 --- a/quest/m0/ffi-abort-cores.md +++ /dev/null @@ -1,71 +0,0 @@ -# [S] Expected aborts leave no core dump - -## Goal - -The broken-contract cases in [`ffi/tests/fixtures.rs`](/ffi/tests/fixtures.rs), which -spawn each binding's test program and assert it aborts, still assert the abort and its -message, but on Linux no core is written or stored. Except for Kotlin's cases on `java`, -the kernel never hands the dying child to the host's core handler: `systemd-coredump` does -not start and the journal gets no crash entry. Kotlin's aborts still start the handler and -log one CRIT entry each, but write no core. That holds only for crash handlers that respect -the core-size limit, such as `systemd-coredump`, the one measured on fedora. - -## Plan - -Found by the 2026-11-13 write audit of the `nixos` CI host: of one boot's 6,007 cores, -5,957 were these expected aborts (C's `api-test zero`, C++17/20/23, Python, Dart, Kotlin -on `java`, Swift, Go), about 220 GB a day before that host stopped storing cores, and -fedora stored 5,116 more that day. Every one ran with `RLIMIT_CORE` unlimited. Each crash -also logs a CRIT journal entry, and journald syncs on every CRIT entry: about 35 GB a day -of writes on `nixos` that survive `Storage=none`. Agents' `just test` caused 75% of them, -the queue and the land fixers the rest. No single PR caused it: the tests abort by design. - -What disabling the core does, measured on fedora (kernel 7.3, systemd 262, a piped -`core_pattern`) by aborting a child at each limit: - -- `RLIMIT_CORE` at 0, set in the parent's `pre_exec`, survives `execve`. No core is - written, but the kernel still pipes the crash to `systemd-coredump`, which logs - "Resource limits disable core dumping" and then, at priority 2 (CRIT), "terminated - abnormally without generating a coredump". A limit of 1 behaves the same. So this - ends the core writes but neither the journald syncs nor the per-crash - `systemd-coredump` unit. -- `prctl(PR_SET_DUMPABLE, 0)` stops the kernel before it calls the handler, but `execve` - resets it, so a parent's `pre_exec` cannot set it. The child has to set it on itself. - -Each test program already routes its broken cases through one `broken(case)` entry, so -marking itself non-dumpable there is one call per language on Linux: `prctl` in C and -C++, `ctypes` in Python, Swift however its implementer reaches `prctl` (its variadic -`Glibc.prctl` may need a small fixed-arity C wrapper), `syscall`/`unix.Prctl` in Go, and FFI in -Dart. Kotlin runs on JDK 21, whose foreign-function API is still a preview there, so the -JVM cannot make the call without `--enable-preview`. -[#730](https://github.com/kixel-ai/rust2/pull/730) turns many Kotlin, Python, Dart and -Swift aborts into raised errors, but what a closure returns, C and C++ checks, and Rust -panics still abort, so this quest is needed either way; expect to merge with it in the -test programs and `fixtures.rs`. - -Decided with the maintainer on 2026-11-13: - -- Mechanism: ✅ each test program marks itself non-dumpable (`prctl(PR_SET_DUMPABLE, 0)`) - at its `broken(case)` entry on Linux, and the Kotlin JVM's aborting cases, which cannot - call `prctl` on JDK 21 without a preview flag, spawn with `RLIMIT_CORE` at 0 (rejected: - `RLIMIT_CORE` at 0 for every case from one spawn helper, since every crash would still - start `systemd-coredump` and log a CRIT entry; an `LD_PRELOAD` shim, for the build - artifact it adds). Kotlin keeps one CRIT entry per abort and no core. -- Swift's `prctl`: ✅ no plan change; the implementer decides how Swift reaches `prctl`, - such as a small fixed-arity C wrapper (rejected: naming the shim in the plan now). -- Kotlin's no-core claim: ✅ holds only for crash handlers that respect the core-size - limit, such as `systemd-coredump` (rejected: no change). -- Milestone: ✅ m0 (rejected: m1), since every agent's `just test` pays this. - -Verify on the real surface: run `just test` for the ffi fixtures on Linux and compare -`coredumpctl list` and `journalctl -p crit` over the run before and after; the -after-run should show no cores from the test programs, and no CRIT entries except one per -Kotlin abort on `java`. - -## Hosts - -- linux - the core handler and `prctl` are Linux's; macOS's default core limit is already 0 - -## Related - -- [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - turns many of these aborts into raised errors, leaving fewer to cover From 7ddcf59e2e724c725c8bdf9f395e7b39598e003b Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 23:11:43 -0800 Subject: [PATCH 3/3] style(ffi): format the Swift step's reads Co-Authored-By: Claude Opus 5.5 --- ffi/tests/fixtures.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index ad31c31ca..b2841214e 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -1063,7 +1063,16 @@ fn api_swift() { let nodump = manifest_dir().join("tests").join("nodump"); let step = Step { stamp: &step::stamp(&exe), - reads: &[&test, &nodump, &module, &include, &built, &swiftmodule, &lib, &api.staticlib()], + reads: &[ + &test, + &nodump, + &module, + &include, + &built, + &swiftmodule, + &lib, + &api.staticlib(), + ], writes: &[&exe], }; step.run(