Sync with lucet-wasi (#106)
* Open /dev/null for writing as well as reading. Port this fix to wasi-common:b905c44483* Remove all remaining uses of `std::mem::uninitialized`. Patch inspired by:2d6519d051* Replace libc::memcpy() calls with std::ptr::copy_nonoverlapping() Port this fix to wasi-common:a3f3a33e9b* Pass `WasiError` by value. It's a `u16` underneath, so we can pass it by value. * Avoid unnecessary explicit lifetime parameters. * Use immutable references rather than mutable references. Patch inspired by:54baa4c38c
This commit is contained in:
@@ -62,7 +62,7 @@ pub(crate) fn fd_readdir(
|
||||
cookie: host::__wasi_dircookie_t,
|
||||
) -> Result<usize> {
|
||||
use crate::sys::unix::bsd::osfile::DirStream;
|
||||
use libc::{fdopendir, memcpy, readdir, rewinddir, seekdir, telldir};
|
||||
use libc::{fdopendir, readdir, rewinddir, seekdir, telldir};
|
||||
use nix::errno::Errno;
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::sync::Mutex;
|
||||
@@ -129,9 +129,9 @@ pub(crate) fn fd_readdir(
|
||||
host_buf_offset += std::mem::size_of_val(&entry);
|
||||
let name_ptr = unsafe { *host_entry }.d_name.as_ptr();
|
||||
unsafe {
|
||||
memcpy(
|
||||
host_buf_ptr.offset(host_buf_offset.try_into()?) as *mut _,
|
||||
std::ptr::copy_nonoverlapping(
|
||||
name_ptr as *const _,
|
||||
host_buf_ptr.offset(host_buf_offset.try_into()?) as *mut _,
|
||||
name_len,
|
||||
)
|
||||
};
|
||||
|
||||
@@ -5,6 +5,7 @@ use crate::sys::host_impl;
|
||||
use crate::{host, wasm32, Error, Result};
|
||||
use nix::libc::{self, c_int};
|
||||
use std::cmp;
|
||||
use std::mem::MaybeUninit;
|
||||
use std::time::SystemTime;
|
||||
|
||||
pub(crate) fn clock_res_get(clock_id: host::__wasi_clockid_t) -> Result<host::__wasi_timestamp_t> {
|
||||
@@ -18,11 +19,12 @@ pub(crate) fn clock_res_get(clock_id: host::__wasi_clockid_t) -> Result<host::__
|
||||
};
|
||||
|
||||
// no `nix` wrapper for clock_getres, so we do it ourselves
|
||||
let mut timespec = unsafe { std::mem::uninitialized::<libc::timespec>() };
|
||||
let res = unsafe { libc::clock_getres(clock_id, &mut timespec as *mut libc::timespec) };
|
||||
let mut timespec = MaybeUninit::<libc::timespec>::uninit();
|
||||
let res = unsafe { libc::clock_getres(clock_id, timespec.as_mut_ptr()) };
|
||||
if res != 0 {
|
||||
return Err(host_impl::errno_from_nix(nix::errno::Errno::last()));
|
||||
}
|
||||
let timespec = unsafe { timespec.assume_init() };
|
||||
|
||||
// convert to nanoseconds, returning EOVERFLOW in case of overflow;
|
||||
// this is freelancing a bit from the spec but seems like it'll
|
||||
@@ -52,11 +54,12 @@ pub(crate) fn clock_time_get(clock_id: host::__wasi_clockid_t) -> Result<host::_
|
||||
};
|
||||
|
||||
// no `nix` wrapper for clock_getres, so we do it ourselves
|
||||
let mut timespec = unsafe { std::mem::uninitialized::<libc::timespec>() };
|
||||
let res = unsafe { libc::clock_gettime(clock_id, &mut timespec as *mut libc::timespec) };
|
||||
let mut timespec = MaybeUninit::<libc::timespec>::uninit();
|
||||
let res = unsafe { libc::clock_gettime(clock_id, timespec.as_mut_ptr()) };
|
||||
if res != 0 {
|
||||
return Err(host_impl::errno_from_nix(nix::errno::Errno::last()));
|
||||
}
|
||||
let timespec = unsafe { timespec.assume_init() };
|
||||
|
||||
// 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
|
||||
|
||||
@@ -6,6 +6,7 @@ use nix::libc::{self, c_long, c_void};
|
||||
use std::convert::TryInto;
|
||||
use std::ffi::CString;
|
||||
use std::fs::File;
|
||||
use std::mem::MaybeUninit;
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
|
||||
pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> {
|
||||
@@ -33,7 +34,7 @@ pub(crate) fn fd_readdir(
|
||||
host_buf: &mut [u8],
|
||||
cookie: host::__wasi_dircookie_t,
|
||||
) -> Result<usize> {
|
||||
use libc::{dirent, fdopendir, memcpy, readdir_r, rewinddir, seekdir};
|
||||
use libc::{dirent, fdopendir, readdir_r, rewinddir, seekdir};
|
||||
|
||||
let host_buf_ptr = host_buf.as_mut_ptr();
|
||||
let host_buf_len = host_buf.len();
|
||||
@@ -50,7 +51,7 @@ pub(crate) fn fd_readdir(
|
||||
unsafe { rewinddir(dir) };
|
||||
}
|
||||
|
||||
let mut entry_buf = unsafe { std::mem::uninitialized::<dirent>() };
|
||||
let mut entry_buf = MaybeUninit::<dirent>::uninit();
|
||||
let mut left = host_buf_len;
|
||||
let mut host_buf_offset: usize = 0;
|
||||
while left > 0 {
|
||||
@@ -61,13 +62,14 @@ pub(crate) fn fd_readdir(
|
||||
// replacing it with `readdir` call instead.
|
||||
// Also, `readdir_r` returns a positive int on failure, and doesn't
|
||||
// set the errno.
|
||||
let res = unsafe { readdir_r(dir, &mut entry_buf, &mut host_entry) };
|
||||
let res = unsafe { readdir_r(dir, entry_buf.as_mut_ptr(), &mut host_entry) };
|
||||
if res == -1 {
|
||||
return Err(host_impl::errno_from_nix(nix::errno::Errno::last()));
|
||||
}
|
||||
if host_entry.is_null() {
|
||||
break;
|
||||
}
|
||||
unsafe { entry_buf.assume_init() };
|
||||
let entry: host::__wasi_dirent_t = host_impl::dirent_from_host(&unsafe { *host_entry })?;
|
||||
|
||||
log::debug!("fd_readdir entry = {:?}", entry);
|
||||
@@ -85,9 +87,9 @@ pub(crate) fn fd_readdir(
|
||||
host_buf_offset += std::mem::size_of_val(&entry);
|
||||
let name_ptr = unsafe { *host_entry }.d_name.as_ptr();
|
||||
unsafe {
|
||||
memcpy(
|
||||
host_buf_ptr.offset(host_buf_offset.try_into()?) as *mut _,
|
||||
std::ptr::copy_nonoverlapping(
|
||||
name_ptr as *const _,
|
||||
host_buf_ptr.offset(host_buf_offset.try_into()?) as *mut _,
|
||||
name_len,
|
||||
)
|
||||
};
|
||||
|
||||
@@ -15,11 +15,15 @@ mod bsd;
|
||||
mod linux;
|
||||
|
||||
use crate::Result;
|
||||
use std::fs::File;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::path::Path;
|
||||
|
||||
pub(crate) fn dev_null() -> Result<File> {
|
||||
File::open("/dev/null").map_err(Into::into)
|
||||
OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.open("/dev/null")
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File> {
|
||||
|
||||
@@ -3,11 +3,15 @@ pub(crate) mod host_impl;
|
||||
pub(crate) mod hostcalls_impl;
|
||||
|
||||
use crate::Result;
|
||||
use std::fs::File;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::path::Path;
|
||||
|
||||
pub(crate) fn dev_null() -> Result<File> {
|
||||
File::open("NUL").map_err(Into::into)
|
||||
OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.open("NUL")
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File> {
|
||||
|
||||
Reference in New Issue
Block a user