Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement wasi dir path rename #2559

Merged
merged 9 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ wasmer-engine-dylib = { version = "2.0.0", path = "lib/engine-dylib", optional =
wasmer-engine-staticlib = { version = "2.0.0", path = "lib/engine-staticlib", optional = true }
wasmer-wasi = { version = "2.0.0", path = "lib/wasi", optional = true }
wasmer-wast = { version = "2.0.0", path = "tests/lib/wast", optional = true }
wasi-test-generator = { version = "2.0.0", path = "tests/wasi-wast", optional = true }
wasmer-cache = { version = "2.0.0", path = "lib/cache", optional = true }
wasmer-types = { version = "2.0.0", path = "lib/types" }
wasmer-middlewares = { version = "2.0.0", path = "lib/middlewares", optional = true }
Expand Down Expand Up @@ -50,6 +51,7 @@ members = [
"lib/wasi-types",
"lib/wasi-experimental-io-devices",
"lib/types",
"tests/wasi-wast",
"tests/lib/wast",
"tests/lib/compiler-test-derive",
"tests/integration/cli",
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,9 @@ test-capi-integration-%:
test-wasi-unit:
cargo test --manifest-path lib/wasi/Cargo.toml --release

test-wasi:
cargo test --release --tests $(compiler_features) -- wasi::wasitests::snapshot1
bnjjj marked this conversation as resolved.
Show resolved Hide resolved

test-examples:
cargo test --release $(compiler_features) --features wasi --examples

Expand All @@ -573,6 +576,10 @@ test-integration:
test-integration-ios:
cargo test -p wasmer-integration-tests-ios

generate-wasi-tests:
# Uncomment the following for installing the toolchain
# cargo run -p wasi-test-generator -- -s
cargo run -p wasi-test-generator -- -g
#####
#
# Packaging.
Expand Down
9 changes: 9 additions & 0 deletions lib/c-api/wasmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,18 @@
# define DEPRECATED(message) __declspec(deprecated(message))
#endif

// The `universal` feature has been enabled for this build.
#define WASMER_UNIVERSAL_ENABLED

// The `compiler` feature has been enabled for this build.
#define WASMER_COMPILER_ENABLED

// The `wasi` feature has been enabled for this build.
#define WASMER_WASI_ENABLED

// The `middlewares` feature has been enabled for this build.
#define WASMER_MIDDLEWARES_ENABLED

bnjjj marked this conversation as resolved.
Show resolved Hide resolved
// This file corresponds to the following Wasmer version.
#define WASMER_VERSION "2.0.0"
#define WASMER_VERSION_MAJOR 2
Expand Down
1 change: 1 addition & 0 deletions lib/vfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub trait FileSystem: fmt::Debug + Send + Sync + 'static {
self.metadata(path)
}
fn remove_file(&self, path: &Path) -> Result<()>;

fn new_open_options(&self) -> OpenOptions;
}

Expand Down
17 changes: 14 additions & 3 deletions lib/wasi/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1990,7 +1990,7 @@ pub fn path_remove_directory(
),
}

if state.fs_remove_dir(path_str).is_err() {
if state.fs_remove_dir(host_path_to_remove).is_err() {
// reinsert to prevent FS from being in bad state
if let Kind::Dir {
ref mut entries, ..
Expand Down Expand Up @@ -2070,8 +2070,11 @@ pub fn path_rename(
unreachable!("Fatal internal logic error: parent of inode is not a directory")
}
};

let source_entry = match &mut state.fs.inodes[source_parent_inode].kind {
Kind::Dir { entries, .. } => wasi_try!(entries.remove(&source_entry_name), __WASI_EINVAL),
Kind::Dir { entries, .. } => {
wasi_try!(entries.remove(&source_entry_name), __WASI_EINVAL)
}
Kind::Root { .. } => return __WASI_ENOTCAPABLE,
Kind::Symlink { .. } | Kind::File { .. } | Kind::Buffer { .. } => {
unreachable!("Fatal internal logic error: parent of inode is not a directory")
Expand Down Expand Up @@ -2107,7 +2110,15 @@ pub fn path_rename(
}
}
}
Kind::Dir { path, .. } => unimplemented!("wasi::path_rename on Directories"),
Kind::Dir { ref path, .. } => {
let cloned_path = path.clone();
if let Err(e) = state.fs_rename(cloned_path, &host_adjusted_target_path) {
return e;
}
if let Kind::Dir { path, .. } = &mut state.fs.inodes[source_entry].kind {
*path = host_adjusted_target_path;
}
}
Kind::Buffer { .. } => {}
Kind::Symlink { .. } => {}
Kind::Root { .. } => unreachable!("The root can not be moved"),
Expand Down
2 changes: 1 addition & 1 deletion tests/compilers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Config {
self.add_middlewares(&mut compiler);
Box::new(compiler)
}
#[allow(dead_code)]
#[allow(unreachable_patterns)]
compiler => {
panic!(
"The {:?} Compiler is not enabled. Enable it via the features",
Expand Down
2 changes: 1 addition & 1 deletion tests/wasi-wast/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasi-test-generator"
version = "0.17.0"
version = "2.0.0"
description = "Tests for our WASI implementation"
license = "MIT"
authors = ["Wasmer Engineering Team <[email protected]>"]
Expand Down
9 changes: 9 additions & 0 deletions tests/wasi-wast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ In order to run the test generator properly you will need:
## Usage

```
Positional arguments:
free if you want to specify specific tests to generate
Optional arguments:
-a, --all-versions Whether or not to do operations for all versions of WASI or just the latest.
-g, --generate-wasm Whether or not the Wasm will be generated.
Expand All @@ -26,6 +29,12 @@ cargo run -- -as # set up the toolchains for all targets
cargo run -- -ag # generate the WASI tests for all targets
```

If you want to generate specific tests (it's faster when you're developing) you can use this command:

```bash
cargo run -- -g fd_rename_path # If you want to run the test in fd_rename_path.rs
```

## Updating in Wasmer

Run
Expand Down
6 changes: 5 additions & 1 deletion tests/wasi-wast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use gumdrop::Options;

#[derive(Debug, Options)]
pub struct TestGenOptions {
/// if you want to specify specific tests to generate
#[options(free)]
free: Vec<String>,
/// Whether or not to do operations for all versions of WASI or just the latest.
all_versions: bool,
/// Whether or not the Wasm will be generated.
Expand Down Expand Up @@ -48,6 +51,7 @@ fn main() {

// Generate the WASI Wasm files
if generate_wasm {
build(wasi_versions);
let specific_tests: Vec<&str> = opts.free.iter().map(|st| st.as_str()).collect();
build(wasi_versions, &specific_tests);
}
}
2 changes: 1 addition & 1 deletion tests/wasi-wast/src/wasi_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl WasiVersion {
pub fn get_compiler_toolchain(&self) -> &'static str {
match self {
WasiVersion::Unstable => "nightly-2019-09-13",
WasiVersion::Snapshot1 => "nightly-2019-12-18",
WasiVersion::Snapshot1 => "1.53.0",
}
}

Expand Down
14 changes: 11 additions & 3 deletions tests/wasi-wast/src/wasitests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn compile_wasm_for_version(
.create(true)
.open(&temp_wasi_rs_file_name)
.unwrap();
actual_file.write_all(b"#![feature(wasi_ext)]\n").unwrap();
// actual_file.write_all(b"#![feature(wasi_ext)]\n").unwrap();
bnjjj marked this conversation as resolved.
Show resolved Hide resolved
actual_file.write_all(file_contents.as_bytes()).unwrap();
}

Expand Down Expand Up @@ -246,13 +246,21 @@ fn compile(temp_dir: &Path, file: &str, wasi_versions: &[WasiVersion]) {
}

const WASI_TEST_SRC_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/wasi/tests/*.rs");
pub fn build(wasi_versions: &[WasiVersion]) {
pub fn build(wasi_versions: &[WasiVersion], specific_tests: &[&str]) {
let temp_dir = tempfile::TempDir::new().unwrap();
for entry in glob(WASI_TEST_SRC_DIR).unwrap() {
match entry {
Ok(path) => {
let test = path.to_str().unwrap();
compile(temp_dir.path(), test, wasi_versions);
if !specific_tests.is_empty() {
if let Some(filename) = path.file_stem().map(|f| f.to_str()).flatten() {
if specific_tests.contains(&filename) {
compile(temp_dir.path(), test, wasi_versions);
}
}
} else {
compile(temp_dir.path(), test, wasi_versions);
}
}
Err(e) => println!("{:?}", e),
}
Expand Down
Binary file not shown.
6 changes: 6 additions & 0 deletions tests/wasi-wast/wasi/snapshot1/fd_rename_path.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
;; This file was generated by https://github.com/wasmerio/wasi-tests

(wasi_test "fd_rename_path.wasm"
(preopens "test_fs")
(assert_return (i64.const 0))
)
14 changes: 14 additions & 0 deletions tests/wasi-wast/wasi/tests/fd_rename_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// WASI:
// dir: test_fs

use std::fs;
use std::path::PathBuf;

fn main() {
let old_path = PathBuf::from("test_fs/wasitests/dirtorename");
let new_path = PathBuf::from("test_fs/wasitests/dirrenamed");
fs::create_dir_all(&old_path).expect("cannot create the directory");

fs::rename(old_path, &new_path).expect("cannot rename the directory");
fs::remove_dir(&new_path).expect("cannot remove the directory");
}