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 STDIO_CLOSED: AtomicU8 = AtomicU8::new(0);
59
60#[derive(Clone, Copy, Debug)]
62pub(crate) struct SoftSigAct {
63 pub handler: u64,
64 pub mask: u32,
65 pub flags: i32,
66}
67
68#[derive(Clone, Copy, Debug, Default)]
72#[allow(dead_code)]
73pub(crate) struct BsdThreadReg {
74 pub threadstart: u64,
76 pub wqthread: u64,
78 pub flags: u32,
80 pub stack_addr_hint: u64,
82 pub tsd_offset: u32,
84 pub dispatchqueue_offset: u32,
86}
87
88impl SoftSigAct {
89 pub(crate) const fn zero() -> Self {
90 Self {
91 handler: 0,
92 mask: 0,
93 flags: 0,
94 }
95 }
96}
97
98#[derive(Debug, Default)]
100struct FdTable;
101
102impl FdTable {
103 const fn new() -> Self {
104 Self
105 }
106
107 fn reset(&mut self) {
108 let _ = self;
109 reset_fd_map();
110 }
111}
112
113#[must_use]
115#[inline]
116pub fn fd_get(gfd: i32) -> Option<RawFd> {
117 if gfd == 0 || gfd == 1 || gfd == 2 {
118 if stdio_is_closed(gfd) {
119 return None;
120 }
121 return Some(gfd);
122 }
123 if gfd < 0 {
124 return None;
125 }
126 let Ok(idx) = usize::try_from(gfd) else {
127 return None;
128 };
129 let slot = FD_HOST.get(idx)?;
130 let v = slot.load(Ordering::Acquire);
131 if v < 0 { None } else { Some(v) }
132}
133
134#[must_use]
139#[inline]
140pub fn fd_take(gfd: i32) -> Option<RawFd> {
141 if gfd == 0 || gfd == 1 || gfd == 2 {
142 if stdio_is_closed(gfd) {
143 return None;
144 }
145 stdio_mark_closed(gfd);
146 return Some(gfd);
147 }
148 if gfd < 0 {
149 return None;
150 }
151 let Ok(idx) = usize::try_from(gfd) else {
152 return None;
153 };
154 let slot = FD_HOST.get(idx)?;
155 let v = slot.swap(FD_EMPTY, Ordering::AcqRel);
156 if let Some(t) = FD_TLS.get(idx) {
157 t.store(0, Ordering::Release);
158 }
159 if let Some(nb) = FD_GUEST_NB.get(idx) {
160 nb.store(0, Ordering::Release);
161 }
162 if v < 0 { None } else { Some(v) }
163}
164
165#[inline]
167fn stdio_closed_bit(gfd: i32) -> Option<u8> {
168 match gfd {
169 0 => Some(1),
170 1 => Some(2),
171 2 => Some(4),
172 _ => None,
173 }
174}
175
176#[inline]
177fn stdio_is_closed(gfd: i32) -> bool {
178 let Some(bit) = stdio_closed_bit(gfd) else {
179 return false;
180 };
181 STDIO_CLOSED.load(Ordering::Acquire) & bit != 0
182}
183
184#[inline]
185fn stdio_mark_closed(gfd: i32) {
186 let Some(bit) = stdio_closed_bit(gfd) else {
187 return;
188 };
189 STDIO_CLOSED.fetch_or(bit, Ordering::AcqRel);
190}
191
192#[inline]
194pub fn stdio_mark_open(gfd: i32) {
195 let Some(bit) = stdio_closed_bit(gfd) else {
196 return;
197 };
198 STDIO_CLOSED.fetch_and(!bit, Ordering::AcqRel);
199}
200
201#[must_use]
207#[inline]
208pub fn fd_install(gfd: i32, host_fd: RawFd) -> bool {
209 if host_fd < 0 || gfd < 3 {
210 return false;
211 }
212 let Ok(idx) = usize::try_from(gfd) else {
213 return false;
214 };
215 let Some(slot) = FD_HOST.get(idx) else {
216 return false;
217 };
218 let prev = slot.swap(host_fd, Ordering::AcqRel);
219 if prev >= 0 && prev != host_fd {
220 host::close_fd(prev);
221 }
222 true
223}
224
225#[must_use]
230#[inline]
231pub fn fd_alloc(host_fd: RawFd) -> Option<i32> {
232 if host_fd < 0 {
233 return None;
234 }
235 let start = usize::try_from(FD_NEXT.load(Ordering::Relaxed).max(3)).unwrap_or(3);
236 for idx in start..FD_SLOTS {
237 if try_claim_slot(idx, host_fd) {
238 let gfd = i32::try_from(idx).ok()?;
239 fd_set_guest_nonblock(gfd, false);
240 FD_NEXT.store(gfd.saturating_add(1), Ordering::Relaxed);
241 return Some(gfd);
242 }
243 }
244 for idx in 3..start.min(FD_SLOTS) {
245 if try_claim_slot(idx, host_fd) {
246 let gfd = i32::try_from(idx).ok()?;
247 fd_set_guest_nonblock(gfd, false);
248 FD_NEXT.store(gfd.saturating_add(1), Ordering::Relaxed);
249 return Some(gfd);
250 }
251 }
252 None
253}
254
255#[must_use]
257#[inline]
258pub fn fd_guest_nonblock(gfd: i32) -> bool {
259 if gfd < 0 {
260 return false;
261 }
262 let Ok(idx) = usize::try_from(gfd) else {
263 return false;
264 };
265 FD_GUEST_NB
266 .get(idx)
267 .is_some_and(|s| s.load(Ordering::Acquire) != 0)
268}
269
270#[inline]
272pub fn fd_set_guest_nonblock(gfd: i32, nonblock: bool) {
273 if gfd < 0 {
274 return;
275 }
276 let Ok(idx) = usize::try_from(gfd) else {
277 return;
278 };
279 if let Some(slot) = FD_GUEST_NB.get(idx) {
280 slot.store(u8::from(nonblock), Ordering::Release);
281 }
282}
283
284#[must_use]
286#[inline]
287pub fn fd_is_tls(gfd: i32) -> bool {
288 if gfd < 0 {
289 return false;
290 }
291 let Ok(idx) = usize::try_from(gfd) else {
292 return false;
293 };
294 FD_TLS
295 .get(idx)
296 .is_some_and(|s| s.load(Ordering::Acquire) != 0)
297}
298
299#[inline]
301pub fn fd_set_tls(gfd: i32, is_tls: bool) {
302 if gfd < 0 {
303 return;
304 }
305 let Ok(idx) = usize::try_from(gfd) else {
306 return;
307 };
308 if let Some(slot) = FD_TLS.get(idx) {
309 slot.store(u8::from(is_tls), Ordering::Release);
310 }
311}
312
313pub fn normalize_stdio_pipes_blocking() {
319 for fd in 0_i32..=2 {
320 if host::fd_is_fifo(fd) {
321 host::clear_nonblock(fd);
322 fd_set_guest_nonblock(fd, false);
323 }
324 }
325}
326
327static BOTTLE_ROOT: RwLock<Option<Arc<PathBuf>>> = RwLock::new(None);
329
330static BOTTLE_DIRFD: AtomicI32 = AtomicI32::new(-1);
335
336static BSDTHREAD_REG: RwLock<Option<BsdThreadReg>> = RwLock::new(None);
338
339fn try_claim_slot(idx: usize, host_fd: RawFd) -> bool {
340 let Some(slot) = FD_HOST.get(idx) else {
341 return false;
342 };
343 slot.compare_exchange(FD_EMPTY, host_fd, Ordering::AcqRel, Ordering::Relaxed)
344 .is_ok()
345}
346
347fn reset_fd_map() {
348 for (i, slot) in FD_HOST.iter().enumerate() {
349 let prev = slot.swap(FD_EMPTY, Ordering::AcqRel);
350 if i > 2 && prev >= 0 {
351 host::close_fd(prev);
352 }
353 }
354 for slot in &FD_GUEST_NB {
355 slot.store(0, Ordering::Relaxed);
356 }
357 for slot in &FD_TLS {
358 slot.store(0, Ordering::Relaxed);
359 }
360 STDIO_CLOSED.store(0, Ordering::Relaxed);
361 FD_NEXT.store(3, Ordering::Relaxed);
362}
363
364#[derive(Debug)]
369pub struct ProcessState {
370 fds: FdTable,
371 dir_streams: HashMap<i32, host::HostDir>,
373 sig_mask: u32,
374 sigactions: [SoftSigAct; 32],
375}
376
377impl ProcessState {
378 #[must_use]
380 pub fn new() -> Self {
381 Self {
382 fds: FdTable::new(),
383 dir_streams: HashMap::new(),
384 sig_mask: 0,
385 sigactions: [SoftSigAct::zero(); 32],
386 }
387 }
388
389 pub fn reset_run(&mut self, max_syscalls: usize) {
391 self.dir_streams.clear();
392 self.fds.reset();
393 self.sig_mask = 0;
394 self.sigactions = [SoftSigAct::zero(); 32];
395 clear_bsdthread_reg();
396 let max = u64::try_from(max_syscalls).unwrap_or(256);
397 MAX_SYSCALLS.store(max, Ordering::Relaxed);
398 SYSCALL_COUNT.store(0, Ordering::Relaxed);
399 LIVE_CHILDREN.store(0, Ordering::Relaxed);
400 crate::thread::reset_thread_runtime();
401 }
402
403 pub(crate) fn close_dir_stream(&mut self, gfd: i32) {
405 drop(self.dir_streams.remove(&gfd));
406 }
407
408 pub(crate) fn readdir_next(&mut self, gfd: i32) -> Result<Option<(Vec<u8>, u8)>, i64> {
412 let Some(host_fd) = fd_get(gfd) else {
413 return Err(9); };
415
416 if let std::collections::hash_map::Entry::Vacant(e) = self.dir_streams.entry(gfd) {
417 match host::HostDir::open_dup(host_fd) {
418 Ok(dir) => {
419 e.insert(dir);
420 }
421 Err(err) => return Err(i64::from(err)),
422 }
423 }
424
425 let Some(stream) = self.dir_streams.get_mut(&gfd) else {
426 return Err(9);
427 };
428 Ok(stream.read_next())
429 }
430
431 #[must_use]
432 pub(crate) fn sig_mask(&self) -> u32 {
433 self.sig_mask
434 }
435
436 pub(crate) fn set_sig_mask(&mut self, mask: u32) {
437 self.sig_mask = mask;
438 }
439
440 #[must_use]
441 pub(crate) fn sigaction(&self, sig: usize) -> Option<SoftSigAct> {
442 self.sigactions.get(sig).copied()
443 }
444
445 pub(crate) fn set_sigaction(&mut self, sig: usize, act: SoftSigAct) -> bool {
446 if let Some(slot) = self.sigactions.get_mut(sig) {
447 *slot = act;
448 true
449 } else {
450 false
451 }
452 }
453}
454
455impl Default for ProcessState {
456 fn default() -> Self {
457 Self::new()
458 }
459}
460
461#[inline]
463pub fn child_born() {
464 LIVE_CHILDREN.fetch_add(1, Ordering::Relaxed);
465}
466
467#[inline]
469pub fn child_reaped() {
470 let _ = LIVE_CHILDREN.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |n| {
471 Some(n.saturating_sub(1))
472 });
473}
474
475#[must_use]
477#[inline]
478pub fn live_children() -> u64 {
479 LIVE_CHILDREN.load(Ordering::Relaxed)
480}
481
482#[must_use]
486pub fn tick_syscall() -> bool {
487 let n = SYSCALL_COUNT.fetch_add(1, Ordering::Relaxed);
488 n >= MAX_SYSCALLS.load(Ordering::Relaxed)
489}
490
491fn active_lock() -> &'static RwLock<ProcessState> {
492 static ACTIVE: OnceLock<RwLock<ProcessState>> = OnceLock::new();
493 ACTIVE.get_or_init(|| RwLock::new(ProcessState::new()))
494}
495
496pub fn with_mut<R>(f: impl FnOnce(&mut ProcessState) -> R) -> R {
498 match active_lock().write() {
499 Ok(mut guard) => f(&mut guard),
500 Err(poisoned) => f(&mut poisoned.into_inner()),
501 }
502}
503
504pub fn with_ref<R>(f: impl FnOnce(&ProcessState) -> R) -> R {
506 match active_lock().read() {
507 Ok(guard) => f(&guard),
508 Err(poisoned) => f(&poisoned.into_inner()),
509 }
510}
511
512pub fn reset_run(max_syscalls: usize) {
514 with_mut(|p| p.reset_run(max_syscalls));
515 normalize_stdio_pipes_blocking();
518}
519
520pub fn set_bottle_root(root: Option<PathBuf>) {
524 let new_fd = root
525 .as_ref()
526 .map_or(-1, |p| open_bottle_dirfd(p).unwrap_or(-1));
527 let old_fd = BOTTLE_DIRFD.swap(new_fd, Ordering::AcqRel);
528 if old_fd >= 0 {
529 host::close_fd(old_fd);
530 }
531
532 let next = root.map(Arc::new);
533 match BOTTLE_ROOT.write() {
534 Ok(mut guard) => {
535 *guard = next;
536 }
537 Err(poisoned) => {
538 *poisoned.into_inner() = next;
539 }
540 }
541}
542
543#[must_use]
545pub fn bottle_root() -> Option<PathBuf> {
546 with_bottle_root(|p| p.map(Path::to_path_buf))
547}
548
549pub fn with_bottle_root<R>(f: impl FnOnce(Option<&Path>) -> R) -> R {
553 match BOTTLE_ROOT.read() {
554 Ok(guard) => f(guard.as_ref().map(|a| a.as_path())),
555 Err(poisoned) => f(poisoned.into_inner().as_ref().map(|a| a.as_path())),
556 }
557}
558
559#[must_use]
563pub fn bottle_dirfd() -> Option<RawFd> {
564 let fd = BOTTLE_DIRFD.load(Ordering::Acquire);
565 if fd >= 0 { Some(fd) } else { None }
566}
567
568fn open_bottle_dirfd(root: &Path) -> Option<RawFd> {
570 let c = std::ffi::CString::new(root.as_os_str().as_encoded_bytes()).ok()?;
571 host::open_directory(&c)
572}
573
574pub(crate) fn set_bsdthread_reg(reg: BsdThreadReg) {
576 match BSDTHREAD_REG.write() {
577 Ok(mut guard) => {
578 *guard = Some(reg);
579 }
580 Err(poisoned) => {
581 *poisoned.into_inner() = Some(reg);
582 }
583 }
584}
585
586#[must_use]
588pub(crate) fn bsdthread_reg() -> Option<BsdThreadReg> {
589 match BSDTHREAD_REG.read() {
590 Ok(guard) => *guard,
591 Err(poisoned) => *poisoned.into_inner(),
592 }
593}
594
595fn clear_bsdthread_reg() {
596 match BSDTHREAD_REG.write() {
597 Ok(mut guard) => {
598 *guard = None;
599 }
600 Err(poisoned) => {
601 *poisoned.into_inner() = None;
602 }
603 }
604}
605
606#[cfg(test)]
608pub(crate) fn test_lock() -> std::sync::MutexGuard<'static, ()> {
609 use std::sync::Mutex;
610 static LOCK: Mutex<()> = Mutex::new(());
611 LOCK.lock()
612 .unwrap_or_else(std::sync::PoisonError::into_inner)
613}