1#![deny(unused_mut)]
2#![doc(html_favicon_url = "https://wasmer.io/images/icons/favicon-32x32.png")]
3#![doc(html_logo_url = "https://github.com/wasmerio.png?size=200")]
4
5#[cfg(all(not(feature = "sys"), not(feature = "js")))]
16compile_error!("At least the `sys` or the `js` feature must be enabled. Please, pick one.");
17
18#[cfg(all(feature = "sys", feature = "js"))]
19compile_error!(
20 "Cannot have both `sys` and `js` features enabled at the same time. Please, pick one."
21);
22
23#[cfg(all(feature = "sys", target_arch = "wasm32"))]
24compile_error!("The `sys` feature must be enabled only for non-`wasm32` target.");
25
26#[cfg(all(feature = "js", not(target_arch = "wasm32")))]
27compile_error!(
28 "The `js` feature must be enabled only for the `wasm32` target (either `wasm32-unknown-unknown` or `wasm32-wasi`)."
29);
30
31#[cfg(all(feature = "host-fs", feature = "mem-fs"))]
32compile_error!(
33 "Cannot have both `host-fs` and `mem-fs` features enabled at the same time. Please, pick one."
34);
35
36#[macro_use]
37mod macros;
38mod runtime;
39mod state;
40mod syscalls;
41mod utils;
42
43#[cfg(feature = "webc_runner")]
45pub mod runners;
46
47use crate::syscalls::*;
48
49pub use crate::state::{
50 Fd, Pipe, Stderr, Stdin, Stdout, WasiFs, WasiInodes, WasiState, WasiStateBuilder,
51 WasiStateCreationError, ALL_RIGHTS, VIRTUAL_ROOT_FD,
52};
53pub use crate::syscalls::types;
54#[cfg(feature = "wasix")]
55pub use crate::utils::is_wasix_module;
56pub use crate::utils::wasi_import_shared_memory;
57pub use crate::utils::{get_wasi_version, get_wasi_versions, is_wasi_module, WasiVersion};
58
59pub use wasmer_vbus::{UnsupportedVirtualBus, VirtualBus};
60#[deprecated(since = "2.1.0", note = "Please use `wasmer_vfs::FsError`")]
61pub use wasmer_vfs::FsError as WasiFsError;
62#[deprecated(since = "2.1.0", note = "Please use `wasmer_vfs::VirtualFile`")]
63pub use wasmer_vfs::VirtualFile as WasiFile;
64pub use wasmer_vfs::{FsError, VirtualFile};
65pub use wasmer_vnet::{UnsupportedVirtualNetworking, VirtualNetworking};
66
67use derivative::*;
68use std::ops::Deref;
69use thiserror::Error;
70use tracing::trace;
71use wasmer::{
72 imports, namespace, AsStoreMut, AsStoreRef, ExportError, Exports, Function, FunctionEnv,
73 Imports, Instance, Memory, Memory32, MemoryAccessError, MemorySize, MemoryView, Module,
74 TypedFunction,
75};
76use wasmer_wasi_types::wasi::{BusErrno, Errno, Snapshot0Clockid};
77
78pub use runtime::{
79 PluggableRuntimeImplementation, WasiRuntimeImplementation, WasiThreadError, WasiTtyState,
80};
81use std::sync::{mpsc, Arc, Mutex, RwLockReadGuard, RwLockWriteGuard};
82use std::time::Duration;
83
84#[derive(Error, Debug)]
87pub enum WasiError {
88 #[error("WASI exited with code: {0}")]
89 Exit(syscalls::types::__wasi_exitcode_t),
90 #[error("The WASI version could not be determined")]
91 UnknownWasiVersion,
92}
93
94#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
96pub struct WasiThreadId(u32);
97
98impl From<u32> for WasiThreadId {
99 fn from(id: u32) -> Self {
100 Self(id)
101 }
102}
103impl From<WasiThreadId> for u32 {
104 fn from(t: WasiThreadId) -> u32 {
105 t.0 as u32
106 }
107}
108
109#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
111pub struct WasiBusProcessId(u32);
112
113impl From<u32> for WasiBusProcessId {
114 fn from(id: u32) -> Self {
115 Self(id)
116 }
117}
118impl From<WasiBusProcessId> for u32 {
119 fn from(id: WasiBusProcessId) -> u32 {
120 id.0 as u32
121 }
122}
123
124#[derive(Debug, Clone)]
125pub struct WasiThread {
126 #[allow(dead_code)]
128 id: WasiThreadId,
129 exit: Arc<Mutex<Option<mpsc::Sender<()>>>>,
131 join: Arc<Mutex<mpsc::Receiver<()>>>,
133}
134
135impl WasiThread {
136 pub fn join(&self, timeout: Duration) -> bool {
138 let guard = self.join.lock().unwrap();
139 let timeout = guard.recv_timeout(timeout);
140 match timeout {
141 Ok(_) => true,
142 Err(mpsc::RecvTimeoutError::Disconnected) => true,
143 Err(mpsc::RecvTimeoutError::Timeout) => false,
144 }
145 }
146}
147
148pub struct WasiFunctionEnv {
149 pub env: FunctionEnv<WasiEnv>,
150}
151
152impl WasiFunctionEnv {
153 pub fn new(store: &mut impl AsStoreMut, env: WasiEnv) -> Self {
154 Self {
155 env: FunctionEnv::new(store, env),
156 }
157 }
158
159 pub fn import_object(
161 &self,
162 store: &mut impl AsStoreMut,
163 module: &Module,
164 ) -> Result<Imports, WasiError> {
165 let wasi_version = get_wasi_version(module, false).ok_or(WasiError::UnknownWasiVersion)?;
166 Ok(generate_import_object_from_env(
167 store,
168 &self.env,
169 wasi_version,
170 ))
171 }
172
173 pub fn data_mut<'a>(&'a self, store: &'a mut impl AsStoreMut) -> &'a mut WasiEnv {
174 self.env.as_mut(store)
175 }
176
177 pub fn initialize(
182 &mut self,
183 store: &mut impl AsStoreMut,
184 instance: &Instance,
185 ) -> Result<(), ExportError> {
186 for ns in instance.module().exports() {
188 trace!("module::export - {}", ns.name());
190 }
191 for ns in instance.module().imports() {
192 trace!("module::import - {}::{}", ns.module(), ns.name());
193 }
194
195 let memory = instance.exports.get_memory("memory")?.clone();
198 let env = self.data_mut(store);
199 env.set_memory(memory);
200
201 Ok(())
202 }
203
204 pub fn import_object_for_all_wasi_versions(
207 &self,
208 store: &mut impl AsStoreMut,
209 module: &Module,
210 ) -> Result<Imports, WasiError> {
211 let wasi_versions =
212 get_wasi_versions(module, false).ok_or(WasiError::UnknownWasiVersion)?;
213
214 let mut resolver = Imports::new();
215 for version in wasi_versions.iter() {
216 let new_import_object = generate_import_object_from_env(store, &self.env, *version);
217 for ((n, m), e) in new_import_object.into_iter() {
218 resolver.define(&n, &m, e);
219 }
220 }
221
222 #[cfg(feature = "wasix")]
223 if is_wasix_module(module) {
224 self.data_mut(store)
225 .state
226 .fs
227 .is_wasix
228 .store(true, std::sync::atomic::Ordering::Release);
229 }
230
231 Ok(resolver)
232 }
233}
234
235#[derive(Derivative, Clone)]
237#[derivative(Debug)]
238#[allow(dead_code)]
239pub struct WasiEnv {
240 id: WasiThreadId,
242 memory: Option<Memory>,
244 #[derivative(Debug = "ignore")]
246 thread_start: Option<TypedFunction<u64, ()>>,
247 #[derivative(Debug = "ignore")]
248 reactor_work: Option<TypedFunction<u64, ()>>,
249 #[derivative(Debug = "ignore")]
250 reactor_finish: Option<TypedFunction<u64, ()>>,
251 #[derivative(Debug = "ignore")]
252 malloc: Option<TypedFunction<u64, u64>>,
253 #[derivative(Debug = "ignore")]
254 free: Option<TypedFunction<(u64, u64), ()>>,
255 pub state: Arc<WasiState>,
258 pub(crate) runtime: Arc<dyn WasiRuntimeImplementation + Send + Sync + 'static>,
260}
261
262impl WasiEnv {
263 pub fn new(state: WasiState) -> Self {
265 Self {
266 id: 0u32.into(),
267 state: Arc::new(state),
268 memory: None,
269 thread_start: None,
270 reactor_work: None,
271 reactor_finish: None,
272 malloc: None,
273 free: None,
274 runtime: Arc::new(PluggableRuntimeImplementation::default()),
275 }
276 }
277
278 pub fn runtime(&self) -> &(dyn WasiRuntimeImplementation) {
280 self.runtime.deref()
281 }
282
283 pub fn set_runtime<R>(&mut self, runtime: R)
285 where
286 R: WasiRuntimeImplementation + Send + Sync + 'static,
287 {
288 self.runtime = Arc::new(runtime);
289 }
290
291 pub fn current_thread_id(&self) -> WasiThreadId {
293 self.id
294 }
295
296 pub fn new_thread(&self) -> WasiThread {
298 let (tx, rx) = mpsc::channel();
299
300 let mut guard = self.state.threading.lock().unwrap();
301
302 guard.thread_seed += 1;
303 let next_id: WasiThreadId = guard.thread_seed.into();
304
305 let thread = WasiThread {
306 id: next_id,
307 exit: Arc::new(Mutex::new(Some(tx))),
308 join: Arc::new(Mutex::new(rx)),
309 };
310
311 guard.threads.insert(thread.id, thread.clone());
312 thread
313 }
314
315 pub fn memory_clone(&self) -> Option<Memory> {
318 self.memory.clone()
319 }
320
321 pub fn yield_now(&self) -> Result<(), WasiError> {
323 self.runtime.yield_now(self.id)?;
324 Ok(())
325 }
326
327 pub fn sleep(&self, duration: Duration) -> Result<(), WasiError> {
329 let duration = duration.as_nanos();
330 let start =
331 platform_clock_time_get(Snapshot0Clockid::Monotonic, 1_000_000).unwrap() as u128;
332 self.yield_now()?;
333 loop {
334 let now =
335 platform_clock_time_get(Snapshot0Clockid::Monotonic, 1_000_000).unwrap() as u128;
336 let delta = match now.checked_sub(start) {
337 Some(a) => a,
338 None => {
339 break;
340 }
341 };
342 if delta >= duration {
343 break;
344 }
345 let remaining = match duration.checked_sub(delta) {
346 Some(a) => Duration::from_nanos(a as u64),
347 None => {
348 break;
349 }
350 };
351 std::thread::sleep(remaining.min(Duration::from_millis(10)));
352 self.yield_now()?;
353 }
354 Ok(())
355 }
356
357 pub fn net(&self) -> &(dyn VirtualNetworking) {
359 self.runtime.networking()
360 }
361
362 pub fn bus(&self) -> &(dyn VirtualBus) {
364 self.runtime.bus()
365 }
366
367 pub fn set_memory(&mut self, memory: Memory) {
369 if self.memory.is_some() {
370 panic!("Memory of a WasiEnv can only be set once!");
371 }
372 self.memory = Some(memory);
373 }
374
375 pub fn memory_view<'a>(&'a self, store: &'a impl AsStoreRef) -> MemoryView<'a> {
378 self.memory().view(store)
379 }
380
381 pub fn memory(&self) -> &Memory {
383 self.memory.as_ref().unwrap()
384 }
385
386 pub fn state(&self) -> &WasiState {
388 &self.state
389 }
390
391 pub(crate) fn get_memory_and_wasi_state<'a>(
392 &'a self,
393 store: &'a impl AsStoreRef,
394 _mem_index: u32,
395 ) -> (MemoryView<'a>, &WasiState) {
396 let memory = self.memory_view(store);
397 let state = self.state.deref();
398 (memory, state)
399 }
400
401 pub(crate) fn get_memory_and_wasi_state_and_inodes<'a>(
402 &'a self,
403 store: &'a impl AsStoreRef,
404 _mem_index: u32,
405 ) -> (MemoryView<'a>, &WasiState, RwLockReadGuard<WasiInodes>) {
406 let memory = self.memory_view(store);
407 let state = self.state.deref();
408 let inodes = state.inodes.read().unwrap();
409 (memory, state, inodes)
410 }
411
412 pub(crate) fn get_memory_and_wasi_state_and_inodes_mut<'a>(
413 &'a self,
414 store: &'a impl AsStoreRef,
415 _mem_index: u32,
416 ) -> (MemoryView<'a>, &WasiState, RwLockWriteGuard<WasiInodes>) {
417 let memory = self.memory_view(store);
418 let state = self.state.deref();
419 let inodes = state.inodes.write().unwrap();
420 (memory, state, inodes)
421 }
422}
423
424pub fn generate_import_object_from_env(
426 store: &mut impl AsStoreMut,
427 env: &FunctionEnv<WasiEnv>,
428 version: WasiVersion,
429) -> Imports {
430 match version {
431 WasiVersion::Snapshot0 => generate_import_object_snapshot0(store, env),
432 WasiVersion::Snapshot1 | WasiVersion::Latest => {
433 generate_import_object_snapshot1(store, env)
434 }
435 #[cfg(feature = "wasix")]
436 WasiVersion::Wasix32v1 => generate_import_object_wasix32_v1(store, env),
437 #[cfg(feature = "wasix")]
438 WasiVersion::Wasix64v1 => generate_import_object_wasix64_v1(store, env),
439 #[cfg(not(feature = "wasix"))]
440 _ => unimplemented!(),
441 }
442}
443
444fn wasi_unstable_exports(mut store: &mut impl AsStoreMut, env: &FunctionEnv<WasiEnv>) -> Exports {
445 let namespace = namespace! {
446 "args_get" => Function::new_typed_with_env(&mut store, env, args_get::<Memory32>),
447 "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get::<Memory32>),
448 "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get::<Memory32>),
449 "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get::<Memory32>),
450 "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get::<Memory32>),
451 "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get::<Memory32>),
452 "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise),
453 "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate),
454 "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close),
455 "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync),
456 "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get::<Memory32>),
457 "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags),
458 "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights),
459 "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::fd_filestat_get),
460 "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size),
461 "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times),
462 "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread::<Memory32>),
463 "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get::<Memory32>),
464 "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name::<Memory32>),
465 "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite::<Memory32>),
466 "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read::<Memory32>),
467 "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir::<Memory32>),
468 "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber),
469 "fd_seek" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::fd_seek),
470 "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync),
471 "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell::<Memory32>),
472 "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write::<Memory32>),
473 "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory::<Memory32>),
474 "path_filestat_get" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::path_filestat_get),
475 "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times::<Memory32>),
476 "path_link" => Function::new_typed_with_env(&mut store, env, path_link::<Memory32>),
477 "path_open" => Function::new_typed_with_env(&mut store, env, path_open::<Memory32>),
478 "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink::<Memory32>),
479 "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory::<Memory32>),
480 "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename::<Memory32>),
481 "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink::<Memory32>),
482 "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file::<Memory32>),
483 "poll_oneoff" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::poll_oneoff),
484 "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit),
485 "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
486 "random_get" => Function::new_typed_with_env(&mut store, env, random_get::<Memory32>),
487 "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield),
488 "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv::<Memory32>),
489 "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send::<Memory32>),
490 "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown),
491 };
492 namespace
493}
494
495fn wasi_snapshot_preview1_exports(
496 mut store: &mut impl AsStoreMut,
497 env: &FunctionEnv<WasiEnv>,
498) -> Exports {
499 let namespace = namespace! {
500 "args_get" => Function::new_typed_with_env(&mut store, env, args_get::<Memory32>),
501 "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get::<Memory32>),
502 "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get::<Memory32>),
503 "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get::<Memory32>),
504 "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get::<Memory32>),
505 "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get::<Memory32>),
506 "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise),
507 "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate),
508 "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close),
509 "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync),
510 "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get::<Memory32>),
511 "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags),
512 "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights),
513 "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, fd_filestat_get::<Memory32>),
514 "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size),
515 "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times),
516 "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread::<Memory32>),
517 "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get::<Memory32>),
518 "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name::<Memory32>),
519 "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite::<Memory32>),
520 "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read::<Memory32>),
521 "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir::<Memory32>),
522 "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber),
523 "fd_seek" => Function::new_typed_with_env(&mut store, env, fd_seek::<Memory32>),
524 "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync),
525 "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell::<Memory32>),
526 "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write::<Memory32>),
527 "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory::<Memory32>),
528 "path_filestat_get" => Function::new_typed_with_env(&mut store, env, path_filestat_get::<Memory32>),
529 "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times::<Memory32>),
530 "path_link" => Function::new_typed_with_env(&mut store, env, path_link::<Memory32>),
531 "path_open" => Function::new_typed_with_env(&mut store, env, path_open::<Memory32>),
532 "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink::<Memory32>),
533 "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory::<Memory32>),
534 "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename::<Memory32>),
535 "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink::<Memory32>),
536 "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file::<Memory32>),
537 "poll_oneoff" => Function::new_typed_with_env(&mut store, env, poll_oneoff::<Memory32>),
538 "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit),
539 "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
540 "random_get" => Function::new_typed_with_env(&mut store, env, random_get::<Memory32>),
541 "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield),
542 "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv::<Memory32>),
543 "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send::<Memory32>),
544 "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown),
545 };
546 namespace
547}
548pub fn import_object_for_all_wasi_versions(
549 store: &mut impl AsStoreMut,
550 env: &FunctionEnv<WasiEnv>,
551) -> Imports {
552 let wasi_unstable_exports = wasi_unstable_exports(store, env);
553 let wasi_snapshot_preview1_exports = wasi_snapshot_preview1_exports(store, env);
554 imports! {
555 "wasi_unstable" => wasi_unstable_exports,
556 "wasi_snapshot_preview1" => wasi_snapshot_preview1_exports,
557 }
558}
559
560fn generate_import_object_snapshot0(
562 store: &mut impl AsStoreMut,
563 env: &FunctionEnv<WasiEnv>,
564) -> Imports {
565 let wasi_unstable_exports = wasi_unstable_exports(store, env);
566 imports! {
567 "wasi_unstable" => wasi_unstable_exports
568 }
569}
570
571fn generate_import_object_snapshot1(
572 store: &mut impl AsStoreMut,
573 env: &FunctionEnv<WasiEnv>,
574) -> Imports {
575 let wasi_snapshot_preview1_exports = wasi_snapshot_preview1_exports(store, env);
576 imports! {
577 "wasi_snapshot_preview1" => wasi_snapshot_preview1_exports
578 }
579}
580
581#[cfg(feature = "wasix")]
583fn generate_import_object_wasix32_v1(
584 mut store: &mut impl AsStoreMut,
585 env: &FunctionEnv<WasiEnv>,
586) -> Imports {
587 use self::wasix32::*;
588 imports! {
589 "wasix_32v1" => {
590 "args_get" => Function::new_typed_with_env(&mut store, env, args_get),
591 "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get),
592 "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get),
593 "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get),
594 "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get),
595 "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get),
596 "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise),
597 "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate),
598 "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close),
599 "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync),
600 "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get),
601 "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags),
602 "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights),
603 "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, fd_filestat_get),
604 "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size),
605 "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times),
606 "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread),
607 "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get),
608 "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name),
609 "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite),
610 "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read),
611 "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir),
612 "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber),
613 "fd_dup" => Function::new_typed_with_env(&mut store, env, fd_dup),
614 "fd_event" => Function::new_typed_with_env(&mut store, env, fd_event),
615 "fd_seek" => Function::new_typed_with_env(&mut store, env, fd_seek),
616 "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync),
617 "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell),
618 "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write),
619 "fd_pipe" => Function::new_typed_with_env(&mut store, env, fd_pipe),
620 "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory),
621 "path_filestat_get" => Function::new_typed_with_env(&mut store, env, path_filestat_get),
622 "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times),
623 "path_link" => Function::new_typed_with_env(&mut store, env, path_link),
624 "path_open" => Function::new_typed_with_env(&mut store, env, path_open),
625 "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink),
626 "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory),
627 "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename),
628 "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink),
629 "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file),
630 "poll_oneoff" => Function::new_typed_with_env(&mut store, env, poll_oneoff),
631 "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit),
632 "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
633 "random_get" => Function::new_typed_with_env(&mut store, env, random_get),
634 "tty_get" => Function::new_typed_with_env(&mut store, env, tty_get),
635 "tty_set" => Function::new_typed_with_env(&mut store, env, tty_set),
636 "getcwd" => Function::new_typed_with_env(&mut store, env, getcwd),
637 "chdir" => Function::new_typed_with_env(&mut store, env, chdir),
638 "thread_spawn" => Function::new_typed_with_env(&mut store, env, thread_spawn),
639 "thread_sleep" => Function::new_typed_with_env(&mut store, env, thread_sleep),
640 "thread_id" => Function::new_typed_with_env(&mut store, env, thread_id),
641 "thread_join" => Function::new_typed_with_env(&mut store, env, thread_join),
642 "thread_parallelism" => Function::new_typed_with_env(&mut store, env, thread_parallelism),
643 "thread_exit" => Function::new_typed_with_env(&mut store, env, thread_exit),
644 "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield),
645 "getpid" => Function::new_typed_with_env(&mut store, env, getpid),
646 "process_spawn" => Function::new_typed_with_env(&mut store, env, process_spawn),
647 "bus_open_local" => Function::new_typed_with_env(&mut store, env, bus_open_local),
648 "bus_open_remote" => Function::new_typed_with_env(&mut store, env, bus_open_remote),
649 "bus_close" => Function::new_typed_with_env(&mut store, env, bus_close),
650 "bus_call" => Function::new_typed_with_env(&mut store, env, bus_call),
651 "bus_subcall" => Function::new_typed_with_env(&mut store, env, bus_subcall),
652 "bus_poll" => Function::new_typed_with_env(&mut store, env, bus_poll),
653 "call_reply" => Function::new_typed_with_env(&mut store, env, call_reply),
654 "call_fault" => Function::new_typed_with_env(&mut store, env, call_fault),
655 "call_close" => Function::new_typed_with_env(&mut store, env, call_close),
656 "ws_connect" => Function::new_typed_with_env(&mut store, env, ws_connect),
657 "http_request" => Function::new_typed_with_env(&mut store, env, http_request),
658 "http_status" => Function::new_typed_with_env(&mut store, env, http_status),
659 "port_bridge" => Function::new_typed_with_env(&mut store, env, port_bridge),
660 "port_unbridge" => Function::new_typed_with_env(&mut store, env, port_unbridge),
661 "port_dhcp_acquire" => Function::new_typed_with_env(&mut store, env, port_dhcp_acquire),
662 "port_addr_add" => Function::new_typed_with_env(&mut store, env, port_addr_add),
663 "port_addr_remove" => Function::new_typed_with_env(&mut store, env, port_addr_remove),
664 "port_addr_clear" => Function::new_typed_with_env(&mut store, env, port_addr_clear),
665 "port_addr_list" => Function::new_typed_with_env(&mut store, env, port_addr_list),
666 "port_mac" => Function::new_typed_with_env(&mut store, env, port_mac),
667 "port_gateway_set" => Function::new_typed_with_env(&mut store, env, port_gateway_set),
668 "port_route_add" => Function::new_typed_with_env(&mut store, env, port_route_add),
669 "port_route_remove" => Function::new_typed_with_env(&mut store, env, port_route_remove),
670 "port_route_clear" => Function::new_typed_with_env(&mut store, env, port_route_clear),
671 "port_route_list" => Function::new_typed_with_env(&mut store, env, port_route_list),
672 "sock_status" => Function::new_typed_with_env(&mut store, env, sock_status),
673 "sock_addr_local" => Function::new_typed_with_env(&mut store, env, sock_addr_local),
674 "sock_addr_peer" => Function::new_typed_with_env(&mut store, env, sock_addr_peer),
675 "sock_open" => Function::new_typed_with_env(&mut store, env, sock_open),
676 "sock_set_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_set_opt_flag),
677 "sock_get_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_get_opt_flag),
678 "sock_set_opt_time" => Function::new_typed_with_env(&mut store, env, sock_set_opt_time),
679 "sock_get_opt_time" => Function::new_typed_with_env(&mut store, env, sock_get_opt_time),
680 "sock_set_opt_size" => Function::new_typed_with_env(&mut store, env, sock_set_opt_size),
681 "sock_get_opt_size" => Function::new_typed_with_env(&mut store, env, sock_get_opt_size),
682 "sock_join_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v4),
683 "sock_leave_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v4),
684 "sock_join_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v6),
685 "sock_leave_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v6),
686 "sock_bind" => Function::new_typed_with_env(&mut store, env, sock_bind),
687 "sock_listen" => Function::new_typed_with_env(&mut store, env, sock_listen),
688 "sock_accept" => Function::new_typed_with_env(&mut store, env, sock_accept),
689 "sock_connect" => Function::new_typed_with_env(&mut store, env, sock_connect),
690 "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv),
691 "sock_recv_from" => Function::new_typed_with_env(&mut store, env, sock_recv_from),
692 "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send),
693 "sock_send_to" => Function::new_typed_with_env(&mut store, env, sock_send_to),
694 "sock_send_file" => Function::new_typed_with_env(&mut store, env, sock_send_file),
695 "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown),
696 "resolve" => Function::new_typed_with_env(&mut store, env, resolve),
697 }
698 }
699}
700
701#[cfg(feature = "wasix")]
702fn generate_import_object_wasix64_v1(
703 mut store: &mut impl AsStoreMut,
704 env: &FunctionEnv<WasiEnv>,
705) -> Imports {
706 use self::wasix64::*;
707 imports! {
708 "wasix_64v1" => {
709 "args_get" => Function::new_typed_with_env(&mut store, env, args_get),
710 "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get),
711 "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get),
712 "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get),
713 "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get),
714 "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get),
715 "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise),
716 "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate),
717 "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close),
718 "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync),
719 "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get),
720 "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags),
721 "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights),
722 "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, fd_filestat_get),
723 "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size),
724 "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times),
725 "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread),
726 "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get),
727 "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name),
728 "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite),
729 "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read),
730 "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir),
731 "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber),
732 "fd_dup" => Function::new_typed_with_env(&mut store, env, fd_dup),
733 "fd_event" => Function::new_typed_with_env(&mut store, env, fd_event),
734 "fd_seek" => Function::new_typed_with_env(&mut store, env, fd_seek),
735 "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync),
736 "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell),
737 "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write),
738 "fd_pipe" => Function::new_typed_with_env(&mut store, env, fd_pipe),
739 "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory),
740 "path_filestat_get" => Function::new_typed_with_env(&mut store, env, path_filestat_get),
741 "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times),
742 "path_link" => Function::new_typed_with_env(&mut store, env, path_link),
743 "path_open" => Function::new_typed_with_env(&mut store, env, path_open),
744 "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink),
745 "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory),
746 "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename),
747 "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink),
748 "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file),
749 "poll_oneoff" => Function::new_typed_with_env(&mut store, env, poll_oneoff),
750 "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit),
751 "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
752 "random_get" => Function::new_typed_with_env(&mut store, env, random_get),
753 "tty_get" => Function::new_typed_with_env(&mut store, env, tty_get),
754 "tty_set" => Function::new_typed_with_env(&mut store, env, tty_set),
755 "getcwd" => Function::new_typed_with_env(&mut store, env, getcwd),
756 "chdir" => Function::new_typed_with_env(&mut store, env, chdir),
757 "thread_spawn" => Function::new_typed_with_env(&mut store, env, thread_spawn),
758 "thread_sleep" => Function::new_typed_with_env(&mut store, env, thread_sleep),
759 "thread_id" => Function::new_typed_with_env(&mut store, env, thread_id),
760 "thread_join" => Function::new_typed_with_env(&mut store, env, thread_join),
761 "thread_parallelism" => Function::new_typed_with_env(&mut store, env, thread_parallelism),
762 "thread_exit" => Function::new_typed_with_env(&mut store, env, thread_exit),
763 "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield),
764 "getpid" => Function::new_typed_with_env(&mut store, env, getpid),
765 "process_spawn" => Function::new_typed_with_env(&mut store, env, process_spawn),
766 "bus_open_local" => Function::new_typed_with_env(&mut store, env, bus_open_local),
767 "bus_open_remote" => Function::new_typed_with_env(&mut store, env, bus_open_remote),
768 "bus_close" => Function::new_typed_with_env(&mut store, env, bus_close),
769 "bus_call" => Function::new_typed_with_env(&mut store, env, bus_call),
770 "bus_subcall" => Function::new_typed_with_env(&mut store, env, bus_subcall),
771 "bus_poll" => Function::new_typed_with_env(&mut store, env, bus_poll),
772 "call_reply" => Function::new_typed_with_env(&mut store, env, call_reply),
773 "call_fault" => Function::new_typed_with_env(&mut store, env, call_fault),
774 "call_close" => Function::new_typed_with_env(&mut store, env, call_close),
775 "ws_connect" => Function::new_typed_with_env(&mut store, env, ws_connect),
776 "http_request" => Function::new_typed_with_env(&mut store, env, http_request),
777 "http_status" => Function::new_typed_with_env(&mut store, env, http_status),
778 "port_bridge" => Function::new_typed_with_env(&mut store, env, port_bridge),
779 "port_unbridge" => Function::new_typed_with_env(&mut store, env, port_unbridge),
780 "port_dhcp_acquire" => Function::new_typed_with_env(&mut store, env, port_dhcp_acquire),
781 "port_addr_add" => Function::new_typed_with_env(&mut store, env, port_addr_add),
782 "port_addr_remove" => Function::new_typed_with_env(&mut store, env, port_addr_remove),
783 "port_addr_clear" => Function::new_typed_with_env(&mut store, env, port_addr_clear),
784 "port_addr_list" => Function::new_typed_with_env(&mut store, env, port_addr_list),
785 "port_mac" => Function::new_typed_with_env(&mut store, env, port_mac),
786 "port_gateway_set" => Function::new_typed_with_env(&mut store, env, port_gateway_set),
787 "port_route_add" => Function::new_typed_with_env(&mut store, env, port_route_add),
788 "port_route_remove" => Function::new_typed_with_env(&mut store, env, port_route_remove),
789 "port_route_clear" => Function::new_typed_with_env(&mut store, env, port_route_clear),
790 "port_route_list" => Function::new_typed_with_env(&mut store, env, port_route_list),
791 "sock_status" => Function::new_typed_with_env(&mut store, env, sock_status),
792 "sock_addr_local" => Function::new_typed_with_env(&mut store, env, sock_addr_local),
793 "sock_addr_peer" => Function::new_typed_with_env(&mut store, env, sock_addr_peer),
794 "sock_open" => Function::new_typed_with_env(&mut store, env, sock_open),
795 "sock_set_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_set_opt_flag),
796 "sock_get_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_get_opt_flag),
797 "sock_set_opt_time" => Function::new_typed_with_env(&mut store, env, sock_set_opt_time),
798 "sock_get_opt_time" => Function::new_typed_with_env(&mut store, env, sock_get_opt_time),
799 "sock_set_opt_size" => Function::new_typed_with_env(&mut store, env, sock_set_opt_size),
800 "sock_get_opt_size" => Function::new_typed_with_env(&mut store, env, sock_get_opt_size),
801 "sock_join_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v4),
802 "sock_leave_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v4),
803 "sock_join_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v6),
804 "sock_leave_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v6),
805 "sock_bind" => Function::new_typed_with_env(&mut store, env, sock_bind),
806 "sock_listen" => Function::new_typed_with_env(&mut store, env, sock_listen),
807 "sock_accept" => Function::new_typed_with_env(&mut store, env, sock_accept),
808 "sock_connect" => Function::new_typed_with_env(&mut store, env, sock_connect),
809 "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv),
810 "sock_recv_from" => Function::new_typed_with_env(&mut store, env, sock_recv_from),
811 "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send),
812 "sock_send_to" => Function::new_typed_with_env(&mut store, env, sock_send_to),
813 "sock_send_file" => Function::new_typed_with_env(&mut store, env, sock_send_file),
814 "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown),
815 "resolve" => Function::new_typed_with_env(&mut store, env, resolve),
816 }
817 }
818}
819
820fn mem_error_to_wasi(err: MemoryAccessError) -> Errno {
821 match err {
822 MemoryAccessError::HeapOutOfBounds => Errno::Fault,
823 MemoryAccessError::Overflow => Errno::Overflow,
824 MemoryAccessError::NonUtf8String => Errno::Inval,
825 _ => Errno::Inval,
826 }
827}
828
829fn mem_error_to_bus(err: MemoryAccessError) -> BusErrno {
830 match err {
831 MemoryAccessError::HeapOutOfBounds => BusErrno::Memviolation,
832 MemoryAccessError::Overflow => BusErrno::Memviolation,
833 MemoryAccessError::NonUtf8String => BusErrno::Badrequest,
834 _ => BusErrno::Unknown,
835 }
836}