wasmer_wasix/syscalls/
mod.rs

1#![allow(
2    unused,
3    clippy::too_many_arguments,
4    clippy::cognitive_complexity,
5    clippy::result_large_err
6)]
7
8pub mod types {
9    pub use wasmer_wasix_types::{types::*, wasi};
10}
11
12#[cfg(any(
13    target_os = "freebsd",
14    target_os = "linux",
15    target_os = "android",
16    target_vendor = "apple"
17))]
18pub mod unix;
19#[cfg(target_family = "wasm")]
20pub mod wasm;
21#[cfg(target_os = "windows")]
22pub mod windows;
23
24pub mod journal;
25pub mod wasi;
26pub mod wasix;
27
28use bincode::config;
29use bytes::{Buf, BufMut};
30use futures::{
31    Future,
32    future::{BoxFuture, LocalBoxFuture},
33};
34use tracing::instrument;
35use virtual_mio::block_on;
36pub use wasi::*;
37pub use wasix::*;
38use wasmer_journal::SnapshotTrigger;
39use wasmer_wasix_types::wasix::ThreadStartType;
40
41pub mod legacy;
42
43pub(crate) use std::{
44    borrow::{Borrow, Cow},
45    cell::RefCell,
46    collections::{HashMap, HashSet, hash_map::Entry},
47    convert::{Infallible, TryInto},
48    io::{self, Read, Seek, Write},
49    mem::transmute,
50    net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
51    num::NonZeroU64,
52    ops::{Deref, DerefMut},
53    path::Path,
54    pin::Pin,
55    sync::{
56        Arc, Condvar, Mutex,
57        atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering},
58        mpsc,
59    },
60    task::{Context, Poll},
61    thread::LocalKey,
62    time::Duration,
63};
64use std::{io::IoSlice, marker::PhantomData, mem::MaybeUninit, task::Waker, time::Instant};
65
66pub(crate) use bytes::{Bytes, BytesMut};
67pub(crate) use cooked_waker::IntoWaker;
68pub use journal::*;
69pub(crate) use sha2::Sha256;
70pub(crate) use tracing::{debug, error, trace, warn};
71#[cfg(any(
72    target_os = "freebsd",
73    target_os = "linux",
74    target_os = "android",
75    target_vendor = "apple"
76))]
77pub use unix::*;
78#[cfg(target_family = "wasm")]
79pub use wasm::*;
80
81pub(crate) use virtual_fs::{
82    AsyncSeekExt, AsyncWriteExt, DuplexPipe, FileSystem, FsError, VirtualFile,
83};
84pub(crate) use virtual_net::StreamSecurity;
85pub(crate) use wasmer::{
86    AsStoreMut, AsStoreRef, Extern, Function, FunctionEnv, FunctionEnvMut, Global, Instance,
87    Memory, Memory32, Memory64, MemoryAccessError, MemoryError, MemorySize, MemoryView, Module,
88    OnCalledAction, Pages, RuntimeError, Store, TypedFunction, Value, WasmPtr, WasmSlice,
89};
90pub(crate) use wasmer_wasix_types::{asyncify::__wasi_asyncify_t, wasi::EventUnion};
91#[cfg(target_os = "windows")]
92pub use windows::*;
93
94pub(crate) use self::types::{
95    wasi::{
96        Addressfamily, Advice, Clockid, Dircookie, Dirent, DlFlags, DlHandle, Errno, Event,
97        EventFdReadwrite, Eventrwflags, Eventtype, ExitCode, Fd as WasiFd, Fdflags, Fdflagsext,
98        Fdstat, Filesize, Filestat, Filetype, Fstflags, Linkcount, Longsize, OptionFd, Pid,
99        Prestat, ProcSpawnFdOp, Rights, SignalDisposition, Snapshot0Clockid, Sockoption,
100        Sockstatus, Socktype, StackSnapshot, StdioMode as WasiStdioMode, Streamsecurity,
101        Subscription, SubscriptionFsReadwrite, Tid, Timestamp, TlKey, TlUser, TlVal, Tty, Whence,
102    },
103    *,
104};
105use self::{
106    state::{WasiInstanceGuardMemory, conv_env_vars},
107    utils::WasiDummyWaker,
108};
109pub(crate) use crate::os::task::{
110    process::{WasiProcessId, WasiProcessWait},
111    thread::{WasiThread, WasiThreadId},
112};
113use crate::{
114    DeepSleepWork, RewindPostProcess, RewindState, RewindStateOption, SpawnError, WasiInodes,
115    WasiResult, WasiRuntimeError,
116    fs::{
117        Fd, FdInner, InodeVal, Kind, MAX_SYMLINKS, fs_error_into_wasi_err,
118        virtual_file_type_to_wasi_file_type,
119    },
120    journal::{DynJournal, DynReadableJournal, DynWritableJournal, JournalEffector},
121    os::task::{
122        process::{MaybeCheckpointResult, WasiProcessCheckpoint},
123        thread::{RewindResult, RewindResultType},
124    },
125    utils::store::StoreSnapshot,
126};
127pub(crate) use crate::{
128    Runtime, VirtualTaskManager, WasiEnv, WasiError, WasiFunctionEnv, WasiModuleTreeHandles,
129    WasiVFork,
130    bin_factory::spawn_exec_module,
131    import_object_for_all_wasi_versions, mem_error_to_wasi,
132    net::{
133        read_ip_port,
134        socket::{InodeHttpSocketType, InodeSocket, InodeSocketKind},
135        write_ip_port,
136    },
137    runtime::SpawnType,
138    state::{
139        self, InodeGuard, InodeWeakGuard, PollEvent, PollEventBuilder, WasiFutex, WasiState,
140        iterate_poll_events,
141    },
142    utils::{self, map_io_err},
143};
144pub(crate) use crate::{net::net_error_into_wasi_err, utils::WasiParkingLot};
145
146pub(crate) fn to_offset<M: MemorySize>(offset: usize) -> Result<M::Offset, Errno> {
147    let ret: M::Offset = offset.try_into().map_err(|_| Errno::Inval)?;
148    Ok(ret)
149}
150
151pub(crate) fn from_offset<M: MemorySize>(offset: M::Offset) -> Result<usize, Errno> {
152    let ret: usize = offset.try_into().map_err(|_| Errno::Inval)?;
153    Ok(ret)
154}
155
156pub(crate) fn write_bytes_inner<T: Write, M: MemorySize>(
157    mut write_loc: T,
158    memory: &MemoryView,
159    iovs_arr_cell: WasmSlice<__wasi_ciovec_t<M>>,
160) -> Result<usize, Errno> {
161    let mut bytes_written = 0usize;
162    for iov in iovs_arr_cell.iter() {
163        let iov_inner = iov.read().map_err(mem_error_to_wasi)?;
164        let bytes = WasmPtr::<u8, M>::new(iov_inner.buf)
165            .slice(memory, iov_inner.buf_len)
166            .map_err(mem_error_to_wasi)?;
167        let bytes = bytes.read_to_vec().map_err(mem_error_to_wasi)?;
168        write_loc.write_all(&bytes).map_err(map_io_err)?;
169
170        bytes_written += from_offset::<M>(iov_inner.buf_len)?;
171    }
172    Ok(bytes_written)
173}
174
175pub(crate) fn write_bytes<T: Write, M: MemorySize>(
176    mut write_loc: T,
177    memory: &MemoryView,
178    iovs_arr: WasmSlice<__wasi_ciovec_t<M>>,
179) -> Result<usize, Errno> {
180    let result = write_bytes_inner::<_, M>(&mut write_loc, memory, iovs_arr);
181    write_loc.flush();
182    result
183}
184
185pub(crate) fn copy_from_slice<M: MemorySize>(
186    mut read_loc: &[u8],
187    memory: &MemoryView,
188    iovs_arr: WasmSlice<__wasi_iovec_t<M>>,
189) -> Result<usize, Errno> {
190    let mut bytes_read = 0usize;
191
192    let iovs_arr = iovs_arr.access().map_err(mem_error_to_wasi)?;
193    for iovs in iovs_arr.iter() {
194        let mut buf = WasmPtr::<u8, M>::new(iovs.buf)
195            .slice(memory, iovs.buf_len)
196            .map_err(mem_error_to_wasi)?
197            .access()
198            .map_err(mem_error_to_wasi)?;
199
200        let to_read = from_offset::<M>(iovs.buf_len)?;
201        let to_read = to_read.min(read_loc.len());
202        if to_read == 0 {
203            break;
204        }
205        let (left, right) = read_loc.split_at(to_read);
206        let amt = buf.copy_from_slice_min(left);
207        if amt != to_read {
208            return Ok(bytes_read + amt);
209        }
210
211        read_loc = right;
212        bytes_read += to_read;
213    }
214    Ok(bytes_read)
215}
216
217pub(crate) fn read_bytes<T: Read, M: MemorySize>(
218    mut reader: T,
219    memory: &MemoryView,
220    iovs_arr: WasmSlice<__wasi_iovec_t<M>>,
221) -> Result<usize, Errno> {
222    let mut bytes_read = 0usize;
223
224    let iovs_arr = iovs_arr.access().map_err(mem_error_to_wasi)?;
225    for iovs in iovs_arr.iter() {
226        let mut buf = WasmPtr::<u8, M>::new(iovs.buf)
227            .slice(memory, iovs.buf_len)
228            .map_err(mem_error_to_wasi)?
229            .access()
230            .map_err(mem_error_to_wasi)?;
231
232        let to_read = buf.len();
233        let has_read = reader.read(buf.as_mut()).map_err(map_io_err)?;
234
235        bytes_read += has_read;
236        if has_read != to_read {
237            return Ok(bytes_read);
238        }
239    }
240    Ok(bytes_read)
241}
242
243// TODO: remove allow once inodes are refactored (see comments on [`WasiState`])
244/// Writes data to the stderr
245#[allow(clippy::await_holding_lock)]
246pub unsafe fn stderr_write<'a>(
247    ctx: &FunctionEnvMut<'_, WasiEnv>,
248    buf: &[u8],
249) -> LocalBoxFuture<'a, Result<(), Errno>> {
250    let env = ctx.data();
251    let (memory, state, inodes) = unsafe { env.get_memory_and_wasi_state_and_inodes(ctx, 0) };
252
253    let buf = buf.to_vec();
254    let mut stderr = WasiInodes::stderr_mut(&state.fs.fd_map).map_err(fs_error_into_wasi_err);
255    Box::pin(async move { stderr?.write_all(&buf).await.map_err(map_io_err) })
256}
257
258fn block_on_with_timeout<T, Fut>(
259    tasks: &Arc<dyn VirtualTaskManager>,
260    timeout: Option<Duration>,
261    work: Fut,
262) -> WasiResult<T>
263where
264    Fut: Future<Output = WasiResult<T>>,
265{
266    let mut nonblocking = false;
267    if timeout == Some(Duration::ZERO) {
268        nonblocking = true;
269    }
270    let timeout = async {
271        if let Some(timeout) = timeout {
272            if !nonblocking {
273                tasks.sleep_now(timeout).await
274            } else {
275                InfiniteSleep::default().await
276            }
277        } else {
278            InfiniteSleep::default().await
279        }
280    };
281
282    let work = async move {
283        tokio::select! {
284            // The main work we are doing
285            res = work => res,
286            // Optional timeout
287            _ = timeout => Ok(Err(Errno::Timedout)),
288        }
289    };
290
291    // Fast path
292    if nonblocking {
293        let waker = WasiDummyWaker.into_waker();
294        let mut cx = Context::from_waker(&waker);
295        let mut pinned_work = Box::pin(work);
296        if let Poll::Ready(res) = pinned_work.as_mut().poll(&mut cx) {
297            return res;
298        }
299        return Ok(Err(Errno::Again));
300    }
301
302    // Slow path, block on the work and process process
303    block_on(work)
304}
305
306/// Asyncify takes the current thread and blocks on the async runtime associated with it
307/// thus allowed for asynchronous operations to execute. It has built in functionality
308/// to (optionally) timeout the IO, force exit the process, callback signals and pump
309/// synchronous IO engine
310pub(crate) fn __asyncify<T, Fut>(
311    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
312    timeout: Option<Duration>,
313    work: Fut,
314) -> WasiResult<T>
315where
316    T: 'static,
317    Fut: std::future::Future<Output = Result<T, Errno>>,
318{
319    let mut env = ctx.data();
320
321    // Check if we need to exit the asynchronous loop
322    if let Some(exit_code) = env.should_exit() {
323        return Err(WasiError::Exit(exit_code));
324    }
325
326    // This poller will process any signals when the main working function is idle
327    struct SignalPoller<'a, 'b, Fut, T>
328    where
329        Fut: Future<Output = Result<T, Errno>>,
330    {
331        ctx: &'a mut FunctionEnvMut<'b, WasiEnv>,
332        pinned_work: Pin<Box<Fut>>,
333    }
334    impl<Fut, T> Future for SignalPoller<'_, '_, Fut, T>
335    where
336        Fut: Future<Output = Result<T, Errno>>,
337    {
338        type Output = Result<Fut::Output, WasiError>;
339        fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
340            if let Poll::Ready(res) = Pin::new(&mut self.pinned_work).poll(cx) {
341                return Poll::Ready(Ok(res));
342            }
343            if let Some(signals) = self.ctx.data().thread.pop_signals_or_subscribe(cx.waker()) {
344                if let Err(err) = WasiEnv::process_signals_internal(self.ctx, signals) {
345                    return Poll::Ready(Err(err));
346                }
347                return Poll::Ready(Ok(Err(Errno::Intr)));
348            }
349            Poll::Pending
350        }
351    }
352
353    // Block on the work
354    let mut pinned_work = Box::pin(work);
355    let tasks = env.tasks().clone();
356    let poller = SignalPoller { ctx, pinned_work };
357    block_on_with_timeout(&tasks, timeout, poller)
358}
359
360/// Future that will be polled by asyncify methods
361/// (the return value is what will be returned in rewind
362///  or in the instant response)
363pub type AsyncifyFuture = dyn Future<Output = Bytes> + Send + Sync + 'static;
364
365// This poller will process any signals when the main working function is idle
366struct AsyncifyPoller<'a, 'b, 'c, T, Fut>
367where
368    Fut: Future<Output = T> + Send + Sync + 'static,
369{
370    ctx: &'b mut FunctionEnvMut<'c, WasiEnv>,
371    work: &'a mut Pin<Box<Fut>>,
372}
373impl<T, Fut> Future for AsyncifyPoller<'_, '_, '_, T, Fut>
374where
375    Fut: Future<Output = T> + Send + Sync + 'static,
376{
377    type Output = Result<T, WasiError>;
378
379    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
380        if let Poll::Ready(res) = self.work.as_mut().poll(cx) {
381            return Poll::Ready(Ok(res));
382        }
383
384        if let Err(err) = WasiEnv::do_pending_link_operations(self.ctx, false) {
385            return Poll::Ready(Err(err));
386        }
387
388        let env = self.ctx.data();
389        if let Some(forced_exit) = env.thread.try_join() {
390            return Poll::Ready(Err(WasiError::Exit(forced_exit.unwrap_or_else(|err| {
391                tracing::debug!("exit runtime error - {}", err);
392                Errno::Child.into()
393            }))));
394        }
395        if env.thread.has_signals_or_subscribe(cx.waker()) {
396            let has_exit = {
397                let signals = env.thread.signals().lock().unwrap();
398                signals
399                    .0
400                    .iter()
401                    .filter_map(|sig| {
402                        if *sig == Signal::Sigint
403                            || *sig == Signal::Sigquit
404                            || *sig == Signal::Sigkill
405                            || *sig == Signal::Sigabrt
406                        {
407                            Some(env.thread.set_or_get_exit_code_for_signal(*sig))
408                        } else {
409                            None
410                        }
411                    })
412                    .next()
413            };
414
415            return match WasiEnv::process_signals_and_exit(self.ctx) {
416                Ok(Ok(_)) => {
417                    if let Some(exit_code) = has_exit {
418                        Poll::Ready(Err(WasiError::Exit(exit_code)))
419                    } else {
420                        // Re-subscribe so we get woken up for further signals as well
421                        self.ctx.data().thread.signals_subscribe(cx.waker());
422                        // Retry after Sigwakeup drain: dl ops may have started after the check above.
423                        if let Err(err) = WasiEnv::do_pending_link_operations(self.ctx, false) {
424                            return Poll::Ready(Err(err));
425                        }
426                        Poll::Pending
427                    }
428                }
429                Ok(Err(err)) => Poll::Ready(Err(WasiError::Exit(ExitCode::from(err)))),
430                Err(err) => Poll::Ready(Err(err)),
431            };
432        }
433        Poll::Pending
434    }
435}
436
437pub enum AsyncifyAction<'a, R> {
438    /// Indicates that asyncify callback finished and the
439    /// caller now has ownership of the ctx again
440    Finish(FunctionEnvMut<'a, WasiEnv>, R),
441    /// Indicates that asyncify should unwind by immediately exiting
442    /// the current function
443    Unwind,
444}
445
446/// Exponentially increasing backoff of CPU usage
447///
448/// Under certain conditions the process will exponentially backoff
449/// using waits that either put the thread into a low usage state
450/// or even underload the thread completely when deep sleep is enabled
451///
452/// The use-case for this is to handle rogue WASM processes that
453/// generate excessively high CPU usage and need to be artificially
454/// throttled
455///
456pub(crate) fn maybe_backoff<M: MemorySize>(
457    mut ctx: FunctionEnvMut<'_, WasiEnv>,
458) -> Result<Result<FunctionEnvMut<'_, WasiEnv>, Errno>, WasiError> {
459    let env = ctx.data();
460
461    // Fast path that exits this high volume call if we do not have
462    // exponential backoff enabled
463    if env.enable_exponential_cpu_backoff.is_none() {
464        return Ok(Ok(ctx));
465    }
466
467    // Determine if we need to do a backoff, if so lets do one
468    if let Some(backoff) = env.process.acquire_cpu_backoff_token(env.tasks()) {
469        tracing::trace!("exponential CPU backoff {:?}", backoff.backoff_time());
470        if let AsyncifyAction::Finish(mut ctx, _) =
471            __asyncify_with_deep_sleep::<M, _, _>(ctx, backoff)?
472        {
473            Ok(Ok(ctx))
474        } else {
475            Ok(Err(Errno::Success))
476        }
477    } else {
478        Ok(Ok(ctx))
479    }
480}
481
482/// Asyncify takes the current thread and blocks on the async runtime associated with it
483/// thus allowed for asynchronous operations to execute. It has built in functionality
484/// to (optionally) timeout the IO, force exit the process, callback signals and pump
485/// synchronous IO engine
486///
487/// This will either return the `ctx` as the asyncify has completed successfully
488/// or it will return an WasiError which will exit the WASM call using asyncify
489/// and instead process it on a shared task
490///
491pub(crate) fn __asyncify_with_deep_sleep<M: MemorySize, T, Fut>(
492    mut ctx: FunctionEnvMut<'_, WasiEnv>,
493    work: Fut,
494) -> Result<AsyncifyAction<'_, T>, WasiError>
495where
496    T: serde::Serialize + serde::de::DeserializeOwned,
497    Fut: Future<Output = T> + Send + Sync + 'static,
498{
499    // Determine the deep sleep time
500    let deep_sleep_time = match ctx.data().enable_journal {
501        true => Duration::from_micros(100),
502        false => Duration::from_millis(50),
503    };
504
505    // Box up the trigger
506    let mut trigger = Box::pin(work);
507
508    // Define the work
509    let tasks = ctx.data().tasks().clone();
510    let work = async move {
511        let env = ctx.data();
512
513        // Create the deep sleeper
514        // Deep sleep breaks the linker completely, as it expects other modules to catch the
515        // signal for DL ops
516        let tasks_for_deep_sleep = if env.enable_deep_sleep && env.inner().linker().is_none() {
517            Some(env.tasks().clone())
518        } else {
519            None
520        };
521
522        let deep_sleep_wait = async {
523            if let Some(tasks) = tasks_for_deep_sleep {
524                tasks.sleep_now(deep_sleep_time).await
525            } else {
526                InfiniteSleep::default().await
527            }
528        };
529
530        Ok(tokio::select! {
531            // Inner wait with finializer
532            res = AsyncifyPoller {
533                ctx: &mut ctx,
534                work: &mut trigger,
535            } => {
536                let result = res?;
537                AsyncifyAction::Finish(ctx, result)
538            },
539            // Determines when and if we should go into a deep sleep
540            _ = deep_sleep_wait => {
541                let pid = ctx.data().pid();
542                let tid = ctx.data().tid();
543
544                // We put thread into a deep sleeping state and
545                // notify anyone who is waiting for that
546                let thread = ctx.data().thread.clone();
547                thread.set_deep_sleeping(true);
548                ctx.data().process.inner.1.notify_one();
549
550                tracing::trace!(%pid, %tid, "thread entering deep sleep");
551                deep_sleep::<M>(ctx, Box::pin(async move {
552                    // After this wakes the background work or waking
553                    // event has triggered and its time to result
554                    let result = trigger.await;
555                    tracing::trace!(%pid, %tid, "thread leaving deep sleep");
556                    thread.set_deep_sleeping(false);
557                    bincode::serde::encode_to_vec(&result, config::legacy()).unwrap().into()
558                }))?;
559                AsyncifyAction::Unwind
560            },
561        })
562    };
563
564    // Block until the work is finished or until we
565    // unload the thread using asyncify
566    block_on(work)
567}
568
569/// Asyncify takes the current thread and blocks on the async runtime associated with it
570/// thus allowed for asynchronous operations to execute. It has built in functionality
571/// to (optionally) timeout the IO, force exit the process, callback signals and pump
572/// synchronous IO engine
573pub(crate) fn __asyncify_light<T, Fut>(
574    env: &WasiEnv,
575    _timeout: Option<Duration>,
576    work: Fut,
577) -> WasiResult<T>
578where
579    T: 'static,
580    Fut: Future<Output = Result<T, Errno>>,
581{
582    let snapshot_wait = wait_for_snapshot(env);
583
584    // Block until the work is finished or until we
585    // unload the thread using asyncify
586    Ok(block_on(work))
587}
588
589// This should be compiled away, it will simply wait forever however its never
590// used by itself, normally this is passed into asyncify which will still abort
591// the operating on timeouts, signals or other work due to a select! around the await
592#[derive(Default)]
593pub struct InfiniteSleep {}
594impl std::future::Future for InfiniteSleep {
595    type Output = ();
596    fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
597        Poll::Pending
598    }
599}
600
601/// Performs an immutable operation on the socket while running in an asynchronous runtime
602/// This has built in signal support
603pub(crate) fn __sock_asyncify<T, F, Fut>(
604    env: &WasiEnv,
605    sock: WasiFd,
606    rights: Rights,
607    actor: F,
608) -> Result<T, Errno>
609where
610    F: FnOnce(crate::net::socket::InodeSocket, Fd) -> Fut,
611    Fut: std::future::Future<Output = Result<T, Errno>>,
612{
613    let fd_entry = env.state.fs.get_fd(sock)?;
614    if !rights.is_empty() && !fd_entry.inner.rights.contains(rights) {
615        return Err(Errno::Access);
616    }
617
618    let mut work = {
619        let inode = fd_entry.inode.clone();
620        let tasks = env.tasks().clone();
621        let mut guard = inode.write();
622        match guard.deref_mut() {
623            Kind::Socket { socket } => {
624                // Clone the socket and release the lock
625                let socket = socket.clone();
626                drop(guard);
627
628                // Start the work using the socket
629                actor(socket, fd_entry)
630            }
631            _ => {
632                return Err(Errno::Notsock);
633            }
634        }
635    };
636
637    // Block until the work is finished or until we
638    // unload the thread using asyncify
639    block_on(work)
640}
641
642/// Performs mutable work on a socket under an asynchronous runtime with
643/// built in signal processing
644pub(crate) fn __sock_asyncify_mut<T, F, Fut>(
645    ctx: &'_ mut FunctionEnvMut<'_, WasiEnv>,
646    sock: WasiFd,
647    rights: Rights,
648    actor: F,
649) -> Result<T, Errno>
650where
651    F: FnOnce(crate::net::socket::InodeSocket, Fd) -> Fut,
652    Fut: std::future::Future<Output = Result<T, Errno>>,
653{
654    let env = ctx.data();
655    let tasks = env.tasks().clone();
656
657    let fd_entry = env.state.fs.get_fd(sock)?;
658    if !rights.is_empty() && !fd_entry.inner.rights.contains(rights) {
659        return Err(Errno::Access);
660    }
661
662    let inode = fd_entry.inode.clone();
663    let mut guard = inode.write();
664    match guard.deref_mut() {
665        Kind::Socket { socket } => {
666            // Clone the socket and release the lock
667            let socket = socket.clone();
668            drop(guard);
669
670            // Start the work using the socket
671            let mut work = actor(socket, fd_entry);
672
673            // Otherwise we block on the work and process it
674            // using an asynchronou context
675            block_on(work)
676        }
677        _ => Err(Errno::Notsock),
678    }
679}
680
681/// Performs an immutable operation on the socket while running in an asynchronous runtime
682/// This has built in signal support
683pub(crate) fn __sock_actor<T, F>(
684    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
685    sock: WasiFd,
686    rights: Rights,
687    actor: F,
688) -> Result<T, Errno>
689where
690    T: 'static,
691    F: FnOnce(crate::net::socket::InodeSocket, Fd) -> Result<T, Errno>,
692{
693    let env = ctx.data();
694    let tasks = env.tasks().clone();
695
696    let fd_entry = env.state.fs.get_fd(sock)?;
697    if !rights.is_empty() && !fd_entry.inner.rights.contains(rights) {
698        return Err(Errno::Access);
699    }
700
701    let inode = fd_entry.inode.clone();
702
703    let tasks = env.tasks().clone();
704    let mut guard = inode.write();
705    match guard.deref_mut() {
706        Kind::Socket { socket } => {
707            // Clone the socket and release the lock
708            let socket = socket.clone();
709            drop(guard);
710
711            // Start the work using the socket
712            actor(socket, fd_entry)
713        }
714        _ => Err(Errno::Notsock),
715    }
716}
717
718/// Performs mutable work on a socket under an asynchronous runtime with
719/// built in signal processing
720pub(crate) fn __sock_actor_mut<T, F>(
721    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
722    sock: WasiFd,
723    rights: Rights,
724    actor: F,
725) -> Result<T, Errno>
726where
727    T: 'static,
728    F: FnOnce(crate::net::socket::InodeSocket, Fd) -> Result<T, Errno>,
729{
730    let env = ctx.data();
731    let tasks = env.tasks().clone();
732
733    let fd_entry = env.state.fs.get_fd(sock)?;
734    if !rights.is_empty() && !fd_entry.inner.rights.contains(rights) {
735        return Err(Errno::Access);
736    }
737
738    let inode = fd_entry.inode.clone();
739    let mut guard = inode.write();
740    match guard.deref_mut() {
741        Kind::Socket { socket } => {
742            // Clone the socket and release the lock
743            let socket = socket.clone();
744            drop(guard);
745
746            // Start the work using the socket
747            actor(socket, fd_entry)
748        }
749        _ => Err(Errno::Notsock),
750    }
751}
752
753/// Replaces a socket with another socket in under an asynchronous runtime.
754/// This is used for opening sockets or connecting sockets which changes
755/// the fundamental state of the socket to another state machine
756pub(crate) fn __sock_upgrade<'a, F, Fut>(
757    ctx: &'a mut FunctionEnvMut<'_, WasiEnv>,
758    sock: WasiFd,
759    rights: Rights,
760    actor: F,
761) -> Result<(), Errno>
762where
763    F: FnOnce(crate::net::socket::InodeSocket, Fdflags) -> Fut,
764    Fut: std::future::Future<Output = Result<Option<crate::net::socket::InodeSocket>, Errno>> + 'a,
765{
766    let env = ctx.data();
767    let fd_entry = env.state.fs.get_fd(sock)?;
768    if !rights.is_empty() && !fd_entry.inner.rights.contains(rights) {
769        tracing::warn!(
770            "wasi[{}:{}]::sock_upgrade(fd={}, rights={:?}) - failed - no access rights to upgrade",
771            ctx.data().pid(),
772            ctx.data().tid(),
773            sock,
774            rights
775        );
776        return Err(Errno::Access);
777    }
778
779    let tasks = env.tasks().clone();
780    {
781        let inode = fd_entry.inode;
782        let mut guard = inode.write();
783        match guard.deref_mut() {
784            Kind::Socket { socket } => {
785                let socket = socket.clone();
786                drop(guard);
787
788                // Start the work using the socket
789                let work = actor(socket, fd_entry.inner.flags);
790
791                // Block on the work and process it
792                let res = block_on(work);
793                let new_socket = res?;
794
795                if let Some(mut new_socket) = new_socket {
796                    let mut guard = inode.write();
797                    match guard.deref_mut() {
798                        Kind::Socket { socket, .. } => {
799                            std::mem::swap(socket, &mut new_socket);
800                        }
801                        _ => {
802                            tracing::warn!(
803                                "wasi[{}:{}]::sock_upgrade(fd={}, rights={:?}) - failed - not a socket",
804                                ctx.data().pid(),
805                                ctx.data().tid(),
806                                sock,
807                                rights
808                            );
809                            return Err(Errno::Notsock);
810                        }
811                    }
812                }
813            }
814            _ => {
815                tracing::warn!(
816                    "wasi[{}:{}]::sock_upgrade(fd={}, rights={:?}) - failed - not a socket",
817                    ctx.data().pid(),
818                    ctx.data().tid(),
819                    sock,
820                    rights
821                );
822                return Err(Errno::Notsock);
823            }
824        }
825    }
826
827    Ok(())
828}
829
830#[must_use]
831pub(crate) fn write_buffer_array<M: MemorySize>(
832    memory: &MemoryView,
833    from: &[Vec<u8>],
834    ptr_buffer: WasmPtr<WasmPtr<u8, M>, M>,
835    buffer: WasmPtr<u8, M>,
836) -> Errno {
837    let ptrs = wasi_try_mem!(ptr_buffer.slice(memory, wasi_try!(to_offset::<M>(from.len()))));
838
839    let mut current_buffer_offset = 0usize;
840    for ((i, sub_buffer), ptr) in from.iter().enumerate().zip(ptrs.iter()) {
841        let mut buf_offset = buffer.offset();
842        buf_offset += wasi_try!(to_offset::<M>(current_buffer_offset));
843        let new_ptr = WasmPtr::new(buf_offset);
844        wasi_try_mem!(ptr.write(new_ptr));
845
846        let data =
847            wasi_try_mem!(new_ptr.slice(memory, wasi_try!(to_offset::<M>(sub_buffer.len()))));
848        wasi_try_mem!(data.write_slice(sub_buffer));
849        wasi_try_mem!(
850            wasi_try_mem!(new_ptr.add_offset(wasi_try!(to_offset::<M>(sub_buffer.len()))))
851                .write(memory, 0)
852        );
853
854        current_buffer_offset += sub_buffer.len() + 1;
855    }
856
857    Errno::Success
858}
859
860pub(crate) fn read_string_array<M: MemorySize>(
861    memory: &MemoryView,
862    ptrs: WasmPtr<WasmPtr<u8, M>, M>,
863    count: M::Offset,
864) -> Result<Vec<String>, Errno> {
865    if ptrs.is_null() || count == M::ZERO {
866        return Ok(vec![]);
867    }
868
869    let ptr_slice = ptrs.slice(memory, count).map_err(mem_error_to_wasi)?;
870    let capacity = from_offset::<M>(count)?;
871    let mut result = Vec::with_capacity(capacity);
872    for ptr in ptr_slice.access().map_err(mem_error_to_wasi)?.iter() {
873        let s = ptr
874            .read_utf8_string_with_nul(memory)
875            .map_err(mem_error_to_wasi)?;
876        result.push(s);
877    }
878    Ok(result)
879}
880
881pub(crate) fn parse_env_entries(envs: Vec<String>) -> Result<Vec<(String, String)>, Errno> {
882    envs.into_iter()
883        .map(|env| {
884            let (key, value) = env.split_once('=').ok_or(Errno::Inval)?;
885            Ok((key.to_string(), value.to_string()))
886        })
887        .collect()
888}
889
890pub(crate) fn parse_delimited_string_list(s: &str) -> Vec<String> {
891    s.split(&['\n', '\r'])
892        .map(|a| a.to_string())
893        .filter(|a| !a.is_empty())
894        .collect()
895}
896
897pub(crate) fn parse_delimited_exec_args(s: &str) -> Vec<String> {
898    s.trim_end_matches(['\r', '\n'])
899        .lines()
900        .map(str::to_owned)
901        .collect()
902}
903
904pub(crate) fn parse_delimited_env_list(s: &str) -> Result<Vec<(String, String)>, Errno> {
905    let envs = s
906        .split(&['\n', '\r'])
907        .map(|a| a.to_string())
908        .filter(|a| !a.is_empty())
909        .collect::<Vec<_>>();
910    parse_env_entries(envs)
911}
912
913pub(crate) fn get_current_time_in_nanos() -> Result<Timestamp, Errno> {
914    let now = platform_clock_time_get(Snapshot0Clockid::Monotonic, 1_000_000).unwrap() as u128;
915    Ok(now as Timestamp)
916}
917
918pub(crate) fn get_stack_lower(env: &WasiEnv) -> u64 {
919    env.layout.stack_lower
920}
921
922pub(crate) fn get_stack_upper(env: &WasiEnv) -> u64 {
923    env.layout.stack_upper
924}
925
926pub(crate) unsafe fn get_memory_stack_pointer(
927    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
928) -> Result<u64, String> {
929    // Get the current value of the stack pointer (which we will use
930    // to save all of the stack)
931    let stack_upper = get_stack_upper(ctx.data());
932    let stack_pointer = if let Some(stack_pointer) = ctx
933        .data()
934        .inner()
935        .main_module_instance_handles()
936        .stack_pointer
937        .clone()
938    {
939        match stack_pointer.get(ctx) {
940            Value::I32(a) => a as u64,
941            Value::I64(a) => a as u64,
942            _ => stack_upper,
943        }
944    } else {
945        return Err("failed to save stack: no __stack_pointer global".to_string());
946    };
947    Ok(stack_pointer)
948}
949
950pub(crate) unsafe fn get_memory_stack_offset(
951    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
952) -> Result<u64, String> {
953    let stack_upper = get_stack_upper(ctx.data());
954    let stack_pointer = unsafe { get_memory_stack_pointer(ctx) }?;
955    Ok(stack_upper - stack_pointer)
956}
957
958pub(crate) fn set_memory_stack_offset(
959    env: &WasiEnv,
960    store: &mut impl AsStoreMut,
961    offset: u64,
962) -> Result<(), String> {
963    // Sets the stack pointer
964    let stack_upper = get_stack_upper(env);
965    let stack_pointer = stack_upper - offset;
966    if let Some(stack_pointer_ptr) = env
967        .inner()
968        .main_module_instance_handles()
969        .stack_pointer
970        .clone()
971    {
972        match stack_pointer_ptr.get(store) {
973            Value::I32(_) => {
974                stack_pointer_ptr.set(store, Value::I32(stack_pointer as i32));
975            }
976            Value::I64(_) => {
977                stack_pointer_ptr.set(store, Value::I64(stack_pointer as i64));
978            }
979            _ => {
980                return Err(
981                    "failed to save stack: __stack_pointer global is of an unknown type"
982                        .to_string(),
983                );
984            }
985        }
986    } else {
987        return Err("failed to save stack: no __stack_pointer global".to_string());
988    }
989    Ok(())
990}
991
992#[allow(dead_code)]
993pub(crate) fn get_memory_stack<M: MemorySize>(
994    env: &WasiEnv,
995    store: &mut impl AsStoreMut,
996) -> Result<BytesMut, String> {
997    // Get the current value of the stack pointer (which we will use
998    // to save all of the stack)
999    let stack_base = get_stack_upper(env);
1000    let stack_pointer = if let Some(stack_pointer) = env
1001        .inner()
1002        .main_module_instance_handles()
1003        .stack_pointer
1004        .clone()
1005    {
1006        match stack_pointer.get(store) {
1007            Value::I32(a) => a as u64,
1008            Value::I64(a) => a as u64,
1009            _ => stack_base,
1010        }
1011    } else {
1012        return Err("failed to save stack: no __stack_pointer global".to_string());
1013    };
1014    let memory = env
1015        .try_memory_view(store)
1016        .ok_or_else(|| "unable to access the memory of the instance".to_string())?;
1017    let stack_offset = env.layout.stack_upper - stack_pointer;
1018
1019    // Read the memory stack into a vector
1020    let memory_stack_ptr = WasmPtr::<u8, M>::new(
1021        stack_pointer
1022            .try_into()
1023            .map_err(|err| format!("failed to save stack: stack pointer overflow (stack_pointer={}, stack_lower={}, stack_upper={})", stack_offset, env.layout.stack_lower, env.layout.stack_upper))?,
1024    );
1025
1026    memory_stack_ptr
1027        .slice(
1028            &memory,
1029            stack_offset
1030                .try_into()
1031                .map_err(|err| format!("failed to save stack: stack pointer overflow (stack_pointer={}, stack_lower={}, stack_upper={})", stack_offset, env.layout.stack_lower, env.layout.stack_upper))?,
1032        )
1033        .and_then(|memory_stack| memory_stack.read_to_bytes())
1034        .map_err(|err| format!("failed to read stack: {err}"))
1035}
1036
1037#[allow(dead_code)]
1038pub(crate) fn set_memory_stack<M: MemorySize>(
1039    env: &WasiEnv,
1040    store: &mut impl AsStoreMut,
1041    stack: Bytes,
1042) -> Result<(), String> {
1043    // First we restore the memory stack
1044    let stack_upper = get_stack_upper(env);
1045    let stack_offset = stack.len() as u64;
1046    let stack_pointer = stack_upper - stack_offset;
1047    let stack_ptr = WasmPtr::<u8, M>::new(
1048        stack_pointer
1049            .try_into()
1050            .map_err(|_| "failed to restore stack: stack pointer overflow".to_string())?,
1051    );
1052
1053    let memory = env
1054        .try_memory_view(store)
1055        .ok_or_else(|| "unable to set the stack pointer of the instance".to_string())?;
1056    stack_ptr
1057        .slice(
1058            &memory,
1059            stack_offset
1060                .try_into()
1061                .map_err(|_| "failed to restore stack: stack pointer overflow".to_string())?,
1062        )
1063        .and_then(|memory_stack| memory_stack.write_slice(&stack[..]))
1064        .map_err(|err| format!("failed to write stack: {err}"))?;
1065
1066    // Set the stack pointer itself and return
1067    set_memory_stack_offset(env, store, stack_offset)?;
1068    Ok(())
1069}
1070
1071/// Puts the process to deep sleep and wakes it again when
1072/// the supplied future completes
1073#[must_use = "you must return the result immediately so the stack can unwind"]
1074pub(crate) fn deep_sleep<M: MemorySize>(
1075    mut ctx: FunctionEnvMut<'_, WasiEnv>,
1076    trigger: Pin<Box<AsyncifyFuture>>,
1077) -> Result<(), WasiError> {
1078    // Grab all the globals and serialize them
1079    let store_data = crate::utils::store::capture_store_snapshot(&mut ctx.as_store_mut())
1080        .serialize()
1081        .unwrap();
1082    let store_data = Bytes::from(store_data);
1083    let thread_start = ctx.data().thread.thread_start_type();
1084
1085    // Perform the unwind action
1086    let tasks = ctx.data().tasks().clone();
1087    let res = unwind::<M, _>(ctx, move |mut ctx, memory_stack, rewind_stack| {
1088        let memory_stack = memory_stack.freeze();
1089        let rewind_stack = rewind_stack.freeze();
1090        let thread_layout = ctx.data().thread.memory_layout().clone();
1091
1092        // If journal'ing is enabled then we dump the stack into the journal
1093        if ctx.data().enable_journal {
1094            // Grab all the globals and serialize them
1095            let store_data = crate::utils::store::capture_store_snapshot(&mut ctx.as_store_mut())
1096                .serialize()
1097                .unwrap();
1098            let store_data = Bytes::from(store_data);
1099
1100            tracing::trace!(
1101                "stack snapshot unwind (memory_stack={}, rewind_stack={}, store_data={})",
1102                memory_stack.len(),
1103                rewind_stack.len(),
1104                store_data.len(),
1105            );
1106
1107            #[cfg(feature = "journal")]
1108            {
1109                // Write our thread state to the snapshot
1110                let tid = ctx.data().thread.tid();
1111                let thread_start = ctx.data().thread.thread_start_type();
1112                if let Err(err) = JournalEffector::save_thread_state::<M>(
1113                    &mut ctx,
1114                    tid,
1115                    memory_stack.clone(),
1116                    rewind_stack.clone(),
1117                    store_data.clone(),
1118                    thread_start,
1119                    thread_layout.clone(),
1120                ) {
1121                    return wasmer_types::OnCalledAction::Trap(err.into());
1122                }
1123            }
1124
1125            // If all the threads are now in a deep sleep state
1126            // then we can trigger the idle snapshot event
1127            let inner = ctx.data().process.inner.clone();
1128            let is_idle = {
1129                let mut guard = inner.0.lock().unwrap();
1130                guard.threads.values().all(WasiThread::is_deep_sleeping)
1131            };
1132
1133            // When we idle the journal functionality may be set
1134            // will take a snapshot of the memory and threads so
1135            // that it can resumed.
1136            #[cfg(feature = "journal")]
1137            {
1138                if is_idle && ctx.data_mut().has_snapshot_trigger(SnapshotTrigger::Idle) {
1139                    let mut guard = inner.0.lock().unwrap();
1140                    if let Err(err) = JournalEffector::save_memory_and_snapshot(
1141                        &mut ctx,
1142                        &mut guard,
1143                        SnapshotTrigger::Idle,
1144                    ) {
1145                        return wasmer_types::OnCalledAction::Trap(err.into());
1146                    }
1147                }
1148            }
1149        }
1150
1151        // Schedule the process on the stack so that it can be resumed
1152        OnCalledAction::Trap(Box::new(WasiError::DeepSleep(DeepSleepWork {
1153            trigger,
1154            rewind: RewindState {
1155                memory_stack,
1156                rewind_stack,
1157                store_data,
1158                start: thread_start,
1159                layout: thread_layout,
1160                is_64bit: M::is_64bit(),
1161            },
1162        })))
1163    })?;
1164
1165    // If there is an error then exit the process, otherwise we are done
1166    match res {
1167        Errno::Success => Ok(()),
1168        err => Err(WasiError::Exit(ExitCode::from(err))),
1169    }
1170}
1171
1172#[must_use = "you must return the result immediately so the stack can unwind"]
1173pub fn unwind<M: MemorySize, F>(
1174    mut ctx: FunctionEnvMut<'_, WasiEnv>,
1175    callback: F,
1176) -> Result<Errno, WasiError>
1177where
1178    F: FnOnce(FunctionEnvMut<'_, WasiEnv>, BytesMut, BytesMut) -> OnCalledAction
1179        + Send
1180        + Sync
1181        + 'static,
1182{
1183    // Get the current stack pointer (this will be used to determine the
1184    // upper limit of stack space remaining to unwind into)
1185    let (env, mut store) = ctx.data_and_store_mut();
1186    let memory_stack = match get_memory_stack::<M>(env, &mut store) {
1187        Ok(a) => a,
1188        Err(err) => {
1189            warn!("unable to get the memory stack - {}", err);
1190            return Err(WasiError::Exit(Errno::Unknown.into()));
1191        }
1192    };
1193
1194    // Perform a check to see if we have enough room
1195    let env = ctx.data();
1196    let memory = unsafe { env.memory_view(&ctx) };
1197
1198    // Write the addresses to the start of the stack space
1199    let unwind_pointer = env.layout.stack_lower;
1200    let unwind_data_start =
1201        unwind_pointer + (std::mem::size_of::<__wasi_asyncify_t<M::Offset>>() as u64);
1202    let unwind_data = __wasi_asyncify_t::<M::Offset> {
1203        start: wasi_try_ok!(unwind_data_start.try_into().map_err(|_| Errno::Overflow)),
1204        end: wasi_try_ok!(
1205            (env.layout.stack_upper - memory_stack.len() as u64)
1206                .try_into()
1207                .map_err(|_| Errno::Overflow)
1208        ),
1209    };
1210    let unwind_data_ptr: WasmPtr<__wasi_asyncify_t<M::Offset>, M> = WasmPtr::new(wasi_try_ok!(
1211        unwind_pointer.try_into().map_err(|_| Errno::Overflow)
1212    ));
1213    wasi_try_mem_ok!(unwind_data_ptr.write(&memory, unwind_data));
1214
1215    // Invoke the callback that will prepare to unwind
1216    // We need to start unwinding the stack
1217    let asyncify_data = wasi_try_ok!(unwind_pointer.try_into().map_err(|_| Errno::Overflow));
1218    if let Some(asyncify_start_unwind) = env
1219        .inner()
1220        .static_module_instance_handles()
1221        .and_then(|handles| handles.asyncify_start_unwind.clone())
1222    {
1223        asyncify_start_unwind.call(&mut ctx, asyncify_data);
1224    } else {
1225        warn!("failed to unwind the stack because the asyncify_start_unwind export is missing");
1226        return Err(WasiError::Exit(Errno::Noexec.into()));
1227    }
1228
1229    // Set callback that will be invoked when this process finishes
1230    let env = ctx.data();
1231    let unwind_stack_begin: u64 = unwind_data.start.into();
1232    let total_stack_space = env.layout.stack_size;
1233    let func = ctx.as_ref();
1234    trace!(
1235        stack_upper = env.layout.stack_upper,
1236        stack_lower = env.layout.stack_lower,
1237        "wasi[{}:{}]::unwinding (used_stack_space={} total_stack_space={})",
1238        ctx.data().pid(),
1239        ctx.data().tid(),
1240        memory_stack.len(),
1241        total_stack_space
1242    );
1243    ctx.as_store_mut().on_called(move |mut store| {
1244        let mut ctx = func.into_mut(&mut store);
1245        let env = ctx.data();
1246        let memory = env
1247            .try_memory_view(&ctx)
1248            .ok_or_else(|| "failed to save stack: stack pointer overflow - unable to access the memory of the instance".to_string())?;
1249
1250        let unwind_data_ptr: WasmPtr<__wasi_asyncify_t<M::Offset>, M> = WasmPtr::new(
1251            unwind_pointer
1252                .try_into()
1253                .map_err(|_| Errno::Overflow)
1254                .unwrap(),
1255        );
1256        let unwind_data_result = unwind_data_ptr.read(&memory).unwrap();
1257        let unwind_stack_finish: u64 = unwind_data_result.start.into();
1258        let unwind_size = unwind_stack_finish - unwind_stack_begin;
1259        trace!(
1260            "wasi[{}:{}]::unwound (memory_stack_size={} unwind_size={})",
1261            ctx.data().pid(),
1262            ctx.data().tid(),
1263            memory_stack.len(),
1264            unwind_size
1265        );
1266
1267        // Read the memory stack into a vector
1268        let unwind_stack_ptr = WasmPtr::<u8, M>::new(
1269            unwind_stack_begin
1270                .try_into()
1271                .map_err(|_| "failed to save stack: stack pointer overflow".to_string())?,
1272        );
1273        let unwind_stack = unwind_stack_ptr
1274            .slice(
1275                &memory,
1276                unwind_size
1277                    .try_into()
1278                    .map_err(|_| "failed to save stack: stack pointer overflow".to_string())?,
1279            )
1280            .and_then(|memory_stack| memory_stack.read_to_bytes())
1281            .map_err(|err| format!("failed to read stack: {err}"))?;
1282
1283        // Notify asyncify that we are no longer unwinding
1284        if let Some(asyncify_stop_unwind) = env
1285            .inner()
1286            .static_module_instance_handles()
1287            .and_then(|i| i.asyncify_stop_unwind.clone())
1288        {
1289            asyncify_stop_unwind.call(&mut ctx);
1290        } else {
1291            warn!("failed to unwind the stack because the asyncify_stop_unwind export is missing");
1292            return Ok(OnCalledAction::Finish);
1293        }
1294
1295        Ok(callback(ctx, memory_stack, unwind_stack))
1296    });
1297
1298    // We need to exit the function so that it can unwind and then invoke the callback
1299    Ok(Errno::Success)
1300}
1301
1302// NOTE: not tracing-instrumented because [`rewind_ext`] already is.
1303#[must_use = "the action must be passed to the call loop"]
1304pub fn rewind<M: MemorySize, T>(
1305    mut ctx: FunctionEnvMut<WasiEnv>,
1306    memory_stack: Option<Bytes>,
1307    rewind_stack: Bytes,
1308    store_data: Bytes,
1309    result: T,
1310) -> Errno
1311where
1312    T: serde::Serialize,
1313{
1314    let rewind_result = bincode::serde::encode_to_vec(&result, config::legacy())
1315        .unwrap()
1316        .into();
1317    rewind_ext::<M>(
1318        &mut ctx,
1319        memory_stack,
1320        rewind_stack,
1321        store_data,
1322        RewindResultType::RewindWithResult(rewind_result),
1323    )
1324}
1325
1326#[instrument(level = "trace", skip_all, fields(rewind_stack_len = rewind_stack.len(), store_data_len = store_data.len()))]
1327#[must_use = "the action must be passed to the call loop"]
1328pub fn rewind_ext<M: MemorySize>(
1329    ctx: &mut FunctionEnvMut<WasiEnv>,
1330    memory_stack: Option<Bytes>,
1331    rewind_stack: Bytes,
1332    store_data: Bytes,
1333    rewind_result: RewindResultType,
1334) -> Errno {
1335    // Store the memory stack so that it can be restored later
1336    ctx.data_mut().thread.set_rewind(RewindResult {
1337        memory_stack,
1338        rewind_result,
1339    });
1340
1341    // Deserialize the store data back into a snapshot
1342    let store_snapshot = match StoreSnapshot::deserialize(&store_data[..]) {
1343        Ok(a) => a,
1344        Err(err) => {
1345            warn!("snapshot restore failed - the store snapshot could not be deserialized");
1346            return Errno::Unknown;
1347        }
1348    };
1349    crate::utils::store::restore_store_snapshot(ctx, &store_snapshot);
1350    let env = ctx.data();
1351    let memory = match env.try_memory_view(&ctx) {
1352        Some(v) => v,
1353        None => {
1354            warn!("snapshot restore failed - unable to access the memory of the instance");
1355            return Errno::Unknown;
1356        }
1357    };
1358
1359    // Write the addresses to the start of the stack space
1360    let rewind_pointer = env.layout.stack_lower;
1361    let rewind_data_start =
1362        rewind_pointer + (std::mem::size_of::<__wasi_asyncify_t<M::Offset>>() as u64);
1363    let rewind_data_end = rewind_data_start + (rewind_stack.len() as u64);
1364    if rewind_data_end > env.layout.stack_upper {
1365        warn!(
1366            "attempting to rewind a stack bigger than the allocated stack space ({} > {})",
1367            rewind_data_end, env.layout.stack_upper
1368        );
1369        return Errno::Overflow;
1370    }
1371    let rewind_data = __wasi_asyncify_t::<M::Offset> {
1372        start: wasi_try!(rewind_data_end.try_into().map_err(|_| Errno::Overflow)),
1373        end: wasi_try!(
1374            env.layout
1375                .stack_upper
1376                .try_into()
1377                .map_err(|_| Errno::Overflow)
1378        ),
1379    };
1380    let rewind_data_ptr: WasmPtr<__wasi_asyncify_t<M::Offset>, M> = WasmPtr::new(wasi_try!(
1381        rewind_pointer.try_into().map_err(|_| Errno::Overflow)
1382    ));
1383    wasi_try_mem!(rewind_data_ptr.write(&memory, rewind_data));
1384
1385    // Copy the data to the address
1386    let rewind_stack_ptr = WasmPtr::<u8, M>::new(wasi_try!(
1387        rewind_data_start.try_into().map_err(|_| Errno::Overflow)
1388    ));
1389    wasi_try_mem!(
1390        rewind_stack_ptr
1391            .slice(
1392                &memory,
1393                wasi_try!(rewind_stack.len().try_into().map_err(|_| Errno::Overflow))
1394            )
1395            .and_then(|stack| { stack.write_slice(&rewind_stack[..]) })
1396    );
1397
1398    // Invoke the callback that will prepare to rewind
1399    let asyncify_data = wasi_try!(rewind_pointer.try_into().map_err(|_| Errno::Overflow));
1400    if let Some(asyncify_start_rewind) = env
1401        .inner()
1402        .static_module_instance_handles()
1403        .and_then(|a| a.asyncify_start_rewind.clone())
1404    {
1405        asyncify_start_rewind.call(ctx, asyncify_data);
1406    } else {
1407        warn!(
1408            "failed to rewind the stack because the asyncify_start_rewind export is missing or inaccessible"
1409        );
1410        return Errno::Noexec;
1411    }
1412
1413    Errno::Success
1414}
1415
1416pub fn rewind_ext2(
1417    ctx: &mut FunctionEnvMut<WasiEnv>,
1418    rewind_state: RewindStateOption,
1419) -> Result<(), ExitCode> {
1420    if let Some((rewind_state, rewind_result)) = rewind_state {
1421        tracing::trace!("Rewinding");
1422        let errno = if rewind_state.is_64bit {
1423            crate::rewind_ext::<wasmer_types::Memory64>(
1424                ctx,
1425                Some(rewind_state.memory_stack),
1426                rewind_state.rewind_stack,
1427                rewind_state.store_data,
1428                rewind_result,
1429            )
1430        } else {
1431            crate::rewind_ext::<wasmer_types::Memory32>(
1432                ctx,
1433                Some(rewind_state.memory_stack),
1434                rewind_state.rewind_stack,
1435                rewind_state.store_data,
1436                rewind_result,
1437            )
1438        };
1439
1440        if errno != Errno::Success {
1441            let exit_code = ExitCode::from(errno);
1442            ctx.data().blocking_on_exit(Some(exit_code));
1443            return Err(exit_code);
1444        }
1445    }
1446
1447    Ok(())
1448}
1449
1450pub fn anyhow_err_to_runtime_err(err: anyhow::Error) -> WasiRuntimeError {
1451    WasiRuntimeError::Runtime(RuntimeError::user(err.into()))
1452}
1453
1454pub(crate) unsafe fn handle_rewind<M: MemorySize, T>(
1455    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
1456) -> Option<T>
1457where
1458    T: serde::de::DeserializeOwned,
1459{
1460    unsafe { handle_rewind_ext::<M, T>(ctx, HandleRewindType::ResultDriven) }.flatten()
1461}
1462
1463pub(crate) enum HandleRewindType {
1464    /// Handle rewind types that have a result to be processed
1465    ResultDriven,
1466    /// Handle rewind types that are result-less (generally these
1467    /// are caused by snapshot events)
1468    ResultLess,
1469}
1470
1471pub(crate) unsafe fn handle_rewind_ext_with_default<M: MemorySize, T>(
1472    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
1473    type_: HandleRewindType,
1474) -> Option<T>
1475where
1476    T: serde::de::DeserializeOwned + Default,
1477{
1478    let ret = unsafe { handle_rewind_ext::<M, T>(ctx, type_) };
1479    ret.unwrap_or_default()
1480}
1481
1482pub(crate) unsafe fn handle_rewind_ext<M: MemorySize, T>(
1483    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
1484    type_: HandleRewindType,
1485) -> Option<Option<T>>
1486where
1487    T: serde::de::DeserializeOwned,
1488{
1489    let env = ctx.data();
1490    if !env.thread.has_rewind_of_type(type_) {
1491        return None;
1492    };
1493
1494    // If the stack has been restored
1495    let tid = env.tid();
1496    let pid = env.pid();
1497    if let Some(result) = ctx.data_mut().thread.take_rewind() {
1498        // Deserialize the result
1499        let memory_stack = result.memory_stack;
1500
1501        // Notify asyncify that we are no longer rewinding
1502        let env = ctx.data();
1503        if let Some(asyncify_stop_rewind) = env
1504            .inner()
1505            .static_module_instance_handles()
1506            .and_then(|handles| handles.asyncify_stop_rewind.clone())
1507        {
1508            asyncify_stop_rewind.call(ctx);
1509        } else {
1510            warn!(
1511                "failed to handle rewind because the asyncify_stop_rewind export is missing or inaccessible"
1512            );
1513            return Some(None);
1514        }
1515
1516        // Restore the memory stack
1517        let (env, mut store) = ctx.data_and_store_mut();
1518        if let Some(memory_stack) = memory_stack {
1519            set_memory_stack::<M>(env, &mut store, memory_stack);
1520        }
1521
1522        match result.rewind_result {
1523            RewindResultType::RewindRestart => {
1524                tracing::trace!(%pid, %tid, "rewind for syscall restart");
1525                None
1526            }
1527            RewindResultType::RewindWithoutResult => {
1528                tracing::trace!(%pid, %tid, "rewind with no result");
1529                Some(None)
1530            }
1531            RewindResultType::RewindWithResult(rewind_result) => {
1532                tracing::trace!(%pid, %tid, "rewind with result (data={})", rewind_result.len());
1533                let (ret, _) = bincode::serde::decode_from_slice(&rewind_result, config::legacy())
1534                    .expect("failed to deserialize the rewind result");
1535                Some(Some(ret))
1536            }
1537        }
1538    } else {
1539        tracing::trace!(%pid, %tid, "rewind miss");
1540        Some(None)
1541    }
1542}
1543
1544// Function to prepare the WASI environment
1545pub(crate) fn _prepare_wasi(
1546    wasi_env: &mut WasiEnv,
1547    args: Option<Vec<String>>,
1548    envs: Option<Vec<(String, String)>>,
1549    signals: Option<Vec<SignalDisposition>>,
1550) {
1551    // Swap out the arguments with the new ones
1552    if let Some(args) = args {
1553        *wasi_env.state.args.lock().unwrap() = args;
1554    }
1555
1556    // Update the env vars
1557    if let Some(envs) = envs {
1558        let mut guard = wasi_env.state.envs.lock().unwrap();
1559
1560        let mut existing_envs = guard
1561            .iter()
1562            .map(|b| {
1563                let string = String::from_utf8_lossy(b);
1564                let (key, val) = string.split_once('=').expect("env var is malformed");
1565
1566                (key.to_string(), val.to_string().as_bytes().to_vec())
1567            })
1568            .collect::<Vec<_>>();
1569
1570        for (key, val) in envs {
1571            let val = val.as_bytes().to_vec();
1572            match existing_envs
1573                .iter_mut()
1574                .find(|(existing_key, _)| existing_key == &key)
1575            {
1576                Some((_, existing_val)) => *existing_val = val,
1577                None => existing_envs.push((key, val)),
1578            }
1579        }
1580
1581        let envs = conv_env_vars(existing_envs);
1582
1583        *guard = envs;
1584
1585        drop(guard)
1586    }
1587
1588    if let Some(signals) = signals {
1589        let mut guard = wasi_env.state.signals.lock().unwrap();
1590        for signal in signals {
1591            guard.insert(signal.sig, signal.disp);
1592        }
1593        drop(guard);
1594    }
1595}
1596
1597pub(crate) fn conv_spawn_err_to_errno(err: &SpawnError) -> Errno {
1598    match err {
1599        SpawnError::AccessDenied => Errno::Access,
1600        SpawnError::Unsupported => Errno::Noexec,
1601        _ if err.is_not_found() => Errno::Noent,
1602        _ => Errno::Inval,
1603    }
1604}
1605
1606pub(crate) fn conv_spawn_err_to_exit_code(err: &SpawnError) -> ExitCode {
1607    conv_spawn_err_to_errno(err).into()
1608}