1use 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
15static SYSCALL_COUNT: AtomicU64 = AtomicU64::new(0);
17static MAX_SYSCALLS: AtomicU64 = AtomicU64::new(256);
19
20static LIVE_CHILDREN: AtomicU64 = AtomicU64::new(0);
25
26const FD_SLOTS: usize = 1024;
28const FD_EMPTY: i32 = -1;
30
31static FD_HOST: [AtomicI32; FD_SLOTS] = [const { AtomicI32::new(FD_EMPTY) }; FD_SLOTS];
36static FD_GUEST_NB: [AtomicU8; FD_SLOTS] = [const { AtomicU8::new(0) }; FD_SLOTS];
42static FD_TLS: [AtomicU8; FD_SLOTS] = [const { AtomicU8::new(0) }; FD_SLOTS];
46static FD_NEXT: AtomicI32 = AtomicI32::new(3);
48static FD_GUEST_PATH: RwLock<Option<HashMap<i32, String>>> = RwLock::new(None);
51static STDIO_CLOSED: AtomicU8 = AtomicU8::new(0);
56
57#[derive(Clone, Copy, Debug)]
59pub(crate) struct SoftSigAct {
60 pub handler: u64,
61 pub mask: u32,
62 pub flags: i32,
63}
64
65#[derive(Clone, Copy, Debug, Default)]
69#[allow(dead_code)]
70pub(crate) struct BsdThreadReg {
71 pub threadstart: u64,
73 pub wqthread: u64,
75 pub flags: u32,
77 pub stack_addr_hint: u64,
79 pub tsd_offset: u32,
81 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#[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#[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#[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
163pub 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#[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#[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#[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#[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#[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#[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#[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#[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#[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
345pub 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
359static BOTTLE_ROOT: RwLock<Option<Arc<PathBuf>>> = RwLock::new(None);
361
362static GUEST_EXECUTABLE: RwLock<Option<Arc<str>>> = RwLock::new(None);
367
368static BOTTLE_DIRFD: AtomicI32 = AtomicI32::new(-1);
373
374static 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#[derive(Debug)]
410pub struct ProcessState {
411 fds: FdTable,
412 dir_streams: HashMap<i32, host::HostDir>,
414 sig_mask: u32,
415 sigactions: [SoftSigAct; 32],
416}
417
418impl ProcessState {
419 #[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 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 pub(crate) fn close_dir_stream(&mut self, gfd: i32) {
446 drop(self.dir_streams.remove(&gfd));
447 }
448
449 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); };
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#[inline]
504pub fn child_born() {
505 LIVE_CHILDREN.fetch_add(1, Ordering::Relaxed);
506}
507
508#[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#[must_use]
518#[inline]
519pub fn live_children() -> u64 {
520 LIVE_CHILDREN.load(Ordering::Relaxed)
521}
522
523#[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
537pub 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
545pub 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
553pub fn reset_run(max_syscalls: usize) {
555 with_mut(|p| p.reset_run(max_syscalls));
556 crate::dyld_table::clear();
558 normalize_stdio_pipes_blocking();
561}
562
563pub 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#[must_use]
588pub fn bottle_root() -> Option<PathBuf> {
589 with_bottle_root(|p| p.map(Path::to_path_buf))
590}
591
592pub 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
602pub 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#[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#[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
636fn 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
642pub(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#[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#[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}