Introduce strongly-typed system primitives (#1561)

* Introduce strongly-typed system primitives

This commit does a lot of reshuffling and even some more. It introduces
strongly-typed system primitives which are: `OsFile`, `OsDir`, `Stdio`,
and `OsOther`. Those primitives are separate structs now, each implementing
a subset of `Handle` methods, rather than all being an enumeration of some
supertype such as `OsHandle`. To summarise the structs:

* `OsFile` represents a regular file, and implements fd-ops
  of `Handle` trait
* `OsDir` represents a directory, and primarily implements path-ops, plus
  `readdir` and some common fd-ops such as `fdstat`, etc.
* `Stdio` represents a stdio handle, and implements a subset of fd-ops
  such as `fdstat` _and_ `read_` and `write_vectored` calls
* `OsOther` currently represents anything else and implements a set similar
  to that implemented by `Stdio`

This commit is effectively an experiment and an excercise into better
understanding what's going on for each OS resource/type under-the-hood.
It's meant to give us some intuition in order to move on with the idea
of having strongly-typed handles in WASI both in the syscall impl as well
as at the libc level.

Some more minor changes include making `OsHandle` represent an OS-specific
wrapper for a raw OS handle (Unix fd or Windows handle). Also, since `OsDir`
is tricky across OSes, we also have a supertype of `OsHandle` called
`OsDirHandle` which may store a `DIR*` stream pointer (mainly BSD). Last but not
least, the `Filetype` and `Rights` are now computed when the resource is created,
rather than every time we call `Handle::get_file_type` and `Handle::get_rights`.
Finally, in order to facilitate the latter, I've converted `EntryRights` into
`HandleRights` and pushed them into each `Handle` implementor.

* Do not adjust rights on Stdio

* Clean up testing for TTY and escaping writes

* Implement AsFile for dyn Handle

This cleans up a lot of repeating boilerplate code todo with
dynamic dispatch.

* Delegate definition of OsDir to OS-specific modules

Delegates defining `OsDir` struct to OS-specific modules (BSD, Linux,
Emscripten, Windows). This way, `OsDir` can safely re-use `OsHandle`
for raw OS handle storage, and can store some aux data such as an
initialized stream ptr in case of BSD. As a result, we can safely
get rid of `OsDirHandle` which IMHO was causing unnecessary noise and
overcomplicating the design. On the other hand, delegating definition
of `OsDir` to OS-specific modules isn't super clean in and of itself
either. Perhaps there's a better way of handling this?

* Check if filetype of OS handle matches WASI filetype when creating

It seems prudent to check if the passed in `File` instance is of
type matching that of the requested WASI filetype. In other words,
we'd like to avoid situations where `OsFile` is created from a
pipe.

* Make AsFile fallible

Return `EBADF` in `AsFile` in case a `Handle` cannot be made into
a `std::fs::File`.

* Remove unnecessary as_file conversion

* Remove unnecessary check for TTY for Stdio handle type

* Fix incorrect stdio ctors on Unix

* Split Stdio into three separate types: Stdin, Stdout, Stderr

* Rename PendingEntry::File to PendingEntry::OsHandle to avoid confusion

* Rename OsHandle to RawOsHandle

Also, since `RawOsHandle` on *nix doesn't need interior mutability
wrt the inner raw file descriptor, we can safely swap the `RawFd`
for `File` instance.

* Add docs explaining what OsOther is

* Allow for stdio to be non-character-device (e.g., piped)

* Return error on bad preopen rather than panic
This commit is contained in:
Jakub Konka
2020-05-08 01:00:14 +02:00
committed by GitHub
parent 528d3c1355
commit cbf7cbfa39
39 changed files with 1643 additions and 1073 deletions

View File

@@ -1,5 +1,4 @@
use crate::entry::EntryRights;
use crate::handle::Handle;
use crate::handle::{Handle, HandleRights};
use crate::wasi::{self, types, Errno, Result, RightsExt};
use log::trace;
use std::any::Any;
@@ -134,6 +133,7 @@ impl VecFileContents {
/// a filesystem wherein a file descriptor is one view into a possibly-shared underlying collection
/// of data and permissions on a filesystem.
pub struct InMemoryFile {
rights: Cell<HandleRights>,
cursor: Cell<types::Filesize>,
parent: Rc<RefCell<Option<Box<dyn Handle>>>>,
fd_flags: Cell<types::Fdflags>,
@@ -142,16 +142,17 @@ pub struct InMemoryFile {
impl InMemoryFile {
pub fn memory_backed() -> Self {
Self {
cursor: Cell::new(0),
parent: Rc::new(RefCell::new(None)),
fd_flags: Cell::new(types::Fdflags::empty()),
data: Rc::new(RefCell::new(Box::new(VecFileContents::new()))),
}
Self::new(Box::new(VecFileContents::new()))
}
pub fn new(contents: Box<dyn FileContents>) -> Self {
let rights = HandleRights::new(
types::Rights::regular_file_base(),
types::Rights::regular_file_inheriting(),
);
let rights = Cell::new(rights);
Self {
rights,
cursor: Cell::new(0),
fd_flags: Cell::new(types::Fdflags::empty()),
parent: Rc::new(RefCell::new(None)),
@@ -172,20 +173,21 @@ impl Handle for InMemoryFile {
}
fn try_clone(&self) -> io::Result<Box<dyn Handle>> {
Ok(Box::new(Self {
rights: self.rights.clone(),
cursor: Cell::new(0),
fd_flags: self.fd_flags.clone(),
parent: Rc::clone(&self.parent),
data: Rc::clone(&self.data),
}))
}
fn get_file_type(&self) -> io::Result<types::Filetype> {
Ok(types::Filetype::RegularFile)
fn get_file_type(&self) -> types::Filetype {
types::Filetype::RegularFile
}
fn get_rights(&self) -> io::Result<EntryRights> {
Ok(EntryRights::new(
types::Rights::regular_file_base(),
types::Rights::regular_file_inheriting(),
))
fn get_rights(&self) -> HandleRights {
self.rights.get()
}
fn set_rights(&self, rights: HandleRights) {
self.rights.set(rights)
}
// FdOps
fn advise(
@@ -227,7 +229,7 @@ impl Handle for InMemoryFile {
atim: 0,
ctim: 0,
mtim: 0,
filetype: self.get_file_type()?,
filetype: self.get_file_type(),
};
Ok(stat)
}
@@ -280,11 +282,7 @@ impl Handle for InMemoryFile {
Ok(self.cursor.get())
}
fn write_vectored(&self, iovs: &[io::IoSlice], isatty: bool) -> Result<usize> {
if isatty {
unimplemented!("writes to virtual tty");
}
fn write_vectored(&self, iovs: &[io::IoSlice]) -> Result<usize> {
trace!("write_vectored(iovs={:?})", iovs);
let mut data = self.data.borrow_mut();
@@ -402,6 +400,7 @@ impl Handle for InMemoryFile {
/// A clonable read/write directory.
pub struct VirtualDir {
rights: Cell<HandleRights>,
writable: bool,
// All copies of this `VirtualDir` must share `parent`, and changes in one copy's `parent`
// must be reflected in all handles, so they share `Rc` of an underlying `parent`.
@@ -411,7 +410,13 @@ pub struct VirtualDir {
impl VirtualDir {
pub fn new(writable: bool) -> Self {
let rights = HandleRights::new(
types::Rights::directory_base(),
types::Rights::directory_inheriting(),
);
let rights = Cell::new(rights);
Self {
rights,
writable,
parent: Rc::new(RefCell::new(None)),
entries: Rc::new(RefCell::new(HashMap::new())),
@@ -468,19 +473,20 @@ impl Handle for VirtualDir {
}
fn try_clone(&self) -> io::Result<Box<dyn Handle>> {
Ok(Box::new(Self {
rights: self.rights.clone(),
writable: self.writable,
parent: Rc::clone(&self.parent),
entries: Rc::clone(&self.entries),
}))
}
fn get_file_type(&self) -> io::Result<types::Filetype> {
Ok(types::Filetype::Directory)
fn get_file_type(&self) -> types::Filetype {
types::Filetype::Directory
}
fn get_rights(&self) -> io::Result<EntryRights> {
Ok(EntryRights::new(
types::Rights::directory_base(),
types::Rights::directory_inheriting(),
))
fn get_rights(&self) -> HandleRights {
self.rights.get()
}
fn set_rights(&self, rights: HandleRights) {
self.rights.set(rights)
}
// FdOps
fn filestat_get(&self) -> Result<types::Filestat> {
@@ -492,7 +498,7 @@ impl Handle for VirtualDir {
atim: 0,
ctim: 0,
mtim: 0,
filetype: self.get_file_type()?,
filetype: self.get_file_type(),
};
Ok(stat)
}
@@ -555,7 +561,7 @@ impl Handle for VirtualDir {
let dirent = || -> Result<types::Dirent> {
let dirent = types::Dirent {
d_namlen: name.len().try_into()?,
d_type: file.get_file_type()?,
d_type: file.get_file_type(),
d_ino: 0,
d_next: self.start as u64,
};
@@ -639,7 +645,7 @@ impl Handle for VirtualDir {
}
if oflags.contains(&types::Oflags::DIRECTORY)
&& e.get().get_file_type()? != types::Filetype::Directory
&& e.get().get_file_type() != types::Filetype::Directory
{
log::trace!(
"VirtualDir::openat was passed oflags DIRECTORY, but {:?} is a file.",
@@ -683,7 +689,7 @@ impl Handle for VirtualDir {
match entries.entry(Path::new(trimmed_path).to_path_buf()) {
Entry::Occupied(e) => {
// first, does this name a directory?
if e.get().get_file_type()? != types::Filetype::Directory {
if e.get().get_file_type() != types::Filetype::Directory {
return Err(Errno::Notdir);
}
@@ -731,7 +737,7 @@ impl Handle for VirtualDir {
match entries.entry(Path::new(trimmed_path).to_path_buf()) {
Entry::Occupied(e) => {
// Directories must be removed through `remove_directory`, not `unlink_file`.
if e.get().get_file_type()? == types::Filetype::Directory {
if e.get().get_file_type() == types::Filetype::Directory {
return Err(Errno::Isdir);
}