Path: blob/main/crates/wasi-common/src/lib.rs
3068 views
//! # wasi-common1//!2//! This is Wasmtime's legacy implementation of WASI 0.1 (Preview 1). The3//! Wasmtime maintainers suggest all users upgrade to the implementation4//! of WASI 0.1 and 0.2 provided by the `wasmtime-wasi` crate. This5//! implementation remains in the wasmtime tree because it is required to use6//! the `wasmtime-wasi-threads` crate, an implementation of the `wasi-threads`7//! proposal which is not compatible with WASI 0.2.8//!9//! In addition to integration with Wasmtime, this implementation may be used10//! by other runtimes by disabling the `wasmtime` feature on this crate.11//!12//! ## The `WasiFile` and `WasiDir` traits13//!14//! The WASI specification only defines one `handle` type, `fd`, on which all15//! operations on both files and directories (aka dirfds) are defined. We16//! believe this is a design mistake, and are architecting wasi-common to make17//! this straightforward to correct in future snapshots of WASI. Wasi-common18//! internally treats files and directories as two distinct resource types in19//! the table - `Box<dyn WasiFile>` and `Box<dyn WasiDir>`. The snapshot 0 and20//! 1 interfaces via `fd` will attempt to downcast a table element to one or21//! both of these interfaces depending on what is appropriate - e.g.22//! `fd_close` operates on both files and directories, `fd_read` only operates23//! on files, and `fd_readdir` only operates on directories.2425//! The `WasiFile` and `WasiDir` traits are defined by `wasi-common` in terms26//! of types defined directly in the crate's source code (I decided it should27//! NOT those generated by the `wiggle` proc macros, see snapshot architecture28//! below), as well as the `cap_std::time` family of types. And, importantly,29//! `wasi-common` itself provides no implementation of `WasiDir`, and only two30//! trivial implementations of `WasiFile` on the `crate::pipe::{ReadPipe,31//! WritePipe}` types, which in turn just delegate to `std::io::{Read,32//! Write}`. In order for `wasi-common` to access the local filesystem at all,33//! you need to provide `WasiFile` and `WasiDir` impls through either the new34//! `wasi-cap-std-sync` crate found at `crates/wasi-common/cap-std-sync` - see35//! the section on that crate below - or by providing your own implementation36//! from elsewhere.37//!38//! This design makes it possible for `wasi-common` embedders to statically39//! reason about access to the local filesystem by examining what impls are40//! linked into an application. We found that this separation of concerns also41//! makes it pretty enjoyable to write alternative implementations, e.g. a42//! virtual filesystem.43//!44//! Implementations of the `WasiFile` and `WasiDir` traits are provided45//! for synchronous embeddings in `wasi_common::sync` and for Tokio embeddings46//! in `wasi_common::tokio`.47//!48//! ## Traits for the rest of WASI's features49//!50//! Other aspects of a WASI implementation are not yet considered resources51//! and accessed by `handle`. We plan to correct this design deficiency in52//! WASI in the future, but for now we have designed the following traits to53//! provide embedders with the same sort of implementation flexibility they54//! get with WasiFile/WasiDir:55//!56//! * Timekeeping: `WasiSystemClock` and `WasiMonotonicClock` provide the two57//! interfaces for a clock. `WasiSystemClock` represents time as a58//! `cap_std::time::SystemTime`, and `WasiMonotonicClock` represents time as59//! `cap_std::time::Instant`. * Randomness: we re-use the `cap_rand::RngCore`60//! trait to represent a randomness source. A trivial `Deterministic` impl is61//! provided. * Scheduling: The `WasiSched` trait abstracts over the62//! `sched_yield` and `poll_oneoff` functions.63//!64//! Users can provide implementations of each of these interfaces to the65//! `WasiCtx::builder(...)` function. The66//! `wasi_cap_std_sync::WasiCtxBuilder::new()` function uses this public67//! interface to plug in its own implementations of each of these resources.6869#![warn(clippy::cast_sign_loss)]70#![cfg_attr(docsrs, feature(doc_cfg))]7172pub mod clocks;73mod ctx;74pub mod dir;75mod error;76pub mod file;77pub mod pipe;78pub mod random;79pub mod sched;80pub mod snapshots;81mod string_array;82#[cfg_attr(docsrs, doc(cfg(feature = "sync")))]83#[cfg(feature = "sync")]84pub mod sync;85pub mod table;86#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]87#[cfg(feature = "tokio")]88pub mod tokio;8990pub use cap_rand::RngCore;91pub use clocks::{SystemTimeSpec, WasiClocks, WasiMonotonicClock, WasiSystemClock};92pub use ctx::WasiCtx;93pub use dir::WasiDir;94pub use error::{Error, ErrorExt, I32Exit};95pub use file::WasiFile;96pub use sched::{Poll, WasiSched};97pub use string_array::{StringArray, StringArrayError};98pub use table::Table;99100pub(crate) use wasmtime_environ::error::Error as EnvError;101102// The only difference between these definitions for sync vs async is whether103// the wasmtime::Funcs generated are async (& therefore need an async Store and an executor to run)104// or whether they have an internal "dummy executor" that expects the implementation of all105// the async funcs to poll to Ready immediately.106#[cfg(feature = "wasmtime")]107#[doc(hidden)]108#[macro_export]109macro_rules! define_wasi {110($async_mode:tt $($bounds:tt)*) => {111112use wasmtime::Linker;113use wasmtime_environ::error::Result as EnvResult;114115pub fn add_to_linker<T, U>(116linker: &mut Linker<T>,117get_cx: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,118) -> EnvResult<()>119where U: Send120+ crate::snapshots::preview_0::wasi_unstable::WasiUnstable121+ crate::snapshots::preview_1::wasi_snapshot_preview1::WasiSnapshotPreview1,122T: 'static,123$($bounds)*124{125snapshots::preview_1::add_wasi_snapshot_preview1_to_linker(linker, get_cx)?;126snapshots::preview_0::add_wasi_unstable_to_linker(linker, get_cx)?;127Ok(())128}129130pub mod snapshots {131pub mod preview_1 {132wiggle::wasmtime_integration!({133// The wiggle code to integrate with lives here:134target: crate::snapshots::preview_1,135witx: ["witx/preview1/wasi_snapshot_preview1.witx"],136errors: { errno => trappable Error },137$async_mode: *138});139}140pub mod preview_0 {141wiggle::wasmtime_integration!({142// The wiggle code to integrate with lives here:143target: crate::snapshots::preview_0,144witx: ["witx/preview0/wasi_unstable.witx"],145errors: { errno => trappable Error },146$async_mode: *147});148}149}150}}151152/// Exit the process with a conventional OS error code as long as Wasmtime153/// understands the error. If the error is not an `I32Exit` or `Trap`, return154/// the error back to the caller for it to decide what to do.155///156/// Note: this function is designed for usage where it is acceptable for157/// Wasmtime failures to terminate the parent process, such as in the Wasmtime158/// CLI; this would not be suitable for use in multi-tenant embeddings.159#[cfg_attr(docsrs, doc(cfg(feature = "exit")))]160#[cfg(feature = "exit")]161pub fn maybe_exit_on_error(e: EnvError) -> EnvError {162use std::process;163use wasmtime::Trap;164165// If a specific WASI error code was requested then that's166// forwarded through to the process here without printing any167// extra error information.168if let Some(exit) = e.downcast_ref::<crate::I32Exit>() {169process::exit(exit.0);170}171172// If the program exited because of a trap, return an error code173// to the outside environment indicating a more severe problem174// than a simple failure.175if e.is::<Trap>() {176eprintln!("Error: {e:?}");177178if cfg!(unix) {179// On Unix, return the error code of an abort.180process::exit(128 + libc::SIGABRT);181} else if cfg!(windows) {182// On Windows, return 3.183// https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/abort?view=vs-2019184process::exit(3);185}186}187188e189}190191192