hermit-run 0.5.0

Hermit: a deterministic Linux execution engine for reproducible runs, record and replay, and concurrency-race search.
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
/* Copyright (c) Meta Platforms, Inc. and affiliates. */
//! Maintained CLI process-lifecycle regression. An explicit private role
//! enters before runtime threads; each case has its own process because retained
//! owners deliberately keep process-wide admission closed. Actual production
//! adapter and backing guards are used, not a Detcore failure simulator.
use std::cell::RefCell;
use std::ffi::CString;
use std::mem::ManuallyDrop;
use std::os::fd::AsRawFd;
use std::os::fd::FromRawFd;
use std::os::fd::OwnedFd;
use std::rc::Rc;
use std::sync::atomic::AtomicI32;
use std::sync::atomic::Ordering;
use std::time::Duration;
use std::time::Instant;

use reverie::process::Container;
use reverie::process::Namespace;
use serde::Deserialize;
use serde::Serialize;
use serde::Serializer;

#[repr(C)]
struct Shared {
    pid: AtomicI32,
    ready: AtomicI32,
    check: AtomicI32,
    observed: AtomicI32,
    stop: AtomicI32,
    drops: AtomicI32,
    retired_before_drop: AtomicI32,
    monitor_self_exit_reason: AtomicI32,
}
struct WorkerObservation {
    receiver: std::os::unix::net::UnixDatagram,
    fd: RefCell<Option<OwnedFd>>,
}
impl WorkerObservation {
    fn receive_once(&self) {
        if self.fd.borrow().is_none() {
            *self.fd.borrow_mut() = Some(receive_fd(self.receiver.as_raw_fd()));
        }
    }
}
struct Guard {
    directory: ManuallyDrop<tempfile::TempDir>,
    original: i32,
    shared: *const Shared,
    worker: Rc<WorkerObservation>,
}
impl Drop for Guard {
    fn drop(&mut self) {
        if unsafe { libc::getpid() } == self.original {
            let shared = unsafe { &*self.shared };
            if shared.pid.load(Ordering::SeqCst) > 0 {
                // Observe the descriptor transferred by the original worker's
                // parent before releasing any backing path. A later sample
                // after run returns cannot establish this ordering.
                self.worker.receive_once();
                let fd = self.worker.fd.borrow();
                shared
                    .retired_before_drop
                    .store(i32::from(ready(fd.as_ref().unwrap())), Ordering::SeqCst);
            }
            unsafe { &*self.shared }
                .drops
                .fetch_add(1, Ordering::SeqCst);
            unsafe { ManuallyDrop::drop(&mut self.directory) };
        }
    }
}
#[derive(Debug, Deserialize)]
struct Value(u64);
static BAD_WIRE: AtomicI32 = AtomicI32::new(0);
impl Serialize for Value {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        if BAD_WIRE.load(Ordering::SeqCst) == 1 {
            serializer.serialize_u8(7)
        } else {
            serializer.serialize_u64(self.0)
        }
    }
}
pub(crate) fn wait(deadline: Instant, mut yes: impl FnMut() -> bool) -> bool {
    loop {
        if Instant::now() >= deadline {
            return false;
        }
        if yes() {
            return true;
        }
        std::thread::yield_now();
    }
}
pub(crate) fn pidfd(pid: i32) -> OwnedFd {
    let fd = unsafe { libc::syscall(libc::SYS_pidfd_open, pid, 0) } as i32;
    assert!(fd >= 0, "pidfd: {}", std::io::Error::last_os_error());
    unsafe { OwnedFd::from_raw_fd(fd) }
}
pub(crate) fn transfer_fd(socket: i32, fd: i32) {
    let mut byte = 1u8;
    let mut iov = libc::iovec {
        iov_base: (&mut byte as *mut u8).cast(),
        iov_len: 1,
    };
    let mut control = [0usize; 8];
    let mut msg: libc::msghdr = unsafe { std::mem::zeroed() };
    msg.msg_iov = &mut iov;
    msg.msg_iovlen = 1;
    msg.msg_control = control.as_mut_ptr().cast();
    msg.msg_controllen = unsafe { libc::CMSG_SPACE(std::mem::size_of::<i32>() as u32) } as usize;
    unsafe {
        let c = libc::CMSG_FIRSTHDR(&msg);
        (*c).cmsg_level = libc::SOL_SOCKET;
        (*c).cmsg_type = libc::SCM_RIGHTS;
        (*c).cmsg_len = libc::CMSG_LEN(std::mem::size_of::<i32>() as u32) as usize;
        std::ptr::write(libc::CMSG_DATA(c).cast::<i32>(), fd);
        assert_eq!(libc::sendmsg(socket, &msg, 0), 1);
    }
}
pub(crate) fn receive_fd(socket: i32) -> OwnedFd {
    let mut byte = 0u8;
    let mut iov = libc::iovec {
        iov_base: (&mut byte as *mut u8).cast(),
        iov_len: 1,
    };
    let mut control = [0usize; 8];
    let mut msg: libc::msghdr = unsafe { std::mem::zeroed() };
    msg.msg_iov = &mut iov;
    msg.msg_iovlen = 1;
    msg.msg_control = control.as_mut_ptr().cast();
    msg.msg_controllen = std::mem::size_of_val(&control);
    unsafe {
        assert_eq!(
            libc::recvmsg(
                socket,
                &mut msg,
                libc::MSG_DONTWAIT | libc::MSG_CMSG_CLOEXEC
            ),
            1
        );
        let c = libc::CMSG_FIRSTHDR(&msg);
        assert!(!c.is_null());
        assert_eq!((*c).cmsg_type, libc::SCM_RIGHTS);
        OwnedFd::from_raw_fd(std::ptr::read(libc::CMSG_DATA(c).cast::<i32>()))
    }
}
pub(crate) fn ready(fd: &OwnedFd) -> bool {
    let mut p = libc::pollfd {
        fd: fd.as_raw_fd(),
        events: libc::POLLIN,
        revents: 0,
    };
    let n = unsafe { libc::poll(&mut p, 1, 0) };
    assert!(n >= 0, "poll refusal");
    n > 0
}
// These are fixture worker exits, never guest/product completion receipts.
const WORKER_SUPERVISOR_GONE: i32 = 38;
const WORKER_ABSOLUTE_CEILING: i32 = 39;
const WORKER_MONITOR_REFUSED: i32 = 40;

fn container_case(mode: String, deadline: Instant, end: u64) -> i32 {
    let start = Instant::now();
    assert_eq!(
        std::fs::read_dir("/proc/self/task").unwrap().count(),
        1,
        "call-before-threads precondition"
    );
    assert_eq!(
        unsafe { libc::prctl(libc::PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0) },
        0
    );
    let memory = unsafe {
        libc::mmap(
            std::ptr::null_mut(),
            std::mem::size_of::<Shared>(),
            libc::PROT_READ | libc::PROT_WRITE,
            libc::MAP_SHARED | libc::MAP_ANONYMOUS,
            -1,
            0,
        )
    };
    assert_ne!(memory, libc::MAP_FAILED);
    let shared = memory.cast::<Shared>();
    unsafe {
        shared.write(Shared {
            pid: AtomicI32::new(0),
            ready: AtomicI32::new(0),
            check: AtomicI32::new(0),
            observed: AtomicI32::new(-1),
            stop: AtomicI32::new(0),
            drops: AtomicI32::new(0),
            retired_before_drop: AtomicI32::new(-1),
            monitor_self_exit_reason: AtomicI32::new(0),
        })
    };
    let s = unsafe { &*shared };
    let directory = tempfile::tempdir().unwrap();
    let path = directory.path().to_owned();
    let (receiver, sender) = std::os::unix::net::UnixDatagram::pair().unwrap();
    let worker_observation = Rc::new(WorkerObservation {
        receiver,
        fd: RefCell::new(None),
    });
    let guard = Guard {
        directory: ManuallyDrop::new(directory),
        original: unsafe { libc::getpid() },
        shared,
        worker: worker_observation.clone(),
    };
    let child_path = CString::new(path.as_os_str().as_encoded_bytes()).unwrap();
    let private = mode.starts_with("private-");
    let success = mode == "success";
    let mut container = Container::new();
    if private {
        container.unshare(Namespace::PID).map_root();
    }
    let selected = mode.clone();
    // Capture T while it is live, before P/G exist. P deliberately exits in
    // these cases; a death signal tied to P would invalidate their live-G proof.
    let supervisor = pidfd(unsafe { libc::getpid() });
    let worker_ceiling = end.checked_add(2_000_000_000).unwrap();
    let result = super::owned_container::run(
        &mut container,
        guard,
        path.display().to_string(),
        private,
        "owned-container-lifecycle",
        None,
        move |_| {
            if success {
                return Ok(Value(41));
            }
            let child = unsafe { libc::fork() };
            assert!(child >= 0);
            if child == 0 {
                // Host procfs remains mounted, even in the private PID namespace.
                let host_pid = std::fs::read_link("/proc/self")
                    .unwrap()
                    .to_str()
                    .unwrap()
                    .parse::<i32>()
                    .unwrap();
                s.pid.store(host_pid, Ordering::SeqCst);
                // Close inherited workload descriptors except the original T
                // pidfd. This descriptor cannot retarget after PID reuse.
                let monitor = supervisor.as_raw_fd() as u32;
                assert!(monitor >= 3);
                if monitor > 3 {
                    assert_eq!(
                        unsafe { libc::syscall(libc::SYS_close_range, 3u32, monitor - 1, 0) },
                        0
                    );
                }
                assert_eq!(
                    unsafe { libc::syscall(libc::SYS_close_range, monitor + 1, u32::MAX, 0) },
                    0
                );
                s.ready.store(1, Ordering::SeqCst);
                loop {
                    if s.check.load(Ordering::SeqCst) == 1 {
                        let exists = unsafe { libc::access(child_path.as_ptr(), libc::F_OK) } == 0;
                        s.observed.store(i32::from(exists), Ordering::SeqCst);
                    }
                    if s.stop.load(Ordering::SeqCst) == 1 {
                        unsafe { libc::_exit(37) }
                    }
                    let mut owner = libc::pollfd {
                        fd: supervisor.as_raw_fd(),
                        events: libc::POLLIN,
                        revents: 0,
                    };
                    let polled = unsafe { libc::poll(&mut owner, 1, 0) };
                    if polled < 0 || owner.revents & (libc::POLLERR | libc::POLLNVAL) != 0 {
                        s.monitor_self_exit_reason
                            .store(WORKER_MONITOR_REFUSED, Ordering::SeqCst);
                        unsafe { libc::_exit(WORKER_MONITOR_REFUSED) }
                    }
                    if owner.revents & libc::POLLIN != 0 {
                        s.monitor_self_exit_reason
                            .store(WORKER_SUPERVISOR_GONE, Ordering::SeqCst);
                        unsafe { libc::_exit(WORKER_SUPERVISOR_GONE) }
                    }
                    // Independent of T reaching its stop store. This is the
                    // original 3s absolute budget plus the existing 2s rescue,
                    // never extra time for the original product predicate.
                    let mut now: libc::timespec = unsafe { std::mem::zeroed() };
                    if unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut now) } != 0 {
                        s.monitor_self_exit_reason
                            .store(WORKER_MONITOR_REFUSED, Ordering::SeqCst);
                        unsafe { libc::_exit(WORKER_MONITOR_REFUSED) }
                    }
                    let now = now.tv_sec as u64 * 1_000_000_000 + now.tv_nsec as u64;
                    if now >= worker_ceiling {
                        s.monitor_self_exit_reason
                            .store(WORKER_ABSOLUTE_CEILING, Ordering::SeqCst);
                        unsafe { libc::_exit(WORKER_ABSOLUTE_CEILING) }
                    }
                    unsafe {
                        libc::sched_yield();
                    }
                }
            }
            let original_worker = pidfd(child);
            transfer_fd(sender.as_raw_fd(), original_worker.as_raw_fd());
            assert!(wait(deadline, || s.ready.load(Ordering::SeqCst) == 1));
            match selected.as_str() {
                "abnormal"
                | "private-abnormal"
                | "containment-child-assertion"
                | "containment-child-owner-death" => unsafe { libc::_exit(23) },
                "reported" => Err(anyhow::anyhow!("ORIGINAL_REPORTED_FAILURE")),
                "malformed" => {
                    BAD_WIRE.store(1, Ordering::SeqCst);
                    Ok(Value(41))
                }
                _ => panic!("unknown mode"),
            }
        },
    );
    let elapsed = start.elapsed();
    let (classification, message, retained, typed_status) = match &result {
        Ok((value, _)) => (format!("success:{}", value.0), String::new(), false, false),
        Err(error) => (
            super::classify_failure(error),
            format!("{error:#}"),
            error
                .downcast_ref::<super::owned_container::ParentCleanupUnconfirmed>()
                .is_some(),
            error
                .downcast_ref::<super::container::ContainerChildExit>()
                .is_some_and(|x| x.0 == reverie::process::ExitStatus::Exited(23)),
        ),
    };
    let worker = s.pid.load(Ordering::SeqCst);
    let fd = if worker > 0 {
        worker_observation.receive_once();
        worker_observation.fd.borrow_mut().take()
    } else {
        None
    };
    let live = fd.as_ref().is_some_and(|fd| !ready(fd));
    let exists = path.exists();
    if mode.starts_with("containment-child-") {
        // The control may trigger failure only after this actual no-namespace
        // outcome: P exited23, while original G and its backing remain live.
        assert!(!private && live && exists && retained && typed_status);
        let channel = unsafe { std::os::unix::net::UnixDatagram::from_raw_fd(libc::STDIN_FILENO) };
        channel
            .set_read_timeout(Some(deadline.saturating_duration_since(Instant::now())))
            .unwrap();
        transfer_fd(channel.as_raw_fd(), fd.as_ref().unwrap().as_raw_fd());
        let proof = serde_json::to_vec(&serde_json::json!({
            "worker":worker, "live":live, "guard_exists":exists,
            "retained":retained, "typed_original_exit23":typed_status,
            "phase":"before-stop-store"
        }))
        .unwrap();
        assert_eq!(channel.send(&proof).unwrap(), proof.len());
        let mut trigger = [0u8; 1];
        channel
            .set_read_timeout(Some(deadline.saturating_duration_since(Instant::now())))
            .unwrap();
        assert_eq!(channel.recv(&mut trigger).unwrap(), 1);
        assert_eq!(trigger, [1]);
        // Restrict observation of this private control process before induced
        // failure. Only T's mm changes; G already exists. The assertion still
        // unwinds to exit101; this precaution is not a core-dump mechanism.
        assert_eq!(unsafe { libc::prctl(libc::PR_SET_DUMPABLE, 0, 0, 0, 0) }, 0);
        assert_eq!(channel.send(&[2]).unwrap(), 1);
        if mode == "containment-child-assertion" {
            assert_ne!(
                trigger,
                [1],
                "intentional original CLI assertion before stop store"
            );
        }
        assert_eq!(mode, "containment-child-owner-death");
        // R owns the original T pidfd and kills it only after the live proof.
        loop {
            unsafe {
                libc::pause();
            }
        }
    }
    if live {
        s.check.store(1, Ordering::SeqCst);
        assert!(wait(deadline, || s.observed.load(Ordering::SeqCst) != -1));
    }
    let observed = s.observed.load(Ordering::SeqCst);
    let before_deadline = Instant::now() < deadline;
    let monitor_self_exit_reason = s.monitor_self_exit_reason.load(Ordering::SeqCst);
    let predicate = if success {
        result.as_ref().is_ok_and(|(v, _)| v.0 == 41)
            && exists
            && s.drops.load(Ordering::SeqCst) == 0
    } else if private {
        result.is_err()
            && worker > 0
            && fd.is_some()
            && !live
            && !exists
            && !retained
            && typed_status
            && s.drops.load(Ordering::SeqCst) == 1
            && s.retired_before_drop.load(Ordering::SeqCst) == 1
    } else {
        result.is_err()
            && worker > 0
            && fd.is_some()
            && live
            && exists
            && observed == 1
            && retained
            && s.drops.load(Ordering::SeqCst) == 0
            && (mode != "abnormal" || typed_status)
            && (mode != "reported" || message.contains("ORIGINAL_REPORTED_FAILURE"))
            && (mode != "malformed"
                || (message.contains("UnexpectedEnd") && message.contains("decode refused")))
    } && before_deadline
        && monitor_self_exit_reason == 0;
    println!(
        "{}",
        serde_json::json!({"mode":mode,"predicate":predicate,"before_deadline":before_deadline,"seconds":elapsed.as_secs_f64(),"worker_host_pid":worker,"worker_live_before_cleanup":live,"guard_exists":exists,"worker_observed_guard":observed,"parent_guard_drops":s.drops.load(Ordering::SeqCst),"worker_retired_before_guard_drop":s.retired_before_drop.load(Ordering::SeqCst),"retained_diagnostic":retained,"typed_original_exit23":typed_status,"class":classification,"message":message,"monitor_self_exit_reason":monitor_self_exit_reason,"phase":"original predicate before separate cleanup"})
    );
    // Separate two-second diagnostic teardown, never used to satisfy predicate.
    let rescue = Instant::now() + Duration::from_secs(2);
    s.stop.store(1, Ordering::SeqCst);
    if let Some(fd) = &fd {
        if !wait(rescue, || ready(fd)) {
            assert_eq!(
                unsafe {
                    libc::syscall(
                        libc::SYS_pidfd_send_signal,
                        fd.as_raw_fd(),
                        libc::SIGKILL,
                        std::ptr::null::<libc::siginfo_t>(),
                        0,
                    )
                },
                0
            );
        }
        let mut status = 0;
        let mut got = 0;
        let mut wait_errno = None;
        assert!(wait(rescue, || {
            got = unsafe { libc::waitpid(worker, &mut status, libc::WNOHANG) };
            wait_errno = (got < 0)
                .then(|| std::io::Error::last_os_error().raw_os_error())
                .flatten();
            got != 0
        }));
        println!(
            "{}",
            serde_json::json!({"phase":"separate teardown","pid":worker,"waitpid":got,"raw_status":status,"wait_errno":wait_errno,"ready":ready(fd),"within_rescue":Instant::now()<rescue})
        );
        assert!(got == worker || (private && got == -1 && wait_errno == Some(libc::ECHILD)));
    }
    drop(result);
    if predicate { 0 } else { 1 }
}

fn containment_control(mode: &str, deadline: Instant, end: u64) -> i32 {
    use std::os::unix::fs::MetadataExt;
    use std::os::unix::process::CommandExt;
    use std::os::unix::process::ExitStatusExt;

    assert_eq!(
        unsafe { libc::prctl(libc::PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0) },
        0
    );
    let selected = match mode {
        "containment-early-assertion" => "containment-child-assertion",
        "containment-cli-owner-death" => "containment-child-owner-death",
        _ => panic!("unknown containment control"),
    };
    let (channel, child_channel) = std::os::unix::net::UnixDatagram::pair().unwrap();
    channel
        .set_read_timeout(Some(deadline.saturating_duration_since(Instant::now())))
        .unwrap();
    let parent = unsafe { libc::getpid() };
    let mut command = std::process::Command::new(std::env::current_exe().unwrap());
    command
        .env("HERMIT_INTERNAL_CLI_LIFECYCLE", "1")
        .args(["__hermit-cli-lifecycle", selected, &end.to_string()])
        .stdin(std::process::Stdio::from(OwnedFd::from(child_channel)));
    // T depends on its true supervisor R, unlike G's deliberately exiting P.
    // If a control panics, R's death stops T and G observes its original pidfd.
    unsafe {
        command.pre_exec(move || {
            if libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGKILL, 0, 0, 0) != 0 {
                return Err(std::io::Error::last_os_error());
            }
            if libc::getppid() != parent {
                return Err(std::io::Error::from_raw_os_error(libc::ESRCH));
            }
            Ok(())
        });
    }
    let mut child = command.spawn().unwrap();
    let cli = pidfd(i32::try_from(child.id()).unwrap());
    assert!(wait(deadline, || {
        let mut socket = libc::pollfd {
            fd: channel.as_raw_fd(),
            events: libc::POLLIN,
            revents: 0,
        };
        let n = unsafe { libc::poll(&mut socket, 1, 0) };
        assert!(n >= 0);
        n == 1 && socket.revents & libc::POLLIN != 0
    }));
    // The fd comes from P's original handle via T; no numeric-PID reacquisition.
    let worker = receive_fd(channel.as_raw_fd());
    let mut bytes = [0u8; 1024];
    channel
        .set_read_timeout(Some(deadline.saturating_duration_since(Instant::now())))
        .unwrap();
    let n = channel.recv(&mut bytes).unwrap();
    let proof: serde_json::Value = serde_json::from_slice(&bytes[..n]).unwrap();
    assert_eq!(proof["phase"], "before-stop-store");
    for field in ["live", "guard_exists", "retained", "typed_original_exit23"] {
        assert_eq!(proof[field], true, "missing actual proof: {field}");
    }
    let pid = i32::try_from(proof["worker"].as_i64().unwrap()).unwrap();
    assert!(pid > 0 && !ready(&worker) && !ready(&cli));
    let proc_path = format!("/proc/{pid}");
    let inode = std::fs::metadata(&proc_path).unwrap().ino();
    let stat = std::fs::read_to_string(format!("{proc_path}/stat")).unwrap();
    let start: u64 = stat
        .rsplit_once(')')
        .unwrap()
        .1
        .split_whitespace()
        .nth(19)
        .unwrap()
        .parse()
        .unwrap();
    assert!(start > 0);
    println!(
        "{}",
        serde_json::json!({
            "phase":"sealed original live worker before induced CLI failure",
            "control":mode,"worker":pid,"start":start,"inode":inode,
            "worker_pidfd_ready":false,"proof":proof
        })
    );
    assert_eq!(channel.send(&[1]).unwrap(), 1);
    let mut armed = [0u8; 1];
    channel
        .set_read_timeout(Some(deadline.saturating_duration_since(Instant::now())))
        .unwrap();
    assert_eq!(channel.recv(&mut armed).unwrap(), 1);
    assert_eq!(armed, [2], "specific pre-stop trigger was not armed");
    if mode == "containment-cli-owner-death" {
        assert!(!ready(&worker) && !ready(&cli));
        assert_eq!(
            unsafe {
                libc::syscall(
                    libc::SYS_pidfd_send_signal,
                    cli.as_raw_fd(),
                    libc::SIGKILL,
                    std::ptr::null::<libc::siginfo_t>(),
                    0,
                )
            },
            0
        );
    }
    let mut status = None;
    assert!(wait(deadline, || {
        status = child.try_wait().unwrap();
        status.is_some()
    }));
    let status = status.unwrap();
    if mode == "containment-early-assertion" {
        assert_eq!(
            status.code(),
            Some(101),
            "the intentional assertion must remain failed"
        );
    } else {
        assert_eq!(status.signal(), Some(libc::SIGKILL));
    }
    assert!(wait(deadline, || ready(&worker)));
    let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
    assert_eq!(
        unsafe {
            libc::waitid(
                libc::P_PIDFD,
                worker.as_raw_fd() as u32,
                &mut info,
                libc::WEXITED | libc::WNOHANG | libc::WNOWAIT,
            )
        },
        0
    );
    assert_eq!(unsafe { info.si_pid() }, pid);
    assert_eq!(info.si_code, libc::CLD_EXITED);
    // Exit39 (absolute ceiling),40 (monitor refusal),37 (ordinary stop), or
    // any rescue signal must FAIL this supervisor-loss control.
    assert_eq!(unsafe { info.si_status() }, WORKER_SUPERVISOR_GONE);
    assert_eq!(std::fs::metadata(&proc_path).unwrap().ino(), inode);
    let final_stat = std::fs::read_to_string(format!("{proc_path}/stat")).unwrap();
    assert_eq!(
        final_stat
            .rsplit_once(')')
            .unwrap()
            .1
            .split_whitespace()
            .nth(19)
            .unwrap()
            .parse::<u64>()
            .unwrap(),
        start
    );
    assert_eq!(
        unsafe {
            libc::waitid(
                libc::P_PIDFD,
                worker.as_raw_fd() as u32,
                &mut info,
                libc::WEXITED | libc::WNOHANG,
            )
        },
        0
    );
    assert_eq!(unsafe { info.si_pid() }, pid);
    assert_eq!(info.si_code, libc::CLD_EXITED);
    assert_eq!(unsafe { info.si_status() }, WORKER_SUPERVISOR_GONE);
    assert!(!std::path::Path::new(&proc_path).exists());
    assert!(Instant::now() < deadline);
    println!(
        "{}",
        serde_json::json!({
            "phase":"containment control only; induced CLI failure remains failed",
            "control":mode,"worker":pid,"start":start,"inode":inode,
            "actual_worker_exit":WORKER_SUPERVISOR_GONE,"naturally_reaped":true,
            "original_cli_status":status.to_string(),"rescue_used":false
        })
    );
    0
}

fn monotonic_ns() -> u64 {
    let mut now = std::mem::MaybeUninit::<libc::timespec>::uninit();
    assert_eq!(
        unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, now.as_mut_ptr()) },
        0
    );
    let now = unsafe { now.assume_init() };
    u64::try_from(now.tv_sec).unwrap() * 1_000_000_000 + u64::try_from(now.tv_nsec).unwrap()
}

/// Like the existing fault/activation/timeout controls, this is inert unless
/// explicitly selected. It is absent from ordinary help and CLI parsing; the
/// maintained integration test enters before any runtime/libtest threads.
pub(super) fn maybe_run() -> Option<i32> {
    if std::env::var_os("HERMIT_INTERNAL_CLI_LIFECYCLE").as_deref()
        != Some(std::ffi::OsStr::new("1"))
    {
        return None;
    }
    let args = std::env::args().collect::<Vec<_>>();
    if args.get(1).map(String::as_str) != Some("__hermit-cli-lifecycle") {
        return None;
    }
    assert!(
        (4..=5).contains(&args.len()),
        "exact private lifecycle role arguments"
    );
    if let Err(error) = super::retire_inherited_descriptors(&[]) {
        super::display_entry_error(
            "CLI lifecycle descriptor entry failed; use a Linux environment that allows close_range",
            &error.into(),
        );
        return Some(125);
    }
    assert_eq!(
        std::fs::read_dir("/proc/self/task").unwrap().count(),
        1,
        "lifecycle role must start before runtime/libtest threads"
    );
    let end: u64 = args[3].parse().unwrap();
    let remaining = end
        .checked_sub(monotonic_ns())
        .expect("deadline expired before role start");
    let deadline = Instant::now() + Duration::from_nanos(remaining);
    if matches!(
        args[2].as_str(),
        "containment-early-assertion" | "containment-cli-owner-death"
    ) {
        assert_eq!(args.len(), 4);
        Some(containment_control(&args[2], deadline, end))
    } else if args[2].starts_with("gdb-") {
        Some(super::gdb_client::lifecycle::run(
            &args[2],
            deadline,
            end,
            args.get(4).map(String::as_str),
        ))
    } else {
        assert_eq!(args.len(), 4);
        Some(container_case(args[2].clone(), deadline, end))
    }
}