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

Improve support for preopened directories in WASI syscalls #1267

Merged
merged 1 commit into from
Mar 4, 2020
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 @@ -2,6 +2,7 @@

## **[Unreleased]**

- [#1263](https://github.com/wasmerio/wasmer/pull/1263) Changed the behavior of some WASI syscalls to now handle preopened directories more properly. Changed default `--debug` logging to only show Wasmer-related messages.
- [#1217](https://github.com/wasmerio/wasmer/pull/1217) Polymorphic host functions based on dynamic trampoline generation.
- [#1252](https://github.com/wasmerio/wasmer/pull/1252) Allow `/` in wasi `--mapdir` wasm path.
- [#1212](https://github.com/wasmerio/wasmer/pull/1212) Add support for GDB JIT debugging:
Expand Down
9 changes: 9 additions & 0 deletions lib/wasi/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,15 @@ impl WasiFs {
fs_rights_inheriting: 0,
})
}
VIRTUAL_ROOT_FD => {
return Ok(__wasi_fdstat_t {
fs_filetype: __WASI_FILETYPE_DIRECTORY,
fs_flags: 0,
// TODO: fix this
fs_rights_base: ALL_RIGHTS,
fs_rights_inheriting: ALL_RIGHTS,
});
}
_ => (),
}
let fd = self.get_fd(fd)?;
Expand Down
50 changes: 36 additions & 14 deletions lib/wasi/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,7 @@ pub fn fd_allocate(
/// - `__WASI_EBADF`
/// If `fd` is invalid or not open
pub fn fd_close(ctx: &mut Ctx, fd: __wasi_fd_t) -> __wasi_errno_t {
debug!("wasi::fd_close");
debug!("=> fd={}", fd);
debug!("wasi::fd_close: fd={}", fd);
let (memory, state) = get_memory_and_wasi_state(ctx, 0);

let fd_entry = wasi_try!(state.fs.get_fd(fd));
Expand Down Expand Up @@ -649,7 +648,7 @@ pub fn fd_pread(
offset: __wasi_filesize_t,
nread: WasmPtr<u32>,
) -> __wasi_errno_t {
debug!("wasi::fd_pread");
debug!("wasi::fd_pread: fd={}, offset={}", fd, offset);
let (memory, state) = get_memory_and_wasi_state(ctx, 0);

let iov_cells = wasi_try!(iovs.deref(memory, 0, iovs_len));
Expand All @@ -674,6 +673,10 @@ pub fn fd_pread(
if !(has_rights(fd_entry.rights, __WASI_RIGHT_FD_READ)
&& has_rights(fd_entry.rights, __WASI_RIGHT_FD_SEEK))
{
debug!(
"Invalid rights on {:X}: expected READ and SEEK",
fd_entry.rights
);
return __WASI_EACCES;
}
match &mut state.fs.inodes[inode].kind {
Expand All @@ -699,6 +702,7 @@ pub fn fd_pread(
};

nread_cell.set(bytes_read);
debug!("Success: {} bytes read", bytes_read);
__WASI_ESUCCESS
}

Expand Down Expand Up @@ -971,24 +975,38 @@ pub fn fd_readdir(
let mut cur_cookie = cookie;
let mut buf_idx = 0;

let entries = match &state.fs.inodes[working_dir.inode].kind {
Kind::Dir { path, .. } => {
let entries: Vec<(String, u8, u64)> = match &state.fs.inodes[working_dir.inode].kind {
Kind::Dir { path, entries, .. } => {
// TODO: refactor this code
// we need to support multiple calls,
// simple and obviously correct implementation for now:
// maintain consistent order via lexacographic sorting
let mut entries = wasi_try!(wasi_try!(std::fs::read_dir(path).map_err(|_| __WASI_EIO))
.collect::<Result<Vec<std::fs::DirEntry>, _>>()
let fs_info = wasi_try!(wasi_try!(std::fs::read_dir(path).map_err(|_| __WASI_EIO))
.collect::<Result<Vec<_>, _>>()
.map_err(|_| __WASI_EIO));
entries.sort_by(|a, b| a.file_name().cmp(&b.file_name()));
wasi_try!(entries
let mut entry_vec = wasi_try!(fs_info
.into_iter()
.map(|entry| Ok((
entry.file_name().to_string_lossy().to_string(),
host_file_type_to_wasi_file_type(entry.file_type().map_err(|_| __WASI_EIO)?),
0, // TODO: inode
)))
.collect::<Result<Vec<(String, u8, u64)>, __wasi_errno_t>>())
.collect::<Result<Vec<(String, u8, u64)>, _>>());
entry_vec.extend(
entries
.iter()
.filter(|(_, inode)| state.fs.inodes[**inode].is_preopened)
.map(|(name, inode)| {
let entry = &state.fs.inodes[*inode];
(
format!("{}", entry.name),
entry.stat.st_filetype,
entry.stat.st_ino,
)
}),
);
entry_vec.sort_by(|a, b| a.0.cmp(&b.0));
entry_vec
}
Kind::Root { entries } => {
let sorted_entries = {
Expand Down Expand Up @@ -1435,10 +1453,14 @@ pub fn path_filestat_get(
path_string,
flags & __WASI_LOOKUP_SYMLINK_FOLLOW != 0,
));
let stat = wasi_try!(state
.fs
.get_stat_for_kind(&state.fs.inodes[file_inode].kind)
.ok_or(__WASI_EIO));
let stat = if state.fs.inodes[file_inode].is_preopened {
state.fs.inodes[file_inode].stat.clone()
} else {
wasi_try!(state
.fs
.get_stat_for_kind(&state.fs.inodes[file_inode].kind)
.ok_or(__WASI_EIO))
};

let buf_cell = wasi_try!(buf.deref(memory));
buf_cell.set(stat);
Expand Down
6 changes: 5 additions & 1 deletion src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ pub fn set_up_logging() -> Result<(), String> {
})
};

base.chain(std::io::stdout())
base
.filter(|metadata| {
metadata.target().starts_with("wasmer")
})
.chain(std::io::stdout())
});

dispatch.apply().map_err(|e| format!("{}", e))?;
Expand Down