From e13fabb2760b31ee0b6a0976940f9c957c4c4c9b Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 9 Dec 2019 04:08:47 -0800 Subject: [PATCH] Start to update the `wasi` crate in wasi tests (#675) * 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 --- crates/test-programs/build.rs | 2 + .../test-programs/tests/wasm_tests/runtime.rs | 71 +++++++++++-------- crates/test-programs/wasi-tests/Cargo.toml | 3 +- .../wasi-tests/src/bin/big_random_buf.rs | 9 +-- .../wasi-tests/src/bin/clock_time_get.rs | 26 ++----- .../wasi-tests/src/bin/close_preopen.rs | 44 ++++-------- .../wasi-tests/src/bin/dangling_fd.rs | 2 +- .../wasi-tests/src/bin/dangling_symlink.rs | 2 +- .../wasi-tests/src/bin/directory_seek.rs | 2 +- .../wasi-tests/src/bin/fd_advise.rs | 2 +- .../wasi-tests/src/bin/fd_filestat_set.rs | 2 +- .../wasi-tests/src/bin/fd_readdir.rs | 2 +- .../wasi-tests/src/bin/file_allocate.rs | 2 +- .../wasi-tests/src/bin/file_pread_pwrite.rs | 2 +- .../wasi-tests/src/bin/file_seek_tell.rs | 2 +- .../src/bin/file_unbuffered_write.rs | 2 +- .../wasi-tests/src/bin/interesting_paths.rs | 2 +- .../wasi-tests/src/bin/isatty.rs | 2 +- .../wasi-tests/src/bin/nofollow_errors.rs | 2 +- .../wasi-tests/src/bin/path_filestat.rs | 2 +- .../wasi-tests/src/bin/path_link.rs | 2 +- .../src/bin/path_open_create_existing.rs | 2 +- .../src/bin/path_open_dirfd_not_dir.rs | 2 +- .../wasi-tests/src/bin/path_rename.rs | 2 +- .../src/bin/path_rename_trailing_slashes.rs | 2 +- .../src/bin/path_symlink_trailing_slashes.rs | 2 +- .../wasi-tests/src/bin/poll_oneoff.rs | 2 +- .../wasi-tests/src/bin/readlink.rs | 2 +- .../wasi-tests/src/bin/readlink_no_buffer.rs | 2 +- .../bin/remove_directory_trailing_slashes.rs | 2 +- .../src/bin/remove_nonempty_directory.rs | 2 +- .../wasi-tests/src/bin/renumber.rs | 2 +- .../wasi-tests/src/bin/sched_yield.rs | 2 +- .../test-programs/wasi-tests/src/bin/stdio.rs | 2 +- .../wasi-tests/src/bin/symlink_loop.rs | 2 +- .../wasi-tests/src/bin/truncation_rights.rs | 2 +- .../src/bin/unlink_file_trailing_slashes.rs | 2 +- crates/test-programs/wasi-tests/src/lib.rs | 36 +++++++++- crates/test-programs/wasi-tests/src/utils.rs | 2 +- .../wasi-tests/src/wasi_wrappers.rs | 2 +- 40 files changed, 137 insertions(+), 120 deletions(-) diff --git a/crates/test-programs/build.rs b/crates/test-programs/build.rs index 0c453be639..0f223b1c71 100644 --- a/crates/test-programs/build.rs +++ b/crates/test-programs/build.rs @@ -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"), diff --git a/crates/test-programs/tests/wasm_tests/runtime.rs b/crates/test-programs/tests/wasm_tests/runtime.rs index 2d21c29e88..ea631eaab1 100644 --- a/crates/test-programs/tests/wasm_tests/runtime.rs +++ b/crates/test-programs/tests/wasm_tests/runtime.rs @@ -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}; @@ -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> { if let Some(workspace) = workspace { @@ -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(); @@ -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")?); @@ -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", module_name) + 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 {} was not found in module {}", + field_name, + i.module(), + ) } }) .collect::, _>>()?; diff --git a/crates/test-programs/wasi-tests/Cargo.toml b/crates/test-programs/wasi-tests/Cargo.toml index 5e7c471b04..345e5ce50a 100644 --- a/crates/test-programs/wasi-tests/Cargo.toml +++ b/crates/test-programs/wasi-tests/Cargo.toml @@ -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 diff --git a/crates/test-programs/wasi-tests/src/bin/big_random_buf.rs b/crates/test-programs/wasi-tests/src/bin/big_random_buf.rs index 5cbbc11928..ad40497f5c 100644 --- a/crates/test-programs/wasi-tests/src/bin/big_random_buf.rs +++ b/crates/test-programs/wasi-tests/src/bin/big_random_buf.rs @@ -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"); diff --git a/crates/test-programs/wasi-tests/src/bin/clock_time_get.rs b/crates/test-programs/wasi-tests/src/bin/clock_time_get.rs index 0ad84d37fd..2ef8a6c8f3 100644 --- a/crates/test-programs/wasi-tests/src/bin/clock_time_get.rs +++ b/crates/test-programs/wasi-tests/src/bin/clock_time_get.rs @@ -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"); } diff --git a/crates/test-programs/wasi-tests/src/bin/close_preopen.rs b/crates/test-programs/wasi-tests/src/bin/close_preopen.rs index 57acb6f747..bf06d109ef 100644 --- a/crates/test-programs/wasi-tests/src/bin/close_preopen.rs +++ b/crates/test-programs/wasi-tests/src/bin/close_preopen.rs @@ -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", ); } @@ -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); diff --git a/crates/test-programs/wasi-tests/src/bin/dangling_fd.rs b/crates/test-programs/wasi-tests/src/bin/dangling_fd.rs index 0f085724b2..23e08d3a32 100644 --- a/crates/test-programs/wasi-tests/src/bin/dangling_fd.rs +++ b/crates/test-programs/wasi-tests/src/bin/dangling_fd.rs @@ -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; diff --git a/crates/test-programs/wasi-tests/src/bin/dangling_symlink.rs b/crates/test-programs/wasi-tests/src/bin/dangling_symlink.rs index 4a36f052ad..a634742f70 100644 --- a/crates/test-programs/wasi-tests/src/bin/dangling_symlink.rs +++ b/crates/test-programs/wasi-tests/src/bin/dangling_symlink.rs @@ -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}; diff --git a/crates/test-programs/wasi-tests/src/bin/directory_seek.rs b/crates/test-programs/wasi-tests/src/bin/directory_seek.rs index 6cf3ff10a9..714f1f7e36 100644 --- a/crates/test-programs/wasi-tests/src/bin/directory_seek.rs +++ b/crates/test-programs/wasi-tests/src/bin/directory_seek.rs @@ -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}; diff --git a/crates/test-programs/wasi-tests/src/bin/fd_advise.rs b/crates/test-programs/wasi-tests/src/bin/fd_advise.rs index c436cec6de..e0c0056e2f 100644 --- a/crates/test-programs/wasi-tests/src/bin/fd_advise.rs +++ b/crates/test-programs/wasi-tests/src/bin/fd_advise.rs @@ -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}; diff --git a/crates/test-programs/wasi-tests/src/bin/fd_filestat_set.rs b/crates/test-programs/wasi-tests/src/bin/fd_filestat_set.rs index 13bfd4a8b9..7d5f68f0ae 100644 --- a/crates/test-programs/wasi-tests/src/bin/fd_filestat_set.rs +++ b/crates/test-programs/wasi-tests/src/bin/fd_filestat_set.rs @@ -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}; diff --git a/crates/test-programs/wasi-tests/src/bin/fd_readdir.rs b/crates/test-programs/wasi-tests/src/bin/fd_readdir.rs index 7c93ab6ee2..52902a07ba 100644 --- a/crates/test-programs/wasi-tests/src/bin/fd_readdir.rs +++ b/crates/test-programs/wasi-tests/src/bin/fd_readdir.rs @@ -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}; diff --git a/crates/test-programs/wasi-tests/src/bin/file_allocate.rs b/crates/test-programs/wasi-tests/src/bin/file_allocate.rs index 5c9d5784f6..bb0e8e4c24 100644 --- a/crates/test-programs/wasi-tests/src/bin/file_allocate.rs +++ b/crates/test-programs/wasi-tests/src/bin/file_allocate.rs @@ -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}; diff --git a/crates/test-programs/wasi-tests/src/bin/file_pread_pwrite.rs b/crates/test-programs/wasi-tests/src/bin/file_pread_pwrite.rs index b9e9924ed1..d1c2c3fed9 100644 --- a/crates/test-programs/wasi-tests/src/bin/file_pread_pwrite.rs +++ b/crates/test-programs/wasi-tests/src/bin/file_pread_pwrite.rs @@ -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}; diff --git a/crates/test-programs/wasi-tests/src/bin/file_seek_tell.rs b/crates/test-programs/wasi-tests/src/bin/file_seek_tell.rs index abdd0aa56b..30fef2194a 100644 --- a/crates/test-programs/wasi-tests/src/bin/file_seek_tell.rs +++ b/crates/test-programs/wasi-tests/src/bin/file_seek_tell.rs @@ -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}; diff --git a/crates/test-programs/wasi-tests/src/bin/file_unbuffered_write.rs b/crates/test-programs/wasi-tests/src/bin/file_unbuffered_write.rs index 656a04429a..1e8f6ddf0a 100644 --- a/crates/test-programs/wasi-tests/src/bin/file_unbuffered_write.rs +++ b/crates/test-programs/wasi-tests/src/bin/file_unbuffered_write.rs @@ -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}; diff --git a/crates/test-programs/wasi-tests/src/bin/interesting_paths.rs b/crates/test-programs/wasi-tests/src/bin/interesting_paths.rs index d712d09ec9..a8e97c1d77 100644 --- a/crates/test-programs/wasi-tests/src/bin/interesting_paths.rs +++ b/crates/test-programs/wasi-tests/src/bin/interesting_paths.rs @@ -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::{ diff --git a/crates/test-programs/wasi-tests/src/bin/isatty.rs b/crates/test-programs/wasi-tests/src/bin/isatty.rs index e2996d52f9..988b9fb069 100644 --- a/crates/test-programs/wasi-tests/src/bin/isatty.rs +++ b/crates/test-programs/wasi-tests/src/bin/isatty.rs @@ -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; diff --git a/crates/test-programs/wasi-tests/src/bin/nofollow_errors.rs b/crates/test-programs/wasi-tests/src/bin/nofollow_errors.rs index 8102104d09..ca77a1a713 100644 --- a/crates/test-programs/wasi-tests/src/bin/nofollow_errors.rs +++ b/crates/test-programs/wasi-tests/src/bin/nofollow_errors.rs @@ -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}; diff --git a/crates/test-programs/wasi-tests/src/bin/path_filestat.rs b/crates/test-programs/wasi-tests/src/bin/path_filestat.rs index 006e25c772..762f808143 100644 --- a/crates/test-programs/wasi-tests/src/bin/path_filestat.rs +++ b/crates/test-programs/wasi-tests/src/bin/path_filestat.rs @@ -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::{ diff --git a/crates/test-programs/wasi-tests/src/bin/path_link.rs b/crates/test-programs/wasi-tests/src/bin/path_link.rs index d4b0200722..787265535d 100644 --- a/crates/test-programs/wasi-tests/src/bin/path_link.rs +++ b/crates/test-programs/wasi-tests/src/bin/path_link.rs @@ -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::{ diff --git a/crates/test-programs/wasi-tests/src/bin/path_open_create_existing.rs b/crates/test-programs/wasi-tests/src/bin/path_open_create_existing.rs index 11bf603a54..296f18a318 100644 --- a/crates/test-programs/wasi-tests/src/bin/path_open_create_existing.rs +++ b/crates/test-programs/wasi-tests/src/bin/path_open_create_existing.rs @@ -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; diff --git a/crates/test-programs/wasi-tests/src/bin/path_open_dirfd_not_dir.rs b/crates/test-programs/wasi-tests/src/bin/path_open_dirfd_not_dir.rs index 113c1d6a57..e478ce52fa 100644 --- a/crates/test-programs/wasi-tests/src/bin/path_open_dirfd_not_dir.rs +++ b/crates/test-programs/wasi-tests/src/bin/path_open_dirfd_not_dir.rs @@ -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::close_fd; use wasi_tests::wasi_wrappers::wasi_path_open; diff --git a/crates/test-programs/wasi-tests/src/bin/path_rename.rs b/crates/test-programs/wasi-tests/src/bin/path_rename.rs index d8d2fd3f22..08df1b0aab 100644 --- a/crates/test-programs/wasi-tests/src/bin/path_rename.rs +++ b/crates/test-programs/wasi-tests/src/bin/path_rename.rs @@ -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, close_fd, create_dir, create_file}; use wasi_tests::wasi_wrappers::{wasi_path_open, wasi_path_rename}; diff --git a/crates/test-programs/wasi-tests/src/bin/path_rename_trailing_slashes.rs b/crates/test-programs/wasi-tests/src/bin/path_rename_trailing_slashes.rs index a1b347c683..eef7525d50 100644 --- a/crates/test-programs/wasi-tests/src/bin/path_rename_trailing_slashes.rs +++ b/crates/test-programs/wasi-tests/src/bin/path_rename_trailing_slashes.rs @@ -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_dir, cleanup_file, create_dir, create_file}; use wasi_tests::wasi_wrappers::wasi_path_rename; diff --git a/crates/test-programs/wasi-tests/src/bin/path_symlink_trailing_slashes.rs b/crates/test-programs/wasi-tests/src/bin/path_symlink_trailing_slashes.rs index 7eb4eea2b4..c9d38eee3a 100644 --- a/crates/test-programs/wasi-tests/src/bin/path_symlink_trailing_slashes.rs +++ b/crates/test-programs/wasi-tests/src/bin/path_symlink_trailing_slashes.rs @@ -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_dir, cleanup_file, create_dir, create_file}; use wasi_tests::wasi_wrappers::wasi_path_symlink; diff --git a/crates/test-programs/wasi-tests/src/bin/poll_oneoff.rs b/crates/test-programs/wasi-tests/src/bin/poll_oneoff.rs index 37b51d3985..6054340778 100644 --- a/crates/test-programs/wasi-tests/src/bin/poll_oneoff.rs +++ b/crates/test-programs/wasi-tests/src/bin/poll_oneoff.rs @@ -1,6 +1,6 @@ use more_asserts::assert_gt; use std::{env, mem::MaybeUninit, process}; -use wasi::wasi_unstable; +use wasi_old::wasi_unstable; use wasi_tests::{ open_scratch_directory, utils::{cleanup_file, close_fd}, diff --git a/crates/test-programs/wasi-tests/src/bin/readlink.rs b/crates/test-programs/wasi-tests/src/bin/readlink.rs index 11486d1607..69c6a487bc 100644 --- a/crates/test-programs/wasi-tests/src/bin/readlink.rs +++ b/crates/test-programs/wasi-tests/src/bin/readlink.rs @@ -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, create_file}; use wasi_tests::wasi_wrappers::{wasi_path_readlink, wasi_path_symlink}; diff --git a/crates/test-programs/wasi-tests/src/bin/readlink_no_buffer.rs b/crates/test-programs/wasi-tests/src/bin/readlink_no_buffer.rs index 9886258a05..d04f3f600a 100644 --- a/crates/test-programs/wasi-tests/src/bin/readlink_no_buffer.rs +++ b/crates/test-programs/wasi-tests/src/bin/readlink_no_buffer.rs @@ -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_readlink, wasi_path_symlink}; diff --git a/crates/test-programs/wasi-tests/src/bin/remove_directory_trailing_slashes.rs b/crates/test-programs/wasi-tests/src/bin/remove_directory_trailing_slashes.rs index a1bc56a82e..7c7c6e9b8c 100644 --- a/crates/test-programs/wasi-tests/src/bin/remove_directory_trailing_slashes.rs +++ b/crates/test-programs/wasi-tests/src/bin/remove_directory_trailing_slashes.rs @@ -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, create_dir, create_file}; use wasi_tests::wasi_wrappers::wasi_path_remove_directory; diff --git a/crates/test-programs/wasi-tests/src/bin/remove_nonempty_directory.rs b/crates/test-programs/wasi-tests/src/bin/remove_nonempty_directory.rs index 81f9107dcd..e8ea9cf159 100644 --- a/crates/test-programs/wasi-tests/src/bin/remove_nonempty_directory.rs +++ b/crates/test-programs/wasi-tests/src/bin/remove_nonempty_directory.rs @@ -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_dir, create_dir}; use wasi_tests::wasi_wrappers::wasi_path_remove_directory; diff --git a/crates/test-programs/wasi-tests/src/bin/renumber.rs b/crates/test-programs/wasi-tests/src/bin/renumber.rs index 3bab561028..33c0fc4764 100644 --- a/crates/test-programs/wasi-tests/src/bin/renumber.rs +++ b/crates/test-programs/wasi-tests/src/bin/renumber.rs @@ -1,7 +1,7 @@ use libc; 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::close_fd; use wasi_tests::wasi_wrappers::{wasi_fd_fdstat_get, wasi_path_open}; diff --git a/crates/test-programs/wasi-tests/src/bin/sched_yield.rs b/crates/test-programs/wasi-tests/src/bin/sched_yield.rs index d498f88495..57d2b73d9e 100644 --- a/crates/test-programs/wasi-tests/src/bin/sched_yield.rs +++ b/crates/test-programs/wasi-tests/src/bin/sched_yield.rs @@ -1,4 +1,4 @@ -use wasi::wasi_unstable; +use wasi_old::wasi_unstable; fn test_sched_yield() { assert!(wasi_unstable::sched_yield().is_ok(), "sched_yield"); diff --git a/crates/test-programs/wasi-tests/src/bin/stdio.rs b/crates/test-programs/wasi-tests/src/bin/stdio.rs index ae48aae8c1..10437e66a9 100644 --- a/crates/test-programs/wasi-tests/src/bin/stdio.rs +++ b/crates/test-programs/wasi-tests/src/bin/stdio.rs @@ -1,5 +1,5 @@ use std::mem::MaybeUninit; -use wasi::wasi_unstable; +use wasi_old::wasi_unstable; use wasi_tests::wasi_wrappers::wasi_fd_fdstat_get; unsafe fn test_stdio() { diff --git a/crates/test-programs/wasi-tests/src/bin/symlink_loop.rs b/crates/test-programs/wasi-tests/src/bin/symlink_loop.rs index b8d8b34a34..013f5de9e8 100644 --- a/crates/test-programs/wasi-tests/src/bin/symlink_loop.rs +++ b/crates/test-programs/wasi-tests/src/bin/symlink_loop.rs @@ -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}; diff --git a/crates/test-programs/wasi-tests/src/bin/truncation_rights.rs b/crates/test-programs/wasi-tests/src/bin/truncation_rights.rs index 11ba2e77f0..427d3b2a4a 100644 --- a/crates/test-programs/wasi-tests/src/bin/truncation_rights.rs +++ b/crates/test-programs/wasi-tests/src/bin/truncation_rights.rs @@ -1,5 +1,5 @@ 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_file, close_fd, create_file}; use wasi_tests::wasi_wrappers::{wasi_fd_fdstat_get, wasi_path_open}; diff --git a/crates/test-programs/wasi-tests/src/bin/unlink_file_trailing_slashes.rs b/crates/test-programs/wasi-tests/src/bin/unlink_file_trailing_slashes.rs index 14031c5f13..b32f8a619c 100644 --- a/crates/test-programs/wasi-tests/src/bin/unlink_file_trailing_slashes.rs +++ b/crates/test-programs/wasi-tests/src/bin/unlink_file_trailing_slashes.rs @@ -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_dir, create_dir, create_file}; use wasi_tests::wasi_wrappers::wasi_path_unlink_file; diff --git a/crates/test-programs/wasi-tests/src/lib.rs b/crates/test-programs/wasi-tests/src/lib.rs index 5960d09f5c..75fd1bb04d 100644 --- a/crates/test-programs/wasi-tests/src/lib.rs +++ b/crates/test-programs/wasi-tests/src/lib.rs @@ -4,8 +4,12 @@ pub mod wasi_wrappers; use libc; use std::ffi::CString; use std::io; -use wasi::wasi_unstable; +use wasi_old::wasi_unstable; +/// Opens a fresh file descriptor for `path` where `path` should be a preopened +/// directory. This is intended to be used with `wasi_unstable`, not with +/// `wasi_snapshot_preview1`. This is getting phased out and will likely be +/// deleted soon. pub fn open_scratch_directory(path: &str) -> Result { // Open the scratch directory. let dir_fd: wasi_unstable::Fd = unsafe { @@ -23,3 +27,33 @@ pub fn open_scratch_directory(path: &str) -> Result { Ok(dir_fd) } } + +/// Same as `open_scratch_directory` above, except uses `wasi_snapshot_preview1` +/// APIs instead of `wasi_unstable` ones. +/// +/// This is intended to replace `open_scratch_directory` once all the tests are +/// updated. +pub fn open_scratch_directory_new(path: &str) -> Result { + unsafe { + for i in 3.. { + let stat = match wasi::fd_prestat_get(i) { + Ok(s) => s, + Err(_) => break, + }; + if stat.pr_type != wasi::PREOPENTYPE_DIR { + continue; + } + let mut dst = Vec::with_capacity(stat.u.dir.pr_name_len); + if wasi::fd_prestat_dir_name(i, dst.as_mut_ptr(), dst.capacity()).is_err() { + continue; + } + dst.set_len(stat.u.dir.pr_name_len); + if dst == path.as_bytes() { + return Ok(wasi::path_open(i, 0, ".", wasi::OFLAGS_DIRECTORY, 0, 0, 0) + .expect("failed to open dir")); + } + } + + Err(format!("failed to find scratch dir")) + } +} diff --git a/crates/test-programs/wasi-tests/src/utils.rs b/crates/test-programs/wasi-tests/src/utils.rs index b9c1f1620c..fbf4e5b035 100644 --- a/crates/test-programs/wasi-tests/src/utils.rs +++ b/crates/test-programs/wasi-tests/src/utils.rs @@ -1,6 +1,6 @@ use crate::wasi_wrappers::*; use more_asserts::assert_gt; -use wasi::wasi_unstable; +use wasi_old::wasi_unstable; pub unsafe fn create_dir(dir_fd: wasi_unstable::Fd, dir_name: &str) { assert!( diff --git a/crates/test-programs/wasi-tests/src/wasi_wrappers.rs b/crates/test-programs/wasi-tests/src/wasi_wrappers.rs index 3fcfd8ba95..365ab7c62e 100644 --- a/crates/test-programs/wasi-tests/src/wasi_wrappers.rs +++ b/crates/test-programs/wasi-tests/src/wasi_wrappers.rs @@ -6,7 +6,7 @@ //! interfaces so that we can test whether they are stored to. In the future, //! WASI should switch to multi-value and eliminate out parameters altogether. -use wasi::wasi_unstable; +use wasi_old::wasi_unstable; pub unsafe fn wasi_path_create_directory( dir_fd: wasi_unstable::Fd,