use std::collections::HashMap;
use std::os::fd::RawFd;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicI32, AtomicU8, AtomicU64, Ordering};
use std::sync::{Arc, OnceLock, RwLock};
use crate::host;
static SYSCALL_COUNT: AtomicU64 = AtomicU64::new(0);
static MAX_SYSCALLS: AtomicU64 = AtomicU64::new(256);
static LIVE_CHILDREN: AtomicU64 = AtomicU64::new(0);
const FD_SLOTS: usize = 1024;
const FD_EMPTY: i32 = -1;
static FD_HOST: [AtomicI32; FD_SLOTS] = [const { AtomicI32::new(FD_EMPTY) }; FD_SLOTS];
static FD_GUEST_NB: [AtomicU8; FD_SLOTS] = [const { AtomicU8::new(0) }; FD_SLOTS];
static FD_TLS: [AtomicU8; FD_SLOTS] = [const { AtomicU8::new(0) }; FD_SLOTS];
static FD_NEXT: AtomicI32 = AtomicI32::new(3);
static FD_GUEST_PATH: RwLock<Option<HashMap<i32, String>>> = RwLock::new(None);
static STDIO_CLOSED: AtomicU8 = AtomicU8::new(0);
#[derive(Clone, Copy, Debug)]
pub(crate) struct SoftSigAct {
pub handler: u64,
pub mask: u32,
pub flags: i32,
}
#[derive(Clone, Copy, Debug, Default)]
#[allow(dead_code)]
pub(crate) struct BsdThreadReg {
pub threadstart: u64,
pub wqthread: u64,
pub flags: u32,
pub stack_addr_hint: u64,
pub tsd_offset: u32,
pub dispatchqueue_offset: u32,
}
impl SoftSigAct {
pub(crate) const fn zero() -> Self {
Self {
handler: 0,
mask: 0,
flags: 0,
}
}
}
#[derive(Debug, Default)]
struct FdTable;
impl FdTable {
const fn new() -> Self {
Self
}
fn reset(&mut self) {
let _ = self;
reset_fd_map();
}
}
#[must_use]
#[inline]
pub fn fd_get(gfd: i32) -> Option<RawFd> {
if gfd == 0 || gfd == 1 || gfd == 2 {
if stdio_is_closed(gfd) {
return None;
}
return Some(gfd);
}
if gfd < 0 {
return None;
}
let Ok(idx) = usize::try_from(gfd) else {
return None;
};
let slot = FD_HOST.get(idx)?;
let v = slot.load(Ordering::Acquire);
if v < 0 { None } else { Some(v) }
}
#[must_use]
#[inline]
pub fn fd_take(gfd: i32) -> Option<RawFd> {
if gfd == 0 || gfd == 1 || gfd == 2 {
if stdio_is_closed(gfd) {
return None;
}
stdio_mark_closed(gfd);
return Some(gfd);
}
if gfd < 0 {
return None;
}
let Ok(idx) = usize::try_from(gfd) else {
return None;
};
let slot = FD_HOST.get(idx)?;
let v = slot.swap(FD_EMPTY, Ordering::AcqRel);
if let Some(t) = FD_TLS.get(idx) {
t.store(0, Ordering::Release);
}
if let Some(nb) = FD_GUEST_NB.get(idx) {
nb.store(0, Ordering::Release);
}
fd_clear_guest_path(gfd);
if v < 0 { None } else { Some(v) }
}
pub fn fd_set_guest_path(gfd: i32, path: impl Into<String>) {
if gfd < 3 {
return;
}
if let Ok(mut g) = FD_GUEST_PATH.write() {
let map = g.get_or_insert_with(HashMap::new);
map.insert(gfd, path.into());
}
}
#[must_use]
pub fn fd_guest_path(gfd: i32) -> Option<String> {
if gfd < 3 {
return None;
}
FD_GUEST_PATH
.read()
.ok()
.and_then(|g| g.as_ref().and_then(|m| m.get(&gfd).cloned()))
}
fn fd_clear_guest_path(gfd: i32) {
if gfd < 3 {
return;
}
if let Ok(mut g) = FD_GUEST_PATH.write()
&& let Some(map) = g.as_mut()
{
map.remove(&gfd);
}
}
#[inline]
fn stdio_closed_bit(gfd: i32) -> Option<u8> {
match gfd {
0 => Some(1),
1 => Some(2),
2 => Some(4),
_ => None,
}
}
#[inline]
fn stdio_is_closed(gfd: i32) -> bool {
let Some(bit) = stdio_closed_bit(gfd) else {
return false;
};
STDIO_CLOSED.load(Ordering::Acquire) & bit != 0
}
#[inline]
fn stdio_mark_closed(gfd: i32) {
let Some(bit) = stdio_closed_bit(gfd) else {
return;
};
STDIO_CLOSED.fetch_or(bit, Ordering::AcqRel);
}
#[inline]
pub fn stdio_mark_open(gfd: i32) {
let Some(bit) = stdio_closed_bit(gfd) else {
return;
};
STDIO_CLOSED.fetch_and(!bit, Ordering::AcqRel);
}
#[must_use]
#[inline]
pub fn fd_install(gfd: i32, host_fd: RawFd) -> bool {
if host_fd < 0 || gfd < 3 {
return false;
}
let Ok(idx) = usize::try_from(gfd) else {
return false;
};
let Some(slot) = FD_HOST.get(idx) else {
return false;
};
let prev = slot.swap(host_fd, Ordering::AcqRel);
if prev >= 0 && prev != host_fd {
host::close_fd(prev);
}
true
}
#[must_use]
#[inline]
pub fn fd_alloc(host_fd: RawFd) -> Option<i32> {
if host_fd < 0 {
return None;
}
let start = usize::try_from(FD_NEXT.load(Ordering::Relaxed).max(3)).unwrap_or(3);
for idx in start..FD_SLOTS {
if try_claim_slot(idx, host_fd) {
let gfd = i32::try_from(idx).ok()?;
fd_set_guest_nonblock(gfd, false);
FD_NEXT.store(gfd.saturating_add(1), Ordering::Relaxed);
return Some(gfd);
}
}
for idx in 3..start.min(FD_SLOTS) {
if try_claim_slot(idx, host_fd) {
let gfd = i32::try_from(idx).ok()?;
fd_set_guest_nonblock(gfd, false);
FD_NEXT.store(gfd.saturating_add(1), Ordering::Relaxed);
return Some(gfd);
}
}
None
}
#[must_use]
#[inline]
pub fn fd_guest_nonblock(gfd: i32) -> bool {
if gfd < 0 {
return false;
}
let Ok(idx) = usize::try_from(gfd) else {
return false;
};
FD_GUEST_NB
.get(idx)
.is_some_and(|s| s.load(Ordering::Acquire) != 0)
}
#[inline]
pub fn fd_set_guest_nonblock(gfd: i32, nonblock: bool) {
if gfd < 0 {
return;
}
let Ok(idx) = usize::try_from(gfd) else {
return;
};
if let Some(slot) = FD_GUEST_NB.get(idx) {
slot.store(u8::from(nonblock), Ordering::Release);
}
}
#[must_use]
#[inline]
pub fn fd_is_tls(gfd: i32) -> bool {
if gfd < 0 {
return false;
}
let Ok(idx) = usize::try_from(gfd) else {
return false;
};
FD_TLS
.get(idx)
.is_some_and(|s| s.load(Ordering::Acquire) != 0)
}
#[inline]
pub fn fd_set_tls(gfd: i32, is_tls: bool) {
if gfd < 0 {
return;
}
let Ok(idx) = usize::try_from(gfd) else {
return;
};
if let Some(slot) = FD_TLS.get(idx) {
slot.store(u8::from(is_tls), Ordering::Release);
}
}
pub fn normalize_stdio_pipes_blocking() {
for fd in 0_i32..=2 {
if host::fd_is_fifo(fd) {
host::clear_nonblock(fd);
fd_set_guest_nonblock(fd, false);
}
}
}
static BOTTLE_ROOT: RwLock<Option<Arc<PathBuf>>> = RwLock::new(None);
static GUEST_EXECUTABLE: RwLock<Option<Arc<str>>> = RwLock::new(None);
static BOTTLE_DIRFD: AtomicI32 = AtomicI32::new(-1);
static BSDTHREAD_REG: RwLock<Option<BsdThreadReg>> = RwLock::new(None);
fn try_claim_slot(idx: usize, host_fd: RawFd) -> bool {
let Some(slot) = FD_HOST.get(idx) else {
return false;
};
slot.compare_exchange(FD_EMPTY, host_fd, Ordering::AcqRel, Ordering::Relaxed)
.is_ok()
}
fn reset_fd_map() {
for (i, slot) in FD_HOST.iter().enumerate() {
let prev = slot.swap(FD_EMPTY, Ordering::AcqRel);
if i > 2 && prev >= 0 {
host::close_fd(prev);
}
}
for slot in &FD_GUEST_NB {
slot.store(0, Ordering::Relaxed);
}
for slot in &FD_TLS {
slot.store(0, Ordering::Relaxed);
}
if let Ok(mut g) = FD_GUEST_PATH.write() {
*g = None;
}
STDIO_CLOSED.store(0, Ordering::Relaxed);
FD_NEXT.store(3, Ordering::Relaxed);
}
#[derive(Debug)]
pub struct ProcessState {
fds: FdTable,
dir_streams: HashMap<i32, host::HostDir>,
sig_mask: u32,
sigactions: [SoftSigAct; 32],
}
impl ProcessState {
#[must_use]
pub fn new() -> Self {
Self {
fds: FdTable::new(),
dir_streams: HashMap::new(),
sig_mask: 0,
sigactions: [SoftSigAct::zero(); 32],
}
}
pub fn reset_run(&mut self, max_syscalls: usize) {
self.dir_streams.clear();
self.fds.reset();
self.sig_mask = 0;
self.sigactions = [SoftSigAct::zero(); 32];
clear_bsdthread_reg();
let max = u64::try_from(max_syscalls).unwrap_or(256);
MAX_SYSCALLS.store(max, Ordering::Relaxed);
SYSCALL_COUNT.store(0, Ordering::Relaxed);
LIVE_CHILDREN.store(0, Ordering::Relaxed);
crate::thread::reset_thread_runtime();
}
pub(crate) fn close_dir_stream(&mut self, gfd: i32) {
drop(self.dir_streams.remove(&gfd));
}
pub(crate) fn readdir_next(&mut self, gfd: i32) -> Result<Option<(Vec<u8>, u8)>, i64> {
let Some(host_fd) = fd_get(gfd) else {
return Err(9); };
if let std::collections::hash_map::Entry::Vacant(e) = self.dir_streams.entry(gfd) {
match host::HostDir::open_dup(host_fd) {
Ok(dir) => {
e.insert(dir);
}
Err(err) => return Err(i64::from(err)),
}
}
let Some(stream) = self.dir_streams.get_mut(&gfd) else {
return Err(9);
};
Ok(stream.read_next())
}
#[must_use]
pub(crate) fn sig_mask(&self) -> u32 {
self.sig_mask
}
pub(crate) fn set_sig_mask(&mut self, mask: u32) {
self.sig_mask = mask;
}
#[must_use]
pub(crate) fn sigaction(&self, sig: usize) -> Option<SoftSigAct> {
self.sigactions.get(sig).copied()
}
pub(crate) fn set_sigaction(&mut self, sig: usize, act: SoftSigAct) -> bool {
if let Some(slot) = self.sigactions.get_mut(sig) {
*slot = act;
true
} else {
false
}
}
}
impl Default for ProcessState {
fn default() -> Self {
Self::new()
}
}
#[inline]
pub fn child_born() {
LIVE_CHILDREN.fetch_add(1, Ordering::Relaxed);
}
#[inline]
pub fn child_reaped() {
let _ = LIVE_CHILDREN.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |n| {
Some(n.saturating_sub(1))
});
}
#[must_use]
#[inline]
pub fn live_children() -> u64 {
LIVE_CHILDREN.load(Ordering::Relaxed)
}
#[must_use]
pub fn tick_syscall() -> bool {
let n = SYSCALL_COUNT.fetch_add(1, Ordering::Relaxed);
n >= MAX_SYSCALLS.load(Ordering::Relaxed)
}
fn active_lock() -> &'static RwLock<ProcessState> {
static ACTIVE: OnceLock<RwLock<ProcessState>> = OnceLock::new();
ACTIVE.get_or_init(|| RwLock::new(ProcessState::new()))
}
pub fn with_mut<R>(f: impl FnOnce(&mut ProcessState) -> R) -> R {
match active_lock().write() {
Ok(mut guard) => f(&mut guard),
Err(poisoned) => f(&mut poisoned.into_inner()),
}
}
pub fn with_ref<R>(f: impl FnOnce(&ProcessState) -> R) -> R {
match active_lock().read() {
Ok(guard) => f(&guard),
Err(poisoned) => f(&poisoned.into_inner()),
}
}
pub fn reset_run(max_syscalls: usize) {
with_mut(|p| p.reset_run(max_syscalls));
crate::dyld_table::clear();
normalize_stdio_pipes_blocking();
}
pub fn set_bottle_root(root: Option<PathBuf>) {
let new_fd = root
.as_ref()
.map_or(-1, |p| open_bottle_dirfd(p).unwrap_or(-1));
let old_fd = BOTTLE_DIRFD.swap(new_fd, Ordering::AcqRel);
if old_fd >= 0 {
host::close_fd(old_fd);
}
let next = root.map(Arc::new);
match BOTTLE_ROOT.write() {
Ok(mut guard) => {
*guard = next;
}
Err(poisoned) => {
*poisoned.into_inner() = next;
}
}
}
#[must_use]
pub fn bottle_root() -> Option<PathBuf> {
with_bottle_root(|p| p.map(Path::to_path_buf))
}
pub fn with_bottle_root<R>(f: impl FnOnce(Option<&Path>) -> R) -> R {
match BOTTLE_ROOT.read() {
Ok(guard) => f(guard.as_ref().map(|a| a.as_path())),
Err(poisoned) => f(poisoned.into_inner().as_ref().map(|a| a.as_path())),
}
}
pub fn set_guest_executable_path(path: Option<String>) {
let next = path.filter(|s| !s.is_empty() && !s.contains('\0')).map(Arc::from);
match GUEST_EXECUTABLE.write() {
Ok(mut guard) => {
*guard = next;
}
Err(poisoned) => {
*poisoned.into_inner() = next;
}
}
}
#[must_use]
pub fn guest_executable_path() -> Option<String> {
match GUEST_EXECUTABLE.read() {
Ok(guard) => guard.as_ref().map(|s| s.as_ref().to_owned()),
Err(poisoned) => poisoned
.into_inner()
.as_ref()
.map(|s| s.as_ref().to_owned()),
}
}
#[must_use]
pub fn bottle_dirfd() -> Option<RawFd> {
let fd = BOTTLE_DIRFD.load(Ordering::Acquire);
if fd >= 0 { Some(fd) } else { None }
}
fn open_bottle_dirfd(root: &Path) -> Option<RawFd> {
let c = std::ffi::CString::new(root.as_os_str().as_encoded_bytes()).ok()?;
host::open_directory(&c)
}
pub(crate) fn set_bsdthread_reg(reg: BsdThreadReg) {
match BSDTHREAD_REG.write() {
Ok(mut guard) => {
*guard = Some(reg);
}
Err(poisoned) => {
*poisoned.into_inner() = Some(reg);
}
}
}
#[must_use]
pub(crate) fn bsdthread_reg() -> Option<BsdThreadReg> {
match BSDTHREAD_REG.read() {
Ok(guard) => *guard,
Err(poisoned) => *poisoned.into_inner(),
}
}
fn clear_bsdthread_reg() {
match BSDTHREAD_REG.write() {
Ok(mut guard) => {
*guard = None;
}
Err(poisoned) => {
*poisoned.into_inner() = None;
}
}
}
#[cfg(test)]
pub(crate) fn test_lock() -> std::sync::MutexGuard<'static, ()> {
use std::sync::Mutex;
static LOCK: Mutex<()> = Mutex::new(());
LOCK.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}