1use std::collections::HashMap;
11use std::os::fd::RawFd;
12use std::path::{Path, PathBuf};
13use std::sync::atomic::{AtomicI32, AtomicU8, AtomicU64, Ordering};
14use std::sync::{Arc, OnceLock, RwLock};
15
16use crate::host;
17
18static SYSCALL_COUNT: AtomicU64 = AtomicU64::new(0);
20static MAX_SYSCALLS: AtomicU64 = AtomicU64::new(256);
22
23static LIVE_CHILDREN: AtomicU64 = AtomicU64::new(0);
28
29const FD_SLOTS: usize = 1024;
31const FD_EMPTY: i32 = -1;
33
34static FD_HOST: [AtomicI32; FD_SLOTS] = [const { AtomicI32::new(FD_EMPTY) }; FD_SLOTS];
39static FD_GUEST_NB: [AtomicU8; FD_SLOTS] = [const { AtomicU8::new(0) }; FD_SLOTS];
45static FD_TLS: [AtomicU8; FD_SLOTS] = [const { AtomicU8::new(0) }; FD_SLOTS];
49static FD_NEXT: AtomicI32 = AtomicI32::new(3);
51static FD_GUEST_PATH: RwLock<Option<HashMap<i32, String>>> = RwLock::new(None);
54static STDIO_CLOSED: AtomicU8 = AtomicU8::new(0);
62
63#[derive(Clone, Copy, Debug)]
65pub(crate) struct SoftSigAct {
66 pub handler: u64,
67 pub mask: u32,
68 pub flags: i32,
69}
70
71#[derive(Clone, Copy, Debug, Default)]
75#[allow(dead_code)]
76pub(crate) struct BsdThreadReg {
77 pub threadstart: u64,
79 pub wqthread: u64,
81 pub flags: u32,
83 pub stack_addr_hint: u64,
85 pub tsd_offset: u32,
87 pub dispatchqueue_offset: u32,
89}
90
91impl SoftSigAct {
92 pub(crate) const fn zero() -> Self {
93 Self {
94 handler: 0,
95 mask: 0,
96 flags: 0,
97 }
98 }
99}
100
101#[derive(Debug, Default)]
103struct FdTable;
104
105impl FdTable {
106 const fn new() -> Self {
107 Self
108 }
109
110 fn reset(&mut self) {
111 let _ = self;
112 reset_fd_map();
113 }
114}
115
116#[must_use]
118#[inline]
119pub fn fd_get(gfd: i32) -> Option<RawFd> {
120 if gfd == 0 || gfd == 1 || gfd == 2 {
121 if stdio_is_closed(gfd) {
122 return None;
123 }
124 return Some(gfd);
125 }
126 if gfd < 0 {
127 return None;
128 }
129 let Ok(idx) = usize::try_from(gfd) else {
130 return None;
131 };
132 let slot = FD_HOST.get(idx)?;
133 let v = slot.load(Ordering::Acquire);
134 if v < 0 { None } else { Some(v) }
135}
136
137#[must_use]
142#[inline]
143pub fn fd_take(gfd: i32) -> Option<RawFd> {
144 if gfd == 0 || gfd == 1 || gfd == 2 {
145 if stdio_is_closed(gfd) {
146 return None;
147 }
148 stdio_mark_closed(gfd);
149 return Some(gfd);
150 }
151 if gfd < 0 {
152 return None;
153 }
154 let Ok(idx) = usize::try_from(gfd) else {
155 return None;
156 };
157 let slot = FD_HOST.get(idx)?;
158 let v = slot.swap(FD_EMPTY, Ordering::AcqRel);
159 if let Some(t) = FD_TLS.get(idx) {
160 t.store(0, Ordering::Release);
161 }
162 if let Some(nb) = FD_GUEST_NB.get(idx) {
163 nb.store(0, Ordering::Release);
164 }
165 fd_clear_guest_path(gfd);
166 if v < 0 { None } else { Some(v) }
167}
168
169pub fn fd_set_guest_path(gfd: i32, path: impl Into<String>) {
171 if gfd < 3 {
172 return;
173 }
174 if let Ok(mut g) = FD_GUEST_PATH.write() {
175 let map = g.get_or_insert_with(HashMap::new);
176 map.insert(gfd, path.into());
177 }
178}
179
180#[must_use]
182pub fn fd_guest_path(gfd: i32) -> Option<String> {
183 if gfd < 3 {
184 return None;
185 }
186 FD_GUEST_PATH
187 .read()
188 .ok()
189 .and_then(|g| g.as_ref().and_then(|m| m.get(&gfd).cloned()))
190}
191
192fn fd_clear_guest_path(gfd: i32) {
193 if gfd < 3 {
194 return;
195 }
196 if let Ok(mut g) = FD_GUEST_PATH.write()
197 && let Some(map) = g.as_mut()
198 {
199 map.remove(&gfd);
200 }
201}
202
203#[inline]
205fn stdio_closed_bit(gfd: i32) -> Option<u8> {
206 match gfd {
207 0 => Some(1),
208 1 => Some(2),
209 2 => Some(4),
210 _ => None,
211 }
212}
213
214#[inline]
215fn stdio_is_closed(gfd: i32) -> bool {
216 let Some(bit) = stdio_closed_bit(gfd) else {
217 return false;
218 };
219 STDIO_CLOSED.load(Ordering::Acquire) & bit != 0
220}
221
222#[inline]
223fn stdio_mark_closed(gfd: i32) {
224 let Some(bit) = stdio_closed_bit(gfd) else {
225 return;
226 };
227 STDIO_CLOSED.fetch_or(bit, Ordering::AcqRel);
228}
229
230#[inline]
232pub fn stdio_mark_open(gfd: i32) {
233 let Some(bit) = stdio_closed_bit(gfd) else {
234 return;
235 };
236 STDIO_CLOSED.fetch_and(!bit, Ordering::AcqRel);
237}
238
239#[must_use]
245#[inline]
246pub fn fd_install(gfd: i32, host_fd: RawFd) -> bool {
247 if host_fd < 0 || gfd < 3 {
248 return false;
249 }
250 let Ok(idx) = usize::try_from(gfd) else {
251 return false;
252 };
253 let Some(slot) = FD_HOST.get(idx) else {
254 return false;
255 };
256 let prev = slot.swap(host_fd, Ordering::AcqRel);
257 if prev >= 0 && prev != host_fd {
258 host::close_fd(prev);
259 }
260 true
261}
262
263#[must_use]
268#[inline]
269pub fn fd_alloc(host_fd: RawFd) -> Option<i32> {
270 if host_fd < 0 {
271 return None;
272 }
273 let start = usize::try_from(FD_NEXT.load(Ordering::Relaxed).max(3)).unwrap_or(3);
274 for idx in start..FD_SLOTS {
275 if try_claim_slot(idx, host_fd) {
276 let gfd = i32::try_from(idx).ok()?;
277 fd_set_guest_nonblock(gfd, false);
278 FD_NEXT.store(gfd.saturating_add(1), Ordering::Relaxed);
279 return Some(gfd);
280 }
281 }
282 for idx in 3..start.min(FD_SLOTS) {
283 if try_claim_slot(idx, host_fd) {
284 let gfd = i32::try_from(idx).ok()?;
285 fd_set_guest_nonblock(gfd, false);
286 FD_NEXT.store(gfd.saturating_add(1), Ordering::Relaxed);
287 return Some(gfd);
288 }
289 }
290 None
291}
292
293#[must_use]
295#[inline]
296pub fn fd_guest_nonblock(gfd: i32) -> bool {
297 if gfd < 0 {
298 return false;
299 }
300 let Ok(idx) = usize::try_from(gfd) else {
301 return false;
302 };
303 FD_GUEST_NB
304 .get(idx)
305 .is_some_and(|s| s.load(Ordering::Acquire) != 0)
306}
307
308#[inline]
310pub fn fd_set_guest_nonblock(gfd: i32, nonblock: bool) {
311 if gfd < 0 {
312 return;
313 }
314 let Ok(idx) = usize::try_from(gfd) else {
315 return;
316 };
317 if let Some(slot) = FD_GUEST_NB.get(idx) {
318 slot.store(u8::from(nonblock), Ordering::Release);
319 }
320}
321
322#[must_use]
324#[inline]
325pub fn fd_is_tls(gfd: i32) -> bool {
326 if gfd < 0 {
327 return false;
328 }
329 let Ok(idx) = usize::try_from(gfd) else {
330 return false;
331 };
332 FD_TLS
333 .get(idx)
334 .is_some_and(|s| s.load(Ordering::Acquire) != 0)
335}
336
337#[inline]
339pub fn fd_set_tls(gfd: i32, is_tls: bool) {
340 if gfd < 0 {
341 return;
342 }
343 let Ok(idx) = usize::try_from(gfd) else {
344 return;
345 };
346 if let Some(slot) = FD_TLS.get(idx) {
347 slot.store(u8::from(is_tls), Ordering::Release);
348 }
349}
350
351pub fn normalize_stdio_pipes_blocking() {
357 for fd in 0_i32..=2 {
358 if host::fd_is_fifo(fd) {
359 host::clear_nonblock(fd);
360 fd_set_guest_nonblock(fd, false);
361 }
362 }
363}
364
365static BOTTLE_ROOT: RwLock<Option<Arc<PathBuf>>> = RwLock::new(None);
367
368static GUEST_EXECUTABLE: RwLock<Option<Arc<str>>> = RwLock::new(None);
373
374static BOTTLE_DIRFD: AtomicI32 = AtomicI32::new(-1);
379
380static BSDTHREAD_REG: RwLock<Option<BsdThreadReg>> = RwLock::new(None);
382
383fn try_claim_slot(idx: usize, host_fd: RawFd) -> bool {
384 let Some(slot) = FD_HOST.get(idx) else {
385 return false;
386 };
387 slot.compare_exchange(FD_EMPTY, host_fd, Ordering::AcqRel, Ordering::Relaxed)
388 .is_ok()
389}
390
391fn reset_fd_map() {
392 for (i, slot) in FD_HOST.iter().enumerate() {
393 let prev = slot.swap(FD_EMPTY, Ordering::AcqRel);
394 if i > 2 && prev >= 0 {
395 host::close_fd(prev);
396 }
397 }
398 for slot in &FD_GUEST_NB {
399 slot.store(0, Ordering::Relaxed);
400 }
401 for slot in &FD_TLS {
402 slot.store(0, Ordering::Relaxed);
403 }
404 if let Ok(mut g) = FD_GUEST_PATH.write() {
405 *g = None;
406 }
407 STDIO_CLOSED.store(0, Ordering::Relaxed);
408 FD_NEXT.store(3, Ordering::Relaxed);
409}
410
411#[derive(Debug)]
416pub struct ProcessState {
417 fds: FdTable,
418 dir_streams: HashMap<i32, host::HostDir>,
420 sig_mask: u32,
421 sigactions: [SoftSigAct; 32],
422}
423
424impl ProcessState {
425 #[must_use]
427 pub fn new() -> Self {
428 Self {
429 fds: FdTable::new(),
430 dir_streams: HashMap::new(),
431 sig_mask: 0,
432 sigactions: [SoftSigAct::zero(); 32],
433 }
434 }
435
436 pub fn reset_run(&mut self, max_syscalls: usize) {
438 self.dir_streams.clear();
439 self.fds.reset();
440 self.sig_mask = 0;
441 self.sigactions = [SoftSigAct::zero(); 32];
442 clear_bsdthread_reg();
443 let max = u64::try_from(max_syscalls).unwrap_or(256);
444 MAX_SYSCALLS.store(max, Ordering::Relaxed);
445 SYSCALL_COUNT.store(0, Ordering::Relaxed);
446 LIVE_CHILDREN.store(0, Ordering::Relaxed);
447 crate::thread::reset_thread_runtime();
448 }
449
450 pub(crate) fn close_dir_stream(&mut self, gfd: i32) {
452 drop(self.dir_streams.remove(&gfd));
453 }
454
455 pub(crate) fn readdir_next(&mut self, gfd: i32) -> Result<Option<(Vec<u8>, u8)>, i64> {
459 let Some(host_fd) = fd_get(gfd) else {
460 return Err(9); };
462
463 if let std::collections::hash_map::Entry::Vacant(e) = self.dir_streams.entry(gfd) {
464 match host::HostDir::open_dup(host_fd) {
465 Ok(dir) => {
466 e.insert(dir);
467 }
468 Err(err) => return Err(i64::from(err)),
469 }
470 }
471
472 let Some(stream) = self.dir_streams.get_mut(&gfd) else {
473 return Err(9);
474 };
475 Ok(stream.read_next())
476 }
477
478 #[must_use]
479 pub(crate) fn sig_mask(&self) -> u32 {
480 self.sig_mask
481 }
482
483 pub(crate) fn set_sig_mask(&mut self, mask: u32) {
484 self.sig_mask = mask;
485 }
486
487 #[must_use]
488 pub(crate) fn sigaction(&self, sig: usize) -> Option<SoftSigAct> {
489 self.sigactions.get(sig).copied()
490 }
491
492 pub(crate) fn set_sigaction(&mut self, sig: usize, act: SoftSigAct) -> bool {
493 if let Some(slot) = self.sigactions.get_mut(sig) {
494 *slot = act;
495 true
496 } else {
497 false
498 }
499 }
500}
501
502impl Default for ProcessState {
503 fn default() -> Self {
504 Self::new()
505 }
506}
507
508#[inline]
510pub fn child_born() {
511 LIVE_CHILDREN.fetch_add(1, Ordering::Relaxed);
512}
513
514#[inline]
516pub fn child_reaped() {
517 let _ = LIVE_CHILDREN.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |n| {
518 Some(n.saturating_sub(1))
519 });
520}
521
522#[must_use]
524#[inline]
525pub fn live_children() -> u64 {
526 LIVE_CHILDREN.load(Ordering::Relaxed)
527}
528
529#[must_use]
533pub fn tick_syscall() -> bool {
534 let n = SYSCALL_COUNT.fetch_add(1, Ordering::Relaxed);
535 n >= MAX_SYSCALLS.load(Ordering::Relaxed)
536}
537
538fn active_lock() -> &'static RwLock<ProcessState> {
539 static ACTIVE: OnceLock<RwLock<ProcessState>> = OnceLock::new();
540 ACTIVE.get_or_init(|| RwLock::new(ProcessState::new()))
541}
542
543pub fn with_mut<R>(f: impl FnOnce(&mut ProcessState) -> R) -> R {
545 match active_lock().write() {
546 Ok(mut guard) => f(&mut guard),
547 Err(poisoned) => f(&mut poisoned.into_inner()),
548 }
549}
550
551pub fn with_ref<R>(f: impl FnOnce(&ProcessState) -> R) -> R {
553 match active_lock().read() {
554 Ok(guard) => f(&guard),
555 Err(poisoned) => f(&poisoned.into_inner()),
556 }
557}
558
559pub fn reset_run(max_syscalls: usize) {
561 with_mut(|p| p.reset_run(max_syscalls));
562 crate::dyld_table::clear();
564 normalize_stdio_pipes_blocking();
567}
568
569pub fn set_bottle_root(root: Option<PathBuf>) {
573 let new_fd = root
574 .as_ref()
575 .map_or(-1, |p| open_bottle_dirfd(p).unwrap_or(-1));
576 let old_fd = BOTTLE_DIRFD.swap(new_fd, Ordering::AcqRel);
577 if old_fd >= 0 {
578 host::close_fd(old_fd);
579 }
580
581 let next = root.map(Arc::new);
582 match BOTTLE_ROOT.write() {
583 Ok(mut guard) => {
584 *guard = next;
585 }
586 Err(poisoned) => {
587 *poisoned.into_inner() = next;
588 }
589 }
590}
591
592#[must_use]
594pub fn bottle_root() -> Option<PathBuf> {
595 with_bottle_root(|p| p.map(Path::to_path_buf))
596}
597
598pub fn with_bottle_root<R>(f: impl FnOnce(Option<&Path>) -> R) -> R {
602 match BOTTLE_ROOT.read() {
603 Ok(guard) => f(guard.as_ref().map(|a| a.as_path())),
604 Err(poisoned) => f(poisoned.into_inner().as_ref().map(|a| a.as_path())),
605 }
606}
607
608pub fn set_guest_executable_path(path: Option<String>) {
610 let next = path.filter(|s| !s.is_empty() && !s.contains('\0')).map(Arc::from);
611 match GUEST_EXECUTABLE.write() {
612 Ok(mut guard) => {
613 *guard = next;
614 }
615 Err(poisoned) => {
616 *poisoned.into_inner() = next;
617 }
618 }
619}
620
621#[must_use]
623pub fn guest_executable_path() -> Option<String> {
624 match GUEST_EXECUTABLE.read() {
625 Ok(guard) => guard.as_ref().map(|s| s.as_ref().to_owned()),
626 Err(poisoned) => poisoned
627 .into_inner()
628 .as_ref()
629 .map(|s| s.as_ref().to_owned()),
630 }
631}
632
633#[must_use]
637pub fn bottle_dirfd() -> Option<RawFd> {
638 let fd = BOTTLE_DIRFD.load(Ordering::Acquire);
639 if fd >= 0 { Some(fd) } else { None }
640}
641
642fn open_bottle_dirfd(root: &Path) -> Option<RawFd> {
644 let c = std::ffi::CString::new(root.as_os_str().as_encoded_bytes()).ok()?;
645 host::open_directory(&c)
646}
647
648pub(crate) fn set_bsdthread_reg(reg: BsdThreadReg) {
650 match BSDTHREAD_REG.write() {
651 Ok(mut guard) => {
652 *guard = Some(reg);
653 }
654 Err(poisoned) => {
655 *poisoned.into_inner() = Some(reg);
656 }
657 }
658}
659
660#[must_use]
662pub(crate) fn bsdthread_reg() -> Option<BsdThreadReg> {
663 match BSDTHREAD_REG.read() {
664 Ok(guard) => *guard,
665 Err(poisoned) => *poisoned.into_inner(),
666 }
667}
668
669fn clear_bsdthread_reg() {
670 match BSDTHREAD_REG.write() {
671 Ok(mut guard) => {
672 *guard = None;
673 }
674 Err(poisoned) => {
675 *poisoned.into_inner() = None;
676 }
677 }
678}
679
680#[cfg(test)]
682pub(crate) fn test_lock() -> std::sync::MutexGuard<'static, ()> {
683 use std::sync::Mutex;
684 static LOCK: Mutex<()> = Mutex::new(());
685 LOCK.lock()
686 .unwrap_or_else(std::sync::PoisonError::into_inner)
687}