use std::collections::HashMap;
use std::os::fd::RawFd;
use std::path::PathBuf;
use std::sync::atomic::{AtomicI32, AtomicU64, Ordering};
use std::sync::{OnceLock, RwLock};
use crate::host;
static SYSCALL_COUNT: AtomicU64 = AtomicU64::new(0);
static MAX_SYSCALLS: AtomicU64 = AtomicU64::new(256);
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_NEXT: AtomicI32 = AtomicI32::new(3);
#[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)]
pub(crate) struct FdTable;
impl FdTable {
const fn new() -> Self {
Self
}
pub(crate) fn reset(&mut self) {
let _ = self;
reset_fd_map();
}
pub(crate) fn take(&mut self, gfd: i32) -> Option<RawFd> {
let _ = self;
fd_take(gfd)
}
pub(crate) fn alloc(&mut self, host_fd: RawFd) -> Option<i32> {
let _ = self;
fd_alloc(host_fd)
}
}
#[must_use]
#[inline]
pub fn fd_get(gfd: i32) -> Option<RawFd> {
if gfd == 0 || gfd == 1 || gfd == 2 {
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) }
}
fn fd_take(gfd: i32) -> Option<RawFd> {
if gfd <= 2 {
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 v < 0 { None } else { Some(v) }
}
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_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_NEXT.store(gfd.saturating_add(1), Ordering::Relaxed);
return Some(gfd);
}
}
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);
}
}
FD_NEXT.store(3, Ordering::Relaxed);
}
#[derive(Debug)]
pub struct ProcessState {
fds: FdTable,
dir_streams: HashMap<i32, host::HostDir>,
bottle_root: Option<PathBuf>,
sig_mask: u32,
sigactions: [SoftSigAct; 32],
bsdthread: Option<BsdThreadReg>,
}
impl ProcessState {
#[must_use]
pub fn new() -> Self {
Self {
fds: FdTable::new(),
dir_streams: HashMap::new(),
bottle_root: None,
sig_mask: 0,
sigactions: [SoftSigAct::zero(); 32],
bsdthread: None,
}
}
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];
self.bsdthread = None;
let max = u64::try_from(max_syscalls).unwrap_or(256);
MAX_SYSCALLS.store(max, Ordering::Relaxed);
SYSCALL_COUNT.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 bsdthread(&self) -> Option<BsdThreadReg> {
self.bsdthread
}
pub(crate) fn set_bsdthread(&mut self, reg: BsdThreadReg) {
self.bsdthread = Some(reg);
}
pub(crate) fn fds_mut(&mut self) -> &mut FdTable {
&mut self.fds
}
#[must_use]
pub fn bottle_root(&self) -> Option<&std::path::Path> {
self.bottle_root.as_deref()
}
pub fn set_bottle_root(&mut self, root: Option<PathBuf>) {
self.bottle_root = root;
}
#[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()
}
}
#[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));
}
pub fn set_bottle_root(root: Option<PathBuf>) {
with_mut(|p| p.set_bottle_root(root));
}
#[must_use]
pub fn bottle_root() -> Option<PathBuf> {
with_ref(|p| p.bottle_root.clone())
}
pub fn with_bottle_root<R>(f: impl FnOnce(Option<&std::path::Path>) -> R) -> R {
with_ref(|p| f(p.bottle_root.as_deref()))
}
#[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)
}