Skip to content

Commit

Permalink
only wasi_cap_std_sync and wasi_tokio need to define WasiCtxBuilders (#…
Browse files Browse the repository at this point in the history
…2917)

* wasmtime-wasi: re-exporting this WasiCtxBuilder was shadowing the right one

wasi-common's WasiCtxBuilder is really only useful wasi_cap_std_sync and
wasi_tokio to implement their own Builder on top of.

This re-export of wasi-common's is 1. not useful and 2. shadow's the
re-export of the right one in sync::*.

* wasi-common: eliminate WasiCtxBuilder, make the builder methods on WasiCtx instead

* delete wasi-common::WasiCtxBuilder altogether

just put those methods directly on &mut WasiCtx.

As a bonus, the sync and tokio WasiCtxBuilder::build functions
are no longer fallible!

* bench fixes

* more test fixes
  • Loading branch information
Pat Hickey authored May 21, 2021
1 parent 817d72a commit 0f5bdc6
Show file tree
Hide file tree
Showing 19 changed files with 114 additions and 138 deletions.
2 changes: 1 addition & 1 deletion benches/instantiation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn instantiate(module: &Module) -> Result<Instance> {
// As we don't actually invoke Wasm code in this benchmark, we still add
// the WASI context to the store as it is considered part of getting a
// module that depends on WASI "ready to run".
Wasi::set_context(&store, WasiCtxBuilder::new().build()?)
Wasi::set_context(&store, WasiCtxBuilder::new().build())
.map_err(|_| anyhow::anyhow!("wasi set_context failed"))?;

let linker = Linker::new(&store);
Expand Down
2 changes: 1 addition & 1 deletion crates/bench-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl BenchState {
cx = cx.env("WASM_BENCH_USE_SMALL_WORKLOAD", &val)?;
}

Wasi::new(linker.store(), cx.build()?).add_to_linker(&mut linker)?;
Wasi::new(linker.store(), cx.build()).add_to_linker(&mut linker)?;

#[cfg(feature = "wasi-nn")]
{
Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ fn create_wasi_ctx(config: wasi_config_t) -> Result<Rc<RefCell<WasiCtx>>> {
for (dir, path) in config.preopens {
builder = builder.preopened_dir(dir, path)?;
}
Ok(Rc::new(RefCell::new(builder.build()?)))
Ok(Rc::new(RefCell::new(builder.build())))
}

#[repr(C)]
Expand Down
2 changes: 1 addition & 1 deletion crates/misc/rust/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn generate_load(item: &syn::ItemTrait) -> syn::Result<TokenStream> {

let mut imports: Vec<Extern> = Vec::new();
if let Some(module_name) = data.find_wasi_module_name() {
let wasi_cx = #root::wasmtime_wasi::WasiCtxBuilder::new().build()?;
let wasi_cx = #root::wasmtime_wasi::WasiCtxBuilder::new().build();
let wasi = #root::wasmtime_wasi::Wasi::new(&store, wasi_cx);
for i in module.imports().iter() {
if i.module() != module_name {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn run(
// cap-std-sync does not yet support the sync family of fdflags
builder = builder.env("NO_FDFLAGS_SYNC_SUPPORT", "1")?;

let wasi = Wasi::new(&store, builder.build()?);
let wasi = Wasi::new(&store, builder.build());

let mut linker = Linker::new(&store);

Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/tests/wasm_tests/runtime/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn run(
// does not.
builder = builder.env("NO_FDFLAGS_SYNC_SUPPORT", "1")?;

Wasi::set_context(&store, builder.build()?)
Wasi::set_context(&store, builder.build())
.map_err(|_| anyhow::anyhow!("wasi set_context failed"))?;

let module =
Expand Down
68 changes: 34 additions & 34 deletions crates/wasi-common/cap-std-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,61 +47,60 @@ use std::path::Path;
use std::rc::Rc;
use wasi_common::{table::Table, Error, WasiCtx, WasiFile};

pub struct WasiCtxBuilder(wasi_common::WasiCtxBuilder);
pub struct WasiCtxBuilder(WasiCtx);

impl WasiCtxBuilder {
pub fn new() -> Self {
WasiCtxBuilder(WasiCtx::builder(
WasiCtxBuilder(WasiCtx::new(
random_ctx(),
clocks_ctx(),
sched_ctx(),
Rc::new(RefCell::new(Table::new())),
))
}
pub fn env(self, var: &str, value: &str) -> Result<Self, wasi_common::StringArrayError> {
let s = self.0.env(var, value)?;
Ok(WasiCtxBuilder(s))
pub fn env(mut self, var: &str, value: &str) -> Result<Self, wasi_common::StringArrayError> {
self.0.push_env(var, value)?;
Ok(self)
}
pub fn envs(self, env: &[(String, String)]) -> Result<Self, wasi_common::StringArrayError> {
let mut s = self;
pub fn envs(mut self, env: &[(String, String)]) -> Result<Self, wasi_common::StringArrayError> {
for (k, v) in env {
s = s.env(k, v)?;
self.0.push_env(k, v)?;
}
Ok(s)
Ok(self)
}
pub fn inherit_env(self) -> Result<Self, wasi_common::StringArrayError> {
let mut s = self.0;
pub fn inherit_env(mut self) -> Result<Self, wasi_common::StringArrayError> {
for (key, value) in std::env::vars() {
s = s.env(&key, &value)?;
self.0.push_env(&key, &value)?;
}
Ok(WasiCtxBuilder(s))
Ok(self)
}
pub fn arg(self, arg: &str) -> Result<Self, wasi_common::StringArrayError> {
let s = self.0.arg(arg)?;
Ok(WasiCtxBuilder(s))
pub fn arg(mut self, arg: &str) -> Result<Self, wasi_common::StringArrayError> {
self.0.push_arg(arg)?;
Ok(self)
}
pub fn args(self, arg: &[String]) -> Result<Self, wasi_common::StringArrayError> {
let mut s = self;
pub fn args(mut self, arg: &[String]) -> Result<Self, wasi_common::StringArrayError> {
for a in arg {
s = s.arg(&a)?;
self.0.push_arg(&a)?;
}
Ok(s)
Ok(self)
}
pub fn inherit_args(self) -> Result<Self, wasi_common::StringArrayError> {
let mut s = self.0;
pub fn inherit_args(mut self) -> Result<Self, wasi_common::StringArrayError> {
for arg in std::env::args() {
s = s.arg(&arg)?;
self.0.push_arg(&arg)?;
}
Ok(WasiCtxBuilder(s))
Ok(self)
}
pub fn stdin(self, f: Box<dyn WasiFile>) -> Self {
WasiCtxBuilder(self.0.stdin(f))
pub fn stdin(mut self, f: Box<dyn WasiFile>) -> Self {
self.0.set_stdin(f);
self
}
pub fn stdout(self, f: Box<dyn WasiFile>) -> Self {
WasiCtxBuilder(self.0.stdout(f))
pub fn stdout(mut self, f: Box<dyn WasiFile>) -> Self {
self.0.set_stdout(f);
self
}
pub fn stderr(self, f: Box<dyn WasiFile>) -> Self {
WasiCtxBuilder(self.0.stderr(f))
pub fn stderr(mut self, f: Box<dyn WasiFile>) -> Self {
self.0.set_stderr(f);
self
}
pub fn inherit_stdin(self) -> Self {
self.stdin(Box::new(crate::stdio::stdin()))
Expand All @@ -115,12 +114,13 @@ impl WasiCtxBuilder {
pub fn inherit_stdio(self) -> Self {
self.inherit_stdin().inherit_stdout().inherit_stderr()
}
pub fn preopened_dir(self, dir: Dir, guest_path: impl AsRef<Path>) -> Result<Self, Error> {
pub fn preopened_dir(mut self, dir: Dir, guest_path: impl AsRef<Path>) -> Result<Self, Error> {
let dir = Box::new(crate::dir::Dir::from_cap_std(dir));
Ok(WasiCtxBuilder(self.0.preopened_dir(dir, guest_path)?))
self.0.push_preopened_dir(dir, guest_path)?;
Ok(self)
}
pub fn build(self) -> Result<WasiCtx, Error> {
self.0.build()
pub fn build(self) -> WasiCtx {
self.0
}
}

Expand Down
70 changes: 24 additions & 46 deletions crates/wasi-common/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@ pub struct WasiCtx {
}

impl WasiCtx {
pub fn builder(
pub fn new(
random: RefCell<Box<dyn RngCore>>,
clocks: WasiClocks,
sched: Box<dyn WasiSched>,
table: Rc<RefCell<Table>>,
) -> WasiCtxBuilder {
WasiCtxBuilder(WasiCtx {
) -> Self {
let mut s = WasiCtx {
args: StringArray::new(),
env: StringArray::new(),
random,
clocks,
sched,
table,
})
};
s.set_stdin(Box::new(crate::pipe::ReadPipe::new(std::io::empty())));
s.set_stdout(Box::new(crate::pipe::WritePipe::new(std::io::sink())));
s.set_stderr(Box::new(crate::pipe::WritePipe::new(std::io::sink())));
s
}

pub fn insert_file(&self, fd: u32, file: Box<dyn WasiFile>, caps: FileCaps) {
Expand All @@ -58,67 +62,41 @@ impl WasiCtx {
pub fn table(&self) -> RefMut<Table> {
self.table.borrow_mut()
}
}

pub struct WasiCtxBuilder(WasiCtx);

impl WasiCtxBuilder {
pub fn build(self) -> Result<WasiCtx, Error> {
use crate::file::TableFileExt;
// Default to an empty readpipe for stdin:
if self.0.table().get_file(0).is_err() {
let stdin = crate::pipe::ReadPipe::new(std::io::empty());
self.0.insert_file(0, Box::new(stdin), FileCaps::all());
}
// Default to a sink writepipe for stdout, stderr:
for stdio_write in &[1, 2] {
if self.0.table().get_file(*stdio_write).is_err() {
let output_file = crate::pipe::WritePipe::new(std::io::sink());
self.0
.insert_file(*stdio_write, Box::new(output_file), FileCaps::all());
}
}
Ok(self.0)
}

pub fn arg(mut self, arg: &str) -> Result<Self, StringArrayError> {
self.0.args.push(arg.to_owned())?;
Ok(self)
pub fn push_arg(&mut self, arg: &str) -> Result<(), StringArrayError> {
self.args.push(arg.to_owned())
}

pub fn env(mut self, var: &str, value: &str) -> Result<Self, StringArrayError> {
self.0.env.push(format!("{}={}", var, value))?;
Ok(self)
pub fn push_env(&mut self, var: &str, value: &str) -> Result<(), StringArrayError> {
self.env.push(format!("{}={}", var, value))?;
Ok(())
}

pub fn stdin(self, f: Box<dyn WasiFile>) -> Self {
self.0.insert_file(0, f, FileCaps::all());
self
pub fn set_stdin(&mut self, f: Box<dyn WasiFile>) {
self.insert_file(0, f, FileCaps::all());
}

pub fn stdout(self, f: Box<dyn WasiFile>) -> Self {
self.0.insert_file(1, f, FileCaps::all());
self
pub fn set_stdout(&mut self, f: Box<dyn WasiFile>) {
self.insert_file(1, f, FileCaps::all());
}

pub fn stderr(self, f: Box<dyn WasiFile>) -> Self {
self.0.insert_file(2, f, FileCaps::all());
self
pub fn set_stderr(&mut self, f: Box<dyn WasiFile>) {
self.insert_file(2, f, FileCaps::all());
}

pub fn preopened_dir(
self,
pub fn push_preopened_dir(
&mut self,
dir: Box<dyn WasiDir>,
path: impl AsRef<Path>,
) -> Result<Self, Error> {
) -> Result<(), Error> {
let caps = DirCaps::all();
let file_caps = FileCaps::all();
self.0.table().push(Box::new(DirEntry::new(
self.table().push(Box::new(DirEntry::new(
caps,
file_caps,
Some(path.as_ref().to_owned()),
dir,
)))?;
Ok(self)
Ok(())
}
}
2 changes: 1 addition & 1 deletion crates/wasi-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub mod table;

pub use cap_rand::RngCore;
pub use clocks::{SystemTimeSpec, WasiClocks, WasiMonotonicClock, WasiSystemClock};
pub use ctx::{WasiCtx, WasiCtxBuilder};
pub use ctx::WasiCtx;
pub use dir::WasiDir;
pub use error::{Context, Error, ErrorExt, ErrorKind};
pub use file::WasiFile;
Expand Down
10 changes: 4 additions & 6 deletions crates/wasi-common/src/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ use std::sync::{Arc, RwLock};
/// let clocks = todo!();
/// let sched = todo!();
/// let table = Rc::new(RefCell::new(Table::new()));
/// let ctx = WasiCtx::builder(random, clocks, sched, table)
/// .stdin(Box::new(stdin.clone()))
/// .build();
/// let mut ctx = WasiCtx::new(random, clocks, sched, table);
/// ctx.set_stdin(Box::new(stdin.clone()));
/// ```
#[derive(Debug)]
pub struct ReadPipe<R: Read> {
Expand Down Expand Up @@ -203,9 +202,8 @@ impl<R: Read + Any + Send + Sync> WasiFile for ReadPipe<R> {
/// let clocks = todo!();
/// let sched = todo!();
/// let table = Rc::new(RefCell::new(Table::new()));
/// let ctx = WasiCtx::builder(random, clocks, sched, table)
/// .stdout(Box::new(stdout.clone()))
/// .build();
/// let mut ctx = WasiCtx::new(random, clocks, sched, table);
/// ctx.set_stdout(Box::new(stdout.clone()));
/// // use ctx in an instance, then make sure it is dropped:
/// drop(ctx);
/// let contents: Vec<u8> = stdout.try_into_inner().expect("sole remaining reference to WritePipe").into_inner();
Expand Down
Loading

0 comments on commit 0f5bdc6

Please sign in to comment.