1use std::collections::HashMap;
11use std::os::fd::RawFd;
12use std::path::{Path, PathBuf};
13use std::sync::atomic::{AtomicI32, 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
23const FD_SLOTS: usize = 1024;
25const FD_EMPTY: i32 = -1;
27
28static FD_HOST: [AtomicI32; FD_SLOTS] = [const { AtomicI32::new(FD_EMPTY) }; FD_SLOTS];
33static FD_NEXT: AtomicI32 = AtomicI32::new(3);
35
36#[derive(Clone, Copy, Debug)]
38pub(crate) struct SoftSigAct {
39 pub handler: u64,
40 pub mask: u32,
41 pub flags: i32,
42}
43
44#[derive(Clone, Copy, Debug, Default)]
48#[allow(dead_code)]
49pub(crate) struct BsdThreadReg {
50 pub threadstart: u64,
52 pub wqthread: u64,
54 pub flags: u32,
56 pub stack_addr_hint: u64,
58 pub tsd_offset: u32,
60 pub dispatchqueue_offset: u32,
62}
63
64impl SoftSigAct {
65 pub(crate) const fn zero() -> Self {
66 Self {
67 handler: 0,
68 mask: 0,
69 flags: 0,
70 }
71 }
72}
73
74#[derive(Debug, Default)]
76struct FdTable;
77
78impl FdTable {
79 const fn new() -> Self {
80 Self
81 }
82
83 fn reset(&mut self) {
84 let _ = self;
85 reset_fd_map();
86 }
87}
88
89#[must_use]
91#[inline]
92pub fn fd_get(gfd: i32) -> Option<RawFd> {
93 if gfd == 0 || gfd == 1 || gfd == 2 {
94 return Some(gfd);
95 }
96 if gfd < 0 {
97 return None;
98 }
99 let Ok(idx) = usize::try_from(gfd) else {
100 return None;
101 };
102 let slot = FD_HOST.get(idx)?;
103 let v = slot.load(Ordering::Acquire);
104 if v < 0 { None } else { Some(v) }
105}
106
107#[must_use]
109#[inline]
110pub fn fd_take(gfd: i32) -> Option<RawFd> {
111 if gfd <= 2 {
112 return None;
113 }
114 let Ok(idx) = usize::try_from(gfd) else {
115 return None;
116 };
117 let slot = FD_HOST.get(idx)?;
118 let v = slot.swap(FD_EMPTY, Ordering::AcqRel);
119 if v < 0 { None } else { Some(v) }
120}
121
122#[must_use]
124#[inline]
125pub fn fd_alloc(host_fd: RawFd) -> Option<i32> {
126 if host_fd < 0 {
127 return None;
128 }
129 let start = usize::try_from(FD_NEXT.load(Ordering::Relaxed).max(3)).unwrap_or(3);
130 for idx in start..FD_SLOTS {
131 if try_claim_slot(idx, host_fd) {
132 let gfd = i32::try_from(idx).ok()?;
133 FD_NEXT.store(gfd.saturating_add(1), Ordering::Relaxed);
134 return Some(gfd);
135 }
136 }
137 for idx in 3..start.min(FD_SLOTS) {
138 if try_claim_slot(idx, host_fd) {
139 let gfd = i32::try_from(idx).ok()?;
140 FD_NEXT.store(gfd.saturating_add(1), Ordering::Relaxed);
141 return Some(gfd);
142 }
143 }
144 None
145}
146
147static BOTTLE_ROOT: RwLock<Option<Arc<PathBuf>>> = RwLock::new(None);
149
150static BSDTHREAD_REG: RwLock<Option<BsdThreadReg>> = RwLock::new(None);
152
153fn try_claim_slot(idx: usize, host_fd: RawFd) -> bool {
154 let Some(slot) = FD_HOST.get(idx) else {
155 return false;
156 };
157 slot.compare_exchange(FD_EMPTY, host_fd, Ordering::AcqRel, Ordering::Relaxed)
158 .is_ok()
159}
160
161fn reset_fd_map() {
162 for (i, slot) in FD_HOST.iter().enumerate() {
163 let prev = slot.swap(FD_EMPTY, Ordering::AcqRel);
164 if i > 2 && prev >= 0 {
165 host::close_fd(prev);
166 }
167 }
168 FD_NEXT.store(3, Ordering::Relaxed);
169}
170
171#[derive(Debug)]
176pub struct ProcessState {
177 fds: FdTable,
178 dir_streams: HashMap<i32, host::HostDir>,
180 sig_mask: u32,
181 sigactions: [SoftSigAct; 32],
182}
183
184impl ProcessState {
185 #[must_use]
187 pub fn new() -> Self {
188 Self {
189 fds: FdTable::new(),
190 dir_streams: HashMap::new(),
191 sig_mask: 0,
192 sigactions: [SoftSigAct::zero(); 32],
193 }
194 }
195
196 pub fn reset_run(&mut self, max_syscalls: usize) {
198 self.dir_streams.clear();
199 self.fds.reset();
200 self.sig_mask = 0;
201 self.sigactions = [SoftSigAct::zero(); 32];
202 clear_bsdthread_reg();
203 let max = u64::try_from(max_syscalls).unwrap_or(256);
204 MAX_SYSCALLS.store(max, Ordering::Relaxed);
205 SYSCALL_COUNT.store(0, Ordering::Relaxed);
206 crate::thread::reset_thread_runtime();
207 }
208
209 pub(crate) fn close_dir_stream(&mut self, gfd: i32) {
211 drop(self.dir_streams.remove(&gfd));
212 }
213
214 pub(crate) fn readdir_next(&mut self, gfd: i32) -> Result<Option<(Vec<u8>, u8)>, i64> {
218 let Some(host_fd) = fd_get(gfd) else {
219 return Err(9); };
221
222 if let std::collections::hash_map::Entry::Vacant(e) = self.dir_streams.entry(gfd) {
223 match host::HostDir::open_dup(host_fd) {
224 Ok(dir) => {
225 e.insert(dir);
226 }
227 Err(err) => return Err(i64::from(err)),
228 }
229 }
230
231 let Some(stream) = self.dir_streams.get_mut(&gfd) else {
232 return Err(9);
233 };
234 Ok(stream.read_next())
235 }
236
237 #[must_use]
238 pub(crate) fn sig_mask(&self) -> u32 {
239 self.sig_mask
240 }
241
242 pub(crate) fn set_sig_mask(&mut self, mask: u32) {
243 self.sig_mask = mask;
244 }
245
246 #[must_use]
247 pub(crate) fn sigaction(&self, sig: usize) -> Option<SoftSigAct> {
248 self.sigactions.get(sig).copied()
249 }
250
251 pub(crate) fn set_sigaction(&mut self, sig: usize, act: SoftSigAct) -> bool {
252 if let Some(slot) = self.sigactions.get_mut(sig) {
253 *slot = act;
254 true
255 } else {
256 false
257 }
258 }
259}
260
261impl Default for ProcessState {
262 fn default() -> Self {
263 Self::new()
264 }
265}
266
267#[must_use]
271pub fn tick_syscall() -> bool {
272 let n = SYSCALL_COUNT.fetch_add(1, Ordering::Relaxed);
273 n >= MAX_SYSCALLS.load(Ordering::Relaxed)
274}
275
276fn active_lock() -> &'static RwLock<ProcessState> {
277 static ACTIVE: OnceLock<RwLock<ProcessState>> = OnceLock::new();
278 ACTIVE.get_or_init(|| RwLock::new(ProcessState::new()))
279}
280
281pub fn with_mut<R>(f: impl FnOnce(&mut ProcessState) -> R) -> R {
283 match active_lock().write() {
284 Ok(mut guard) => f(&mut guard),
285 Err(poisoned) => f(&mut poisoned.into_inner()),
286 }
287}
288
289pub fn with_ref<R>(f: impl FnOnce(&ProcessState) -> R) -> R {
291 match active_lock().read() {
292 Ok(guard) => f(&guard),
293 Err(poisoned) => f(&poisoned.into_inner()),
294 }
295}
296
297pub fn reset_run(max_syscalls: usize) {
299 with_mut(|p| p.reset_run(max_syscalls));
300}
301
302pub fn set_bottle_root(root: Option<PathBuf>) {
304 let next = root.map(Arc::new);
305 match BOTTLE_ROOT.write() {
306 Ok(mut guard) => {
307 *guard = next;
308 }
309 Err(poisoned) => {
310 *poisoned.into_inner() = next;
311 }
312 }
313}
314
315#[must_use]
317pub fn bottle_root() -> Option<PathBuf> {
318 with_bottle_root(|p| p.map(Path::to_path_buf))
319}
320
321pub fn with_bottle_root<R>(f: impl FnOnce(Option<&Path>) -> R) -> R {
325 match BOTTLE_ROOT.read() {
326 Ok(guard) => f(guard.as_ref().map(|a| a.as_path())),
327 Err(poisoned) => f(poisoned.into_inner().as_ref().map(|a| a.as_path())),
328 }
329}
330
331pub(crate) fn set_bsdthread_reg(reg: BsdThreadReg) {
333 match BSDTHREAD_REG.write() {
334 Ok(mut guard) => {
335 *guard = Some(reg);
336 }
337 Err(poisoned) => {
338 *poisoned.into_inner() = Some(reg);
339 }
340 }
341}
342
343#[must_use]
345pub(crate) fn bsdthread_reg() -> Option<BsdThreadReg> {
346 match BSDTHREAD_REG.read() {
347 Ok(guard) => *guard,
348 Err(poisoned) => *poisoned.into_inner(),
349 }
350}
351
352fn clear_bsdthread_reg() {
353 match BSDTHREAD_REG.write() {
354 Ok(mut guard) => {
355 *guard = None;
356 }
357 Err(poisoned) => {
358 *poisoned.into_inner() = None;
359 }
360 }
361}
362
363#[cfg(test)]
365pub(crate) fn test_lock() -> std::sync::MutexGuard<'static, ()> {
366 use std::sync::Mutex;
367 static LOCK: Mutex<()> = Mutex::new(());
368 LOCK.lock()
369 .unwrap_or_else(std::sync::PoisonError::into_inner)
370}