Skip to content

Commit

Permalink
Improve support for preopened directories in WASI syscalls
Browse files Browse the repository at this point in the history
Also filters our default logging more and improves some debug statements
  • Loading branch information
Mark McCaskey committed Mar 4, 2020
1 parent b2e6535 commit 94bf2eb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
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

0 comments on commit 94bf2eb

Please sign in to comment.