Skip to main content

kh_runtime/process/
state.rs

1//! Process-wide translator state (single guest-process model).
2//!
3//! Hot paths avoid the process [`RwLock`]: lock-free FD map, bottle-root
4//! [`Arc`] slot, atomic `bsdthread_register` snapshot. Soft signals and
5//! directory streams still use [`with_mut`] / [`with_ref`].
6
7use std::collections::HashMap;
8use std::os::fd::RawFd;
9use std::path::{Path, PathBuf};
10use std::sync::atomic::{AtomicI32, AtomicU8, AtomicU64, Ordering};
11use std::sync::{Arc, OnceLock, RwLock};
12
13use crate::host;
14
15/// Process-wide syscall counter (hot path: no process-state lock).
16static SYSCALL_COUNT: AtomicU64 = AtomicU64::new(0);
17/// Cap for [`SYSCALL_COUNT`]; updated by [`reset_run`].
18static MAX_SYSCALLS: AtomicU64 = AtomicU64::new(256);
19
20/// Unreaped host children from guest `fork` (parent side only).
21///
22/// Secondary signal for blocking I/O waits; primary control is
23/// [`fd_guest_nonblock`] (guest `O_NONBLOCK` flag).
24static LIVE_CHILDREN: AtomicU64 = AtomicU64::new(0);
25
26/// Max guest FD slots (stdio 0–2 + table). Keeps FDs under typical OPEN_MAX.
27const FD_SLOTS: usize = 1024;
28/// Empty slot in the lock-free FD map.
29const FD_EMPTY: i32 = -1;
30
31/// Guest → host FD map for **lookup** without the process `RwLock`.
32///
33/// Slots 0–2 are unused (stdio identity). Other slots: host fd ≥ 0, or [`FD_EMPTY`].
34/// Alloc/take update these atomics; dir streams still use the process lock.
35static FD_HOST: [AtomicI32; FD_SLOTS] = [const { AtomicI32::new(FD_EMPTY) }; FD_SLOTS];
36/// Guest-visible `O_NONBLOCK` per FD slot (including stdio 0–2).
37///
38/// Host pipes/sockets are often forced non-blocking for curl multi; Darwin
39/// guests still expect **blocking** semantics until they `fcntl(F_SETFL)`.
40/// I/O emulates blocking waits when this bit is clear.
41static FD_GUEST_NB: [AtomicU8; FD_SLOTS] = [const { AtomicU8::new(0) }; FD_SLOTS];
42/// Guest FD has a host-side TLS session ([`crate::tls_fd`]).
43///
44/// Lock-free hot-path check so ordinary `read`/`write` skip the TLS HashMap.
45static FD_TLS: [AtomicU8; FD_SLOTS] = [const { AtomicU8::new(0) }; FD_SLOTS];
46/// Hint for next free FD scan.
47static FD_NEXT: AtomicI32 = AtomicI32::new(3);
48/// Guest path string for each allocated FD (for `fcntl(F_GETPATH)` after
49/// fat-thin replaces the host FD with a memfd / unlinked temp).
50static FD_GUEST_PATH: RwLock<Option<HashMap<i32, String>>> = RwLock::new(None);
51/// Guest has `close`'d stdio slots 0/1/2 (bit0=stdin, bit1=stdout, bit2=stderr).
52///
53/// Stdio is identity-mapped; soft no-op close would leave host pipes open and
54/// leak fetch-pack noise into git remote-curl helpers.
55static STDIO_CLOSED: AtomicU8 = AtomicU8::new(0);
56
57/// Soft Darwin `sigaction` slot (handler + mask + flags).
58#[derive(Clone, Copy, Debug)]
59pub(crate) struct SoftSigAct {
60    pub handler: u64,
61    pub mask: u32,
62    pub flags: i32,
63}
64
65/// Values from Darwin `bsdthread_register` (libpthread start trampoline, etc.).
66///
67/// Extra fields are retained for future workq/TLS wiring even when unused today.
68#[derive(Clone, Copy, Debug, Default)]
69#[allow(dead_code)]
70pub(crate) struct BsdThreadReg {
71    /// Userland start trampoline (`_pthread_start`).
72    pub threadstart: u64,
73    /// Workqueue thread entry (unused in micro; stored for completeness).
74    pub wqthread: u64,
75    /// Registration flags from libpthread.
76    pub flags: u32,
77    /// Stack address hint (unused in micro).
78    pub stack_addr_hint: u64,
79    /// TSD offset within the pthread structure.
80    pub tsd_offset: u32,
81    /// Dispatch queue offset within the pthread structure.
82    pub dispatchqueue_offset: u32,
83}
84
85impl SoftSigAct {
86    pub(crate) const fn zero() -> Self {
87        Self {
88            handler: 0,
89            mask: 0,
90            flags: 0,
91        }
92    }
93}
94
95/// Marker so `ProcessState::reset_run` can clear the lock-free FD map.
96#[derive(Debug, Default)]
97struct FdTable;
98
99impl FdTable {
100    const fn new() -> Self {
101        Self
102    }
103
104    fn reset(&mut self) {
105        let _ = self;
106        reset_fd_map();
107    }
108}
109
110/// Resolve guest FD → host FD without taking the process lock (I/O hot path).
111#[must_use]
112#[inline]
113pub fn fd_get(gfd: i32) -> Option<RawFd> {
114    if gfd == 0 || gfd == 1 || gfd == 2 {
115        if stdio_is_closed(gfd) {
116            return None;
117        }
118        return Some(gfd);
119    }
120    if gfd < 0 {
121        return None;
122    }
123    let Ok(idx) = usize::try_from(gfd) else {
124        return None;
125    };
126    let slot = FD_HOST.get(idx)?;
127    let v = slot.load(Ordering::Acquire);
128    if v < 0 { None } else { Some(v) }
129}
130
131/// Removes a guest FD mapping and returns the host fd (lock-free).
132///
133/// For stdio 0–2: marks the slot closed and returns the host fd (identity) so
134/// the caller can `close` it — matching Darwin when `fetch-pack` drops stdout.
135#[must_use]
136#[inline]
137pub fn fd_take(gfd: i32) -> Option<RawFd> {
138    if gfd == 0 || gfd == 1 || gfd == 2 {
139        if stdio_is_closed(gfd) {
140            return None;
141        }
142        stdio_mark_closed(gfd);
143        return Some(gfd);
144    }
145    if gfd < 0 {
146        return None;
147    }
148    let Ok(idx) = usize::try_from(gfd) else {
149        return None;
150    };
151    let slot = FD_HOST.get(idx)?;
152    let v = slot.swap(FD_EMPTY, Ordering::AcqRel);
153    if let Some(t) = FD_TLS.get(idx) {
154        t.store(0, Ordering::Release);
155    }
156    if let Some(nb) = FD_GUEST_NB.get(idx) {
157        nb.store(0, Ordering::Release);
158    }
159    fd_clear_guest_path(gfd);
160    if v < 0 { None } else { Some(v) }
161}
162
163/// Record the guest path used to open `gfd` (absolute Darwin path).
164pub fn fd_set_guest_path(gfd: i32, path: impl Into<String>) {
165    if gfd < 3 {
166        return;
167    }
168    if let Ok(mut g) = FD_GUEST_PATH.write() {
169        let map = g.get_or_insert_with(HashMap::new);
170        map.insert(gfd, path.into());
171    }
172}
173
174/// Guest path for `gfd`, if recorded at open.
175#[must_use]
176pub fn fd_guest_path(gfd: i32) -> Option<String> {
177    if gfd < 3 {
178        return None;
179    }
180    FD_GUEST_PATH
181        .read()
182        .ok()
183        .and_then(|g| g.as_ref().and_then(|m| m.get(&gfd).cloned()))
184}
185
186fn fd_clear_guest_path(gfd: i32) {
187    if gfd < 3 {
188        return;
189    }
190    if let Ok(mut g) = FD_GUEST_PATH.write()
191        && let Some(map) = g.as_mut()
192    {
193        map.remove(&gfd);
194    }
195}
196
197/// Bit for guest stdio FD in [`STDIO_CLOSED`] (`0→1`, `1→2`, `2→4`).
198#[inline]
199fn stdio_closed_bit(gfd: i32) -> Option<u8> {
200    match gfd {
201        0 => Some(1),
202        1 => Some(2),
203        2 => Some(4),
204        _ => None,
205    }
206}
207
208#[inline]
209fn stdio_is_closed(gfd: i32) -> bool {
210    let Some(bit) = stdio_closed_bit(gfd) else {
211        return false;
212    };
213    STDIO_CLOSED.load(Ordering::Acquire) & bit != 0
214}
215
216#[inline]
217fn stdio_mark_closed(gfd: i32) {
218    let Some(bit) = stdio_closed_bit(gfd) else {
219        return;
220    };
221    STDIO_CLOSED.fetch_or(bit, Ordering::AcqRel);
222}
223
224/// Clear the closed bit for a stdio slot (after successful `dup2` onto 0/1/2).
225#[inline]
226pub fn stdio_mark_open(gfd: i32) {
227    let Some(bit) = stdio_closed_bit(gfd) else {
228        return;
229    };
230    STDIO_CLOSED.fetch_and(!bit, Ordering::AcqRel);
231}
232
233/// Installs `host_fd` into a **specific** guest FD slot (for `dup2`).
234///
235/// Closes any previous host mapping at `gfd` (except stdio 0–2 identity).
236/// Guest nonblock flag is **not** updated — caller should copy from the source.
237/// Returns `false` if `gfd` is out of range.
238#[must_use]
239#[inline]
240pub fn fd_install(gfd: i32, host_fd: RawFd) -> bool {
241    if host_fd < 0 || gfd < 3 {
242        return false;
243    }
244    let Ok(idx) = usize::try_from(gfd) else {
245        return false;
246    };
247    let Some(slot) = FD_HOST.get(idx) else {
248        return false;
249    };
250    let prev = slot.swap(host_fd, Ordering::AcqRel);
251    if prev >= 0 && prev != host_fd {
252        host::close_fd(prev);
253    }
254    true
255}
256
257/// Allocates a guest FD slot for `host_fd` (lock-free CAS scan).
258///
259/// New slots start with guest-blocking semantics (`O_NONBLOCK` clear). Call
260/// [`fd_set_guest_nonblock`] for sockets / after `fcntl(F_SETFL)`.
261#[must_use]
262#[inline]
263pub fn fd_alloc(host_fd: RawFd) -> Option<i32> {
264    if host_fd < 0 {
265        return None;
266    }
267    let start = usize::try_from(FD_NEXT.load(Ordering::Relaxed).max(3)).unwrap_or(3);
268    for idx in start..FD_SLOTS {
269        if try_claim_slot(idx, host_fd) {
270            let gfd = i32::try_from(idx).ok()?;
271            fd_set_guest_nonblock(gfd, false);
272            FD_NEXT.store(gfd.saturating_add(1), Ordering::Relaxed);
273            return Some(gfd);
274        }
275    }
276    for idx in 3..start.min(FD_SLOTS) {
277        if try_claim_slot(idx, host_fd) {
278            let gfd = i32::try_from(idx).ok()?;
279            fd_set_guest_nonblock(gfd, false);
280            FD_NEXT.store(gfd.saturating_add(1), Ordering::Relaxed);
281            return Some(gfd);
282        }
283    }
284    None
285}
286
287/// Guest-visible `O_NONBLOCK` for `gfd` (stdio or allocated slot).
288#[must_use]
289#[inline]
290pub fn fd_guest_nonblock(gfd: i32) -> bool {
291    if gfd < 0 {
292        return false;
293    }
294    let Ok(idx) = usize::try_from(gfd) else {
295        return false;
296    };
297    FD_GUEST_NB
298        .get(idx)
299        .is_some_and(|s| s.load(Ordering::Acquire) != 0)
300}
301
302/// Sets guest-visible `O_NONBLOCK` for `gfd`.
303#[inline]
304pub fn fd_set_guest_nonblock(gfd: i32, nonblock: bool) {
305    if gfd < 0 {
306        return;
307    }
308    let Ok(idx) = usize::try_from(gfd) else {
309        return;
310    };
311    if let Some(slot) = FD_GUEST_NB.get(idx) {
312        slot.store(u8::from(nonblock), Ordering::Release);
313    }
314}
315
316/// True when `gfd` is a TLS-wrapped guest FD (hot path; no mutex).
317#[must_use]
318#[inline]
319pub fn fd_is_tls(gfd: i32) -> bool {
320    if gfd < 0 {
321        return false;
322    }
323    let Ok(idx) = usize::try_from(gfd) else {
324        return false;
325    };
326    FD_TLS
327        .get(idx)
328        .is_some_and(|s| s.load(Ordering::Acquire) != 0)
329}
330
331/// Mark / clear TLS session association for `gfd` (paired with [`crate::tls_fd`]).
332#[inline]
333pub fn fd_set_tls(gfd: i32, is_tls: bool) {
334    if gfd < 0 {
335        return;
336    }
337    let Ok(idx) = usize::try_from(gfd) else {
338        return;
339    };
340    if let Some(slot) = FD_TLS.get(idx) {
341        slot.store(u8::from(is_tls), Ordering::Release);
342    }
343}
344
345/// Clear host `O_NONBLOCK` on stdio when the fd is a pipe/FIFO.
346///
347/// Nested `kh run` (git helpers after `fork`+re-exec) inherits command pipes as
348/// host 0/1/2. Those pipes are created non-blocking for curl multi; helpers
349/// expect Darwin blocking stdin/stdout. Call once per guest run start.
350pub fn normalize_stdio_pipes_blocking() {
351    for fd in 0_i32..=2 {
352        if host::fd_is_fifo(fd) {
353            host::clear_nonblock(fd);
354            fd_set_guest_nonblock(fd, false);
355        }
356    }
357}
358
359/// Bottle root published for path translation (set once per run; rare updates).
360static BOTTLE_ROOT: RwLock<Option<Arc<PathBuf>>> = RwLock::new(None);
361
362/// Guest absolute path of the main executable (`_NSGetExecutablePath`).
363///
364/// Set at `kh run` / nested re-exec start. Soft-git fallback lived in freestanding
365/// libSystem; clang spawn needs the real binary (not a hard-coded git path).
366static GUEST_EXECUTABLE: RwLock<Option<Arc<str>>> = RwLock::new(None);
367
368/// Open `O_DIRECTORY` fd for the bottle root (`-1` = none).
369///
370/// Hot path: absolute guest paths use `openat`/`fstatat` against this fd
371/// instead of building `{root}/{rel}` PathBuf + `open` from `/` each call (B1).
372static BOTTLE_DIRFD: AtomicI32 = AtomicI32::new(-1);
373
374/// `bsdthread_register` snapshot (narrow lock; not the full process state).
375static BSDTHREAD_REG: RwLock<Option<BsdThreadReg>> = RwLock::new(None);
376
377fn try_claim_slot(idx: usize, host_fd: RawFd) -> bool {
378    let Some(slot) = FD_HOST.get(idx) else {
379        return false;
380    };
381    slot.compare_exchange(FD_EMPTY, host_fd, Ordering::AcqRel, Ordering::Relaxed)
382        .is_ok()
383}
384
385fn reset_fd_map() {
386    for (i, slot) in FD_HOST.iter().enumerate() {
387        let prev = slot.swap(FD_EMPTY, Ordering::AcqRel);
388        if i > 2 && prev >= 0 {
389            host::close_fd(prev);
390        }
391    }
392    for slot in &FD_GUEST_NB {
393        slot.store(0, Ordering::Relaxed);
394    }
395    for slot in &FD_TLS {
396        slot.store(0, Ordering::Relaxed);
397    }
398    if let Ok(mut g) = FD_GUEST_PATH.write() {
399        *g = None;
400    }
401    STDIO_CLOSED.store(0, Ordering::Relaxed);
402    FD_NEXT.store(3, Ordering::Relaxed);
403}
404
405/// Owned process state for one guest (or unit-test isolation via reset).
406///
407/// Bottle root and `bsdthread_register` live in separate process-wide slots so
408/// open/create hot paths do not take this lock.
409#[derive(Debug)]
410pub struct ProcessState {
411    fds: FdTable,
412    /// Guest FD → directory stream (for `readdir` host helper).
413    dir_streams: HashMap<i32, host::HostDir>,
414    sig_mask: u32,
415    sigactions: [SoftSigAct; 32],
416}
417
418impl ProcessState {
419    /// Fresh process state with default syscall limit.
420    #[must_use]
421    pub fn new() -> Self {
422        Self {
423            fds: FdTable::new(),
424            dir_streams: HashMap::new(),
425            sig_mask: 0,
426            sigactions: [SoftSigAct::zero(); 32],
427        }
428    }
429
430    /// Resets FD table, soft signals, thread reg, and counters. Preserves bottle root.
431    pub fn reset_run(&mut self, max_syscalls: usize) {
432        self.dir_streams.clear();
433        self.fds.reset();
434        self.sig_mask = 0;
435        self.sigactions = [SoftSigAct::zero(); 32];
436        clear_bsdthread_reg();
437        let max = u64::try_from(max_syscalls).unwrap_or(256);
438        MAX_SYSCALLS.store(max, Ordering::Relaxed);
439        SYSCALL_COUNT.store(0, Ordering::Relaxed);
440        LIVE_CHILDREN.store(0, Ordering::Relaxed);
441        crate::thread::reset_thread_runtime();
442    }
443
444    /// Closes any directory stream associated with `gfd` (call before FD free).
445    pub(crate) fn close_dir_stream(&mut self, gfd: i32) {
446        drop(self.dir_streams.remove(&gfd));
447    }
448
449    /// Returns the next directory entry for `gfd`, opening a stream on first use.
450    ///
451    /// `None` means end-of-directory (or empty). `Err(errno)` on failure.
452    pub(crate) fn readdir_next(&mut self, gfd: i32) -> Result<Option<(Vec<u8>, u8)>, i64> {
453        let Some(host_fd) = fd_get(gfd) else {
454            return Err(9); // EBADF
455        };
456
457        if let std::collections::hash_map::Entry::Vacant(e) = self.dir_streams.entry(gfd) {
458            match host::HostDir::open_dup(host_fd) {
459                Ok(dir) => {
460                    e.insert(dir);
461                }
462                Err(err) => return Err(i64::from(err)),
463            }
464        }
465
466        let Some(stream) = self.dir_streams.get_mut(&gfd) else {
467            return Err(9);
468        };
469        Ok(stream.read_next())
470    }
471
472    #[must_use]
473    pub(crate) fn sig_mask(&self) -> u32 {
474        self.sig_mask
475    }
476
477    pub(crate) fn set_sig_mask(&mut self, mask: u32) {
478        self.sig_mask = mask;
479    }
480
481    #[must_use]
482    pub(crate) fn sigaction(&self, sig: usize) -> Option<SoftSigAct> {
483        self.sigactions.get(sig).copied()
484    }
485
486    pub(crate) fn set_sigaction(&mut self, sig: usize, act: SoftSigAct) -> bool {
487        if let Some(slot) = self.sigactions.get_mut(sig) {
488            *slot = act;
489            true
490        } else {
491            false
492        }
493    }
494}
495
496impl Default for ProcessState {
497    fn default() -> Self {
498        Self::new()
499    }
500}
501
502/// Parent observed a successful `fork` (child pid > 0).
503#[inline]
504pub fn child_born() {
505    LIVE_CHILDREN.fetch_add(1, Ordering::Relaxed);
506}
507
508/// Parent reaped at least one child via `waitpid`/`wait4`.
509#[inline]
510pub fn child_reaped() {
511    let _ = LIVE_CHILDREN.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |n| {
512        Some(n.saturating_sub(1))
513    });
514}
515
516/// Number of unreaped host children (approximate; for pipe blocking heuristics).
517#[must_use]
518#[inline]
519pub fn live_children() -> u64 {
520    LIVE_CHILDREN.load(Ordering::Relaxed)
521}
522
523/// Bumps the process-wide syscall counter without taking the process lock.
524///
525/// Returns `true` when the configured max has been exceeded.
526#[must_use]
527pub fn tick_syscall() -> bool {
528    let n = SYSCALL_COUNT.fetch_add(1, Ordering::Relaxed);
529    n >= MAX_SYSCALLS.load(Ordering::Relaxed)
530}
531
532fn active_lock() -> &'static RwLock<ProcessState> {
533    static ACTIVE: OnceLock<RwLock<ProcessState>> = OnceLock::new();
534    ACTIVE.get_or_init(|| RwLock::new(ProcessState::new()))
535}
536
537/// Exclusive access to the active process state.
538pub fn with_mut<R>(f: impl FnOnce(&mut ProcessState) -> R) -> R {
539    match active_lock().write() {
540        Ok(mut guard) => f(&mut guard),
541        Err(poisoned) => f(&mut poisoned.into_inner()),
542    }
543}
544
545/// Shared access to the active process state (concurrent FD lookups, etc.).
546pub fn with_ref<R>(f: impl FnOnce(&ProcessState) -> R) -> R {
547    match active_lock().read() {
548        Ok(guard) => f(&guard),
549        Err(poisoned) => f(&poisoned.into_inner()),
550    }
551}
552
553/// Resets FD/signals/counters for a new guest run (keeps bottle root).
554pub fn reset_run(max_syscalls: usize) {
555    with_mut(|p| p.reset_run(max_syscalls));
556    // Fresh guest process: drop prior dlopen image table.
557    crate::dyld_table::clear();
558    // After re-exec of a Mach-O helper, host 0/1/2 may be O_NONBLOCK pipes
559    // created for curl multi in the parent — restore Darwin blocking stdio.
560    normalize_stdio_pipes_blocking();
561}
562
563/// Configures the bottle root used by path-taking syscalls.
564///
565/// Also opens/replaces the process-wide bottle directory fd for B1 `openat`.
566pub fn set_bottle_root(root: Option<PathBuf>) {
567    let new_fd = root
568        .as_ref()
569        .map_or(-1, |p| open_bottle_dirfd(p).unwrap_or(-1));
570    let old_fd = BOTTLE_DIRFD.swap(new_fd, Ordering::AcqRel);
571    if old_fd >= 0 {
572        host::close_fd(old_fd);
573    }
574
575    let next = root.map(Arc::new);
576    match BOTTLE_ROOT.write() {
577        Ok(mut guard) => {
578            *guard = next;
579        }
580        Err(poisoned) => {
581            *poisoned.into_inner() = next;
582        }
583    }
584}
585
586/// Clone of the configured bottle root, if any.
587#[must_use]
588pub fn bottle_root() -> Option<PathBuf> {
589    with_bottle_root(|p| p.map(Path::to_path_buf))
590}
591
592/// Borrow the bottle root without cloning (hot path for path translation).
593///
594/// Uses a **narrow** bottle-root lock (not the full process state).
595pub fn with_bottle_root<R>(f: impl FnOnce(Option<&Path>) -> R) -> R {
596    match BOTTLE_ROOT.read() {
597        Ok(guard) => f(guard.as_ref().map(|a| a.as_path())),
598        Err(poisoned) => f(poisoned.into_inner().as_ref().map(|a| a.as_path())),
599    }
600}
601
602/// Records the guest-visible main executable path for `_NSGetExecutablePath`.
603pub fn set_guest_executable_path(path: Option<String>) {
604    let next = path.filter(|s| !s.is_empty() && !s.contains('\0')).map(Arc::from);
605    match GUEST_EXECUTABLE.write() {
606        Ok(mut guard) => {
607            *guard = next;
608        }
609        Err(poisoned) => {
610            *poisoned.into_inner() = next;
611        }
612    }
613}
614
615/// Guest absolute executable path, if set for this run.
616#[must_use]
617pub fn guest_executable_path() -> Option<String> {
618    match GUEST_EXECUTABLE.read() {
619        Ok(guard) => guard.as_ref().map(|s| s.as_ref().to_owned()),
620        Err(poisoned) => poisoned
621            .into_inner()
622            .as_ref()
623            .map(|s| s.as_ref().to_owned()),
624    }
625}
626
627/// Process-wide bottle root directory fd for `openat`/`fstatat` (B1).
628///
629/// `None` when no bottle is configured or the directory could not be opened.
630#[must_use]
631pub fn bottle_dirfd() -> Option<RawFd> {
632    let fd = BOTTLE_DIRFD.load(Ordering::Acquire);
633    if fd >= 0 { Some(fd) } else { None }
634}
635
636/// Open `root` as `O_RDONLY|O_DIRECTORY|O_CLOEXEC` for hot-path `openat`.
637fn open_bottle_dirfd(root: &Path) -> Option<RawFd> {
638    let c = std::ffi::CString::new(root.as_os_str().as_encoded_bytes()).ok()?;
639    host::open_directory(&c)
640}
641
642/// Stores `bsdthread_register` metadata for subsequent `bsdthread_create`.
643pub(crate) fn set_bsdthread_reg(reg: BsdThreadReg) {
644    match BSDTHREAD_REG.write() {
645        Ok(mut guard) => {
646            *guard = Some(reg);
647        }
648        Err(poisoned) => {
649            *poisoned.into_inner() = Some(reg);
650        }
651    }
652}
653
654/// Snapshot of the last `bsdthread_register` (narrow lock, copy-out).
655#[must_use]
656pub(crate) fn bsdthread_reg() -> Option<BsdThreadReg> {
657    match BSDTHREAD_REG.read() {
658        Ok(guard) => *guard,
659        Err(poisoned) => *poisoned.into_inner(),
660    }
661}
662
663fn clear_bsdthread_reg() {
664    match BSDTHREAD_REG.write() {
665        Ok(mut guard) => {
666            *guard = None;
667        }
668        Err(poisoned) => {
669            *poisoned.into_inner() = None;
670        }
671    }
672}
673
674/// Serializes tests that mutate process-wide state (address space + process).
675#[cfg(test)]
676pub(crate) fn test_lock() -> std::sync::MutexGuard<'static, ()> {
677    use std::sync::Mutex;
678    static LOCK: Mutex<()> = Mutex::new(());
679    LOCK.lock()
680        .unwrap_or_else(std::sync::PoisonError::into_inner)
681}