Skip to content

Commit

Permalink
Merge pull request #2487 from bytecodealliance/pch/wasi_common_cap_std
Browse files Browse the repository at this point in the history
rewrite wasi-common in terms of cap-std
  • Loading branch information
Pat Hickey authored Feb 5, 2021
2 parents 40c4c6a + 6fb2c29 commit e4827ad
Show file tree
Hide file tree
Showing 190 changed files with 6,933 additions and 13,405 deletions.
304 changes: 236 additions & 68 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ wasmtime-wasi = { path = "crates/wasi", version = "0.22.0" }
wasmtime-wasi-crypto = { path = "crates/wasi-crypto", version = "0.22.0", optional = true }
wasmtime-wasi-nn = { path = "crates/wasi-nn", version = "0.22.0", optional = true }
wasi-common = { path = "crates/wasi-common", version = "0.22.0" }
wasi-cap-std-sync = { path = "crates/wasi-common/cap-std-sync", version = "0.22.0" }
structopt = { version = "0.3.5", features = ["color", "suggestions"] }
object = { version = "0.23.0", default-features = false, features = ["write"] }
anyhow = "1.0.19"
Expand All @@ -45,6 +46,7 @@ log = "0.4.8"
rayon = "1.2.1"
humantime = "2.0.0"
wasmparser = "0.73.0"
cap-std = "0.13"

[dev-dependencies]
env_logger = "0.8.1"
Expand Down Expand Up @@ -73,9 +75,10 @@ members = [
"crates/misc/rust",
"crates/wiggle",
"crates/wiggle/wasmtime",
"crates/wasi-common",
"crates/wasi-common/cap-std-sync",
"examples/fib-debug/wasm",
"examples/wasi/wasm",
"examples/wasi-fs/wasm",
"fuzz",
]

Expand Down
3 changes: 2 additions & 1 deletion crates/bench-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ anyhow = "1.0"
shuffling-allocator = { version = "1.1.1", optional = true }
wasmtime = { path = "../wasmtime", default-features = false }
wasmtime-wasi = { path = "../wasi" }
wasi-common = { path = "../wasi-common" }
wasi-cap-std-sync = { path = "../wasi-common/cap-std-sync" }
cap-std = "0.13"

[dev-dependencies]
wat = "1.0"
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 @@ -83,7 +83,7 @@ use std::env;
use std::os::raw::{c_int, c_void};
use std::path::Path;
use std::slice;
use wasi_common::WasiCtxBuilder;
use wasi_cap_std_sync::WasiCtxBuilder;
use wasmtime::{Config, Engine, Instance, Linker, Module, Store};
use wasmtime_wasi::Wasi;

Expand Down Expand Up @@ -211,16 +211,16 @@ impl BenchState {
// Create a WASI environment.

let mut cx = WasiCtxBuilder::new();
cx.inherit_stdio();
cx = cx.inherit_stdio();
// Allow access to the working directory so that the benchmark can read
// its input workload(s).
let working_dir = wasi_common::preopen_dir(working_dir)
let working_dir = unsafe { cap_std::fs::Dir::open_ambient_dir(working_dir) }
.context("failed to preopen the working directory")?;
cx.preopened_dir(working_dir, ".");
cx = cx.preopened_dir(working_dir, ".")?;
// Pass this env var along so that the benchmark program can use smaller
// input workload(s) if it has them and that has been requested.
if let Ok(val) = env::var("WASM_BENCH_USE_SMALL_WORKLOAD") {
cx.env("WASM_BENCH_USE_SMALL_WORKLOAD", &val);
cx = cx.env("WASM_BENCH_USE_SMALL_WORKLOAD", &val)?;
}

let cx = cx.build()?;
Expand Down
4 changes: 3 additions & 1 deletion crates/c-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ wat = { version = "1.0.23", optional = true }

# Optional dependencies for the `wasi` feature
wasi-common = { path = "../wasi-common", optional = true }
wasi-cap-std-sync = { path = "../wasi-common/cap-std-sync", optional = true }
wasmtime-wasi = { path = "../wasi", optional = true }
cap-std = { version = "0.13", optional = true }

[features]
default = ['jitdump', 'wat', 'wasi', 'cache']
lightbeam = ["wasmtime/lightbeam"]
jitdump = ["wasmtime/jitdump"]
cache = ["wasmtime/cache"]
wasi = ['wasi-common', 'wasmtime-wasi']
wasi = ['wasi-common', 'wasi-cap-std-sync', 'wasmtime-wasi', 'cap-std']
64 changes: 44 additions & 20 deletions crates/c-api/src/wasi.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
//! 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;
use std::fs::File;
use std::os::raw::{c_char, c_int};
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::slice;
use std::str;
use wasi_common::{preopen_dir, WasiCtx, WasiCtxBuilder};
use wasi_cap_std_sync::WasiCtxBuilder;
use wasi_common::WasiCtx;
use wasmtime::{Extern, Linker, Trap};
use wasmtime_wasi::{old::snapshot_0::Wasi as WasiSnapshot0, Wasi as WasiPreview1};
use wasmtime_wasi::{
snapshots::preview_0::Wasi as WasiSnapshot0, snapshots::preview_1::Wasi as WasiPreview1,
};

unsafe fn cstr_to_path<'a>(path: *const c_char) -> Option<&'a Path> {
CStr::from_ptr(path).to_str().map(Path::new).ok()
Expand Down Expand Up @@ -39,7 +45,7 @@ pub struct wasi_config_t {
stdin: Option<File>,
stdout: Option<File>,
stderr: Option<File>,
preopens: Vec<(File, PathBuf)>,
preopens: Vec<(Dir, PathBuf)>,
inherit_args: bool,
inherit_env: bool,
inherit_stdin: bool,
Expand Down Expand Up @@ -180,7 +186,7 @@ pub unsafe extern "C" fn wasi_config_preopen_dir(
};

let dir = match cstr_to_path(path) {
Some(p) => match preopen_dir(p) {
Some(p) => match cap_std::fs::Dir::open_ambient_dir(p) {
Ok(d) => d,
Err(_) => return false,
},
Expand All @@ -197,39 +203,57 @@ enum WasiInstance {
Snapshot0(WasiSnapshot0),
}

fn create_wasi_ctx(config: wasi_config_t) -> Result<WasiCtx> {
use std::convert::TryFrom;
use wasi_common::OsFile;
fn create_wasi_ctx(config: wasi_config_t) -> Result<Rc<RefCell<WasiCtx>>> {
let mut builder = WasiCtxBuilder::new();
if config.inherit_args {
builder.inherit_args();
builder = builder.inherit_args()?;
} else if !config.args.is_empty() {
builder.args(config.args);
let args = config
.args
.into_iter()
.map(|bytes| Ok(String::from_utf8(bytes)?))
.collect::<Result<Vec<String>>>()?;
builder = builder.args(&args)?;
}
if config.inherit_env {
builder.inherit_env();
builder = builder.inherit_env()?;
} else if !config.env.is_empty() {
builder.envs(config.env);
let env = config
.env
.into_iter()
.map(|(kbytes, vbytes)| {
let k = String::from_utf8(kbytes)?;
let v = String::from_utf8(vbytes)?;
Ok((k, v))
})
.collect::<Result<Vec<(String, String)>>>()?;
builder = builder.envs(&env)?;
}
if config.inherit_stdin {
builder.inherit_stdin();
builder = builder.inherit_stdin();
} else if let Some(file) = config.stdin {
builder.stdin(OsFile::try_from(file)?);
let file = unsafe { cap_std::fs::File::from_std(file) };
let file = wasi_cap_std_sync::file::File::from_cap_std(file);
builder = builder.stdin(Box::new(file));
}
if config.inherit_stdout {
builder.inherit_stdout();
builder = builder.inherit_stdout();
} else if let Some(file) = config.stdout {
builder.stdout(OsFile::try_from(file)?);
let file = unsafe { cap_std::fs::File::from_std(file) };
let file = wasi_cap_std_sync::file::File::from_cap_std(file);
builder = builder.stdout(Box::new(file));
}
if config.inherit_stderr {
builder.inherit_stderr();
builder = builder.inherit_stderr();
} else if let Some(file) = config.stderr {
builder.stderr(OsFile::try_from(file)?);
let file = unsafe { cap_std::fs::File::from_std(file) };
let file = wasi_cap_std_sync::file::File::from_cap_std(file);
builder = builder.stderr(Box::new(file));
}
for preopen in config.preopens {
builder.preopened_dir(preopen.0, preopen.1);
for (dir, path) in config.preopens {
builder = builder.preopened_dir(dir, path)?;
}
Ok(builder.build()?)
Ok(Rc::new(RefCell::new(builder.build()?)))
}

#[repr(C)]
Expand Down
4 changes: 3 additions & 1 deletion crates/test-programs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ cfg-if = "1.0"

[dev-dependencies]
wasi-common = { path = "../wasi-common", version = "0.22.0" }
wasmtime-wasi = { path = "../wasi", version = "0.22.0" }
wasi-cap-std-sync = { path = "../wasi-common/cap-std-sync", version = "0.22.0" }
wasmtime = { path = "../wasmtime", version = "0.22.0" }
wasmtime-wasi = { path = "../wasi", version = "0.22.0" }
target-lexicon = "0.11.0"
pretty_env_logger = "0.4.0"
tempfile = "3.1.0"
os_pipe = "0.9"
anyhow = "1.0.19"
wat = "1.0.23"
cap-std = "0.13"

[features]
test_programs = []
Loading

0 comments on commit e4827ad

Please sign in to comment.