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))]
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#[derive(Debug)]
324pub struct WasiVFork {
325    /// The information needed to rewind the stack with asyncify
326    pub asyncify: Option<WasiVForkAsyncify>,
327
328    /// The environment before the vfork occurred
329    pub env: Box<WasiEnv>,
330
331    /// Handle of the thread we have forked (dropping this handle
332    /// will signal that the thread is dead)
333    pub handle: WasiThreadHandle,
334}
335
336#[derive(Debug, Clone)]
337pub struct WasiVForkAsyncify {
338    /// The unwound stack before the vfork occurred
339    pub rewind_stack: BytesMut,
340    /// The mutable parts of the store
341    pub store_data: Bytes,
342    /// Whether the store is 64-bit
343    pub is_64bit: bool,
344}
345
346impl Clone for WasiVFork {
347    fn clone(&self) -> Self {
348        Self {
349            asyncify: self.asyncify.clone(),
350            env: Box::new(self.env.as_ref().clone()),
351            handle: self.handle.clone(),
352        }
353    }
354}
355
356/// Create an [`Imports`] with an existing [`WasiEnv`]. [`WasiEnv`] values are
357/// typically constructed with [`WasiEnvBuilder`].
358pub fn generate_import_object_from_env(
359    store: &mut impl AsStoreMut,
360    ctx: &FunctionEnv<WasiEnv>,
361    version: WasiVersion,
362) -> Imports {
363    let mut imports = match version {
364        WasiVersion::Snapshot0 => generate_import_object_snapshot0(store, ctx),
365        WasiVersion::Snapshot1 | WasiVersion::Latest => {
366            generate_import_object_snapshot1(store, ctx)
367        }
368        WasiVersion::Wasix32v1 => generate_import_object_wasix32_v1(store, ctx),
369        WasiVersion::Wasix64v1 => generate_import_object_wasix64_v1(store, ctx),
370    };
371
372    let exports_wasi_generic = wasi_exports_generic(store, ctx);
373
374    let imports_wasi_generic = imports! {
375        "wasi" => exports_wasi_generic,
376    };
377
378    imports.extend(&imports_wasi_generic);
379
380    imports
381}
382
383fn wasi_exports_generic(mut store: &mut impl AsStoreMut, env: &FunctionEnv<WasiEnv>) -> Exports {
384    use syscalls::*;
385    let namespace = namespace! {
386        "thread-spawn" => Function::new_typed_with_env(&mut store, env, thread_spawn::<Memory32>),
387    };
388    namespace
389}
390
391fn wasi_unstable_exports(mut store: &mut impl AsStoreMut, env: &FunctionEnv<WasiEnv>) -> Exports {
392    use syscalls::*;
393    let namespace = namespace! {
394        "args_get" => Function::new_typed_with_env(&mut store, env, args_get::<Memory32>),
395        "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get::<Memory32>),
396        "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get::<Memory32>),
397        "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get::<Memory32>),
398        "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get::<Memory32>),
399        "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get::<Memory32>),
400        "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise),
401        "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate),
402        "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close),
403        "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync),
404        "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get::<Memory32>),
405        "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags),
406        "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights),
407        "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::fd_filestat_get),
408        "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size),
409        "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times),
410        "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread::<Memory32>),
411        "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get::<Memory32>),
412        "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name::<Memory32>),
413        "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite::<Memory32>),
414        "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read::<Memory32>),
415        "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir::<Memory32>),
416        "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber),
417        "fd_seek" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::fd_seek),
418        "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync),
419        "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell::<Memory32>),
420        "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write::<Memory32>),
421        "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory::<Memory32>),
422        "path_filestat_get" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::path_filestat_get),
423        "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times::<Memory32>),
424        "path_link" => Function::new_typed_with_env(&mut store, env, path_link::<Memory32>),
425        "path_open" => Function::new_typed_with_env(&mut store, env, path_open::<Memory32>),
426        "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink::<Memory32>),
427        "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory::<Memory32>),
428        "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename::<Memory32>),
429        "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink::<Memory32>),
430        "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file::<Memory32>),
431        "poll_oneoff" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::poll_oneoff::<Memory32>),
432        "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit::<Memory32>),
433        "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
434        "random_get" => Function::new_typed_with_env(&mut store, env, random_get::<Memory32>),
435        "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield::<Memory32>),
436        "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv::<Memory32>),
437        "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send::<Memory32>),
438        "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown),
439        "thread-spawn" => Function::new_typed_with_env(&mut store, env, thread_spawn::<Memory32>),
440    };
441    namespace
442}
443
444fn wasi_snapshot_preview1_exports(
445    mut store: &mut impl AsStoreMut,
446    env: &FunctionEnv<WasiEnv>,
447) -> Exports {
448    use syscalls::*;
449    let namespace = namespace! {
450        "args_get" => Function::new_typed_with_env(&mut store, env, args_get::<Memory32>),
451        "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get::<Memory32>),
452        "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get::<Memory32>),
453        "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get::<Memory32>),
454        "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get::<Memory32>),
455        "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get::<Memory32>),
456        "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise),
457        "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate),
458        "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close),
459        "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync),
460        "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get::<Memory32>),
461        "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags),
462        "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights),
463        "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, fd_filestat_get::<Memory32>),
464        "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size),
465        "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times),
466        "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread::<Memory32>),
467        "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get::<Memory32>),
468        "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name::<Memory32>),
469        "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite::<Memory32>),
470        "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read::<Memory32>),
471        "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir::<Memory32>),
472        "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber),
473        "fd_seek" => Function::new_typed_with_env(&mut store, env, fd_seek::<Memory32>),
474        "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync),
475        "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell::<Memory32>),
476        "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write::<Memory32>),
477        "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory::<Memory32>),
478        "path_filestat_get" => Function::new_typed_with_env(&mut store, env, path_filestat_get::<Memory32>),
479        "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times::<Memory32>),
480        "path_link" => Function::new_typed_with_env(&mut store, env, path_link::<Memory32>),
481        "path_open" => Function::new_typed_with_env(&mut store, env, path_open::<Memory32>),
482        "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink::<Memory32>),
483        "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory::<Memory32>),
484        "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename::<Memory32>),
485        "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink::<Memory32>),
486        "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file::<Memory32>),
487        "poll_oneoff" => Function::new_typed_with_env(&mut store, env, poll_oneoff::<Memory32>),
488        "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit::<Memory32>),
489        "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
490        "random_get" => Function::new_typed_with_env(&mut store, env, random_get::<Memory32>),
491        "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield::<Memory32>),
492        "sock_accept" => Function::new_typed_with_env(&mut store, env, sock_accept::<Memory32>),
493        "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv::<Memory32>),
494        "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send::<Memory32>),
495        "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown),
496        "thread-spawn" => Function::new_typed_with_env(&mut store, env, thread_spawn::<Memory32>),
497    };
498    namespace
499}
500
501fn wasix_exports_32(mut store: &mut impl AsStoreMut, env: &FunctionEnv<WasiEnv>) -> Exports {
502    let engine_supports_async = store.as_store_ref().engine().supports_async();
503
504    use syscalls::*;
505    let namespace = namespace! {
506        "args_get" => Function::new_typed_with_env(&mut store, env, args_get::<Memory32>),
507        "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get::<Memory32>),
508        "call_dynamic" => Function::new_typed_with_env(&mut store, env, call_dynamic::<Memory32>),
509        "reflect_signature" => Function::new_typed_with_env(&mut store, env, reflect_signature::<Memory32>),
510        "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get::<Memory32>),
511        "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get::<Memory32>),
512        "clock_time_set" => Function::new_typed_with_env(&mut store, env, clock_time_set),
513        "closure_prepare" => Function::new_typed_with_env(&mut store, env, closure_prepare::<Memory32>),
514        "closure_allocate" => Function::new_typed_with_env(&mut store, env, closure_allocate::<Memory32>),
515        "closure_free" => Function::new_typed_with_env(&mut store, env, closure_free),
516        "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get::<Memory32>),
517        "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get::<Memory32>),
518        "epoll_create" => Function::new_typed_with_env(&mut store, env, epoll_create::<Memory32>),
519        "epoll_ctl" => Function::new_typed_with_env(&mut store, env, epoll_ctl::<Memory32>),
520        "epoll_wait" => Function::new_typed_with_env(&mut store, env, epoll_wait::<Memory32>),
521        "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise),
522        "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate),
523        "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close),
524        "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync),
525        "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get::<Memory32>),
526        "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags),
527        "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights),
528        "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, fd_filestat_get::<Memory32>),
529        "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size),
530        "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times),
531        "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread::<Memory32>),
532        "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get::<Memory32>),
533        "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name::<Memory32>),
534        "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite::<Memory32>),
535        "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read::<Memory32>),
536        "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir::<Memory32>),
537        "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber),
538        "fd_dup" => Function::new_typed_with_env(&mut store, env, fd_dup::<Memory32>),
539        "fd_dup2" => Function::new_typed_with_env(&mut store, env, fd_dup2::<Memory32>),
540        "fd_fdflags_get" => Function::new_typed_with_env(&mut store, env, fd_fdflags_get::<Memory32>),
541        "fd_fdflags_set" => Function::new_typed_with_env(&mut store, env, fd_fdflags_set),
542        "fd_event" => Function::new_typed_with_env(&mut store, env, fd_event::<Memory32>),
543        "fd_seek" => Function::new_typed_with_env(&mut store, env, fd_seek::<Memory32>),
544        "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync),
545        "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell::<Memory32>),
546        "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write::<Memory32>),
547        "fd_pipe" => Function::new_typed_with_env(&mut store, env, fd_pipe::<Memory32>),
548        "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory::<Memory32>),
549        "path_filestat_get" => Function::new_typed_with_env(&mut store, env, path_filestat_get::<Memory32>),
550        "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times::<Memory32>),
551        "path_link" => Function::new_typed_with_env(&mut store, env, path_link::<Memory32>),
552        "path_open" => Function::new_typed_with_env(&mut store, env, path_open::<Memory32>),
553        "path_open2" => Function::new_typed_with_env(&mut store, env, path_open2::<Memory32>),
554        "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink::<Memory32>),
555        "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory::<Memory32>),
556        "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename::<Memory32>),
557        "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink::<Memory32>),
558        "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file::<Memory32>),
559        "poll_oneoff" => Function::new_typed_with_env(&mut store, env, poll_oneoff::<Memory32>),
560        "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit::<Memory32>),
561        "proc_fork" => Function::new_typed_with_env(&mut store, env, proc_fork::<Memory32>),
562        "proc_fork_env" => Function::new_typed_with_env(&mut store, env, proc_fork_env::<Memory32>),
563        "proc_join" => Function::new_typed_with_env(&mut store, env, proc_join::<Memory32>),
564        "proc_signal" => Function::new_typed_with_env(&mut store, env, proc_signal),
565        "proc_signals_get" => Function::new_typed_with_env(&mut store, env, proc_signals_get::<Memory32>),
566        "proc_signals_sizes_get" => Function::new_typed_with_env(&mut store, env, proc_signals_sizes_get::<Memory32>),
567        "proc_exec" => Function::new_typed_with_env(&mut store, env, proc_exec::<Memory32>),
568        "proc_exec2" => Function::new_typed_with_env(&mut store, env, proc_exec2::<Memory32>),
569        "proc_exec3" => Function::new_typed_with_env(&mut store, env, proc_exec3::<Memory32>),
570        "proc_exit2" => Function::new_typed_with_env(&mut store, env, proc_exit2::<Memory32>),
571        "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
572        "proc_raise_interval" => Function::new_typed_with_env(&mut store, env, proc_raise_interval),
573        "proc_snapshot" => Function::new_typed_with_env(&mut store, env, proc_snapshot::<Memory32>),
574        "proc_spawn" => Function::new_typed_with_env(&mut store, env, proc_spawn::<Memory32>),
575        "proc_spawn2" => Function::new_typed_with_env(&mut store, env, proc_spawn2::<Memory32>),
576        "proc_id" => Function::new_typed_with_env(&mut store, env, proc_id::<Memory32>),
577        "proc_parent" => Function::new_typed_with_env(&mut store, env, proc_parent::<Memory32>),
578        "random_get" => Function::new_typed_with_env(&mut store, env, random_get::<Memory32>),
579        "tty_get" => Function::new_typed_with_env(&mut store, env, tty_get::<Memory32>),
580        "tty_set" => Function::new_typed_with_env(&mut store, env, tty_set::<Memory32>),
581        "getcwd" => Function::new_typed_with_env(&mut store, env, getcwd::<Memory32>),
582        "chdir" => Function::new_typed_with_env(&mut store, env, chdir::<Memory32>),
583        "dl_invalid_handle" => Function::new_typed_with_env(&mut store, env, dl_invalid_handle),
584        "dlopen" => Function::new_typed_with_env(&mut store, env, dlopen::<Memory32>),
585        "dlsym" => Function::new_typed_with_env(&mut store, env, dlsym::<Memory32>),
586        "callback_signal" => Function::new_typed_with_env(&mut store, env, callback_signal::<Memory32>),
587        "thread_spawn" => Function::new_typed_with_env(&mut store, env, thread_spawn_v2::<Memory32>),
588        "thread_spawn_v2" => Function::new_typed_with_env(&mut store, env, thread_spawn_v2::<Memory32>),
589        "thread_sleep" => Function::new_typed_with_env(&mut store, env, thread_sleep::<Memory32>),
590        "thread_id" => Function::new_typed_with_env(&mut store, env, thread_id::<Memory32>),
591        "thread_signal" => Function::new_typed_with_env(&mut store, env, thread_signal),
592        "thread_join" => Function::new_typed_with_env(&mut store, env, thread_join::<Memory32>),
593        "thread_parallelism" => Function::new_typed_with_env(&mut store, env, thread_parallelism::<Memory32>),
594        "thread_exit" => Function::new_typed_with_env(&mut store, env, thread_exit),
595        "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield::<Memory32>),
596        "stack_checkpoint" => Function::new_typed_with_env(&mut store, env, stack_checkpoint::<Memory32>),
597        "stack_restore" => Function::new_typed_with_env(&mut store, env, stack_restore::<Memory32>),
598        "context_create" => Function::new_typed_with_env(&mut store, env, context_create::<Memory32>),
599        "context_switch" => if engine_supports_async { Function::new_typed_with_env_async(&mut store, env, context_switch) } else { Function::new_typed_with_env(&mut store, env, context_switch_not_supported) },
600        "context_destroy" => Function::new_typed_with_env(&mut store, env, context_destroy),
601        "futex_wait" => Function::new_typed_with_env(&mut store, env, futex_wait::<Memory32>),
602        "futex_wake" => Function::new_typed_with_env(&mut store, env, futex_wake::<Memory32>),
603        "futex_wake_all" => Function::new_typed_with_env(&mut store, env, futex_wake_all::<Memory32>),
604        "port_bridge" => Function::new_typed_with_env(&mut store, env, port_bridge::<Memory32>),
605        "port_unbridge" => Function::new_typed_with_env(&mut store, env, port_unbridge),
606        "port_dhcp_acquire" => Function::new_typed_with_env(&mut store, env, port_dhcp_acquire),
607        "port_addr_add" => Function::new_typed_with_env(&mut store, env, port_addr_add::<Memory32>),
608        "port_addr_remove" => Function::new_typed_with_env(&mut store, env, port_addr_remove::<Memory32>),
609        "port_addr_clear" => Function::new_typed_with_env(&mut store, env, port_addr_clear),
610        "port_addr_list" => Function::new_typed_with_env(&mut store, env, port_addr_list::<Memory32>),
611        "port_mac" => Function::new_typed_with_env(&mut store, env, port_mac::<Memory32>),
612        "port_gateway_set" => Function::new_typed_with_env(&mut store, env, port_gateway_set::<Memory32>),
613        "port_route_add" => Function::new_typed_with_env(&mut store, env, port_route_add::<Memory32>),
614        "port_route_remove" => Function::new_typed_with_env(&mut store, env, port_route_remove::<Memory32>),
615        "port_route_clear" => Function::new_typed_with_env(&mut store, env, port_route_clear),
616        "port_route_list" => Function::new_typed_with_env(&mut store, env, port_route_list::<Memory32>),
617        "sock_status" => Function::new_typed_with_env(&mut store, env, sock_status::<Memory32>),
618        "sock_addr_local" => Function::new_typed_with_env(&mut store, env, sock_addr_local::<Memory32>),
619        "sock_addr_peer" => Function::new_typed_with_env(&mut store, env, sock_addr_peer::<Memory32>),
620        "sock_open" => Function::new_typed_with_env(&mut store, env, sock_open::<Memory32>),
621        "sock_pair" => Function::new_typed_with_env(&mut store, env, sock_pair::<Memory32>),
622        "sock_set_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_set_opt_flag),
623        "sock_get_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_get_opt_flag::<Memory32>),
624        "sock_set_opt_time" => Function::new_typed_with_env(&mut store, env, sock_set_opt_time::<Memory32>),
625        "sock_get_opt_time" => Function::new_typed_with_env(&mut store, env, sock_get_opt_time::<Memory32>),
626        "sock_set_opt_size" => Function::new_typed_with_env(&mut store, env, sock_set_opt_size),
627        "sock_get_opt_size" => Function::new_typed_with_env(&mut store, env, sock_get_opt_size::<Memory32>),
628        "sock_join_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v4::<Memory32>),
629        "sock_leave_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v4::<Memory32>),
630        "sock_join_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v6::<Memory32>),
631        "sock_leave_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v6::<Memory32>),
632        "sock_bind" => Function::new_typed_with_env(&mut store, env, sock_bind::<Memory32>),
633        "sock_listen" => Function::new_typed_with_env(&mut store, env, sock_listen::<Memory32>),
634        "sock_accept" => Function::new_typed_with_env(&mut store, env, sock_accept_v2::<Memory32>),
635        "sock_accept_v2" => Function::new_typed_with_env(&mut store, env, sock_accept_v2::<Memory32>),
636        "sock_connect" => Function::new_typed_with_env(&mut store, env, sock_connect::<Memory32>),
637        "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv::<Memory32>),
638        "sock_recv_from" => Function::new_typed_with_env(&mut store, env, sock_recv_from::<Memory32>),
639        "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send::<Memory32>),
640        "sock_send_to" => Function::new_typed_with_env(&mut store, env, sock_send_to::<Memory32>),
641        "sock_send_file" => Function::new_typed_with_env(&mut store, env, sock_send_file::<Memory32>),
642        "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown),
643        "resolve" => Function::new_typed_with_env(&mut store, env, resolve::<Memory32>),
644    };
645    namespace
646}
647
648fn wasix_exports_64(mut store: &mut impl AsStoreMut, env: &FunctionEnv<WasiEnv>) -> Exports {
649    let engine_supports_async = store.as_store_ref().engine().supports_async();
650
651    use syscalls::*;
652    let namespace = namespace! {
653        "args_get" => Function::new_typed_with_env(&mut store, env, args_get::<Memory64>),
654        "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get::<Memory64>),
655        "call_dynamic" => Function::new_typed_with_env(&mut store, env, call_dynamic::<Memory64>),
656        "reflect_signature" => Function::new_typed_with_env(&mut store, env, reflect_signature::<Memory64>),
657        "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get::<Memory64>),
658        "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get::<Memory64>),
659        "clock_time_set" => Function::new_typed_with_env(&mut store, env, clock_time_set),
660        "closure_prepare" => Function::new_typed_with_env(&mut store, env, closure_prepare::<Memory64>),
661        "closure_allocate" => Function::new_typed_with_env(&mut store, env, closure_allocate::<Memory64>),
662        "closure_free" => Function::new_typed_with_env(&mut store, env, closure_free),
663        "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get::<Memory64>),
664        "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get::<Memory64>),
665        "epoll_create" => Function::new_typed_with_env(&mut store, env, epoll_create::<Memory64>),
666        "epoll_ctl" => Function::new_typed_with_env(&mut store, env, epoll_ctl::<Memory64>),
667        "epoll_wait" => Function::new_typed_with_env(&mut store, env, epoll_wait::<Memory64>),
668        "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise),
669        "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate),
670        "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close),
671        "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync),
672        "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get::<Memory64>),
673        "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags),
674        "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights),
675        "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, fd_filestat_get::<Memory64>),
676        "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size),
677        "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times),
678        "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread::<Memory64>),
679        "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get::<Memory64>),
680        "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name::<Memory64>),
681        "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite::<Memory64>),
682        "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read::<Memory64>),
683        "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir::<Memory64>),
684        "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber),
685        "fd_dup" => Function::new_typed_with_env(&mut store, env, fd_dup::<Memory64>),
686        "fd_dup2" => Function::new_typed_with_env(&mut store, env, fd_dup2::<Memory64>),
687        "fd_fdflags_get" => Function::new_typed_with_env(&mut store, env, fd_fdflags_get::<Memory64>),
688        "fd_fdflags_set" => Function::new_typed_with_env(&mut store, env, fd_fdflags_set),
689        "fd_event" => Function::new_typed_with_env(&mut store, env, fd_event::<Memory64>),
690        "fd_seek" => Function::new_typed_with_env(&mut store, env, fd_seek::<Memory64>),
691        "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync),
692        "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell::<Memory64>),
693        "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write::<Memory64>),
694        "fd_pipe" => Function::new_typed_with_env(&mut store, env, fd_pipe::<Memory64>),
695        "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory::<Memory64>),
696        "path_filestat_get" => Function::new_typed_with_env(&mut store, env, path_filestat_get::<Memory64>),
697        "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times::<Memory64>),
698        "path_link" => Function::new_typed_with_env(&mut store, env, path_link::<Memory64>),
699        "path_open" => Function::new_typed_with_env(&mut store, env, path_open::<Memory64>),
700        "path_open2" => Function::new_typed_with_env(&mut store, env, path_open2::<Memory64>),
701        "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink::<Memory64>),
702        "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory::<Memory64>),
703        "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename::<Memory64>),
704        "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink::<Memory64>),
705        "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file::<Memory64>),
706        "poll_oneoff" => Function::new_typed_with_env(&mut store, env, poll_oneoff::<Memory64>),
707        "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit::<Memory64>),
708        "proc_fork" => Function::new_typed_with_env(&mut store, env, proc_fork::<Memory64>),
709        "proc_fork_env" => Function::new_typed_with_env(&mut store, env, proc_fork_env::<Memory64>),
710        "proc_join" => Function::new_typed_with_env(&mut store, env, proc_join::<Memory64>),
711        "proc_signal" => Function::new_typed_with_env(&mut store, env, proc_signal),
712        "proc_signals_get" => Function::new_typed_with_env(&mut store, env, proc_signals_get::<Memory64>),
713        "proc_signals_sizes_get" => Function::new_typed_with_env(&mut store, env, proc_signals_sizes_get::<Memory64>),
714        "proc_exec" => Function::new_typed_with_env(&mut store, env, proc_exec::<Memory64>),
715        "proc_exec2" => Function::new_typed_with_env(&mut store, env, proc_exec2::<Memory64>),
716        "proc_exec3" => Function::new_typed_with_env(&mut store, env, proc_exec3::<Memory64>),
717        "proc_exit2" => Function::new_typed_with_env(&mut store, env, proc_exit2::<Memory64>),
718        "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
719        "proc_raise_interval" => Function::new_typed_with_env(&mut store, env, proc_raise_interval),
720        "proc_snapshot" => Function::new_typed_with_env(&mut store, env, proc_snapshot::<Memory64>),
721        "proc_spawn" => Function::new_typed_with_env(&mut store, env, proc_spawn::<Memory64>),
722        "proc_spawn2" => Function::new_typed_with_env(&mut store, env, proc_spawn2::<Memory64>),
723        "proc_id" => Function::new_typed_with_env(&mut store, env, proc_id::<Memory64>),
724        "proc_parent" => Function::new_typed_with_env(&mut store, env, proc_parent::<Memory64>),
725        "random_get" => Function::new_typed_with_env(&mut store, env, random_get::<Memory64>),
726        "tty_get" => Function::new_typed_with_env(&mut store, env, tty_get::<Memory64>),
727        "tty_set" => Function::new_typed_with_env(&mut store, env, tty_set::<Memory64>),
728        "getcwd" => Function::new_typed_with_env(&mut store, env, getcwd::<Memory64>),
729        "chdir" => Function::new_typed_with_env(&mut store, env, chdir::<Memory64>),
730        "dl_invalid_handle" => Function::new_typed_with_env(&mut store, env, dl_invalid_handle),
731        "dlopen" => Function::new_typed_with_env(&mut store, env, dlopen::<Memory64>),
732        "dlsym" => Function::new_typed_with_env(&mut store, env, dlsym::<Memory64>),
733        "callback_signal" => Function::new_typed_with_env(&mut store, env, callback_signal::<Memory64>),
734        "thread_spawn" => Function::new_typed_with_env(&mut store, env, thread_spawn_v2::<Memory64>),
735        "thread_spawn_v2" => Function::new_typed_with_env(&mut store, env, thread_spawn_v2::<Memory64>),
736        "thread_sleep" => Function::new_typed_with_env(&mut store, env, thread_sleep::<Memory64>),
737        "thread_id" => Function::new_typed_with_env(&mut store, env, thread_id::<Memory64>),
738        "thread_signal" => Function::new_typed_with_env(&mut store, env, thread_signal),
739        "thread_join" => Function::new_typed_with_env(&mut store, env, thread_join::<Memory64>),
740        "thread_parallelism" => Function::new_typed_with_env(&mut store, env, thread_parallelism::<Memory64>),
741        "thread_exit" => Function::new_typed_with_env(&mut store, env, thread_exit),
742        "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield::<Memory64>),
743        "stack_checkpoint" => Function::new_typed_with_env(&mut store, env, stack_checkpoint::<Memory64>),
744        "stack_restore" => Function::new_typed_with_env(&mut store, env, stack_restore::<Memory64>),
745        "context_create" => Function::new_typed_with_env(&mut store, env, context_create::<Memory64>),
746        "context_switch" => if engine_supports_async { Function::new_typed_with_env_async(&mut store, env, context_switch) } else { Function::new_typed_with_env(&mut store, env, context_switch_not_supported) },
747        "context_destroy" => Function::new_typed_with_env(&mut store, env, context_destroy),
748        "futex_wait" => Function::new_typed_with_env(&mut store, env, futex_wait::<Memory64>),
749        "futex_wake" => Function::new_typed_with_env(&mut store, env, futex_wake::<Memory64>),
750        "futex_wake_all" => Function::new_typed_with_env(&mut store, env, futex_wake_all::<Memory64>),
751        "port_bridge" => Function::new_typed_with_env(&mut store, env, port_bridge::<Memory64>),
752        "port_unbridge" => Function::new_typed_with_env(&mut store, env, port_unbridge),
753        "port_dhcp_acquire" => Function::new_typed_with_env(&mut store, env, port_dhcp_acquire),
754        "port_addr_add" => Function::new_typed_with_env(&mut store, env, port_addr_add::<Memory64>),
755        "port_addr_remove" => Function::new_typed_with_env(&mut store, env, port_addr_remove::<Memory64>),
756        "port_addr_clear" => Function::new_typed_with_env(&mut store, env, port_addr_clear),
757        "port_addr_list" => Function::new_typed_with_env(&mut store, env, port_addr_list::<Memory64>),
758        "port_mac" => Function::new_typed_with_env(&mut store, env, port_mac::<Memory64>),
759        "port_gateway_set" => Function::new_typed_with_env(&mut store, env, port_gateway_set::<Memory64>),
760        "port_route_add" => Function::new_typed_with_env(&mut store, env, port_route_add::<Memory64>),
761        "port_route_remove" => Function::new_typed_with_env(&mut store, env, port_route_remove::<Memory64>),
762        "port_route_clear" => Function::new_typed_with_env(&mut store, env, port_route_clear),
763        "port_route_list" => Function::new_typed_with_env(&mut store, env, port_route_list::<Memory64>),
764        "sock_status" => Function::new_typed_with_env(&mut store, env, sock_status::<Memory64>),
765        "sock_addr_local" => Function::new_typed_with_env(&mut store, env, sock_addr_local::<Memory64>),
766        "sock_addr_peer" => Function::new_typed_with_env(&mut store, env, sock_addr_peer::<Memory64>),
767        "sock_open" => Function::new_typed_with_env(&mut store, env, sock_open::<Memory64>),
768        "sock_pair" => Function::new_typed_with_env(&mut store, env, sock_pair::<Memory64>),
769        "sock_set_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_set_opt_flag),
770        "sock_get_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_get_opt_flag::<Memory64>),
771        "sock_set_opt_time" => Function::new_typed_with_env(&mut store, env, sock_set_opt_time::<Memory64>),
772        "sock_get_opt_time" => Function::new_typed_with_env(&mut store, env, sock_get_opt_time::<Memory64>),
773        "sock_set_opt_size" => Function::new_typed_with_env(&mut store, env, sock_set_opt_size),
774        "sock_get_opt_size" => Function::new_typed_with_env(&mut store, env, sock_get_opt_size::<Memory64>),
775        "sock_join_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v4::<Memory64>),
776        "sock_leave_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v4::<Memory64>),
777        "sock_join_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v6::<Memory64>),
778        "sock_leave_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v6::<Memory64>),
779        "sock_bind" => Function::new_typed_with_env(&mut store, env, sock_bind::<Memory64>),
780        "sock_listen" => Function::new_typed_with_env(&mut store, env, sock_listen::<Memory64>),
781        "sock_accept" => Function::new_typed_with_env(&mut store, env, sock_accept_v2::<Memory64>),
782        "sock_accept_v2" => Function::new_typed_with_env(&mut store, env, sock_accept_v2::<Memory64>),
783        "sock_connect" => Function::new_typed_with_env(&mut store, env, sock_connect::<Memory64>),
784        "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv::<Memory64>),
785        "sock_recv_from" => Function::new_typed_with_env(&mut store, env, sock_recv_from::<Memory64>),
786        "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send::<Memory64>),
787        "sock_send_to" => Function::new_typed_with_env(&mut store, env, sock_send_to::<Memory64>),
788        "sock_send_file" => Function::new_typed_with_env(&mut store, env, sock_send_file::<Memory64>),
789        "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown),
790        "resolve" => Function::new_typed_with_env(&mut store, env, resolve::<Memory64>),
791    };
792    namespace
793}
794
795// TODO: split function into two variants, one for JS and one for sys.
796// (this will make code less messy)
797fn import_object_for_all_wasi_versions(
798    _module: &wasmer::Module,
799    store: &mut impl AsStoreMut,
800    env: &FunctionEnv<WasiEnv>,
801) -> Imports {
802    let exports_wasi_generic = wasi_exports_generic(store, env);
803    let exports_wasi_unstable = wasi_unstable_exports(store, env);
804    let exports_wasi_snapshot_preview1 = wasi_snapshot_preview1_exports(store, env);
805    let exports_wasix_32v1 = wasix_exports_32(store, env);
806    let exports_wasix_64v1 = wasix_exports_64(store, env);
807
808    // Allowed due to JS feature flag complications.
809    #[allow(unused_mut)]
810    let mut imports = imports! {
811        "wasi" => exports_wasi_generic,
812        "wasi_unstable" => exports_wasi_unstable,
813        "wasi_snapshot_preview1" => exports_wasi_snapshot_preview1,
814        "wasix_32v1" => exports_wasix_32v1,
815        "wasix_64v1" => exports_wasix_64v1,
816    };
817
818    imports
819}
820
821/// Combines a state generating function with the import list for legacy WASI
822fn generate_import_object_snapshot0(
823    store: &mut impl AsStoreMut,
824    env: &FunctionEnv<WasiEnv>,
825) -> Imports {
826    let exports_unstable = wasi_unstable_exports(store, env);
827    imports! {
828        "wasi_unstable" => exports_unstable
829    }
830}
831
832fn generate_import_object_snapshot1(
833    store: &mut impl AsStoreMut,
834    env: &FunctionEnv<WasiEnv>,
835) -> Imports {
836    let exports_wasi_snapshot_preview1 = wasi_snapshot_preview1_exports(store, env);
837    imports! {
838        "wasi_snapshot_preview1" => exports_wasi_snapshot_preview1
839    }
840}
841
842/// Combines a state generating function with the import list for snapshot 1
843fn generate_import_object_wasix32_v1(
844    store: &mut impl AsStoreMut,
845    env: &FunctionEnv<WasiEnv>,
846) -> Imports {
847    let exports_wasix_32v1 = wasix_exports_32(store, env);
848    imports! {
849        "wasix_32v1" => exports_wasix_32v1
850    }
851}
852
853fn generate_import_object_wasix64_v1(
854    store: &mut impl AsStoreMut,
855    env: &FunctionEnv<WasiEnv>,
856) -> Imports {
857    let exports_wasix_64v1 = wasix_exports_64(store, env);
858    imports! {
859        "wasix_64v1" => exports_wasix_64v1
860    }
861}
862
863fn mem_error_to_wasi(err: MemoryAccessError) -> Errno {
864    match err {
865        MemoryAccessError::HeapOutOfBounds => Errno::Memviolation,
866        MemoryAccessError::Overflow => Errno::Overflow,
867        MemoryAccessError::NonUtf8String => Errno::Inval,
868        _ => Errno::Unknown,
869    }
870}
871
872/// Run a synchronous function that would normally be blocking.
873///
874/// When the `sys-thread` feature is enabled, this will call
875/// [`tokio::task::block_in_place()`]. Otherwise, it calls the function
876/// immediately.
877pub(crate) fn block_in_place<Ret>(thunk: impl FnOnce() -> Ret) -> Ret {
878    cfg_if::cfg_if! {
879        if #[cfg(feature = "sys-thread")] {
880            tokio::task::block_in_place(thunk)
881        } else {
882            thunk()
883        }
884    }
885}
886
887/// Spawns a new blocking task that runs the provided closure.
888///
889/// The closure is executed on a separate thread, allowing it to perform blocking operations
890/// without blocking the main thread. The closure is wrapped in a `Future` that resolves to the
891/// result of the closure's execution.
892pub(crate) async fn spawn_blocking<F, R>(f: F) -> Result<R, tokio::task::JoinError>
893where
894    F: FnOnce() -> R + Send + 'static,
895    R: Send + 'static,
896{
897    cfg_if::cfg_if! {
898        if #[cfg(target_arch = "wasm32")] {
899            Ok(block_in_place(f))
900        } else {
901            tokio::task::spawn_blocking(f).await
902        }
903    }
904}
905
906pub(crate) fn flatten_runtime_error(err: RuntimeError) -> RuntimeError {
907    let e_ref = err.downcast_ref::<WasiRuntimeError>();
908    match e_ref {
909        Some(WasiRuntimeError::Wasi(_)) => {
910            let Ok(WasiRuntimeError::Wasi(err)) = err.downcast::<WasiRuntimeError>() else {
911                unreachable!()
912            };
913            RuntimeError::user(Box::new(err))
914        }
915        Some(WasiRuntimeError::Runtime(_)) => {
916            let Ok(WasiRuntimeError::Runtime(err)) = err.downcast::<WasiRuntimeError>() else {
917                unreachable!()
918            };
919            flatten_runtime_error(err)
920        }
921        _ => err,
922    }
923}