Add enough Windows functionality to run WASI tutorial (#22)
* Add partial impl of determine_type_rights fn * Add draft of fd_fdstat_get hostcall * Add writev wrapper for writing IoVec in RawHandle * Move IoVec and writev to separate helper crate * Add Win error handling Clean up closing and duplicating RawHandle * Wrap Win file type result * Add draft impl of fd_close and fd_read * Refactor getting file access rights * Remove winapi from the main Cargo.toml * Add very rough draft of open_path (very incomplete) * Clean up WinError with macro * Ignore dir handle in openat if path absolute * Decode oflags and advance open_path hostcall * Clean up AccessRight and FlagsAndAttributes flags * Implement path_get (without symlink expansion yet!) * Add ShareMode and fix path_get for nested paths * Add some error mappings between Win and WASI * Clean up fdflags conversions * Fix sharing violation when calling openat at '.' * Apply Alex's fix of using ManuallyDrop instead forget * Clean up * Explicitly specify workspace to avoid comp errors at tests
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(unused_unsafe)]
|
||||
#![allow(unused)]
|
||||
use super::fdentry::FdEntry;
|
||||
use super::fdentry::{determine_type_rights, FdEntry};
|
||||
use super::fs_helpers::*;
|
||||
use super::host_impl;
|
||||
|
||||
use crate::ctx::WasiCtx;
|
||||
use crate::host;
|
||||
|
||||
use std::ffi::OsStr;
|
||||
use std::os::windows::prelude::FromRawHandle;
|
||||
|
||||
pub(crate) fn fd_close(fd_entry: FdEntry) -> Result<(), host::__wasi_errno_t> {
|
||||
unimplemented!("fd_close")
|
||||
winx::handle::close(fd_entry.fd_object.raw_handle).map_err(|e| host_impl::errno_from_win(e))
|
||||
}
|
||||
|
||||
pub(crate) fn fd_datasync(fd_entry: &FdEntry) -> Result<(), host::__wasi_errno_t> {
|
||||
@@ -37,7 +38,14 @@ pub(crate) fn fd_read(
|
||||
fd_entry: &FdEntry,
|
||||
iovs: &mut [host::__wasi_iovec_t],
|
||||
) -> Result<usize, host::__wasi_errno_t> {
|
||||
unimplemented!("fd_pread")
|
||||
use winx::io::{readv, IoVecMut};
|
||||
|
||||
let mut iovs: Vec<IoVecMut> = iovs
|
||||
.iter_mut()
|
||||
.map(|iov| unsafe { host_impl::iovec_to_win_mut(iov) })
|
||||
.collect();
|
||||
|
||||
readv(fd_entry.fd_object.raw_handle, &mut iovs).map_err(|e| host_impl::errno_from_win(e))
|
||||
}
|
||||
|
||||
pub(crate) fn fd_renumber(
|
||||
@@ -63,7 +71,13 @@ pub(crate) fn fd_tell(fd_entry: &FdEntry) -> Result<u64, host::__wasi_errno_t> {
|
||||
pub(crate) fn fd_fdstat_get(
|
||||
fd_entry: &FdEntry,
|
||||
) -> Result<host::__wasi_fdflags_t, host::__wasi_errno_t> {
|
||||
unimplemented!("fd_fdstat_get")
|
||||
use winx::file::AccessRight;
|
||||
match winx::file::get_file_access_rights(fd_entry.fd_object.raw_handle)
|
||||
.map(AccessRight::from_bits_truncate)
|
||||
{
|
||||
Ok(rights) => Ok(host_impl::fdflags_from_win(rights)),
|
||||
Err(e) => Err(host_impl::errno_from_win(e)),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn fd_fdstat_set_flags(
|
||||
@@ -81,32 +95,14 @@ pub(crate) fn fd_write(
|
||||
fd_entry: &FdEntry,
|
||||
iovs: &[host::__wasi_iovec_t],
|
||||
) -> Result<usize, host::__wasi_errno_t> {
|
||||
use winapi::shared::minwindef::{DWORD, LPVOID};
|
||||
use winapi::um::fileapi::WriteFile;
|
||||
use winx::io::{writev, IoVec};
|
||||
|
||||
let iovs: Vec<host_impl::IoVec> = iovs
|
||||
let iovs: Vec<IoVec> = iovs
|
||||
.iter()
|
||||
.map(|iov| unsafe { host_impl::iovec_to_win(iov) })
|
||||
.collect();
|
||||
|
||||
let buf = iovs
|
||||
.iter()
|
||||
.find(|b| !b.as_slice().is_empty())
|
||||
.map_or(&[][..], |b| b.as_slice());
|
||||
|
||||
let mut host_nwritten = 0;
|
||||
let len = std::cmp::min(buf.len(), <DWORD>::max_value() as usize) as DWORD;
|
||||
unsafe {
|
||||
WriteFile(
|
||||
fd_entry.fd_object.raw_handle,
|
||||
buf.as_ptr() as LPVOID,
|
||||
len,
|
||||
&mut host_nwritten,
|
||||
std::ptr::null_mut(),
|
||||
)
|
||||
};
|
||||
|
||||
Ok(host_nwritten as usize)
|
||||
writev(fd_entry.fd_object.raw_handle, &iovs).map_err(|e| host_impl::errno_from_win(e))
|
||||
}
|
||||
|
||||
pub(crate) fn fd_advise(
|
||||
@@ -158,7 +154,70 @@ pub(crate) fn path_open(
|
||||
mut needed_inheriting: host::__wasi_rights_t,
|
||||
fs_flags: host::__wasi_fdflags_t,
|
||||
) -> Result<FdEntry, host::__wasi_errno_t> {
|
||||
unimplemented!("path_open")
|
||||
use winx::file::{AccessRight, CreationDisposition, FlagsAndAttributes, ShareMode};
|
||||
|
||||
let mut win_rights = AccessRight::READ_CONTROL;
|
||||
if read {
|
||||
win_rights.insert(AccessRight::FILE_GENERIC_READ);
|
||||
}
|
||||
if write {
|
||||
win_rights.insert(AccessRight::FILE_GENERIC_WRITE);
|
||||
}
|
||||
|
||||
// convert open flags
|
||||
let (win_create_disp, mut win_flags_attrs) = host_impl::win_from_oflags(oflags);
|
||||
if win_create_disp == CreationDisposition::CREATE_NEW {
|
||||
needed_base |= host::__WASI_RIGHT_PATH_CREATE_FILE;
|
||||
} else if win_create_disp == CreationDisposition::CREATE_ALWAYS {
|
||||
needed_base |= host::__WASI_RIGHT_PATH_CREATE_FILE;
|
||||
} else if win_create_disp == CreationDisposition::TRUNCATE_EXISTING {
|
||||
needed_base |= host::__WASI_RIGHT_PATH_FILESTAT_SET_SIZE;
|
||||
}
|
||||
|
||||
// convert file descriptor flags
|
||||
let win_fdflags_res = host_impl::win_from_fdflags(fs_flags);
|
||||
win_rights.insert(win_fdflags_res.0);
|
||||
win_flags_attrs.insert(win_fdflags_res.1);
|
||||
if win_rights.contains(AccessRight::SYNCHRONIZE) {
|
||||
needed_inheriting |= host::__WASI_RIGHT_FD_DATASYNC;
|
||||
needed_inheriting |= host::__WASI_RIGHT_FD_SYNC;
|
||||
}
|
||||
|
||||
let (dir, path) = match path_get(
|
||||
ctx,
|
||||
dirfd,
|
||||
dirflags,
|
||||
path,
|
||||
needed_base,
|
||||
needed_inheriting,
|
||||
!win_flags_attrs.contains(FlagsAndAttributes::FILE_FLAG_BACKUP_SEMANTICS),
|
||||
) {
|
||||
Ok((dir, path)) => (dir, path),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
|
||||
let new_handle =
|
||||
match winx::file::openat(dir, &path, win_rights, win_create_disp, win_flags_attrs) {
|
||||
Ok(handle) => handle,
|
||||
Err(e) => return Err(host_impl::errno_from_win(e)),
|
||||
};
|
||||
|
||||
// Determine the type of the new file descriptor and which rights contradict with this type
|
||||
match unsafe { determine_type_rights(new_handle) } {
|
||||
Err(e) => {
|
||||
// if `close` fails, note it but do not override the underlying errno
|
||||
winx::handle::close(new_handle).unwrap_or_else(|e| {
|
||||
dbg!(e);
|
||||
});
|
||||
Err(e)
|
||||
}
|
||||
Ok((_ty, max_base, max_inheriting)) => {
|
||||
let mut fe = unsafe { FdEntry::from_raw_handle(new_handle) };
|
||||
fe.rights_base &= max_base;
|
||||
fe.rights_inheriting &= max_inheriting;
|
||||
Ok(fe)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn fd_readdir(
|
||||
|
||||
139
src/sys/windows/hostcalls_impl/fs_helpers.rs
Normal file
139
src/sys/windows/hostcalls_impl/fs_helpers.rs
Normal file
@@ -0,0 +1,139 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(unused_unsafe)]
|
||||
|
||||
use super::host_impl;
|
||||
use crate::ctx::WasiCtx;
|
||||
use crate::host;
|
||||
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::os::windows::prelude::RawHandle;
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
|
||||
/// Normalizes a path to ensure that the target path is located under the directory provided.
|
||||
pub fn path_get<P: AsRef<OsStr>>(
|
||||
wasi_ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
_dirflags: host::__wasi_lookupflags_t,
|
||||
path: P,
|
||||
needed_base: host::__wasi_rights_t,
|
||||
needed_inheriting: host::__wasi_rights_t,
|
||||
needs_final_component: bool,
|
||||
) -> Result<(RawHandle, OsString), host::__wasi_errno_t> {
|
||||
/// close all the intermediate handles, but make sure not to drop either the original
|
||||
/// dirfd or the one we return (which may be the same dirfd)
|
||||
fn ret_dir_success(dir_stack: &mut Vec<RawHandle>) -> RawHandle {
|
||||
let ret_dir = dir_stack.pop().expect("there is always a dirfd to return");
|
||||
if let Some(dirfds) = dir_stack.get(1..) {
|
||||
for dirfd in dirfds {
|
||||
winx::handle::close(*dirfd).unwrap_or_else(|e| {
|
||||
dbg!(e);
|
||||
});
|
||||
}
|
||||
}
|
||||
ret_dir
|
||||
}
|
||||
|
||||
/// close all file descriptors other than the base directory, and return the errno for
|
||||
/// convenience with `return`
|
||||
fn ret_error(
|
||||
dir_stack: &mut Vec<RawHandle>,
|
||||
errno: host::__wasi_errno_t,
|
||||
) -> Result<(RawHandle, OsString), host::__wasi_errno_t> {
|
||||
if let Some(dirfds) = dir_stack.get(1..) {
|
||||
for dirfd in dirfds {
|
||||
winx::handle::close(*dirfd).unwrap_or_else(|e| {
|
||||
dbg!(e);
|
||||
});
|
||||
}
|
||||
}
|
||||
Err(errno)
|
||||
}
|
||||
|
||||
let dirfe = wasi_ctx.get_fd_entry(dirfd, needed_base, needed_inheriting)?;
|
||||
|
||||
// Stack of directory handles. Index 0 always corresponds with the directory provided
|
||||
// to this function. Entering a directory causes a handle to be pushed, while handling
|
||||
// ".." entries causes an entry to be popped. Index 0 cannot be popped, as this would imply
|
||||
// escaping the base directory.
|
||||
let mut dir_stack = vec![dirfe.fd_object.raw_handle];
|
||||
|
||||
// Stack of paths left to process. This is initially the `path` argument to this function, but
|
||||
// any symlinks we encounter are processed by pushing them on the stack.
|
||||
let mut path_stack = vec![PathBuf::from(path.as_ref())];
|
||||
|
||||
loop {
|
||||
match path_stack.pop() {
|
||||
Some(cur_path) => {
|
||||
// dbg!(&cur_path);
|
||||
let mut components = cur_path.components();
|
||||
let head = match components.next() {
|
||||
None => return ret_error(&mut dir_stack, host::__WASI_ENOENT),
|
||||
Some(p) => p,
|
||||
};
|
||||
let tail = components.as_path();
|
||||
|
||||
if tail.components().next().is_some() {
|
||||
path_stack.push(PathBuf::from(tail));
|
||||
}
|
||||
|
||||
match head {
|
||||
Component::Prefix(_) | Component::RootDir => {
|
||||
// path is absolute!
|
||||
return ret_error(&mut dir_stack, host::__WASI_ENOTCAPABLE);
|
||||
}
|
||||
Component::CurDir => {
|
||||
// "." so skip
|
||||
continue;
|
||||
}
|
||||
Component::ParentDir => {
|
||||
// ".." so pop a dir
|
||||
let dirfd = dir_stack.pop().expect("dir_stack is never empty");
|
||||
|
||||
// we're not allowed to pop past the original directory
|
||||
if dir_stack.is_empty() {
|
||||
return ret_error(&mut dir_stack, host::__WASI_ENOTCAPABLE);
|
||||
} else {
|
||||
winx::handle::close(dirfd).unwrap_or_else(|e| {
|
||||
dbg!(e);
|
||||
});
|
||||
}
|
||||
}
|
||||
Component::Normal(head) => {
|
||||
// should the component be a directory? it should if there is more path left to process, or
|
||||
// if it has a trailing slash and `needs_final_component` is not set
|
||||
if !path_stack.is_empty()
|
||||
|| (Path::new(head).is_dir() && !needs_final_component)
|
||||
{
|
||||
match winx::file::openat(
|
||||
*dir_stack.last().expect("dir_stack is never empty"),
|
||||
head,
|
||||
winx::file::AccessRight::FILE_GENERIC_READ,
|
||||
winx::file::CreationDisposition::OPEN_EXISTING,
|
||||
winx::file::FlagsAndAttributes::FILE_FLAG_BACKUP_SEMANTICS,
|
||||
) {
|
||||
Ok(new_dir) => {
|
||||
dir_stack.push(new_dir);
|
||||
continue;
|
||||
}
|
||||
Err(e) => {
|
||||
return ret_error(&mut dir_stack, host_impl::errno_from_win(e));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// we're done
|
||||
return Ok((ret_dir_success(&mut dir_stack), head.to_os_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
// no further components to process. means we've hit a case like "." or "a/..", or if the
|
||||
// input path has trailing slashes and `needs_final_component` is not set
|
||||
return Ok((
|
||||
ret_dir_success(&mut dir_stack),
|
||||
OsStr::new(".").to_os_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
//! Windows-specific hostcalls that implement
|
||||
//! [WASI](https://github.com/CraneStation/wasmtime-wasi/blob/wasi/docs/WASI-overview.md).
|
||||
mod fs;
|
||||
mod fs_helpers;
|
||||
mod misc;
|
||||
|
||||
use super::fdentry;
|
||||
|
||||
Reference in New Issue
Block a user