Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Update and refactor how wasi-testsuite is run (#11560)

Alex Crichton1df12556e7bb
diff --git a/Cargo.lock b/Cargo.lock
index 38079579b6..df0582b3ef 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3587,9 +3587,9 @@ dependencies = [
[[package]]
name = "tempfile"
-version = "3.19.1"
+version = "3.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7437ac7763b9b123ccf33c338a5cc1bac6f69b45a136c19bdd8a65e3916435bf"
+checksum = "15b61f8f20e3a6f7e0649d825294eaf317edce30f82cf6026e7e4cb9222a7d1e"
dependencies = [
"fastrand",
"getrandom 0.3.1",
diff --git a/Cargo.toml b/Cargo.toml
index 902dd5fbf4..f943222be7 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -380,7 +380,7 @@ glob = "0.3.0"
libfuzzer-sys = "0.4.8"
walkdir = "2.3.3"
cfg-if = "1.0"
-tempfile = "3.1.0"
+tempfile = "3.21.0"
filecheck = "0.5.0"
libc = { version = "0.2.112", default-features = true }
file-per-thread-logger = "0.2.0"
@@ -571,6 +571,10 @@ harness = false
name = "wast"
harness = false
+[[test]]
+name = "wasi"
+harness = false
+
[[example]]
name = "tokio"
required-features = ["wasi-common/tokio"]
diff --git a/crates/test-programs/artifacts/build.rs b/crates/test-programs/artifacts/build.rs
index c921fc16be..ef35e09023 100644
--- a/crates/test-programs/artifacts/build.rs
+++ b/crates/test-programs/artifacts/build.rs
@@ -37,12 +37,19 @@ struct Test {
impl Artifacts {
fn build(&mut self) {
+ let mut generated_code = String::new();
// Build adapters used below for componentization.
- let reactor_adapter = self.build_adapter("reactor", &[]);
- let command_adapter =
- self.build_adapter("command", &["--no-default-features", "--features=command"]);
- let proxy_adapter =
- self.build_adapter("proxy", &["--no-default-features", "--features=proxy"]);
+ let reactor_adapter = self.build_adapter(&mut generated_code, "reactor", &[]);
+ let command_adapter = self.build_adapter(
+ &mut generated_code,
+ "command",
+ &["--no-default-features", "--features=command"],
+ );
+ let proxy_adapter = self.build_adapter(
+ &mut generated_code,
+ "proxy",
+ &["--no-default-features", "--features=proxy"],
+ );
// Build all test programs both in Rust and C/C++.
let mut tests = Vec::new();
@@ -53,7 +60,6 @@ impl Artifacts {
// test along with constants pointing to various paths. Note that
// components are created here as well from core modules.
let mut kinds = BTreeMap::new();
- let mut generated_code = String::new();
let missing_sdk_path =
PathBuf::from("Asset not compiled, WASI_SDK_PATH missing at compile time");
for test in tests.iter() {
@@ -175,7 +181,12 @@ impl Artifacts {
}
// Build the WASI Preview 1 adapter, and get the binary:
- fn build_adapter(&mut self, name: &str, features: &[&str]) -> Vec<u8> {
+ fn build_adapter(
+ &mut self,
+ generated_code: &mut String,
+ name: &str,
+ features: &[&str],
+ ) -> Vec<u8> {
let mut cmd = cargo();
cmd.arg("build")
.arg("--release")
@@ -202,6 +213,10 @@ impl Artifacts {
std::fs::copy(&artifact, &adapter).unwrap();
self.read_deps_of(&artifact);
println!("wasi {name} adapter: {:?}", &adapter);
+ generated_code.push_str(&format!(
+ "pub const ADAPTER_{}: &'static str = {adapter:?};\n",
+ name.to_shouty_snake_case(),
+ ));
fs::read(&adapter).unwrap()
}
diff --git a/crates/test-programs/src/bin/preview1_renumber.rs b/crates/test-programs/src/bin/preview1_renumber.rs
index 394e5776f4..6782fc709b 100644
--- a/crates/test-programs/src/bin/preview1_renumber.rs
+++ b/crates/test-programs/src/bin/preview1_renumber.rs
@@ -91,14 +91,11 @@ unsafe fn test_renumber(dir_fd: wasip1::Fd) {
"file descriptor range check",
);
- wasip1::fd_renumber(fd_file3, 127).expect("renumbering FD to 127");
- match wasip1::fd_renumber(127, u32::MAX) {
- Err(wasip1::ERRNO_NOMEM) => {
- // The preview1 adapter cannot handle more than 128 descriptors
- eprintln!("fd_renumber({fd_file3}, {}) returned NOMEM", u32::MAX)
- }
- res => res.expect("renumbering FD to `u32::MAX`"),
- }
+ assert_eq!(wasip1::fd_renumber(fd_file3, 127), Err(wasip1::ERRNO_BADF));
+ assert_eq!(
+ wasip1::fd_renumber(fd_file3, u32::MAX),
+ Err(wasip1::ERRNO_BADF)
+ );
let fd_file4 = wasip1::path_open(
dir_fd,
diff --git a/crates/test-programs/src/bin/preview1_stdio.rs b/crates/test-programs/src/bin/preview1_stdio.rs
index f1f864854b..ff5b1c0d62 100644
--- a/crates/test-programs/src/bin/preview1_stdio.rs
+++ b/crates/test-programs/src/bin/preview1_stdio.rs
@@ -5,7 +5,8 @@ use test_programs::preview1::{STDERR_FD, STDIN_FD, STDOUT_FD};
unsafe fn test_stdio() {
for fd in &[STDIN_FD, STDOUT_FD, STDERR_FD] {
wasip1::fd_fdstat_get(*fd).expect("fd_fdstat_get on stdio");
- wasip1::fd_renumber(*fd, *fd + 100).expect("renumbering stdio");
+ assert_eq!(wasip1::fd_renumber(*fd, *fd + 100), Err(wasip1::ERRNO_BADF));
+ wasip1::fd_renumber(*fd, *fd).unwrap();
}
}
diff --git a/crates/wasi-common/src/snapshots/preview_1.rs b/crates/wasi-common/src/snapshots/preview_1.rs
index e0454a1fd5..b0ed833738 100644
--- a/crates/wasi-common/src/snapshots/preview_1.rs
+++ b/crates/wasi-common/src/snapshots/preview_1.rs
@@ -580,6 +580,9 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
if !table.contains_key(from) {
return Err(Error::badf());
}
+ if !table.contains_key(to) {
+ return Err(Error::badf());
+ }
table.renumber(from, to)
}
diff --git a/crates/wasi-preview1-component-adapter/src/descriptors.rs b/crates/wasi-preview1-component-adapter/src/descriptors.rs
index da3dc6e705..be773113db 100644
--- a/crates/wasi-preview1-component-adapter/src/descriptors.rs
+++ b/crates/wasi-preview1-component-adapter/src/descriptors.rs
@@ -355,8 +355,13 @@ impl Descriptors {
// Implementation of fd_renumber
pub fn renumber(&mut self, from_fd: Fd, to_fd: Fd) -> Result<(), Errno> {
- // First, ensure from_fd is in bounds:
- let _ = self.get(from_fd)?;
+ // First, ensure to_fd/from_fd is in bounds:
+ if let Descriptor::Closed(_) = self.get(to_fd)? {
+ return Err(wasi::ERRNO_BADF);
+ }
+ if let Descriptor::Closed(_) = self.get(from_fd)? {
+ return Err(wasi::ERRNO_BADF);
+ }
// Expand table until to_fd is in bounds as well:
while self.table_len.get() as u32 <= to_fd {
self.push_closed()?;
diff --git a/crates/wasi-preview1-component-adapter/src/lib.rs b/crates/wasi-preview1-component-adapter/src/lib.rs
index 43138b52bf..79bd515d7c 100644
--- a/crates/wasi-preview1-component-adapter/src/lib.rs
+++ b/crates/wasi-preview1-component-adapter/src/lib.rs
@@ -2415,8 +2415,13 @@ pub unsafe extern "C" fn sock_send(
/// Shut down socket send and receive channels.
/// Note: This is similar to `shutdown` in POSIX.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn sock_shutdown(_fd: Fd, _how: Sdflags) -> Errno {
- unreachable!()
+pub unsafe extern "C" fn sock_shutdown(fd: Fd, _how: Sdflags) -> Errno {
+ State::with(|state| {
+ state
+ .descriptors_mut()
+ .get_stream_with_error_mut(fd, wasi::ERRNO_BADF)?;
+ Err(wasi::ERRNO_NOTSOCK)
+ })
}
#[cfg(not(feature = "proxy"))]
diff --git a/crates/wasi/src/p1.rs b/crates/wasi/src/p1.rs
index 6b0392c279..c63331f5de 100644
--- a/crates/wasi/src/p1.rs
+++ b/crates/wasi/src/p1.rs
@@ -1468,6 +1468,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiP1Ctx {
if let types::Filetype::Directory = fs_filetype {
fs_rights_base &= !types::Rights::FD_SEEK;
fs_rights_base &= !types::Rights::FD_FILESTAT_SET_SIZE;
+ fs_rights_base &= !types::Rights::PATH_FILESTAT_SET_SIZE;
}
if !flags.contains(filesystem::DescriptorFlags::READ) {
fs_rights_base &= !types::Rights::FD_READ;
@@ -1840,10 +1841,13 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiP1Ctx {
) -> Result<(), types::Error> {
let mut st = self.transact()?;
let from = from.into();
+ let to = to.into();
+ if !st.descriptors.used.contains_key(&to) {
+ return Err(types::Errno::Badf.into());
+ }
let btree_map::Entry::Occupied(desc) = st.descriptors.used.entry(from) else {
return Err(types::Errno::Badf.into());
};
- let to = to.into();
if from != to {
let desc = desc.remove();
st.descriptors.free.insert(from);
diff --git a/supply-chain/audits.toml b/supply-chain/audits.toml
index 7d94242bfe..21dfca2e05 100644
--- a/supply-chain/audits.toml
+++ b/supply-chain/audits.toml
@@ -3635,6 +3635,12 @@ criteria = "safe-to-deploy"
delta = "3.16.0 -> 3.19.1"
notes = "Idiom and platform updates, but nothing major and nothing out of line."
+[[audits.tempfile]]
+who = "Alex Crichton <[email protected]>"
+criteria = "safe-to-deploy"
+delta = "3.20.0 -> 3.21.0"
+notes = "Only minor manifest/CI changes."
+
[[audits.terminal_size]]
who = "Alex Crichton <[email protected]>"
criteria = "safe-to-deploy"
diff --git a/supply-chain/imports.lock b/supply-chain/imports.lock
index ebe895f5b2..63c7187d11 100644
--- a/supply-chain/imports.lock
+++ b/supply-chain/imports.lock
@@ -2787,6 +2787,12 @@ delta = "3.10.1 -> 3.16.0"
notes = "Big change, but nothing unsafe and lots of it is documentation and convenience APIs"
aggregated-from = "https://hg.mozilla.org/mozilla-central/raw-file/tip/supply-chain/audits.toml"
+[[audits.mozilla.audits.tempfile]]
+who = "Nika Layzell <[email protected]>"
+criteria = "safe-to-deploy"
+delta = "3.19.1 -> 3.20.0"
+aggregated-from = "https://raw.githubusercontent.com/mozilla/cargo-vet/main/supply-chain/audits.toml"
+
[[audits.mozilla.audits.time-core]]
who = "Kershaw Chang <[email protected]>"
criteria = "safe-to-deploy"
diff --git a/tests/all/main.rs b/tests/all/main.rs
index d06392b74d..c91f7bb78c 100644
--- a/tests/all/main.rs
+++ b/tests/all/main.rs
@@ -51,7 +51,6 @@ mod threads;
mod traps;
mod types;
mod wait_notify;
-mod wasi_testsuite;
mod winch_engine_features;
/// A helper to compile a module in a new store with reference types enabled.
diff --git a/tests/all/wasi_testsuite.rs b/tests/all/wasi_testsuite.rs
deleted file mode 100644
index aa6897945d..0000000000
--- a/tests/all/wasi_testsuite.rs
+++ /dev/null
@@ -1,210 +0,0 @@
-//! Run the tests in `wasi_testsuite` using Wasmtime's CLI binary and checking
-//! the results with a [wasi-testsuite] spec.
-//!
-//! [wasi-testsuite]: https://github.com/WebAssembly/wasi-testsuite
-
-#![cfg(not(miri))]
-
-use crate::cli_tests::get_wasmtime_command;
-use anyhow::{Result, anyhow};
-use serde_derive::Deserialize;
-use std::collections::HashMap;
-use std::ffi::OsStr;
-use std::fs;
-use std::path::{Path, PathBuf};
-use std::process::{Command, Output};
-use walkdir::{DirEntry, WalkDir};
-
-#[test]
-#[cfg_attr(target_os = "windows", ignore)] // TODO: https://github.com/WebAssembly/WASI/issues/524
-fn wasi_testsuite() -> Result<()> {
- // Currently, Wasmtime's implementation in wasi-common does not line up
- // exactly with the expectations in wasi-testsuite. This could be for one of
- // various reasons:
- // - wasi-common has a bug
- // - wasi-testsuite overspecifies (or incorrectly specifies) a test
- // - this test runner incorrectly configures Wasmtime's CLI execution.
- //
- // This list is expected to shrink as the failures are resolved. The easiest
- // way to resolve one of these is to remove the file from the list and run
- // `cargo test wasi_testsuite -- --nocapture`. The printed output will show
- // the expected result, the actual result, and a command to replicate the
- // failure from the command line.
- const WASI_COMMON_IGNORE_LIST: &[&str] = &[
- // Uses functions not supported in Wasmtime due to portability concerns
- "fd_advise.wasm",
- "file_allocate.wasm",
- // #8828
- "remove_directory_trailing_slashes.wasm",
- // Working with rights which are removed from wasmtime
- "truncation_rights.wasm",
- "fd_fdstat_set_rights.wasm",
- "path_open_preopen.wasm",
- "path_link.wasm",
- // This test wants an operation to fail that wasmtime thinks is valid
- "renumber.wasm",
- // Works with FDFLAGS_SYNC which isn't supported
- "path_filestat.wasm",
- // this test asserts semantics of pwrite which don't match Linux so
- // ignore the test for now.
- "pwrite-with-append.wasm",
- ];
- run_all(
- "tests/wasi_testsuite/wasi-common",
- &[],
- WASI_COMMON_IGNORE_LIST,
- )?;
-
- // Only run threaded tests on platforms that support threads. Also skip
- // these tests with ASAN as it, rightfully, complains about a memory leak.
- // The memory leak at this time is that child threads aren't joined with the
- // main thread, meaning that allocations done on child threads are indeed
- // leaked.
- if crate::threads::engine().is_some() && !cfg!(asan) {
- run_all(
- "tests/wasi_testsuite/wasi-threads",
- &["-Sthreads", "-Wthreads"],
- &[],
- )?;
- }
- Ok(())
-}
-
-fn run_all(testsuite_dir: &str, extra_flags: &[&str], ignore: &[&str]) -> Result<()> {
- // In case the previous run ended in failure, we clean up any created files
- // that would otherwise be cleaned up at the end of this function.
- clean_garbage(testsuite_dir)?;
-
- // Execute and check each WebAssembly test case.
- for module in list_files(testsuite_dir, is_wasm) {
- if should_ignore(&module, ignore) {
- println!("Ignoring {}", module.display());
- } else {
- println!("Testing {}", module.display());
- let spec = if let Ok(contents) = fs::read_to_string(&module.with_extension("json")) {
- serde_json::from_str(&contents)?
- } else {
- Spec::default()
- };
- let mut cmd = build_command(module, extra_flags, &spec)?;
- let result = cmd.output()?;
- if spec != result {
- println!(" command: {cmd:?}");
- println!(" spec: {spec:#?}");
- println!(" result.status: {}", result.status);
- if !result.stdout.is_empty() {
- println!(
- " result.stdout:\n {}",
- String::from_utf8_lossy(&result.stdout).replace("\n", "\n ")
- );
- }
- if !result.stderr.is_empty() {
- println!(
- " result.stderr:\n {}",
- String::from_utf8_lossy(&result.stderr).replace("\n", "\n ")
- );
- }
- panic!("FAILED! The result does not match the specification");
- }
- }
- }
-
- // Clean up any created files to avoid making the Git repository dirty.
- clean_garbage(testsuite_dir)
-}
-
-fn list_files<F>(testsuite_dir: &str, filter: F) -> impl Iterator<Item = PathBuf> + use<F>
-where
- F: FnMut(&DirEntry) -> bool,
-{
- WalkDir::new(testsuite_dir)
- .into_iter()
- .filter_map(Result::ok)
- .filter(filter)
- .map(|e| e.path().to_path_buf())
-}
-
-fn is_wasm(entry: &DirEntry) -> bool {
- let path = entry.path();
- let ext = path.extension().map(OsStr::to_str).flatten();
- path.is_file() && (ext == Some("wat") || ext == Some("wasm"))
-}
-
-fn should_ignore<P: AsRef<Path>>(path: P, ignore_list: &[&str]) -> bool {
- let file_name = path.as_ref().file_name().unwrap().to_str().unwrap();
- ignore_list.contains(&file_name)
-}
-
-fn build_command<P: AsRef<Path>>(module: P, extra_flags: &[&str], spec: &Spec) -> Result<Command> {
- let mut cmd = get_wasmtime_command()?;
- let parent_dir = module
- .as_ref()
- .parent()
- .ok_or(anyhow!("module has no parent?"))?;
-
- // Add arguments.
- cmd.args(["run", "-Ccache=n"]);
- cmd.args(extra_flags);
- if let Some(dirs) = &spec.dirs {
- for dir in dirs {
- cmd.arg("--dir");
- cmd.arg(format!("{}::{}", parent_dir.join(dir).display(), dir));
- }
- }
- // Add environment variables as CLI arguments.
- if let Some(env) = &spec.env {
- for env_pair in env {
- cmd.arg("--env");
- cmd.arg(format!("{}={}", env_pair.0, env_pair.1));
- }
- cmd.envs(env);
- }
- cmd.arg(module.as_ref().to_str().unwrap());
- if let Some(spec_args) = &spec.args {
- cmd.args(spec_args);
- }
-
- Ok(cmd)
-}
-
-fn clean_garbage(testsuite_dir: &str) -> Result<()> {
- for path in list_files(testsuite_dir, is_garbage) {
- println!("Removing {}", path.display());
- if path.is_dir() {
- fs::remove_dir_all(path)?;
- } else {
- fs::remove_file(path)?;
- }
- }
- Ok(())
-}
-
-fn is_garbage(entry: &DirEntry) -> bool {
- let path = entry.path();
- let ext = path.extension().map(OsStr::to_str).flatten();
- ext == Some("cleanup")
-}
-
-#[derive(Debug, Default, Deserialize)]
-struct Spec {
- args: Option<Vec<String>>,
- dirs: Option<Vec<String>>,
- env: Option<HashMap<String, String>>,
- exit_code: Option<i32>,
- stderr: Option<String>,
- stdout: Option<String>,
-}
-
-impl PartialEq<Output> for Spec {
- fn eq(&self, other: &Output) -> bool {
- self.exit_code.unwrap_or(0) == other.status.code().unwrap()
- && matches_or_missing(&self.stdout, &other.stdout)
- && matches_or_missing(&self.stderr, &other.stderr)
- }
-}
-
-fn matches_or_missing(a: &Option<String>, b: &[u8]) -> bool {
- a.as_ref()
- .map(|s| s == &String::from_utf8_lossy(b))
- .unwrap_or(true)
-}
diff --git a/tests/wasi.rs b/tests/wasi.rs
new file mode 100644
index 0000000000..c796fdd3c9
--- /dev/null
+++ b/tests/wasi.rs
@@ -0,0 +1,274 @@
+//! Run the tests in `wasi_testsuite` using Wasmtime's CLI binary and checking
+//! the results with a [wasi-testsuite] spec.
+//!
+//! [wasi-testsuite]: https://github.com/WebAssembly/wasi-testsuite
+
+use anyhow::{Result, anyhow};
+use libtest_mimic::{Arguments, Trial};
+use serde_derive::Deserialize;
+use std::collections::HashMap;
+use std::fmt::Write;
+use std::fs;
+use std::path::Path;
+use std::process::Output;
+use tempfile::TempDir;
+use wit_component::ComponentEncoder;
+
+const KNOWN_FAILURES: &[&str] = &[
+ // FIXME(WebAssembly/wasi-testsuite#117 and #124) - these tests assert the
+ // host runtime supports rights, but we don't.
+ "fd_fdstat_set_rights",
+ "path_link",
+ // FIXME(#11524)
+ "remove_directory_trailing_slashes",
+ // FIXME(WebAssembly/wasi-testsuite#125)
+ "pwrite-with-append",
+ // FIXME(WebAssembly/wasi-testsuite#128)
+ #[cfg(windows)]
+ "dangling_fd",
+ #[cfg(windows)]
+ "dangling_symlink",
+ #[cfg(windows)]
+ "file_allocate",
+ #[cfg(windows)]
+ "file_pread_pwrite",
+ #[cfg(windows)]
+ "file_seek_tell",
+ #[cfg(windows)]
+ "file_truncation",
+ #[cfg(windows)]
+ "file_unbuffered_write",
+ #[cfg(windows)]
+ "interesting_paths",
+ #[cfg(windows)]
+ "isatty",
+ #[cfg(windows)]
+ "fd_readdir",
+ #[cfg(windows)]
+ "nofollow_errors",
+ #[cfg(windows)]
+ "overwrite_preopen",
+ #[cfg(windows)]
+ "path_exists",
+ #[cfg(windows)]
+ "path_filestat",
+ #[cfg(windows)]
+ "path_open_create_existing",
+ #[cfg(windows)]
+ "path_open_dirfd_not_dir",
+ #[cfg(windows)]
+ "path_open_missing",
+ #[cfg(windows)]
+ "path_open_preopen",
+ #[cfg(windows)]
+ "path_open_read_write",
+ #[cfg(windows)]
+ "path_rename",
+ #[cfg(windows)]
+ "path_rename_dir_trailing_slashes",
+ #[cfg(windows)]
+ "path_symlink_trailing_slashes",
+ #[cfg(windows)]
+ "readlink",
+ #[cfg(windows)]
+ "remove_nonempty_directory",
+ #[cfg(windows)]
+ "renumber",
+ #[cfg(windows)]
+ "symlink_create",
+ #[cfg(windows)]
+ "stdio",
+ #[cfg(windows)]
+ "symlink_filestat",
+ #[cfg(windows)]
+ "truncation_rights",
+ #[cfg(windows)]
+ "symlink_loop",
+ #[cfg(windows)]
+ "unlink_file_trailing_slashes",
+];
+
+fn main() -> Result<()> {
+ env_logger::init();
+
+ let mut trials = Vec::new();
+ if !cfg!(miri) {
+ find_tests("tests/wasi_testsuite/wasi-common".as_ref(), &mut trials).unwrap();
+ find_tests("tests/wasi_testsuite/wasi-threads".as_ref(), &mut trials).unwrap();
+ }
+
+ libtest_mimic::run(&Arguments::from_args(), trials).exit()
+}
+
+fn find_tests(path: &Path, trials: &mut Vec<Trial>) -> Result<()> {
+ for entry in path.read_dir()? {
+ let entry = entry?;
+ let path = entry.path();
+ if entry.file_type()?.is_dir() {
+ find_tests(&path, trials)?;
+ continue;
+ }
+ if path.extension().and_then(|s| s.to_str()) != Some("wasm") {
+ continue;
+ }
+
+ // Test the core wasm itself.
+ trials.push(Trial::test(
+ format!("wasmtime-wasi - {}", path.display()),
+ {
+ let path = path.clone();
+ move || run_test(&path, false).map_err(|e| format!("{e:?}").into())
+ },
+ ));
+
+ // Also test the component version using the wasip1 adapter. Note that
+ // this is skipped for `wasi-threads` since that's not supported in
+ // components and it's also skipped for assemblyscript because that
+ // doesn't support the wasip1 adapter.
+ if !path.iter().any(|p| p == "wasi-threads") && !path.iter().any(|p| p == "assemblyscript")
+ {
+ trials.push(Trial::test(
+ format!("wasip1 adapter - {}", path.display()),
+ move || run_test(&path, true).map_err(|e| format!("{e:?}").into()),
+ ));
+ }
+ }
+ Ok(())
+}
+
+fn run_test(path: &Path, componentize: bool) -> Result<()> {
+ let wasmtime = Path::new(env!("CARGO_BIN_EXE_wasmtime"));
+ let test_name = path.file_stem().unwrap().to_str().unwrap();
+ let target_dir = wasmtime.parent().unwrap().parent().unwrap();
+ let parent_dir = path.parent().ok_or(anyhow!("module has no parent?"))?;
+ let spec = if let Ok(contents) = fs::read_to_string(&path.with_extension("json")) {
+ serde_json::from_str(&contents)?
+ } else {
+ Spec::default()
+ };
+
+ let mut td = TempDir::new_in(&target_dir)?;
+ td.disable_cleanup(true);
+ let path = if componentize {
+ let module = fs::read(path).expect("read wasm module");
+ let component = ComponentEncoder::default()
+ .module(module.as_slice())?
+ .validate(true)
+ .adapter(
+ "wasi_snapshot_preview1",
+ &fs::read(test_programs_artifacts::ADAPTER_COMMAND)?,
+ )?
+ .encode()?;
+ let stem = path.file_stem().unwrap().to_str().unwrap();
+ let component_path = td.path().join(format!("{stem}.component.wasm"));
+ fs::write(&component_path, component)?;
+ component_path
+ } else {
+ path.to_path_buf()
+ };
+
+ let Spec {
+ args,
+ dirs,
+ env,
+ exit_code: _,
+ stderr: _,
+ stdout: _,
+ } = &spec;
+ let mut cmd = wasmtime_test_util::command(wasmtime);
+ cmd.arg("run");
+ for dir in dirs {
+ cmd.arg("--dir");
+ let src = parent_dir.join(dir);
+ let dst = td.path().join(dir);
+ cp_r(&src, &dst)?;
+ cmd.arg(format!("{}::{dir}", dst.display()));
+ }
+ for (k, v) in env {
+ cmd.arg("--env");
+ cmd.arg(format!("{k}={v}"));
+ }
+ cmd.arg(path);
+ cmd.args(args);
+
+ let result = cmd.output()?;
+ td.disable_cleanup(true);
+ let ok = spec == result;
+ match (ok, KNOWN_FAILURES.contains(&test_name)) {
+ // If this test passed and is not a known failure, or if it failed and
+ // it's a known failure, then flag this test as "ok".
+ (true, false) | (false, true) => Ok(()),
+
+ // If this test failed and it's not known to fail, explain why.
+ (false, false) => {
+ td.disable_cleanup(false);
+ let mut msg = String::new();
+ writeln!(msg, " command: {cmd:?}")?;
+ writeln!(msg, " spec: {spec:#?}")?;
+ writeln!(msg, " result.status: {}", result.status)?;
+ if !result.stdout.is_empty() {
+ write!(
+ msg,
+ " result.stdout:\n {}",
+ String::from_utf8_lossy(&result.stdout).replace("\n", "\n ")
+ )?;
+ }
+ if !result.stderr.is_empty() {
+ writeln!(
+ msg,
+ " result.stderr:\n {}",
+ String::from_utf8_lossy(&result.stderr).replace("\n", "\n ")
+ )?;
+ }
+ anyhow::bail!("{msg}\nFAILED! The result does not match the specification");
+ }
+
+ // If this test passed but it's flagged as should be failed, then fail
+ // this test for someone to update `KNOWN_FAILURES`.
+ (true, true) => {
+ anyhow::bail!("test passed but it's listed in `KNOWN_FAILURES`")
+ }
+ }
+}
+
+fn cp_r(path: &Path, dst: &Path) -> Result<()> {
+ fs::create_dir(dst)?;
+ for entry in path.read_dir()? {
+ let entry = entry?;
+ let path = entry.path();
+ let dst = dst.join(entry.file_name());
+ if entry.file_type()?.is_dir() {
+ cp_r(&path, &dst)?;
+ } else {
+ fs::copy(&path, &dst)?;
+ }
+ }
+ Ok(())
+}
+
+#[derive(Debug, Default, Deserialize)]
+struct Spec {
+ #[serde(default)]
+ args: Vec<String>,
+ #[serde(default)]
+ dirs: Vec<String>,
+ #[serde(default)]
+ env: HashMap<String, String>,
+ exit_code: Option<i32>,
+ stderr: Option<String>,
+ stdout: Option<String>,
+}
+
+impl PartialEq<Output> for Spec {
+ fn eq(&self, other: &Output) -> bool {
+ self.exit_code.unwrap_or(0) == other.status.code().unwrap()
+ && matches_or_missing(&self.stdout, &other.stdout)
+ && matches_or_missing(&self.stderr, &other.stderr)
+ }
+}
+
+fn matches_or_missing(a: &Option<String>, b: &[u8]) -> bool {
+ a.as_ref()
+ .map(|s| s == &String::from_utf8_lossy(b))
+ .unwrap_or(true)
+}
diff --git a/tests/wasi_testsuite/wasi-common b/tests/wasi_testsuite/wasi-common
index 2fec29ea6d..c11cd6bbee 160000
--- a/tests/wasi_testsuite/wasi-common
+++ b/tests/wasi_testsuite/wasi-common
@@ -1 +1 @@
-Subproject commit 2fec29ea6de1244c124f7fe3bfe9f2946113f66e
+Subproject commit c11cd6bbee3e209431415262f9701e42e9fe050a