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        WasiEnv::do_pending_link_operations(self.ctx, false);
385
386        let env = self.ctx.data();
387        if let Some(forced_exit) = env.thread.try_join() {
388            return Poll::Ready(Err(WasiError::Exit(forced_exit.unwrap_or_else(|err| {
389                tracing::debug!("exit runtime error - {}", err);
390                Errno::Child.into()
391            }))));
392        }
393        if env.thread.has_signals_or_subscribe(cx.waker()) {
394            let has_exit = {
395                let signals = env.thread.signals().lock().unwrap();
396                signals
397                    .0
398                    .iter()
399                    .filter_map(|sig| {
400                        if *sig == Signal::Sigint
401                            || *sig == Signal::Sigquit
402                            || *sig == Signal::Sigkill
403                            || *sig == Signal::Sigabrt
404                        {
405                            Some(env.thread.set_or_get_exit_code_for_signal(*sig))
406                        } else {
407                            None
408                        }
409                    })
410                    .next()
411            };
412
413            return match WasiEnv::process_signals_and_exit(self.ctx) {
414                Ok(Ok(_)) => {
415                    if let Some(exit_code) = has_exit {
416                        Poll::Ready(Err(WasiError::Exit(exit_code)))
417                    } else {
418                        // Re-subscribe so we get woken up for further signals as well
419                        self.ctx.data().thread.signals_subscribe(cx.waker());
420                        Poll::Pending
421                    }
422                }
423                Ok(Err(err)) => Poll::Ready(Err(WasiError::Exit(ExitCode::from(err)))),
424                Err(err) => Poll::Ready(Err(err)),
425            };
426        }
427        Poll::Pending
428    }
429}
430
431pub enum AsyncifyAction<'a, R> {
432    /// Indicates that asyncify callback finished and the
433    /// caller now has ownership of the ctx again
434    Finish(FunctionEnvMut<'a, WasiEnv>, R),
435    /// Indicates that asyncify should unwind by immediately exiting
436    /// the current function
437    Unwind,
438}
439
440/// Exponentially increasing backoff of CPU usage
441///
442/// Under certain conditions the process will exponentially backoff
443/// using waits that either put the thread into a low usage state
444/// or even underload the thread completely when deep sleep is enabled
445///
446/// The use-case for this is to handle rogue WASM processes that
447/// generate excessively high CPU usage and need to be artificially
448/// throttled
449///
450pub(crate) fn maybe_backoff<M: MemorySize>(
451    mut ctx: FunctionEnvMut<'_, WasiEnv>,
452) -> Result<Result<FunctionEnvMut<'_, WasiEnv>, Errno>, WasiError> {
453    let env = ctx.data();
454
455    // Fast path that exits this high volume call if we do not have
456    // exponential backoff enabled
457    if env.enable_exponential_cpu_backoff.is_none() {
458        return Ok(Ok(ctx));
459    }
460
461    // Determine if we need to do a backoff, if so lets do one
462    if let Some(backoff) = env.process.acquire_cpu_backoff_token(env.tasks()) {
463        tracing::trace!("exponential CPU backoff {:?}", backoff.backoff_time());
464        if let AsyncifyAction::Finish(mut ctx, _) =
465            __asyncify_with_deep_sleep::<M, _, _>(ctx, backoff)?
466        {
467            Ok(Ok(ctx))
468        } else {
469            Ok(Err(Errno::Success))
470        }
471    } else {
472        Ok(Ok(ctx))
473    }
474}
475
476/// Asyncify takes the current thread and blocks on the async runtime associated with it
477/// thus allowed for asynchronous operations to execute. It has built in functionality
478/// to (optionally) timeout the IO, force exit the process, callback signals and pump
479/// synchronous IO engine
480///
481/// This will either return the `ctx` as the asyncify has completed successfully
482/// or it will return an WasiError which will exit the WASM call using asyncify
483/// and instead process it on a shared task
484///
485pub(crate) fn __asyncify_with_deep_sleep<M: MemorySize, T, Fut>(
486    mut ctx: FunctionEnvMut<'_, WasiEnv>,
487    work: Fut,
488) -> Result<AsyncifyAction<'_, T>, WasiError>
489where
490    T: serde::Serialize + serde::de::DeserializeOwned,
491    Fut: Future<Output = T> + Send + Sync + 'static,
492{
493    // Determine the deep sleep time
494    let deep_sleep_time = match ctx.data().enable_journal {
495        true => Duration::from_micros(100),
496        false => Duration::from_millis(50),
497    };
498
499    // Box up the trigger
500    let mut trigger = Box::pin(work);
501
502    // Define the work
503    let tasks = ctx.data().tasks().clone();
504    let work = async move {
505        let env = ctx.data();
506
507        // Create the deep sleeper
508        // Deep sleep breaks the linker completely, as it expects other modules to catch the
509        // signal for DL ops
510        let tasks_for_deep_sleep = if env.enable_deep_sleep && env.inner().linker().is_none() {
511            Some(env.tasks().clone())
512        } else {
513            None
514        };
515
516        let deep_sleep_wait = async {
517            if let Some(tasks) = tasks_for_deep_sleep {
518                tasks.sleep_now(deep_sleep_time).await
519            } else {
520                InfiniteSleep::default().await
521            }
522        };
523
524        Ok(tokio::select! {
525            // Inner wait with finializer
526            res = AsyncifyPoller {
527                ctx: &mut ctx,
528                work: &mut trigger,
529            } => {
530                let result = res?;
531                AsyncifyAction::Finish(ctx, result)
532            },
533            // Determines when and if we should go into a deep sleep
534            _ = deep_sleep_wait => {
535                let pid = ctx.data().pid();
536                let tid = ctx.data().tid();
537
538                // We put thread into a deep sleeping state and
539                // notify anyone who is waiting for that
540                let thread = ctx.data().thread.clone();
541                thread.set_deep_sleeping(true);
542                ctx.data().process.inner.1.notify_one();
543
544                tracing::trace!(%pid, %tid, "thread entering deep sleep");
545                deep_sleep::<M>(ctx, Box::pin(async move {
546                    // After this wakes the background work or waking
547                    // event has triggered and its time to result
548                    let result = trigger.await;
549                    tracing::trace!(%pid, %tid, "thread leaving deep sleep");
550                    thread.set_deep_sleeping(false);
551                    bincode::serde::encode_to_vec(&result, config::legacy()).unwrap().into()
552                }))?;
553                AsyncifyAction::Unwind
554            },
555        })
556    };
557
558    // Block until the work is finished or until we
559    // unload the thread using asyncify
560    block_on(work)
561}
562
563/// Asyncify takes the current thread and blocks on the async runtime associated with it
564/// thus allowed for asynchronous operations to execute. It has built in functionality
565/// to (optionally) timeout the IO, force exit the process, callback signals and pump
566/// synchronous IO engine
567pub(crate) fn __asyncify_light<T, Fut>(
568    env: &WasiEnv,
569    _timeout: Option<Duration>,
570    work: Fut,
571) -> WasiResult<T>
572where
573    T: 'static,
574    Fut: Future<Output = Result<T, Errno>>,
575{
576    let snapshot_wait = wait_for_snapshot(env);
577
578    // Block until the work is finished or until we
579    // unload the thread using asyncify
580    Ok(block_on(work))
581}
582
583// This should be compiled away, it will simply wait forever however its never
584// used by itself, normally this is passed into asyncify which will still abort
585// the operating on timeouts, signals or other work due to a select! around the await
586#[derive(Default)]
587pub struct InfiniteSleep {}
588impl std::future::Future for InfiniteSleep {
589    type Output = ();
590    fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
591        Poll::Pending
592    }
593}
594
595/// Performs an immutable operation on the socket while running in an asynchronous runtime
596/// This has built in signal support
597pub(crate) fn __sock_asyncify<T, F, Fut>(
598    env: &WasiEnv,
599    sock: WasiFd,
600    rights: Rights,
601    actor: F,
602) -> Result<T, Errno>
603where
604    F: FnOnce(crate::net::socket::InodeSocket, Fd) -> Fut,
605    Fut: std::future::Future<Output = Result<T, Errno>>,
606{
607    let fd_entry = env.state.fs.get_fd(sock)?;
608    if !rights.is_empty() && !fd_entry.inner.rights.contains(rights) {
609        return Err(Errno::Access);
610    }
611
612    let mut work = {
613        let inode = fd_entry.inode.clone();
614        let tasks = env.tasks().clone();
615        let mut guard = inode.write();
616        match guard.deref_mut() {
617            Kind::Socket { socket } => {
618                // Clone the socket and release the lock
619                let socket = socket.clone();
620                drop(guard);
621
622                // Start the work using the socket
623                actor(socket, fd_entry)
624            }
625            _ => {
626                return Err(Errno::Notsock);
627            }
628        }
629    };
630
631    // Block until the work is finished or until we
632    // unload the thread using asyncify
633    block_on(work)
634}
635
636/// Performs mutable work on a socket under an asynchronous runtime with
637/// built in signal processing
638pub(crate) fn __sock_asyncify_mut<T, F, Fut>(
639    ctx: &'_ mut FunctionEnvMut<'_, WasiEnv>,
640    sock: WasiFd,
641    rights: Rights,
642    actor: F,
643) -> Result<T, Errno>
644where
645    F: FnOnce(crate::net::socket::InodeSocket, Fd) -> Fut,
646    Fut: std::future::Future<Output = Result<T, Errno>>,
647{
648    let env = ctx.data();
649    let tasks = env.tasks().clone();
650
651    let fd_entry = env.state.fs.get_fd(sock)?;
652    if !rights.is_empty() && !fd_entry.inner.rights.contains(rights) {
653        return Err(Errno::Access);
654    }
655
656    let inode = fd_entry.inode.clone();
657    let mut guard = inode.write();
658    match guard.deref_mut() {
659        Kind::Socket { socket } => {
660            // Clone the socket and release the lock
661            let socket = socket.clone();
662            drop(guard);
663
664            // Start the work using the socket
665            let mut work = actor(socket, fd_entry);
666
667            // Otherwise we block on the work and process it
668            // using an asynchronou context
669            block_on(work)
670        }
671        _ => Err(Errno::Notsock),
672    }
673}
674
675/// Performs an immutable operation on the socket while running in an asynchronous runtime
676/// This has built in signal support
677pub(crate) fn __sock_actor<T, F>(
678    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
679    sock: WasiFd,
680    rights: Rights,
681    actor: F,
682) -> Result<T, Errno>
683where
684    T: 'static,
685    F: FnOnce(crate::net::socket::InodeSocket, Fd) -> Result<T, Errno>,
686{
687    let env = ctx.data();
688    let tasks = env.tasks().clone();
689
690    let fd_entry = env.state.fs.get_fd(sock)?;
691    if !rights.is_empty() && !fd_entry.inner.rights.contains(rights) {
692        return Err(Errno::Access);
693    }
694
695    let inode = fd_entry.inode.clone();
696
697    let tasks = env.tasks().clone();
698    let mut guard = inode.write();
699    match guard.deref_mut() {
700        Kind::Socket { socket } => {
701            // Clone the socket and release the lock
702            let socket = socket.clone();
703            drop(guard);
704
705            // Start the work using the socket
706            actor(socket, fd_entry)
707        }
708        _ => Err(Errno::Notsock),
709    }
710}
711
712/// Performs mutable work on a socket under an asynchronous runtime with
713/// built in signal processing
714pub(crate) fn __sock_actor_mut<T, F>(
715    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
716    sock: WasiFd,
717    rights: Rights,
718    actor: F,
719) -> Result<T, Errno>
720where
721    T: 'static,
722    F: FnOnce(crate::net::socket::InodeSocket, Fd) -> Result<T, Errno>,
723{
724    let env = ctx.data();
725    let tasks = env.tasks().clone();
726
727    let fd_entry = env.state.fs.get_fd(sock)?;
728    if !rights.is_empty() && !fd_entry.inner.rights.contains(rights) {
729        return Err(Errno::Access);
730    }
731
732    let inode = fd_entry.inode.clone();
733    let mut guard = inode.write();
734    match guard.deref_mut() {
735        Kind::Socket { socket } => {
736            // Clone the socket and release the lock
737            let socket = socket.clone();
738            drop(guard);
739
740            // Start the work using the socket
741            actor(socket, fd_entry)
742        }
743        _ => Err(Errno::Notsock),
744    }
745}
746
747/// Replaces a socket with another socket in under an asynchronous runtime.
748/// This is used for opening sockets or connecting sockets which changes
749/// the fundamental state of the socket to another state machine
750pub(crate) fn __sock_upgrade<'a, F, Fut>(
751    ctx: &'a mut FunctionEnvMut<'_, WasiEnv>,
752    sock: WasiFd,
753    rights: Rights,
754    actor: F,
755) -> Result<(), Errno>
756where
757    F: FnOnce(crate::net::socket::InodeSocket, Fdflags) -> Fut,
758    Fut: std::future::Future<Output = Result<Option<crate::net::socket::InodeSocket>, Errno>> + 'a,
759{
760    let env = ctx.data();
761    let fd_entry = env.state.fs.get_fd(sock)?;
762    if !rights.is_empty() && !fd_entry.inner.rights.contains(rights) {
763        tracing::warn!(
764            "wasi[{}:{}]::sock_upgrade(fd={}, rights={:?}) - failed - no access rights to upgrade",
765            ctx.data().pid(),
766            ctx.data().tid(),
767            sock,
768            rights
769        );
770        return Err(Errno::Access);
771    }
772
773    let tasks = env.tasks().clone();
774    {
775        let inode = fd_entry.inode;
776        let mut guard = inode.write();
777        match guard.deref_mut() {
778            Kind::Socket { socket } => {
779                let socket = socket.clone();
780                drop(guard);
781
782                // Start the work using the socket
783                let work = actor(socket, fd_entry.inner.flags);
784
785                // Block on the work and process it
786                let res = block_on(work);
787                let new_socket = res?;
788
789                if let Some(mut new_socket) = new_socket {
790                    let mut guard = inode.write();
791                    match guard.deref_mut() {
792                        Kind::Socket { socket, .. } => {
793                            std::mem::swap(socket, &mut new_socket);
794                        }
795                        _ => {
796                            tracing::warn!(
797                                "wasi[{}:{}]::sock_upgrade(fd={}, rights={:?}) - failed - not a socket",
798                                ctx.data().pid(),
799                                ctx.data().tid(),
800                                sock,
801                                rights
802                            );
803                            return Err(Errno::Notsock);
804                        }
805                    }
806                }
807            }
808            _ => {
809                tracing::warn!(
810                    "wasi[{}:{}]::sock_upgrade(fd={}, rights={:?}) - failed - not a socket",
811                    ctx.data().pid(),
812                    ctx.data().tid(),
813                    sock,
814                    rights
815                );
816                return Err(Errno::Notsock);
817            }
818        }
819    }
820
821    Ok(())
822}
823
824#[must_use]
825pub(crate) fn write_buffer_array<M: MemorySize>(
826    memory: &MemoryView,
827    from: &[Vec<u8>],
828    ptr_buffer: WasmPtr<WasmPtr<u8, M>, M>,
829    buffer: WasmPtr<u8, M>,
830) -> Errno {
831    let ptrs = wasi_try_mem!(ptr_buffer.slice(memory, wasi_try!(to_offset::<M>(from.len()))));
832
833    let mut current_buffer_offset = 0usize;
834    for ((i, sub_buffer), ptr) in from.iter().enumerate().zip(ptrs.iter()) {
835        let mut buf_offset = buffer.offset();
836        buf_offset += wasi_try!(to_offset::<M>(current_buffer_offset));
837        let new_ptr = WasmPtr::new(buf_offset);
838        wasi_try_mem!(ptr.write(new_ptr));
839
840        let data =
841            wasi_try_mem!(new_ptr.slice(memory, wasi_try!(to_offset::<M>(sub_buffer.len()))));
842        wasi_try_mem!(data.write_slice(sub_buffer));
843        wasi_try_mem!(
844            wasi_try_mem!(new_ptr.add_offset(wasi_try!(to_offset::<M>(sub_buffer.len()))))
845                .write(memory, 0)
846        );
847
848        current_buffer_offset += sub_buffer.len() + 1;
849    }
850
851    Errno::Success
852}
853
854pub(crate) fn get_current_time_in_nanos() -> Result<Timestamp, Errno> {
855    let now = platform_clock_time_get(Snapshot0Clockid::Monotonic, 1_000_000).unwrap() as u128;
856    Ok(now as Timestamp)
857}
858
859pub(crate) fn get_stack_lower(env: &WasiEnv) -> u64 {
860    env.layout.stack_lower
861}
862
863pub(crate) fn get_stack_upper(env: &WasiEnv) -> u64 {
864    env.layout.stack_upper
865}
866
867pub(crate) unsafe fn get_memory_stack_pointer(
868    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
869) -> Result<u64, String> {
870    // Get the current value of the stack pointer (which we will use
871    // to save all of the stack)
872    let stack_upper = get_stack_upper(ctx.data());
873    let stack_pointer = if let Some(stack_pointer) = ctx
874        .data()
875        .inner()
876        .main_module_instance_handles()
877        .stack_pointer
878        .clone()
879    {
880        match stack_pointer.get(ctx) {
881            Value::I32(a) => a as u64,
882            Value::I64(a) => a as u64,
883            _ => stack_upper,
884        }
885    } else {
886        return Err("failed to save stack: no __stack_pointer global".to_string());
887    };
888    Ok(stack_pointer)
889}
890
891pub(crate) unsafe fn get_memory_stack_offset(
892    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
893) -> Result<u64, String> {
894    let stack_upper = get_stack_upper(ctx.data());
895    let stack_pointer = unsafe { get_memory_stack_pointer(ctx) }?;
896    Ok(stack_upper - stack_pointer)
897}
898
899pub(crate) fn set_memory_stack_offset(
900    env: &WasiEnv,
901    store: &mut impl AsStoreMut,
902    offset: u64,
903) -> Result<(), String> {
904    // Sets the stack pointer
905    let stack_upper = get_stack_upper(env);
906    let stack_pointer = stack_upper - offset;
907    if let Some(stack_pointer_ptr) = env
908        .inner()
909        .main_module_instance_handles()
910        .stack_pointer
911        .clone()
912    {
913        match stack_pointer_ptr.get(store) {
914            Value::I32(_) => {
915                stack_pointer_ptr.set(store, Value::I32(stack_pointer as i32));
916            }
917            Value::I64(_) => {
918                stack_pointer_ptr.set(store, Value::I64(stack_pointer as i64));
919            }
920            _ => {
921                return Err(
922                    "failed to save stack: __stack_pointer global is of an unknown type"
923                        .to_string(),
924                );
925            }
926        }
927    } else {
928        return Err("failed to save stack: no __stack_pointer global".to_string());
929    }
930    Ok(())
931}
932
933#[allow(dead_code)]
934pub(crate) fn get_memory_stack<M: MemorySize>(
935    env: &WasiEnv,
936    store: &mut impl AsStoreMut,
937) -> Result<BytesMut, String> {
938    // Get the current value of the stack pointer (which we will use
939    // to save all of the stack)
940    let stack_base = get_stack_upper(env);
941    let stack_pointer = if let Some(stack_pointer) = env
942        .inner()
943        .main_module_instance_handles()
944        .stack_pointer
945        .clone()
946    {
947        match stack_pointer.get(store) {
948            Value::I32(a) => a as u64,
949            Value::I64(a) => a as u64,
950            _ => stack_base,
951        }
952    } else {
953        return Err("failed to save stack: no __stack_pointer global".to_string());
954    };
955    let memory = env
956        .try_memory_view(store)
957        .ok_or_else(|| "unable to access the memory of the instance".to_string())?;
958    let stack_offset = env.layout.stack_upper - stack_pointer;
959
960    // Read the memory stack into a vector
961    let memory_stack_ptr = WasmPtr::<u8, M>::new(
962        stack_pointer
963            .try_into()
964            .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))?,
965    );
966
967    memory_stack_ptr
968        .slice(
969            &memory,
970            stack_offset
971                .try_into()
972                .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))?,
973        )
974        .and_then(|memory_stack| memory_stack.read_to_bytes())
975        .map_err(|err| format!("failed to read stack: {err}"))
976}
977
978#[allow(dead_code)]
979pub(crate) fn set_memory_stack<M: MemorySize>(
980    env: &WasiEnv,
981    store: &mut impl AsStoreMut,
982    stack: Bytes,
983) -> Result<(), String> {
984    // First we restore the memory stack
985    let stack_upper = get_stack_upper(env);
986    let stack_offset = stack.len() as u64;
987    let stack_pointer = stack_upper - stack_offset;
988    let stack_ptr = WasmPtr::<u8, M>::new(
989        stack_pointer
990            .try_into()
991            .map_err(|_| "failed to restore stack: stack pointer overflow".to_string())?,
992    );
993
994    let memory = env
995        .try_memory_view(store)
996        .ok_or_else(|| "unable to set the stack pointer of the instance".to_string())?;
997    stack_ptr
998        .slice(
999            &memory,
1000            stack_offset
1001                .try_into()
1002                .map_err(|_| "failed to restore stack: stack pointer overflow".to_string())?,
1003        )
1004        .and_then(|memory_stack| memory_stack.write_slice(&stack[..]))
1005        .map_err(|err| format!("failed to write stack: {err}"))?;
1006
1007    // Set the stack pointer itself and return
1008    set_memory_stack_offset(env, store, stack_offset)?;
1009    Ok(())
1010}
1011
1012/// Puts the process to deep sleep and wakes it again when
1013/// the supplied future completes
1014#[must_use = "you must return the result immediately so the stack can unwind"]
1015pub(crate) fn deep_sleep<M: MemorySize>(
1016    mut ctx: FunctionEnvMut<'_, WasiEnv>,
1017    trigger: Pin<Box<AsyncifyFuture>>,
1018) -> Result<(), WasiError> {
1019    // Grab all the globals and serialize them
1020    let store_data = crate::utils::store::capture_store_snapshot(&mut ctx.as_store_mut())
1021        .serialize()
1022        .unwrap();
1023    let store_data = Bytes::from(store_data);
1024    let thread_start = ctx.data().thread.thread_start_type();
1025
1026    // Perform the unwind action
1027    let tasks = ctx.data().tasks().clone();
1028    let res = unwind::<M, _>(ctx, move |mut ctx, memory_stack, rewind_stack| {
1029        let memory_stack = memory_stack.freeze();
1030        let rewind_stack = rewind_stack.freeze();
1031        let thread_layout = ctx.data().thread.memory_layout().clone();
1032
1033        // If journal'ing is enabled then we dump the stack into the journal
1034        if ctx.data().enable_journal {
1035            // Grab all the globals and serialize them
1036            let store_data = crate::utils::store::capture_store_snapshot(&mut ctx.as_store_mut())
1037                .serialize()
1038                .unwrap();
1039            let store_data = Bytes::from(store_data);
1040
1041            tracing::trace!(
1042                "stack snapshot unwind (memory_stack={}, rewind_stack={}, store_data={})",
1043                memory_stack.len(),
1044                rewind_stack.len(),
1045                store_data.len(),
1046            );
1047
1048            #[cfg(feature = "journal")]
1049            {
1050                // Write our thread state to the snapshot
1051                let tid = ctx.data().thread.tid();
1052                let thread_start = ctx.data().thread.thread_start_type();
1053                if let Err(err) = JournalEffector::save_thread_state::<M>(
1054                    &mut ctx,
1055                    tid,
1056                    memory_stack.clone(),
1057                    rewind_stack.clone(),
1058                    store_data.clone(),
1059                    thread_start,
1060                    thread_layout.clone(),
1061                ) {
1062                    return wasmer_types::OnCalledAction::Trap(err.into());
1063                }
1064            }
1065
1066            // If all the threads are now in a deep sleep state
1067            // then we can trigger the idle snapshot event
1068            let inner = ctx.data().process.inner.clone();
1069            let is_idle = {
1070                let mut guard = inner.0.lock().unwrap();
1071                guard.threads.values().all(WasiThread::is_deep_sleeping)
1072            };
1073
1074            // When we idle the journal functionality may be set
1075            // will take a snapshot of the memory and threads so
1076            // that it can resumed.
1077            #[cfg(feature = "journal")]
1078            {
1079                if is_idle && ctx.data_mut().has_snapshot_trigger(SnapshotTrigger::Idle) {
1080                    let mut guard = inner.0.lock().unwrap();
1081                    if let Err(err) = JournalEffector::save_memory_and_snapshot(
1082                        &mut ctx,
1083                        &mut guard,
1084                        SnapshotTrigger::Idle,
1085                    ) {
1086                        return wasmer_types::OnCalledAction::Trap(err.into());
1087                    }
1088                }
1089            }
1090        }
1091
1092        // Schedule the process on the stack so that it can be resumed
1093        OnCalledAction::Trap(Box::new(WasiError::DeepSleep(DeepSleepWork {
1094            trigger,
1095            rewind: RewindState {
1096                memory_stack,
1097                rewind_stack,
1098                store_data,
1099                start: thread_start,
1100                layout: thread_layout,
1101                is_64bit: M::is_64bit(),
1102            },
1103        })))
1104    })?;
1105
1106    // If there is an error then exit the process, otherwise we are done
1107    match res {
1108        Errno::Success => Ok(()),
1109        err => Err(WasiError::Exit(ExitCode::from(err))),
1110    }
1111}
1112
1113#[must_use = "you must return the result immediately so the stack can unwind"]
1114pub fn unwind<M: MemorySize, F>(
1115    mut ctx: FunctionEnvMut<'_, WasiEnv>,
1116    callback: F,
1117) -> Result<Errno, WasiError>
1118where
1119    F: FnOnce(FunctionEnvMut<'_, WasiEnv>, BytesMut, BytesMut) -> OnCalledAction
1120        + Send
1121        + Sync
1122        + 'static,
1123{
1124    // Get the current stack pointer (this will be used to determine the
1125    // upper limit of stack space remaining to unwind into)
1126    let (env, mut store) = ctx.data_and_store_mut();
1127    let memory_stack = match get_memory_stack::<M>(env, &mut store) {
1128        Ok(a) => a,
1129        Err(err) => {
1130            warn!("unable to get the memory stack - {}", err);
1131            return Err(WasiError::Exit(Errno::Unknown.into()));
1132        }
1133    };
1134
1135    // Perform a check to see if we have enough room
1136    let env = ctx.data();
1137    let memory = unsafe { env.memory_view(&ctx) };
1138
1139    // Write the addresses to the start of the stack space
1140    let unwind_pointer = env.layout.stack_lower;
1141    let unwind_data_start =
1142        unwind_pointer + (std::mem::size_of::<__wasi_asyncify_t<M::Offset>>() as u64);
1143    let unwind_data = __wasi_asyncify_t::<M::Offset> {
1144        start: wasi_try_ok!(unwind_data_start.try_into().map_err(|_| Errno::Overflow)),
1145        end: wasi_try_ok!(
1146            (env.layout.stack_upper - memory_stack.len() as u64)
1147                .try_into()
1148                .map_err(|_| Errno::Overflow)
1149        ),
1150    };
1151    let unwind_data_ptr: WasmPtr<__wasi_asyncify_t<M::Offset>, M> = WasmPtr::new(wasi_try_ok!(
1152        unwind_pointer.try_into().map_err(|_| Errno::Overflow)
1153    ));
1154    wasi_try_mem_ok!(unwind_data_ptr.write(&memory, unwind_data));
1155
1156    // Invoke the callback that will prepare to unwind
1157    // We need to start unwinding the stack
1158    let asyncify_data = wasi_try_ok!(unwind_pointer.try_into().map_err(|_| Errno::Overflow));
1159    if let Some(asyncify_start_unwind) = env
1160        .inner()
1161        .static_module_instance_handles()
1162        .and_then(|handles| handles.asyncify_start_unwind.clone())
1163    {
1164        asyncify_start_unwind.call(&mut ctx, asyncify_data);
1165    } else {
1166        warn!("failed to unwind the stack because the asyncify_start_rewind export is missing");
1167        return Err(WasiError::Exit(Errno::Noexec.into()));
1168    }
1169
1170    // Set callback that will be invoked when this process finishes
1171    let env = ctx.data();
1172    let unwind_stack_begin: u64 = unwind_data.start.into();
1173    let total_stack_space = env.layout.stack_size;
1174    let func = ctx.as_ref();
1175    trace!(
1176        stack_upper = env.layout.stack_upper,
1177        stack_lower = env.layout.stack_lower,
1178        "wasi[{}:{}]::unwinding (used_stack_space={} total_stack_space={})",
1179        ctx.data().pid(),
1180        ctx.data().tid(),
1181        memory_stack.len(),
1182        total_stack_space
1183    );
1184    ctx.as_store_mut().on_called(move |mut store| {
1185        let mut ctx = func.into_mut(&mut store);
1186        let env = ctx.data();
1187        let memory = env
1188            .try_memory_view(&ctx)
1189            .ok_or_else(|| "failed to save stack: stack pointer overflow - unable to access the memory of the instance".to_string())?;
1190
1191        let unwind_data_ptr: WasmPtr<__wasi_asyncify_t<M::Offset>, M> = WasmPtr::new(
1192            unwind_pointer
1193                .try_into()
1194                .map_err(|_| Errno::Overflow)
1195                .unwrap(),
1196        );
1197        let unwind_data_result = unwind_data_ptr.read(&memory).unwrap();
1198        let unwind_stack_finish: u64 = unwind_data_result.start.into();
1199        let unwind_size = unwind_stack_finish - unwind_stack_begin;
1200        trace!(
1201            "wasi[{}:{}]::unwound (memory_stack_size={} unwind_size={})",
1202            ctx.data().pid(),
1203            ctx.data().tid(),
1204            memory_stack.len(),
1205            unwind_size
1206        );
1207
1208        // Read the memory stack into a vector
1209        let unwind_stack_ptr = WasmPtr::<u8, M>::new(
1210            unwind_stack_begin
1211                .try_into()
1212                .map_err(|_| "failed to save stack: stack pointer overflow".to_string())?,
1213        );
1214        let unwind_stack = unwind_stack_ptr
1215            .slice(
1216                &memory,
1217                unwind_size
1218                    .try_into()
1219                    .map_err(|_| "failed to save stack: stack pointer overflow".to_string())?,
1220            )
1221            .and_then(|memory_stack| memory_stack.read_to_bytes())
1222            .map_err(|err| format!("failed to read stack: {err}"))?;
1223
1224        // Notify asyncify that we are no longer unwinding
1225        if let Some(asyncify_stop_unwind) = env
1226            .inner()
1227            .static_module_instance_handles()
1228            .and_then(|i| i.asyncify_stop_unwind.clone())
1229        {
1230            asyncify_stop_unwind.call(&mut ctx);
1231        } else {
1232            warn!("failed to unwind the stack because the asyncify_start_rewind export is missing");
1233            return Ok(OnCalledAction::Finish);
1234        }
1235
1236        Ok(callback(ctx, memory_stack, unwind_stack))
1237    });
1238
1239    // We need to exit the function so that it can unwind and then invoke the callback
1240    Ok(Errno::Success)
1241}
1242
1243// NOTE: not tracing-instrumented because [`rewind_ext`] already is.
1244#[must_use = "the action must be passed to the call loop"]
1245pub fn rewind<M: MemorySize, T>(
1246    mut ctx: FunctionEnvMut<WasiEnv>,
1247    memory_stack: Option<Bytes>,
1248    rewind_stack: Bytes,
1249    store_data: Bytes,
1250    result: T,
1251) -> Errno
1252where
1253    T: serde::Serialize,
1254{
1255    let rewind_result = bincode::serde::encode_to_vec(&result, config::legacy())
1256        .unwrap()
1257        .into();
1258    rewind_ext::<M>(
1259        &mut ctx,
1260        memory_stack,
1261        rewind_stack,
1262        store_data,
1263        RewindResultType::RewindWithResult(rewind_result),
1264    )
1265}
1266
1267#[instrument(level = "trace", skip_all, fields(rewind_stack_len = rewind_stack.len(), store_data_len = store_data.len()))]
1268#[must_use = "the action must be passed to the call loop"]
1269pub fn rewind_ext<M: MemorySize>(
1270    ctx: &mut FunctionEnvMut<WasiEnv>,
1271    memory_stack: Option<Bytes>,
1272    rewind_stack: Bytes,
1273    store_data: Bytes,
1274    rewind_result: RewindResultType,
1275) -> Errno {
1276    // Store the memory stack so that it can be restored later
1277    ctx.data_mut().thread.set_rewind(RewindResult {
1278        memory_stack,
1279        rewind_result,
1280    });
1281
1282    // Deserialize the store data back into a snapshot
1283    let store_snapshot = match StoreSnapshot::deserialize(&store_data[..]) {
1284        Ok(a) => a,
1285        Err(err) => {
1286            warn!("snapshot restore failed - the store snapshot could not be deserialized");
1287            return Errno::Unknown;
1288        }
1289    };
1290    crate::utils::store::restore_store_snapshot(ctx, &store_snapshot);
1291    let env = ctx.data();
1292    let memory = match env.try_memory_view(&ctx) {
1293        Some(v) => v,
1294        None => {
1295            warn!("snapshot restore failed - unable to access the memory of the instance");
1296            return Errno::Unknown;
1297        }
1298    };
1299
1300    // Write the addresses to the start of the stack space
1301    let rewind_pointer = env.layout.stack_lower;
1302    let rewind_data_start =
1303        rewind_pointer + (std::mem::size_of::<__wasi_asyncify_t<M::Offset>>() as u64);
1304    let rewind_data_end = rewind_data_start + (rewind_stack.len() as u64);
1305    if rewind_data_end > env.layout.stack_upper {
1306        warn!(
1307            "attempting to rewind a stack bigger than the allocated stack space ({} > {})",
1308            rewind_data_end, env.layout.stack_upper
1309        );
1310        return Errno::Overflow;
1311    }
1312    let rewind_data = __wasi_asyncify_t::<M::Offset> {
1313        start: wasi_try!(rewind_data_end.try_into().map_err(|_| Errno::Overflow)),
1314        end: wasi_try!(
1315            env.layout
1316                .stack_upper
1317                .try_into()
1318                .map_err(|_| Errno::Overflow)
1319        ),
1320    };
1321    let rewind_data_ptr: WasmPtr<__wasi_asyncify_t<M::Offset>, M> = WasmPtr::new(wasi_try!(
1322        rewind_pointer.try_into().map_err(|_| Errno::Overflow)
1323    ));
1324    wasi_try_mem!(rewind_data_ptr.write(&memory, rewind_data));
1325
1326    // Copy the data to the address
1327    let rewind_stack_ptr = WasmPtr::<u8, M>::new(wasi_try!(
1328        rewind_data_start.try_into().map_err(|_| Errno::Overflow)
1329    ));
1330    wasi_try_mem!(
1331        rewind_stack_ptr
1332            .slice(
1333                &memory,
1334                wasi_try!(rewind_stack.len().try_into().map_err(|_| Errno::Overflow))
1335            )
1336            .and_then(|stack| { stack.write_slice(&rewind_stack[..]) })
1337    );
1338
1339    // Invoke the callback that will prepare to rewind
1340    let asyncify_data = wasi_try!(rewind_pointer.try_into().map_err(|_| Errno::Overflow));
1341    if let Some(asyncify_start_rewind) = env
1342        .inner()
1343        .static_module_instance_handles()
1344        .and_then(|a| a.asyncify_start_rewind.clone())
1345    {
1346        asyncify_start_rewind.call(ctx, asyncify_data);
1347    } else {
1348        warn!(
1349            "failed to rewind the stack because the asyncify_start_rewind export is missing or inaccessible"
1350        );
1351        return Errno::Noexec;
1352    }
1353
1354    Errno::Success
1355}
1356
1357pub fn rewind_ext2(
1358    ctx: &mut FunctionEnvMut<WasiEnv>,
1359    rewind_state: RewindStateOption,
1360) -> Result<(), ExitCode> {
1361    if let Some((rewind_state, rewind_result)) = rewind_state {
1362        tracing::trace!("Rewinding");
1363        let errno = if rewind_state.is_64bit {
1364            crate::rewind_ext::<wasmer_types::Memory64>(
1365                ctx,
1366                Some(rewind_state.memory_stack),
1367                rewind_state.rewind_stack,
1368                rewind_state.store_data,
1369                rewind_result,
1370            )
1371        } else {
1372            crate::rewind_ext::<wasmer_types::Memory32>(
1373                ctx,
1374                Some(rewind_state.memory_stack),
1375                rewind_state.rewind_stack,
1376                rewind_state.store_data,
1377                rewind_result,
1378            )
1379        };
1380
1381        if errno != Errno::Success {
1382            let exit_code = ExitCode::from(errno);
1383            ctx.data().blocking_on_exit(Some(exit_code));
1384            return Err(exit_code);
1385        }
1386    }
1387
1388    Ok(())
1389}
1390
1391pub fn anyhow_err_to_runtime_err(err: anyhow::Error) -> WasiRuntimeError {
1392    WasiRuntimeError::Runtime(RuntimeError::user(err.into()))
1393}
1394
1395pub(crate) unsafe fn handle_rewind<M: MemorySize, T>(
1396    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
1397) -> Option<T>
1398where
1399    T: serde::de::DeserializeOwned,
1400{
1401    unsafe { handle_rewind_ext::<M, T>(ctx, HandleRewindType::ResultDriven) }.flatten()
1402}
1403
1404pub(crate) enum HandleRewindType {
1405    /// Handle rewind types that have a result to be processed
1406    ResultDriven,
1407    /// Handle rewind types that are result-less (generally these
1408    /// are caused by snapshot events)
1409    ResultLess,
1410}
1411
1412pub(crate) unsafe fn handle_rewind_ext_with_default<M: MemorySize, T>(
1413    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
1414    type_: HandleRewindType,
1415) -> Option<T>
1416where
1417    T: serde::de::DeserializeOwned + Default,
1418{
1419    let ret = unsafe { handle_rewind_ext::<M, T>(ctx, type_) };
1420    ret.unwrap_or_default()
1421}
1422
1423pub(crate) unsafe fn handle_rewind_ext<M: MemorySize, T>(
1424    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
1425    type_: HandleRewindType,
1426) -> Option<Option<T>>
1427where
1428    T: serde::de::DeserializeOwned,
1429{
1430    let env = ctx.data();
1431    if !env.thread.has_rewind_of_type(type_) {
1432        return None;
1433    };
1434
1435    // If the stack has been restored
1436    let tid = env.tid();
1437    let pid = env.pid();
1438    if let Some(result) = ctx.data_mut().thread.take_rewind() {
1439        // Deserialize the result
1440        let memory_stack = result.memory_stack;
1441
1442        // Notify asyncify that we are no longer rewinding
1443        let env = ctx.data();
1444        if let Some(asyncify_stop_rewind) = env
1445            .inner()
1446            .static_module_instance_handles()
1447            .and_then(|handles| handles.asyncify_stop_unwind.clone())
1448        {
1449            asyncify_stop_rewind.call(ctx);
1450        } else {
1451            warn!(
1452                "failed to handle rewind because the asyncify_start_rewind export is missing or inaccessible"
1453            );
1454            return Some(None);
1455        }
1456
1457        // Restore the memory stack
1458        let (env, mut store) = ctx.data_and_store_mut();
1459        if let Some(memory_stack) = memory_stack {
1460            set_memory_stack::<M>(env, &mut store, memory_stack);
1461        }
1462
1463        match result.rewind_result {
1464            RewindResultType::RewindRestart => {
1465                tracing::trace!(%pid, %tid, "rewind for syscall restart");
1466                None
1467            }
1468            RewindResultType::RewindWithoutResult => {
1469                tracing::trace!(%pid, %tid, "rewind with no result");
1470                Some(None)
1471            }
1472            RewindResultType::RewindWithResult(rewind_result) => {
1473                tracing::trace!(%pid, %tid, "rewind with result (data={})", rewind_result.len());
1474                let (ret, _) = bincode::serde::decode_from_slice(&rewind_result, config::legacy())
1475                    .expect("failed to deserialize the rewind result");
1476                Some(Some(ret))
1477            }
1478        }
1479    } else {
1480        tracing::trace!(%pid, %tid, "rewind miss");
1481        Some(None)
1482    }
1483}
1484
1485// Function to prepare the WASI environment
1486pub(crate) fn _prepare_wasi(
1487    wasi_env: &mut WasiEnv,
1488    args: Option<Vec<String>>,
1489    envs: Option<Vec<(String, String)>>,
1490    signals: Option<Vec<SignalDisposition>>,
1491) {
1492    // Swap out the arguments with the new ones
1493    if let Some(args) = args {
1494        *wasi_env.state.args.lock().unwrap() = args;
1495    }
1496
1497    // Update the env vars
1498    if let Some(envs) = envs {
1499        let mut guard = wasi_env.state.envs.lock().unwrap();
1500
1501        let mut existing_envs = guard
1502            .iter()
1503            .map(|b| {
1504                let string = String::from_utf8_lossy(b);
1505                let (key, val) = string.split_once('=').expect("env var is malformed");
1506
1507                (key.to_string(), val.to_string().as_bytes().to_vec())
1508            })
1509            .collect::<Vec<_>>();
1510
1511        for (key, val) in envs {
1512            let val = val.as_bytes().to_vec();
1513            match existing_envs
1514                .iter_mut()
1515                .find(|(existing_key, _)| existing_key == &key)
1516            {
1517                Some((_, existing_val)) => *existing_val = val,
1518                None => existing_envs.push((key, val)),
1519            }
1520        }
1521
1522        let envs = conv_env_vars(existing_envs);
1523
1524        *guard = envs;
1525
1526        drop(guard)
1527    }
1528
1529    if let Some(signals) = signals {
1530        let mut guard = wasi_env.state.signals.lock().unwrap();
1531        for signal in signals {
1532            guard.insert(signal.sig, signal.disp);
1533        }
1534        drop(guard);
1535    }
1536}
1537
1538pub(crate) fn conv_spawn_err_to_errno(err: &SpawnError) -> Errno {
1539    match err {
1540        SpawnError::AccessDenied => Errno::Access,
1541        SpawnError::Unsupported => Errno::Noexec,
1542        _ if err.is_not_found() => Errno::Noent,
1543        _ => Errno::Inval,
1544    }
1545}
1546
1547pub(crate) fn conv_spawn_err_to_exit_code(err: &SpawnError) -> ExitCode {
1548    conv_spawn_err_to_errno(err).into()
1549}