Port wasi-common from unsafe-io to io-lifetimes (#3049)
* Port wasi-common to io-lifetimes. This ports wasi-common from unsafe-io to io-lifetimes. Ambient authority is now indicated via calls to `ambient_authority()` from the ambient-authority crate, rather than using `unsafe` blocks. The `GetSetFdFlags::set_fd_flags` function is now split into two phases, to simplify lifetimes in implementations which need to close and re-open the underlying file. * Use posish for errno values instead of libc. This eliminates one of the few remaining direct libc dependencies. * Port to posish::io::poll. Use posish::io::poll instead of calling libc directly. This factors out more code from Wasmtime, and eliminates the need to manipulate raw file descriptors directly. And, this eliminates the last remaining direct dependency on libc in wasi-common. * Port wasi-c-api to io-lifetimes. * Update to posish 0.16.0. * Embeded NULs in filenames now get `EINVAL` instead of `EILSEQ`. * Accept either `EILSEQ` or `EINVAL` for embedded NULs. * Bump the nightly toolchain to 2021-07-12. This fixes build errors on the semver crate, which as of this writing builds with latest nightly and stable but not 2021-04-11, the old pinned version. * Have cap-std-sync re-export ambient_authority so that users get the same version.
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
use cap_std::time::{Duration, Instant, SystemTime};
|
||||
use cap_std::{ambient_authority, AmbientAuthority};
|
||||
use cap_time_ext::{MonotonicClockExt, SystemClockExt};
|
||||
use wasi_common::clocks::{WasiClocks, WasiMonotonicClock, WasiSystemClock};
|
||||
|
||||
pub struct SystemClock(cap_std::time::SystemClock);
|
||||
|
||||
impl SystemClock {
|
||||
pub unsafe fn new() -> Self {
|
||||
SystemClock(cap_std::time::SystemClock::new())
|
||||
pub fn new(ambient_authority: AmbientAuthority) -> Self {
|
||||
SystemClock(cap_std::time::SystemClock::new(ambient_authority))
|
||||
}
|
||||
}
|
||||
impl WasiSystemClock for SystemClock {
|
||||
@@ -20,8 +21,8 @@ impl WasiSystemClock for SystemClock {
|
||||
|
||||
pub struct MonotonicClock(cap_std::time::MonotonicClock);
|
||||
impl MonotonicClock {
|
||||
pub unsafe fn new() -> Self {
|
||||
MonotonicClock(cap_std::time::MonotonicClock::new())
|
||||
pub fn new(ambient_authority: AmbientAuthority) -> Self {
|
||||
MonotonicClock(cap_std::time::MonotonicClock::new(ambient_authority))
|
||||
}
|
||||
}
|
||||
impl WasiMonotonicClock for MonotonicClock {
|
||||
@@ -34,8 +35,8 @@ impl WasiMonotonicClock for MonotonicClock {
|
||||
}
|
||||
|
||||
pub fn clocks_ctx() -> WasiClocks {
|
||||
let system = Box::new(unsafe { SystemClock::new() });
|
||||
let monotonic = unsafe { cap_std::time::MonotonicClock::new() };
|
||||
let system = Box::new(SystemClock::new(ambient_authority()));
|
||||
let monotonic = cap_std::time::MonotonicClock::new(ambient_authority());
|
||||
let creation_time = monotonic.now();
|
||||
let monotonic = Box::new(MonotonicClock(monotonic));
|
||||
WasiClocks {
|
||||
|
||||
@@ -74,7 +74,8 @@ impl Dir {
|
||||
let mut f = self.0.open_with(Path::new(path), &opts)?;
|
||||
// NONBLOCK does not have an OpenOption either, but we can patch that on with set_fd_flags:
|
||||
if fdflags.contains(wasi_common::file::FdFlags::NONBLOCK) {
|
||||
f.set_fd_flags(system_interface::fs::FdFlags::NONBLOCK)?;
|
||||
let set_fd_flags = f.new_set_fd_flags(system_interface::fs::FdFlags::NONBLOCK)?;
|
||||
f.set_fd_flags(set_fd_flags)?;
|
||||
}
|
||||
Ok(File::from_cap_std(f))
|
||||
}
|
||||
@@ -311,13 +312,14 @@ fn convert_systimespec(t: Option<wasi_common::SystemTimeSpec>) -> Option<SystemT
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::Dir;
|
||||
use cap_std::ambient_authority;
|
||||
#[test]
|
||||
fn scratch_dir() {
|
||||
let tempdir = tempfile::Builder::new()
|
||||
.prefix("cap-std-sync")
|
||||
.tempdir()
|
||||
.expect("create temporary dir");
|
||||
let preopen_dir = unsafe { cap_std::fs::Dir::open_ambient_dir(tempdir.path()) }
|
||||
let preopen_dir = cap_std::fs::Dir::open_ambient_dir(tempdir.path(), ambient_authority())
|
||||
.expect("open ambient temporary dir");
|
||||
let preopen_dir = Dir::from_cap_std(preopen_dir);
|
||||
run(wasi_common::WasiDir::open_dir(&preopen_dir, false, "."))
|
||||
@@ -347,7 +349,7 @@ mod test {
|
||||
.prefix("cap-std-sync")
|
||||
.tempdir()
|
||||
.expect("create temporary dir");
|
||||
let preopen_dir = unsafe { cap_std::fs::Dir::open_ambient_dir(tempdir.path()) }
|
||||
let preopen_dir = cap_std::fs::Dir::open_ambient_dir(tempdir.path(), ambient_authority())
|
||||
.expect("open ambient temporary dir");
|
||||
let preopen_dir = Dir::from_cap_std(preopen_dir);
|
||||
|
||||
|
||||
@@ -49,7 +49,9 @@ impl WasiFile for File {
|
||||
) {
|
||||
return Err(Error::invalid_argument().context("cannot set DSYNC, SYNC, or RSYNC flag"));
|
||||
}
|
||||
Ok(self.0.set_fd_flags(to_sysif_fdflags(fdflags))?)
|
||||
let set_fd_flags = self.0.new_set_fd_flags(to_sysif_fdflags(fdflags))?;
|
||||
self.0.set_fd_flags(set_fd_flags)?;
|
||||
Ok(())
|
||||
}
|
||||
async fn get_filestat(&self) -> Result<Filestat, Error> {
|
||||
let meta = self.0.metadata()?;
|
||||
@@ -151,20 +153,20 @@ pub fn filetype_from(ft: &cap_std::fs::FileType) -> FileType {
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
use std::os::windows::io::{AsRawHandle, RawHandle};
|
||||
use io_lifetimes::{AsHandle, BorrowedHandle};
|
||||
#[cfg(windows)]
|
||||
impl AsRawHandle for File {
|
||||
fn as_raw_handle(&self) -> RawHandle {
|
||||
self.0.as_raw_handle()
|
||||
impl AsHandle for File {
|
||||
fn as_handle(&self) -> BorrowedHandle<'_> {
|
||||
self.0.as_handle()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
use std::os::unix::io::{AsRawFd, RawFd};
|
||||
use io_lifetimes::{AsFd, BorrowedFd};
|
||||
#[cfg(unix)]
|
||||
impl AsRawFd for File {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
self.0.as_raw_fd()
|
||||
impl AsFd for File {
|
||||
fn as_fd(&self) -> BorrowedFd<'_> {
|
||||
self.0.as_fd()
|
||||
}
|
||||
}
|
||||
pub fn convert_systimespec(t: Option<wasi_common::SystemTimeSpec>) -> Option<SystemTimeSpec> {
|
||||
|
||||
@@ -37,6 +37,7 @@ pub mod file;
|
||||
pub mod sched;
|
||||
pub mod stdio;
|
||||
|
||||
pub use cap_std::ambient_authority;
|
||||
pub use cap_std::fs::Dir;
|
||||
pub use clocks::clocks_ctx;
|
||||
pub use sched::sched_ctx;
|
||||
@@ -123,5 +124,5 @@ impl WasiCtxBuilder {
|
||||
}
|
||||
|
||||
pub fn random_ctx() -> Box<dyn RngCore + Send + Sync> {
|
||||
Box::new(unsafe { cap_rand::rngs::OsRng::default() })
|
||||
Box::new(cap_rand::rngs::OsRng::default(ambient_authority()))
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use cap_std::time::Duration;
|
||||
use io_lifetimes::{AsFd, BorrowedFd};
|
||||
use posish::io::{PollFd, PollFdVec, PollFlags};
|
||||
use std::convert::TryInto;
|
||||
use std::os::unix::io::{AsRawFd, RawFd};
|
||||
use wasi_common::{
|
||||
file::WasiFile,
|
||||
sched::{
|
||||
@@ -10,27 +11,25 @@ use wasi_common::{
|
||||
Error, ErrorExt,
|
||||
};
|
||||
|
||||
use poll::{PollFd, PollFlags};
|
||||
|
||||
pub async fn poll_oneoff<'a>(poll: &mut Poll<'a>) -> Result<(), Error> {
|
||||
if poll.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let mut pollfds = Vec::new();
|
||||
let mut pollfds = PollFdVec::new();
|
||||
for s in poll.rw_subscriptions() {
|
||||
match s {
|
||||
Subscription::Read(f) => {
|
||||
let raw_fd = wasi_file_raw_fd(f.file).ok_or(
|
||||
let fd = wasi_file_fd(f.file).ok_or(
|
||||
Error::invalid_argument().context("read subscription fd downcast failed"),
|
||||
)?;
|
||||
pollfds.push(unsafe { PollFd::new(raw_fd, PollFlags::POLLIN) });
|
||||
pollfds.push(PollFd::from_borrowed_fd(fd, PollFlags::IN));
|
||||
}
|
||||
|
||||
Subscription::Write(f) => {
|
||||
let raw_fd = wasi_file_raw_fd(f.file).ok_or(
|
||||
let fd = wasi_file_fd(f.file).ok_or(
|
||||
Error::invalid_argument().context("write subscription fd downcast failed"),
|
||||
)?;
|
||||
pollfds.push(unsafe { PollFd::new(raw_fd, PollFlags::POLLOUT) });
|
||||
pollfds.push(PollFd::from_borrowed_fd(fd, PollFlags::OUT));
|
||||
}
|
||||
Subscription::MonotonicClock { .. } => unreachable!(),
|
||||
}
|
||||
@@ -43,46 +42,39 @@ pub async fn poll_oneoff<'a>(poll: &mut Poll<'a>) -> Result<(), Error> {
|
||||
.try_into()
|
||||
.map_err(|_| Error::overflow().context("poll timeout"))?
|
||||
} else {
|
||||
libc::c_int::max_value()
|
||||
std::os::raw::c_int::max_value()
|
||||
};
|
||||
tracing::debug!(
|
||||
poll_timeout = tracing::field::debug(poll_timeout),
|
||||
poll_fds = tracing::field::debug(&pollfds),
|
||||
"poll"
|
||||
);
|
||||
match poll::poll(&mut pollfds, poll_timeout) {
|
||||
match pollfds.poll(poll_timeout) {
|
||||
Ok(ready) => break ready,
|
||||
Err(_) => {
|
||||
let last_err = std::io::Error::last_os_error();
|
||||
if last_err.raw_os_error().unwrap() == libc::EINTR {
|
||||
continue;
|
||||
} else {
|
||||
return Err(last_err.into());
|
||||
}
|
||||
}
|
||||
Err(posish::io::Error::INTR) => continue,
|
||||
Err(err) => return Err(err.into()),
|
||||
}
|
||||
};
|
||||
if ready > 0 {
|
||||
for (rwsub, pollfd) in poll.rw_subscriptions().zip(pollfds.into_iter()) {
|
||||
if let Some(revents) = pollfd.revents() {
|
||||
let (nbytes, rwsub) = match rwsub {
|
||||
Subscription::Read(sub) => {
|
||||
let ready = sub.file.num_ready_bytes().await?;
|
||||
(std::cmp::max(ready, 1), sub)
|
||||
}
|
||||
Subscription::Write(sub) => (0, sub),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
if revents.contains(PollFlags::POLLNVAL) {
|
||||
rwsub.error(Error::badf());
|
||||
} else if revents.contains(PollFlags::POLLERR) {
|
||||
rwsub.error(Error::io());
|
||||
} else if revents.contains(PollFlags::POLLHUP) {
|
||||
rwsub.complete(nbytes, RwEventFlags::HANGUP);
|
||||
} else {
|
||||
rwsub.complete(nbytes, RwEventFlags::empty());
|
||||
};
|
||||
}
|
||||
let revents = pollfd.revents();
|
||||
let (nbytes, rwsub) = match rwsub {
|
||||
Subscription::Read(sub) => {
|
||||
let ready = sub.file.num_ready_bytes().await?;
|
||||
(std::cmp::max(ready, 1), sub)
|
||||
}
|
||||
Subscription::Write(sub) => (0, sub),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
if revents.contains(PollFlags::NVAL) {
|
||||
rwsub.error(Error::badf());
|
||||
} else if revents.contains(PollFlags::ERR) {
|
||||
rwsub.error(Error::io());
|
||||
} else if revents.contains(PollFlags::HUP) {
|
||||
rwsub.complete(nbytes, RwEventFlags::HANGUP);
|
||||
} else {
|
||||
rwsub.complete(nbytes, RwEventFlags::empty());
|
||||
};
|
||||
}
|
||||
} else {
|
||||
poll.earliest_clock_deadline()
|
||||
@@ -94,81 +86,17 @@ pub async fn poll_oneoff<'a>(poll: &mut Poll<'a>) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn wasi_file_raw_fd(f: &dyn WasiFile) -> Option<RawFd> {
|
||||
fn wasi_file_fd(f: &dyn WasiFile) -> Option<BorrowedFd<'_>> {
|
||||
let a = f.as_any();
|
||||
if a.is::<crate::file::File>() {
|
||||
Some(a.downcast_ref::<crate::file::File>().unwrap().as_raw_fd())
|
||||
Some(a.downcast_ref::<crate::file::File>().unwrap().as_fd())
|
||||
} else if a.is::<crate::stdio::Stdin>() {
|
||||
Some(a.downcast_ref::<crate::stdio::Stdin>().unwrap().as_raw_fd())
|
||||
Some(a.downcast_ref::<crate::stdio::Stdin>().unwrap().as_fd())
|
||||
} else if a.is::<crate::stdio::Stdout>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::stdio::Stdout>()
|
||||
.unwrap()
|
||||
.as_raw_fd(),
|
||||
)
|
||||
Some(a.downcast_ref::<crate::stdio::Stdout>().unwrap().as_fd())
|
||||
} else if a.is::<crate::stdio::Stderr>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::stdio::Stderr>()
|
||||
.unwrap()
|
||||
.as_raw_fd(),
|
||||
)
|
||||
Some(a.downcast_ref::<crate::stdio::Stderr>().unwrap().as_fd())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
mod poll {
|
||||
use bitflags::bitflags;
|
||||
use std::convert::TryInto;
|
||||
use std::os::unix::io::RawFd;
|
||||
|
||||
bitflags! {
|
||||
pub struct PollFlags: libc::c_short {
|
||||
const POLLIN = libc::POLLIN;
|
||||
const POLLPRI = libc::POLLPRI;
|
||||
const POLLOUT = libc::POLLOUT;
|
||||
const POLLRDNORM = libc::POLLRDNORM;
|
||||
const POLLWRNORM = libc::POLLWRNORM;
|
||||
const POLLRDBAND = libc::POLLRDBAND;
|
||||
const POLLWRBAND = libc::POLLWRBAND;
|
||||
const POLLERR = libc::POLLERR;
|
||||
const POLLHUP = libc::POLLHUP;
|
||||
const POLLNVAL = libc::POLLNVAL;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
||||
#[repr(C)]
|
||||
pub struct PollFd(libc::pollfd);
|
||||
|
||||
impl PollFd {
|
||||
pub unsafe fn new(fd: RawFd, events: PollFlags) -> Self {
|
||||
Self(libc::pollfd {
|
||||
fd,
|
||||
events: events.bits(),
|
||||
revents: PollFlags::empty().bits(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn revents(self) -> Option<PollFlags> {
|
||||
PollFlags::from_bits(self.0.revents)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn poll(fds: &mut [PollFd], timeout: libc::c_int) -> Result<usize, std::io::Error> {
|
||||
let nready = unsafe {
|
||||
libc::poll(
|
||||
fds.as_mut_ptr() as *mut libc::pollfd,
|
||||
fds.len() as libc::nfds_t,
|
||||
timeout,
|
||||
)
|
||||
};
|
||||
if nready == -1 {
|
||||
Err(std::io::Error::last_os_error())
|
||||
} else {
|
||||
// When poll doesn't fail, its return value is a non-negative int, which will
|
||||
// always be convertable to usize, so we can unwrap() here.
|
||||
Ok(nready.try_into().unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
// taken the time to improve it. See bug #2880.
|
||||
|
||||
use anyhow::Context;
|
||||
use io_lifetimes::AsHandle;
|
||||
use std::ops::Deref;
|
||||
use std::os::windows::io::{AsRawHandle, RawHandle};
|
||||
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender, TryRecvError};
|
||||
@@ -145,24 +146,28 @@ pub fn wasi_file_raw_handle(f: &dyn WasiFile) -> Option<RawHandle> {
|
||||
Some(
|
||||
a.downcast_ref::<crate::file::File>()
|
||||
.unwrap()
|
||||
.as_handle()
|
||||
.as_raw_handle(),
|
||||
)
|
||||
} else if a.is::<crate::stdio::Stdin>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::stdio::Stdin>()
|
||||
.unwrap()
|
||||
.as_handle()
|
||||
.as_raw_handle(),
|
||||
)
|
||||
} else if a.is::<crate::stdio::Stdout>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::stdio::Stdout>()
|
||||
.unwrap()
|
||||
.as_handle()
|
||||
.as_raw_handle(),
|
||||
)
|
||||
} else if a.is::<crate::stdio::Stderr>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::stdio::Stderr>()
|
||||
.unwrap()
|
||||
.as_handle()
|
||||
.as_raw_handle(),
|
||||
)
|
||||
} else {
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
use crate::file::convert_systimespec;
|
||||
use fs_set_times::SetTimes;
|
||||
use io_lifetimes::AsFilelike;
|
||||
use std::any::Any;
|
||||
use std::convert::TryInto;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::io::{Read, Write};
|
||||
use system_interface::io::ReadReady;
|
||||
|
||||
#[cfg(unix)]
|
||||
use std::os::unix::io::{AsRawFd, RawFd};
|
||||
use io_lifetimes::{AsFd, BorrowedFd};
|
||||
#[cfg(windows)]
|
||||
use std::os::windows::io::{AsRawHandle, RawHandle};
|
||||
use unsafe_io::AsUnsafeFile;
|
||||
use io_lifetimes::{AsHandle, BorrowedHandle};
|
||||
use wasi_common::{
|
||||
file::{Advice, FdFlags, FileType, Filestat, WasiFile},
|
||||
Error, ErrorExt,
|
||||
@@ -43,7 +44,7 @@ impl WasiFile for Stdin {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn get_filestat(&self) -> Result<Filestat, Error> {
|
||||
let meta = self.0.as_file_view().metadata()?;
|
||||
let meta = self.0.as_filelike_view::<File>().metadata()?;
|
||||
Ok(Filestat {
|
||||
device_id: 0,
|
||||
inode: 0,
|
||||
@@ -65,7 +66,7 @@ impl WasiFile for Stdin {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn read_vectored<'a>(&self, bufs: &mut [io::IoSliceMut<'a>]) -> Result<u64, Error> {
|
||||
let n = self.0.as_file_view().read_vectored(bufs)?;
|
||||
let n = self.0.as_filelike_view::<File>().read_vectored(bufs)?;
|
||||
Ok(n.try_into().map_err(|_| Error::range())?)
|
||||
}
|
||||
async fn read_vectored_at<'a>(
|
||||
@@ -111,15 +112,15 @@ impl WasiFile for Stdin {
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
impl AsRawHandle for Stdin {
|
||||
fn as_raw_handle(&self) -> RawHandle {
|
||||
self.0.as_raw_handle()
|
||||
impl AsHandle for Stdin {
|
||||
fn as_handle(&self) -> BorrowedHandle<'_> {
|
||||
self.0.as_handle()
|
||||
}
|
||||
}
|
||||
#[cfg(unix)]
|
||||
impl AsRawFd for Stdin {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
self.0.as_raw_fd()
|
||||
impl AsFd for Stdin {
|
||||
fn as_fd(&self) -> BorrowedFd<'_> {
|
||||
self.0.as_fd()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,7 +147,7 @@ macro_rules! wasi_file_write_impl {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn get_filestat(&self) -> Result<Filestat, Error> {
|
||||
let meta = self.0.as_file_view().metadata()?;
|
||||
let meta = self.0.as_filelike_view::<File>().metadata()?;
|
||||
Ok(Filestat {
|
||||
device_id: 0,
|
||||
inode: 0,
|
||||
@@ -181,7 +182,7 @@ macro_rules! wasi_file_write_impl {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn write_vectored<'a>(&self, bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> {
|
||||
let n = self.0.as_file_view().write_vectored(bufs)?;
|
||||
let n = self.0.as_filelike_view::<File>().write_vectored(bufs)?;
|
||||
Ok(n.try_into().map_err(|c| Error::range().context(c))?)
|
||||
}
|
||||
async fn write_vectored_at<'a>(
|
||||
@@ -217,15 +218,15 @@ macro_rules! wasi_file_write_impl {
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
impl AsRawHandle for $ty {
|
||||
fn as_raw_handle(&self) -> RawHandle {
|
||||
self.0.as_raw_handle()
|
||||
impl AsHandle for $ty {
|
||||
fn as_handle(&self) -> BorrowedHandle<'_> {
|
||||
self.0.as_handle()
|
||||
}
|
||||
}
|
||||
#[cfg(unix)]
|
||||
impl AsRawFd for $ty {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
self.0.as_raw_fd()
|
||||
impl AsFd for $ty {
|
||||
fn as_fd(&self) -> BorrowedFd<'_> {
|
||||
self.0.as_fd()
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user