wasmer_wasix/
lib.rs

1// FIXME: merge with ./lib.rs_upstream
2
3#![allow(clippy::result_large_err)]
4#![doc(html_favicon_url = "https://wasmer.io/images/icons/favicon-32x32.png")]
5#![doc(html_logo_url = "https://github.com/wasmerio.png?size=200")]
6#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
7
8//! Wasmer's WASI implementation
9//!
10//! Use `generate_import_object` to create an [`Imports`].  This [`Imports`]
11//! can be combined with a module to create an `Instance` which can execute WASI
12//! Wasm functions.
13//!
14//! See `state` for the experimental WASI FS API.  Also see the
15//! [WASI plugin example](https://github.com/wasmerio/wasmer/blob/main/examples/plugin.rs)
16//! for an example of how to extend WASI using the WASI FS API.
17
18#[cfg(all(
19    not(feature = "sys"),
20    not(feature = "js"),
21    not(feature = "sys-minimal")
22))]
23compile_error!(
24    "At least the `sys` or the `js` or `sys-minimal` feature must be enabled. Please, pick one."
25);
26
27#[cfg(any(
28    all(feature = "js", feature = "sys"),
29    all(feature = "js", feature = "sys-minimal")
30))]
31compile_error!(
32    "Cannot have both `sys` and `js` or `sys-minimal` and `sys` features enabled at the same time. Please, pick one."
33);
34
35#[cfg(all(feature = "sys", target_arch = "wasm32"))]
36compile_error!("The `sys` feature must be enabled only for non-`wasm32` target.");
37
38#[cfg(all(feature = "js", not(target_arch = "wasm32")))]
39compile_error!(
40    "The `js` feature must be enabled only for the `wasm32` target (either `wasm32-unknown-unknown` or `wasm32-wasip1`)."
41);
42
43#[cfg(all(test, target_arch = "wasm32"))]
44wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
45
46#[cfg(test)]
47#[macro_use]
48extern crate pretty_assertions;
49
50#[macro_use]
51mod macros;
52pub mod bin_factory;
53pub mod os;
54// TODO: should this be pub?
55pub mod net;
56// TODO: should this be pub?
57pub mod capabilities;
58pub mod fs;
59pub mod http;
60pub mod journal;
61mod rewind;
62pub mod runners;
63pub mod runtime;
64mod state;
65mod syscalls;
66mod utils;
67
68use std::sync::Arc;
69
70#[allow(unused_imports)]
71use bytes::{Bytes, BytesMut};
72use os::task::control_plane::ControlPlaneError;
73use thiserror::Error;
74// re-exports needed for OS
75pub use wasmer;
76pub use wasmer_wasix_types;
77
78use wasmer::{
79    AsStoreMut, Exports, FunctionEnv, Imports, Memory32, MemoryAccessError, MemorySize,
80    RuntimeError, imports, namespace,
81};
82
83pub use virtual_fs;
84pub use virtual_fs::{DuplexPipe, FsError, Pipe, VirtualFile, WasiBidirectionalSharedPipePair};
85pub use virtual_net;
86pub use virtual_net::{UnsupportedVirtualNetworking, VirtualNetworking};
87
88#[cfg(feature = "host-vnet")]
89pub use virtual_net::{
90    host::{LocalNetworking, LocalTcpListener, LocalTcpStream, LocalUdpSocket},
91    io_err_into_net_error,
92};
93use wasmer_wasix_types::wasi::{Errno, ExitCode};
94
95pub use crate::{
96    fs::{Fd, VIRTUAL_ROOT_FD, WasiFs, WasiInodes, default_fs_backing},
97    os::{
98        WasiTtyState,
99        task::{
100            control_plane::WasiControlPlane,
101            process::{WasiProcess, WasiProcessId},
102            thread::{WasiThread, WasiThreadError, WasiThreadHandle, WasiThreadId},
103        },
104    },
105    rewind::*,
106    runtime::{PluggableRuntime, Runtime, task_manager::VirtualTaskManager},
107    state::{
108        ALL_RIGHTS, WasiEnv, WasiEnvBuilder, WasiEnvInit, WasiFunctionEnv,
109        WasiModuleInstanceHandles, WasiModuleTreeHandles, WasiStateCreationError,
110    },
111    syscalls::{journal::wait_for_snapshot, rewind, rewind_ext, types, unwind},
112    utils::is_wasix_module,
113    utils::{
114        WasiVersion, get_wasi_version, get_wasi_versions, is_wasi_module,
115        store::{StoreSnapshot, capture_store_snapshot, restore_store_snapshot},
116    },
117};
118
119/// This is returned in `RuntimeError`.
120/// Use `downcast` or `downcast_ref` to retrieve the `ExitCode`.
121#[derive(Error, Debug)]
122pub enum WasiError {
123    #[error("WASI exited with code: {0}")]
124    Exit(ExitCode),
125    #[error("WASI thread exited")]
126    ThreadExit,
127    #[error("WASI deep sleep: {0:?}")]
128    DeepSleep(DeepSleepWork),
129    #[error("The WASI version could not be determined")]
130    UnknownWasiVersion,
131    #[error("Dynamically-linked symbol not found or has bad type: {0}")]
132    DlSymbolResolutionFailed(String),
133}
134
135pub type WasiResult<T> = Result<Result<T, Errno>, WasiError>;
136
137#[deny(unused, dead_code)]
138#[derive(Error, Debug)]
139pub enum SpawnError {
140    /// Failed during serialization
141    #[error("serialization failed")]
142    Serialization,
143    /// Failed during deserialization
144    #[error("deserialization failed")]
145    Deserialization,
146    /// Invalid Wasmer process
147    #[error("invalid wasmer")]
148    InvalidWasmer,
149    /// Failed to fetch the Wasmer process
150    #[error("fetch failed")]
151    FetchFailed,
152    #[error(transparent)]
153    CacheError(crate::runtime::module_cache::CacheError),
154    /// Failed to compile the Wasmer process
155    #[error("compile error: {error:?}")]
156    CompileError {
157        module_hash: wasmer_types::ModuleHash,
158        error: wasmer::CompileError,
159    },
160    /// Invalid ABI
161    #[error("Wasmer process has an invalid ABI")]
162    InvalidABI,
163    /// Bad handle
164    #[error("bad handle")]
165    BadHandle,
166    /// Call is unsupported
167    #[error("unsupported")]
168    Unsupported,
169    /// Not found
170    #[error("not found: {message}")]
171    NotFound { message: String },
172    /// Tried to run the specified binary as a new WASI thread/process, but
173    /// the binary name was not found.
174    #[error("could not find binary '{binary}'")]
175    BinaryNotFound { binary: String },
176    #[error("could not find an entrypoint in the package '{package_id}'")]
177    MissingEntrypoint {
178        package_id: wasmer_config::package::PackageId,
179    },
180    #[error("could not load ")]
181    ModuleLoad { message: String },
182    /// Bad request
183    #[error("bad request")]
184    BadRequest,
185    /// Access denied
186    #[error("access denied")]
187    AccessDenied,
188    /// Internal error has occurred
189    #[error("internal error")]
190    InternalError,
191    /// An error occurred while preparing the file system
192    #[error(transparent)]
193    FileSystemError(ExtendedFsError),
194    /// Memory allocation failed
195    #[error("memory allocation failed")]
196    MemoryAllocationFailed,
197    /// Memory access violation
198    #[error("memory access violation")]
199    MemoryAccessViolation,
200    /// Some other unhandled error. If you see this, it's probably a bug.
201    #[error("unknown error found")]
202    UnknownError,
203    #[error("runtime error")]
204    Runtime(#[from] WasiRuntimeError),
205    #[error(transparent)]
206    Other(#[from] Box<dyn std::error::Error + Send + Sync>),
207}
208
209#[derive(Debug)]
210pub struct ExtendedFsError {
211    pub error: virtual_fs::FsError,
212    pub message: Option<String>,
213}
214
215impl ExtendedFsError {
216    pub fn with_msg(error: virtual_fs::FsError, msg: impl Into<String>) -> Self {
217        Self {
218            error,
219            message: Some(msg.into()),
220        }
221    }
222
223    pub fn new(error: virtual_fs::FsError) -> Self {
224        Self {
225            error,
226            message: None,
227        }
228    }
229}
230
231impl std::fmt::Display for ExtendedFsError {
232    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
233        write!(f, "fs error: {}", self.error)?;
234
235        if let Some(msg) = &self.message {
236            write!(f, " | {msg}")?;
237        }
238
239        Ok(())
240    }
241}
242
243impl std::error::Error for ExtendedFsError {
244    fn cause(&self) -> Option<&dyn std::error::Error> {
245        Some(&self.error)
246    }
247}
248
249impl SpawnError {
250    /// Returns `true` if the spawn error is [`NotFound`].
251    ///
252    /// [`NotFound`]: SpawnError::NotFound
253    #[must_use]
254    pub fn is_not_found(&self) -> bool {
255        matches!(
256            self,
257            Self::NotFound { .. } | Self::MissingEntrypoint { .. } | Self::BinaryNotFound { .. }
258        )
259    }
260}
261
262#[derive(thiserror::Error, Debug)]
263pub enum WasiRuntimeError {
264    #[error("WASI state setup failed: {0}")]
265    Init(#[from] WasiStateCreationError),
266    #[error("Loading exports failed: {0}")]
267    Export(#[from] wasmer::ExportError),
268    #[error("Instantiation failed: {0}")]
269    Instantiation(#[from] wasmer::InstantiationError),
270    #[error("WASI error: {0}")]
271    Wasi(#[from] WasiError),
272    #[error("Process manager error: {0}")]
273    ControlPlane(#[from] ControlPlaneError),
274    #[error("{0}")]
275    Runtime(#[from] RuntimeError),
276    #[error("Memory access error: {0}")]
277    Thread(#[from] WasiThreadError),
278    #[error("{0}")]
279    Anyhow(#[from] Arc<anyhow::Error>),
280}
281
282impl WasiRuntimeError {
283    /// Retrieve the concrete exit code returned by an instance.
284    ///
285    /// Returns [`None`] if a general execution error occurred.
286    pub fn as_exit_code(&self) -> Option<ExitCode> {
287        if let WasiRuntimeError::Wasi(WasiError::Exit(code)) = self {
288            Some(*code)
289        } else if let WasiRuntimeError::Runtime(err) = self {
290            if let Some(WasiError::Exit(code)) = err.downcast_ref() {
291                Some(*code)
292            } else {
293                None
294            }
295        } else {
296            None
297        }
298    }
299
300    pub fn display<'a>(&'a self, store: &'a mut impl AsStoreMut) -> WasiRuntimeErrorDisplay<'a> {
301        if let WasiRuntimeError::Runtime(err) = self {
302            WasiRuntimeErrorDisplay::Runtime(err.display(store))
303        } else {
304            WasiRuntimeErrorDisplay::Other(self)
305        }
306    }
307}
308
309pub enum WasiRuntimeErrorDisplay<'a> {
310    Runtime(wasmer::RuntimeErrorDisplay<'a>),
311    Other(&'a WasiRuntimeError),
312}
313
314impl std::fmt::Display for WasiRuntimeErrorDisplay<'_> {
315    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
316        match self {
317            WasiRuntimeErrorDisplay::Runtime(display) => write!(f, "{display}"),
318            WasiRuntimeErrorDisplay::Other(err) => write!(f, "{err}"),
319        }
320    }
321}
322
323#[allow(clippy::result_large_err)]
324pub(crate) fn run_wasi_func(
325    func: &wasmer::Function,
326    store: &mut impl AsStoreMut,
327    params: &[wasmer::Value],
328) -> Result<Box<[wasmer::Value]>, WasiRuntimeError> {
329    func.call(store, params).map_err(|err| {
330        if let Some(_werr) = err.downcast_ref::<WasiError>() {
331            let werr = err.downcast::<WasiError>().unwrap();
332            WasiRuntimeError::Wasi(werr)
333        } else {
334            WasiRuntimeError::Runtime(err)
335        }
336    })
337}
338
339/// Run a main function.
340///
341/// This is usually called "_start" in WASI modules.
342/// The function will not receive arguments or return values.
343///
344/// An exit code that is not 0 will be returned as a `WasiError::Exit`.
345#[allow(clippy::result_large_err)]
346pub(crate) fn run_wasi_func_start(
347    func: &wasmer::Function,
348    store: &mut impl AsStoreMut,
349) -> Result<(), WasiRuntimeError> {
350    run_wasi_func(func, store, &[])?;
351    Ok(())
352}
353
354#[derive(Debug)]
355pub struct WasiVFork {
356    /// The unwound stack before the vfork occured
357    pub rewind_stack: BytesMut,
358    /// The mutable parts of the store
359    pub store_data: Bytes,
360    /// The environment before the vfork occured
361    pub env: Box<WasiEnv>,
362
363    /// Handle of the thread we have forked (dropping this handle
364    /// will signal that the thread is dead)
365    pub handle: WasiThreadHandle,
366
367    is_64bit: bool,
368}
369
370impl Clone for WasiVFork {
371    fn clone(&self) -> Self {
372        Self {
373            rewind_stack: self.rewind_stack.clone(),
374            store_data: self.store_data.clone(),
375            env: Box::new(self.env.as_ref().clone()),
376            handle: self.handle.clone(),
377            is_64bit: self.is_64bit,
378        }
379    }
380}
381
382/// Create an [`Imports`] with an existing [`WasiEnv`]. [`WasiEnv`] values are
383/// typically constructed with [`WasiEnvBuilder`].
384pub fn generate_import_object_from_env(
385    store: &mut impl AsStoreMut,
386    ctx: &FunctionEnv<WasiEnv>,
387    version: WasiVersion,
388) -> Imports {
389    let mut imports = match version {
390        WasiVersion::Snapshot0 => generate_import_object_snapshot0(store, ctx),
391        WasiVersion::Snapshot1 | WasiVersion::Latest => {
392            generate_import_object_snapshot1(store, ctx)
393        }
394        WasiVersion::Wasix32v1 => generate_import_object_wasix32_v1(store, ctx),
395        WasiVersion::Wasix64v1 => generate_import_object_wasix64_v1(store, ctx),
396    };
397
398    let exports_wasi_generic = wasi_exports_generic(store, ctx);
399
400    let imports_wasi_generic = imports! {
401        "wasi" => exports_wasi_generic,
402    };
403
404    imports.extend(&imports_wasi_generic);
405
406    imports
407}
408
409fn wasi_exports_generic(mut store: &mut impl AsStoreMut, env: &FunctionEnv<WasiEnv>) -> Exports {
410    use syscalls::*;
411    let namespace = namespace! {
412        "thread-spawn" => Function::new_typed_with_env(&mut store, env, thread_spawn::<Memory32>),
413    };
414    namespace
415}
416
417fn wasi_unstable_exports(mut store: &mut impl AsStoreMut, env: &FunctionEnv<WasiEnv>) -> Exports {
418    use syscalls::*;
419    let namespace = namespace! {
420        "args_get" => Function::new_typed_with_env(&mut store, env, args_get::<Memory32>),
421        "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get::<Memory32>),
422        "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get::<Memory32>),
423        "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get::<Memory32>),
424        "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get::<Memory32>),
425        "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get::<Memory32>),
426        "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise),
427        "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate),
428        "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close),
429        "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync),
430        "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get::<Memory32>),
431        "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags),
432        "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights),
433        "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::fd_filestat_get),
434        "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size),
435        "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times),
436        "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread::<Memory32>),
437        "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get::<Memory32>),
438        "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name::<Memory32>),
439        "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite::<Memory32>),
440        "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read::<Memory32>),
441        "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir::<Memory32>),
442        "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber),
443        "fd_seek" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::fd_seek),
444        "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync),
445        "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell::<Memory32>),
446        "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write::<Memory32>),
447        "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory::<Memory32>),
448        "path_filestat_get" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::path_filestat_get),
449        "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times::<Memory32>),
450        "path_link" => Function::new_typed_with_env(&mut store, env, path_link::<Memory32>),
451        "path_open" => Function::new_typed_with_env(&mut store, env, path_open::<Memory32>),
452        "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink::<Memory32>),
453        "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory::<Memory32>),
454        "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename::<Memory32>),
455        "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink::<Memory32>),
456        "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file::<Memory32>),
457        "poll_oneoff" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::poll_oneoff::<Memory32>),
458        "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit::<Memory32>),
459        "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
460        "random_get" => Function::new_typed_with_env(&mut store, env, random_get::<Memory32>),
461        "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield::<Memory32>),
462        "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv::<Memory32>),
463        "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send::<Memory32>),
464        "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown),
465        "thread-spawn" => Function::new_typed_with_env(&mut store, env, thread_spawn::<Memory32>),
466    };
467    namespace
468}
469
470fn wasi_snapshot_preview1_exports(
471    mut store: &mut impl AsStoreMut,
472    env: &FunctionEnv<WasiEnv>,
473) -> Exports {
474    use syscalls::*;
475    let namespace = namespace! {
476        "args_get" => Function::new_typed_with_env(&mut store, env, args_get::<Memory32>),
477        "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get::<Memory32>),
478        "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get::<Memory32>),
479        "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get::<Memory32>),
480        "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get::<Memory32>),
481        "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get::<Memory32>),
482        "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise),
483        "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate),
484        "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close),
485        "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync),
486        "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get::<Memory32>),
487        "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags),
488        "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights),
489        "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, fd_filestat_get::<Memory32>),
490        "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size),
491        "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times),
492        "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread::<Memory32>),
493        "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get::<Memory32>),
494        "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name::<Memory32>),
495        "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite::<Memory32>),
496        "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read::<Memory32>),
497        "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir::<Memory32>),
498        "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber),
499        "fd_seek" => Function::new_typed_with_env(&mut store, env, fd_seek::<Memory32>),
500        "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync),
501        "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell::<Memory32>),
502        "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write::<Memory32>),
503        "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory::<Memory32>),
504        "path_filestat_get" => Function::new_typed_with_env(&mut store, env, path_filestat_get::<Memory32>),
505        "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times::<Memory32>),
506        "path_link" => Function::new_typed_with_env(&mut store, env, path_link::<Memory32>),
507        "path_open" => Function::new_typed_with_env(&mut store, env, path_open::<Memory32>),
508        "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink::<Memory32>),
509        "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory::<Memory32>),
510        "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename::<Memory32>),
511        "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink::<Memory32>),
512        "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file::<Memory32>),
513        "poll_oneoff" => Function::new_typed_with_env(&mut store, env, poll_oneoff::<Memory32>),
514        "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit::<Memory32>),
515        "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
516        "random_get" => Function::new_typed_with_env(&mut store, env, random_get::<Memory32>),
517        "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield::<Memory32>),
518        "sock_accept" => Function::new_typed_with_env(&mut store, env, sock_accept::<Memory32>),
519        "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv::<Memory32>),
520        "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send::<Memory32>),
521        "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown),
522        "thread-spawn" => Function::new_typed_with_env(&mut store, env, thread_spawn::<Memory32>),
523    };
524    namespace
525}
526
527fn wasix_exports_32(mut store: &mut impl AsStoreMut, env: &FunctionEnv<WasiEnv>) -> Exports {
528    use syscalls::*;
529    let namespace = namespace! {
530        "args_get" => Function::new_typed_with_env(&mut store, env, args_get::<Memory32>),
531        "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get::<Memory32>),
532        "call_dynamic" => Function::new_typed_with_env(&mut store, env, call_dynamic::<Memory32>),
533        "reflect_signature" => Function::new_typed_with_env(&mut store, env, reflect_signature::<Memory32>),
534        "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get::<Memory32>),
535        "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get::<Memory32>),
536        "clock_time_set" => Function::new_typed_with_env(&mut store, env, clock_time_set),
537        "closure_prepare" => Function::new_typed_with_env(&mut store, env, closure_prepare::<Memory32>),
538        "closure_allocate" => Function::new_typed_with_env(&mut store, env, closure_allocate::<Memory32>),
539        "closure_free" => Function::new_typed_with_env(&mut store, env, closure_free),
540        "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get::<Memory32>),
541        "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get::<Memory32>),
542        "epoll_create" => Function::new_typed_with_env(&mut store, env, epoll_create::<Memory32>),
543        "epoll_ctl" => Function::new_typed_with_env(&mut store, env, epoll_ctl::<Memory32>),
544        "epoll_wait" => Function::new_typed_with_env(&mut store, env, epoll_wait::<Memory32>),
545        "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise),
546        "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate),
547        "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close),
548        "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync),
549        "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get::<Memory32>),
550        "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags),
551        "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights),
552        "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, fd_filestat_get::<Memory32>),
553        "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size),
554        "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times),
555        "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread::<Memory32>),
556        "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get::<Memory32>),
557        "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name::<Memory32>),
558        "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite::<Memory32>),
559        "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read::<Memory32>),
560        "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir::<Memory32>),
561        "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber),
562        "fd_dup" => Function::new_typed_with_env(&mut store, env, fd_dup::<Memory32>),
563        "fd_dup2" => Function::new_typed_with_env(&mut store, env, fd_dup2::<Memory32>),
564        "fd_fdflags_get" => Function::new_typed_with_env(&mut store, env, fd_fdflags_get::<Memory32>),
565        "fd_fdflags_set" => Function::new_typed_with_env(&mut store, env, fd_fdflags_set),
566        "fd_event" => Function::new_typed_with_env(&mut store, env, fd_event::<Memory32>),
567        "fd_seek" => Function::new_typed_with_env(&mut store, env, fd_seek::<Memory32>),
568        "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync),
569        "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell::<Memory32>),
570        "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write::<Memory32>),
571        "fd_pipe" => Function::new_typed_with_env(&mut store, env, fd_pipe::<Memory32>),
572        "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory::<Memory32>),
573        "path_filestat_get" => Function::new_typed_with_env(&mut store, env, path_filestat_get::<Memory32>),
574        "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times::<Memory32>),
575        "path_link" => Function::new_typed_with_env(&mut store, env, path_link::<Memory32>),
576        "path_open" => Function::new_typed_with_env(&mut store, env, path_open::<Memory32>),
577        "path_open2" => Function::new_typed_with_env(&mut store, env, path_open2::<Memory32>),
578        "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink::<Memory32>),
579        "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory::<Memory32>),
580        "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename::<Memory32>),
581        "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink::<Memory32>),
582        "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file::<Memory32>),
583        "poll_oneoff" => Function::new_typed_with_env(&mut store, env, poll_oneoff::<Memory32>),
584        "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit::<Memory32>),
585        "proc_fork" => Function::new_typed_with_env(&mut store, env, proc_fork::<Memory32>),
586        "proc_join" => Function::new_typed_with_env(&mut store, env, proc_join::<Memory32>),
587        "proc_signal" => Function::new_typed_with_env(&mut store, env, proc_signal),
588        "proc_signals_get" => Function::new_typed_with_env(&mut store, env, proc_signals_get::<Memory32>),
589        "proc_signals_sizes_get" => Function::new_typed_with_env(&mut store, env, proc_signals_sizes_get::<Memory32>),
590        "proc_exec" => Function::new_typed_with_env(&mut store, env, proc_exec::<Memory32>),
591        "proc_exec2" => Function::new_typed_with_env(&mut store, env, proc_exec2::<Memory32>),
592        "proc_exec3" => Function::new_typed_with_env(&mut store, env, proc_exec3::<Memory32>),
593        "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
594        "proc_raise_interval" => Function::new_typed_with_env(&mut store, env, proc_raise_interval),
595        "proc_snapshot" => Function::new_typed_with_env(&mut store, env, proc_snapshot::<Memory32>),
596        "proc_spawn" => Function::new_typed_with_env(&mut store, env, proc_spawn::<Memory32>),
597        "proc_spawn2" => Function::new_typed_with_env(&mut store, env, proc_spawn2::<Memory32>),
598        "proc_id" => Function::new_typed_with_env(&mut store, env, proc_id::<Memory32>),
599        "proc_parent" => Function::new_typed_with_env(&mut store, env, proc_parent::<Memory32>),
600        "random_get" => Function::new_typed_with_env(&mut store, env, random_get::<Memory32>),
601        "tty_get" => Function::new_typed_with_env(&mut store, env, tty_get::<Memory32>),
602        "tty_set" => Function::new_typed_with_env(&mut store, env, tty_set::<Memory32>),
603        "getcwd" => Function::new_typed_with_env(&mut store, env, getcwd::<Memory32>),
604        "chdir" => Function::new_typed_with_env(&mut store, env, chdir::<Memory32>),
605        "dl_invalid_handle" => Function::new_typed_with_env(&mut store, env, dl_invalid_handle),
606        "dlopen" => Function::new_typed_with_env(&mut store, env, dlopen::<Memory32>),
607        "dlsym" => Function::new_typed_with_env(&mut store, env, dlsym::<Memory32>),
608        "callback_signal" => Function::new_typed_with_env(&mut store, env, callback_signal::<Memory32>),
609        "thread_spawn" => Function::new_typed_with_env(&mut store, env, thread_spawn_v2::<Memory32>),
610        "thread_spawn_v2" => Function::new_typed_with_env(&mut store, env, thread_spawn_v2::<Memory32>),
611        "thread_sleep" => Function::new_typed_with_env(&mut store, env, thread_sleep::<Memory32>),
612        "thread_id" => Function::new_typed_with_env(&mut store, env, thread_id::<Memory32>),
613        "thread_signal" => Function::new_typed_with_env(&mut store, env, thread_signal),
614        "thread_join" => Function::new_typed_with_env(&mut store, env, thread_join::<Memory32>),
615        "thread_parallelism" => Function::new_typed_with_env(&mut store, env, thread_parallelism::<Memory32>),
616        "thread_exit" => Function::new_typed_with_env(&mut store, env, thread_exit),
617        "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield::<Memory32>),
618        "stack_checkpoint" => Function::new_typed_with_env(&mut store, env, stack_checkpoint::<Memory32>),
619        "stack_restore" => Function::new_typed_with_env(&mut store, env, stack_restore::<Memory32>),
620        "futex_wait" => Function::new_typed_with_env(&mut store, env, futex_wait::<Memory32>),
621        "futex_wake" => Function::new_typed_with_env(&mut store, env, futex_wake::<Memory32>),
622        "futex_wake_all" => Function::new_typed_with_env(&mut store, env, futex_wake_all::<Memory32>),
623        "port_bridge" => Function::new_typed_with_env(&mut store, env, port_bridge::<Memory32>),
624        "port_unbridge" => Function::new_typed_with_env(&mut store, env, port_unbridge),
625        "port_dhcp_acquire" => Function::new_typed_with_env(&mut store, env, port_dhcp_acquire),
626        "port_addr_add" => Function::new_typed_with_env(&mut store, env, port_addr_add::<Memory32>),
627        "port_addr_remove" => Function::new_typed_with_env(&mut store, env, port_addr_remove::<Memory32>),
628        "port_addr_clear" => Function::new_typed_with_env(&mut store, env, port_addr_clear),
629        "port_addr_list" => Function::new_typed_with_env(&mut store, env, port_addr_list::<Memory32>),
630        "port_mac" => Function::new_typed_with_env(&mut store, env, port_mac::<Memory32>),
631        "port_gateway_set" => Function::new_typed_with_env(&mut store, env, port_gateway_set::<Memory32>),
632        "port_route_add" => Function::new_typed_with_env(&mut store, env, port_route_add::<Memory32>),
633        "port_route_remove" => Function::new_typed_with_env(&mut store, env, port_route_remove::<Memory32>),
634        "port_route_clear" => Function::new_typed_with_env(&mut store, env, port_route_clear),
635        "port_route_list" => Function::new_typed_with_env(&mut store, env, port_route_list::<Memory32>),
636        "sock_status" => Function::new_typed_with_env(&mut store, env, sock_status::<Memory32>),
637        "sock_addr_local" => Function::new_typed_with_env(&mut store, env, sock_addr_local::<Memory32>),
638        "sock_addr_peer" => Function::new_typed_with_env(&mut store, env, sock_addr_peer::<Memory32>),
639        "sock_open" => Function::new_typed_with_env(&mut store, env, sock_open::<Memory32>),
640        "sock_pair" => Function::new_typed_with_env(&mut store, env, sock_pair::<Memory32>),
641        "sock_set_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_set_opt_flag),
642        "sock_get_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_get_opt_flag::<Memory32>),
643        "sock_set_opt_time" => Function::new_typed_with_env(&mut store, env, sock_set_opt_time::<Memory32>),
644        "sock_get_opt_time" => Function::new_typed_with_env(&mut store, env, sock_get_opt_time::<Memory32>),
645        "sock_set_opt_size" => Function::new_typed_with_env(&mut store, env, sock_set_opt_size),
646        "sock_get_opt_size" => Function::new_typed_with_env(&mut store, env, sock_get_opt_size::<Memory32>),
647        "sock_join_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v4::<Memory32>),
648        "sock_leave_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v4::<Memory32>),
649        "sock_join_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v6::<Memory32>),
650        "sock_leave_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v6::<Memory32>),
651        "sock_bind" => Function::new_typed_with_env(&mut store, env, sock_bind::<Memory32>),
652        "sock_listen" => Function::new_typed_with_env(&mut store, env, sock_listen::<Memory32>),
653        "sock_accept" => Function::new_typed_with_env(&mut store, env, sock_accept_v2::<Memory32>),
654        "sock_accept_v2" => Function::new_typed_with_env(&mut store, env, sock_accept_v2::<Memory32>),
655        "sock_connect" => Function::new_typed_with_env(&mut store, env, sock_connect::<Memory32>),
656        "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv::<Memory32>),
657        "sock_recv_from" => Function::new_typed_with_env(&mut store, env, sock_recv_from::<Memory32>),
658        "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send::<Memory32>),
659        "sock_send_to" => Function::new_typed_with_env(&mut store, env, sock_send_to::<Memory32>),
660        "sock_send_file" => Function::new_typed_with_env(&mut store, env, sock_send_file::<Memory32>),
661        "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown),
662        "resolve" => Function::new_typed_with_env(&mut store, env, resolve::<Memory32>),
663    };
664    namespace
665}
666
667fn wasix_exports_64(mut store: &mut impl AsStoreMut, env: &FunctionEnv<WasiEnv>) -> Exports {
668    use syscalls::*;
669    let namespace = namespace! {
670        "args_get" => Function::new_typed_with_env(&mut store, env, args_get::<Memory64>),
671        "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get::<Memory64>),
672        "call_dynamic" => Function::new_typed_with_env(&mut store, env, call_dynamic::<Memory64>),
673        "reflect_signature" => Function::new_typed_with_env(&mut store, env, reflect_signature::<Memory64>),
674        "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get::<Memory64>),
675        "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get::<Memory64>),
676        "clock_time_set" => Function::new_typed_with_env(&mut store, env, clock_time_set),
677        "closure_prepare" => Function::new_typed_with_env(&mut store, env, closure_prepare::<Memory64>),
678        "closure_allocate" => Function::new_typed_with_env(&mut store, env, closure_allocate::<Memory64>),
679        "closure_free" => Function::new_typed_with_env(&mut store, env, closure_free),
680        "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get::<Memory64>),
681        "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get::<Memory64>),
682        "epoll_create" => Function::new_typed_with_env(&mut store, env, epoll_create::<Memory64>),
683        "epoll_ctl" => Function::new_typed_with_env(&mut store, env, epoll_ctl::<Memory64>),
684        "epoll_wait" => Function::new_typed_with_env(&mut store, env, epoll_wait::<Memory64>),
685        "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise),
686        "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate),
687        "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close),
688        "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync),
689        "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get::<Memory64>),
690        "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags),
691        "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights),
692        "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, fd_filestat_get::<Memory64>),
693        "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size),
694        "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times),
695        "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread::<Memory64>),
696        "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get::<Memory64>),
697        "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name::<Memory64>),
698        "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite::<Memory64>),
699        "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read::<Memory64>),
700        "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir::<Memory64>),
701        "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber),
702        "fd_dup" => Function::new_typed_with_env(&mut store, env, fd_dup::<Memory64>),
703        "fd_dup2" => Function::new_typed_with_env(&mut store, env, fd_dup2::<Memory64>),
704        "fd_fdflags_get" => Function::new_typed_with_env(&mut store, env, fd_fdflags_get::<Memory64>),
705        "fd_fdflags_set" => Function::new_typed_with_env(&mut store, env, fd_fdflags_set),
706        "fd_event" => Function::new_typed_with_env(&mut store, env, fd_event::<Memory64>),
707        "fd_seek" => Function::new_typed_with_env(&mut store, env, fd_seek::<Memory64>),
708        "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync),
709        "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell::<Memory64>),
710        "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write::<Memory64>),
711        "fd_pipe" => Function::new_typed_with_env(&mut store, env, fd_pipe::<Memory64>),
712        "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory::<Memory64>),
713        "path_filestat_get" => Function::new_typed_with_env(&mut store, env, path_filestat_get::<Memory64>),
714        "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times::<Memory64>),
715        "path_link" => Function::new_typed_with_env(&mut store, env, path_link::<Memory64>),
716        "path_open" => Function::new_typed_with_env(&mut store, env, path_open::<Memory64>),
717        "path_open2" => Function::new_typed_with_env(&mut store, env, path_open2::<Memory64>),
718        "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink::<Memory64>),
719        "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory::<Memory64>),
720        "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename::<Memory64>),
721        "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink::<Memory64>),
722        "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file::<Memory64>),
723        "poll_oneoff" => Function::new_typed_with_env(&mut store, env, poll_oneoff::<Memory64>),
724        "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit::<Memory64>),
725        "proc_fork" => Function::new_typed_with_env(&mut store, env, proc_fork::<Memory64>),
726        "proc_join" => Function::new_typed_with_env(&mut store, env, proc_join::<Memory64>),
727        "proc_signal" => Function::new_typed_with_env(&mut store, env, proc_signal),
728        "proc_signals_get" => Function::new_typed_with_env(&mut store, env, proc_signals_get::<Memory64>),
729        "proc_signals_sizes_get" => Function::new_typed_with_env(&mut store, env, proc_signals_sizes_get::<Memory64>),
730        "proc_exec" => Function::new_typed_with_env(&mut store, env, proc_exec::<Memory64>),
731        "proc_exec2" => Function::new_typed_with_env(&mut store, env, proc_exec2::<Memory64>),
732        "proc_exec3" => Function::new_typed_with_env(&mut store, env, proc_exec3::<Memory64>),
733        "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
734        "proc_raise_interval" => Function::new_typed_with_env(&mut store, env, proc_raise_interval),
735        "proc_snapshot" => Function::new_typed_with_env(&mut store, env, proc_snapshot::<Memory64>),
736        "proc_spawn" => Function::new_typed_with_env(&mut store, env, proc_spawn::<Memory64>),
737        "proc_spawn2" => Function::new_typed_with_env(&mut store, env, proc_spawn2::<Memory64>),
738        "proc_id" => Function::new_typed_with_env(&mut store, env, proc_id::<Memory64>),
739        "proc_parent" => Function::new_typed_with_env(&mut store, env, proc_parent::<Memory64>),
740        "random_get" => Function::new_typed_with_env(&mut store, env, random_get::<Memory64>),
741        "tty_get" => Function::new_typed_with_env(&mut store, env, tty_get::<Memory64>),
742        "tty_set" => Function::new_typed_with_env(&mut store, env, tty_set::<Memory64>),
743        "getcwd" => Function::new_typed_with_env(&mut store, env, getcwd::<Memory64>),
744        "chdir" => Function::new_typed_with_env(&mut store, env, chdir::<Memory64>),
745        "dl_invalid_handle" => Function::new_typed_with_env(&mut store, env, dl_invalid_handle),
746        "dlopen" => Function::new_typed_with_env(&mut store, env, dlopen::<Memory64>),
747        "dlsym" => Function::new_typed_with_env(&mut store, env, dlsym::<Memory64>),
748        "callback_signal" => Function::new_typed_with_env(&mut store, env, callback_signal::<Memory64>),
749        "thread_spawn" => Function::new_typed_with_env(&mut store, env, thread_spawn_v2::<Memory64>),
750        "thread_spawn_v2" => Function::new_typed_with_env(&mut store, env, thread_spawn_v2::<Memory64>),
751        "thread_sleep" => Function::new_typed_with_env(&mut store, env, thread_sleep::<Memory64>),
752        "thread_id" => Function::new_typed_with_env(&mut store, env, thread_id::<Memory64>),
753        "thread_signal" => Function::new_typed_with_env(&mut store, env, thread_signal),
754        "thread_join" => Function::new_typed_with_env(&mut store, env, thread_join::<Memory64>),
755        "thread_parallelism" => Function::new_typed_with_env(&mut store, env, thread_parallelism::<Memory64>),
756        "thread_exit" => Function::new_typed_with_env(&mut store, env, thread_exit),
757        "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield::<Memory64>),
758        "stack_checkpoint" => Function::new_typed_with_env(&mut store, env, stack_checkpoint::<Memory64>),
759        "stack_restore" => Function::new_typed_with_env(&mut store, env, stack_restore::<Memory64>),
760        "futex_wait" => Function::new_typed_with_env(&mut store, env, futex_wait::<Memory64>),
761        "futex_wake" => Function::new_typed_with_env(&mut store, env, futex_wake::<Memory64>),
762        "futex_wake_all" => Function::new_typed_with_env(&mut store, env, futex_wake_all::<Memory64>),
763        "port_bridge" => Function::new_typed_with_env(&mut store, env, port_bridge::<Memory64>),
764        "port_unbridge" => Function::new_typed_with_env(&mut store, env, port_unbridge),
765        "port_dhcp_acquire" => Function::new_typed_with_env(&mut store, env, port_dhcp_acquire),
766        "port_addr_add" => Function::new_typed_with_env(&mut store, env, port_addr_add::<Memory64>),
767        "port_addr_remove" => Function::new_typed_with_env(&mut store, env, port_addr_remove::<Memory64>),
768        "port_addr_clear" => Function::new_typed_with_env(&mut store, env, port_addr_clear),
769        "port_addr_list" => Function::new_typed_with_env(&mut store, env, port_addr_list::<Memory64>),
770        "port_mac" => Function::new_typed_with_env(&mut store, env, port_mac::<Memory64>),
771        "port_gateway_set" => Function::new_typed_with_env(&mut store, env, port_gateway_set::<Memory64>),
772        "port_route_add" => Function::new_typed_with_env(&mut store, env, port_route_add::<Memory64>),
773        "port_route_remove" => Function::new_typed_with_env(&mut store, env, port_route_remove::<Memory64>),
774        "port_route_clear" => Function::new_typed_with_env(&mut store, env, port_route_clear),
775        "port_route_list" => Function::new_typed_with_env(&mut store, env, port_route_list::<Memory64>),
776        "sock_status" => Function::new_typed_with_env(&mut store, env, sock_status::<Memory64>),
777        "sock_addr_local" => Function::new_typed_with_env(&mut store, env, sock_addr_local::<Memory64>),
778        "sock_addr_peer" => Function::new_typed_with_env(&mut store, env, sock_addr_peer::<Memory64>),
779        "sock_open" => Function::new_typed_with_env(&mut store, env, sock_open::<Memory64>),
780        "sock_pair" => Function::new_typed_with_env(&mut store, env, sock_pair::<Memory64>),
781        "sock_set_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_set_opt_flag),
782        "sock_get_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_get_opt_flag::<Memory64>),
783        "sock_set_opt_time" => Function::new_typed_with_env(&mut store, env, sock_set_opt_time::<Memory64>),
784        "sock_get_opt_time" => Function::new_typed_with_env(&mut store, env, sock_get_opt_time::<Memory64>),
785        "sock_set_opt_size" => Function::new_typed_with_env(&mut store, env, sock_set_opt_size),
786        "sock_get_opt_size" => Function::new_typed_with_env(&mut store, env, sock_get_opt_size::<Memory64>),
787        "sock_join_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v4::<Memory64>),
788        "sock_leave_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v4::<Memory64>),
789        "sock_join_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v6::<Memory64>),
790        "sock_leave_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v6::<Memory64>),
791        "sock_bind" => Function::new_typed_with_env(&mut store, env, sock_bind::<Memory64>),
792        "sock_listen" => Function::new_typed_with_env(&mut store, env, sock_listen::<Memory64>),
793        "sock_accept" => Function::new_typed_with_env(&mut store, env, sock_accept_v2::<Memory64>),
794        "sock_accept_v2" => Function::new_typed_with_env(&mut store, env, sock_accept_v2::<Memory64>),
795        "sock_connect" => Function::new_typed_with_env(&mut store, env, sock_connect::<Memory64>),
796        "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv::<Memory64>),
797        "sock_recv_from" => Function::new_typed_with_env(&mut store, env, sock_recv_from::<Memory64>),
798        "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send::<Memory64>),
799        "sock_send_to" => Function::new_typed_with_env(&mut store, env, sock_send_to::<Memory64>),
800        "sock_send_file" => Function::new_typed_with_env(&mut store, env, sock_send_file::<Memory64>),
801        "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown),
802        "resolve" => Function::new_typed_with_env(&mut store, env, resolve::<Memory64>),
803    };
804    namespace
805}
806
807// TODO: split function into two variants, one for JS and one for sys.
808// (this will make code less messy)
809fn import_object_for_all_wasi_versions(
810    _module: &wasmer::Module,
811    store: &mut impl AsStoreMut,
812    env: &FunctionEnv<WasiEnv>,
813) -> Imports {
814    let exports_wasi_generic = wasi_exports_generic(store, env);
815    let exports_wasi_unstable = wasi_unstable_exports(store, env);
816    let exports_wasi_snapshot_preview1 = wasi_snapshot_preview1_exports(store, env);
817    let exports_wasix_32v1 = wasix_exports_32(store, env);
818    let exports_wasix_64v1 = wasix_exports_64(store, env);
819
820    // Allowed due to JS feature flag complications.
821    #[allow(unused_mut)]
822    let mut imports = imports! {
823        "wasi" => exports_wasi_generic,
824        "wasi_unstable" => exports_wasi_unstable,
825        "wasi_snapshot_preview1" => exports_wasi_snapshot_preview1,
826        "wasix_32v1" => exports_wasix_32v1,
827        "wasix_64v1" => exports_wasix_64v1,
828    };
829
830    imports
831}
832
833/// Combines a state generating function with the import list for legacy WASI
834fn generate_import_object_snapshot0(
835    store: &mut impl AsStoreMut,
836    env: &FunctionEnv<WasiEnv>,
837) -> Imports {
838    let exports_unstable = wasi_unstable_exports(store, env);
839    imports! {
840        "wasi_unstable" => exports_unstable
841    }
842}
843
844fn generate_import_object_snapshot1(
845    store: &mut impl AsStoreMut,
846    env: &FunctionEnv<WasiEnv>,
847) -> Imports {
848    let exports_wasi_snapshot_preview1 = wasi_snapshot_preview1_exports(store, env);
849    imports! {
850        "wasi_snapshot_preview1" => exports_wasi_snapshot_preview1
851    }
852}
853
854/// Combines a state generating function with the import list for snapshot 1
855fn generate_import_object_wasix32_v1(
856    store: &mut impl AsStoreMut,
857    env: &FunctionEnv<WasiEnv>,
858) -> Imports {
859    let exports_wasix_32v1 = wasix_exports_32(store, env);
860    imports! {
861        "wasix_32v1" => exports_wasix_32v1
862    }
863}
864
865fn generate_import_object_wasix64_v1(
866    store: &mut impl AsStoreMut,
867    env: &FunctionEnv<WasiEnv>,
868) -> Imports {
869    let exports_wasix_64v1 = wasix_exports_64(store, env);
870    imports! {
871        "wasix_64v1" => exports_wasix_64v1
872    }
873}
874
875fn mem_error_to_wasi(err: MemoryAccessError) -> Errno {
876    match err {
877        MemoryAccessError::HeapOutOfBounds => Errno::Memviolation,
878        MemoryAccessError::Overflow => Errno::Overflow,
879        MemoryAccessError::NonUtf8String => Errno::Inval,
880        _ => Errno::Unknown,
881    }
882}
883
884/// Run a synchronous function that would normally be blocking.
885///
886/// When the `sys-thread` feature is enabled, this will call
887/// [`tokio::task::block_in_place()`]. Otherwise, it calls the function
888/// immediately.
889pub(crate) fn block_in_place<Ret>(thunk: impl FnOnce() -> Ret) -> Ret {
890    cfg_if::cfg_if! {
891        if #[cfg(feature = "sys-thread")] {
892            tokio::task::block_in_place(thunk)
893        } else {
894            thunk()
895        }
896    }
897}
898
899/// Spawns a new blocking task that runs the provided closure.
900///
901/// The closure is executed on a separate thread, allowing it to perform blocking operations
902/// without blocking the main thread. The closure is wrapped in a `Future` that resolves to the
903/// result of the closure's execution.
904pub(crate) async fn spawn_blocking<F, R>(f: F) -> Result<R, tokio::task::JoinError>
905where
906    F: FnOnce() -> R + Send + 'static,
907    R: Send + 'static,
908{
909    cfg_if::cfg_if! {
910        if #[cfg(target_arch = "wasm32")] {
911            Ok(block_in_place(f))
912        } else {
913            tokio::task::spawn_blocking(f).await
914        }
915    }
916}
917
918pub(crate) fn flatten_runtime_error(err: RuntimeError) -> RuntimeError {
919    let e_ref = err.downcast_ref::<WasiRuntimeError>();
920    match e_ref {
921        Some(WasiRuntimeError::Wasi(_)) => {
922            let Ok(WasiRuntimeError::Wasi(err)) = err.downcast::<WasiRuntimeError>() else {
923                unreachable!()
924            };
925            RuntimeError::user(Box::new(err))
926        }
927        Some(WasiRuntimeError::Runtime(_)) => {
928            let Ok(WasiRuntimeError::Runtime(err)) = err.downcast::<WasiRuntimeError>() else {
929                unreachable!()
930            };
931            flatten_runtime_error(err)
932        }
933        _ => err,
934    }
935}