Skip to content

Commit f7a5aa3

Browse files
authored
Unify WASIp{2,3} context structures (#11370)
This removes `wasmtime_wasi::p{2,3}::{WasiCtx, WasiCtxBuilder, WasiView}` in favor of only having `wasmtime_wasi::{WasiCtx, WasiCtxBuilder, WasiView}` instead. Conceptually these revisions of WASI all provide the same functionality just with a different veneer that the component model offers, so having only one way to configure host-side behavior will make it easier to both organize implementations internally (e.g. more sharing of code) as well as for embedders to configure (only one context to create/manage).
1 parent 7ccd258 commit f7a5aa3

File tree

60 files changed

+506
-1380
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+506
-1380
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benches/instantiation.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ use std::process::Command;
66
use std::sync::Arc;
77
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};
88
use std::thread;
9-
use wasi_common::{WasiCtx, sync::WasiCtxBuilder};
109
use wasmtime::*;
10+
use wasmtime_wasi::{WasiCtx, preview1::WasiP1Ctx};
1111

12-
fn store(engine: &Engine) -> Store<WasiCtx> {
13-
let wasi = WasiCtxBuilder::new().build();
12+
fn store(engine: &Engine) -> Store<WasiP1Ctx> {
13+
let wasi = WasiCtx::builder().build_p1();
1414
Store::new(engine, wasi)
1515
}
1616

17-
fn instantiate(pre: &InstancePre<WasiCtx>, engine: &Engine) -> Result<()> {
17+
fn instantiate(pre: &InstancePre<WasiP1Ctx>, engine: &Engine) -> Result<()> {
1818
let mut store = store(engine);
1919
let _instance = pre.instantiate(&mut store)?;
2020
Ok(())
@@ -49,7 +49,7 @@ fn bench_sequential(c: &mut Criterion, path: &Path) {
4949
// benchmark programs.
5050
linker.func_wrap("bench", "start", || {}).unwrap();
5151
linker.func_wrap("bench", "end", || {}).unwrap();
52-
wasi_common::sync::add_to_linker(&mut linker, |cx| cx).unwrap();
52+
wasmtime_wasi::preview1::add_to_linker_sync(&mut linker, |cx| cx).unwrap();
5353
let pre = linker
5454
.instantiate_pre(&module)
5555
.expect("failed to pre-instantiate");
@@ -83,7 +83,7 @@ fn bench_parallel(c: &mut Criterion, path: &Path) {
8383
// benchmark programs.
8484
linker.func_wrap("bench", "start", || {}).unwrap();
8585
linker.func_wrap("bench", "end", || {}).unwrap();
86-
wasi_common::sync::add_to_linker(&mut linker, |cx| cx).unwrap();
86+
wasmtime_wasi::preview1::add_to_linker_sync(&mut linker, |cx| cx).unwrap();
8787
let pre = Arc::new(
8888
linker
8989
.instantiate_pre(&module)

benches/wasi.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use criterion::{Criterion, criterion_group, criterion_main};
44
use std::{fs::File, path::Path, time::Instant};
5-
use wasi_common::{WasiCtx, sync::WasiCtxBuilder};
65
use wasmtime::{Engine, Linker, Module, Store, TypedFunc};
6+
use wasmtime_wasi::{DirPerms, FilePerms, WasiCtx, preview1::WasiP1Ctx};
77

88
criterion_group!(benches, bench_wasi);
99
criterion_main!(benches);
@@ -47,43 +47,34 @@ fn bench_wasi(c: &mut Criterion) {
4747
/// - execute the body of the function for that number of loop iterations
4848
/// - return a single `u64` indicating how many loop iterations were executed
4949
/// (to double-check)
50-
fn instantiate(wat: &[u8]) -> (Store<WasiCtx>, TypedFunc<u64, u64>) {
50+
fn instantiate(wat: &[u8]) -> (Store<WasiP1Ctx>, TypedFunc<u64, u64>) {
5151
let engine = Engine::default();
5252
let wasi = wasi_context();
5353
let mut store = Store::new(&engine, wasi);
5454
let module = Module::new(&engine, wat).unwrap();
5555
let mut linker = Linker::new(&engine);
56-
wasi_common::sync::add_to_linker(&mut linker, |cx| cx).unwrap();
56+
wasmtime_wasi::preview1::add_to_linker_sync(&mut linker, |cx| cx).unwrap();
5757
let instance = linker.instantiate(&mut store, &module).unwrap();
5858
let run = instance.get_typed_func(&mut store, "run").unwrap();
5959
(store, run)
6060
}
6161

6262
/// Build a WASI context with some actual data to retrieve.
63-
fn wasi_context() -> WasiCtx {
64-
WasiCtxBuilder::new()
63+
fn wasi_context() -> WasiP1Ctx {
64+
WasiCtx::builder()
6565
.envs(&[
6666
("a".to_string(), "b".to_string()),
6767
("b".to_string(), "c".to_string()),
6868
("c".to_string(), "d".to_string()),
6969
])
70-
.unwrap()
7170
.args(&[
7271
"exe".to_string(),
7372
"--flag1".to_string(),
7473
"--flag2".to_string(),
7574
"--flag3".to_string(),
7675
"--flag4".to_string(),
7776
])
77+
.preopened_dir("benches/wasi", "/", DirPerms::READ, FilePerms::READ)
7878
.unwrap()
79-
.preopened_dir(
80-
wasi_common::sync::Dir::open_ambient_dir(
81-
"benches/wasi",
82-
wasi_common::sync::ambient_authority(),
83-
)
84-
.unwrap(),
85-
"/",
86-
)
87-
.unwrap()
88-
.build()
79+
.build_p1()
8980
}

crates/bench-api/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ wasmtime = { workspace = true, default-features = true, features = ["winch", "pu
2626
wasmtime-cli-flags = { workspace = true, default-features = true, features = [
2727
"cranelift",
2828
] }
29-
wasi-common = { workspace = true, default-features = true }
29+
wasmtime-wasi = { workspace = true, features = ['preview1'] }
3030
wasmtime-wasi-nn = { workspace = true, optional = true }
3131
cap-std = { workspace = true }
3232
clap = { workspace = true }

crates/bench-api/src/lib.rs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,10 @@ use clap::Parser;
141141
use std::os::raw::{c_int, c_void};
142142
use std::slice;
143143
use std::{env, path::PathBuf};
144-
use wasi_common::{I32Exit, WasiCtx, sync::WasiCtxBuilder};
145144
use wasmtime::{Engine, Instance, Linker, Module, Store};
146145
use wasmtime_cli_flags::CommonOptions;
146+
use wasmtime_wasi::cli::{InputFile, OutputFile};
147+
use wasmtime_wasi::{DirPerms, FilePerms, I32Exit, WasiCtx, preview1::WasiP1Ctx};
147148

148149
pub type ExitCode = c_int;
149150
pub const OK: ExitCode = 0;
@@ -272,15 +273,6 @@ pub extern "C" fn wasm_bench_create(
272273
) -> ExitCode {
273274
let result = (|| -> Result<_> {
274275
let working_dir = config.working_dir()?;
275-
let working_dir =
276-
cap_std::fs::Dir::open_ambient_dir(&working_dir, cap_std::ambient_authority())
277-
.with_context(|| {
278-
format!(
279-
"failed to preopen the working directory: {}",
280-
working_dir.display(),
281-
)
282-
})?;
283-
284276
let stdout_path = config.stdout_path()?;
285277
let stderr_path = config.stderr_path()?;
286278
let stdin_path = config.stdin_path()?;
@@ -298,39 +290,33 @@ pub extern "C" fn wasm_bench_create(
298290
config.execution_start,
299291
config.execution_end,
300292
move || {
301-
let mut cx = WasiCtxBuilder::new();
293+
let mut cx = WasiCtx::builder();
302294

303295
let stdout = std::fs::File::create(&stdout_path)
304296
.with_context(|| format!("failed to create {}", stdout_path.display()))?;
305-
let stdout = cap_std::fs::File::from_std(stdout);
306-
let stdout = wasi_common::sync::file::File::from_cap_std(stdout);
307-
cx.stdout(Box::new(stdout));
297+
cx.stdout(OutputFile::new(stdout));
308298

309299
let stderr = std::fs::File::create(&stderr_path)
310300
.with_context(|| format!("failed to create {}", stderr_path.display()))?;
311-
let stderr = cap_std::fs::File::from_std(stderr);
312-
let stderr = wasi_common::sync::file::File::from_cap_std(stderr);
313-
cx.stderr(Box::new(stderr));
301+
cx.stderr(OutputFile::new(stderr));
314302

315303
if let Some(stdin_path) = &stdin_path {
316304
let stdin = std::fs::File::open(stdin_path)
317305
.with_context(|| format!("failed to open {}", stdin_path.display()))?;
318-
let stdin = cap_std::fs::File::from_std(stdin);
319-
let stdin = wasi_common::sync::file::File::from_cap_std(stdin);
320-
cx.stdin(Box::new(stdin));
306+
cx.stdin(InputFile::new(stdin));
321307
}
322308

323309
// Allow access to the working directory so that the benchmark can read
324310
// its input workload(s).
325-
cx.preopened_dir(working_dir.try_clone()?, ".")?;
311+
cx.preopened_dir(working_dir.clone(), ".", DirPerms::READ, FilePerms::READ)?;
326312

327313
// Pass this env var along so that the benchmark program can use smaller
328314
// input workload(s) if it has them and that has been requested.
329315
if let Ok(val) = env::var("WASM_BENCH_USE_SMALL_WORKLOAD") {
330-
cx.env("WASM_BENCH_USE_SMALL_WORKLOAD", &val)?;
316+
cx.env("WASM_BENCH_USE_SMALL_WORKLOAD", &val);
331317
}
332318

333-
Ok(cx.build())
319+
Ok(cx.build_p1())
334320
},
335321
)?);
336322
Ok(Box::into_raw(state) as _)
@@ -407,15 +393,15 @@ struct BenchState {
407393
instantiation_timer: *mut u8,
408394
instantiation_start: extern "C" fn(*mut u8),
409395
instantiation_end: extern "C" fn(*mut u8),
410-
make_wasi_cx: Box<dyn FnMut() -> Result<WasiCtx>>,
396+
make_wasi_cx: Box<dyn FnMut() -> Result<WasiP1Ctx>>,
411397
module: Option<Module>,
412398
store_and_instance: Option<(Store<HostState>, Instance)>,
413399
epoch_interruption: bool,
414400
fuel: Option<u64>,
415401
}
416402

417403
struct HostState {
418-
wasi: WasiCtx,
404+
wasi: WasiP1Ctx,
419405
#[cfg(feature = "wasi-nn")]
420406
wasi_nn: wasmtime_wasi_nn::witx::WasiNnCtx,
421407
}
@@ -432,7 +418,7 @@ impl BenchState {
432418
execution_timer: *mut u8,
433419
execution_start: extern "C" fn(*mut u8),
434420
execution_end: extern "C" fn(*mut u8),
435-
make_wasi_cx: impl FnMut() -> Result<WasiCtx> + 'static,
421+
make_wasi_cx: impl FnMut() -> Result<WasiP1Ctx> + 'static,
436422
) -> Result<Self> {
437423
let mut config = options.config(None)?;
438424
// NB: always disable the compilation cache.
@@ -459,7 +445,7 @@ impl BenchState {
459445
let fuel = options.wasm.fuel;
460446

461447
if options.wasi.common != Some(false) {
462-
wasi_common::sync::add_to_linker(&mut linker, |cx| &mut cx.wasi)?;
448+
wasmtime_wasi::preview1::add_to_linker_sync(&mut linker, |cx| &mut cx.wasi)?;
463449
}
464450

465451
#[cfg(feature = "wasi-nn")]

crates/c-api/src/store.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ pub struct WasmtimeStoreData {
100100
pub(crate) resource_table: wasmtime::component::ResourceTable,
101101

102102
#[cfg(all(feature = "component-model", feature = "wasi"))]
103-
pub(crate) wasip2: Option<wasmtime_wasi::p2::WasiCtx>,
103+
pub(crate) wasip2: Option<wasmtime_wasi::WasiCtx>,
104104
}
105105

106106
#[cfg(all(feature = "component-model", feature = "wasi"))]
107-
impl wasmtime_wasi::p2::WasiView for WasmtimeStoreData {
108-
fn ctx(&mut self) -> wasmtime_wasi::p2::WasiCtxView<'_> {
109-
wasmtime_wasi::p2::WasiCtxView {
107+
impl wasmtime_wasi::WasiView for WasmtimeStoreData {
108+
fn ctx(&mut self) -> wasmtime_wasi::WasiCtxView<'_> {
109+
wasmtime_wasi::WasiCtxView {
110110
ctx: self.wasip2.as_mut().unwrap(),
111111
table: &mut self.resource_table,
112112
}

crates/c-api/src/wasi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::ffi::{CStr, c_char};
66
use std::fs::File;
77
use std::path::Path;
88
use std::slice;
9-
use wasmtime_wasi::p2::WasiCtxBuilder;
9+
use wasmtime_wasi::WasiCtxBuilder;
1010
use wasmtime_wasi::preview1::WasiP1Ctx;
1111

1212
unsafe fn cstr_to_path<'a>(path: *const c_char) -> Option<&'a Path> {

crates/c-api/src/wasip2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#[repr(transparent)]
22
pub struct wasmtime_wasip2_config_t {
3-
pub(crate) builder: wasmtime_wasi::p2::WasiCtxBuilder,
3+
pub(crate) builder: wasmtime_wasi::WasiCtxBuilder,
44
}
55

66
#[unsafe(no_mangle)]
77
pub unsafe extern "C" fn wasmtime_wasip2_config_new() -> Box<wasmtime_wasip2_config_t> {
88
Box::new(wasmtime_wasip2_config_t {
9-
builder: wasmtime_wasi::p2::WasiCtxBuilder::new(),
9+
builder: wasmtime_wasi::WasiCtxBuilder::new(),
1010
})
1111
}
1212

crates/misc/component-async-tests/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::sync::{Arc, Mutex};
44
use std::task::Waker;
55

66
use wasmtime::component::{HasData, ResourceTable};
7-
use wasmtime_wasi::p2::{WasiCtx, WasiCtxView, WasiView};
7+
use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
88

99
pub mod borrowing_host;
1010
pub mod closed_streams;

crates/misc/component-async-tests/tests/scenario/borrowing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use anyhow::Result;
77
use futures::stream::{FuturesUnordered, TryStreamExt};
88
use wasmtime::component::{Linker, ResourceTable};
99
use wasmtime::{Engine, Store};
10-
use wasmtime_wasi::p2::WasiCtxBuilder;
10+
use wasmtime_wasi::WasiCtxBuilder;
1111

1212
#[tokio::test]
1313
pub async fn async_borrowing_caller() -> Result<()> {

0 commit comments

Comments
 (0)