diff --git a/crates/test-programs/wasi-tests/src/bin/stdio.rs b/crates/test-programs/wasi-tests/src/bin/stdio.rs new file mode 100644 index 0000000000..1ef15114de --- /dev/null +++ b/crates/test-programs/wasi-tests/src/bin/stdio.rs @@ -0,0 +1,31 @@ +use libc; +use std::mem::MaybeUninit; +use std::{env, process}; +use wasi::wasi_unstable; +use wasi_tests::wasi_wrappers::wasi_fd_fdstat_get; + +unsafe fn test_stdio() { + for fd in &[ + wasi_unstable::STDIN_FD, + wasi_unstable::STDOUT_FD, + wasi_unstable::STDERR_FD, + ] { + let mut fdstat: wasi_unstable::FdStat = MaybeUninit::zeroed().assume_init(); + let status = wasi_fd_fdstat_get(*fd, &mut fdstat); + assert_eq!( + status, + wasi_unstable::raw::__WASI_ESUCCESS, + "fd_fdstat_get on stdio" + ); + + assert!( + wasi_unstable::fd_renumber(*fd, *fd + 100).is_ok(), + "renumbering stdio", + ); + } +} + +fn main() { + // Run the tests. + unsafe { test_stdio() } +} diff --git a/crates/wasi-common/src/fdentry.rs b/crates/wasi-common/src/fdentry.rs index 948a8ee459..2bad6f79ab 100644 --- a/crates/wasi-common/src/fdentry.rs +++ b/crates/wasi-common/src/fdentry.rs @@ -1,61 +1,44 @@ use crate::sys::dev_null; -use crate::sys::fdentry_impl::{determine_type_and_access_rights, OsFile}; +use crate::sys::fdentry_impl::{ + descriptor_as_oshandle, determine_type_and_access_rights, OsHandle, +}; use crate::{wasi, Error, Result}; +use std::marker::PhantomData; +use std::mem::ManuallyDrop; +use std::ops::{Deref, DerefMut}; use std::path::PathBuf; use std::{fs, io}; #[derive(Debug)] pub(crate) enum Descriptor { - OsFile(OsFile), + OsHandle(OsHandle), Stdin, Stdout, Stderr, } impl Descriptor { - pub(crate) fn as_file(&self) -> Result<&OsFile> { + /// Return a reference to the `OsHandle` treating it as an actual file/dir, and + /// allowing operations which require an actual file and not just a stream or + /// socket file descriptor. + pub(crate) fn as_file(&self) -> Result<&OsHandle> { match self { - Self::OsFile(file) => Ok(file), + Self::OsHandle(file) => Ok(file), _ => Err(Error::EBADF), } } - pub(crate) fn as_file_mut(&mut self) -> Result<&mut OsFile> { + /// Like `as_file`, but return a mutable reference. + pub(crate) fn as_file_mut(&mut self) -> Result<&mut OsHandle> { match self { - Self::OsFile(file) => Ok(file), + Self::OsHandle(file) => Ok(file), _ => Err(Error::EBADF), } } - pub(crate) fn is_file(&self) -> bool { - match self { - Self::OsFile(_) => true, - _ => false, - } - } - - #[allow(unused)] - pub(crate) fn is_stdin(&self) -> bool { - match self { - Self::Stdin => true, - _ => false, - } - } - - #[allow(unused)] - pub(crate) fn is_stdout(&self) -> bool { - match self { - Self::Stdout => true, - _ => false, - } - } - - #[allow(unused)] - pub(crate) fn is_stderr(&self) -> bool { - match self { - Self::Stderr => true, - _ => false, - } + /// Return an `OsHandle`, which may be a stream or socket file descriptor. + pub(crate) fn as_os_handle<'descriptor>(&'descriptor self) -> OsHandleRef<'descriptor> { + descriptor_as_oshandle(self) } } @@ -82,7 +65,7 @@ impl FdEntry { unsafe { determine_type_and_access_rights(&file) }.map( |(file_type, rights_base, rights_inheriting)| Self { file_type, - descriptor: Descriptor::OsFile(OsFile::from(file)), + descriptor: Descriptor::OsHandle(OsHandle::from(file)), rights_base, rights_inheriting, preopen_path: None, @@ -90,10 +73,6 @@ impl FdEntry { ) } - pub(crate) fn duplicate(file: &fs::File) -> Result { - Self::from(file.try_clone()?) - } - pub(crate) fn duplicate_stdin() -> Result { unsafe { determine_type_and_access_rights(&io::stdin()) }.map( |(file_type, rights_base, rights_inheriting)| Self { @@ -184,3 +163,35 @@ impl FdEntry { } } } + +/// This allows an `OsHandle` to be temporarily borrowed from a +/// `Descriptor`. The `Descriptor` continues to own the resource, +/// and `OsHandleRef`'s lifetime parameter ensures that it doesn't +/// outlive the `Descriptor`. +pub(crate) struct OsHandleRef<'descriptor> { + handle: ManuallyDrop, + _ref: PhantomData<&'descriptor Descriptor>, +} + +impl<'descriptor> OsHandleRef<'descriptor> { + pub(crate) fn new(handle: ManuallyDrop) -> Self { + OsHandleRef { + handle, + _ref: PhantomData, + } + } +} + +impl<'descriptor> Deref for OsHandleRef<'descriptor> { + type Target = fs::File; + + fn deref(&self) -> &Self::Target { + &self.handle + } +} + +impl<'descriptor> DerefMut for OsHandleRef<'descriptor> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.handle + } +} diff --git a/crates/wasi-common/src/hostcalls_impl/fs.rs b/crates/wasi-common/src/hostcalls_impl/fs.rs index 7fc0eedbb8..8550efb17e 100644 --- a/crates/wasi-common/src/hostcalls_impl/fs.rs +++ b/crates/wasi-common/src/hostcalls_impl/fs.rs @@ -161,7 +161,7 @@ pub(crate) unsafe fn fd_read( .get_fd_entry_mut(fd)? .as_descriptor_mut(wasi::__WASI_RIGHTS_FD_READ, 0)? { - Descriptor::OsFile(file) => file.read_vectored(&mut iovs), + Descriptor::OsHandle(file) => file.read_vectored(&mut iovs), Descriptor::Stdin => io::stdin().lock().read_vectored(&mut iovs), _ => return Err(Error::EBADF), }; @@ -180,33 +180,25 @@ pub(crate) unsafe fn fd_renumber( ) -> Result<()> { trace!("fd_renumber(from={:?}, to={:?})", from, to); - if !wasi_ctx.contains_fd_entry(from) || !wasi_ctx.contains_fd_entry(to) { + if !wasi_ctx.contains_fd_entry(from) { return Err(Error::EBADF); } - let from_fe = wasi_ctx.get_fd_entry(from)?; - let to_fe = wasi_ctx.get_fd_entry(to)?; - // Don't allow renumbering over a pre-opened resource. // TODO: Eventually, we do want to permit this, once libpreopen in // userspace is capable of removing entries from its tables as well. - if from_fe.preopen_path.is_some() || to_fe.preopen_path.is_some() { + let from_fe = wasi_ctx.get_fd_entry(from)?; + if from_fe.preopen_path.is_some() { return Err(Error::ENOTSUP); } - - // check if stdio fds - // TODO should we renumber stdio fds? - if !from_fe.as_descriptor(0, 0)?.is_file() || !to_fe.as_descriptor(0, 0)?.is_file() { - return Err(Error::EBADF); + if let Ok(to_fe) = wasi_ctx.get_fd_entry(to) { + if to_fe.preopen_path.is_some() { + return Err(Error::ENOTSUP); + } } - let fe_from_dup = from_fe - .as_descriptor(0, 0)? - .as_file() - .and_then(|file| FdEntry::duplicate(file))?; - - wasi_ctx.insert_fd_entry_at(to, fe_from_dup); - wasi_ctx.remove_fd_entry(from)?; + let fe = wasi_ctx.remove_fd_entry(from)?; + wasi_ctx.insert_fd_entry_at(to, fe); Ok(()) } @@ -279,9 +271,12 @@ pub(crate) unsafe fn fd_fdstat_get( trace!("fd_fdstat_get(fd={:?}, fdstat_ptr={:#x?})", fd, fdstat_ptr); let mut fdstat = dec_fdstat_byref(memory, fdstat_ptr)?; - let wasi_fd = wasi_ctx.get_fd_entry(fd)?.as_descriptor(0, 0)?.as_file()?; + let host_fd = wasi_ctx + .get_fd_entry(fd)? + .as_descriptor(0, 0)? + .as_os_handle(); - let fs_flags = hostcalls_impl::fd_fdstat_get(wasi_fd)?; + let fs_flags = hostcalls_impl::fd_fdstat_get(&host_fd)?; let fe = wasi_ctx.get_fd_entry(fd)?; fdstat.fs_filetype = fe.file_type; @@ -301,9 +296,12 @@ pub(crate) unsafe fn fd_fdstat_set_flags( ) -> Result<()> { trace!("fd_fdstat_set_flags(fd={:?}, fdflags={:#x?})", fd, fdflags); - let fd = wasi_ctx.get_fd_entry(fd)?.as_descriptor(0, 0)?.as_file()?; + let fd = wasi_ctx + .get_fd_entry(fd)? + .as_descriptor(0, 0)? + .as_os_handle(); - hostcalls_impl::fd_fdstat_set_flags(fd, fdflags) + hostcalls_impl::fd_fdstat_set_flags(&fd, fdflags) } pub(crate) unsafe fn fd_fdstat_set_rights( @@ -365,7 +363,7 @@ pub(crate) unsafe fn fd_write( .get_fd_entry_mut(fd)? .as_descriptor_mut(wasi::__WASI_RIGHTS_FD_WRITE, 0)? { - Descriptor::OsFile(file) => file.write_vectored(&iovs)?, + Descriptor::OsHandle(file) => file.write_vectored(&iovs)?, Descriptor::Stdin => return Err(Error::EBADF), Descriptor::Stdout => { // lock for the duration of the scope diff --git a/crates/wasi-common/src/old/snapshot_0/fdentry.rs b/crates/wasi-common/src/old/snapshot_0/fdentry.rs index 25cda13dc0..d6b69eed47 100644 --- a/crates/wasi-common/src/old/snapshot_0/fdentry.rs +++ b/crates/wasi-common/src/old/snapshot_0/fdentry.rs @@ -1,61 +1,44 @@ use crate::old::snapshot_0::sys::dev_null; -use crate::old::snapshot_0::sys::fdentry_impl::{determine_type_and_access_rights, OsFile}; +use crate::old::snapshot_0::sys::fdentry_impl::{ + descriptor_as_oshandle, determine_type_and_access_rights, OsHandle, +}; use crate::old::snapshot_0::{wasi, Error, Result}; +use std::marker::PhantomData; +use std::mem::ManuallyDrop; +use std::ops::{Deref, DerefMut}; use std::path::PathBuf; use std::{fs, io}; #[derive(Debug)] pub(crate) enum Descriptor { - OsFile(OsFile), + OsHandle(OsHandle), Stdin, Stdout, Stderr, } impl Descriptor { - pub(crate) fn as_file(&self) -> Result<&OsFile> { + /// Return a reference to the `OsHandle` treating it as an actual file/dir, and + /// allowing operations which require an actual file and not just a stream or + /// socket file descriptor. + pub(crate) fn as_file(&self) -> Result<&OsHandle> { match self { - Self::OsFile(file) => Ok(file), + Self::OsHandle(file) => Ok(file), _ => Err(Error::EBADF), } } - pub(crate) fn as_file_mut(&mut self) -> Result<&mut OsFile> { + /// Like `as_file`, but return a mutable reference. + pub(crate) fn as_file_mut(&mut self) -> Result<&mut OsHandle> { match self { - Self::OsFile(file) => Ok(file), + Self::OsHandle(file) => Ok(file), _ => Err(Error::EBADF), } } - pub(crate) fn is_file(&self) -> bool { - match self { - Self::OsFile(_) => true, - _ => false, - } - } - - #[allow(unused)] - pub(crate) fn is_stdin(&self) -> bool { - match self { - Self::Stdin => true, - _ => false, - } - } - - #[allow(unused)] - pub(crate) fn is_stdout(&self) -> bool { - match self { - Self::Stdout => true, - _ => false, - } - } - - #[allow(unused)] - pub(crate) fn is_stderr(&self) -> bool { - match self { - Self::Stderr => true, - _ => false, - } + /// Return an `OsHandle`, which may be a stream or socket file descriptor. + pub(crate) fn as_os_handle<'descriptor>(&'descriptor self) -> OsHandleRef<'descriptor> { + descriptor_as_oshandle(self) } } @@ -82,7 +65,7 @@ impl FdEntry { unsafe { determine_type_and_access_rights(&file) }.map( |(file_type, rights_base, rights_inheriting)| Self { file_type, - descriptor: Descriptor::OsFile(OsFile::from(file)), + descriptor: Descriptor::OsHandle(OsHandle::from(file)), rights_base, rights_inheriting, preopen_path: None, @@ -90,10 +73,6 @@ impl FdEntry { ) } - pub(crate) fn duplicate(file: &fs::File) -> Result { - Self::from(file.try_clone()?) - } - pub(crate) fn duplicate_stdin() -> Result { unsafe { determine_type_and_access_rights(&io::stdin()) }.map( |(file_type, rights_base, rights_inheriting)| Self { @@ -184,3 +163,35 @@ impl FdEntry { } } } + +/// This allows an `OsHandle` to be temporarily borrowed from a +/// `Descriptor`. The `Descriptor` continues to own the resource, +/// and `OsHandleRef`'s lifetime parameter ensures that it doesn't +/// outlive the `Descriptor`. +pub(crate) struct OsHandleRef<'descriptor> { + handle: ManuallyDrop, + _ref: PhantomData<&'descriptor Descriptor>, +} + +impl<'descriptor> OsHandleRef<'descriptor> { + pub(crate) fn new(handle: ManuallyDrop) -> Self { + OsHandleRef { + handle, + _ref: PhantomData, + } + } +} + +impl<'descriptor> Deref for OsHandleRef<'descriptor> { + type Target = fs::File; + + fn deref(&self) -> &Self::Target { + &self.handle + } +} + +impl<'descriptor> DerefMut for OsHandleRef<'descriptor> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.handle + } +} diff --git a/crates/wasi-common/src/old/snapshot_0/hostcalls_impl/fs.rs b/crates/wasi-common/src/old/snapshot_0/hostcalls_impl/fs.rs index 4dd4d1c579..631c8b171b 100644 --- a/crates/wasi-common/src/old/snapshot_0/hostcalls_impl/fs.rs +++ b/crates/wasi-common/src/old/snapshot_0/hostcalls_impl/fs.rs @@ -161,7 +161,7 @@ pub(crate) unsafe fn fd_read( .get_fd_entry_mut(fd)? .as_descriptor_mut(wasi::__WASI_RIGHTS_FD_READ, 0)? { - Descriptor::OsFile(file) => file.read_vectored(&mut iovs), + Descriptor::OsHandle(file) => file.read_vectored(&mut iovs), Descriptor::Stdin => io::stdin().lock().read_vectored(&mut iovs), _ => return Err(Error::EBADF), }; @@ -180,33 +180,25 @@ pub(crate) unsafe fn fd_renumber( ) -> Result<()> { trace!("fd_renumber(from={:?}, to={:?})", from, to); - if !wasi_ctx.contains_fd_entry(from) || !wasi_ctx.contains_fd_entry(to) { + if !wasi_ctx.contains_fd_entry(from) { return Err(Error::EBADF); } - let from_fe = wasi_ctx.get_fd_entry(from)?; - let to_fe = wasi_ctx.get_fd_entry(to)?; - // Don't allow renumbering over a pre-opened resource. // TODO: Eventually, we do want to permit this, once libpreopen in // userspace is capable of removing entries from its tables as well. - if from_fe.preopen_path.is_some() || to_fe.preopen_path.is_some() { + let from_fe = wasi_ctx.get_fd_entry(from)?; + if from_fe.preopen_path.is_some() { return Err(Error::ENOTSUP); } - - // check if stdio fds - // TODO should we renumber stdio fds? - if !from_fe.as_descriptor(0, 0)?.is_file() || !to_fe.as_descriptor(0, 0)?.is_file() { - return Err(Error::EBADF); + if let Ok(to_fe) = wasi_ctx.get_fd_entry(to) { + if to_fe.preopen_path.is_some() { + return Err(Error::ENOTSUP); + } } - let fe_from_dup = from_fe - .as_descriptor(0, 0)? - .as_file() - .and_then(|file| FdEntry::duplicate(file))?; - - wasi_ctx.insert_fd_entry_at(to, fe_from_dup); - wasi_ctx.remove_fd_entry(from)?; + let fe = wasi_ctx.remove_fd_entry(from)?; + wasi_ctx.insert_fd_entry_at(to, fe); Ok(()) } @@ -279,9 +271,12 @@ pub(crate) unsafe fn fd_fdstat_get( trace!("fd_fdstat_get(fd={:?}, fdstat_ptr={:#x?})", fd, fdstat_ptr); let mut fdstat = dec_fdstat_byref(memory, fdstat_ptr)?; - let wasi_fd = wasi_ctx.get_fd_entry(fd)?.as_descriptor(0, 0)?.as_file()?; + let host_fd = wasi_ctx + .get_fd_entry(fd)? + .as_descriptor(0, 0)? + .as_os_handle(); - let fs_flags = hostcalls_impl::fd_fdstat_get(wasi_fd)?; + let fs_flags = hostcalls_impl::fd_fdstat_get(&host_fd)?; let fe = wasi_ctx.get_fd_entry(fd)?; fdstat.fs_filetype = fe.file_type; @@ -301,9 +296,12 @@ pub(crate) unsafe fn fd_fdstat_set_flags( ) -> Result<()> { trace!("fd_fdstat_set_flags(fd={:?}, fdflags={:#x?})", fd, fdflags); - let fd = wasi_ctx.get_fd_entry(fd)?.as_descriptor(0, 0)?.as_file()?; + let fd = wasi_ctx + .get_fd_entry(fd)? + .as_descriptor(0, 0)? + .as_os_handle(); - hostcalls_impl::fd_fdstat_set_flags(fd, fdflags) + hostcalls_impl::fd_fdstat_set_flags(&fd, fdflags) } pub(crate) unsafe fn fd_fdstat_set_rights( @@ -365,7 +363,7 @@ pub(crate) unsafe fn fd_write( .get_fd_entry_mut(fd)? .as_descriptor_mut(wasi::__WASI_RIGHTS_FD_WRITE, 0)? { - Descriptor::OsFile(file) => file.write_vectored(&iovs)?, + Descriptor::OsHandle(file) => file.write_vectored(&iovs)?, Descriptor::Stdin => return Err(Error::EBADF), Descriptor::Stdout => { // lock for the duration of the scope diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/hostcalls_impl.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/hostcalls_impl.rs index f42467198a..d3647fb81b 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/hostcalls_impl.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/hostcalls_impl.rs @@ -1,4 +1,4 @@ -use super::osfile::OsFile; +use super::oshandle::OsHandle; use crate::old::snapshot_0::hostcalls_impl::PathGet; use crate::old::snapshot_0::sys::host_impl; use crate::old::snapshot_0::sys::unix::str_to_cstring; @@ -140,23 +140,23 @@ pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> Resul } pub(crate) fn fd_readdir( - os_file: &mut OsFile, + os_handle: &mut OsHandle, host_buf: &mut [u8], cookie: wasi::__wasi_dircookie_t, ) -> Result { - use crate::old::snapshot_0::sys::unix::bsd::osfile::DirStream; + use crate::old::snapshot_0::sys::unix::bsd::oshandle::DirStream; use libc::{fdopendir, readdir, rewinddir, seekdir, telldir}; use nix::errno::Errno; use std::ffi::CStr; use std::mem::ManuallyDrop; use std::sync::Mutex; - let dir_stream = match os_file.dir_stream { + let dir_stream = match os_handle.dir_stream { Some(ref mut dir_stream) => dir_stream, None => { - let file = os_file.file.try_clone()?; + let file = os_handle.file.try_clone()?; let dir_ptr = unsafe { fdopendir(file.as_raw_fd()) }; - os_file.dir_stream.get_or_insert(Mutex::new(DirStream { + os_handle.dir_stream.get_or_insert(Mutex::new(DirStream { file: ManuallyDrop::new(file), dir_ptr, })) diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/mod.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/mod.rs index 4ba789dbc6..27d5442886 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/mod.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/mod.rs @@ -1,6 +1,6 @@ pub(crate) mod filetime; pub(crate) mod hostcalls_impl; -pub(crate) mod osfile; +pub(crate) mod oshandle; pub(crate) mod fdentry_impl { use crate::old::snapshot_0::{sys::host_impl, Result}; diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/osfile.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/oshandle.rs similarity index 85% rename from crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/osfile.rs rename to crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/oshandle.rs index 41340149aa..3900ef8141 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/osfile.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/oshandle.rs @@ -17,12 +17,12 @@ impl Drop for DirStream { } #[derive(Debug)] -pub(crate) struct OsFile { +pub(crate) struct OsHandle { pub(crate) file: fs::File, pub(crate) dir_stream: Option>, } -impl From for OsFile { +impl From for OsHandle { fn from(file: fs::File) -> Self { Self { file, @@ -31,13 +31,13 @@ impl From for OsFile { } } -impl AsRawFd for OsFile { +impl AsRawFd for OsHandle { fn as_raw_fd(&self) -> RawFd { self.file.as_raw_fd() } } -impl Deref for OsFile { +impl Deref for OsHandle { type Target = fs::File; fn deref(&self) -> &Self::Target { @@ -45,7 +45,7 @@ impl Deref for OsFile { } } -impl DerefMut for OsFile { +impl DerefMut for OsHandle { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.file } diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/fdentry_impl.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/fdentry_impl.rs index b63a850df5..3a628123d7 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/fdentry_impl.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/unix/fdentry_impl.rs @@ -1,11 +1,13 @@ -use crate::old::snapshot_0::fdentry::Descriptor; +use crate::old::snapshot_0::fdentry::{Descriptor, OsHandleRef}; use crate::old::snapshot_0::{wasi, Error, Result}; +use std::fs::File; use std::io; +use std::mem::ManuallyDrop; use std::os::unix::prelude::{AsRawFd, FileTypeExt, FromRawFd, RawFd}; cfg_if::cfg_if! { if #[cfg(target_os = "linux")] { - pub(crate) use super::linux::osfile::*; + pub(crate) use super::linux::oshandle::*; pub(crate) use super::linux::fdentry_impl::*; } else if #[cfg(any( target_os = "macos", @@ -15,7 +17,7 @@ cfg_if::cfg_if! { target_os = "ios", target_os = "dragonfly" ))] { - pub(crate) use super::bsd::osfile::*; + pub(crate) use super::bsd::oshandle::*; pub(crate) use super::bsd::fdentry_impl::*; } } @@ -23,7 +25,7 @@ cfg_if::cfg_if! { impl AsRawFd for Descriptor { fn as_raw_fd(&self) -> RawFd { match self { - Self::OsFile(file) => file.as_raw_fd(), + Self::OsHandle(file) => file.as_raw_fd(), Self::Stdin => io::stdin().as_raw_fd(), Self::Stdout => io::stdout().as_raw_fd(), Self::Stderr => io::stderr().as_raw_fd(), @@ -31,6 +33,14 @@ impl AsRawFd for Descriptor { } } +pub(crate) fn descriptor_as_oshandle<'lifetime>( + desc: &'lifetime Descriptor, +) -> OsHandleRef<'lifetime> { + OsHandleRef::new(ManuallyDrop::new(OsHandle::from(unsafe { + File::from_raw_fd(desc.as_raw_fd()) + }))) +} + /// This function is unsafe because it operates on a raw file descriptor. pub(crate) unsafe fn determine_type_and_access_rights( fd: &Fd, diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/hostcalls_impl.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/hostcalls_impl.rs index deebe005ff..d582105ede 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/hostcalls_impl.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/hostcalls_impl.rs @@ -1,5 +1,5 @@ use super::super::dir::{Dir, Entry, SeekLoc}; -use super::osfile::OsFile; +use super::oshandle::OsHandle; use crate::old::snapshot_0::hostcalls_impl::{Dirent, PathGet}; use crate::old::snapshot_0::sys::host_impl; use crate::old::snapshot_0::sys::unix::str_to_cstring; @@ -115,11 +115,11 @@ pub(crate) fn fd_readdir_impl( // This should actually be common code with Windows, // but there's BSD stuff remaining pub(crate) fn fd_readdir( - os_file: &mut OsFile, + os_handle: &mut OsHandle, mut host_buf: &mut [u8], cookie: wasi::__wasi_dircookie_t, ) -> Result { - let iter = fd_readdir_impl(os_file, cookie)?; + let iter = fd_readdir_impl(os_handle, cookie)?; let mut used = 0; for dirent in iter { let dirent_raw = dirent?.to_wasi_raw()?; diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/mod.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/mod.rs index 4bfb6ec08f..e16410b61e 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/mod.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/mod.rs @@ -1,6 +1,6 @@ pub(crate) mod filetime; pub(crate) mod hostcalls_impl; -pub(crate) mod osfile; +pub(crate) mod oshandle; pub(crate) mod fdentry_impl { use crate::old::snapshot_0::{sys::host_impl, Result}; diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/osfile.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/oshandle.rs similarity index 72% rename from crates/wasi-common/src/old/snapshot_0/sys/unix/linux/osfile.rs rename to crates/wasi-common/src/old/snapshot_0/sys/unix/linux/oshandle.rs index b92a9793a6..8104d9d5e3 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/osfile.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/oshandle.rs @@ -3,21 +3,21 @@ use std::ops::{Deref, DerefMut}; use std::os::unix::prelude::{AsRawFd, RawFd}; #[derive(Debug)] -pub(crate) struct OsFile(fs::File); +pub(crate) struct OsHandle(fs::File); -impl From for OsFile { +impl From for OsHandle { fn from(file: fs::File) -> Self { Self(file) } } -impl AsRawFd for OsFile { +impl AsRawFd for OsHandle { fn as_raw_fd(&self) -> RawFd { self.0.as_raw_fd() } } -impl Deref for OsFile { +impl Deref for OsHandle { type Target = fs::File; fn deref(&self) -> &Self::Target { @@ -25,7 +25,7 @@ impl Deref for OsFile { } } -impl DerefMut for OsFile { +impl DerefMut for OsHandle { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } diff --git a/crates/wasi-common/src/old/snapshot_0/sys/windows/fdentry_impl.rs b/crates/wasi-common/src/old/snapshot_0/sys/windows/fdentry_impl.rs index e903e1a34b..4b1c6ba39e 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/windows/fdentry_impl.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/windows/fdentry_impl.rs @@ -1,26 +1,27 @@ -use crate::old::snapshot_0::fdentry::Descriptor; +use crate::old::snapshot_0::fdentry::{Descriptor, OsHandleRef}; use crate::old::snapshot_0::{wasi, Error, Result}; use std::fs::File; use std::io; +use std::mem::ManuallyDrop; use std::ops::{Deref, DerefMut}; use std::os::windows::prelude::{AsRawHandle, FromRawHandle, RawHandle}; #[derive(Debug)] -pub(crate) struct OsFile(File); +pub(crate) struct OsHandle(File); -impl From for OsFile { +impl From for OsHandle { fn from(file: File) -> Self { Self(file) } } -impl AsRawHandle for OsFile { +impl AsRawHandle for OsHandle { fn as_raw_handle(&self) -> RawHandle { self.0.as_raw_handle() } } -impl Deref for OsFile { +impl Deref for OsHandle { type Target = File; fn deref(&self) -> &Self::Target { @@ -28,7 +29,7 @@ impl Deref for OsFile { } } -impl DerefMut for OsFile { +impl DerefMut for OsHandle { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } @@ -37,7 +38,7 @@ impl DerefMut for OsFile { impl AsRawHandle for Descriptor { fn as_raw_handle(&self) -> RawHandle { match self { - Self::OsFile(file) => file.as_raw_handle(), + Self::OsHandle(file) => file.as_raw_handle(), Self::Stdin => io::stdin().as_raw_handle(), Self::Stdout => io::stdout().as_raw_handle(), Self::Stderr => io::stderr().as_raw_handle(), @@ -45,6 +46,14 @@ impl AsRawHandle for Descriptor { } } +pub(crate) fn descriptor_as_oshandle<'lifetime>( + desc: &'lifetime Descriptor, +) -> OsHandleRef<'lifetime> { + OsHandleRef::new(ManuallyDrop::new(OsHandle::from(unsafe { + File::from_raw_handle(desc.as_raw_handle()) + }))) +} + /// This function is unsafe because it operates on a raw file handle. pub(crate) unsafe fn determine_type_and_access_rights( handle: &Handle, diff --git a/crates/wasi-common/src/old/snapshot_0/sys/windows/hostcalls_impl/fs.rs b/crates/wasi-common/src/old/snapshot_0/sys/windows/hostcalls_impl/fs.rs index 3f3b9da7ac..0a33e9056e 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/windows/hostcalls_impl/fs.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/windows/hostcalls_impl/fs.rs @@ -7,7 +7,7 @@ use crate::old::snapshot_0::helpers::systemtime_to_timestamp; use crate::old::snapshot_0::hostcalls_impl::{ fd_filestat_set_times_impl, Dirent, FileType, PathGet, }; -use crate::old::snapshot_0::sys::fdentry_impl::{determine_type_rights, OsFile}; +use crate::old::snapshot_0::sys::fdentry_impl::{determine_type_rights, OsHandle}; use crate::old::snapshot_0::sys::host_impl::{self, path_from_host}; use crate::old::snapshot_0::sys::hostcalls_impl::fs_helpers::PathGetExt; use crate::old::snapshot_0::{wasi, Error, Result}; @@ -261,11 +261,11 @@ pub(crate) fn fd_readdir_impl( // This should actually be common code with Linux pub(crate) fn fd_readdir( - os_file: &mut OsFile, + os_handle: &mut OsHandle, mut host_buf: &mut [u8], cookie: wasi::__wasi_dircookie_t, ) -> Result { - let iter = fd_readdir_impl(os_file, cookie)?; + let iter = fd_readdir_impl(os_handle, cookie)?; let mut used = 0; for dirent in iter { let dirent_raw = dirent?.to_wasi_raw()?; diff --git a/crates/wasi-common/src/sys/unix/bsd/hostcalls_impl.rs b/crates/wasi-common/src/sys/unix/bsd/hostcalls_impl.rs index 40cfcb4809..67194f02ac 100644 --- a/crates/wasi-common/src/sys/unix/bsd/hostcalls_impl.rs +++ b/crates/wasi-common/src/sys/unix/bsd/hostcalls_impl.rs @@ -1,5 +1,5 @@ use super::super::dir::{Dir, Entry, SeekLoc}; -use super::osfile::OsFile; +use super::oshandle::OsHandle; use crate::hostcalls_impl::{Dirent, PathGet}; use crate::sys::host_impl; use crate::sys::unix::str_to_cstring; @@ -205,12 +205,12 @@ pub(crate) fn fd_advise( } pub(crate) fn fd_readdir<'a>( - os_file: &'a mut OsFile, + os_handle: &'a mut OsHandle, cookie: wasi::__wasi_dircookie_t, ) -> Result> + 'a> { use std::sync::Mutex; - let dir = match os_file.dir { + let dir = match os_handle.dir { Some(ref mut dir) => dir, None => { // We need to duplicate the fd, because `opendir(3)`: @@ -219,9 +219,9 @@ pub(crate) fn fd_readdir<'a>( // descriptor, or to modify the state of the associated description other // than by means of closedir(), readdir(), readdir_r(), or rewinddir(), // the behaviour is undefined. - let fd = (*os_file).try_clone()?; + let fd = (*os_handle).try_clone()?; let dir = Dir::from(fd)?; - os_file.dir.get_or_insert(Mutex::new(dir)) + os_handle.dir.get_or_insert(Mutex::new(dir)) } }; let mut dir = dir.lock().unwrap(); diff --git a/crates/wasi-common/src/sys/unix/bsd/mod.rs b/crates/wasi-common/src/sys/unix/bsd/mod.rs index e973751b77..4fe76b1be7 100644 --- a/crates/wasi-common/src/sys/unix/bsd/mod.rs +++ b/crates/wasi-common/src/sys/unix/bsd/mod.rs @@ -1,6 +1,6 @@ pub(crate) mod filetime; pub(crate) mod hostcalls_impl; -pub(crate) mod osfile; +pub(crate) mod oshandle; pub(crate) mod fdentry_impl { use crate::{sys::host_impl, Result}; diff --git a/crates/wasi-common/src/sys/unix/bsd/osfile.rs b/crates/wasi-common/src/sys/unix/bsd/oshandle.rs similarity index 84% rename from crates/wasi-common/src/sys/unix/bsd/osfile.rs rename to crates/wasi-common/src/sys/unix/bsd/oshandle.rs index 379dcfdced..eccc7ab33e 100644 --- a/crates/wasi-common/src/sys/unix/bsd/osfile.rs +++ b/crates/wasi-common/src/sys/unix/bsd/oshandle.rs @@ -5,9 +5,9 @@ use std::os::unix::prelude::{AsRawFd, RawFd}; use std::sync::Mutex; #[derive(Debug)] -pub(crate) struct OsFile { +pub(crate) struct OsHandle { pub(crate) file: fs::File, - // In case that this `OsFile` actually refers to a directory, + // In case that this `OsHandle` actually refers to a directory, // when the client makes a `fd_readdir` syscall on this descriptor, // we will need to cache the `libc::DIR` pointer manually in order // to be able to seek on it later. While on Linux, this is handled @@ -21,19 +21,19 @@ pub(crate) struct OsFile { pub(crate) dir: Option>, } -impl From for OsFile { +impl From for OsHandle { fn from(file: fs::File) -> Self { Self { file, dir: None } } } -impl AsRawFd for OsFile { +impl AsRawFd for OsHandle { fn as_raw_fd(&self) -> RawFd { self.file.as_raw_fd() } } -impl Deref for OsFile { +impl Deref for OsHandle { type Target = fs::File; fn deref(&self) -> &Self::Target { @@ -41,7 +41,7 @@ impl Deref for OsFile { } } -impl DerefMut for OsFile { +impl DerefMut for OsHandle { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.file } diff --git a/crates/wasi-common/src/sys/unix/fdentry_impl.rs b/crates/wasi-common/src/sys/unix/fdentry_impl.rs index fcbe499999..8015eebf6f 100644 --- a/crates/wasi-common/src/sys/unix/fdentry_impl.rs +++ b/crates/wasi-common/src/sys/unix/fdentry_impl.rs @@ -1,11 +1,13 @@ -use crate::fdentry::Descriptor; +use crate::fdentry::{Descriptor, OsHandleRef}; use crate::{wasi, Error, Result}; +use std::fs::File; use std::io; +use std::mem::ManuallyDrop; use std::os::unix::prelude::{AsRawFd, FileTypeExt, FromRawFd, RawFd}; cfg_if::cfg_if! { if #[cfg(target_os = "linux")] { - pub(crate) use super::linux::osfile::*; + pub(crate) use super::linux::oshandle::*; pub(crate) use super::linux::fdentry_impl::*; } else if #[cfg(any( target_os = "macos", @@ -15,7 +17,7 @@ cfg_if::cfg_if! { target_os = "ios", target_os = "dragonfly" ))] { - pub(crate) use super::bsd::osfile::*; + pub(crate) use super::bsd::oshandle::*; pub(crate) use super::bsd::fdentry_impl::*; } } @@ -23,7 +25,7 @@ cfg_if::cfg_if! { impl AsRawFd for Descriptor { fn as_raw_fd(&self) -> RawFd { match self { - Self::OsFile(file) => file.as_raw_fd(), + Self::OsHandle(file) => file.as_raw_fd(), Self::Stdin => io::stdin().as_raw_fd(), Self::Stdout => io::stdout().as_raw_fd(), Self::Stderr => io::stderr().as_raw_fd(), @@ -31,6 +33,14 @@ impl AsRawFd for Descriptor { } } +pub(crate) fn descriptor_as_oshandle<'lifetime>( + desc: &'lifetime Descriptor, +) -> OsHandleRef<'lifetime> { + OsHandleRef::new(ManuallyDrop::new(OsHandle::from(unsafe { + File::from_raw_fd(desc.as_raw_fd()) + }))) +} + /// This function is unsafe because it operates on a raw file descriptor. pub(crate) unsafe fn determine_type_and_access_rights( fd: &Fd, diff --git a/crates/wasi-common/src/sys/unix/linux/mod.rs b/crates/wasi-common/src/sys/unix/linux/mod.rs index 16b3555b5d..32955b18c7 100644 --- a/crates/wasi-common/src/sys/unix/linux/mod.rs +++ b/crates/wasi-common/src/sys/unix/linux/mod.rs @@ -1,6 +1,6 @@ pub(crate) mod filetime; pub(crate) mod hostcalls_impl; -pub(crate) mod osfile; +pub(crate) mod oshandle; pub(crate) mod fdentry_impl { use crate::{sys::host_impl, Result}; diff --git a/crates/wasi-common/src/sys/unix/linux/osfile.rs b/crates/wasi-common/src/sys/unix/linux/oshandle.rs similarity index 72% rename from crates/wasi-common/src/sys/unix/linux/osfile.rs rename to crates/wasi-common/src/sys/unix/linux/oshandle.rs index b92a9793a6..8104d9d5e3 100644 --- a/crates/wasi-common/src/sys/unix/linux/osfile.rs +++ b/crates/wasi-common/src/sys/unix/linux/oshandle.rs @@ -3,21 +3,21 @@ use std::ops::{Deref, DerefMut}; use std::os::unix::prelude::{AsRawFd, RawFd}; #[derive(Debug)] -pub(crate) struct OsFile(fs::File); +pub(crate) struct OsHandle(fs::File); -impl From for OsFile { +impl From for OsHandle { fn from(file: fs::File) -> Self { Self(file) } } -impl AsRawFd for OsFile { +impl AsRawFd for OsHandle { fn as_raw_fd(&self) -> RawFd { self.0.as_raw_fd() } } -impl Deref for OsFile { +impl Deref for OsHandle { type Target = fs::File; fn deref(&self) -> &Self::Target { @@ -25,7 +25,7 @@ impl Deref for OsFile { } } -impl DerefMut for OsFile { +impl DerefMut for OsHandle { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } diff --git a/crates/wasi-common/src/sys/windows/fdentry_impl.rs b/crates/wasi-common/src/sys/windows/fdentry_impl.rs index 18cf9914fe..bb7b4263fc 100644 --- a/crates/wasi-common/src/sys/windows/fdentry_impl.rs +++ b/crates/wasi-common/src/sys/windows/fdentry_impl.rs @@ -1,26 +1,27 @@ -use crate::fdentry::Descriptor; +use crate::fdentry::{Descriptor, OsHandleRef}; use crate::{wasi, Error, Result}; use std::fs::File; use std::io; +use std::mem::ManuallyDrop; use std::ops::{Deref, DerefMut}; use std::os::windows::prelude::{AsRawHandle, FromRawHandle, RawHandle}; #[derive(Debug)] -pub(crate) struct OsFile(File); +pub(crate) struct OsHandle(File); -impl From for OsFile { +impl From for OsHandle { fn from(file: File) -> Self { Self(file) } } -impl AsRawHandle for OsFile { +impl AsRawHandle for OsHandle { fn as_raw_handle(&self) -> RawHandle { self.0.as_raw_handle() } } -impl Deref for OsFile { +impl Deref for OsHandle { type Target = File; fn deref(&self) -> &Self::Target { @@ -28,7 +29,7 @@ impl Deref for OsFile { } } -impl DerefMut for OsFile { +impl DerefMut for OsHandle { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } @@ -37,7 +38,7 @@ impl DerefMut for OsFile { impl AsRawHandle for Descriptor { fn as_raw_handle(&self) -> RawHandle { match self { - Self::OsFile(file) => file.as_raw_handle(), + Self::OsHandle(file) => file.as_raw_handle(), Self::Stdin => io::stdin().as_raw_handle(), Self::Stdout => io::stdout().as_raw_handle(), Self::Stderr => io::stderr().as_raw_handle(), @@ -45,6 +46,14 @@ impl AsRawHandle for Descriptor { } } +pub(crate) fn descriptor_as_oshandle<'lifetime>( + desc: &'lifetime Descriptor, +) -> OsHandleRef<'lifetime> { + OsHandleRef::new(ManuallyDrop::new(OsHandle::from(unsafe { + File::from_raw_handle(desc.as_raw_handle()) + }))) +} + /// This function is unsafe because it operates on a raw file handle. pub(crate) unsafe fn determine_type_and_access_rights( handle: &Handle, diff --git a/crates/wasi-common/src/sys/windows/hostcalls_impl/fs.rs b/crates/wasi-common/src/sys/windows/hostcalls_impl/fs.rs index d98c2a17aa..4c8cfb269f 100644 --- a/crates/wasi-common/src/sys/windows/hostcalls_impl/fs.rs +++ b/crates/wasi-common/src/sys/windows/hostcalls_impl/fs.rs @@ -5,7 +5,7 @@ use crate::ctx::WasiCtx; use crate::fdentry::FdEntry; use crate::helpers::systemtime_to_timestamp; use crate::hostcalls_impl::{fd_filestat_set_times_impl, Dirent, FileType, PathGet}; -use crate::sys::fdentry_impl::{determine_type_rights, OsFile}; +use crate::sys::fdentry_impl::determine_type_rights; use crate::sys::host_impl::{self, path_from_host}; use crate::sys::hostcalls_impl::fs_helpers::PathGetExt; use crate::{wasi, Error, Result};