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

finish implementation of wasi::fd_seek, fix bug in filestat #554

Merged
merged 5 commits into from
Jul 13, 2019
Merged
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All PRs to the Wasmer repository must add to this file.
Blocks of changes will separated by version increments.

## **[Unreleased]**
- [#554](https://github.com/wasmerio/wasmer/pull/554) Finish implementation of `wasi::fd_seek`, fix bug in filestat

## 0.5.5
- [#541](https://github.com/wasmerio/wasmer/pull/541) Fix dependency graph by making separate test crates; ABI implementations should not depend on compilers. Add Cranelift fork as git submodule of clif-backend
Expand Down
3 changes: 1 addition & 2 deletions lib/wasi-tests/build/wasitests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub fn compile(file: &str, ignores: &HashSet<String>) -> Option<String> {
};

Command::new("rustc")
.arg("+nightly")
.arg(file)
.arg("-o")
.arg(&normalized_name)
Expand Down Expand Up @@ -85,7 +84,7 @@ pub fn compile(file: &str, ignores: &HashSet<String>) -> Option<String> {
out_str.push_str("vec![");
for (alias, real_dir) in args.mapdir {
out_str.push_str(&format!(
"(\"{}\".to_string(), \"{}\".to_string()),",
"(\"{}\".to_string(), ::std::path::PathBuf::from(\"{}\")),",
alias, real_dir
));
}
Expand Down
13 changes: 13 additions & 0 deletions lib/wasi-tests/tests/wasitests/fseek.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[test]
fn test_fseek() {
assert_wasi_output!(
"../../wasitests/fseek.wasm",
"fseek",
vec![(
".".to_string(),
::std::path::PathBuf::from("wasitests/test_fs/hamlet")
),],
vec![],
"../../wasitests/fseek.out"
);
}
5 changes: 4 additions & 1 deletion lib/wasi-tests/tests/wasitests/mapdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ fn test_mapdir() {
assert_wasi_output!(
"../../wasitests/mapdir.wasm",
"mapdir",
vec![],
vec![(
".".to_string(),
::std::path::PathBuf::from("wasitests/test_fs/hamlet")
),],
vec![],
"../../wasitests/mapdir.out"
);
Expand Down
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 @@ -9,6 +9,7 @@ mod create_dir;
mod envvar;
mod file_metadata;
mod fs_sandbox_test;
mod fseek;
mod hello;
mod mapdir;
mod quine;
Binary file modified lib/wasi-tests/wasitests/create_dir.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/envvar.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/file_metadata.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fs_sandbox_test.wasm
Binary file not shown.
11 changes: 11 additions & 0 deletions lib/wasi-tests/wasitests/fseek.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SCENE III. A room in Polonius' h
ouse.

Enter LAERTES and OPH
And, sister, as the winds gi
r talk with the Lord Hamlet.

uits,
Breathing like sanctif
is is for all:
I would not,
47 changes: 47 additions & 0 deletions lib/wasi-tests/wasitests/fseek.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Args:
// mapdir: .:wasitests/test_fs/hamlet

use std::fs;
use std::io::{Read, Seek, SeekFrom};
use std::path::PathBuf;

fn main() {
#[cfg(not(target_os = "wasi"))]
let mut base = PathBuf::from("wasitests/test_fs/hamlet");
#[cfg(target_os = "wasi")]
let mut base = PathBuf::from(".");

base.push("act1/scene3.txt");

let mut file = fs::File::open(&base).expect("Could not open file");

let mut buffer = [0u8; 32];

assert_eq!(file.read(&mut buffer).unwrap(), 32);
let str_val = std::str::from_utf8(&buffer[..]).unwrap();
println!("{}", str_val);

assert_eq!(file.read(&mut buffer).unwrap(), 32);
let str_val = std::str::from_utf8(&buffer[..]).unwrap();
println!("{}", str_val);

assert_eq!(file.seek(SeekFrom::Start(123)).unwrap(), 123);
assert_eq!(file.read(&mut buffer).unwrap(), 32);
let str_val = std::str::from_utf8(&buffer[..]).unwrap();
println!("{}", str_val);

assert_eq!(file.seek(SeekFrom::End(-123)).unwrap(), 6617);
assert_eq!(file.read(&mut buffer).unwrap(), 32);
let str_val = std::str::from_utf8(&buffer[..]).unwrap();
println!("{}", str_val);

assert_eq!(file.seek(SeekFrom::Current(-250)).unwrap(), 6399);
assert_eq!(file.read(&mut buffer).unwrap(), 32);
let str_val = std::str::from_utf8(&buffer[..]).unwrap();
println!("{}", str_val);

assert_eq!(file.seek(SeekFrom::Current(50)).unwrap(), 6481);
assert_eq!(file.read(&mut buffer).unwrap(), 32);
let str_val = std::str::from_utf8(&buffer[..]).unwrap();
println!("{}", str_val);
}
Binary file added lib/wasi-tests/wasitests/fseek.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/hello.wasm
Binary file not shown.
12 changes: 6 additions & 6 deletions lib/wasi-tests/wasitests/mapdir.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"wasitests/test_fs/hamlet/README.md"
"wasitests/test_fs/hamlet/act1"
"wasitests/test_fs/hamlet/act2"
"wasitests/test_fs/hamlet/act3"
"wasitests/test_fs/hamlet/act4"
"wasitests/test_fs/hamlet/act5"
"./README.md"
"./act1"
"./act2"
"./act3"
"./act4"
"./act5"
6 changes: 3 additions & 3 deletions lib/wasi-tests/wasitests/mapdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
use std::fs;

fn main() {
#[cfg(not(target = "wasi"))]
let read_dir = fs::read_dir("wasitests/test_fs/hamlet").unwrap();
#[cfg(target = "wasi")]
// #[cfg(not(target_os = "wasi"))]
// let read_dir = fs::read_dir("wasitests/test_fs/hamlet").unwrap();
// #[cfg(target_os = "wasi")]
let read_dir = fs::read_dir(".").unwrap();
let mut out = vec![];
for entry in read_dir {
Expand Down
Binary file modified lib/wasi-tests/wasitests/mapdir.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/quine.wasm
Binary file not shown.
44 changes: 42 additions & 2 deletions lib/wasi/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,28 @@ pub fn fd_seek(
// TODO: handle case if fd is a dir?
match whence {
__WASI_WHENCE_CUR => fd_entry.offset = (fd_entry.offset as i64 + offset) as u64,
__WASI_WHENCE_END => unimplemented!("__WASI__WHENCE_END in wasi::fd_seek"),
__WASI_WHENCE_END => {
use std::io::SeekFrom;
match state.fs.inodes[fd_entry.inode].kind {
Kind::File { ref mut handle } => {
let end = wasi_try!(handle.seek(SeekFrom::End(0)).ok().ok_or(__WASI_EIO));
// TODO: handle case if fd_entry.offset uses 64 bits of a u64
fd_entry.offset = (end as i64 + offset) as u64;
}
Kind::Symlink { .. } => {
unimplemented!("wasi::fd_seek not implemented for symlinks")
}
Kind::Dir { .. } => {
// TODO: check this
return __WASI_EINVAL;
}
Kind::Buffer { .. } => {
// seeking buffers probably makes sense
// TODO: implement this
return __WASI_EINVAL;
}
}
}
__WASI_WHENCE_SET => fd_entry.offset = offset as u64,
_ => return __WASI_EINVAL,
}
Expand Down Expand Up @@ -1235,6 +1256,7 @@ pub fn path_filestat_get(
let last_segment = path_vec.last().unwrap();
cumulative_path.push(last_segment);

// read it from wasi FS cache first, otherwise check host system
if entries.contains_key(last_segment) {
state.fs.inodes[entries[last_segment]].stat
} else {
Expand All @@ -1244,7 +1266,25 @@ pub fn path_filestat_get(
}
let final_path_metadata =
wasi_try!(cumulative_path.metadata().map_err(|_| __WASI_EIO));
wasi_try!(get_stat_for_kind(&state.fs.inodes[inode].kind).ok_or(__WASI_EIO))
let kind = if final_path_metadata.is_file() {
let file =
wasi_try!(std::fs::File::open(&cumulative_path).ok().ok_or(__WASI_EIO));
Kind::File {
handle: WasiFile::HostFile(file),
}
} else if final_path_metadata.is_dir() {
Kind::Dir {
parent: Some(inode),
// TODO: verify that this doesn't cause issues with relative paths
path: cumulative_path.clone(),
entries: Default::default(),
}
} else {
// TODO: check this
return __WASI_EINVAL;
};

wasi_try!(get_stat_for_kind(&kind).ok_or(__WASI_EIO))
}
}
_ => {
Expand Down