cosca 0.6.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Tests for the macOS wait (`await_reapable`): its own `kevent` loop, the self-registration, the
//! backoff and the deadline. Every test that blocks on a real kqueue is covered by the nextest
//! override in `.config/nextest.toml`.
//!
//! The cases that change the process-wide `SIGCHLD` disposition each run in a fresh re-exec of
//! this test binary (`run_case`), so they cannot affect the other tests.

use std::process::{Child, ChildStdin};
use std::time::Duration;

use nix::sys::event::Kqueue;

use crate::wait::backend::{await_reapable, await_reapable_on, test_hooks, Waited};
use crate::wait::exit_only::seams as exit_seams;
use crate::wait::exit_only::seams::HolderStep;
use crate::wait::exit_only::Peek;

fn spawn_blocker() -> (Child, ChildStdin) {
    let mut child = crate::test_spawn::spawn(&mut crate::test_child::held_std_blocker(std::process::Stdio::null()))
        .expect("spawn the blocker");
    let stdin = child.stdin.take().expect("piped stdin");
    (child, stdin)
}

/// Block until `child`'s exit is visible, without consuming it.
fn confirm_exit(child: &Child) {
    // SAFETY: an all-zero `siginfo_t` is a valid value, and `waitid` writes only into it.
    let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
    let r = unsafe {
        libc::waitid(
            libc::P_PID,
            child.id() as libc::id_t,
            &mut info,
            libc::WEXITED | libc::WNOWAIT,
        )
    };
    assert_eq!(r, 0, "waitid(WNOWAIT): {}", std::io::Error::last_os_error());
}

// Reapable, and never reaping =====

/// A zombie is `Reapable`, and the wait leaves it reapable: the caller's own `wait` then reaps the
/// root with its status.
///
/// Mutant: the peek consumes (drops `WNOWAIT`), so the caller's `wait` finds nothing.
#[skuld::test]
fn await_reapable_reports_a_zombie_and_leaves_it_reapable() {
    let (mut child, stdin) = spawn_blocker();
    drop(stdin);
    assert_eq!(await_reapable(child.id(), None, None).expect("wait"), Waited::Reapable);
    let status = child.wait().expect("the zombie must still be there to reap");
    assert!(status.success(), "{status:?}");
}

// The deadline contract =====

/// A deadline already past still takes its final peek, and a zombie it finds is `Reapable`.
///
/// Mutant: no final peek at expiry: `DeadlinePassed`.
#[skuld::test]
fn await_reapable_at_a_past_deadline_takes_its_final_peek() {
    let (child, stdin) = spawn_blocker();
    drop(stdin);
    confirm_exit(&child);
    let _running = exit_seams::force_peek_once(Ok(Peek::Running));
    let waited = await_reapable(child.id(), None, Some(std::time::Instant::now())).expect("wait");
    assert_eq!(waited, Waited::Reapable);
    let mut child = child;
    child.wait().expect("reap");
}

/// What a runner that deschedules this thread for `d` inside a `kevent` call does to the frozen
/// clock: the wait advances it by the call's measured elapsed time (#488).
fn stall(d: Duration) {
    crate::wait::test_clock::advance_by_elapsed_if_frozen(d);
}

/// Hold the frozen clock until the wait's round `round`: the calls before it take no clock time,
/// however long the runner stalls them. From `round` on, measured time drives the clock again and
/// runs the deadline out.
fn hold_the_clock_until_round(round: u32) -> test_hooks::ForcedOnce {
    let held = crate::wait::test_clock::ZeroElapsedGuard::install();
    test_hooks::on_kevent_round(round, move || drop(held))
}

/// A short-lived child that starts and exits inside the wait: its `SIGCHLD` wakes the kqueue with
/// no event the wait is looking for, exactly as a sibling's exit would.
fn spurious_wake() {
    let mut child =
        crate::test_spawn::spawn(&mut std::process::Command::new("/usr/bin/true")).expect("spawn /usr/bin/true");
    child.wait().expect("reap /usr/bin/true");
}

/// Principle 13 for the kqueue wait: with the clock frozen, every blocking `kevent` is armed with
/// at most the time remaining when it starts, no round starts at or after the deadline, and the
/// expired wait takes one final peek. Round 0 is woken by a real `SIGCHLD` before the deadline, so
/// the wait must go round again, and the test tells that wake from a round after the deadline by
/// the frozen clock, not by counting rounds (another test's child may wake this one too). The
/// clock is held until round 1, so a runner stall before it cannot run the deadline out first;
/// round 0 spends 1 ms of it explicitly, so round 1 has less left than round 0.
///
/// Mutant: an unbounded `kevent` under a deadline (its `debug_assert!` fires); a round after the
/// deadline (no expiry check before the block); a timeout above the time remaining, or computed
/// once per wait; no final peek; the clock not held (the stall at round 0 ends the wait there).
#[skuld::test]
fn a_deadline_kevent_backoff_is_clamped_and_ends_with_one_peek() {
    let (mut child, stdin) = spawn_blocker();
    let (_clock, at) = crate::wait::test_clock::FrozenClockGuard::install();
    let _held = hold_the_clock_until_round(1);
    let limit = Duration::from_millis(30);
    let deadline = at + limit;
    let _hooks = test_hooks::HookGuard::install(|_, _| {});
    let remainings = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
    let _rounds = test_hooks::on_every_kevent_round({
        let remainings = std::rc::Rc::clone(&remainings);
        move |round| {
            let left = deadline.saturating_duration_since(crate::wait::now());
            assert!(!left.is_zero(), "round {round} started at or after the deadline");
            remainings.borrow_mut().push(left);
            if round == 0 {
                stall(limit);
                crate::wait::test_clock::advance(Duration::from_millis(1));
                spurious_wake();
            }
        }
    });
    exit_seams::holder_steps();
    let waited = await_reapable(child.id(), None, Some(deadline)).expect("wait");
    assert_eq!(waited, Waited::DeadlinePassed);
    let timeouts = test_hooks::await_requested_timeouts();
    let blocking: Vec<Duration> = timeouts
        .iter()
        .filter(|t| **t != Some(Duration::ZERO))
        .map(|t| t.expect("a deadline wait arms a bounded kevent"))
        .collect();
    let remainings = remainings.borrow();
    assert!(
        remainings.len() >= 2,
        "the wake must send the wait round again: {remainings:?}"
    );
    assert_eq!(blocking.len(), remainings.len(), "{timeouts:?}");
    for (armed, left) in blocking.iter().zip(remainings.iter()) {
        assert!(armed <= left, "armed {armed:?} with only {left:?} remaining");
    }
    assert_eq!(exit_seams::holder_steps(), [HolderStep::FinalPeek]);
    assert!(crate::wait::now() >= deadline);
    drop(stdin);
    child.wait().expect("reap");
}

/// A `kevent` interrupted by a signal is retried with the time remaining now, not the time
/// remaining when the interrupted call started. The interrupted call is made to spend 10 ms of
/// the frozen clock, which is held until round 0 so a runner stall before it changes nothing.
///
/// Mutant: the timeout computed once, before the retry loop; the clock not held (the stall leaves
/// less than `spent` for round 0).
#[skuld::test]
fn an_interrupted_kevent_is_retried_with_the_time_remaining_now() {
    let (mut child, stdin) = spawn_blocker();
    let (_clock, at) = crate::wait::test_clock::FrozenClockGuard::install();
    let _held = hold_the_clock_until_round(0);
    let spent = Duration::from_millis(10);
    let _hooks = test_hooks::HookGuard::install(|_, _| {});
    let _eintr = test_hooks::force_eintr_once(spent);
    let _stall = test_hooks::on_every_kevent_round(|round| {
        if round == 0 {
            stall(Duration::from_millis(25));
        }
    });
    let waited = await_reapable(child.id(), None, Some(at + Duration::from_millis(30))).expect("wait");
    assert_eq!(waited, Waited::DeadlinePassed);
    let timeouts = test_hooks::await_requested_timeouts();
    // The drain, the interrupted call, its retry, then the drain after expiry.
    let first = timeouts[1].expect("bounded");
    let retry = timeouts[2].expect("bounded");
    assert!(
        retry + spent <= first,
        "the retry was armed with {retry:?} after {spent:?} of {first:?} was spent"
    );
    drop(stdin);
    child.wait().expect("reap");
}

/// A deadline beyond XNU's `kevent` `tv_sec` limit still returns the child's exit, and no call is
/// armed with more than `i32::MAX` seconds. The child is ended from the round hook, so the exit
/// is imminent when the first `kevent` is called.
///
/// Mutant: no clamp in `kevent_timeout`: `EINVAL` from the first call.
#[skuld::test]
fn await_reapable_with_a_deadline_beyond_the_kevent_limit_returns_the_exit() {
    let (mut child, stdin) = spawn_blocker();
    let mut stdin = Some(stdin);
    let _end = test_hooks::on_kevent_round(0, move || drop(stdin.take()));
    let deadline = crate::wait::deadline_from(Duration::from_secs(u64::from(u32::MAX)))
        .and_then(|d| d)
        .expect("a deadline this far is still finite");
    let waited = await_reapable(child.id(), None, Some(deadline)).expect("a far deadline is not an error");
    assert_eq!(waited, Waited::Reapable);
    for armed in test_hooks::await_requested_timeouts() {
        let armed = armed.expect("a deadline wait arms a bounded kevent");
        assert!(armed <= Duration::from_secs(i32::MAX as u64), "armed {armed:?}");
    }
    child.wait().expect("reap");
}

/// The wait's own rounds honour the clamp seam: with the clamp lowered to 10 ms, a 50 ms
/// deadline is covered by several calls, none longer than the clamp, and the wait still ends only
/// at the deadline. The clock is held until round 1, so a runner stall cannot leave round 0 the
/// only blocking call.
///
/// Mutant: `kevent_round` arms its `kevent` without `kevent_timeout`: one call carries the whole
/// remaining time; the clock not held (the stall leaves round 0 5 ms, and it ends the wait).
#[skuld::test]
fn await_reapable_rearms_a_remaining_time_above_the_clamp_in_pieces() {
    let (mut child, stdin) = spawn_blocker();
    let (_clock, at) = crate::wait::test_clock::FrozenClockGuard::install();
    let _held = hold_the_clock_until_round(1);
    let _hooks = test_hooks::HookGuard::install(|_, _| {});
    let clamp = Duration::from_millis(10);
    test_hooks::set_clamp_override(clamp);
    let _stall = test_hooks::on_every_kevent_round(|round| {
        if round == 0 {
            stall(Duration::from_millis(45));
        }
    });
    let waited = await_reapable(child.id(), None, Some(at + Duration::from_millis(50))).expect("wait");
    assert_eq!(waited, Waited::DeadlinePassed);
    let timeouts = test_hooks::await_requested_timeouts();
    let blocking = timeouts.iter().filter(|t| **t != Some(Duration::ZERO)).count();
    assert!(
        blocking >= 2,
        "one call cannot cover 50 ms under a 10 ms clamp: {timeouts:?}"
    );
    assert!(
        timeouts.iter().all(|t| t.is_some_and(|t| t <= clamp)),
        "every call is capped: {timeouts:?}"
    );
    drop(stdin);
    child.wait().expect("reap");
}

// The test hooks =====

/// A dropped hook guard takes its unfired hook with it.
///
/// Mutant: a guard whose `Drop` does nothing.
#[skuld::test]
fn dropped_hook_guards_remove_their_hooks() {
    let fired = std::rc::Rc::new(std::cell::Cell::new(0u32));
    let count = |fired: &std::rc::Rc<std::cell::Cell<u32>>| {
        let fired = std::rc::Rc::clone(fired);
        move || fired.set(fired.get() + 1)
    };
    drop(test_hooks::on_before_repeek(count(&fired)));
    drop(test_hooks::on_esrch_repeek(count(&fired)));
    drop(test_hooks::on_kevent_round(0, count(&fired)));
    drop(test_hooks::on_every_kevent_round({
        let fired = std::rc::Rc::clone(&fired);
        move |_| fired.set(fired.get() + 1)
    }));
    test_hooks::fire_before_repeek();
    test_hooks::fire_esrch_repeek();
    test_hooks::fire_kevent_round(0);
    assert_eq!(fired.get(), 0, "a hook outlived its guard");
}

// EV_CLEAR =====

const MARKER_KNOTE: &str = "COSCA_TEST_AWAIT_KNOTE";

/// After the round that delivered `NOTE_EXIT`, no `EVFILT_PROC` event arrives again: the knote is
/// registered with `EV_CLEAR`, so a backoff `kevent` blocks for its interval rather than
/// re-activating at once. (An `EVFILT_SIGNAL` event may legitimately land later: `NOTE_EXIT`
/// posts before `SZOMB` and the `SIGCHLD`, `kern_exit.c:2562` against `:2631`, `:2637`.)
///
/// Mutant: the knote registered without `EV_CLEAR`.
#[skuld::test]
fn no_evfilt_proc_event_arrives_after_the_round_that_delivered_note_exit() {
    if !crate::test_child::is_marked_fixture_reexec(MARKER_KNOTE) {
        return run_case(
            crate::test_child::fixture_path!(no_evfilt_proc_event_arrives_after_the_round_that_delivered_note_exit),
            MARKER_KNOTE,
            "",
        );
    }
    let (mut child, stdin) = spawn_blocker();
    let _hooks = test_hooks::HookGuard::install(|_, _| {});
    let mut stdin = Some(stdin);
    // The child exits only after `EVFILT_PROC` is registered and the wait is about to block.
    let _end = test_hooks::on_kevent_round(0, move || drop(stdin.take()));
    // At least one more round after the one that delivers `NOTE_EXIT`.
    let _repeek = test_hooks::on_before_repeek(|| {
        std::mem::forget(exit_seams::force_peek_once(Ok(Peek::Running)));
    });
    assert_eq!(await_reapable(child.id(), None, None).expect("wait"), Waited::Reapable);
    child.wait().expect("reap");

    let events = test_hooks::take_events();
    let proc_filter = nix::sys::event::EventFilter::EVFILT_PROC as i16;
    let first_exit = events
        .iter()
        .position(|&(filter, fflags)| filter == proc_filter && fflags & libc::NOTE_EXIT != 0)
        .unwrap_or_else(|| panic!("a NOTE_EXIT must have been recorded: {events:?}"));
    let later: Vec<_> = events[first_exit + 1..]
        .iter()
        .filter(|&&(filter, _)| filter == proc_filter)
        .collect();
    assert!(later.is_empty(), "EVFILT_PROC re-delivered after NOTE_EXIT: {events:?}");
}

// Subprocess plumbing =====

/// Run the fixture `path` in a fresh re-exec of this binary with `marker` set and `case` in
/// [`CASE_ENV`].
fn run_case(path: &str, marker: &str, case: &str) {
    crate::test_child::run_fixture_case(path, marker, CASE_ENV, case);
}

const CASE_ENV: &str = "COSCA_TEST_AWAIT_CASE";

fn case() -> String {
    std::env::var(CASE_ENV).expect("the driver sets the case")
}

/// Whether `pid` can still be reaped by number: `Some(status)` if so, `None` on `ECHILD`.
fn reap_by_number(pid: u32) -> Option<i32> {
    let mut status = 0;
    // SAFETY: `status` is a valid out-pointer.
    let r = unsafe { libc::waitpid(pid as i32, &mut status, 0) };
    if r == pid as i32 {
        return Some(status);
    }
    assert_eq!(
        std::io::Error::last_os_error().raw_os_error(),
        Some(libc::ECHILD),
        "waitpid({pid})"
    );
    None
}

// The ESRCH paths =====

const MARKER_ESRCH: &str = "COSCA_TEST_AWAIT_ESRCH";

/// A registration that got `ESRCH` sends the wait to its backoff, and the backoff follows the
/// kernel, not the `SIGCHLD` disposition: the disposition is flipped between re-peeks (default to
/// `SIG_IGN`, and back), and the wait ends with whichever verdict the kernel produced, `Reapable`
/// if the zombie is there and `Gone` if XNU reaped the child itself (`kern_exit.c:2577`).
///
/// Mutant: a wait that decides from the disposition it saw when it started.
#[skuld::test]
fn a_macos_esrch_wait_follows_the_kernel_not_the_disposition() {
    if !crate::test_child::is_marked_fixture_reexec(MARKER_ESRCH) {
        for case in ["dfl_then_ign", "ign_then_dfl"] {
            run_case(
                crate::test_child::fixture_path!(a_macos_esrch_wait_follows_the_kernel_not_the_disposition),
                MARKER_ESRCH,
                case,
            );
        }
        return;
    }
    let case = case();
    let (flip_to_ignored, expect_gone) = match case.as_str() {
        "dfl_then_ign" => (true, true),
        "ign_then_dfl" => {
            crate::test_child::set_sigchld_ignored(true);
            (false, false)
        }
        other => panic!("unknown case {other}"),
    };
    let (mut child, stdin) = spawn_blocker();
    let pid = child.id();
    let _esrch = test_hooks::force_proc_registration_esrch_once();
    let mut stdin = Some(stdin);
    // Between re-peeks: flip the disposition, then let the child exit.
    let _flip = test_hooks::on_esrch_repeek(move || {
        crate::test_child::set_sigchld_ignored(flip_to_ignored);
        drop(stdin.take());
    });
    let waited = await_reapable(pid, None, None).expect("wait");
    if expect_gone {
        assert_eq!(waited, Waited::Gone, "XNU reaped the child itself under SIG_IGN");
        assert_eq!(reap_by_number(pid), None, "there is nothing left to reap");
    } else {
        assert_eq!(
            waited,
            Waited::Reapable,
            "the zombie stays under the default disposition"
        );
        assert!(reap_by_number(pid).is_some(), "the zombie is there to reap");
    }
    // The std `Child` owns nothing more: it was reaped (or auto-reaped) above.
    std::mem::forget(child.stdout.take());
    let _ = &mut child;
}

/// The wait registers `EVFILT_PROC` itself, so a caller whose own registration got `ESRCH` (a
/// failed spawn's normal path) still waits for a real `NOTE_EXIT`: under `SIG_IGN` with a live
/// sibling XNU sends no `SIGCHLD`, and nothing else would wake the kqueue.
///
/// Mutant: no self-registration: the wait blocks on a `NOTE_EXIT` that never comes.
#[skuld::test]
fn await_reapable_on_waits_out_a_caller_registration_that_got_esrch() {
    const MARKER: &str = "COSCA_TEST_AWAIT_CALLER_ESRCH";
    if !crate::test_child::is_marked_fixture_reexec(MARKER) {
        return run_case(
            crate::test_child::fixture_path!(await_reapable_on_waits_out_a_caller_registration_that_got_esrch),
            MARKER,
            "",
        );
    }
    crate::test_child::set_sigchld_ignored(true);
    let (_sibling, _sibling_stdin) = spawn_blocker();
    let (child, stdin) = spawn_blocker();
    let pid = child.id();
    // The caller's own registration, on the kqueue it hands in, "got ESRCH": it registered nothing.
    let kq = Kqueue::new().expect("kqueue");
    let mut stdin = Some(stdin);
    // The child exits only from inside the first blocking round, once the wait is about to sleep.
    let _end = test_hooks::on_kevent_round(0, move || drop(stdin.take()));
    // The first peek finds the child running (it is): the wait must then block, not spin.
    let waited = await_reapable_on(&kq, pid, None, None).expect("wait");
    assert_eq!(waited, Waited::Gone, "XNU reaped the child itself under SIG_IGN");
    std::mem::forget(child);
}