* Rename FdEntry to Entry
* Add custom FdSet container for managing fd allocs/deallocs
This commit adds a custom `FdSet` container which is intended
for use in `wasi-common` to track WASI fd allocs/deallocs. The
main aim for this container is to abstract away the current
approach of spawning new handles
```rust
fd = fd.checked_add(1).ok_or(...)?;
```
and to make it possible to reuse unused/reclaimed handles
which currently is not done.
The struct offers 3 methods to manage its functionality:
* `FdSet::new` initialises the internal data structures,
and most notably, it preallocates an `FdSet::BATCH_SIZE`
worth of handles in such a way that we always start popping
from the "smallest" handle (think of it as of reversed stack,
I guess; it's not a binary heap since we don't really care
whether internally the handles are sorted in some way, just that
the "largets" handle is at the bottom. Why will become clear
when describing `allocate` method.)
* `FdSet::allocate` pops the next available handle if one is available.
The tricky bit here is that, if we run out of handles, we preallocate
the next `FdSet::BATCH_SIZE` worth of handles starting from the
latest popped handle (i.e., the "largest" handle). This
works only because we make sure to only ever pop and push already
existing handles from the back, and push _new_ handles (from the
preallocation step) from the front. When we ultimately run out
of _all_ available handles, we then return `None` for the client
to handle in some way (e.g., throwing an error such as `WasiError::EMFILE`
or whatnot).
* `FdSet::deallocate` returns the already allocated handle back to
the pool for further reuse.
When figuring out the internals, I've tried to optimise for both
alloc and dealloc performance, and I believe we've got an amortised
`O(1)~*` performance for both (if my maths is right, and it may very
well not be, so please verify!).
In order to keep `FdSet` fairly generic, I've made sure not to hard-code
it for the current type system generated by `wig` (i.e., `wasi::__wasi_fd_t`
representing WASI handle), but rather, any type which wants to be managed
by `FdSet` needs to conform to `Fd` trait. This trait is quite simple as
it only requires a couple of rudimentary traits (although `std:#️⃣:Hash`
is quite a powerful assumption here!), and a custom method
```rust
Fd::next(&self) -> Option<Self>;
```
which is there to encapsulate creating another handle from the given one.
In the current state of the code, that'd be simply `u32::checked_add(1)`.
When `wiggle` makes it way into the `wasi-common`, I'd imagine it being
similar to
```rust
fn next(&self) -> Option<Self> {
self.0.checked_add(1).map(Self::from)
}
```
Anyhow, I'd be happy to learn your thoughts about this design!
* Fix compilation on other targets
* Rename FdSet to FdPool
* Fix FdPool unit tests
* Skip preallocation step in FdPool
* Replace 'replace' calls with direct assignment
* Reuse FdPool from snapshot1 in snapshot0
* Refactor FdPool::allocate
* Remove entry before deallocating the fd
* Refactor the design to accommodate `u32` as underlying type
This commit refactors the design by ensuring that the underlying
type in `FdPool` which we use to track and represent raw file
descriptors is `u32`. As a result, the structure of `FdPool` is
simplified massively as we no longer need to track the claimed
descriptors; in a way, we trust the caller to return the handle
after it's done with it. In case the caller decides to be clever
and return a handle which was not yet legally allocated, we panic.
This should never be a problem in `wasi-common` unless we hit a
bug.
To make all of this work, `Fd` trait is modified to require two
methods: `as_raw(&self) -> u32` and `from_raw(raw_fd: u32) -> Self`
both of which are used to convert to and from the `FdPool`'s underlying
type `u32`.
146 lines
4.9 KiB
Rust
146 lines
4.9 KiB
Rust
#![allow(non_camel_case_types)]
|
|
use crate::entry::Descriptor;
|
|
use crate::hostcalls_impl::PathGet;
|
|
use crate::wasi::{self, WasiError, WasiResult};
|
|
use std::ffi::{OsStr, OsString};
|
|
use std::fs::File;
|
|
use std::os::windows::ffi::{OsStrExt, OsStringExt};
|
|
use std::path::{Path, PathBuf};
|
|
use winapi::shared::winerror;
|
|
|
|
pub(crate) trait PathGetExt {
|
|
fn concatenate(&self) -> WasiResult<PathBuf>;
|
|
}
|
|
|
|
impl PathGetExt for PathGet {
|
|
fn concatenate(&self) -> WasiResult<PathBuf> {
|
|
match self.dirfd() {
|
|
Descriptor::OsHandle(file) => concatenate(file, Path::new(self.path())),
|
|
Descriptor::VirtualFile(_virt) => {
|
|
panic!("concatenate on a virtual base");
|
|
}
|
|
Descriptor::Stdin | Descriptor::Stdout | Descriptor::Stderr => {
|
|
unreachable!("streams do not have paths and should not be accessible via PathGet");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn path_open_rights(
|
|
rights_base: wasi::__wasi_rights_t,
|
|
rights_inheriting: wasi::__wasi_rights_t,
|
|
oflags: wasi::__wasi_oflags_t,
|
|
fdflags: wasi::__wasi_fdflags_t,
|
|
) -> (wasi::__wasi_rights_t, wasi::__wasi_rights_t) {
|
|
// which rights are needed on the dirfd?
|
|
let mut needed_base = wasi::__WASI_RIGHTS_PATH_OPEN;
|
|
let mut needed_inheriting = rights_base | rights_inheriting;
|
|
|
|
// convert open flags
|
|
if oflags & wasi::__WASI_OFLAGS_CREAT != 0 {
|
|
needed_base |= wasi::__WASI_RIGHTS_PATH_CREATE_FILE;
|
|
} else if oflags & wasi::__WASI_OFLAGS_TRUNC != 0 {
|
|
needed_base |= wasi::__WASI_RIGHTS_PATH_FILESTAT_SET_SIZE;
|
|
}
|
|
|
|
// convert file descriptor flags
|
|
if fdflags & wasi::__WASI_FDFLAGS_DSYNC != 0
|
|
|| fdflags & wasi::__WASI_FDFLAGS_RSYNC != 0
|
|
|| fdflags & wasi::__WASI_FDFLAGS_SYNC != 0
|
|
{
|
|
needed_inheriting |= wasi::__WASI_RIGHTS_FD_DATASYNC;
|
|
needed_inheriting |= wasi::__WASI_RIGHTS_FD_SYNC;
|
|
}
|
|
|
|
(needed_base, needed_inheriting)
|
|
}
|
|
|
|
pub(crate) fn openat(dirfd: &File, path: &str) -> WasiResult<File> {
|
|
use std::fs::OpenOptions;
|
|
use std::os::windows::fs::OpenOptionsExt;
|
|
use winx::file::Flags;
|
|
|
|
let path = concatenate(dirfd, Path::new(path))?;
|
|
let err = match OpenOptions::new()
|
|
.read(true)
|
|
.custom_flags(Flags::FILE_FLAG_BACKUP_SEMANTICS.bits())
|
|
.open(&path)
|
|
{
|
|
Ok(file) => return Ok(file),
|
|
Err(e) => e,
|
|
};
|
|
if let Some(code) = err.raw_os_error() {
|
|
log::debug!("openat error={:?}", code);
|
|
if code as u32 == winerror::ERROR_INVALID_NAME {
|
|
return Err(WasiError::ENOTDIR);
|
|
}
|
|
}
|
|
Err(err.into())
|
|
}
|
|
|
|
pub(crate) fn readlinkat(dirfd: &File, s_path: &str) -> WasiResult<String> {
|
|
use winx::file::get_file_path;
|
|
|
|
let path = concatenate(dirfd, Path::new(s_path))?;
|
|
let err = match path.read_link() {
|
|
Ok(target_path) => {
|
|
// since on Windows we are effectively emulating 'at' syscalls
|
|
// we need to strip the prefix from the absolute path
|
|
// as otherwise we will error out since WASI is not capable
|
|
// of dealing with absolute paths
|
|
let dir_path = get_file_path(dirfd)?;
|
|
let dir_path = PathBuf::from(strip_extended_prefix(dir_path));
|
|
let target_path = target_path
|
|
.strip_prefix(dir_path)
|
|
.map_err(|_| WasiError::ENOTCAPABLE)?;
|
|
let target_path = target_path.to_str().ok_or(WasiError::EILSEQ)?;
|
|
return Ok(target_path.to_owned());
|
|
}
|
|
Err(e) => e,
|
|
};
|
|
if let Some(code) = err.raw_os_error() {
|
|
log::debug!("readlinkat error={:?}", code);
|
|
if code as u32 == winerror::ERROR_INVALID_NAME {
|
|
if s_path.ends_with('/') {
|
|
// strip "/" and check if exists
|
|
let path = concatenate(dirfd, Path::new(s_path.trim_end_matches('/')))?;
|
|
if path.exists() && !path.is_dir() {
|
|
return Err(WasiError::ENOTDIR);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Err(err.into())
|
|
}
|
|
|
|
pub(crate) fn strip_extended_prefix<P: AsRef<OsStr>>(path: P) -> OsString {
|
|
let path: Vec<u16> = path.as_ref().encode_wide().collect();
|
|
if &[92, 92, 63, 92] == &path[0..4] {
|
|
OsString::from_wide(&path[4..])
|
|
} else {
|
|
OsString::from_wide(&path)
|
|
}
|
|
}
|
|
|
|
pub(crate) fn concatenate<P: AsRef<Path>>(file: &File, path: P) -> WasiResult<PathBuf> {
|
|
use winx::file::get_file_path;
|
|
|
|
// WASI is not able to deal with absolute paths
|
|
// so error out if absolute
|
|
if path.as_ref().is_absolute() {
|
|
return Err(WasiError::ENOTCAPABLE);
|
|
}
|
|
|
|
let dir_path = get_file_path(file)?;
|
|
// concatenate paths
|
|
let mut out_path = PathBuf::from(dir_path);
|
|
out_path.push(path.as_ref());
|
|
// strip extended prefix; otherwise we will error out on any relative
|
|
// components with `out_path`
|
|
let out_path = PathBuf::from(strip_extended_prefix(out_path));
|
|
|
|
log::debug!("out_path={:?}", out_path);
|
|
|
|
Ok(out_path)
|
|
}
|