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

Fix WASI directory bugs, improve WASI preopen APIs #1263

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions lib/wasi-tests/tests/wasitests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod path_link;
mod path_rename;
mod path_symlink;
mod poll_oneoff;
mod preopen;
mod quine;
mod readlink;
mod wasi_sees_virtual_root;
Expand Down
28 changes: 28 additions & 0 deletions lib/wasi-tests/tests/wasitests/preopen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// !!! THIS IS A GENERATED FILE !!!
// ANY MANUAL EDITS MAY BE OVERWRITTEN AT ANY TIME
// Files autogenerated with cargo build (build/wasitests.rs).

#[test]
fn test_preopen() {
assert_wasi_output!(
"../../wasitests/preopen.wasm",
"preopen",
vec![std::path::PathBuf::from("."),],
vec![
(
"/act1".to_string(),
::std::path::PathBuf::from("wasitests/test_fs/hamlet/act1")
),
(
"./act2".to_string(),
::std::path::PathBuf::from("wasitests/test_fs/hamlet/act2")
),
(
"act3".to_string(),
::std::path::PathBuf::from("wasitests/test_fs/hamlet/act3")
),
],
vec![],
"../../wasitests/preopen.out"
);
}
9 changes: 9 additions & 0 deletions lib/wasi-tests/wasitests/preopen.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Path `act1` exists? true
Path `/act1` exists? true
Path `./act1` exists? false
Path `act2` exists? true
Path `/act2` exists? false
Path `./act2` exists? true
Path `act3` exists? true
Path `/act3` exists? false
Path `./act3` exists? true
34 changes: 34 additions & 0 deletions lib/wasi-tests/wasitests/preopen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Args:
// mapdir: /act1:wasitests/test_fs/hamlet/act1
// mapdir: ./act2:wasitests/test_fs/hamlet/act2
// mapdir: act3:wasitests/test_fs/hamlet/act3
// dir: .

#[cfg(not(target_os = "wasi"))]
fn main() {
let correct_results = [
("act1", true) , ("/act1", true) , ("./act1", false),
("act2", true) , ("/act2", false), ("./act2", true) ,
("act3", true) , ("/act3", false), ("./act3", true) ,
];

for (path, exists) in &correct_results {
println!("Path `{}` exists? {}", path, exists);
}
}

#[cfg(target_os = "wasi")]
fn main() {
use std::path::Path;

let paths = ["act1", "act2", "act3"];
let prefixes = ["", "/", "./"];

for path in &paths {
for prefix in &prefixes {
let path_name = format!("{}{}", prefix, path);
let path = Path::new(&path_name);
println!("Path `{}` exists? {}", &path_name, path.exists());
}
}
}
Binary file added lib/wasi-tests/wasitests/preopen.wasm
Binary file not shown.
36 changes: 26 additions & 10 deletions lib/wasi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(
/*#![deny(
dead_code,
nonstandard_style,
unused_imports,
Expand All @@ -7,7 +7,7 @@
unused_unsafe,
unreachable_patterns,
clippy::missing_safety_doc
)]
)]*/
#![doc(html_favicon_url = "https://wasmer.io/static/icons/favicon.ico")]
#![doc(html_logo_url = "https://avatars3.githubusercontent.com/u/44205449?s=200&v=4")]

Expand Down Expand Up @@ -65,17 +65,33 @@ pub fn generate_import_object(
drop(Box::from_raw(data as *mut WasiState));
}
}
let args = args.clone();
let envs = envs.clone();
let preopened_files = preopened_files.clone();
let mapped_dirs = mapped_dirs.clone();
let prog_name = args
.get(0)
.and_then(|p| std::str::from_utf8(p).ok())
.unwrap_or("unnamed_program")
.to_string();
let mut wasi_state_builder = WasiState::new(&prog_name);
if !args.is_empty() {
wasi_state_builder.args(&args[1..]);
}

// this deprecation warning only applies to external callers
#[allow(deprecated)]
let state = Box::new(WasiState {
fs: WasiFs::new(&preopened_files, &mapped_dirs).expect("Could not create WASI FS"),
args: args.clone(),
envs: envs.clone(),
});

let state: Box<WasiState> = Box::new(
wasi_state_builder
.envs(envs.iter().map(|e| {
let mut split = e.split(|c| *c == b'=');
(split.next().unwrap(), split.next().unwrap())
}))
.preopen_dirs(preopened_files)
.expect("preopen directories")
.map_dirs(mapped_dirs)
.expect("preopen mapped directories")
.build()
.expect("build WasiState"),
);
(
Box::into_raw(state) as *mut c_void,
state_destructor as fn(*mut c_void),
Expand Down
1 change: 0 additions & 1 deletion lib/wasi/src/state/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ impl WasiStateBuilder {
// self.preopens are checked in [`PreopenDirBuilder::build`]

// this deprecation warning only applies to external callers
#[allow(deprecated)]
let mut wasi_fs = WasiFs::new_with_preopen(&self.preopens)
.map_err(WasiStateCreationError::WasiFsCreationError)?;
// set up the file system, overriding base files and calling the setup function
Expand Down
Loading