diff --git a/crates/wasi-common/src/host.rs b/crates/wasi-common/src/host.rs index 8af57ec476..2b79007517 100644 --- a/crates/wasi-common/src/host.rs +++ b/crates/wasi-common/src/host.rs @@ -63,12 +63,12 @@ impl Dirent { let sys_dirent = raw.as_mut_ptr() as *mut __wasi_dirent_t; unsafe { - *sys_dirent = __wasi_dirent_t { + sys_dirent.write_unaligned(__wasi_dirent_t { d_namlen: namlen.try_into()?, d_ino: self.ino, d_next: self.cookie, d_type: self.ftype.to_wasi(), - }; + }); } let sys_name = unsafe { sys_dirent.offset(1) as *mut u8 }; diff --git a/crates/wasi-common/src/old/snapshot_0/host.rs b/crates/wasi-common/src/old/snapshot_0/host.rs index 87d8eb3845..711b81f869 100644 --- a/crates/wasi-common/src/old/snapshot_0/host.rs +++ b/crates/wasi-common/src/old/snapshot_0/host.rs @@ -63,12 +63,12 @@ impl Dirent { let sys_dirent = raw.as_mut_ptr() as *mut __wasi_dirent_t; unsafe { - *sys_dirent = __wasi_dirent_t { + sys_dirent.write_unaligned(__wasi_dirent_t { d_namlen: namlen.try_into()?, d_ino: self.ino, d_next: self.cookie, d_type: self.ftype.to_wasi(), - }; + }); } let sys_name = unsafe { sys_dirent.offset(1) as *mut u8 }; diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/misc.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/misc.rs index ac84e3b7c2..4be75c61d4 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/misc.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/misc.rs @@ -3,7 +3,7 @@ use crate::old::snapshot_0::hostcalls_impl::{ClockEventData, FdEventData}; use crate::old::snapshot_0::sys::host_impl; use crate::old::snapshot_0::{wasi, Error, Result}; -use yanix::clock::{clock_getres, ClockId}; +use yanix::clock::{clock_getres, clock_gettime, ClockId}; fn wasi_clock_id_to_unix(clock_id: wasi::__wasi_clockid_t) -> Result { // convert the supported clocks to libc types, or return EINVAL @@ -39,7 +39,7 @@ pub(crate) fn clock_res_get(clock_id: wasi::__wasi_clockid_t) -> Result Result { let clock_id = wasi_clock_id_to_unix(clock_id)?; - let timespec = clock_getres(clock_id)?; + let timespec = clock_gettime(clock_id)?; // convert to nanoseconds, returning EOVERFLOW in case of overflow; this is freelancing a bit // from the spec but seems like it'll be an unusual situation to hit diff --git a/crates/wasi-common/src/sys/mod.rs b/crates/wasi-common/src/sys/mod.rs index db73bf1a78..1149a426a3 100644 --- a/crates/wasi-common/src/sys/mod.rs +++ b/crates/wasi-common/src/sys/mod.rs @@ -4,16 +4,16 @@ use cfg_if::cfg_if; cfg_if! { if #[cfg(unix)] { mod unix; - pub(crate) use self::unix::*; - pub use self::unix::preopen_dir; + pub(crate) use unix::*; + pub use unix::preopen_dir; pub(crate) fn errno_from_host(err: i32) -> wasi::__wasi_errno_t { host_impl::errno_from_nix(yanix::Errno::from_i32(err)).as_wasi_errno() } } else if #[cfg(windows)] { mod windows; - pub(crate) use self::windows::*; - pub use self::windows::preopen_dir; + pub(crate) use windows::*; + pub use windows::preopen_dir; pub(crate) fn errno_from_host(err: i32) -> wasi::__wasi_errno_t { host_impl::errno_from_win(winx::winerror::WinError::from_u32(err as u32)) diff --git a/crates/wasi-common/src/sys/unix/hostcalls_impl/misc.rs b/crates/wasi-common/src/sys/unix/hostcalls_impl/misc.rs index 077adb3868..34135f59de 100644 --- a/crates/wasi-common/src/sys/unix/hostcalls_impl/misc.rs +++ b/crates/wasi-common/src/sys/unix/hostcalls_impl/misc.rs @@ -3,7 +3,7 @@ use crate::hostcalls_impl::{ClockEventData, FdEventData}; use crate::sys::host_impl; use crate::{wasi, Error, Result}; -use yanix::clock::{clock_getres, ClockId}; +use yanix::clock::{clock_getres, clock_gettime, ClockId}; fn wasi_clock_id_to_unix(clock_id: wasi::__wasi_clockid_t) -> Result { // convert the supported clocks to libc types, or return EINVAL @@ -39,7 +39,7 @@ pub(crate) fn clock_res_get(clock_id: wasi::__wasi_clockid_t) -> Result Result { let clock_id = wasi_clock_id_to_unix(clock_id)?; - let timespec = clock_getres(clock_id)?; + let timespec = clock_gettime(clock_id)?; // convert to nanoseconds, returning EOVERFLOW in case of overflow; this is freelancing a bit // from the spec but seems like it'll be an unusual situation to hit diff --git a/crates/wasi-common/yanix/src/clock.rs b/crates/wasi-common/yanix/src/clock.rs index 530272cc19..1f70d18893 100644 --- a/crates/wasi-common/yanix/src/clock.rs +++ b/crates/wasi-common/yanix/src/clock.rs @@ -27,3 +27,11 @@ pub fn clock_getres(clock_id: ClockId) -> Result { })?; Ok(unsafe { timespec.assume_init() }) } + +pub fn clock_gettime(clock_id: ClockId) -> Result { + let mut timespec = MaybeUninit::::uninit(); + Errno::from_success_code(unsafe { + libc::clock_gettime(clock_id.as_raw(), timespec.as_mut_ptr()) + })?; + Ok(unsafe { timespec.assume_init() }) +} diff --git a/crates/wasi-common/yanix/src/dir.rs b/crates/wasi-common/yanix/src/dir.rs index a62a451d2a..00f41ab908 100644 --- a/crates/wasi-common/yanix/src/dir.rs +++ b/crates/wasi-common/yanix/src/dir.rs @@ -8,7 +8,7 @@ use std::{ffi::CStr, ops::Deref, ptr}; pub use crate::sys::EntryExt; #[derive(Clone, Debug, Eq, Hash, PartialEq)] -pub struct Dir(pub(crate) ptr::NonNull); +pub struct Dir(ptr::NonNull); impl Dir { /// Takes the ownership of the passed-in descriptor-based object, @@ -51,6 +51,12 @@ impl Dir { let loc = unsafe { libc::telldir(self.0.as_ptr()) }; SeekLoc(loc) } + + /// For use by platform-specific implementation code. Returns the raw + /// underlying state. + pub(crate) fn as_raw(&self) -> ptr::NonNull { + self.0 + } } unsafe impl Send for Dir {} @@ -78,7 +84,7 @@ impl Entry { /// Returns the type of this directory entry. pub fn file_type(&self) -> FileType { - unsafe { FileType::from_raw(self.0.d_type) } + FileType::from_raw(self.0.d_type) } } @@ -107,7 +113,7 @@ pub enum FileType { } impl FileType { - pub unsafe fn from_raw(file_type: u8) -> Self { + pub fn from_raw(file_type: u8) -> Self { match file_type { libc::DT_CHR => Self::CharacterDevice, libc::DT_DIR => Self::Directory, @@ -153,6 +159,6 @@ where type Item = Result; fn next(&mut self) -> Option { - unsafe { iter_impl(&self.0).map(|x| x.map(Entry)) } + iter_impl(&self.0).map(|x| x.map(Entry)) } } diff --git a/crates/wasi-common/yanix/src/poll.rs b/crates/wasi-common/yanix/src/poll.rs index 58cb4426cf..db33b584b4 100644 --- a/crates/wasi-common/yanix/src/poll.rs +++ b/crates/wasi-common/yanix/src/poll.rs @@ -35,7 +35,7 @@ impl PollFd { } } -pub fn poll(fds: &mut [PollFd], timeout: i32) -> Result { +pub fn poll(fds: &mut [PollFd], timeout: libc::c_int) -> Result { Errno::from_result(unsafe { libc::poll( fds.as_mut_ptr() as *mut libc::pollfd, diff --git a/crates/wasi-common/yanix/src/sys/bsd/dir.rs b/crates/wasi-common/yanix/src/sys/bsd/dir.rs index f9255eac4a..41a8d5941a 100644 --- a/crates/wasi-common/yanix/src/sys/bsd/dir.rs +++ b/crates/wasi-common/yanix/src/sys/bsd/dir.rs @@ -18,9 +18,9 @@ impl Deref for EntryImpl { } } -pub(crate) unsafe fn iter_impl(dir: &Dir) -> Option> { +pub(crate) fn iter_impl(dir: &Dir) -> Option> { let errno = Errno::last(); - let dirent = libc::readdir(dir.0.as_ptr()); + let dirent = unsafe { libc::readdir(dir.as_raw().as_ptr()) }; if dirent.is_null() { if errno != Errno::last() { // TODO This should be verified on different BSD-flavours. @@ -35,7 +35,7 @@ pub(crate) unsafe fn iter_impl(dir: &Dir) -> Option> { } } else { Some(Ok(EntryImpl { - dirent: *dirent, + dirent: unsafe { *dirent }, loc: dir.tell(), })) } diff --git a/crates/wasi-common/yanix/src/sys/linux/dir.rs b/crates/wasi-common/yanix/src/sys/linux/dir.rs index 90fb45a3b6..aa2598f070 100644 --- a/crates/wasi-common/yanix/src/sys/linux/dir.rs +++ b/crates/wasi-common/yanix/src/sys/linux/dir.rs @@ -25,9 +25,9 @@ impl EntryExt for Entry { } } -pub(crate) unsafe fn iter_impl(dir: &Dir) -> Option> { +pub(crate) fn iter_impl(dir: &Dir) -> Option> { let errno = Errno::last(); - let dirent = libc::readdir64(dir.0.as_ptr()); + let dirent = unsafe { libc::readdir64(dir.as_raw().as_ptr()) }; if dirent.is_null() { if errno != Errno::last() { // TODO This should be verified on different BSD-flavours. @@ -41,6 +41,6 @@ pub(crate) unsafe fn iter_impl(dir: &Dir) -> Option> { None } } else { - Some(Ok(EntryImpl(*dirent))) + Some(Ok(EntryImpl(unsafe { *dirent }))) } } diff --git a/crates/wasi-common/yanix/src/sys/mod.rs b/crates/wasi-common/yanix/src/sys/mod.rs index 72cdcb44fa..3743f6f190 100644 --- a/crates/wasi-common/yanix/src/sys/mod.rs +++ b/crates/wasi-common/yanix/src/sys/mod.rs @@ -5,10 +5,10 @@ cfg_if! { if #[cfg(any(target_os = "linux", target_os = "android"))] { mod linux; - pub(crate) use self::linux::*; + pub(crate) use linux::*; } else if #[cfg(target_os = "emscripten")] { mod emscripten; - pub(crate) use self::emscripten::*; + pub(crate) use emscripten::*; } else if #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd", @@ -16,7 +16,7 @@ cfg_if! { target_os = "openbsd", target_os = "dragonfly"))] { mod bsd; - pub(crate) use self::bsd::*; + pub(crate) use bsd::*; } else { compile_error!("yanix doesn't compile for this platform yet"); }