1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
#![deny(unused_mut)]
#![doc(html_favicon_url = "https://wasmer.io/images/icons/favicon-32x32.png")]
#![doc(html_logo_url = "https://github.com/wasmerio.png?size=200")]

//! Wasmer's WASI implementation
//!
//! Use `generate_import_object` to create an [`Imports`].  This [`Imports`]
//! can be combined with a module to create an `Instance` which can execute WASI
//! Wasm functions.
//!
//! See `state` for the experimental WASI FS API.  Also see the
//! [WASI plugin example](https://github.com/wasmerio/wasmer/blob/master/examples/plugin.rs)
//! for an example of how to extend WASI using the WASI FS API.

#[cfg(all(not(feature = "sys"), not(feature = "js")))]
compile_error!("At least the `sys` or the `js` feature must be enabled. Please, pick one.");

#[cfg(all(feature = "sys", feature = "js"))]
compile_error!(
    "Cannot have both `sys` and `js` features enabled at the same time. Please, pick one."
);

#[cfg(all(feature = "sys", target_arch = "wasm32"))]
compile_error!("The `sys` feature must be enabled only for non-`wasm32` target.");

#[cfg(all(feature = "js", not(target_arch = "wasm32")))]
compile_error!(
    "The `js` feature must be enabled only for the `wasm32` target (either `wasm32-unknown-unknown` or `wasm32-wasi`)."
);

#[cfg(all(feature = "host-fs", feature = "mem-fs"))]
compile_error!(
    "Cannot have both `host-fs` and `mem-fs` features enabled at the same time. Please, pick one."
);

#[macro_use]
mod macros;
mod runtime;
mod state;
mod syscalls;
mod utils;

/// Runners for WASI / Emscripten
#[cfg(feature = "webc_runner")]
pub mod runners;

use crate::syscalls::*;

pub use crate::state::{
    Fd, Pipe, Stderr, Stdin, Stdout, WasiFs, WasiInodes, WasiState, WasiStateBuilder,
    WasiStateCreationError, ALL_RIGHTS, VIRTUAL_ROOT_FD,
};
pub use crate::syscalls::types;
#[cfg(feature = "wasix")]
pub use crate::utils::is_wasix_module;
pub use crate::utils::wasi_import_shared_memory;
pub use crate::utils::{get_wasi_version, get_wasi_versions, is_wasi_module, WasiVersion};

pub use wasmer_vbus::{UnsupportedVirtualBus, VirtualBus};
#[deprecated(since = "2.1.0", note = "Please use `wasmer_vfs::FsError`")]
pub use wasmer_vfs::FsError as WasiFsError;
#[deprecated(since = "2.1.0", note = "Please use `wasmer_vfs::VirtualFile`")]
pub use wasmer_vfs::VirtualFile as WasiFile;
pub use wasmer_vfs::{FsError, VirtualFile};
pub use wasmer_vnet::{UnsupportedVirtualNetworking, VirtualNetworking};

use derivative::*;
use std::ops::Deref;
use thiserror::Error;
use tracing::trace;
use wasmer::{
    imports, namespace, AsStoreMut, AsStoreRef, ExportError, Exports, Function, FunctionEnv,
    Imports, Instance, Memory, Memory32, MemoryAccessError, MemorySize, MemoryView, Module,
    TypedFunction,
};
use wasmer_wasi_types::wasi::{BusErrno, Errno, Snapshot0Clockid};

pub use runtime::{
    PluggableRuntimeImplementation, WasiRuntimeImplementation, WasiThreadError, WasiTtyState,
};
use std::sync::{mpsc, Arc, Mutex, RwLockReadGuard, RwLockWriteGuard};
use std::time::Duration;

/// This is returned in `RuntimeError`.
/// Use `downcast` or `downcast_ref` to retrieve the `ExitCode`.
#[derive(Error, Debug)]
pub enum WasiError {
    #[error("WASI exited with code: {0}")]
    Exit(syscalls::types::__wasi_exitcode_t),
    #[error("The WASI version could not be determined")]
    UnknownWasiVersion,
}

/// Represents the ID of a WASI thread
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WasiThreadId(u32);

impl From<u32> for WasiThreadId {
    fn from(id: u32) -> Self {
        Self(id)
    }
}
impl From<WasiThreadId> for u32 {
    fn from(t: WasiThreadId) -> u32 {
        t.0 as u32
    }
}

/// Represents the ID of a sub-process
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WasiBusProcessId(u32);

impl From<u32> for WasiBusProcessId {
    fn from(id: u32) -> Self {
        Self(id)
    }
}
impl From<WasiBusProcessId> for u32 {
    fn from(id: WasiBusProcessId) -> u32 {
        id.0 as u32
    }
}

#[derive(Debug, Clone)]
pub struct WasiThread {
    /// ID of this thread
    #[allow(dead_code)]
    id: WasiThreadId,
    /// Signalers used to tell joiners that the thread has exited
    exit: Arc<Mutex<Option<mpsc::Sender<()>>>>,
    /// Event to wait on for the thread to join
    join: Arc<Mutex<mpsc::Receiver<()>>>,
}

impl WasiThread {
    /// Waits for the thread to exit (false = timeout)
    pub fn join(&self, timeout: Duration) -> bool {
        let guard = self.join.lock().unwrap();
        let timeout = guard.recv_timeout(timeout);
        match timeout {
            Ok(_) => true,
            Err(mpsc::RecvTimeoutError::Disconnected) => true,
            Err(mpsc::RecvTimeoutError::Timeout) => false,
        }
    }
}

pub struct WasiFunctionEnv {
    pub env: FunctionEnv<WasiEnv>,
}

impl WasiFunctionEnv {
    pub fn new(store: &mut impl AsStoreMut, env: WasiEnv) -> Self {
        Self {
            env: FunctionEnv::new(store, env),
        }
    }

    /// Get an `Imports` for a specific version of WASI detected in the module.
    pub fn import_object(
        &self,
        store: &mut impl AsStoreMut,
        module: &Module,
    ) -> Result<Imports, WasiError> {
        let wasi_version = get_wasi_version(module, false).ok_or(WasiError::UnknownWasiVersion)?;
        Ok(generate_import_object_from_env(
            store,
            &self.env,
            wasi_version,
        ))
    }

    pub fn data_mut<'a>(&'a self, store: &'a mut impl AsStoreMut) -> &'a mut WasiEnv {
        self.env.as_mut(store)
    }

    /// Initializes the WasiEnv using the instance exports
    /// (this must be executed before attempting to use it)
    /// (as the stores can not by themselves be passed between threads we can store the module
    ///  in a thread-local variables and use it later - for multithreading)
    pub fn initialize(
        &mut self,
        store: &mut impl AsStoreMut,
        instance: &Instance,
    ) -> Result<(), ExportError> {
        // List all the exports and imports
        for ns in instance.module().exports() {
            //trace!("module::export - {} ({:?})", ns.name(), ns.ty());
            trace!("module::export - {}", ns.name());
        }
        for ns in instance.module().imports() {
            trace!("module::import - {}::{}", ns.module(), ns.name());
        }

        // First we get the malloc function which if it exists will be used to
        // create the pthread_self structure
        let memory = instance.exports.get_memory("memory")?.clone();
        let env = self.data_mut(store);
        env.set_memory(memory);

        Ok(())
    }

    /// Like `import_object` but containing all the WASI versions detected in
    /// the module.
    pub fn import_object_for_all_wasi_versions(
        &self,
        store: &mut impl AsStoreMut,
        module: &Module,
    ) -> Result<Imports, WasiError> {
        let wasi_versions =
            get_wasi_versions(module, false).ok_or(WasiError::UnknownWasiVersion)?;

        let mut resolver = Imports::new();
        for version in wasi_versions.iter() {
            let new_import_object = generate_import_object_from_env(store, &self.env, *version);
            for ((n, m), e) in new_import_object.into_iter() {
                resolver.define(&n, &m, e);
            }
        }

        #[cfg(feature = "wasix")]
        if is_wasix_module(module) {
            self.data_mut(store)
                .state
                .fs
                .is_wasix
                .store(true, std::sync::atomic::Ordering::Release);
        }

        Ok(resolver)
    }
}

/// The environment provided to the WASI imports.
#[derive(Derivative, Clone)]
#[derivative(Debug)]
#[allow(dead_code)]
pub struct WasiEnv {
    /// ID of this thread (zero is the main thread)
    id: WasiThreadId,
    /// Represents a reference to the memory
    memory: Option<Memory>,
    /// If the module has it then map the thread start
    #[derivative(Debug = "ignore")]
    thread_start: Option<TypedFunction<u64, ()>>,
    #[derivative(Debug = "ignore")]
    reactor_work: Option<TypedFunction<u64, ()>>,
    #[derivative(Debug = "ignore")]
    reactor_finish: Option<TypedFunction<u64, ()>>,
    #[derivative(Debug = "ignore")]
    malloc: Option<TypedFunction<u64, u64>>,
    #[derivative(Debug = "ignore")]
    free: Option<TypedFunction<(u64, u64), ()>>,
    /// Shared state of the WASI system. Manages all the data that the
    /// executing WASI program can see.
    pub state: Arc<WasiState>,
    /// Implementation of the WASI runtime.
    pub(crate) runtime: Arc<dyn WasiRuntimeImplementation + Send + Sync + 'static>,
}

impl WasiEnv {
    /// Create a new WasiEnv from a WasiState (memory will be set to None)
    pub fn new(state: WasiState) -> Self {
        Self {
            id: 0u32.into(),
            state: Arc::new(state),
            memory: None,
            thread_start: None,
            reactor_work: None,
            reactor_finish: None,
            malloc: None,
            free: None,
            runtime: Arc::new(PluggableRuntimeImplementation::default()),
        }
    }

    /// Returns a copy of the current runtime implementation for this environment
    pub fn runtime(&self) -> &(dyn WasiRuntimeImplementation) {
        self.runtime.deref()
    }

    /// Overrides the runtime implementation for this environment
    pub fn set_runtime<R>(&mut self, runtime: R)
    where
        R: WasiRuntimeImplementation + Send + Sync + 'static,
    {
        self.runtime = Arc::new(runtime);
    }

    /// Returns the current thread ID
    pub fn current_thread_id(&self) -> WasiThreadId {
        self.id
    }

    /// Creates a new thread only this wasi environment
    pub fn new_thread(&self) -> WasiThread {
        let (tx, rx) = mpsc::channel();

        let mut guard = self.state.threading.lock().unwrap();

        guard.thread_seed += 1;
        let next_id: WasiThreadId = guard.thread_seed.into();

        let thread = WasiThread {
            id: next_id,
            exit: Arc::new(Mutex::new(Some(tx))),
            join: Arc::new(Mutex::new(rx)),
        };

        guard.threads.insert(thread.id, thread.clone());
        thread
    }

    /// Copy the lazy reference so that when it's initialized during the
    /// export phase, all the other references get a copy of it
    pub fn memory_clone(&self) -> Option<Memory> {
        self.memory.clone()
    }

    // Yields execution
    pub fn yield_now(&self) -> Result<(), WasiError> {
        self.runtime.yield_now(self.id)?;
        Ok(())
    }

    // Sleeps for a period of time
    pub fn sleep(&self, duration: Duration) -> Result<(), WasiError> {
        let duration = duration.as_nanos();
        let start =
            platform_clock_time_get(Snapshot0Clockid::Monotonic, 1_000_000).unwrap() as u128;
        self.yield_now()?;
        loop {
            let now =
                platform_clock_time_get(Snapshot0Clockid::Monotonic, 1_000_000).unwrap() as u128;
            let delta = match now.checked_sub(start) {
                Some(a) => a,
                None => {
                    break;
                }
            };
            if delta >= duration {
                break;
            }
            let remaining = match duration.checked_sub(delta) {
                Some(a) => Duration::from_nanos(a as u64),
                None => {
                    break;
                }
            };
            std::thread::sleep(remaining.min(Duration::from_millis(10)));
            self.yield_now()?;
        }
        Ok(())
    }

    /// Accesses the virtual networking implementation
    pub fn net(&self) -> &(dyn VirtualNetworking) {
        self.runtime.networking()
    }

    /// Accesses the virtual bus implementation
    pub fn bus(&self) -> &(dyn VirtualBus) {
        self.runtime.bus()
    }

    /// Set the memory of the WasiEnv (can only be done once)
    pub fn set_memory(&mut self, memory: Memory) {
        if self.memory.is_some() {
            panic!("Memory of a WasiEnv can only be set once!");
        }
        self.memory = Some(memory);
    }

    /// Providers safe access to the memory
    /// (it must be initialized before it can be used)
    pub fn memory_view<'a>(&'a self, store: &'a impl AsStoreRef) -> MemoryView<'a> {
        self.memory().view(store)
    }

    /// Get memory, that needs to have been set fist
    pub fn memory(&self) -> &Memory {
        self.memory.as_ref().unwrap()
    }

    /// Get the WASI state
    pub fn state(&self) -> &WasiState {
        &self.state
    }

    pub(crate) fn get_memory_and_wasi_state<'a>(
        &'a self,
        store: &'a impl AsStoreRef,
        _mem_index: u32,
    ) -> (MemoryView<'a>, &WasiState) {
        let memory = self.memory_view(store);
        let state = self.state.deref();
        (memory, state)
    }

    pub(crate) fn get_memory_and_wasi_state_and_inodes<'a>(
        &'a self,
        store: &'a impl AsStoreRef,
        _mem_index: u32,
    ) -> (MemoryView<'a>, &WasiState, RwLockReadGuard<WasiInodes>) {
        let memory = self.memory_view(store);
        let state = self.state.deref();
        let inodes = state.inodes.read().unwrap();
        (memory, state, inodes)
    }

    pub(crate) fn get_memory_and_wasi_state_and_inodes_mut<'a>(
        &'a self,
        store: &'a impl AsStoreRef,
        _mem_index: u32,
    ) -> (MemoryView<'a>, &WasiState, RwLockWriteGuard<WasiInodes>) {
        let memory = self.memory_view(store);
        let state = self.state.deref();
        let inodes = state.inodes.write().unwrap();
        (memory, state, inodes)
    }
}

/// Create an [`Imports`]  from a [`Context`]
pub fn generate_import_object_from_env(
    store: &mut impl AsStoreMut,
    env: &FunctionEnv<WasiEnv>,
    version: WasiVersion,
) -> Imports {
    match version {
        WasiVersion::Snapshot0 => generate_import_object_snapshot0(store, env),
        WasiVersion::Snapshot1 | WasiVersion::Latest => {
            generate_import_object_snapshot1(store, env)
        }
        #[cfg(feature = "wasix")]
        WasiVersion::Wasix32v1 => generate_import_object_wasix32_v1(store, env),
        #[cfg(feature = "wasix")]
        WasiVersion::Wasix64v1 => generate_import_object_wasix64_v1(store, env),
        #[cfg(not(feature = "wasix"))]
        _ => unimplemented!(),
    }
}

fn wasi_unstable_exports(mut store: &mut impl AsStoreMut, env: &FunctionEnv<WasiEnv>) -> Exports {
    let namespace = namespace! {
        "args_get" => Function::new_typed_with_env(&mut store, env, args_get::<Memory32>),
        "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get::<Memory32>),
        "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get::<Memory32>),
        "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get::<Memory32>),
        "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get::<Memory32>),
        "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get::<Memory32>),
        "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise),
        "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate),
        "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close),
        "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync),
        "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get::<Memory32>),
        "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags),
        "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights),
        "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::fd_filestat_get),
        "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size),
        "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times),
        "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread::<Memory32>),
        "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get::<Memory32>),
        "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name::<Memory32>),
        "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite::<Memory32>),
        "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read::<Memory32>),
        "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir::<Memory32>),
        "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber),
        "fd_seek" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::fd_seek),
        "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync),
        "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell::<Memory32>),
        "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write::<Memory32>),
        "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory::<Memory32>),
        "path_filestat_get" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::path_filestat_get),
        "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times::<Memory32>),
        "path_link" => Function::new_typed_with_env(&mut store, env, path_link::<Memory32>),
        "path_open" => Function::new_typed_with_env(&mut store, env, path_open::<Memory32>),
        "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink::<Memory32>),
        "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory::<Memory32>),
        "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename::<Memory32>),
        "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink::<Memory32>),
        "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file::<Memory32>),
        "poll_oneoff" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::poll_oneoff),
        "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit),
        "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
        "random_get" => Function::new_typed_with_env(&mut store, env, random_get::<Memory32>),
        "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield),
        "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv::<Memory32>),
        "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send::<Memory32>),
        "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown),
    };
    namespace
}

fn wasi_snapshot_preview1_exports(
    mut store: &mut impl AsStoreMut,
    env: &FunctionEnv<WasiEnv>,
) -> Exports {
    let namespace = namespace! {
        "args_get" => Function::new_typed_with_env(&mut store, env, args_get::<Memory32>),
        "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get::<Memory32>),
        "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get::<Memory32>),
        "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get::<Memory32>),
        "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get::<Memory32>),
        "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get::<Memory32>),
        "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise),
        "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate),
        "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close),
        "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync),
        "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get::<Memory32>),
        "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags),
        "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights),
        "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, fd_filestat_get::<Memory32>),
        "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size),
        "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times),
        "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread::<Memory32>),
        "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get::<Memory32>),
        "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name::<Memory32>),
        "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite::<Memory32>),
        "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read::<Memory32>),
        "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir::<Memory32>),
        "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber),
        "fd_seek" => Function::new_typed_with_env(&mut store, env, fd_seek::<Memory32>),
        "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync),
        "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell::<Memory32>),
        "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write::<Memory32>),
        "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory::<Memory32>),
        "path_filestat_get" => Function::new_typed_with_env(&mut store, env, path_filestat_get::<Memory32>),
        "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times::<Memory32>),
        "path_link" => Function::new_typed_with_env(&mut store, env, path_link::<Memory32>),
        "path_open" => Function::new_typed_with_env(&mut store, env, path_open::<Memory32>),
        "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink::<Memory32>),
        "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory::<Memory32>),
        "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename::<Memory32>),
        "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink::<Memory32>),
        "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file::<Memory32>),
        "poll_oneoff" => Function::new_typed_with_env(&mut store, env, poll_oneoff::<Memory32>),
        "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit),
        "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
        "random_get" => Function::new_typed_with_env(&mut store, env, random_get::<Memory32>),
        "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield),
        "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv::<Memory32>),
        "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send::<Memory32>),
        "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown),
    };
    namespace
}
pub fn import_object_for_all_wasi_versions(
    store: &mut impl AsStoreMut,
    env: &FunctionEnv<WasiEnv>,
) -> Imports {
    let wasi_unstable_exports = wasi_unstable_exports(store, env);
    let wasi_snapshot_preview1_exports = wasi_snapshot_preview1_exports(store, env);
    imports! {
        "wasi_unstable" => wasi_unstable_exports,
        "wasi_snapshot_preview1" => wasi_snapshot_preview1_exports,
    }
}

/// Combines a state generating function with the import list for legacy WASI
fn generate_import_object_snapshot0(
    store: &mut impl AsStoreMut,
    env: &FunctionEnv<WasiEnv>,
) -> Imports {
    let wasi_unstable_exports = wasi_unstable_exports(store, env);
    imports! {
        "wasi_unstable" => wasi_unstable_exports
    }
}

fn generate_import_object_snapshot1(
    store: &mut impl AsStoreMut,
    env: &FunctionEnv<WasiEnv>,
) -> Imports {
    let wasi_snapshot_preview1_exports = wasi_snapshot_preview1_exports(store, env);
    imports! {
        "wasi_snapshot_preview1" => wasi_snapshot_preview1_exports
    }
}

/// Combines a state generating function with the import list for snapshot 1
#[cfg(feature = "wasix")]
fn generate_import_object_wasix32_v1(
    mut store: &mut impl AsStoreMut,
    env: &FunctionEnv<WasiEnv>,
) -> Imports {
    use self::wasix32::*;
    imports! {
        "wasix_32v1" => {
            "args_get" => Function::new_typed_with_env(&mut store, env, args_get),
            "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get),
            "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get),
            "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get),
            "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get),
            "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get),
            "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise),
            "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate),
            "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close),
            "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync),
            "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get),
            "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags),
            "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights),
            "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, fd_filestat_get),
            "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size),
            "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times),
            "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread),
            "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get),
            "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name),
            "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite),
            "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read),
            "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir),
            "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber),
            "fd_dup" => Function::new_typed_with_env(&mut store, env, fd_dup),
            "fd_event" => Function::new_typed_with_env(&mut store, env, fd_event),
            "fd_seek" => Function::new_typed_with_env(&mut store, env, fd_seek),
            "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync),
            "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell),
            "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write),
            "fd_pipe" => Function::new_typed_with_env(&mut store, env, fd_pipe),
            "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory),
            "path_filestat_get" => Function::new_typed_with_env(&mut store, env, path_filestat_get),
            "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times),
            "path_link" => Function::new_typed_with_env(&mut store, env, path_link),
            "path_open" => Function::new_typed_with_env(&mut store, env, path_open),
            "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink),
            "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory),
            "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename),
            "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink),
            "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file),
            "poll_oneoff" => Function::new_typed_with_env(&mut store, env, poll_oneoff),
            "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit),
            "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
            "random_get" => Function::new_typed_with_env(&mut store, env, random_get),
            "tty_get" => Function::new_typed_with_env(&mut store, env, tty_get),
            "tty_set" => Function::new_typed_with_env(&mut store, env, tty_set),
            "getcwd" => Function::new_typed_with_env(&mut store, env, getcwd),
            "chdir" => Function::new_typed_with_env(&mut store, env, chdir),
            "thread_spawn" => Function::new_typed_with_env(&mut store, env, thread_spawn),
            "thread_sleep" => Function::new_typed_with_env(&mut store, env, thread_sleep),
            "thread_id" => Function::new_typed_with_env(&mut store, env, thread_id),
            "thread_join" => Function::new_typed_with_env(&mut store, env, thread_join),
            "thread_parallelism" => Function::new_typed_with_env(&mut store, env, thread_parallelism),
            "thread_exit" => Function::new_typed_with_env(&mut store, env, thread_exit),
            "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield),
            "getpid" => Function::new_typed_with_env(&mut store, env, getpid),
            "process_spawn" => Function::new_typed_with_env(&mut store, env, process_spawn),
            "bus_open_local" => Function::new_typed_with_env(&mut store, env, bus_open_local),
            "bus_open_remote" => Function::new_typed_with_env(&mut store, env, bus_open_remote),
            "bus_close" => Function::new_typed_with_env(&mut store, env, bus_close),
            "bus_call" => Function::new_typed_with_env(&mut store, env, bus_call),
            "bus_subcall" => Function::new_typed_with_env(&mut store, env, bus_subcall),
            "bus_poll" => Function::new_typed_with_env(&mut store, env, bus_poll),
            "call_reply" => Function::new_typed_with_env(&mut store, env, call_reply),
            "call_fault" => Function::new_typed_with_env(&mut store, env, call_fault),
            "call_close" => Function::new_typed_with_env(&mut store, env, call_close),
            "ws_connect" => Function::new_typed_with_env(&mut store, env, ws_connect),
            "http_request" => Function::new_typed_with_env(&mut store, env, http_request),
            "http_status" => Function::new_typed_with_env(&mut store, env, http_status),
            "port_bridge" => Function::new_typed_with_env(&mut store, env, port_bridge),
            "port_unbridge" => Function::new_typed_with_env(&mut store, env, port_unbridge),
            "port_dhcp_acquire" => Function::new_typed_with_env(&mut store, env, port_dhcp_acquire),
            "port_addr_add" => Function::new_typed_with_env(&mut store, env, port_addr_add),
            "port_addr_remove" => Function::new_typed_with_env(&mut store, env, port_addr_remove),
            "port_addr_clear" => Function::new_typed_with_env(&mut store, env, port_addr_clear),
            "port_addr_list" => Function::new_typed_with_env(&mut store, env, port_addr_list),
            "port_mac" => Function::new_typed_with_env(&mut store, env, port_mac),
            "port_gateway_set" => Function::new_typed_with_env(&mut store, env, port_gateway_set),
            "port_route_add" => Function::new_typed_with_env(&mut store, env, port_route_add),
            "port_route_remove" => Function::new_typed_with_env(&mut store, env, port_route_remove),
            "port_route_clear" => Function::new_typed_with_env(&mut store, env, port_route_clear),
            "port_route_list" => Function::new_typed_with_env(&mut store, env, port_route_list),
            "sock_status" => Function::new_typed_with_env(&mut store, env, sock_status),
            "sock_addr_local" => Function::new_typed_with_env(&mut store, env, sock_addr_local),
            "sock_addr_peer" => Function::new_typed_with_env(&mut store, env, sock_addr_peer),
            "sock_open" => Function::new_typed_with_env(&mut store, env, sock_open),
            "sock_set_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_set_opt_flag),
            "sock_get_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_get_opt_flag),
            "sock_set_opt_time" => Function::new_typed_with_env(&mut store, env, sock_set_opt_time),
            "sock_get_opt_time" => Function::new_typed_with_env(&mut store, env, sock_get_opt_time),
            "sock_set_opt_size" => Function::new_typed_with_env(&mut store, env, sock_set_opt_size),
            "sock_get_opt_size" => Function::new_typed_with_env(&mut store, env, sock_get_opt_size),
            "sock_join_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v4),
            "sock_leave_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v4),
            "sock_join_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v6),
            "sock_leave_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v6),
            "sock_bind" => Function::new_typed_with_env(&mut store, env, sock_bind),
            "sock_listen" => Function::new_typed_with_env(&mut store, env, sock_listen),
            "sock_accept" => Function::new_typed_with_env(&mut store, env, sock_accept),
            "sock_connect" => Function::new_typed_with_env(&mut store, env, sock_connect),
            "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv),
            "sock_recv_from" => Function::new_typed_with_env(&mut store, env, sock_recv_from),
            "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send),
            "sock_send_to" => Function::new_typed_with_env(&mut store, env, sock_send_to),
            "sock_send_file" => Function::new_typed_with_env(&mut store, env, sock_send_file),
            "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown),
            "resolve" => Function::new_typed_with_env(&mut store, env, resolve),
        }
    }
}

#[cfg(feature = "wasix")]
fn generate_import_object_wasix64_v1(
    mut store: &mut impl AsStoreMut,
    env: &FunctionEnv<WasiEnv>,
) -> Imports {
    use self::wasix64::*;
    imports! {
        "wasix_64v1" => {
            "args_get" => Function::new_typed_with_env(&mut store, env, args_get),
            "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get),
            "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get),
            "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get),
            "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get),
            "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get),
            "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise),
            "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate),
            "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close),
            "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync),
            "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get),
            "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags),
            "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights),
            "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, fd_filestat_get),
            "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size),
            "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times),
            "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread),
            "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get),
            "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name),
            "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite),
            "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read),
            "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir),
            "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber),
            "fd_dup" => Function::new_typed_with_env(&mut store, env, fd_dup),
            "fd_event" => Function::new_typed_with_env(&mut store, env, fd_event),
            "fd_seek" => Function::new_typed_with_env(&mut store, env, fd_seek),
            "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync),
            "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell),
            "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write),
            "fd_pipe" => Function::new_typed_with_env(&mut store, env, fd_pipe),
            "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory),
            "path_filestat_get" => Function::new_typed_with_env(&mut store, env, path_filestat_get),
            "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times),
            "path_link" => Function::new_typed_with_env(&mut store, env, path_link),
            "path_open" => Function::new_typed_with_env(&mut store, env, path_open),
            "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink),
            "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory),
            "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename),
            "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink),
            "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file),
            "poll_oneoff" => Function::new_typed_with_env(&mut store, env, poll_oneoff),
            "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit),
            "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
            "random_get" => Function::new_typed_with_env(&mut store, env, random_get),
            "tty_get" => Function::new_typed_with_env(&mut store, env, tty_get),
            "tty_set" => Function::new_typed_with_env(&mut store, env, tty_set),
            "getcwd" => Function::new_typed_with_env(&mut store, env, getcwd),
            "chdir" => Function::new_typed_with_env(&mut store, env, chdir),
            "thread_spawn" => Function::new_typed_with_env(&mut store, env, thread_spawn),
            "thread_sleep" => Function::new_typed_with_env(&mut store, env, thread_sleep),
            "thread_id" => Function::new_typed_with_env(&mut store, env, thread_id),
            "thread_join" => Function::new_typed_with_env(&mut store, env, thread_join),
            "thread_parallelism" => Function::new_typed_with_env(&mut store, env, thread_parallelism),
            "thread_exit" => Function::new_typed_with_env(&mut store, env, thread_exit),
            "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield),
            "getpid" => Function::new_typed_with_env(&mut store, env, getpid),
            "process_spawn" => Function::new_typed_with_env(&mut store, env, process_spawn),
            "bus_open_local" => Function::new_typed_with_env(&mut store, env, bus_open_local),
            "bus_open_remote" => Function::new_typed_with_env(&mut store, env, bus_open_remote),
            "bus_close" => Function::new_typed_with_env(&mut store, env, bus_close),
            "bus_call" => Function::new_typed_with_env(&mut store, env, bus_call),
            "bus_subcall" => Function::new_typed_with_env(&mut store, env, bus_subcall),
            "bus_poll" => Function::new_typed_with_env(&mut store, env, bus_poll),
            "call_reply" => Function::new_typed_with_env(&mut store, env, call_reply),
            "call_fault" => Function::new_typed_with_env(&mut store, env, call_fault),
            "call_close" => Function::new_typed_with_env(&mut store, env, call_close),
            "ws_connect" => Function::new_typed_with_env(&mut store, env, ws_connect),
            "http_request" => Function::new_typed_with_env(&mut store, env, http_request),
            "http_status" => Function::new_typed_with_env(&mut store, env, http_status),
            "port_bridge" => Function::new_typed_with_env(&mut store, env, port_bridge),
            "port_unbridge" => Function::new_typed_with_env(&mut store, env, port_unbridge),
            "port_dhcp_acquire" => Function::new_typed_with_env(&mut store, env, port_dhcp_acquire),
            "port_addr_add" => Function::new_typed_with_env(&mut store, env, port_addr_add),
            "port_addr_remove" => Function::new_typed_with_env(&mut store, env, port_addr_remove),
            "port_addr_clear" => Function::new_typed_with_env(&mut store, env, port_addr_clear),
            "port_addr_list" => Function::new_typed_with_env(&mut store, env, port_addr_list),
            "port_mac" => Function::new_typed_with_env(&mut store, env, port_mac),
            "port_gateway_set" => Function::new_typed_with_env(&mut store, env, port_gateway_set),
            "port_route_add" => Function::new_typed_with_env(&mut store, env, port_route_add),
            "port_route_remove" => Function::new_typed_with_env(&mut store, env, port_route_remove),
            "port_route_clear" => Function::new_typed_with_env(&mut store, env, port_route_clear),
            "port_route_list" => Function::new_typed_with_env(&mut store, env, port_route_list),
            "sock_status" => Function::new_typed_with_env(&mut store, env, sock_status),
            "sock_addr_local" => Function::new_typed_with_env(&mut store, env, sock_addr_local),
            "sock_addr_peer" => Function::new_typed_with_env(&mut store, env, sock_addr_peer),
            "sock_open" => Function::new_typed_with_env(&mut store, env, sock_open),
            "sock_set_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_set_opt_flag),
            "sock_get_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_get_opt_flag),
            "sock_set_opt_time" => Function::new_typed_with_env(&mut store, env, sock_set_opt_time),
            "sock_get_opt_time" => Function::new_typed_with_env(&mut store, env, sock_get_opt_time),
            "sock_set_opt_size" => Function::new_typed_with_env(&mut store, env, sock_set_opt_size),
            "sock_get_opt_size" => Function::new_typed_with_env(&mut store, env, sock_get_opt_size),
            "sock_join_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v4),
            "sock_leave_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v4),
            "sock_join_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v6),
            "sock_leave_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v6),
            "sock_bind" => Function::new_typed_with_env(&mut store, env, sock_bind),
            "sock_listen" => Function::new_typed_with_env(&mut store, env, sock_listen),
            "sock_accept" => Function::new_typed_with_env(&mut store, env, sock_accept),
            "sock_connect" => Function::new_typed_with_env(&mut store, env, sock_connect),
            "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv),
            "sock_recv_from" => Function::new_typed_with_env(&mut store, env, sock_recv_from),
            "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send),
            "sock_send_to" => Function::new_typed_with_env(&mut store, env, sock_send_to),
            "sock_send_file" => Function::new_typed_with_env(&mut store, env, sock_send_file),
            "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown),
            "resolve" => Function::new_typed_with_env(&mut store, env, resolve),
        }
    }
}

fn mem_error_to_wasi(err: MemoryAccessError) -> Errno {
    match err {
        MemoryAccessError::HeapOutOfBounds => Errno::Fault,
        MemoryAccessError::Overflow => Errno::Overflow,
        MemoryAccessError::NonUtf8String => Errno::Inval,
        _ => Errno::Inval,
    }
}

fn mem_error_to_bus(err: MemoryAccessError) -> BusErrno {
    match err {
        MemoryAccessError::HeapOutOfBounds => BusErrno::Memviolation,
        MemoryAccessError::Overflow => BusErrno::Memviolation,
        MemoryAccessError::NonUtf8String => BusErrno::Badrequest,
        _ => BusErrno::Unknown,
    }
}