Repository navigation
fix: checked pid conversion so debug and release agree (#179) - #181
Closed
chaodu-agent wants to merge 2 commits into
Closed
chaodu-agent wants to merge 2 commits into
chaodu-agent wants to merge 2 commits into
Conversation
A u32 pid was turned into a signed id with `as i32`. u32::MAX became -1, and rustix's Pid::from_raw asserts against negative values in debug builds, so the proc test panicked in debug and passed in release. Every conversion now goes through one helper, proc::checked_pid, which returns None for a value that does not fit in i32 or is zero or negative. Callers take the path they already take for a process that does not exist: no signal is sent and has_exited reports true. Sites changed (poc/reverse-attach-linux/src): - proc.rs has_exited: Pid::from_raw(child.id() as i32) -> checked_pid(child.id()).and_then(Pid::from_raw) - proc.rs Child::stop: Pid::from_raw(child.id() as i32) -> checked_pid(child.id()).and_then(Pid::from_raw) - proc.rs kill_pid_checked: Pid::from_raw(pid as i32) -> checked_pid(pid).and_then(Pid::from_raw) - jobs/mod.rs signal and supervise_once: kill(-(pid as i32), sig) -> kill only when checked_pid(pid) is Some - tools/bash.rs: child.id() as i32 -> checked_pid; the kill(-pid) runs only when Some, and the reported pid is null when None - sweep_tests.rs: Pid::from_raw(helper as i32) -> checked_pid(helper).and_then(Pid::from_raw) Tests (proc_tests.rs): - a_pid_above_i32_max_is_treated_as_no_such_process (new): 2147483648, u32::MAX, u32::MAX - 1 - a_pid_that_does_not_exist_signals_nothing_and_does_not_panic (existing, u32::MAX and 0), now passes in debug Commands run on a Linux arm64 host, in a fresh clone of the pushed branch: - cargo test - cargo test --release - cargo fmt --check - cargo clippy -- -D warnings
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
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #179
Follow-up to #180, which merged at b4b6130. Commit 681c594 adds one more conversion site that #180 did not cover, so it needs its own PR to reach main.
Change
Every
pid as i32(and every uncheckedu32passed to a libc call) now goes throughchecked_pid. A pid that does not fit ini32is treated as "no such process" instead of wrapping to a negative value. Negative values reachkillandwaitidas process-group or wildcard targets, so a wrapped pid could signal the wrong process. Pid 0 andu32::MAXused to signal the caller's own group and init respectively. Both are now skipped.Conversion sites in
poc/reverse-attach-linux/src:proc.rshas_exited:Pid::from_raw(child.id() as i32)becomeschecked_pid(child.id()).and_then(Pid::from_raw).Nonereturnstrue.proc.rsChild::stop: same conversion.Noneskips the group TERM.proc.rskill_pid_checked:Pid::from_raw(pid as i32)becomeschecked_pid(pid).and_then(Pid::from_raw).Nonereturnsfalse.jobs/mod.rssignal:kill(-(self.pid as i32), sig)is sent only whenchecked_pid(self.pid)isSome.jobs/mod.rssupervise_once:kill(-(self.pid as i32), SIGKILL)is sent only whenchecked_pidisSome.jobs/mod.rsshell_exited:waitid(P_PID, pid, ...)now takeschecked_pid(pid).Nonereturnstrue, the same as the ECHILD path.Some(id)passesid as u32.tools/bash.rs:child.id() as i32becomeschecked_pid(child.id()). The group kill runs only onSome, and the reported pid isnullonNone.sweep_tests.rs:Pid::from_raw(helper as i32)becomeschecked_pid(helper).and_then(Pid::from_raw).New tests
proc::tests::a_pid_above_i32_max_is_treated_as_no_such_process(2147483648, u32::MAX, u32::MAX - 1), from b4b6130.proc::tests::a_pid_that_does_not_exist_signals_nothing_and_does_not_panic(u32::MAX, 0). In debug builds this failed before the fix withassertion failed: raw >= 0.proc::tests::checked_pid_keeps_valid_pids_and_rejects_the_rest, added in 681c594. It checks the helper's mapping directly.Pid-range guards in the jobs and bash paths have no dedicated test. Their pids come from
child.id(), which cannot be out of range for a live child. The helper test covers the mapping they rely on.Verification
Run on a fresh clone of
fix/179-proc-pidat 681c594 on a Linux aarch64 build host.cargo test(debug)cargo test --releasecargo fmt --checkcargo clippy -- -D warningsIgnored count was 17 in both the debug and release runs. Each new test passed in debug and release.