From c775a6e74e7735a72c15bd0eb7805b92ac8ad56b Mon Sep 17 00:00:00 2001 From: chaodu-agent <274062505+chaodu-agent@users.noreply.github.com> Date: Mon, 23 Nov 2026 19:56:43 -0500 Subject: [PATCH] fix: route shell_exited pid through checked_pid (#179) shell_exited passed a u32 pid straight to waitid. A value that does not fit in i32, or is zero, now returns true, the same answer the ECHILD path already gives for a child that is gone. Valid pids are unchanged. Adds checked_pid_keeps_valid_pids_and_rejects_the_rest, which covers 1, i32::MAX, 0, 2147483648 and u32::MAX. Commands run on a Linux arm64 host, in a fresh clone of the branch: - cargo test - cargo test --release - cargo fmt --check - cargo clippy -- -D warnings --- poc/reverse-attach-linux/src/jobs/mod.rs | 6 +++++- poc/reverse-attach-linux/src/proc_tests.rs | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/poc/reverse-attach-linux/src/jobs/mod.rs b/poc/reverse-attach-linux/src/jobs/mod.rs index cb8d0cc..71c68d5 100644 --- a/poc/reverse-attach-linux/src/jobs/mod.rs +++ b/poc/reverse-attach-linux/src/jobs/mod.rs @@ -222,13 +222,17 @@ fn finish(g: &mut Inner, status: Option, timed_out: bool, late: Opti /// The job's shell (our child `pid`) has exited. It is not reaped (`WNOWAIT`), /// so its pid stays reserved. fn shell_exited(pid: u32) -> bool { + // A pid that names no process has no child to wait for: same answer as ECHILD below. + let Some(id) = crate::proc::checked_pid(pid) else { + return true; + }; // siginfo_t is 128 bytes on Linux; `si_signo` (first int) is set only when // a child was found, so a zeroed buffer distinguishes WNOHANG's "not yet". let mut info = [0i32; 32]; let r = unsafe { waitid( P_PID, - pid, + id as u32, info.as_mut_ptr().cast(), WEXITED | WNOHANG | WNOWAIT, ) diff --git a/poc/reverse-attach-linux/src/proc_tests.rs b/poc/reverse-attach-linux/src/proc_tests.rs index 205a472..cf69f72 100644 --- a/poc/reverse-attach-linux/src/proc_tests.rs +++ b/poc/reverse-attach-linux/src/proc_tests.rs @@ -325,6 +325,15 @@ fn a_pid_that_does_not_exist_signals_nothing_and_does_not_panic() { assert!(!kill_pid_checked(top + 1, &|_| true)); } +#[test] +fn checked_pid_keeps_valid_pids_and_rejects_the_rest() { + assert_eq!(checked_pid(1), Some(1)); + assert_eq!(checked_pid(2_147_483_647), Some(i32::MAX)); + assert_eq!(checked_pid(0), None); + assert_eq!(checked_pid(2_147_483_648), None); + assert_eq!(checked_pid(u32::MAX), None); +} + /// Pids above `i32::MAX` (2^31, and `u32::MAX`, which `as i32` makes `-1`) name no process. The /// call must answer "no" without reaching rustix with a negative id, so debug and release agree. #[cfg(target_os = "linux")]