cosca 0.8.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
//! The policy of `OwnedChild` over a `FakeProcess`, which states what the OS answers: one test per
//! row of the state table, each with the mutant it turns RED against named in its doc. A holder is
//! "parked" when it is blocked on the wait its look returned, which the test fires.

use std::process::ExitStatus;
use std::sync::Arc;
use std::time::{Duration, Instant};

use super::fixtures::{assert_handed_off, owned_over, park_holder, spawn_waiter, status, try_wait};
use crate::child::owned::seams;
use crate::child::owned::{ChildWaitOutcome, OwnedChild, State};
use crate::error::Error;
use crate::os_process::child::{ExitRecord, Foreign, PeekOutcome};
use crate::os_process::fake::{FakeProcess, ReapAnswer};
use crate::signal::{Sent, Sig};

fn exit() -> ExitRecord {
    ExitRecord::Status(ExitStatus::default())
}

/// A deadline far enough away that only a scripted answer can end the wait.
fn far() -> Instant {
    Instant::now() + Duration::from_secs(3600)
}

const KIND: std::io::ErrorKind = std::io::ErrorKind::Other;

#[cfg(unix)]
const UNREAD_PID: u32 = 0;

/// A reap that fails, as the error its caller sees.
fn is_the_scripted_failure(err: &Error) -> bool {
    matches!(err, Error::Io(e) if e.kind() == KIND)
}

// The lock =====

/// A poisoned mutex neither aborts nor hangs. A holder panics just after its re-lock,
/// with the lock held; then `try_wait`, `signal`, `Debug` and `wait` all return from other
/// threads.
///
/// Mutant: `lock().unwrap()`, which panics on the poison.
#[skuld::test]
fn a_poisoned_lock_neither_aborts_nor_hangs() {
    let fake = FakeProcess::new([])
        .reaps([ReapAnswer::Running, ReapAnswer::Reaped(exit())])
        .signals([Ok(Sent::Delivered)]);
    let child = owned_over(&fake);
    let poisoner = std::thread::spawn({
        let child = Arc::clone(&child);
        move || {
            let _panic = seams::panic_after_relock_once();
            child.wait(None)
        }
    });
    assert!(poisoner.join().is_err(), "the holder must have panicked");

    let child = &*child;
    std::thread::scope(|scope| {
        let calls: [Box<dyn FnOnce() + Send + '_>; 3] = [
            Box::new(|| assert!(matches!(try_wait(child), Ok(ChildWaitOutcome::DeadlinePassed)))),
            Box::new(|| assert!(!format!("{child:?}").is_empty())),
            Box::new(|| drop(format!("{child:#?}"))),
        ];
        let threads: Vec<_> = calls.into_iter().map(|call| scope.spawn(call)).collect();
        for t in threads {
            t.join().expect("a call on a poisoned lock must not panic");
        }
        let sent = scope
            .spawn(|| child.signal(Sig::Kill))
            .join()
            .expect("signal must not panic");
        assert_eq!(sent.expect("signal on a poisoned lock"), Sent::Delivered);
        let waited = scope.spawn(|| child.wait(None)).join().expect("wait must not panic");
        assert_eq!(status(waited), ExitStatus::default());
    });
}

/// `Debug`: formatting the handle inside a blocked waiter does not block, and shows
/// `locked`. The `on_condvar_block` hook runs under the lock and formats the handle itself.
///
/// Mutant: `Debug` through `OwnedChild::lock()`, which the test build's owner check turns into a
/// panic in the hook, instead of a deadlock on its own thread.
#[skuld::test]
fn debug_inside_a_blocked_waiter_does_not_block() {
    let fake = FakeProcess::new([]).reaps([ReapAnswer::Running, ReapAnswer::Reaped(exit())]);
    let child = owned_over(&fake);
    let holder = park_holder(&child, &fake, |c| c.wait(None));
    let (printed_tx, printed_rx) = std::sync::mpsc::channel();
    let waiter = std::thread::spawn({
        let child = Arc::clone(&child);
        move || {
            let printer = Arc::clone(&child);
            let _hook = crate::wait::block_probe::on_condvar_block(move || {
                _ = printed_tx.send(format!("{printer:?}"));
            });
            child.wait(None)
        }
    });
    let printed = printed_rx.recv().expect("the waiter must reach its Condvar block");
    assert!(
        printed.contains("locked"),
        "Debug under the lock must say `locked`: {printed}"
    );
    let held = status(holder.release_and_join());
    assert_eq!(status(waiter.join().expect("waiter")), held);
}

// One holder =====

/// A second `wait` blocks behind the holder, on the `Condvar`, and returns the same
/// status.
///
/// Mutant: no `W` check, so the second waiter becomes a second holder; it looks past the holder's
/// scripted answers, and its hook never fires.
#[skuld::test]
fn a_second_wait_blocks_behind_the_holder() {
    let fake = FakeProcess::new([]).reaps([ReapAnswer::Running, ReapAnswer::Reaped(exit())]);
    let child = owned_over(&fake);
    let holder = park_holder(&child, &fake, |c| c.wait(None));
    let (waiter, blocked) = spawn_waiter(&child, |c| c.wait(None));
    blocked
        .recv()
        .expect("the second wait must block on the Condvar behind the holder");
    let held = status(holder.release_and_join());
    assert_eq!(status(waiter.join().expect("waiter")), held);
}

// One look =====

/// A look that finds the exit record reaps it once and caches it: a second call reaps nothing.
///
/// Mutant: no `E` write, so the second call reaps again, past the scripted answers.
#[skuld::test]
fn a_look_reaps_an_exited_child_once() {
    let fake = FakeProcess::new([]).reaps([ReapAnswer::Reaped(exit())]);
    let child = OwnedChild::new(Box::new(fake.clone()));
    let first = status(try_wait(&child));
    let second = status(try_wait(&child));
    assert_eq!(first, second);
    assert_eq!(fake.looks(), 1);
    assert!(child.is_reaped());
}

/// A look that finds the child running records nothing: the child is reaped only once a look takes
/// its record.
///
/// Mutant: `is_reaped` reads the OS, or a look that found the child running records a reap.
#[skuld::test]
fn is_reaped_only_once_the_record_is_taken() {
    let fake = FakeProcess::new([]).reaps([ReapAnswer::Running, ReapAnswer::Reaped(exit())]);
    let child = OwnedChild::new(Box::new(fake.clone()));
    assert!(!child.is_reaped());
    assert_eq!(try_wait(&child).expect("try_wait"), ChildWaitOutcome::DeadlinePassed);
    assert!(!child.is_reaped(), "a look that found the child running is not a reap");
    status(try_wait(&child));
    assert!(child.is_reaped());
}

/// A reap error is returned as it is, and the state stays `N`.
///
/// Mutant: an error taken for the foreign path, or the state left in `W`.
#[skuld::test]
fn a_look_that_fails_returns_the_error_and_stays_n() {
    let fake = FakeProcess::new([]).reaps([ReapAnswer::Err(KIND), ReapAnswer::Reaped(exit())]);
    let child = OwnedChild::new(Box::new(fake));
    let err = try_wait(&child).expect_err("the scripted failure");
    assert!(is_the_scripted_failure(&err), "{err:?}");
    assert!(matches!(child.lock().state, State::N));
    assert!(matches!(try_wait(&child), Ok(ChildWaitOutcome::Exited(_))));
}

// Signal =====

/// `signal` during a parked wait goes to the OS at once and never blocks on the `Condvar`;
/// the holder then reaps.
///
/// Mutant: `signal` blocks on the `Condvar` while `W`; the signalling thread's hook panics.
#[skuld::test]
fn signal_during_a_parked_wait_signals_and_the_holder_reaps() {
    let fake = FakeProcess::new([])
        .reaps([ReapAnswer::Running, ReapAnswer::Reaped(exit())])
        .signals([Ok(Sent::Delivered)]);
    let child = owned_over(&fake);
    let holder = park_holder(&child, &fake, |c| c.wait(None));
    let signaller = std::thread::spawn({
        let child = Arc::clone(&child);
        move || {
            let _hook = crate::wait::block_probe::on_condvar_block(|| panic!("signal blocked on the Condvar"));
            child.signal(Sig::Kill)
        }
    });
    let sent = signaller.join().expect("signal must not block").expect("signal");
    assert_eq!(sent, Sent::Delivered);
    assert_eq!(fake.signal_calls(), [Sig::Kill]);
    status(holder.release_and_join());
}

// A deadline behind a holder =====

/// A deadline waiter behind a parked holder blocks on the `Condvar` for the time remaining,
/// then returns `DeadlinePassed` once its deadline has passed, while the holder is still parked.
/// The clock is frozen, so the armed timeout is exactly the time remaining.
///
/// A block with no timeout is reported by the hook, and the test then lets the holder finish, so
/// the waiter returns and the assertion fails.
///
/// Mutant: the `Condvar::wait` without a timeout; a waiter that returns by peeking, without
/// blocking; a wait armed with anything but the time remaining.
#[skuld::test]
fn a_deadline_waiter_behind_a_parked_holder_returns_at_its_deadline() {
    let fake = FakeProcess::new([])
        .reaps([ReapAnswer::Running, ReapAnswer::Reaped(exit())])
        .peeks([Ok(PeekOutcome::Running)]);
    let child = owned_over(&fake);
    let holder = park_holder(&child, &fake, |c| c.wait(None));
    let remaining = Duration::from_millis(50);
    let (armed_tx, armed_rx) = std::sync::mpsc::channel();
    let waiter = std::thread::spawn({
        let child = Arc::clone(&child);
        move || {
            let (_clock, at) = crate::wait::test_clock::FrozenClockGuard::install();
            let _hook = crate::wait::block_probe::on_condvar_block_with(move |armed| _ = armed_tx.send(armed));
            let got = child.wait(Some(at + remaining));
            let expired = crate::wait::now() >= at + remaining;
            let holder_still_parked = matches!(child.lock().state, State::W { .. });
            (got, expired, holder_still_parked)
        }
    });
    let armed = armed_rx.recv().expect("the waiter must block on the Condvar");
    if armed.is_none() {
        // A wait with no timeout would never return: let the holder reap, which wakes the waiter,
        // and fail on the value.
        holder.release_and_join().expect("holder wait");
        waiter.join().expect("waiter").0.expect("wait");
        panic!("the waiter blocked on the Condvar with no timeout");
    }
    let (got, expired, holder_still_parked) = waiter.join().expect("waiter");
    assert_eq!(armed, Some(remaining), "armed with the time remaining");
    assert_eq!(
        got.expect("a running child is not an error"),
        ChildWaitOutcome::DeadlinePassed
    );
    assert!(expired, "returned before the deadline");
    assert!(
        holder_still_parked,
        "the holder must still be parked when the waiter returns"
    );
    status(holder.release_and_join());
}

/// Principle 13 for a non-holder's `Condvar` block: each round is armed from the time remaining at
/// that moment, so a spurious wake shrinks the next timeout. The clock is frozen, and each block
/// advances it by the real time the block took.
///
/// Mutant: the remaining time computed once, before the loop: both rounds are armed alike.
#[skuld::test]
fn a_spurious_wake_re_arms_a_deadline_waiter_with_less_time() {
    let fake = FakeProcess::new([]).reaps([ReapAnswer::Running, ReapAnswer::Reaped(exit())]);
    let child = owned_over(&fake);
    let holder = park_holder(&child, &fake, |c| c.wait(None));
    let (blocked_tx, blocked_rx) = std::sync::mpsc::channel();
    let (again_tx, again_rx) = std::sync::mpsc::channel();
    let waiter = std::thread::spawn({
        let child = Arc::clone(&child);
        move || {
            let (_clock, at) = crate::wait::test_clock::FrozenClockGuard::install();
            let _first = crate::wait::block_probe::on_condvar_block(move || {
                _ = blocked_tx.send(());
                // The second round's hook arms from inside the first, under the lock. Forgotten:
                // the guard would disarm the hook when this closure returns.
                let second = crate::wait::block_probe::on_condvar_block(move || _ = again_tx.send(()));
                std::mem::forget(second);
            });
            crate::wait::block_probe::take();
            let got = child.wait(Some(at + Duration::from_secs(3600)));
            (got, crate::wait::block_probe::take())
        }
    });
    blocked_rx.recv().expect("the waiter must block in round one");
    // The lock is free only once the waiter is inside its `Condvar` wait, which releases it
    // atomically, so a notify under it cannot be lost.
    {
        let _lock = child.lock();
        child.condvar.notify_all();
    }
    again_rx
        .recv()
        .expect("the spurious wake must send the waiter round again");
    // The holder's reap wakes the waiter with the status.
    status(holder.release_and_join());
    let (got, armed) = waiter.join().expect("waiter");
    status(got);
    let armed: Vec<Duration> = armed
        .into_iter()
        .map(|a| a.expect("a deadline wait arms a bound"))
        .collect();
    assert!(armed.len() >= 2, "a second round must have been armed: {armed:?}");
    assert!(
        armed[1] < armed[0],
        "the second round must be armed from the recomputed remaining time: {armed:?}"
    );
}

/// A waiter whose deadline has passed, behind a holder, returns the exit status from its
/// peek at once, without reaping and without waiting for the holder. `Child::try_wait` is this
/// call with the deadline now.
///
/// Mutant: blocking until the holder writes: the waiter's hook panics; or reaping: the holder's
/// look finds the scripted reap gone.
#[skuld::test]
fn a_waiter_that_sees_an_exit_at_its_deadline_returns_its_status_without_reaping() {
    let fake = FakeProcess::new([])
        .reaps([ReapAnswer::Running, ReapAnswer::Reaped(exit())])
        .peeks([Ok(PeekOutcome::Exit(exit()))]);
    let child = owned_over(&fake);
    let holder = park_holder(&child, &fake, |c| c.wait(None));
    let waiter = std::thread::spawn({
        let child = Arc::clone(&child);
        move || {
            let _hook = crate::wait::block_probe::on_condvar_block(|| panic!("the waiter blocked past its deadline"));
            try_wait(&child)
        }
    });
    let peeked = status(waiter.join().expect("the waiter must not block"));
    let reaped = status(holder.release_and_join());
    assert_eq!(peeked, reaped, "the holder's reap carries the same status");
}

/// A waiter whose deadline has passed, behind a holder, on a running child returns
/// `DeadlinePassed`.
///
/// Mutant: the exit-record branch taken for "running".
#[skuld::test]
fn a_waiter_at_its_deadline_behind_a_holder_on_a_running_child_gets_deadline_passed() {
    let fake = FakeProcess::new([])
        .reaps([ReapAnswer::Running, ReapAnswer::Reaped(exit())])
        .peeks([Ok(PeekOutcome::Running)]);
    let child = owned_over(&fake);
    let holder = park_holder(&child, &fake, |c| c.wait(None));
    let waiter = std::thread::spawn({
        let child = Arc::clone(&child);
        move || {
            let _hook = crate::wait::block_probe::on_condvar_block(|| panic!("the waiter blocked past its deadline"));
            try_wait(&child)
        }
    });
    assert_eq!(
        waiter.join().expect("the waiter must not block").expect("try_wait"),
        ChildWaitOutcome::DeadlinePassed
    );
    status(holder.release_and_join());
}

// A non-holder's foreign verdict =====

/// A non-holder whose peek is foreign returns it and writes nothing: the holder, then released,
/// finds its own `W` on the re-read (a `debug_assert!`) and reaps.
///
/// Mutant: a non-holder's foreign verdict writes `N`, so the holder's re-read finds a foreign state and its assert fires.
#[skuld::test]
fn a_non_holder_peek_that_is_foreign_returns_it_and_writes_nothing() {
    let fake = FakeProcess::new([])
        .reaps([ReapAnswer::Running, ReapAnswer::Reaped(exit())])
        .peeks([Ok(PeekOutcome::Foreign(Foreign::Gone))]);
    let child = owned_over(&fake);
    let holder = park_holder(&child, &fake, |c| c.wait(Some(far())));
    let got = try_wait(&child).expect("the scripted peek");
    assert_eq!(got, ChildWaitOutcome::Foreign(Foreign::Gone));
    assert!(
        matches!(child.lock().state, State::W { .. }),
        "a non-holder's foreign verdict writes nothing"
    );
    status(holder.release_and_join());
    assert!(matches!(child.lock().state, State::E(_)));
}

// The holder's reap =====

/// An exit seen is reaped once, under the lock, and cached: a second `wait` never looks.
///
/// Mutant: no `E` write, so the second `wait` looks past the scripted answers.
#[skuld::test]
fn an_exit_seen_is_reaped_once_under_the_lock() {
    let fake = FakeProcess::new([]).reaps([ReapAnswer::Reaped(exit())]);
    let child = OwnedChild::new(Box::new(fake.clone()));
    let first = status(child.wait(None));
    assert_eq!(status(child.wait(None)), first);
    assert_eq!(fake.looks(), 1);
}

/// A holder's normal return leaves the next holder alone. A regression test, with
/// no RED claimed: `finish` consumes the guard, so the hazard it guards is unrepresentable.
#[skuld::test]
fn a_holders_normal_return_leaves_the_next_holder_alone() {
    let fake = FakeProcess::new([]).reaps([
        ReapAnswer::Running,
        ReapAnswer::Foreign(Foreign::Gone),
        ReapAnswer::Running,
        ReapAnswer::Reaped(exit()),
    ]);
    let child = owned_over(&fake);
    let holder = park_holder(&child, &fake, |c| c.wait(None));
    let (blocked_tx, blocked_rx) = std::sync::mpsc::channel();
    let (parked_tx, parked_rx) = std::sync::mpsc::channel();
    let second = std::thread::spawn({
        let child = Arc::clone(&child);
        move || {
            let _hook = crate::wait::block_probe::on_condvar_block(move || {
                _ = blocked_tx.send(());
            });
            let _blocking = crate::wait::block_probe::on_blocking(move || {
                _ = parked_tx.send(());
            });
            child.wait(None)
        }
    });
    blocked_rx
        .recv()
        .expect("the second waiter must block behind the first");
    assert_eq!(
        holder.release_and_join().expect("first holder"),
        ChildWaitOutcome::Foreign(Foreign::Gone)
    );
    parked_rx.recv().expect("the second waiter must become the holder");
    assert!(
        matches!(child.lock().state, State::W { token: 1 }),
        "the first holder's return must leave the second holder's W alone"
    );
    fake.wake();
    status(second.join().expect("second holder"));
}

/// A reap error returns it, restores `N` and wakes the waiters: the blocked waiter
/// becomes the holder.
///
/// Mutant: a `?` return that leaves `W`: the waiter never wakes.
#[skuld::test]
fn a_reap_error_restores_n_and_wakes_the_waiters() {
    let fake = FakeProcess::new([]).reaps([ReapAnswer::Running, ReapAnswer::Err(KIND), ReapAnswer::Reaped(exit())]);
    let child = owned_over(&fake);
    let holder = park_holder(&child, &fake, |c| c.wait(None));
    let (waiter, blocked) = spawn_waiter(&child, |c| c.wait(None));
    blocked.recv().expect("the waiter must block behind the holder");
    let err = holder.release_and_join().expect_err("the scripted reap error");
    assert_handed_off(&child, 0);
    assert!(is_the_scripted_failure(&err), "{err:?}");
    status(waiter.join().expect("waiter"));
}

// Hand-off and failure of the holder =====

/// A holder whose deadline passes hands off to a blocked `wait`, which becomes the holder and
/// returns the real status. The holder's clock is frozen, and its second look moves it past the
/// deadline, so the final look follows with no block.
///
/// Mutant: `N` written without `notify_all`: the waiter never wakes.
#[skuld::test]
fn an_expired_holder_hands_off_to_a_blocked_wait() {
    let fake = FakeProcess::new([]).reaps([
        ReapAnswer::Running,
        ReapAnswer::Running,
        ReapAnswer::Running,
        ReapAnswer::Reaped(exit()),
    ]);
    fake.on_look(|look| {
        if look == 1 {
            crate::wait::test_clock::advance(Duration::from_secs(7200));
        }
    });
    let child = owned_over(&fake);
    let holder = park_holder(&child, &fake, |c| {
        let (_clock, at) = crate::wait::test_clock::FrozenClockGuard::install();
        c.wait(Some(at + Duration::from_secs(3600)))
    });
    let (waiter, blocked) = spawn_waiter(&child, |c| c.wait(None));
    blocked.recv().expect("the waiter must block behind the holder");
    assert_eq!(
        holder.release_and_join().expect("holder"),
        ChildWaitOutcome::DeadlinePassed
    );
    assert_handed_off(&child, 0);
    status(waiter.join().expect("waiter"));
}

/// A holder that is told the child is foreign takes that path and restores `N`.
///
/// Mutant: *foreign* taken for *deadline passed*.
#[skuld::test]
fn a_holder_that_sees_a_foreign_reap_returns_it_and_restores_n() {
    let fake = FakeProcess::new([]).reaps([ReapAnswer::Foreign(Foreign::Gone)]);
    let child = OwnedChild::new(Box::new(fake));
    assert_eq!(
        child.wait(None).expect("foreign"),
        ChildWaitOutcome::Foreign(Foreign::Gone)
    );
    assert!(matches!(child.lock().state, State::N));
}

/// A holder that panics in its look restores `N` and wakes the waiters.
///
/// Mutant: no guard: the waiter never wakes.
#[skuld::test]
fn a_holder_that_panics_in_its_look_restores_n_and_wakes_the_waiters() {
    let fake = FakeProcess::new([]).reaps([ReapAnswer::Running, ReapAnswer::Reaped(exit())]);
    fake.on_look(|look| {
        if look == 1 {
            panic!("forced panic in the holder's look");
        }
    });
    let child = owned_over(&fake);
    let holder = park_holder(&child, &fake, |c| c.wait(None));
    let (waiter, blocked) = spawn_waiter(&child, |c| c.wait(None));
    blocked.recv().expect("the waiter must block behind the holder");
    assert!(
        holder.release_and_join_thread().is_err(),
        "the holder must have panicked"
    );
    assert_handed_off(&child, 0);
    status(waiter.join().expect("waiter"));
}

/// A holder that panics after its re-lock, holding the lock, restores `N` without
/// deadlocking: the blocked waiter wakes, takes the poisoned lock, becomes the holder and returns
/// the real status.
///
/// Mutant: a guard that re-locks instead of using its own `MutexGuard` (self-deadlock), or a
/// `Condvar` result that is `unwrap()`ped (the waiter panics).
#[skuld::test]
fn a_holder_that_panics_after_its_relock_restores_n_without_deadlocking() {
    let fake = FakeProcess::new([]).reaps([ReapAnswer::Running, ReapAnswer::Reaped(exit())]);
    // Armed in the first look, on the holder's thread: its second look's re-lock panics.
    fake.on_look(|look| {
        if look == 0 {
            // Left armed: the next re-lock on this thread takes it.
            std::mem::forget(seams::panic_after_relock_once());
        }
    });
    let child = owned_over(&fake);
    let holder = park_holder(&child, &fake, |c| c.wait(None));
    let (waiter, blocked) = spawn_waiter(&child, |c| c.wait(None));
    blocked.recv().expect("the waiter must block behind the holder");
    assert!(
        holder.release_and_join_thread().is_err(),
        "the holder must have panicked"
    );
    assert_handed_off(&child, 0);
    status(waiter.join().expect("waiter"));
}

// A recheck =====

/// A look that asks for a recheck (a zombie a tracer holds, or an exit XNU is still finishing)
/// is repeated after a backoff of 1, 2, 4 ... ms until it shows the record: the looks are the
/// pauses plus one, and none polls.
///
/// Mutant: a recheck that loops with no pause (a spin), or that blocks on the look's wait.
#[skuld::test]
fn a_recheck_backs_off_until_the_zombie_shows() {
    let fake = FakeProcess::new([]).reaps([
        ReapAnswer::Recheck,
        ReapAnswer::Recheck,
        ReapAnswer::Recheck,
        ReapAnswer::Reaped(exit()),
    ]);
    let child = OwnedChild::new(Box::new(fake.clone()));
    crate::wait::block_probe::take();
    status(child.wait(None));
    let pauses = crate::wait::block_probe::take();
    let ms = |n| Some(Duration::from_millis(n));
    assert_eq!(pauses, [ms(1), ms(2), ms(4)]);
    assert_eq!(fake.looks(), pauses.len() + 1);
}

/// A holder paused for a recheck holds no lock: the handle answers a peek, on the holder's own
/// thread, from inside the pause.
///
/// Mutant: the pause under the lock, so the peek locks a handle its own thread holds.
#[skuld::test]
fn a_holders_recheck_pause_leaves_the_handle_unlocked() {
    let fake = FakeProcess::new([])
        .reaps([ReapAnswer::Recheck, ReapAnswer::Reaped(exit())])
        .peeks([Ok(PeekOutcome::Running)]);
    let child = owned_over(&fake);
    let (peeked_tx, peeked_rx) = std::sync::mpsc::channel();
    let _hook = crate::wait::block_probe::on_pause({
        let child = Arc::clone(&child);
        move || _ = peeked_tx.send(child.peek())
    });
    status(child.wait(None));
    let peeked = peeked_rx.try_recv().expect("the holder paused once");
    assert!(matches!(peeked, Ok(PeekOutcome::Running)), "{peeked:?}");
}

/// A holder blocked on its wait holds no lock: `Debug` shows `W`, not `locked`, and `signal`
/// returns.
///
/// Mutant: the wait under the lock, so `Debug`'s `try_lock` fails and prints `locked`.
#[skuld::test]
fn a_holder_blocked_on_its_wait_holds_no_lock() {
    let fake = FakeProcess::new([])
        .reaps([ReapAnswer::Running, ReapAnswer::Reaped(exit())])
        .signals([Ok(Sent::Delivered)]);
    let child = owned_over(&fake);
    let holder = park_holder(&child, &fake, |c| c.wait(None));
    // The check comes first, so a RED fails on `locked` rather than hanging in `signal`.
    let printed = format!("{child:?}");
    assert!(printed.contains("W {"), "the holder is in W: {printed}");
    assert!(!printed.contains("locked"), "the wait must hold no lock: {printed}");
    child.signal(Sig::Kill).expect("signal must not wait for the holder");
    status(holder.release_and_join());
}

/// A holder blocked on its wait looks again only when the wait is fired.
///
/// Mutant: a holder that looks again without waiting (a spin).
#[skuld::test]
fn a_holder_blocked_on_its_wait_wakes_when_signalled() {
    let fake = FakeProcess::new([]).reaps([ReapAnswer::Running, ReapAnswer::Reaped(exit())]);
    let child = owned_over(&fake);
    let holder = park_holder(&child, &fake, |c| c.wait(None));
    assert_eq!(fake.looks(), 1, "the holder has not looked again");
    status(holder.release_and_join());
    assert_eq!(fake.looks(), 2);
}

/// A foreign verdict is cached as nothing: the state stays `N` and nothing is reaped.
///
/// Mutant: the foreign verdict written as `E`, or as a stuck `W`.
#[skuld::test]
fn a_foreign_reap_is_cached_as_nothing_and_wait_stays_n() {
    let fake = FakeProcess::new([]).reaps([ReapAnswer::Foreign(Foreign::Gone), ReapAnswer::Reaped(exit())]);
    let child = OwnedChild::new(Box::new(fake));
    assert_eq!(
        child.wait(None).expect("foreign"),
        ChildWaitOutcome::Foreign(Foreign::Gone)
    );
    assert!(matches!(child.lock().state, State::N));
    assert!(!child.is_reaped());
    status(child.wait(None));
}

/// The deadline of `exited_by` is read through the OS's own look, and reaps nothing.
///
/// Mutant: `exited_by` reaps, or asks the reap's look.
#[skuld::test]
fn exited_by_looks_through_exited_and_reaps_nothing() {
    let fake = FakeProcess::new([crate::os_process::fake::Answer::Exited]);
    let child = OwnedChild::new(Box::new(fake.clone()));
    assert!(child.exited_by(None).expect("exited_by"));
    assert_eq!(fake.looks(), 1);
    assert!(!child.is_reaped());
}

// After the reap =====

/// A cached status never reaches the OS, for any deadline.
///
/// Mutant: no `E` check: the look runs past the scripted answers.
#[skuld::test]
fn a_cached_status_never_reaches_the_os() {
    let fake = FakeProcess::new([]).reaps([ReapAnswer::Reaped(exit())]);
    let child = OwnedChild::new(Box::new(fake.clone()));
    let first = status(child.wait(None));
    assert_eq!(status(child.wait(None)), first);
    assert_eq!(status(child.wait(Some(far()))), first);
    assert_eq!(status(try_wait(&child)), first);
    assert_eq!(fake.looks(), 1);
}

/// `signal` after the reap sends nothing.
///
/// Mutant: a `signal` to the OS after `E`: the empty signal table panics.
#[skuld::test]
fn signal_after_the_reap_sends_nothing() {
    let fake = FakeProcess::new([]).reaps([ReapAnswer::Reaped(exit())]);
    let child = OwnedChild::new(Box::new(fake.clone()));
    status(child.wait(None));
    assert_eq!(
        child.signal(Sig::Kill).expect("an already-reaped child is success"),
        Sent::Gone
    );
    assert_eq!(fake.signal_calls(), []);
}

/// `ReapHold::signal` after our own reap (`State::E`) reports `Gone` and sends nothing.
///
/// Mutant: it reports `Delivered`, or signals, after `E`.
#[cfg(unix)]
#[skuld::test]
fn a_held_signal_after_our_own_reap_is_gone() {
    let fake = FakeProcess::new([]).reaps([ReapAnswer::Reaped(exit())]);
    let child = OwnedChild::new(Box::new(fake.clone()));
    status(child.wait(None));
    assert_eq!(
        child
            .hold(UNREAD_PID)
            .signal(Sig::Kill)
            .expect("an already-reaped child is success"),
        Sent::Gone
    );
    assert_eq!(fake.signal_calls(), []);
}

// L8: an unreadable consuming record =====

/// L8: a consuming reap that hands back a record that is not an exit is cached as
/// `E(Unreadable)` and acts by no pid: every later wait answers it and `signal` sends nothing. In
/// a debug build the contract breach panics, after the `E` write.
///
/// Mutant: a panic before the `E` write: the state stays `W` and the guard restores `N` over a
/// reaped pid.
#[skuld::test]
fn an_unreadable_consuming_record_is_cached_and_acts_by_no_pid() {
    let unreadable = ExitRecord::Unreadable { si_code: 99 };
    let fake = FakeProcess::new([]).reaps([ReapAnswer::Reaped(unreadable)]);
    let child = OwnedChild::new(Box::new(fake.clone()));
    // In a debug build the call panics (after the write); in a release build it returns.
    let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| child.wait(None)));
    if cfg!(debug_assertions) {
        assert!(outcome.is_err(), "a debug build asserts the contract");
    } else {
        assert_eq!(
            outcome.expect("release returns").expect("a record"),
            ChildWaitOutcome::Exited(unreadable)
        );
    }
    assert!(
        matches!(child.lock().state, State::E(ExitRecord::Unreadable { si_code: 99 })),
        "the state must already be E(Unreadable): {child:?}"
    );
    for outcome in [child.wait(None), child.wait(Some(far())), try_wait(&child)] {
        assert_eq!(outcome.expect("cached"), ChildWaitOutcome::Exited(unreadable));
    }
    assert_eq!(child.signal(Sig::Kill).expect("signal"), Sent::Gone);
    assert_eq!(fake.signal_calls(), []);
}

// Adoption =====

/// Taking over a child makes no call to it: nothing is reaped or looked at.
///
/// Mutant: `OwnedChild::new` reaps or looks at an already-exited child on adoption.
#[skuld::test]
fn adopt_never_reaps() {
    let fake = FakeProcess::new([]);
    let child = OwnedChild::new(Box::new(fake.clone()));
    assert_eq!(fake.looks(), 0);
    assert!(!child.is_reaped());
}

/// `adopt` never reaps: an already-exited child is still a zombie afterwards, so a later
/// wait reaps it and returns its status.
///
/// Mutant: `OwnedChild::new` reaps or looks at an already-exited child on adoption.
#[cfg(unix)]
#[skuld::test]
fn adopt_never_reaps_a_zombie() {
    let (child, stdin) = super::fixtures::spawn_std_blocker();
    let id = super::fixtures::identity_of(&child);
    drop(stdin);
    // The exit, seen without consuming it.
    // 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());

    let owned = OwnedChild::adopt(child, id).unwrap_or_else(|(e, _)| panic!("adopt: {e}"));
    // Still a zombie: a non-consuming look finds its exit record.
    // SAFETY: as above.
    let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
    let r = unsafe {
        libc::waitid(
            libc::P_PID,
            id.pid() as libc::id_t,
            &mut info,
            libc::WEXITED | libc::WNOHANG | libc::WNOWAIT,
        )
    };
    assert_eq!(r, 0, "waitid: {}", std::io::Error::last_os_error());
    #[cfg(target_os = "linux")]
    // SAFETY: `waitid` filled the `siginfo_t` (an exit record), so `si_pid` is initialised.
    let seen = unsafe { info.si_pid() };
    #[cfg(target_os = "macos")]
    let seen = info.si_pid;
    assert_ne!(seen, 0, "adopt must leave the exited child a zombie");
    assert!(super::fixtures::wait_status(&owned).success());
}

// Deadline arithmetic =====

/// `clamp_block` caps every timed block below the value the platforms read as `INFINITE`.
///
/// Mutant: an unclamped `wait_timeout`.
#[skuld::test]
fn clamp_block_caps_every_timed_block_below_infinite() {
    let cap = Duration::from_millis(0xFFFF_FFFE);
    for huge in [
        Duration::MAX,
        Duration::from_millis(u64::from(u32::MAX)),
        Duration::from_millis(0xFFFF_FFFE),
    ] {
        assert_eq!(crate::wait::clamp_block(huge), cap);
    }
    assert_eq!(
        crate::wait::clamp_block(Duration::from_millis(5)),
        Duration::from_millis(5)
    );
}

// The state of the root =====

/// The state is `Reaped` once the reap is recorded, before the waiter returns, for every shape of
/// deadline; and it reads the record, not the OS.
///
/// Mutant: `state` answers `Unreaped` once the reap is recorded.
#[skuld::test]
fn the_state_is_reaped_as_soon_as_the_reap_is_recorded() {
    for deadline in [None, Some(far()), Some(Instant::now())] {
        // No peek is scripted: a `state` that asks the OS panics.
        let fake = FakeProcess::new([]).reaps([ReapAnswer::Reaped(exit())]);
        let child = OwnedChild::new(Box::new(fake));
        let (gate, reached, release) = seams::park_gate();
        std::thread::scope(|scope| {
            let a = scope.spawn(|| {
                let _armed = seams::park_after_reap_recorded_on(gate);
                child.wait(deadline)
            });
            reached.recv().expect("the reap was recorded");
            #[cfg(unix)]
            let seen = matches!(
                child.hold(UNREAD_PID).state(),
                crate::signal::RootState::Reaped { own: true }
            );
            let seen_own = child.is_reaped();
            release.send(()).expect("release");
            assert!(matches!(a.join().expect("join"), Ok(ChildWaitOutcome::Exited(_))));
            #[cfg(unix)]
            assert!(seen, "the reap was recorded, but the state was not Reaped");
            assert!(seen_own, "the reap was recorded, but is_reaped answered false");
        });
    }
}

/// The state is `Unreaped` until a look returns the record, and `Reaped { own }` from then on,
/// asked of the record and not of the OS.
///
/// Mutant: `state` answers `Reaped` for a state that is not `E`, or asks the OS after `E`.
#[cfg(unix)]
#[skuld::test]
fn the_state_is_an_own_reap_only_once_a_look_returns_the_record() {
    use crate::signal::RootState;
    let fake = FakeProcess::new([])
        .reaps([ReapAnswer::Running, ReapAnswer::Reaped(exit())])
        .peeks([Ok(PeekOutcome::Running)]);
    let child = OwnedChild::new(Box::new(fake));
    assert_eq!(try_wait(&child).expect("try_wait"), ChildWaitOutcome::DeadlinePassed);
    assert!(matches!(child.hold(UNREAD_PID).state(), RootState::Unreaped));
    assert!(!child.is_reaped());
    status(try_wait(&child));
    // The one scripted peek is spent: a `state` that asks the OS now panics.
    assert!(matches!(
        child.hold(UNREAD_PID).state(),
        RootState::Reaped { own: true }
    ));
    assert!(child.is_reaped());
}

/// A hold's group check reads the hold's own state: an unreaped root's pid names its group, a root
/// reaped by someone else or by this handle does not, whatever the pid reads. The own-reaped root's
/// peek table is empty, so a check that peeks panics.
///
/// Mutant: the check passes whatever the state, reads the OS instead of the hold, or passes an own
/// reap whose pid names no process.
#[cfg(unix)]
#[skuld::test]
fn a_hold_checks_the_roots_group_by_its_own_state() {
    use crate::containment::check_number;
    use crate::identity::Resolved;
    let fake = FakeProcess::new([]).peeks([Ok(PeekOutcome::Running), Ok(PeekOutcome::Foreign(Foreign::Gone))]);
    let child = OwnedChild::new(Box::new(fake));
    let check = || check_number(Some(&child.hold(UNREAD_PID))).map_err(|why| why.to_string());
    assert_eq!(check(), Ok(()));
    let refused = check().expect_err("reaped elsewhere");
    assert!(refused.contains("reaped elsewhere"), "{refused}");

    let fake = FakeProcess::new([]).reaps([ReapAnswer::Reaped(exit())]);
    let child = OwnedChild::new(Box::new(fake));
    status(child.wait(None));
    let check = || check_number(Some(&child.hold(UNREAD_PID))).map_err(|why| why.to_string());
    {
        let _gone = crate::child::fault::force_root_reads(Resolved::Gone);
        let refused = check().expect_err("the pid names no process");
        assert!(refused.contains("names no process"), "{refused}");
    }
    let _unknown = crate::child::fault::force_root_reads(Resolved::Unknown);
    let refused = check().expect_err("the pid could not be read");
    assert!(refused.contains("this handle reaped the root"), "{refused}");
}

/// A waiter parked on its wait is a holder (`W`), not a reap.
///
/// Mutant: `state` answers `Reaped` for a state that is not `E`.
#[cfg(unix)]
#[skuld::test]
fn the_state_is_unreaped_while_a_holder_waits() {
    use crate::signal::RootState;
    let fake = FakeProcess::new([])
        .reaps([ReapAnswer::Running, ReapAnswer::Reaped(exit())])
        .peeks([Ok(PeekOutcome::Running)]);
    let child = owned_over(&fake);
    let holder = park_holder(&child, &fake, |c| c.wait(None));
    let seen = child.hold(UNREAD_PID).state();
    let seen_own = child.is_reaped();
    status(holder.release_and_join());
    assert!(matches!(seen, RootState::Unreaped), "{seen:?}");
    assert!(!seen_own, "a holder is waiting, but is_reaped answered true");
    assert!(child.is_reaped());
}