sail-solve 0.3.0

Fast, cached Verus verification for Rust: standalone files, projects, watch and agent workflows
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
//! Sequential, independently verified, complete-run seed portfolios.

use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, ExitCode, Stdio};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use sail_api::output::{
    PortfolioAttempt, PortfolioReport, Report, SolverConfiguration, VerdictName,
};

/// An explicitly requested complete-run portfolio.
pub(crate) struct Options {
    seeds: Vec<u32>,
    timeout: Duration,
    json: bool,
    args: Vec<String>,
}

/// Parses only sail's portfolio flags before the Verus argument separator.
pub(crate) fn parse(args: &[String]) -> Result<Option<Options>, String> {
    let mut enabled = false;
    let mut seeds = (1..8).collect::<Vec<u32>>();
    let mut timeout = 3600;
    let mut customized = false;
    let mut json = false;
    let mut rest = Vec::new();
    let mut it = args.iter();
    while let Some(arg) = it.next() {
        if arg == "--" {
            rest.push(arg.clone());
            rest.extend(it.cloned());
            break;
        }
        let (key, inline) = arg
            .split_once('=')
            .map_or((arg.as_str(), None), |(k, v)| (k, Some(v)));
        let mut value = || {
            inline
                .map(str::to_owned)
                .or_else(|| it.next().cloned())
                .ok_or_else(|| format!("{key} requires a value"))
        };
        match key {
            "--portfolio" => {
                if value()? != "accept-first-proof" {
                    return Err("only --portfolio accept-first-proof is supported".to_owned());
                }
                enabled = true;
            }
            "--portfolio-seeds" => {
                customized = true;
                seeds = value()?
                    .split(',')
                    .map(|s| {
                        s.parse::<u32>()
                            .map_err(|_| "portfolio seeds must be positive u32 integers".to_owned())
                    })
                    .collect::<Result<_, _>>()?;
                let distinct: std::collections::BTreeSet<_> = seeds.iter().collect();
                if seeds.is_empty() || seeds.contains(&0) || distinct.len() != seeds.len() {
                    return Err(
                        "portfolio seeds must be distinct and positive; baseline is always first"
                            .to_owned(),
                    );
                }
            }
            "--verus-source" => {
                customized = true;
                rest.extend(["--verus-source".to_owned(), value()?]);
            }
            "--portfolio-timeout-secs" => {
                customized = true;
                timeout = value()?
                    .parse()
                    .map_err(|_| "invalid portfolio timeout".to_owned())?;
                if timeout == 0 {
                    return Err("portfolio timeout must be positive".to_owned());
                }
            }
            "--output" => {
                let v = value()?;
                json = v == "json";
                rest.extend([key.to_owned(), v]);
            }
            _ => rest.push(arg.clone()),
        }
    }
    if !enabled {
        return if customized {
            Err("portfolio options require --portfolio accept-first-proof".to_owned())
        } else {
            Ok(None)
        };
    }
    validate(&rest)?;
    // The worker always produces JSON; the parent chooses the user format.
    let mut clean = Vec::new();
    let mut it = rest.into_iter();
    while let Some(arg) = it.next() {
        if arg == "--" {
            clean.push(arg);
            clean.extend(it);
            break;
        }
        if arg == "--output" {
            if it.next().as_deref() != Some("json") {
                return Err("only --output json is supported".to_owned());
            }
        } else {
            clean.push(arg);
        }
    }
    seeds.insert(0, 0);
    Ok(Some(Options {
        seeds,
        timeout: Duration::from_secs(timeout),
        json,
        args: clean,
    }))
}

fn validate(rest: &[String]) -> Result<(), String> {
    if rest.iter().any(|s| {
        s == "--target-dir"
            || s.starts_with("--target-dir=")
            || s == "--no-verify"
            || s.starts_with("--no-verify=")
            || s.starts_with("smt.random_seed=")
            || s.starts_with("sat.random_seed=")
            || s.contains("=smt.random_seed=")
            || s.contains("=sat.random_seed=")
    }) {
        return Err(
            "portfolio controls seeds and target directory, and requires verification".to_owned(),
        );
    }
    Ok(())
}

/// Whether the requested Verus scope requires cargo-verus focus.
pub(crate) fn partial(args: &[String]) -> bool {
    args.iter().skip_while(|s| *s != "--").any(|s| {
        [
            "--verify-module",
            "--verify-only-module",
            "--verify-function",
            "--verify-root",
        ]
        .iter()
        .any(|flag| s == flag || s.starts_with(&format!("{flag}=")))
    })
}

/// Only a complete, successful verification can win a portfolio.
fn complete(report: &Report) -> bool {
    report.exit_code == Some(0)
        && report.error.is_none()
        && !report.crates.is_empty()
        && report.invariant_violations().is_empty()
        && report.crates.iter().all(|c| {
            c.verus.errors == Some(0)
                && c.verus.success != Some(false)
                && !c.functions.is_empty()
                && c.functions
                    .iter()
                    .all(|f| f.verdict == VerdictName::Verified)
        })
        && report
            .crates
            .iter()
            .any(|c| c.verus.verified.is_some_and(|n| n > 0))
}

fn manifest_args(args: &[String]) -> Vec<String> {
    let mut out = Vec::new();
    let mut it = args.iter().take_while(|s| *s != "--");
    while let Some(arg) = it.next() {
        if arg == "--manifest-path" {
            out.push(arg.clone());
            if let Some(value) = it.next() {
                out.push(value.clone());
            }
        } else if arg.starts_with("--manifest-path=") {
            out.push(arg.clone());
        }
    }
    out
}

/// Cleans only verification-enabled packages, only in this request's target.
pub(crate) fn force_verification(
    args: &[String],
    target: &Path,
    focus: bool,
) -> Result<Vec<String>, String> {
    let cargo = std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into());
    let manifests = manifest_args(args);
    let output = Command::new(&cargo)
        .args(["metadata", "--format-version", "1"])
        .args(&manifests)
        .output()
        .map_err(|e| e.to_string())?;
    if !output.status.success() {
        return Err(String::from_utf8_lossy(&output.stderr).into_owned());
    }
    let metadata: serde_json::Value =
        serde_json::from_slice(&output.stdout).map_err(|e| e.to_string())?;
    let required = crate::portfolio_scope::required(&metadata, args)?;
    let members = metadata
        .get("workspace_members")
        .and_then(serde_json::Value::as_array)
        .cloned()
        .unwrap_or_default();
    let packages: Vec<(&str, &str)> = metadata
        .get("packages")
        .and_then(serde_json::Value::as_array)
        .into_iter()
        .flatten()
        .filter(|p| {
            members.iter().any(|id| p.get("id") == Some(id))
                && p.pointer("/metadata/verus/verify")
                    .and_then(serde_json::Value::as_bool)
                    == Some(true)
        })
        .filter_map(|p| Some((p.get("id")?.as_str()?, p.get("name")?.as_str()?)))
        .collect();
    if packages.is_empty() {
        return Err("no verification-enabled packages in Cargo metadata".to_owned());
    }
    // cargo-verus focus puts its artifacts in a child target directory.
    let clean_target = if focus {
        target.join("verus-partial")
    } else {
        target.to_path_buf()
    };
    let mut clean = Command::new(cargo);
    clean
        .arg("clean")
        .args(&manifests)
        .arg("--target-dir")
        .arg(clean_target);
    for (package, _) in &packages {
        clean.args(["-p", *package]);
    }
    let output = clean.output().map_err(|e| e.to_string())?;
    if output.status.success() {
        Ok(required)
    } else {
        Err(String::from_utf8_lossy(&output.stderr).into_owned())
    }
}

fn worker_args(args: &[String], seed: u32, target: &Path) -> Vec<String> {
    let split = args.iter().position(|s| s == "--").unwrap_or(args.len());
    let mut out = args[..split].to_vec();
    out.extend([
        "--target-dir".to_owned(),
        target.to_string_lossy().into_owned(),
        "--".to_owned(),
    ]);
    out.extend(args.get(split + 1..).unwrap_or_default().iter().cloned());
    if seed != 0 {
        out.extend([
            "--smt-option".to_owned(),
            format!("smt.random_seed={seed}"),
            "--smt-option".to_owned(),
            format!("sat.random_seed={seed}"),
        ]);
    }
    out
}

/// Sends `SIGKILL` to the process group led by `pid` through the kernel,
/// independent of the caller's `PATH` (#760).
#[cfg(unix)]
fn kill_group(pid: u32) {
    if let Ok(raw) = i32::try_from(pid)
        && let Some(pid) = rustix::process::Pid::from_raw(raw)
    {
        let _ = rustix::process::kill_process_group(pid, rustix::process::Signal::KILL);
    }
}

/// Kills this worker's own process group. If the kernel refuses the group
/// signal, the worker still kills itself, so it never outlives its owner.
#[cfg(unix)]
fn kill_own_group() -> ! {
    let _ = rustix::process::kill_current_process_group(rustix::process::Signal::KILL);
    let _ = rustix::process::kill_process(rustix::process::getpid(), rustix::process::Signal::KILL);
    std::process::abort();
}

/// A worker's stdin is a liveness pipe, not solver input. Parent death or
/// cancellation closes it and kills the whole worker group.
pub(crate) fn guard_parent() {
    #[cfg(unix)]
    std::thread::spawn(|| {
        let mut byte = [0];
        let mut stdin = std::io::stdin().lock();
        loop {
            match stdin.read(&mut byte) {
                Ok(0) => kill_own_group(),
                Ok(_) => {}
                Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {}
                Err(_) => kill_own_group(),
            }
        }
    });
}

fn attempt(
    options: &Options,
    seed: u32,
    target: &Path,
    stop: &AtomicBool,
) -> Result<PortfolioAttempt, String> {
    let mut cmd = Command::new(std::env::current_exe().map_err(|e| e.to_string())?);
    cmd.arg("_portfolio-attempt")
        .arg(seed.to_string())
        .args(worker_args(&options.args, seed, target))
        .env("CARGO_TARGET_DIR", target)
        .env("SAIL_PORTFOLIO_TARGET", target)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    #[cfg(unix)]
    {
        use std::os::unix::process::CommandExt as _;
        cmd.process_group(0);
    }
    let started = Instant::now();
    let mut child = cmd.spawn().map_err(|e| e.to_string())?;
    let heartbeat = child.stdin.take();
    let stdout = child.stdout.take().map(|mut s| {
        std::thread::spawn(move || {
            let mut b = Vec::new();
            let _ = s.read_to_end(&mut b);
            b
        })
    });
    let stderr = child.stderr.take().map(|mut s| {
        std::thread::spawn(move || {
            let mut b = Vec::new();
            let _ = s.read_to_end(&mut b);
            b
        })
    });
    let mut error = None;
    let exit_code = loop {
        if stop.load(Ordering::SeqCst) || started.elapsed() >= options.timeout {
            error = Some(
                if stop.load(Ordering::SeqCst) {
                    "cancelled"
                } else {
                    "attempt timed out"
                }
                .to_owned(),
            );
            #[cfg(unix)]
            kill_group(child.id());
            let _ = child.kill();
            let _ = child.wait();
            break None;
        }
        match child.try_wait() {
            Ok(Some(status)) => break status.code(),
            Ok(None) => std::thread::sleep(Duration::from_millis(20)),
            Err(e) => {
                error = Some(e.to_string());
                #[cfg(unix)]
                kill_group(child.id());
                let _ = child.kill();
                let _ = child.wait();
                break None;
            }
        }
    };
    drop(heartbeat);
    let stdout = stdout.and_then(|h| h.join().ok()).unwrap_or_default();
    let stderr = stderr.and_then(|h| h.join().ok()).unwrap_or_default();
    let report = serde_json::from_slice::<Report>(&stdout).ok();
    if report.is_none() && error.is_none() {
        error = Some("Verus produced no valid report".to_owned());
    }
    if error.is_none()
        && exit_code == Some(0)
        && report
            .as_ref()
            .is_some_and(|r| r.crates.iter().all(|c| c.functions.is_empty()))
    {
        error = Some("Verus produced no function verification results".to_owned());
    }
    let verified = error.is_none() && exit_code == Some(0) && report.as_ref().is_some_and(complete);
    Ok(PortfolioAttempt {
        configuration: SolverConfiguration { seed },
        verified,
        exit_code,
        wall_ms: started.elapsed().as_secs_f64() * 1000.0,
        error,
        report: report.map(Box::new),
        stderr: String::from_utf8_lossy(&stderr).into_owned(),
    })
}

fn directory() -> Result<PathBuf, String> {
    let stamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|e| e.to_string())?
        .as_nanos();
    let dir = sail_verus::cache_dir()
        .map_err(|e| e.to_string())?
        .join("portfolio")
        .join(format!("{}-{stamp}", std::process::id()));
    std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?;
    Ok(dir)
}

/// Runs the sequential portfolio and renders only the selected run.
pub(crate) fn run(options: Options, out: &mut dyn Write, err: &mut dyn Write) -> ExitCode {
    let stop = Arc::new(AtomicBool::new(false));
    let flag = Arc::clone(&stop);
    if let Err(e) = ctrlc::set_handler(move || flag.store(true, Ordering::SeqCst)) {
        let _ = writeln!(
            err,
            "error[SAIL0111]: cannot handle portfolio cancellation: {e}"
        );
        return ExitCode::from(2);
    }
    let dir = match directory() {
        Ok(d) => d,
        Err(e) => {
            let _ = writeln!(err, "error[SAIL0111]: {e}");
            return ExitCode::from(2);
        }
    };
    let mut attempts = Vec::new();
    for &seed in &options.seeds {
        let _ = writeln!(
            err,
            "sail portfolio accept-first-proof: attempting seed {seed}"
        );
        let result = attempt(&options, seed, &dir.join("target"), &stop);
        let attempt = result.unwrap_or_else(|e| PortfolioAttempt {
            configuration: SolverConfiguration { seed },
            verified: false,
            exit_code: None,
            wall_ms: 0.0,
            error: Some(e),
            report: None,
            stderr: String::new(),
        });
        let won = attempt.verified;
        if let Ok(bytes) = serde_json::to_vec_pretty(&attempt)
            && let Err(e) = std::fs::write(dir.join(format!("seed-{seed}.json")), bytes)
        {
            let _ = writeln!(err, "error[SAIL0112]: cannot retain attempt evidence: {e}");
            return ExitCode::from(2);
        }
        attempts.push(attempt);
        if won || stop.load(Ordering::SeqCst) {
            break;
        }
    }
    render(
        options,
        attempts,
        &dir,
        stop.load(Ordering::SeqCst),
        out,
        err,
    )
}

fn render(
    options: Options,
    attempts: Vec<PortfolioAttempt>,
    dir: &Path,
    cancelled: bool,
    out: &mut dyn Write,
    err: &mut dyn Write,
) -> ExitCode {
    let winner = attempts.iter().position(|a| a.verified);
    let selected = winner.unwrap_or(0);
    let mut report = attempts
        .get(selected)
        .and_then(|a| a.report.as_deref())
        .cloned()
        .unwrap_or_else(|| {
            crate::report::setup_failure(
                &options.args,
                "SAIL0113",
                "portfolio established no verification result",
            )
        });
    "accept-first-proof".clone_into(&mut report.mode);
    let _ = writeln!(
        err,
        "sail portfolio accept-first-proof: {}; evidence in {}",
        winner.map_or_else(
            || "no complete proof".to_owned(),
            |i| format!("proved with seed {}", attempts[i].configuration.seed)
        ),
        dir.display()
    );
    if let Some(a) = attempts.get(selected) {
        let _ = err.write_all(a.stderr.as_bytes());
    }
    let exit = if cancelled {
        130
    } else if winner.is_some() {
        0
    } else {
        attempts
            .first()
            .and_then(|a| a.exit_code)
            .filter(|&c| c != 0)
            .unwrap_or(2)
    };
    report.exit_code = Some(exit);
    report.portfolio = Some(PortfolioReport {
        seeds: options.seeds,
        winner_seed: winner.map(|i| attempts[i].configuration.seed),
        attempts,
    });
    if options.json {
        let _ = writeln!(
            out,
            "{}",
            serde_json::to_string_pretty(&report).unwrap_or_default()
        );
    } else {
        let _ = writeln!(
            out,
            "portfolio accept-first-proof: {} verified, {} failed, {} unknown",
            report.summary.verified, report.summary.failed, report.summary.unknown
        );
    }
    ExitCode::from(u8::try_from(exit).unwrap_or(2))
}

#[cfg(test)]
mod tests {
    use super::*;
    fn args(s: &[&str]) -> Vec<String> {
        s.iter().map(|s| (*s).to_owned()).collect()
    }

    #[test]
    fn explicit_portfolio_order_and_invalid_options() {
        let o = parse(&args(&[
            "--portfolio",
            "accept-first-proof",
            "--portfolio-seeds",
            "4,2",
        ]))
        .ok()
        .flatten();
        assert_eq!(o.map(|o| o.seeds), Some(vec![0, 4, 2]));
        for s in ["0", "4,4", "-1", "4294967296", ""] {
            assert!(
                parse(&args(&[
                    "--portfolio",
                    "accept-first-proof",
                    "--portfolio-seeds",
                    s
                ]))
                .is_err()
            );
        }
        assert!(parse(&args(&["--portfolio-seeds", "4"])).is_err());
        assert!(
            parse(&args(&[
                "--portfolio",
                "accept-first-proof",
                "--",
                "--no-verify"
            ]))
            .is_err()
        );
        assert!(
            parse(&args(&[
                "--portfolio",
                "accept-first-proof",
                "--",
                "--smt-option",
                "smt.random_seed=4"
            ]))
            .is_err()
        );
        assert!(
            parse(&args(&["--", "--portfolio", "x"]))
                .ok()
                .flatten()
                .is_none()
        );
    }

    #[test]
    fn baseline_is_unmodified_and_alternatives_set_both_seeds() {
        let original = args(&["-p", "demo", "--", "--rlimit", "3"]);
        let baseline = worker_args(&original, 0, Path::new("/owned/target"));
        assert!(!baseline.iter().any(|s| s.contains("random_seed")));
        let seeded = worker_args(&original, 4, Path::new("/owned/target"));
        assert!(seeded.contains(&"smt.random_seed=4".to_owned()));
        assert!(seeded.contains(&"sat.random_seed=4".to_owned()));
        assert!(partial(&args(&["--", "--verify-function=f"])));
    }

    #[test]
    fn empty_or_failed_reports_never_establish_a_proof() {
        let mut r = crate::report::setup_failure(&[], "SAIL0113", "no evidence");
        r.exit_code = Some(0);
        r.error = None;
        assert!(!complete(&r));
    }
    #[test]
    fn independent_runs_cannot_combine_partial_proofs() {
        let mut r: Report =
            serde_json::from_str(include_str!("../tests/fixtures/attr/report.golden.json"))
                .unwrap_or_else(|e| unreachable!("fixture: {e}"));
        r.exit_code = Some(0);
        for c in &mut r.crates {
            c.verus.success = Some(true);
            c.verus.errors = Some(0);
            for f in &mut c.functions {
                f.verdict = VerdictName::Verified;
            }
        }
        r.summary = sail_api::output::Summary::of(&r.crates);
        assert!(complete(&r));
        let mut first = r.clone();
        first.crates[0].functions[0].verdict = VerdictName::Failed;
        first.summary = sail_api::output::Summary::of(&first.crates);
        let mut second = r.clone();
        second.crates[0].functions[1].verdict = VerdictName::Failed;
        second.summary = sail_api::output::Summary::of(&second.crates);
        assert!(!complete(&first));
        assert!(!complete(&second));
        r.crates[0].functions.clear();
        r.summary = sail_api::output::Summary::of(&r.crates);
        assert!(!complete(&r));
    }

    /// Child half of `guard_kills_group_without_kill_on_path`: guards its
    /// stdin, starts a group member and then waits to be killed.
    #[cfg(unix)]
    #[test]
    fn guard_child_entry() {
        if std::env::var_os("SAIL_TEST_GUARD_CHILD").is_none() {
            return;
        }
        guard_parent();
        let Ok(member) = Command::new("/bin/sleep").arg("300").spawn() else {
            return;
        };
        println!("member {}", member.id());
        let _ = std::io::stdout().flush();
        std::thread::sleep(Duration::from_secs(300));
    }

    /// The owner-death guard must stop the worker and its group even when the
    /// captured `PATH` has no `kill` executable (#760).
    #[cfg(unix)]
    #[test]
    fn guard_kills_group_without_kill_on_path() {
        use std::io::BufRead as _;
        use std::os::unix::process::CommandExt as _;
        use std::os::unix::process::ExitStatusExt as _;

        let Ok(exe) = std::env::current_exe() else {
            unreachable!("test binary path")
        };
        let mut cmd = Command::new(exe);
        cmd.args([
            "portfolio::tests::guard_child_entry",
            "--exact",
            "--nocapture",
        ])
        .env("SAIL_TEST_GUARD_CHILD", "1")
        .env("PATH", "")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .process_group(0);
        let Ok(mut child) = cmd.spawn() else {
            unreachable!("spawn guarded child")
        };
        let owner = child.stdin.take();
        let mut member = None;
        if let Some(stdout) = child.stdout.take() {
            for line in std::io::BufReader::new(stdout).lines() {
                let Ok(line) = line else { break };
                if let Some(pid) = line.strip_prefix("member ") {
                    member = pid.trim().parse::<i32>().ok();
                    break;
                }
            }
        }
        let member = member.and_then(rustix::process::Pid::from_raw);
        assert!(member.is_some(), "guarded child reported no group member");
        drop(owner);
        let deadline = Instant::now() + Duration::from_secs(10);
        let mut status = None;
        while Instant::now() < deadline {
            if let Ok(Some(s)) = child.try_wait() {
                status = Some(s);
                break;
            }
            std::thread::sleep(Duration::from_millis(20));
        }
        if status.is_none() {
            kill_group(child.id());
            let _ = child.wait();
        }
        // An orphaned member that died stays a zombie where no init process
        // reaps it (for example a CI container), so a zombie counts as dead.
        let member_alive = || {
            member.is_some_and(|p| {
                rustix::process::test_kill_process(p).is_ok()
                    && std::fs::read_to_string(format!("/proc/{}/stat", p.as_raw_nonzero()))
                        .ok()
                        .and_then(|stat| {
                            stat.rsplit_once(')')
                                .map(|(_, rest)| rest.trim_start().starts_with('Z'))
                        })
                        != Some(true)
            })
        };
        while member_alive() && Instant::now() < deadline {
            std::thread::sleep(Duration::from_millis(20));
        }
        let survived = member_alive();
        if survived && let Some(p) = member {
            let _ = rustix::process::kill_process(p, rustix::process::Signal::KILL);
        }
        assert_eq!(
            status.and_then(|s| s.signal()),
            Some(9),
            "the guarded worker outlived its owner"
        );
        assert!(!survived, "the worker's group member outlived its owner");
    }
}