Skip to content

Commit

Permalink
Start to update the wasi crate in wasi tests (#675)
Browse files Browse the repository at this point in the history
* Move `wasi` to `wasi_old` in wasi-tests

Leave space for the new `wasi` crate but allow us to incrementally
update tests.

* Update the big_random_buf test

* Update clock_time_get test

* Update close_preopen test

* Review comments

* Update to latest Wasmtime API
  • Loading branch information
alexcrichton authored and Jakub Konka committed Dec 9, 2019
1 parent 51f880f commit e13fabb
Show file tree
Hide file tree
Showing 40 changed files with 137 additions and 120 deletions.
2 changes: 2 additions & 0 deletions crates/test-programs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ mod wasi_tests {
println!("cargo:rerun-if-changed={}", test_file_path);
}
}
println!("cargo:rerun-if-changed=wasi-tests/Cargo.toml");
println!("cargo:rerun-if-changed=wasi-tests/src/lib.rs");
// Build tests to OUT_DIR (target/*/build/wasi-common-*/out/wasm32-wasi/release/*.wasm)
let out_dir = PathBuf::from(
env::var("OUT_DIR").expect("The OUT_DIR environment variable must be set"),
Expand Down
71 changes: 43 additions & 28 deletions crates/test-programs/tests/wasm_tests/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{bail, Context};
use std::fs::File;
use std::{collections::HashMap, path::Path};
use std::path::Path;
use wasmtime::{Config, Engine, HostRef, Instance, Module, Store};
use wasmtime_environ::settings::{self, Configurable};

Expand All @@ -18,7 +18,6 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any
let engine = HostRef::new(Engine::new(&config));
let store = HostRef::new(Store::new(&engine));

let mut module_registry = HashMap::new();
let global_exports = store.borrow().global_exports().clone();
let get_preopens = |workspace: Option<&Path>| -> anyhow::Result<Vec<_>> {
if let Some(workspace) = workspace {
Expand All @@ -33,7 +32,7 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any

// Create our wasi context with pretty standard arguments/inheritance/etc.
// Additionally register andy preopened directories if we have them.
let mut builder = wasi_common::old::snapshot_0::WasiCtxBuilder::new()
let mut builder = wasi_common::WasiCtxBuilder::new()
.arg(bin_name)
.arg(".")
.inherit_stdio();
Expand All @@ -47,19 +46,33 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any
// stdin is closed which causes tests to fail.
let (reader, _writer) = os_pipe::pipe()?;
builder = builder.stdin(reader_to_file(reader));
let snapshot1 = Instance::from_handle(
&store,
wasmtime_wasi::instantiate_wasi_with_context(
global_exports.clone(),
builder.build().context("failed to build wasi context")?,
)
.context("failed to instantiate wasi")?,
);

// The current stable Rust toolchain uses the old `wasi_unstable` ABI,
// aka `snapshot_0`.
module_registry.insert(
"wasi_unstable".to_owned(),
Instance::from_handle(
&store,
wasmtime_wasi::old::snapshot_0::instantiate_wasi_with_context(
global_exports.clone(),
builder.build().context("failed to build wasi context")?,
)
.context("failed to instantiate wasi")?,
),
// ... and then do the same as above but for the old snapshot of wasi, since
// a few tests still test that
let mut builder = wasi_common::old::snapshot_0::WasiCtxBuilder::new()
.arg(bin_name)
.arg(".")
.inherit_stdio();
for (dir, file) in get_preopens(workspace)? {
builder = builder.preopened_dir(file, dir);
}
let (reader, _writer) = os_pipe::pipe()?;
builder = builder.stdin(reader_to_file(reader));
let snapshot0 = Instance::from_handle(
&store,
wasmtime_wasi::old::snapshot_0::instantiate_wasi_with_context(
global_exports.clone(),
builder.build().context("failed to build wasi context")?,
)
.context("failed to instantiate wasi")?,
);

let module = HostRef::new(Module::new(&store, &data).context("failed to create wasm module")?);
Expand All @@ -68,20 +81,22 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any
.imports()
.iter()
.map(|i| {
let module_name = i.module();
if let Some(instance) = module_registry.get(module_name) {
let field_name = i.name();
if let Some(export) = instance.find_export_by_name(field_name) {
Ok(export.clone())
} else {
bail!(
"import {} was not found in module {}",
field_name,
module_name
)
}
let instance = if i.module() == "wasi_unstable" {
&snapshot0
} else if i.module() == "wasi_snapshot_preview1" {
&snapshot1
} else {
bail!("import module {} was not found", i.module())
};
let field_name = i.name();
if let Some(export) = instance.find_export_by_name(field_name) {
Ok(export.clone())
} else {
bail!("import module {} was not found", module_name)
bail!(
"import {} was not found in module {}",
field_name,
i.module(),
)
}
})
.collect::<Result<Vec<_>, _>>()?;
Expand Down
3 changes: 2 additions & 1 deletion crates/test-programs/wasi-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ publish = false

[dependencies]
libc = "0.2.65"
wasi = "0.7.0"
wasi = "0.9.0"
wasi-old = { version = "0.7.0", package = "wasi" }
more-asserts = "0.2.1"

# This crate is built with the wasm32-wasi target, so it's separate
Expand Down
9 changes: 3 additions & 6 deletions crates/test-programs/wasi-tests/src/bin/big_random_buf.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use wasi::wasi_unstable;

fn test_big_random_buf() {
let mut buf = Vec::new();
buf.resize(1024, 0);
assert!(
wasi_unstable::random_get(&mut buf).is_ok(),
"calling get_random on a large buffer"
);
unsafe {
wasi::random_get(buf.as_mut_ptr(), 1024).expect("failed to call random_get");
}
// Chances are pretty good that at least *one* byte will be non-zero in
// any meaningful random function producing 1024 u8 values.
assert!(buf.iter().any(|x| *x != 0), "random_get returned all zeros");
Expand Down
26 changes: 4 additions & 22 deletions crates/test-programs/wasi-tests/src/bin/clock_time_get.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,15 @@
use more_asserts::assert_le;
use wasi::wasi_unstable;
use wasi_tests::wasi_wrappers::wasi_clock_time_get;

unsafe fn test_clock_time_get() {
// Test that clock_time_get succeeds. Even in environments where it's not
// desirable to expose high-precision timers, it should still succeed.
// clock_res_get is where information about precision can be provided.
let mut time: wasi_unstable::Timestamp = 0;
let status = wasi_clock_time_get(wasi_unstable::CLOCK_MONOTONIC, 1, &mut time);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"clock_time_get with a precision of 1"
);
wasi::clock_time_get(wasi::CLOCKID_MONOTONIC, 1).expect("precision 1 should work");

let status = wasi_clock_time_get(wasi_unstable::CLOCK_MONOTONIC, 0, &mut time);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"clock_time_get with a precision of 0"
);
let first_time = time;
let first_time =
wasi::clock_time_get(wasi::CLOCKID_MONOTONIC, 0).expect("precision 0 should work");

let status = wasi_clock_time_get(wasi_unstable::CLOCK_MONOTONIC, 0, &mut time);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"clock_time_get with a precision of 0"
);
let time = wasi::clock_time_get(wasi::CLOCKID_MONOTONIC, 0).expect("re-fetch time should work");
assert_le!(first_time, time, "CLOCK_MONOTONIC should be monotonic");
}

Expand Down
44 changes: 15 additions & 29 deletions crates/test-programs/wasi-tests/src/bin/close_preopen.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,46 @@
use libc;
use more_asserts::assert_gt;
use std::{env, mem, process};
use wasi::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::wasi_wrappers::wasi_fd_fdstat_get;
use std::{env, process};
use wasi_tests::open_scratch_directory_new;

unsafe fn test_close_preopen(dir_fd: wasi_unstable::Fd) {
let pre_fd: wasi_unstable::Fd = (libc::STDERR_FILENO + 1) as wasi_unstable::Fd;
unsafe fn test_close_preopen(dir_fd: wasi::Fd) {
let pre_fd: wasi::Fd = (libc::STDERR_FILENO + 1) as wasi::Fd;

assert_gt!(dir_fd, pre_fd, "dir_fd number");

// Try to close a preopened directory handle.
assert_eq!(
wasi_unstable::fd_close(pre_fd),
Err(wasi_unstable::ENOTSUP),
wasi::fd_close(pre_fd).unwrap_err().raw_error(),
wasi::ERRNO_NOTSUP,
"closing a preopened file descriptor",
);

// Try to renumber over a preopened directory handle.
assert_eq!(
wasi_unstable::fd_renumber(dir_fd, pre_fd),
Err(wasi_unstable::ENOTSUP),
wasi::fd_renumber(dir_fd, pre_fd).unwrap_err().raw_error(),
wasi::ERRNO_NOTSUP,
"renumbering over a preopened file descriptor",
);

// Ensure that dir_fd is still open.
let mut dir_fdstat: wasi_unstable::FdStat = mem::zeroed();
let mut status = wasi_fd_fdstat_get(dir_fd, &mut dir_fdstat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"calling fd_fdstat on the scratch directory"
);
let dir_fdstat = wasi::fd_fdstat_get(dir_fd).expect("failed fd_fdstat_get");
assert_eq!(
dir_fdstat.fs_filetype,
wasi_unstable::FILETYPE_DIRECTORY,
wasi::FILETYPE_DIRECTORY,
"expected the scratch directory to be a directory",
);

// Try to renumber a preopened directory handle.
assert_eq!(
wasi_unstable::fd_renumber(pre_fd, dir_fd),
Err(wasi_unstable::ENOTSUP),
wasi::fd_renumber(pre_fd, dir_fd).unwrap_err().raw_error(),
wasi::ERRNO_NOTSUP,
"renumbering over a preopened file descriptor",
);

// Ensure that dir_fd is still open.
status = wasi_fd_fdstat_get(dir_fd, &mut dir_fdstat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"calling fd_fdstat on the scratch directory"
);
let dir_fdstat = wasi::fd_fdstat_get(dir_fd).expect("failed fd_fdstat_get");
assert_eq!(
dir_fdstat.fs_filetype,
wasi_unstable::FILETYPE_DIRECTORY,
wasi::FILETYPE_DIRECTORY,
"expected the scratch directory to be a directory",
);
}
Expand All @@ -70,7 +56,7 @@ fn main() {
};

// Open scratch directory
let dir_fd = match open_scratch_directory(&arg) {
let dir_fd = match open_scratch_directory_new(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);
Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/wasi-tests/src/bin/dangling_fd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use more_asserts::assert_gt;
use std::{env, process};
use wasi::wasi_unstable;
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_dir, cleanup_file, create_dir, create_file};
use wasi_tests::wasi_wrappers::wasi_path_open;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{env, process};
use wasi::wasi_unstable;
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::cleanup_file;
use wasi_tests::wasi_wrappers::{wasi_path_open, wasi_path_symlink};
Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/wasi-tests/src/bin/directory_seek.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use more_asserts::assert_gt;
use std::{env, mem, process};
use wasi::wasi_unstable;
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_dir, close_fd, create_dir};
use wasi_tests::wasi_wrappers::{wasi_fd_fdstat_get, wasi_fd_seek, wasi_path_open};
Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/wasi-tests/src/bin/fd_advise.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use libc;
use more_asserts::assert_gt;
use std::{env, process};
use wasi::wasi_unstable;
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd};
use wasi_tests::wasi_wrappers::{wasi_fd_advise, wasi_fd_filestat_get, wasi_path_open};
Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/wasi-tests/src/bin/fd_filestat_set.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use libc;
use more_asserts::assert_gt;
use std::{env, process};
use wasi::wasi_unstable;
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd};
use wasi_tests::wasi_wrappers::{wasi_fd_filestat_get, wasi_path_open};
Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/wasi-tests/src/bin/fd_readdir.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use libc;
use more_asserts::assert_gt;
use std::{cmp::min, env, mem, process, slice, str};
use wasi::wasi_unstable;
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::wasi_wrappers::{wasi_fd_filestat_get, wasi_fd_readdir, wasi_path_open};

Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/wasi-tests/src/bin/file_allocate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use more_asserts::assert_gt;
use std::{env, process};
use wasi::wasi_unstable;
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd};
use wasi_tests::wasi_wrappers::{wasi_fd_filestat_get, wasi_path_open};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use libc;
use more_asserts::assert_gt;
use std::{env, process};
use wasi::wasi_unstable;
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd};
use wasi_tests::wasi_wrappers::{wasi_fd_pread, wasi_fd_pwrite, wasi_path_open};
Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/wasi-tests/src/bin/file_seek_tell.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use libc;
use more_asserts::assert_gt;
use std::{env, process};
use wasi::wasi_unstable;
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd};
use wasi_tests::wasi_wrappers::{wasi_fd_seek, wasi_fd_tell, wasi_fd_write, wasi_path_open};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use more_asserts::assert_gt;
use std::{env, process};
use wasi::wasi_unstable;
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd, create_file};
use wasi_tests::wasi_wrappers::{wasi_fd_read, wasi_fd_write, wasi_path_open};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use libc;
use more_asserts::assert_gt;
use std::{env, process};
use wasi::wasi_unstable;
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{close_fd, create_dir, create_file};
use wasi_tests::wasi_wrappers::{
Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/wasi-tests/src/bin/isatty.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use more_asserts::assert_gt;
use std::{env, process};
use wasi::wasi_unstable;
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd};
use wasi_tests::wasi_wrappers::wasi_path_open;
Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/wasi-tests/src/bin/nofollow_errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use libc;
use more_asserts::assert_gt;
use std::{env, process};
use wasi::wasi_unstable;
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd, create_dir, create_file};
use wasi_tests::wasi_wrappers::{wasi_path_open, wasi_path_remove_directory, wasi_path_symlink};
Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/wasi-tests/src/bin/path_filestat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use more_asserts::assert_gt;
use std::{env, process};
use wasi::wasi_unstable;
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd};
use wasi_tests::wasi_wrappers::{
Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/wasi-tests/src/bin/path_link.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use more_asserts::assert_gt;
use std::{env, process};
use wasi::wasi_unstable;
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_dir, cleanup_file, create_dir, create_file};
use wasi_tests::wasi_wrappers::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{env, process};
use wasi::wasi_unstable;
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd};
use wasi_tests::wasi_wrappers::wasi_path_open;
Expand Down
Loading

0 comments on commit e13fabb

Please sign in to comment.