Skip to content

Commit

Permalink
Merge #850
Browse files Browse the repository at this point in the history
850: Add builder API for WasiState r=MarkMcCaskey a=MarkMcCaskey

Nicer to use and it checks for errors!

# Review

- [x] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Mark McCaskey <[email protected]>
Co-authored-by: Mark McCaskey <[email protected]>
  • Loading branch information
3 people authored Oct 2, 2019
2 parents fed80fc + cbdfd87 commit a0643d7
Show file tree
Hide file tree
Showing 29 changed files with 334 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Blocks of changes will separated by version increments.

## **[Unreleased]**

- [#850](https://github.com/wasmerio/wasmer/pull/850) New `WasiStateBuilder` API. small, add misc. breaking changes to existing API (for example, changing the preopen dirs arg on `wasi::generate_import_object` from `Vec<String>` to `Vec<Pathbuf>`)
- [#852](https://github.com/wasmerio/wasmer/pull/852) Make minor grammar/capitalization fixes to README.md
- [#841](https://github.com/wasmerio/wasmer/pull/841) Slightly improve rustdoc documentation and small updates to outdated info in readme files
- [#835](https://github.com/wasmerio/wasmer/pull/836) Update Cranelift fork version to `0.44.0`
Expand Down
2 changes: 1 addition & 1 deletion lib/wasi-tests/build/wasitests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub fn compile(file: &str, ignores: &HashSet<String>) -> Option<String> {
out_str.push_str("vec![");

for entry in args.po_dirs {
out_str.push_str(&format!("\"{}\".to_string(),", entry));
out_str.push_str(&format!("std::path::PathBuf::from(\"{}\"),", entry));
}

out_str.push_str("]");
Expand Down
2 changes: 1 addition & 1 deletion lib/wasi-tests/tests/wasitests/create_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn test_create_dir() {
assert_wasi_output!(
"../../wasitests/create_dir.wasm",
"create_dir",
vec![".".to_string(),],
vec![std::path::PathBuf::from("."),],
vec![],
vec![],
"../../wasitests/create_dir.out"
Expand Down
2 changes: 1 addition & 1 deletion lib/wasi-tests/tests/wasitests/file_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn test_file_metadata() {
assert_wasi_output!(
"../../wasitests/file_metadata.wasm",
"file_metadata",
vec![".".to_string(),],
vec![std::path::PathBuf::from("."),],
vec![],
vec![],
"../../wasitests/file_metadata.out"
Expand Down
2 changes: 1 addition & 1 deletion lib/wasi-tests/tests/wasitests/quine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn test_quine() {
assert_wasi_output!(
"../../wasitests/quine.wasm",
"quine",
vec![".".to_string(),],
vec![std::path::PathBuf::from("."),],
vec![],
vec![],
"../../wasitests/quine.out"
Expand Down
Binary file modified lib/wasi-tests/wasitests/close_preopen_fd.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/create_dir.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/envvar.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fd_allocate.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fd_pread.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fd_read.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fd_sync.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/file_metadata.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fs_sandbox_test.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fseek.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/hello.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/mapdir.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/path_link.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/path_rename.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/path_symlink.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/poll_oneoff.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/quine.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/readlink.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/wasi_sees_virtual_root.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/writing.wasm
Binary file not shown.
5 changes: 3 additions & 2 deletions lib/wasi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ pub struct ExitCode {
pub code: syscalls::types::__wasi_exitcode_t,
}

/// Creates a WasiImport object with `WasiState`.
/// Creates a Wasi [`ImportObject`] with [`WasiState`].
pub fn generate_import_object(
args: Vec<Vec<u8>>,
envs: Vec<Vec<u8>>,
preopened_files: Vec<String>,
preopened_files: Vec<PathBuf>,
mapped_dirs: Vec<(String, PathBuf)>,
) -> ImportObject {
let state_gen = move || {
Expand All @@ -63,6 +63,7 @@ pub fn generate_import_object(
}
let preopened_files = preopened_files.clone();
let mapped_dirs = mapped_dirs.clone();
//let wasi_builder = create_wasi_instance();

let state = Box::new(WasiState {
fs: WasiFs::new(&preopened_files, &mapped_dirs).expect("Could not create WASI FS"),
Expand Down
289 changes: 289 additions & 0 deletions lib/wasi/src/state/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
//! Builder code for [`WasiState`]
use crate::state::{WasiFs, WasiState};
use std::path::{Path, PathBuf};

/// Creates an empty [`WasiStateBuilder`].
pub(crate) fn create_wasi_state(program_name: &str) -> WasiStateBuilder {
WasiStateBuilder {
args: vec![program_name.bytes().collect()],
..WasiStateBuilder::default()
}
}

/// Type for building an instance of [`WasiState`]
#[derive(Debug, Default, Clone, PartialEq)]
pub struct WasiStateBuilder {
args: Vec<Vec<u8>>,
envs: Vec<Vec<u8>>,
preopened_files: Vec<PathBuf>,
mapped_dirs: Vec<(String, PathBuf)>,
}

/// Error type returned when bad data is given to [`WasiStateBuilder`].
#[derive(Debug, PartialEq, Eq)]
pub enum WasiStateCreationError {
EnvironmentVariableFormatError(String),
ArgumentContainsNulByte(String),
PreopenedDirectoryNotFound(PathBuf),
MappedDirAliasFormattingError(String),
WasiFsCreationError(String),
}

fn validate_mapped_dir_alias(alias: &str) -> Result<(), WasiStateCreationError> {
for byte in alias.bytes() {
match byte {
b'/' => {
return Err(WasiStateCreationError::MappedDirAliasFormattingError(
format!("Alias \"{}\" contains the character '/'", alias),
));
}
b'\0' => {
return Err(WasiStateCreationError::MappedDirAliasFormattingError(
format!("Alias \"{}\" contains a nul byte", alias),
));
}
_ => (),
}
}

Ok(())
}

// TODO add other WasiFS APIs here like swapping out stdout, for example (though we need to
// return stdout somehow, it's unclear what that API should look like)
impl WasiStateBuilder {
/// Add an environment variable pair.
/// Environment variable keys and values must not contain the byte `=` (0x3d)
/// or nul (0x0).
pub fn env<Key, Value>(&mut self, key: Key, value: Value) -> &mut Self
where
Key: AsRef<[u8]>,
Value: AsRef<[u8]>,
{
let key_b = key.as_ref();
let val_b = value.as_ref();

let length = key_b.len() + val_b.len() + 1;
let mut byte_vec = Vec::with_capacity(length);

byte_vec.extend_from_slice(&key_b);
byte_vec.push(b'=');
byte_vec.extend_from_slice(&val_b);

self.envs.push(byte_vec);

self
}

/// Add an argument.
/// Arguments must not contain the nul (0x0) byte
pub fn arg<Arg>(&mut self, arg: Arg) -> &mut Self
where
Arg: AsRef<[u8]>,
{
let arg_b = arg.as_ref();
let mut byte_vec = Vec::with_capacity(arg_b.len());
byte_vec.extend_from_slice(&arg_b);
self.args.push(byte_vec);

self
}

/// Add multiple environment variable pairs.
/// Keys and values must not contain the `=` (0x3d) or nul (0x0) byte.
pub fn envs<I, Key, Value>(&mut self, env_pairs: I) -> &mut Self
where
I: IntoIterator<Item = (Key, Value)>,
Key: AsRef<[u8]>,
Value: AsRef<[u8]>,
{
for (key, value) in env_pairs {
let key_b = key.as_ref();
let val_b = value.as_ref();

let length = key_b.len() + val_b.len() + 1;
let mut byte_vec = Vec::with_capacity(length);

byte_vec.extend_from_slice(&key_b);
byte_vec.push(b'=');
byte_vec.extend_from_slice(&val_b);

self.envs.push(byte_vec);
}

self
}

/// Add multiple arguments.
/// Arguments must not contain the nul (0x0) byte
pub fn args<I, Arg>(&mut self, args: I) -> &mut Self
where
I: IntoIterator<Item = Arg>,
Arg: AsRef<[u8]>,
{
for arg in args {
let arg_b = arg.as_ref();
let mut byte_vec = Vec::with_capacity(arg_b.len());
byte_vec.extend_from_slice(&arg_b);
self.args.push(byte_vec);
}

self
}

/// Preopen a directory
/// This opens the given directory at the virtual root, `/`, and allows
/// the WASI module to read and write to the given directory.
// TODO: design a simple API for passing in permissions here (i.e. read-only)
pub fn preopen_dir<FilePath>(&mut self, po_dir: FilePath) -> &mut Self
where
FilePath: AsRef<Path>,
{
let path = po_dir.as_ref();
self.preopened_files.push(path.to_path_buf());

self
}

/// Preopen a directory
/// This opens the given directory at the virtual root, `/`, and allows
/// the WASI module to read and write to the given directory.
pub fn preopen_dirs<I, FilePath>(&mut self, po_dirs: I) -> &mut Self
where
I: IntoIterator<Item = FilePath>,
FilePath: AsRef<Path>,
{
for po_dir in po_dirs {
let path = po_dir.as_ref();
self.preopened_files.push(path.to_path_buf());
}

self
}

/// Preopen a directory with a different name exposed to the WASI.
pub fn map_dir<FilePath>(&mut self, alias: &str, po_dir: FilePath) -> &mut Self
where
FilePath: AsRef<Path>,
{
let path = po_dir.as_ref();
self.mapped_dirs
.push((alias.to_string(), path.to_path_buf()));

self
}

/// Consumes the [`WasiStateBuilder`] and produces a [`WasiState`]
///
/// Returns the error from `WasiFs::new` if there's an error
pub fn build(&mut self) -> Result<WasiState, WasiStateCreationError> {
for (i, arg) in self.args.iter().enumerate() {
for b in arg.iter() {
if *b == 0 {
return Err(WasiStateCreationError::ArgumentContainsNulByte(
std::str::from_utf8(arg)
.unwrap_or(if i == 0 {
"Inner error: program name is invalid utf8!"
} else {
"Inner error: arg is invalid utf8!"
})
.to_string(),
));
}
}
}
for env in self.envs.iter() {
let mut eq_seen = false;
for b in env.iter() {
match *b {
b'=' => {
if eq_seen {
return Err(WasiStateCreationError::EnvironmentVariableFormatError(
format!(
"found '=' in env var string \"{}\" (key=value)",
std::str::from_utf8(env)
.unwrap_or("Inner error: env var is invalid_utf8!")
),
));
}
eq_seen = true;
}
0 => {
return Err(WasiStateCreationError::EnvironmentVariableFormatError(
format!(
"found nul byte in env var string \"{}\" (key=value)",
std::str::from_utf8(env)
.unwrap_or("Inner error: env var is invalid_utf8!")
),
));
}
_ => (),
}
}
}

for po_f in self.preopened_files.iter() {
if !po_f.exists() {
return Err(WasiStateCreationError::PreopenedDirectoryNotFound(
po_f.clone(),
));
}
}

for (alias, po_f) in self.mapped_dirs.iter() {
if !po_f.exists() {
return Err(WasiStateCreationError::PreopenedDirectoryNotFound(
po_f.clone(),
));
}
validate_mapped_dir_alias(&alias)?;
}
Ok(WasiState {
fs: WasiFs::new(&self.preopened_files, &self.mapped_dirs)
.map_err(WasiStateCreationError::WasiFsCreationError)?,
args: self.args.clone(),
envs: self.envs.clone(),
})
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn env_var_errors() {
let output = create_wasi_state("test_prog")
.env("HOM=E", "/home/home")
.build();
match output {
Err(WasiStateCreationError::EnvironmentVariableFormatError(_)) => assert!(true),
_ => assert!(false),
}

let output = create_wasi_state("test_prog")
.env("HOME\0", "/home/home")
.build();
match output {
Err(WasiStateCreationError::EnvironmentVariableFormatError(_)) => assert!(true),
_ => assert!(false),
}
}

#[test]
fn nul_character_in_args() {
let output = create_wasi_state("test_prog").arg("--h\0elp").build();
match output {
Err(WasiStateCreationError::ArgumentContainsNulByte(_)) => assert!(true),
_ => assert!(false),
}
let output = create_wasi_state("test_prog")
.args(&["--help", "--wat\0"])
.build();
match output {
Err(WasiStateCreationError::ArgumentContainsNulByte(_)) => assert!(true),
_ => assert!(false),
}
}
}
Loading

0 comments on commit a0643d7

Please sign in to comment.