Path: blob/main/crates/wasi-common/src/lib.rs
1676 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 (i.e. Config::async_support(false)) in46//! `wasi_common::sync` and for Tokio embeddings 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;99100// The only difference between these definitions for sync vs async is whether101// the wasmtime::Funcs generated are async (& therefore need an async Store and an executor to run)102// or whether they have an internal "dummy executor" that expects the implementation of all103// the async funcs to poll to Ready immediately.104#[cfg(feature = "wasmtime")]105#[doc(hidden)]106#[macro_export]107macro_rules! define_wasi {108($async_mode:tt $($bounds:tt)*) => {109110use wasmtime::Linker;111112pub fn add_to_linker<T, U>(113linker: &mut Linker<T>,114get_cx: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,115) -> anyhow::Result<()>116where U: Send117+ crate::snapshots::preview_0::wasi_unstable::WasiUnstable118+ crate::snapshots::preview_1::wasi_snapshot_preview1::WasiSnapshotPreview1,119T: 'static,120$($bounds)*121{122snapshots::preview_1::add_wasi_snapshot_preview1_to_linker(linker, get_cx)?;123snapshots::preview_0::add_wasi_unstable_to_linker(linker, get_cx)?;124Ok(())125}126127pub mod snapshots {128pub mod preview_1 {129wiggle::wasmtime_integration!({130// The wiggle code to integrate with lives here:131target: crate::snapshots::preview_1,132witx: ["witx/preview1/wasi_snapshot_preview1.witx"],133errors: { errno => trappable Error },134$async_mode: *135});136}137pub mod preview_0 {138wiggle::wasmtime_integration!({139// The wiggle code to integrate with lives here:140target: crate::snapshots::preview_0,141witx: ["witx/preview0/wasi_unstable.witx"],142errors: { errno => trappable Error },143$async_mode: *144});145}146}147}}148149/// Exit the process with a conventional OS error code as long as Wasmtime150/// understands the error. If the error is not an `I32Exit` or `Trap`, return151/// the error back to the caller for it to decide what to do.152///153/// Note: this function is designed for usage where it is acceptable for154/// Wasmtime failures to terminate the parent process, such as in the Wasmtime155/// CLI; this would not be suitable for use in multi-tenant embeddings.156#[cfg_attr(docsrs, doc(cfg(feature = "exit")))]157#[cfg(feature = "exit")]158pub fn maybe_exit_on_error(e: anyhow::Error) -> anyhow::Error {159use std::process;160use wasmtime::Trap;161162// If a specific WASI error code was requested then that's163// forwarded through to the process here without printing any164// extra error information.165if let Some(exit) = e.downcast_ref::<crate::I32Exit>() {166process::exit(exit.0);167}168169// If the program exited because of a trap, return an error code170// to the outside environment indicating a more severe problem171// than a simple failure.172if e.is::<Trap>() {173eprintln!("Error: {e:?}");174175if cfg!(unix) {176// On Unix, return the error code of an abort.177process::exit(128 + libc::SIGABRT);178} else if cfg!(windows) {179// On Windows, return 3.180// https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/abort?view=vs-2019181process::exit(3);182}183}184185e186}187188189