From 19a802549f6557efc639949b8adb713d1c3392d1 Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Thu, 25 Mar 2021 16:49:31 -0700 Subject: [PATCH 1/5] wasmtime-wasi: re-export wasi-cap-std-sync --- Cargo.lock | 1 + crates/wasi/Cargo.toml | 5 +++++ crates/wasi/src/lib.rs | 8 ++++++++ 3 files changed, 14 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 9aa8863d4378..88e88c82e59b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3513,6 +3513,7 @@ name = "wasmtime-wasi" version = "0.25.0" dependencies = [ "anyhow", + "wasi-cap-std-sync", "wasi-common", "wasmtime", "wasmtime-wiggle", diff --git a/crates/wasi/Cargo.toml b/crates/wasi/Cargo.toml index 5937ec55a292..a6db19e5b458 100644 --- a/crates/wasi/Cargo.toml +++ b/crates/wasi/Cargo.toml @@ -14,7 +14,12 @@ build = "build.rs" [dependencies] wasi-common = { path = "../wasi-common", version = "0.25.0" } +wasi-cap-std-sync = { path = "../wasi-common/cap-std-sync", version = "0.25.0", optional = true } wiggle = { path = "../wiggle", default-features = false, version = "0.25.0" } wasmtime-wiggle = { path = "../wiggle/wasmtime", default-features = false, version = "0.25.0" } wasmtime = { path = "../wasmtime", default-features = false, version = "0.25.0" } anyhow = "1.0" + +[features] +default = ["sync"] +sync = ["wasi-cap-std-sync"] diff --git a/crates/wasi/src/lib.rs b/crates/wasi/src/lib.rs index 1c3ad448ad58..2b71d7ab371b 100644 --- a/crates/wasi/src/lib.rs +++ b/crates/wasi/src/lib.rs @@ -12,6 +12,14 @@ use std::rc::Rc; pub use wasi_common::{Error, WasiCtx, WasiCtxBuilder, WasiDir, WasiFile}; use wasmtime::{Config, Linker, Store}; +/// Re-export the commonly used wasi-cap-std-sync crate here. This saves +/// consumers of this library from having to keep additional dependencies +/// in sync. +#[cfg(feature = "sync")] +pub mod sync { + pub use wasi_cap_std_sync::*; +} + /// An instantiated instance of all available wasi exports. Presently includes /// both the "preview1" snapshot and the "unstable" (preview0) snapshot. pub struct Wasi { From 183ee9d6d89372b11488d8850ad8cd708aea87e3 Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Thu, 25 Mar 2021 16:52:15 -0700 Subject: [PATCH 2/5] wasmtime cli: use wasmtime_wasi's re-exports more deliberately this drops the direct dep on wasi-cap-std-sync and wasi-common. --- Cargo.lock | 2 -- Cargo.toml | 2 -- crates/c-api/src/wasi.rs | 5 ++--- src/commands/run.rs | 3 +-- 4 files changed, 3 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 88e88c82e59b..bb27126f3aba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3285,8 +3285,6 @@ dependencies = [ "tempfile", "test-programs", "tracing-subscriber", - "wasi-cap-std-sync", - "wasi-common", "wasmparser", "wasmtime", "wasmtime-cache", diff --git a/Cargo.toml b/Cargo.toml index 6bcd156b6005..dcd82bac6b08 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,8 +32,6 @@ wasmtime-wast = { path = "crates/wast", version = "0.25.0" } wasmtime-wasi = { path = "crates/wasi", version = "0.25.0" } wasmtime-wasi-crypto = { path = "crates/wasi-crypto", version = "0.25.0", optional = true } wasmtime-wasi-nn = { path = "crates/wasi-nn", version = "0.25.0", optional = true } -wasi-common = { path = "crates/wasi-common", version = "0.25.0" } -wasi-cap-std-sync = { path = "crates/wasi-common/cap-std-sync", version = "0.25.0" } structopt = { version = "0.3.5", features = ["color", "suggestions"] } object = { version = "0.23.0", default-features = false, features = ["write"] } anyhow = "1.0.19" diff --git a/crates/c-api/src/wasi.rs b/crates/c-api/src/wasi.rs index 8fa15f0c20b2..828779ea8d4f 100644 --- a/crates/c-api/src/wasi.rs +++ b/crates/c-api/src/wasi.rs @@ -11,11 +11,10 @@ use std::path::{Path, PathBuf}; use std::rc::Rc; use std::slice; use std::str; -use wasi_cap_std_sync::WasiCtxBuilder; -use wasi_common::WasiCtx; use wasmtime::{Extern, Linker, Trap}; use wasmtime_wasi::{ snapshots::preview_0::Wasi as WasiSnapshot0, snapshots::preview_1::Wasi as WasiPreview1, + sync::WasiCtxBuilder, WasiCtx, }; unsafe fn cstr_to_path<'a>(path: *const c_char) -> Option<&'a Path> { @@ -186,7 +185,7 @@ pub unsafe extern "C" fn wasi_config_preopen_dir( }; let dir = match cstr_to_path(path) { - Some(p) => match cap_std::fs::Dir::open_ambient_dir(p) { + Some(p) => match Dir::open_ambient_dir(p) { Ok(d) => d, Err(_) => return false, }, diff --git a/src/commands/run.rs b/src/commands/run.rs index 89f9ec120f77..33a5a255add1 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -11,9 +11,8 @@ use std::{ process, }; use structopt::{clap::AppSettings, StructOpt}; -use wasi_cap_std_sync::WasiCtxBuilder; use wasmtime::{Engine, Func, Linker, Module, Store, Trap, Val, ValType}; -use wasmtime_wasi::Wasi; +use wasmtime_wasi::{sync::WasiCtxBuilder, Wasi}; #[cfg(feature = "wasi-nn")] use wasmtime_wasi_nn::{WasiNn, WasiNnCtx}; From 07245a87636aab1be2a7436cc313f3fb9a8127fe Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Thu, 25 Mar 2021 16:56:34 -0700 Subject: [PATCH 3/5] wasi-cap-std-sync: re-export Dir while we're at it --- crates/wasi-common/cap-std-sync/src/lib.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/wasi-common/cap-std-sync/src/lib.rs b/crates/wasi-common/cap-std-sync/src/lib.rs index fe38951296c8..ac8ee602e53d 100644 --- a/crates/wasi-common/cap-std-sync/src/lib.rs +++ b/crates/wasi-common/cap-std-sync/src/lib.rs @@ -14,6 +14,10 @@ //! `cap_std::fs::Dir`, and provides convenience methods for inheriting the //! parent process's stdio, args, and env. //! +//! For the convenience of consumers, `cap_std::fs::Dir` is re-exported from +//! this crate. This saves consumers tracking an additional dep on the exact +//! version of cap_std used by this crate, if they want to avoid it. +//! //! The only place we expect to run into long-term compatibility issues //! between `wasi-cap-std-sync` and the other impl crates that will come later //! is in the `Sched` abstraction. Once we can build an async scheduler based @@ -33,6 +37,7 @@ pub mod file; pub mod sched; pub mod stdio; +pub use cap_std::fs::Dir; pub use clocks::clocks_ctx; pub use sched::sched_ctx; @@ -110,13 +115,9 @@ impl WasiCtxBuilder { pub fn inherit_stdio(self) -> Self { self.inherit_stdin().inherit_stdout().inherit_stderr() } - pub fn preopened_dir( - self, - dir: cap_std::fs::Dir, - path: impl AsRef, - ) -> Result { + pub fn preopened_dir(self, dir: Dir, guest_path: impl AsRef) -> Result { let dir = Box::new(crate::dir::Dir::from_cap_std(dir)); - Ok(WasiCtxBuilder(self.0.preopened_dir(dir, path)?)) + Ok(WasiCtxBuilder(self.0.preopened_dir(dir, guest_path)?)) } pub fn build(self) -> Result { self.0.build() From af7030197d39fa76f9b3c93597b6a01c821c246e Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Thu, 25 Mar 2021 16:58:06 -0700 Subject: [PATCH 4/5] wasmtime-cli: drop direct dep on cap_std by using re-export --- Cargo.lock | 1 - Cargo.toml | 1 - crates/c-api/src/wasi.rs | 7 ++++--- src/commands/run.rs | 6 ++++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bb27126f3aba..0e40dba03d7a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3269,7 +3269,6 @@ name = "wasmtime-cli" version = "0.25.0" dependencies = [ "anyhow", - "cap-std", "env_logger 0.8.3", "file-per-thread-logger", "filecheck", diff --git a/Cargo.toml b/Cargo.toml index dcd82bac6b08..be37d8cec7a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,7 +44,6 @@ log = "0.4.8" rayon = "1.2.1" humantime = "2.0.0" wasmparser = "0.77.0" -cap-std = "0.13" [dev-dependencies] env_logger = "0.8.1" diff --git a/crates/c-api/src/wasi.rs b/crates/c-api/src/wasi.rs index 828779ea8d4f..441fa6fa5437 100644 --- a/crates/c-api/src/wasi.rs +++ b/crates/c-api/src/wasi.rs @@ -1,7 +1,6 @@ //! The WASI embedding API definitions for Wasmtime. use crate::{wasm_extern_t, wasm_importtype_t, wasm_store_t, wasm_trap_t}; use anyhow::Result; -use cap_std::fs::Dir; use std::cell::RefCell; use std::collections::HashMap; use std::ffi::CStr; @@ -13,8 +12,10 @@ use std::slice; use std::str; use wasmtime::{Extern, Linker, Trap}; use wasmtime_wasi::{ - snapshots::preview_0::Wasi as WasiSnapshot0, snapshots::preview_1::Wasi as WasiPreview1, - sync::WasiCtxBuilder, WasiCtx, + snapshots::preview_0::Wasi as WasiSnapshot0, + snapshots::preview_1::Wasi as WasiPreview1, + sync::{Dir, WasiCtxBuilder}, + WasiCtx, }; unsafe fn cstr_to_path<'a>(path: *const c_char) -> Option<&'a Path> { diff --git a/src/commands/run.rs b/src/commands/run.rs index 33a5a255add1..f8b701014689 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -2,7 +2,6 @@ use crate::{init_file_per_thread_logger, CommonOptions}; use anyhow::{bail, Context as _, Result}; -use cap_std::fs::Dir; use std::thread; use std::time::Duration; use std::{ @@ -12,7 +11,10 @@ use std::{ }; use structopt::{clap::AppSettings, StructOpt}; use wasmtime::{Engine, Func, Linker, Module, Store, Trap, Val, ValType}; -use wasmtime_wasi::{sync::WasiCtxBuilder, Wasi}; +use wasmtime_wasi::{ + sync::{Dir, WasiCtxBuilder}, + Wasi, +}; #[cfg(feature = "wasi-nn")] use wasmtime_wasi_nn::{WasiNn, WasiNnCtx}; From ae4c5a9d7fe531041a4896ff4e7d30fa6a50db0e Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Fri, 26 Mar 2021 15:37:57 -0700 Subject: [PATCH 5/5] fixes in tests and examples --- examples/linking.rs | 3 +-- examples/wasi/main.rs | 3 +-- tests/all/host_funcs.rs | 3 +-- tests/all/traps.rs | 2 +- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/examples/linking.rs b/examples/linking.rs index 11f4022bcd8e..b4cee53a4a55 100644 --- a/examples/linking.rs +++ b/examples/linking.rs @@ -3,9 +3,8 @@ // You can execute this example with `cargo run --example linking` use anyhow::Result; -use wasi_cap_std_sync::WasiCtxBuilder; use wasmtime::*; -use wasmtime_wasi::Wasi; +use wasmtime_wasi::{sync::WasiCtxBuilder, Wasi}; fn main() -> Result<()> { let engine = Engine::default(); diff --git a/examples/wasi/main.rs b/examples/wasi/main.rs index f1487171f71f..f808be3eca2b 100644 --- a/examples/wasi/main.rs +++ b/examples/wasi/main.rs @@ -4,9 +4,8 @@ // You can execute this example with `cargo run --example wasi` use anyhow::Result; -use wasi_cap_std_sync::WasiCtxBuilder; use wasmtime::*; -use wasmtime_wasi::Wasi; +use wasmtime_wasi::{sync::WasiCtxBuilder, Wasi}; fn main() -> Result<()> { tracing_subscriber::FmtSubscriber::builder() diff --git a/tests/all/host_funcs.rs b/tests/all/host_funcs.rs index 26d43299417d..360e2526adee 100644 --- a/tests/all/host_funcs.rs +++ b/tests/all/host_funcs.rs @@ -1,8 +1,7 @@ use anyhow::Result; use std::sync::atomic::{AtomicUsize, Ordering::SeqCst}; -use wasi_cap_std_sync::WasiCtxBuilder; use wasmtime::*; -use wasmtime_wasi::Wasi; +use wasmtime_wasi::{sync::WasiCtxBuilder, Wasi}; #[test] fn async_required() { diff --git a/tests/all/traps.rs b/tests/all/traps.rs index b83ac38daa32..a4686e907378 100644 --- a/tests/all/traps.rs +++ b/tests/all/traps.rs @@ -512,7 +512,7 @@ fn parse_dwarf_info() -> Result<()> { let mut linker = Linker::new(&store); wasmtime_wasi::Wasi::new( &store, - wasi_cap_std_sync::WasiCtxBuilder::new() + wasmtime_wasi::sync::WasiCtxBuilder::new() .inherit_stdio() .build()?, )