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