Skip to content

Commit

Permalink
Make wasi-common self-contained, deprecate exports from wasmtime-wasi (
Browse files Browse the repository at this point in the history
…#7881)

* WIP: try to make wasi-common self contained.

* rebase: cargo.lock

* remove all dependencies between wasi-common and wasmtime-wasi

* use wasi-common directly throughout tests, benches, examples, cli run

* wasi-threads: use wasi-common's maybe_exit_on_error in spawned thread

not a very modular design, but at this point wasi-common and
wasi-threads are forever wed

* fix wasmtime's docs

* re-introduce wasmtime-wasi's exports of wasi-common definitions behind deprecated

* factor out determining i32 process exit code

and remove libc dep because rustix provides the same constant

* commands/run: inline the logic about aborting on trap

since this is the sole place in the codebase its used

* Add high-level summary to wasi-common's top-level doc comment.

* c-api: fix use of wasi_cap_std_sync => wasi_common::sync, wasmtime_wasi => wasi_common

* fix tokio example

* think better of combining downcast and masking into one method

* fix references to wasmtime_wasi in docs

prtest:full

* benches: use wasi-common

* cfg-if around use of rustix::process because that doesnt exist on windows

* wasi-common: include tests, caught by verify-publish

* fix another bench

* exit requires wasmtime dep. caught by verify-publish.
  • Loading branch information
Pat Hickey committed Feb 13, 2024
1 parent 69e9470 commit c869304
Show file tree
Hide file tree
Showing 60 changed files with 514 additions and 1,168 deletions.
72 changes: 10 additions & 62 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ wasmtime-cranelift = { workspace = true, optional = true }
wasmtime-environ = { workspace = true }
wasmtime-explorer = { workspace = true, optional = true }
wasmtime-wast = { workspace = true, optional = true }
wasi-common = { workspace = true, default-features = true, features = [
"exit",
] }
wasmtime-wasi = { workspace = true, default-features = true, features = [
"exit",
] }
Expand All @@ -55,13 +58,14 @@ humantime = { workspace = true }

async-trait = { workspace = true }
bytes = { workspace = true }
cfg-if = { workspace = true }
tokio = { workspace = true, optional = true, features = [ "signal", "macros" ] }
hyper = { workspace = true, optional = true }
http = { workspace = true, optional = true }
http-body-util = { workspace = true, optional = true }

[target.'cfg(unix)'.dependencies]
rustix = { workspace = true, features = ["mm", "param"] }
rustix = { workspace = true, features = ["mm", "param", "process"] }

[dev-dependencies]
# depend again on wasmtime to activate its default features for tests
Expand Down Expand Up @@ -181,8 +185,6 @@ wiggle = { path = "crates/wiggle", version = "=18.0.0", default-features = false
wiggle-macro = { path = "crates/wiggle/macro", version = "=18.0.0" }
wiggle-generate = { path = "crates/wiggle/generate", version = "=18.0.0" }
wasi-common = { path = "crates/wasi-common", version = "=18.0.0", default-features = false }
wasi-tokio = { path = "crates/wasi-common/tokio", version = "=18.0.0" }
wasi-cap-std-sync = { path = "crates/wasi-common/cap-std-sync", version = "=18.0.0" }
wasmtime-fuzzing = { path = "crates/fuzzing" }
wasmtime-jit-icache-coherence = { path = "crates/jit-icache-coherence", version = "=18.0.0" }
wasmtime-wit-bindgen = { path = "crates/wit-bindgen", version = "=18.0.0" }
Expand Down Expand Up @@ -358,7 +360,7 @@ disable-logging = ["log/max_level_off", "tracing/max_level_off"]
# the internal mapping for what they enable in Wasmtime itself.
wasi-nn = ["dep:wasmtime-wasi-nn"]
wasi-threads = ["dep:wasmtime-wasi-threads"]
wasi-http = ["component-model", "dep:wasmtime-wasi-http", "dep:tokio", "dep:hyper", "wasmtime-wasi-http?/sync"]
wasi-http = ["component-model", "dep:wasmtime-wasi-http", "dep:tokio", "dep:hyper"]
pooling-allocator = ["wasmtime/pooling-allocator", "wasmtime-cli-flags/pooling-allocator"]
component-model = [
"wasmtime/component-model",
Expand Down
6 changes: 3 additions & 3 deletions benches/instantiation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::process::Command;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};
use std::sync::Arc;
use std::thread;
use wasi_common::{sync::WasiCtxBuilder, WasiCtx};
use wasmtime::*;
use wasmtime_wasi::{sync::WasiCtxBuilder, WasiCtx};

fn store(engine: &Engine) -> Store<WasiCtx> {
let wasi = WasiCtxBuilder::new().build();
Expand Down Expand Up @@ -48,7 +48,7 @@ fn bench_sequential(c: &mut Criterion, path: &Path) {
// benchmark programs.
linker.func_wrap("bench", "start", || {}).unwrap();
linker.func_wrap("bench", "end", || {}).unwrap();
wasmtime_wasi::add_to_linker(&mut linker, |cx| cx).unwrap();
wasi_common::sync::add_to_linker(&mut linker, |cx| cx).unwrap();
let pre = linker
.instantiate_pre(&module)
.expect("failed to pre-instantiate");
Expand Down Expand Up @@ -82,7 +82,7 @@ fn bench_parallel(c: &mut Criterion, path: &Path) {
// benchmark programs.
linker.func_wrap("bench", "start", || {}).unwrap();
linker.func_wrap("bench", "end", || {}).unwrap();
wasmtime_wasi::add_to_linker(&mut linker, |cx| cx).unwrap();
wasi_common::sync::add_to_linker(&mut linker, |cx| cx).unwrap();
let pre = Arc::new(
linker
.instantiate_pre(&module)
Expand Down
8 changes: 4 additions & 4 deletions benches/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use criterion::{criterion_group, criterion_main, Criterion};
use std::{fs::File, path::Path, time::Instant};
use wasi_common::{sync::WasiCtxBuilder, WasiCtx};
use wasmtime::{Engine, Linker, Module, Store, TypedFunc};
use wasmtime_wasi::{sync::WasiCtxBuilder, WasiCtx};

criterion_group!(benches, bench_wasi);
criterion_main!(benches);
Expand Down Expand Up @@ -53,7 +53,7 @@ fn instantiate(wat: &[u8]) -> (Store<WasiCtx>, TypedFunc<u64, u64>) {
let mut store = Store::new(&engine, wasi);
let module = Module::new(&engine, wat).unwrap();
let mut linker = Linker::new(&engine);
wasmtime_wasi::add_to_linker(&mut linker, |cx| cx).unwrap();
wasi_common::sync::add_to_linker(&mut linker, |cx| cx).unwrap();
let instance = linker.instantiate(&mut store, &module).unwrap();
let run = instance.get_typed_func(&mut store, "run").unwrap();
(store, run)
Expand All @@ -77,9 +77,9 @@ fn wasi_context() -> WasiCtx {
])
.unwrap()
.preopened_dir(
wasmtime_wasi::Dir::open_ambient_dir(
wasi_common::sync::Dir::open_ambient_dir(
"benches/wasi",
wasmtime_wasi::ambient_authority(),
wasi_common::sync::ambient_authority(),
)
.unwrap(),
"/",
Expand Down
3 changes: 1 addition & 2 deletions crates/bench-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ wasmtime = { workspace = true, default-features = true }
wasmtime-cli-flags = { workspace = true, default-features = true, features = [
"cranelift",
] }
wasmtime-wasi = { workspace = true, default-features = true }
wasi-common = { workspace = true, default-features = true }
wasmtime-wasi-nn = { workspace = true, optional = true }
wasi-cap-std-sync = { workspace = true }
cap-std = { workspace = true }
clap = { workspace = true }

Expand Down
10 changes: 5 additions & 5 deletions crates/bench-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ use std::os::raw::{c_int, c_void};
use std::slice;
use std::{env, path::PathBuf};
use target_lexicon::Triple;
use wasi_common::{sync::WasiCtxBuilder, I32Exit, WasiCtx};
use wasmtime::{Engine, Instance, Linker, Module, Store};
use wasmtime_cli_flags::CommonOptions;
use wasmtime_wasi::{sync::WasiCtxBuilder, I32Exit, WasiCtx};

pub type ExitCode = c_int;
pub const OK: ExitCode = 0;
Expand Down Expand Up @@ -304,20 +304,20 @@ pub extern "C" fn wasm_bench_create(
let stdout = std::fs::File::create(&stdout_path)
.with_context(|| format!("failed to create {}", stdout_path.display()))?;
let stdout = cap_std::fs::File::from_std(stdout);
let stdout = wasi_cap_std_sync::file::File::from_cap_std(stdout);
let stdout = wasi_common::sync::file::File::from_cap_std(stdout);
cx.stdout(Box::new(stdout));

let stderr = std::fs::File::create(&stderr_path)
.with_context(|| format!("failed to create {}", stderr_path.display()))?;
let stderr = cap_std::fs::File::from_std(stderr);
let stderr = wasi_cap_std_sync::file::File::from_cap_std(stderr);
let stderr = wasi_common::sync::file::File::from_cap_std(stderr);
cx.stderr(Box::new(stderr));

if let Some(stdin_path) = &stdin_path {
let stdin = std::fs::File::open(stdin_path)
.with_context(|| format!("failed to open {}", stdin_path.display()))?;
let stdin = cap_std::fs::File::from_std(stdin);
let stdin = wasi_cap_std_sync::file::File::from_cap_std(stdin);
let stdin = wasi_common::sync::file::File::from_cap_std(stdin);
cx.stdin(Box::new(stdin));
}

Expand Down Expand Up @@ -460,7 +460,7 @@ impl BenchState {
let fuel = options.wasm.fuel;

if options.wasi.common != Some(false) {
wasmtime_wasi::add_to_linker(&mut linker, |cx| &mut cx.wasi)?;
wasi_common::sync::add_to_linker(&mut linker, |cx| &mut cx.wasi)?;
}

#[cfg(feature = "wasi-nn")]
Expand Down
5 changes: 2 additions & 3 deletions crates/c-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ tracing = { workspace = true }
wat = { workspace = true, optional = true }

# Optional dependencies for the `wasi` feature
wasi-cap-std-sync = { workspace = true, optional = true }
wasmtime-wasi = { workspace = true, default-features = true, optional = true }
cap-std = { workspace = true, optional = true }
wasi-common = { workspace = true, optional = true }
wasi-common = { workspace = true, optional = true, features = ["sync"] }

# Optional dependencies for the `async` feature
futures = { workspace = true, optional = true }
Expand All @@ -44,7 +43,7 @@ async = ['wasmtime/async', 'futures']
profiling = ["wasmtime/profiling"]
cache = ["wasmtime/cache"]
parallel-compilation = ['wasmtime/parallel-compilation']
wasi = ['wasi-cap-std-sync', 'wasmtime-wasi', 'cap-std', 'wasi-common']
wasi = ['wasmtime-wasi', 'cap-std', 'wasi-common']
logging = ['dep:env_logger']
disable-logging = ["log/max_level_off", "tracing/max_level_off"]
coredump = ["wasmtime/coredump"]
Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub extern "C" fn wasmtime_error_message(error: &wasmtime_error_t, message: &mut
#[no_mangle]
pub extern "C" fn wasmtime_error_exit_status(raw: &wasmtime_error_t, status: &mut i32) -> bool {
#[cfg(feature = "wasi")]
if let Some(exit) = raw.error.downcast_ref::<wasmtime_wasi::I32Exit>() {
if let Some(exit) = raw.error.downcast_ref::<wasi_common::I32Exit>() {
*status = exit.0;
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub type CStoreContextMut<'a> = StoreContextMut<'a, StoreData>;
pub struct StoreData {
foreign: crate::ForeignData,
#[cfg(feature = "wasi")]
pub(crate) wasi: Option<wasmtime_wasi::WasiCtx>,
pub(crate) wasi: Option<wasi_common::WasiCtx>,

/// Temporary storage for usage during a wasm->host call to store values
/// in a slice we pass to the C API.
Expand Down
Loading

0 comments on commit c869304

Please sign in to comment.