From 68d4837b8cf0301bf3e833a4f234ec89ccfd9f56 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Wed, 14 Aug 2019 21:10:44 +0300 Subject: [PATCH 01/23] use Result in safe wrappers --- src/wasi_unstable/mod.rs | 552 ++++++++++++++++++++------------------- 1 file changed, 281 insertions(+), 271 deletions(-) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index 37bec24..5c45f3b 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -12,13 +12,14 @@ pub mod raw; use core::ffi::c_void; use core::mem::MaybeUninit; +use core::num::NonZeroU16; use raw::*; pub type Advice = __wasi_advice_t; pub type ClockId = __wasi_clockid_t; pub type Device = __wasi_device_t; pub type DirCookie = __wasi_dircookie_t; -pub type Errno = __wasi_errno_t; +pub type Errno = NonZeroU16; pub type EventRwFlags = __wasi_eventrwflags_t; pub type EventType = __wasi_eventtype_t; pub type ExitCode = __wasi_exitcode_t; @@ -63,83 +64,100 @@ pub const CLOCK_MONOTONIC: ClockId = __WASI_CLOCK_MONOTONIC; pub const CLOCK_PROCESS_CPUTIME_ID: ClockId = __WASI_CLOCK_PROCESS_CPUTIME_ID; pub const CLOCK_THREAD_CPUTIME_ID: ClockId = __WASI_CLOCK_THREAD_CPUTIME_ID; pub const DIRCOOKIE_START: DirCookie = __WASI_DIRCOOKIE_START; -pub const ESUCCESS: Errno = __WASI_ESUCCESS; -pub const E2BIG: Errno = __WASI_E2BIG; -pub const EACCES: Errno = __WASI_EACCES; -pub const EADDRINUSE: Errno = __WASI_EADDRINUSE; -pub const EADDRNOTAVAIL: Errno = __WASI_EADDRNOTAVAIL; -pub const EAFNOSUPPORT: Errno = __WASI_EAFNOSUPPORT; -pub const EAGAIN: Errno = __WASI_EAGAIN; -pub const EALREADY: Errno = __WASI_EALREADY; -pub const EBADF: Errno = __WASI_EBADF; -pub const EBADMSG: Errno = __WASI_EBADMSG; -pub const EBUSY: Errno = __WASI_EBUSY; -pub const ECANCELED: Errno = __WASI_ECANCELED; -pub const ECHILD: Errno = __WASI_ECHILD; -pub const ECONNABORTED: Errno = __WASI_ECONNABORTED; -pub const ECONNREFUSED: Errno = __WASI_ECONNREFUSED; -pub const ECONNRESET: Errno = __WASI_ECONNRESET; -pub const EDEADLK: Errno = __WASI_EDEADLK; -pub const EDESTADDRREQ: Errno = __WASI_EDESTADDRREQ; -pub const EDOM: Errno = __WASI_EDOM; -pub const EDQUOT: Errno = __WASI_EDQUOT; -pub const EEXIST: Errno = __WASI_EEXIST; -pub const EFAULT: Errno = __WASI_EFAULT; -pub const EFBIG: Errno = __WASI_EFBIG; -pub const EHOSTUNREACH: Errno = __WASI_EHOSTUNREACH; -pub const EIDRM: Errno = __WASI_EIDRM; -pub const EILSEQ: Errno = __WASI_EILSEQ; -pub const EINPROGRESS: Errno = __WASI_EINPROGRESS; -pub const EINTR: Errno = __WASI_EINTR; -pub const EINVAL: Errno = __WASI_EINVAL; -pub const EIO: Errno = __WASI_EIO; -pub const EISCONN: Errno = __WASI_EISCONN; -pub const EISDIR: Errno = __WASI_EISDIR; -pub const ELOOP: Errno = __WASI_ELOOP; -pub const EMFILE: Errno = __WASI_EMFILE; -pub const EMLINK: Errno = __WASI_EMLINK; -pub const EMSGSIZE: Errno = __WASI_EMSGSIZE; -pub const EMULTIHOP: Errno = __WASI_EMULTIHOP; -pub const ENAMETOOLONG: Errno = __WASI_ENAMETOOLONG; -pub const ENETDOWN: Errno = __WASI_ENETDOWN; -pub const ENETRESET: Errno = __WASI_ENETRESET; -pub const ENETUNREACH: Errno = __WASI_ENETUNREACH; -pub const ENFILE: Errno = __WASI_ENFILE; -pub const ENOBUFS: Errno = __WASI_ENOBUFS; -pub const ENODEV: Errno = __WASI_ENODEV; -pub const ENOENT: Errno = __WASI_ENOENT; -pub const ENOEXEC: Errno = __WASI_ENOEXEC; -pub const ENOLCK: Errno = __WASI_ENOLCK; -pub const ENOLINK: Errno = __WASI_ENOLINK; -pub const ENOMEM: Errno = __WASI_ENOMEM; -pub const ENOMSG: Errno = __WASI_ENOMSG; -pub const ENOPROTOOPT: Errno = __WASI_ENOPROTOOPT; -pub const ENOSPC: Errno = __WASI_ENOSPC; -pub const ENOSYS: Errno = __WASI_ENOSYS; -pub const ENOTCONN: Errno = __WASI_ENOTCONN; -pub const ENOTDIR: Errno = __WASI_ENOTDIR; -pub const ENOTEMPTY: Errno = __WASI_ENOTEMPTY; -pub const ENOTRECOVERABLE: Errno = __WASI_ENOTRECOVERABLE; -pub const ENOTSOCK: Errno = __WASI_ENOTSOCK; -pub const ENOTSUP: Errno = __WASI_ENOTSUP; -pub const ENOTTY: Errno = __WASI_ENOTTY; -pub const ENXIO: Errno = __WASI_ENXIO; -pub const EOVERFLOW: Errno = __WASI_EOVERFLOW; -pub const EOWNERDEAD: Errno = __WASI_EOWNERDEAD; -pub const EPERM: Errno = __WASI_EPERM; -pub const EPIPE: Errno = __WASI_EPIPE; -pub const EPROTO: Errno = __WASI_EPROTO; -pub const EPROTONOSUPPORT: Errno = __WASI_EPROTONOSUPPORT; -pub const EPROTOTYPE: Errno = __WASI_EPROTOTYPE; -pub const ERANGE: Errno = __WASI_ERANGE; -pub const EROFS: Errno = __WASI_EROFS; -pub const ESPIPE: Errno = __WASI_ESPIPE; -pub const ESRCH: Errno = __WASI_ESRCH; -pub const ESTALE: Errno = __WASI_ESTALE; -pub const ETIMEDOUT: Errno = __WASI_ETIMEDOUT; -pub const ETXTBSY: Errno = __WASI_ETXTBSY; -pub const EXDEV: Errno = __WASI_EXDEV; -pub const ENOTCAPABLE: Errno = __WASI_ENOTCAPABLE; + +// A hucky wasy to assert that `__WASI_ESUCCESS` equals to 0 +#[deny(const_err)] +const _ASSERT: u16 = 0 - __WASI_ESUCCESS; + +macro_rules! errno_set { + ($($safe_const:ident => $raw_const:ident,)*) => { + $( + pub const $safe_const: Errno = unsafe { + NonZeroU16::new_unchecked($raw_const) + }; + )* + }; +} + +errno_set!( + E2BIG => __WASI_E2BIG, + EACCES => __WASI_EACCES, + EADDRINUSE => __WASI_EADDRINUSE, + EADDRNOTAVAIL => __WASI_EADDRNOTAVAIL, + EAFNOSUPPORT => __WASI_EAFNOSUPPORT, + EAGAIN => __WASI_EAGAIN, + EALREADY => __WASI_EALREADY, + EBADF => __WASI_EBADF, + EBADMSG => __WASI_EBADMSG, + EBUSY => __WASI_EBUSY, + ECANCELED => __WASI_ECANCELED, + ECHILD => __WASI_ECHILD, + ECONNABORTED => __WASI_ECONNABORTED, + ECONNREFUSED => __WASI_ECONNREFUSED, + ECONNRESET => __WASI_ECONNRESET, + EDEADLK => __WASI_EDEADLK, + EDESTADDRREQ => __WASI_EDESTADDRREQ, + EDOM => __WASI_EDOM, + EDQUOT => __WASI_EDQUOT, + EEXIST => __WASI_EEXIST, + EFAULT => __WASI_EFAULT, + EFBIG => __WASI_EFBIG, + EHOSTUNREACH => __WASI_EHOSTUNREACH, + EIDRM => __WASI_EIDRM, + EILSEQ => __WASI_EILSEQ, + EINPROGRESS => __WASI_EINPROGRESS, + EINTR => __WASI_EINTR, + EINVAL => __WASI_EINVAL, + EIO => __WASI_EIO, + EISCONN => __WASI_EISCONN, + EISDIR => __WASI_EISDIR, + ELOOP => __WASI_ELOOP, + EMFILE => __WASI_EMFILE, + EMLINK => __WASI_EMLINK, + EMSGSIZE => __WASI_EMSGSIZE, + EMULTIHOP => __WASI_EMULTIHOP, + ENAMETOOLONG => __WASI_ENAMETOOLONG, + ENETDOWN => __WASI_ENETDOWN, + ENETRESET => __WASI_ENETRESET, + ENETUNREACH => __WASI_ENETUNREACH, + ENFILE => __WASI_ENFILE, + ENOBUFS => __WASI_ENOBUFS, + ENODEV => __WASI_ENODEV, + ENOENT => __WASI_ENOENT, + ENOEXEC => __WASI_ENOEXEC, + ENOLCK => __WASI_ENOLCK, + ENOLINK => __WASI_ENOLINK, + ENOMEM => __WASI_ENOMEM, + ENOMSG => __WASI_ENOMSG, + ENOPROTOOPT => __WASI_ENOPROTOOPT, + ENOSPC => __WASI_ENOSPC, + ENOSYS => __WASI_ENOSYS, + ENOTCONN => __WASI_ENOTCONN, + ENOTDIR => __WASI_ENOTDIR, + ENOTEMPTY => __WASI_ENOTEMPTY, + ENOTRECOVERABLE => __WASI_ENOTRECOVERABLE, + ENOTSOCK => __WASI_ENOTSOCK, + ENOTSUP => __WASI_ENOTSUP, + ENOTTY => __WASI_ENOTTY, + ENXIO => __WASI_ENXIO, + EOVERFLOW => __WASI_EOVERFLOW, + EOWNERDEAD => __WASI_EOWNERDEAD, + EPERM => __WASI_EPERM, + EPIPE => __WASI_EPIPE, + EPROTO => __WASI_EPROTO, + EPROTONOSUPPORT => __WASI_EPROTONOSUPPORT, + EPROTOTYPE => __WASI_EPROTOTYPE, + ERANGE => __WASI_ERANGE, + EROFS => __WASI_EROFS, + ESPIPE => __WASI_ESPIPE, + ESRCH => __WASI_ESRCH, + ESTALE => __WASI_ESTALE, + ETIMEDOUT => __WASI_ETIMEDOUT, + ETXTBSY => __WASI_ETXTBSY, + EXDEV => __WASI_EXDEV, + ENOTCAPABLE => __WASI_ENOTCAPABLE, +); + pub const EVENT_FD_READWRITE_HANGUP: EventRwFlags = __WASI_EVENT_FD_READWRITE_HANGUP; pub const EVENTTYPE_CLOCK: EventType = __WASI_EVENTTYPE_CLOCK; pub const EVENTTYPE_FD_READ: EventType = __WASI_EVENTTYPE_FD_READ; @@ -236,134 +254,140 @@ pub const WHENCE_CUR: Whence = __WASI_WHENCE_CUR; pub const WHENCE_END: Whence = __WASI_WHENCE_END; pub const WHENCE_SET: Whence = __WASI_WHENCE_SET; -pub fn clock_res_get(clock_id: ClockId) -> (Errno, Timestamp) { +macro_rules! unsafe_wrap0 { + {$f:expr} => { + unsafe { + if let Some(code) = NonZeroU16::new($f) { + Err(code) + } else { + Ok(()) + } + } + }; +} + +macro_rules! unsafe_wrap1 { + {$var:ident, $f:expr,} => { + unsafe { + if let Some(code) = NonZeroU16::new($f) { + Err(code) + } else { + Ok($var.assume_init()) + } + } + }; +} + +pub fn clock_res_get(clock_id: ClockId) -> Result { let mut resolution = MaybeUninit::::uninit(); - unsafe { - ( - __wasi_clock_res_get(clock_id, resolution.as_mut_ptr()), - resolution.assume_init(), - ) + unsafe_wrap1!{ + resolution, + __wasi_clock_res_get(clock_id, resolution.as_mut_ptr()), } } -pub fn clock_time_get(clock_id: ClockId, precision: Timestamp) -> (Errno, Timestamp) { +pub fn clock_time_get(clock_id: ClockId, precision: Timestamp) -> Result { let mut time = MaybeUninit::::uninit(); - unsafe { - ( - __wasi_clock_time_get(clock_id, precision, time.as_mut_ptr()), - time.assume_init(), - ) + unsafe_wrap1!{ + time, + __wasi_clock_time_get(clock_id, precision, time.as_mut_ptr()), } } -pub fn fd_pread(fd: Fd, iovs: &[IoVec], offset: FileSize) -> (Errno, usize) { +pub fn fd_pread(fd: Fd, iovs: &[IoVec], offset: FileSize) -> Result { let mut nread = MaybeUninit::::uninit(); - unsafe { - ( - __wasi_fd_pread(fd, iovs.as_ptr(), iovs.len(), offset, nread.as_mut_ptr()), - nread.assume_init(), - ) + unsafe_wrap1!{ + nread, + __wasi_fd_pread(fd, iovs.as_ptr(), iovs.len(), offset, nread.as_mut_ptr()), } } -pub fn fd_pwrite(fd: Fd, iovs: &[CIoVec], offset: FileSize) -> (Errno, usize) { +pub fn fd_pwrite(fd: Fd, iovs: &[CIoVec], offset: FileSize) -> Result { let mut nwritten = MaybeUninit::::uninit(); - unsafe { - ( - __wasi_fd_pwrite(fd, iovs.as_ptr(), iovs.len(), offset, nwritten.as_mut_ptr()), - nwritten.assume_init(), - ) + unsafe_wrap1!{ + nwritten, + __wasi_fd_pwrite(fd, iovs.as_ptr(), iovs.len(), offset, nwritten.as_mut_ptr()), } } -pub fn random_get(buf: &mut [u8]) -> Errno { - unsafe { __wasi_random_get(buf.as_mut_ptr() as *mut c_void, buf.len()) } +pub fn random_get(buf: &mut [u8]) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_random_get(buf.as_mut_ptr() as *mut c_void, buf.len()) } } -pub fn fd_close(fd: Fd) -> Errno { - unsafe { __wasi_fd_close(fd) } +pub fn fd_close(fd: Fd) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_fd_close(fd) } } -pub fn fd_datasync(fd: Fd) -> Errno { - unsafe { __wasi_fd_datasync(fd) } +pub fn fd_datasync(fd: Fd) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_fd_datasync(fd) } } -pub fn fd_read(fd: Fd, iovs: &[IoVec]) -> (Errno, usize) { +pub fn fd_read(fd: Fd, iovs: &[IoVec]) -> Result { let mut nread = MaybeUninit::::uninit(); - unsafe { - ( - __wasi_fd_read(fd, iovs.as_ptr(), iovs.len(), nread.as_mut_ptr()), - nread.assume_init(), - ) + unsafe_wrap1!{ + nread, + __wasi_fd_read(fd, iovs.as_ptr(), iovs.len(), nread.as_mut_ptr()), } } -pub fn fd_renumber(from: Fd, to: Fd) -> Errno { - unsafe { __wasi_fd_renumber(from, to) } +pub fn fd_renumber(from: Fd, to: Fd) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_fd_renumber(from, to) } } -pub fn fd_seek(fd: Fd, offset: FileDelta, whence: Whence) -> (Errno, FileSize) { +pub fn fd_seek(fd: Fd, offset: FileDelta, whence: Whence) -> Result { let mut newoffset = MaybeUninit::::uninit(); - unsafe { - ( - __wasi_fd_seek(fd, offset, whence, newoffset.as_mut_ptr()), - newoffset.assume_init(), - ) + unsafe_wrap1!{ + newoffset, + __wasi_fd_seek(fd, offset, whence, newoffset.as_mut_ptr()), } } -pub fn fd_tell(fd: Fd) -> (Errno, FileSize) { +pub fn fd_tell(fd: Fd) -> Result { let mut newoffset = MaybeUninit::::uninit(); - unsafe { - ( - __wasi_fd_tell(fd, newoffset.as_mut_ptr()), - newoffset.assume_init(), - ) + unsafe_wrap1!{ + newoffset, + __wasi_fd_tell(fd, newoffset.as_mut_ptr()), } } -pub fn fd_fdstat_get(fd: Fd) -> (Errno, FdStat) { +pub fn fd_fdstat_get(fd: Fd) -> Result { let mut buf = MaybeUninit::::uninit(); - unsafe { - ( - __wasi_fd_fdstat_get(fd, buf.as_mut_ptr()), - buf.assume_init(), - ) + unsafe_wrap1!{ + buf, + __wasi_fd_fdstat_get(fd, buf.as_mut_ptr()), } } -pub fn fd_fdstat_set_flags(fd: Fd, flags: FdFlags) -> Errno { - unsafe { __wasi_fd_fdstat_set_flags(fd, flags) } +pub fn fd_fdstat_set_flags(fd: Fd, flags: FdFlags) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_fd_fdstat_set_flags(fd, flags) } } -pub fn fd_fdstat_set_rights(fd: Fd, fs_rights_base: Rights, fs_rights_inheriting: Rights) -> Errno { - unsafe { __wasi_fd_fdstat_set_rights(fd, fs_rights_base, fs_rights_inheriting) } +pub fn fd_fdstat_set_rights(fd: Fd, fs_rights_base: Rights, fs_rights_inheriting: Rights) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_fd_fdstat_set_rights(fd, fs_rights_base, fs_rights_inheriting) } } -pub fn fd_sync(fd: Fd) -> Errno { - unsafe { __wasi_fd_sync(fd) } +pub fn fd_sync(fd: Fd) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_fd_sync(fd) } } -pub fn fd_write(fd: Fd, iovs: &[CIoVec]) -> (Errno, usize) { +pub fn fd_write(fd: Fd, iovs: &[CIoVec]) -> Result { let mut nwritten = MaybeUninit::::uninit(); - unsafe { - ( - __wasi_fd_write(fd, iovs.as_ptr(), iovs.len(), nwritten.as_mut_ptr()), - nwritten.assume_init(), - ) + unsafe_wrap1!{ + nwritten, + __wasi_fd_write(fd, iovs.as_ptr(), iovs.len(), nwritten.as_mut_ptr()), } } -pub fn fd_advise(fd: Fd, offset: FileSize, len: FileSize, advice: Advice) -> Errno { - unsafe { __wasi_fd_advise(fd, offset, len, advice) } +pub fn fd_advise(fd: Fd, offset: FileSize, len: FileSize, advice: Advice) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_fd_advise(fd, offset, len, advice) } } -pub fn fd_allocate(fd: Fd, offset: FileSize, len: FileSize) -> Errno { - unsafe { __wasi_fd_allocate(fd, offset, len) } +pub fn fd_allocate(fd: Fd, offset: FileSize, len: FileSize) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_fd_allocate(fd, offset, len) } } -pub fn path_create_directory(fd: Fd, path: &[u8]) -> Errno { - unsafe { __wasi_path_create_directory(fd, path.as_ptr(), path.len()) } +pub fn path_create_directory(fd: Fd, path: &[u8]) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_path_create_directory(fd, path.as_ptr(), path.len()) } } pub fn path_link( @@ -372,8 +396,8 @@ pub fn path_link( old_path: &[u8], new_fd: Fd, new_path: &[u8], -) -> Errno { - unsafe { +) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_path_link( old_fd, old_flags, @@ -394,61 +418,55 @@ pub fn path_open( fs_rights_base: Rights, fs_rights_inheriting: Rights, fs_flags: FdFlags, -) -> (Errno, Fd) { +) -> Result { let mut fd = MaybeUninit::::uninit(); - unsafe { - ( - __wasi_path_open( - dirfd, - dirflags, - path.as_ptr(), - path.len(), - oflags, - fs_rights_base, - fs_rights_inheriting, - fs_flags, - fd.as_mut_ptr(), - ), - fd.assume_init(), - ) + unsafe_wrap1!{ + fd, + __wasi_path_open( + dirfd, + dirflags, + path.as_ptr(), + path.len(), + oflags, + fs_rights_base, + fs_rights_inheriting, + fs_flags, + fd.as_mut_ptr(), + ), } } -pub fn fd_readdir(fd: Fd, buf: &mut [u8], cookie: DirCookie) -> (Errno, usize) { +pub fn fd_readdir(fd: Fd, buf: &mut [u8], cookie: DirCookie) -> Result { let mut bufused = MaybeUninit::::uninit(); - unsafe { - ( - __wasi_fd_readdir( - fd, - buf.as_mut_ptr() as *mut c_void, - buf.len(), - cookie, - bufused.as_mut_ptr(), - ), - bufused.assume_init(), - ) + unsafe_wrap1!{ + bufused, + __wasi_fd_readdir( + fd, + buf.as_mut_ptr() as *mut c_void, + buf.len(), + cookie, + bufused.as_mut_ptr(), + ), } } -pub fn path_readlink(fd: Fd, path: &[u8], buf: &mut [u8]) -> (Errno, usize) { +pub fn path_readlink(fd: Fd, path: &[u8], buf: &mut [u8]) -> Result { let mut bufused = MaybeUninit::::uninit(); - unsafe { - ( - __wasi_path_readlink( - fd, - path.as_ptr(), - path.len(), - buf.as_mut_ptr(), - buf.len(), - bufused.as_mut_ptr(), - ), - bufused.assume_init(), - ) + unsafe_wrap1!{ + bufused, + __wasi_path_readlink( + fd, + path.as_ptr(), + path.len(), + buf.as_mut_ptr(), + buf.len(), + bufused.as_mut_ptr(), + ), } } -pub fn path_rename(old_fd: Fd, old_path: &[u8], new_fd: Fd, new_path: &[u8]) -> Errno { - unsafe { +pub fn path_rename(old_fd: Fd, old_path: &[u8], new_fd: Fd, new_path: &[u8]) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_path_rename( old_fd, old_path.as_ptr(), @@ -460,13 +478,11 @@ pub fn path_rename(old_fd: Fd, old_path: &[u8], new_fd: Fd, new_path: &[u8]) -> } } -pub fn fd_filestat_get(fd: Fd) -> (Errno, FileStat) { +pub fn fd_filestat_get(fd: Fd) -> Result { let mut buf = MaybeUninit::::uninit(); - unsafe { - ( - __wasi_fd_filestat_get(fd, buf.as_mut_ptr()), - buf.assume_init(), - ) + unsafe_wrap1!{ + buf, + __wasi_fd_filestat_get(fd, buf.as_mut_ptr()), } } @@ -475,21 +491,19 @@ pub fn fd_filestat_set_times( st_atim: Timestamp, st_mtim: Timestamp, fstflags: FstFlags, -) -> Errno { - unsafe { __wasi_fd_filestat_set_times(fd, st_atim, st_mtim, fstflags) } +) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_fd_filestat_set_times(fd, st_atim, st_mtim, fstflags) } } -pub fn fd_filestat_set_size(fd: Fd, st_size: FileSize) -> Errno { - unsafe { __wasi_fd_filestat_set_size(fd, st_size) } +pub fn fd_filestat_set_size(fd: Fd, st_size: FileSize) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_fd_filestat_set_size(fd, st_size) } } -pub fn path_filestat_get(fd: Fd, flags: LookupFlags, path: &[u8]) -> (Errno, FileStat) { +pub fn path_filestat_get(fd: Fd, flags: LookupFlags, path: &[u8]) -> Result { let mut buf = MaybeUninit::::uninit(); - unsafe { - ( - __wasi_path_filestat_get(fd, flags, path.as_ptr(), path.len(), buf.as_mut_ptr()), - buf.assume_init(), - ) + unsafe_wrap1!{ + buf, + __wasi_path_filestat_get(fd, flags, path.as_ptr(), path.len(), buf.as_mut_ptr()), } } @@ -500,8 +514,8 @@ pub fn path_filestat_set_times( st_atim: Timestamp, st_mtim: Timestamp, fstflags: FstFlags, -) -> Errno { - unsafe { +) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_path_filestat_set_times( fd, flags, @@ -514,8 +528,8 @@ pub fn path_filestat_set_times( } } -pub fn path_symlink(old_path: &[u8], fd: Fd, new_path: &[u8]) -> Errno { - unsafe { +pub fn path_symlink(old_path: &[u8], fd: Fd, new_path: &[u8]) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_path_symlink( old_path.as_ptr(), old_path.len(), @@ -526,27 +540,25 @@ pub fn path_symlink(old_path: &[u8], fd: Fd, new_path: &[u8]) -> Errno { } } -pub fn path_unlink_file(fd: Fd, path: &[u8]) -> Errno { - unsafe { __wasi_path_unlink_file(fd, path.as_ptr(), path.len()) } +pub fn path_unlink_file(fd: Fd, path: &[u8]) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_path_unlink_file(fd, path.as_ptr(), path.len()) } } -pub fn path_remove_directory(fd: Fd, path: &[u8]) -> Errno { - unsafe { __wasi_path_remove_directory(fd, path.as_ptr(), path.len()) } +pub fn path_remove_directory(fd: Fd, path: &[u8]) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_path_remove_directory(fd, path.as_ptr(), path.len()) } } -pub fn poll_oneoff(in_: &[Subscription], out: &mut [Event]) -> (Errno, usize) { +pub fn poll_oneoff(in_: &[Subscription], out: &mut [Event]) -> Result { assert!(out.len() >= in_.len()); let mut nevents = MaybeUninit::::uninit(); - unsafe { - ( - __wasi_poll_oneoff( - in_.as_ptr(), - out.as_mut_ptr(), - in_.len(), - nevents.as_mut_ptr(), - ), - nevents.assume_init(), - ) + unsafe_wrap1!{ + nevents, + __wasi_poll_oneoff( + in_.as_ptr(), + out.as_mut_ptr(), + in_.len(), + nevents.as_mut_ptr(), + ), } } @@ -554,65 +566,63 @@ pub fn proc_exit(rval: ExitCode) { unsafe { __wasi_proc_exit(rval) } } -pub fn proc_raise(sig: Signal) -> Errno { - unsafe { __wasi_proc_raise(sig) } +pub fn proc_raise(sig: Signal) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_proc_raise(sig) } } -pub fn sock_recv(sock: Fd, ri_data: &[IoVec], ri_flags: RiFlags) -> (Errno, usize, RoFlags) { +pub fn sock_recv(sock: Fd, ri_data: &[IoVec], ri_flags: RiFlags) -> Result<(usize, RoFlags), Errno> { let mut ro_datalen = MaybeUninit::::uninit(); let mut ro_flags = MaybeUninit::::uninit(); + unsafe { - ( - __wasi_sock_recv( - sock, - ri_data.as_ptr(), - ri_data.len(), - ri_flags, - ro_datalen.as_mut_ptr(), - ro_flags.as_mut_ptr(), - ), - ro_datalen.assume_init(), - ro_flags.assume_init(), - ) + let r = __wasi_sock_recv( + sock, + ri_data.as_ptr(), + ri_data.len(), + ri_flags, + ro_datalen.as_mut_ptr(), + ro_flags.as_mut_ptr(), + ); + if let Some(code) = NonZeroU16::new(r) { + Err(code) + } else { + Ok((ro_datalen.assume_init(), ro_flags.assume_init())) + } } } -pub fn sock_send(sock: Fd, si_data: &[CIoVec], si_flags: SiFlags) -> (Errno, usize) { +pub fn sock_send(sock: Fd, si_data: &[CIoVec], si_flags: SiFlags) -> Result { let mut so_datalen = MaybeUninit::::uninit(); - unsafe { - ( - __wasi_sock_send( - sock, - si_data.as_ptr(), - si_data.len(), - si_flags, - so_datalen.as_mut_ptr(), - ), - so_datalen.assume_init(), - ) + unsafe_wrap1!{ + so_datalen, + __wasi_sock_send( + sock, + si_data.as_ptr(), + si_data.len(), + si_flags, + so_datalen.as_mut_ptr(), + ), } } -pub fn sock_shutdown(sock: Fd, how: SdFlags) -> Errno { - unsafe { __wasi_sock_shutdown(sock, how) } +pub fn sock_shutdown(sock: Fd, how: SdFlags) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_sock_shutdown(sock, how) } } -pub fn sched_yield() -> Errno { - unsafe { __wasi_sched_yield() } +pub fn sched_yield() -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_sched_yield() } } -pub fn fd_prestat_get(fd: Fd) -> (Errno, Prestat) { +pub fn fd_prestat_get(fd: Fd) -> Result { let mut buf = MaybeUninit::::uninit(); - unsafe { - ( - __wasi_fd_prestat_get(fd, buf.as_mut_ptr()), - buf.assume_init(), - ) + unsafe_wrap1!{ + buf, + __wasi_fd_prestat_get(fd, buf.as_mut_ptr()), } } -pub fn fd_prestat_dir_name(fd: Fd, path: &mut [u8]) -> Errno { - unsafe { __wasi_fd_prestat_dir_name(fd, path.as_mut_ptr(), path.len()) } +pub fn fd_prestat_dir_name(fd: Fd, path: &mut [u8]) -> Result<(), Errno> { + unsafe_wrap0!{ __wasi_fd_prestat_dir_name(fd, path.as_mut_ptr(), path.len()) } } // TODO: Safe interfaces to the args and environ functions From ee31c89bdc811899432aae3ecbecd6928eb0b678 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Thu, 15 Aug 2019 20:57:18 +0300 Subject: [PATCH 02/23] improve unsafe_wrap macro, add new Event type, rename Errno to Error --- src/wasi_unstable/mod.rs | 243 +++++++++++++++------------------------ 1 file changed, 94 insertions(+), 149 deletions(-) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index 5c45f3b..1122100 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -19,7 +19,7 @@ pub type Advice = __wasi_advice_t; pub type ClockId = __wasi_clockid_t; pub type Device = __wasi_device_t; pub type DirCookie = __wasi_dircookie_t; -pub type Errno = NonZeroU16; +pub type Error = NonZeroU16; pub type EventRwFlags = __wasi_eventrwflags_t; pub type EventType = __wasi_eventtype_t; pub type ExitCode = __wasi_exitcode_t; @@ -50,9 +50,23 @@ pub type FileStat = __wasi_filestat_t; pub type CIoVec = __wasi_ciovec_t; pub type IoVec = __wasi_iovec_t; pub type Subscription = __wasi_subscription_t; -pub type Event = __wasi_event_t; pub type Prestat = __wasi_prestat_t; +// should have exactly the same layout as __wasi_event_t +// TODO: add a more thorough test +#[repr(C)] +#[derive(Copy, Clone)] +pub struct Event { + pub userdata: __wasi_userdata_t, + pub res: Result<(), Error>, + pub type_: __wasi_eventtype_t, + pub u: __wasi_event_u, +} + +// Assert that `Event` and `__wasi_event_t` have the same size +const _ASSERT1: [(); 32] = [(); core::mem::size_of::<__wasi_event_t>()]; +const _ASSERT2: [(); 32] = [(); core::mem::size_of::()]; + pub const ADVICE_NORMAL: Advice = __WASI_ADVICE_NORMAL; pub const ADVICE_SEQUENTIAL: Advice = __WASI_ADVICE_SEQUENTIAL; pub const ADVICE_RANDOM: Advice = __WASI_ADVICE_RANDOM; @@ -65,14 +79,13 @@ pub const CLOCK_PROCESS_CPUTIME_ID: ClockId = __WASI_CLOCK_PROCESS_CPUTIME_ID; pub const CLOCK_THREAD_CPUTIME_ID: ClockId = __WASI_CLOCK_THREAD_CPUTIME_ID; pub const DIRCOOKIE_START: DirCookie = __WASI_DIRCOOKIE_START; -// A hucky wasy to assert that `__WASI_ESUCCESS` equals to 0 -#[deny(const_err)] -const _ASSERT: u16 = 0 - __WASI_ESUCCESS; +// Aassert that `__WASI_ESUCCESS` equals to 0 +const _ASSERT3: [(); 0] = [(); __WASI_ESUCCESS as usize]; macro_rules! errno_set { ($($safe_const:ident => $raw_const:ident,)*) => { $( - pub const $safe_const: Errno = unsafe { + pub const $safe_const: Error = unsafe { NonZeroU16::new_unchecked($raw_const) }; )* @@ -266,127 +279,99 @@ macro_rules! unsafe_wrap0 { }; } -macro_rules! unsafe_wrap1 { - {$var:ident, $f:expr,} => { +macro_rules! unsafe_wrap { + {$f:ident($($args:expr),* $(,)?)} => { + let mut t = MaybeUninit::uninit(); unsafe { - if let Some(code) = NonZeroU16::new($f) { + let r = $f($($args,)* t.as_mut_ptr()); + if let Some(code) = NonZeroU16::new(r) { Err(code) } else { - Ok($var.assume_init()) + Ok(t.assume_init()) } } }; } -pub fn clock_res_get(clock_id: ClockId) -> Result { - let mut resolution = MaybeUninit::::uninit(); - unsafe_wrap1!{ - resolution, - __wasi_clock_res_get(clock_id, resolution.as_mut_ptr()), - } +pub fn clock_res_get(clock_id: ClockId) -> Result { + unsafe_wrap!{ __wasi_clock_res_get(clock_id) } } -pub fn clock_time_get(clock_id: ClockId, precision: Timestamp) -> Result { - let mut time = MaybeUninit::::uninit(); - unsafe_wrap1!{ - time, - __wasi_clock_time_get(clock_id, precision, time.as_mut_ptr()), - } +pub fn clock_time_get(clock_id: ClockId, precision: Timestamp) -> Result { + unsafe_wrap!{ __wasi_clock_time_get(clock_id, precision) } } -pub fn fd_pread(fd: Fd, iovs: &[IoVec], offset: FileSize) -> Result { - let mut nread = MaybeUninit::::uninit(); - unsafe_wrap1!{ - nread, - __wasi_fd_pread(fd, iovs.as_ptr(), iovs.len(), offset, nread.as_mut_ptr()), +pub fn fd_pread(fd: Fd, iovs: &[IoVec], offset: FileSize) -> Result { + unsafe_wrap!{ + __wasi_fd_pread(fd, iovs.as_ptr(), iovs.len(), offset) } } -pub fn fd_pwrite(fd: Fd, iovs: &[CIoVec], offset: FileSize) -> Result { - let mut nwritten = MaybeUninit::::uninit(); - unsafe_wrap1!{ - nwritten, - __wasi_fd_pwrite(fd, iovs.as_ptr(), iovs.len(), offset, nwritten.as_mut_ptr()), +pub fn fd_pwrite(fd: Fd, iovs: &[CIoVec], offset: FileSize) -> Result { + unsafe_wrap!{ + __wasi_fd_pwrite(fd, iovs.as_ptr(), iovs.len(), offset) } } -pub fn random_get(buf: &mut [u8]) -> Result<(), Errno> { +pub fn random_get(buf: &mut [u8]) -> Result<(), Error> { unsafe_wrap0!{ __wasi_random_get(buf.as_mut_ptr() as *mut c_void, buf.len()) } } -pub fn fd_close(fd: Fd) -> Result<(), Errno> { +pub fn fd_close(fd: Fd) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_close(fd) } } -pub fn fd_datasync(fd: Fd) -> Result<(), Errno> { +pub fn fd_datasync(fd: Fd) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_datasync(fd) } } -pub fn fd_read(fd: Fd, iovs: &[IoVec]) -> Result { - let mut nread = MaybeUninit::::uninit(); - unsafe_wrap1!{ - nread, - __wasi_fd_read(fd, iovs.as_ptr(), iovs.len(), nread.as_mut_ptr()), +pub fn fd_read(fd: Fd, iovs: &[IoVec]) -> Result { + unsafe_wrap!{ + __wasi_fd_read(fd, iovs.as_ptr(), iovs.len()) } } -pub fn fd_renumber(from: Fd, to: Fd) -> Result<(), Errno> { +pub fn fd_renumber(from: Fd, to: Fd) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_renumber(from, to) } } -pub fn fd_seek(fd: Fd, offset: FileDelta, whence: Whence) -> Result { - let mut newoffset = MaybeUninit::::uninit(); - unsafe_wrap1!{ - newoffset, - __wasi_fd_seek(fd, offset, whence, newoffset.as_mut_ptr()), - } +pub fn fd_seek(fd: Fd, offset: FileDelta, whence: Whence) -> Result { + unsafe_wrap!{ __wasi_fd_seek(fd, offset, whence) } } -pub fn fd_tell(fd: Fd) -> Result { - let mut newoffset = MaybeUninit::::uninit(); - unsafe_wrap1!{ - newoffset, - __wasi_fd_tell(fd, newoffset.as_mut_ptr()), - } +pub fn fd_tell(fd: Fd) -> Result { + unsafe_wrap!{ __wasi_fd_tell(fd) } } -pub fn fd_fdstat_get(fd: Fd) -> Result { - let mut buf = MaybeUninit::::uninit(); - unsafe_wrap1!{ - buf, - __wasi_fd_fdstat_get(fd, buf.as_mut_ptr()), - } +pub fn fd_fdstat_get(fd: Fd) -> Result { + unsafe_wrap!{ __wasi_fd_fdstat_get(fd) } } -pub fn fd_fdstat_set_flags(fd: Fd, flags: FdFlags) -> Result<(), Errno> { +pub fn fd_fdstat_set_flags(fd: Fd, flags: FdFlags) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_fdstat_set_flags(fd, flags) } } -pub fn fd_fdstat_set_rights(fd: Fd, fs_rights_base: Rights, fs_rights_inheriting: Rights) -> Result<(), Errno> { +pub fn fd_fdstat_set_rights(fd: Fd, fs_rights_base: Rights, fs_rights_inheriting: Rights) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_fdstat_set_rights(fd, fs_rights_base, fs_rights_inheriting) } } -pub fn fd_sync(fd: Fd) -> Result<(), Errno> { +pub fn fd_sync(fd: Fd) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_sync(fd) } } -pub fn fd_write(fd: Fd, iovs: &[CIoVec]) -> Result { - let mut nwritten = MaybeUninit::::uninit(); - unsafe_wrap1!{ - nwritten, - __wasi_fd_write(fd, iovs.as_ptr(), iovs.len(), nwritten.as_mut_ptr()), - } +pub fn fd_write(fd: Fd, iovs: &[CIoVec]) -> Result { + unsafe_wrap!{ __wasi_fd_write(fd, iovs.as_ptr(), iovs.len()) } } -pub fn fd_advise(fd: Fd, offset: FileSize, len: FileSize, advice: Advice) -> Result<(), Errno> { +pub fn fd_advise(fd: Fd, offset: FileSize, len: FileSize, advice: Advice) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_advise(fd, offset, len, advice) } } -pub fn fd_allocate(fd: Fd, offset: FileSize, len: FileSize) -> Result<(), Errno> { +pub fn fd_allocate(fd: Fd, offset: FileSize, len: FileSize) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_allocate(fd, offset, len) } } -pub fn path_create_directory(fd: Fd, path: &[u8]) -> Result<(), Errno> { +pub fn path_create_directory(fd: Fd, path: &[u8]) -> Result<(), Error> { unsafe_wrap0!{ __wasi_path_create_directory(fd, path.as_ptr(), path.len()) } } @@ -396,7 +381,7 @@ pub fn path_link( old_path: &[u8], new_fd: Fd, new_path: &[u8], -) -> Result<(), Errno> { +) -> Result<(), Error> { unsafe_wrap0!{ __wasi_path_link( old_fd, @@ -418,10 +403,8 @@ pub fn path_open( fs_rights_base: Rights, fs_rights_inheriting: Rights, fs_flags: FdFlags, -) -> Result { - let mut fd = MaybeUninit::::uninit(); - unsafe_wrap1!{ - fd, +) -> Result { + unsafe_wrap!{ __wasi_path_open( dirfd, dirflags, @@ -431,41 +414,23 @@ pub fn path_open( fs_rights_base, fs_rights_inheriting, fs_flags, - fd.as_mut_ptr(), - ), + ) } } -pub fn fd_readdir(fd: Fd, buf: &mut [u8], cookie: DirCookie) -> Result { - let mut bufused = MaybeUninit::::uninit(); - unsafe_wrap1!{ - bufused, - __wasi_fd_readdir( - fd, - buf.as_mut_ptr() as *mut c_void, - buf.len(), - cookie, - bufused.as_mut_ptr(), - ), - } +pub fn fd_readdir(fd: Fd, buf: &mut [u8], cookie: DirCookie) -> Result { + let ptr = buf.as_mut_ptr() as *mut c_void; + unsafe_wrap!{ __wasi_fd_readdir(fd, ptr, buf.len(), cookie) } } -pub fn path_readlink(fd: Fd, path: &[u8], buf: &mut [u8]) -> Result { - let mut bufused = MaybeUninit::::uninit(); - unsafe_wrap1!{ - bufused, - __wasi_path_readlink( - fd, - path.as_ptr(), - path.len(), - buf.as_mut_ptr(), - buf.len(), - bufused.as_mut_ptr(), - ), +pub fn path_readlink(fd: Fd, path: &[u8], buf: &mut [u8]) -> Result { + let ptr = buf.as_mut_ptr(); + unsafe_wrap!{ + __wasi_path_readlink(fd, path.as_ptr(), path.len(), ptr, buf.len()) } } -pub fn path_rename(old_fd: Fd, old_path: &[u8], new_fd: Fd, new_path: &[u8]) -> Result<(), Errno> { +pub fn path_rename(old_fd: Fd, old_path: &[u8], new_fd: Fd, new_path: &[u8]) -> Result<(), Error> { unsafe_wrap0!{ __wasi_path_rename( old_fd, @@ -478,12 +443,8 @@ pub fn path_rename(old_fd: Fd, old_path: &[u8], new_fd: Fd, new_path: &[u8]) -> } } -pub fn fd_filestat_get(fd: Fd) -> Result { - let mut buf = MaybeUninit::::uninit(); - unsafe_wrap1!{ - buf, - __wasi_fd_filestat_get(fd, buf.as_mut_ptr()), - } +pub fn fd_filestat_get(fd: Fd) -> Result { + unsafe_wrap!{ __wasi_fd_filestat_get(fd) } } pub fn fd_filestat_set_times( @@ -491,19 +452,17 @@ pub fn fd_filestat_set_times( st_atim: Timestamp, st_mtim: Timestamp, fstflags: FstFlags, -) -> Result<(), Errno> { +) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_filestat_set_times(fd, st_atim, st_mtim, fstflags) } } -pub fn fd_filestat_set_size(fd: Fd, st_size: FileSize) -> Result<(), Errno> { +pub fn fd_filestat_set_size(fd: Fd, st_size: FileSize) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_filestat_set_size(fd, st_size) } } -pub fn path_filestat_get(fd: Fd, flags: LookupFlags, path: &[u8]) -> Result { - let mut buf = MaybeUninit::::uninit(); - unsafe_wrap1!{ - buf, - __wasi_path_filestat_get(fd, flags, path.as_ptr(), path.len(), buf.as_mut_ptr()), +pub fn path_filestat_get(fd: Fd, flags: LookupFlags, path: &[u8]) -> Result { + unsafe_wrap!{ + __wasi_path_filestat_get(fd, flags, path.as_ptr(), path.len()) } } @@ -514,7 +473,7 @@ pub fn path_filestat_set_times( st_atim: Timestamp, st_mtim: Timestamp, fstflags: FstFlags, -) -> Result<(), Errno> { +) -> Result<(), Error> { unsafe_wrap0!{ __wasi_path_filestat_set_times( fd, @@ -528,7 +487,7 @@ pub fn path_filestat_set_times( } } -pub fn path_symlink(old_path: &[u8], fd: Fd, new_path: &[u8]) -> Result<(), Errno> { +pub fn path_symlink(old_path: &[u8], fd: Fd, new_path: &[u8]) -> Result<(), Error> { unsafe_wrap0!{ __wasi_path_symlink( old_path.as_ptr(), @@ -540,25 +499,23 @@ pub fn path_symlink(old_path: &[u8], fd: Fd, new_path: &[u8]) -> Result<(), Errn } } -pub fn path_unlink_file(fd: Fd, path: &[u8]) -> Result<(), Errno> { +pub fn path_unlink_file(fd: Fd, path: &[u8]) -> Result<(), Error> { unsafe_wrap0!{ __wasi_path_unlink_file(fd, path.as_ptr(), path.len()) } } -pub fn path_remove_directory(fd: Fd, path: &[u8]) -> Result<(), Errno> { +pub fn path_remove_directory(fd: Fd, path: &[u8]) -> Result<(), Error> { unsafe_wrap0!{ __wasi_path_remove_directory(fd, path.as_ptr(), path.len()) } } -pub fn poll_oneoff(in_: &[Subscription], out: &mut [Event]) -> Result { +pub fn poll_oneoff(in_: &[Subscription], out: &mut [Event]) -> Result { assert!(out.len() >= in_.len()); - let mut nevents = MaybeUninit::::uninit(); - unsafe_wrap1!{ - nevents, + let ptr = out.as_mut_ptr() as *mut __wasi_event_t; + unsafe_wrap!{ __wasi_poll_oneoff( in_.as_ptr(), - out.as_mut_ptr(), + ptr, in_.len(), - nevents.as_mut_ptr(), - ), + ) } } @@ -566,11 +523,11 @@ pub fn proc_exit(rval: ExitCode) { unsafe { __wasi_proc_exit(rval) } } -pub fn proc_raise(sig: Signal) -> Result<(), Errno> { +pub fn proc_raise(sig: Signal) -> Result<(), Error> { unsafe_wrap0!{ __wasi_proc_raise(sig) } } -pub fn sock_recv(sock: Fd, ri_data: &[IoVec], ri_flags: RiFlags) -> Result<(usize, RoFlags), Errno> { +pub fn sock_recv(sock: Fd, ri_data: &[IoVec], ri_flags: RiFlags) -> Result<(usize, RoFlags), Error> { let mut ro_datalen = MaybeUninit::::uninit(); let mut ro_flags = MaybeUninit::::uninit(); @@ -591,37 +548,25 @@ pub fn sock_recv(sock: Fd, ri_data: &[IoVec], ri_flags: RiFlags) -> Result<(usiz } } -pub fn sock_send(sock: Fd, si_data: &[CIoVec], si_flags: SiFlags) -> Result { - let mut so_datalen = MaybeUninit::::uninit(); - unsafe_wrap1!{ - so_datalen, - __wasi_sock_send( - sock, - si_data.as_ptr(), - si_data.len(), - si_flags, - so_datalen.as_mut_ptr(), - ), +pub fn sock_send(sock: Fd, si_data: &[CIoVec], si_flags: SiFlags) -> Result { + unsafe_wrap!{ + __wasi_sock_send(sock, si_data.as_ptr(), si_data.len(), si_flags) } } -pub fn sock_shutdown(sock: Fd, how: SdFlags) -> Result<(), Errno> { +pub fn sock_shutdown(sock: Fd, how: SdFlags) -> Result<(), Error> { unsafe_wrap0!{ __wasi_sock_shutdown(sock, how) } } -pub fn sched_yield() -> Result<(), Errno> { +pub fn sched_yield() -> Result<(), Error> { unsafe_wrap0!{ __wasi_sched_yield() } } -pub fn fd_prestat_get(fd: Fd) -> Result { - let mut buf = MaybeUninit::::uninit(); - unsafe_wrap1!{ - buf, - __wasi_fd_prestat_get(fd, buf.as_mut_ptr()), - } +pub fn fd_prestat_get(fd: Fd) -> Result { + unsafe_wrap!{ __wasi_fd_prestat_get(fd) } } -pub fn fd_prestat_dir_name(fd: Fd, path: &mut [u8]) -> Result<(), Errno> { +pub fn fd_prestat_dir_name(fd: Fd, path: &mut [u8]) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_prestat_dir_name(fd, path.as_mut_ptr(), path.len()) } } From 02141392c78ad6316c1b9833dce524dc1ddca7b5 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Fri, 16 Aug 2019 16:06:38 +0300 Subject: [PATCH 03/23] modify macros --- src/wasi_unstable/mod.rs | 179 +++++++++++++++++++-------------------- 1 file changed, 85 insertions(+), 94 deletions(-) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index 1122100..e2b2fca 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -66,6 +66,8 @@ pub struct Event { // Assert that `Event` and `__wasi_event_t` have the same size const _ASSERT1: [(); 32] = [(); core::mem::size_of::<__wasi_event_t>()]; const _ASSERT2: [(); 32] = [(); core::mem::size_of::()]; +// Aassert that `__WASI_ESUCCESS` equals to 0 +const _ASSERT3: [(); 0] = [(); __WASI_ESUCCESS as usize]; pub const ADVICE_NORMAL: Advice = __WASI_ADVICE_NORMAL; pub const ADVICE_SEQUENTIAL: Advice = __WASI_ADVICE_SEQUENTIAL; @@ -79,11 +81,8 @@ pub const CLOCK_PROCESS_CPUTIME_ID: ClockId = __WASI_CLOCK_PROCESS_CPUTIME_ID; pub const CLOCK_THREAD_CPUTIME_ID: ClockId = __WASI_CLOCK_THREAD_CPUTIME_ID; pub const DIRCOOKIE_START: DirCookie = __WASI_DIRCOOKIE_START; -// Aassert that `__WASI_ESUCCESS` equals to 0 -const _ASSERT3: [(); 0] = [(); __WASI_ESUCCESS as usize]; - macro_rules! errno_set { - ($($safe_const:ident => $raw_const:ident,)*) => { + {$($safe_const:ident = $raw_const:ident;)*} => { $( pub const $safe_const: Error = unsafe { NonZeroU16::new_unchecked($raw_const) @@ -92,84 +91,84 @@ macro_rules! errno_set { }; } -errno_set!( - E2BIG => __WASI_E2BIG, - EACCES => __WASI_EACCES, - EADDRINUSE => __WASI_EADDRINUSE, - EADDRNOTAVAIL => __WASI_EADDRNOTAVAIL, - EAFNOSUPPORT => __WASI_EAFNOSUPPORT, - EAGAIN => __WASI_EAGAIN, - EALREADY => __WASI_EALREADY, - EBADF => __WASI_EBADF, - EBADMSG => __WASI_EBADMSG, - EBUSY => __WASI_EBUSY, - ECANCELED => __WASI_ECANCELED, - ECHILD => __WASI_ECHILD, - ECONNABORTED => __WASI_ECONNABORTED, - ECONNREFUSED => __WASI_ECONNREFUSED, - ECONNRESET => __WASI_ECONNRESET, - EDEADLK => __WASI_EDEADLK, - EDESTADDRREQ => __WASI_EDESTADDRREQ, - EDOM => __WASI_EDOM, - EDQUOT => __WASI_EDQUOT, - EEXIST => __WASI_EEXIST, - EFAULT => __WASI_EFAULT, - EFBIG => __WASI_EFBIG, - EHOSTUNREACH => __WASI_EHOSTUNREACH, - EIDRM => __WASI_EIDRM, - EILSEQ => __WASI_EILSEQ, - EINPROGRESS => __WASI_EINPROGRESS, - EINTR => __WASI_EINTR, - EINVAL => __WASI_EINVAL, - EIO => __WASI_EIO, - EISCONN => __WASI_EISCONN, - EISDIR => __WASI_EISDIR, - ELOOP => __WASI_ELOOP, - EMFILE => __WASI_EMFILE, - EMLINK => __WASI_EMLINK, - EMSGSIZE => __WASI_EMSGSIZE, - EMULTIHOP => __WASI_EMULTIHOP, - ENAMETOOLONG => __WASI_ENAMETOOLONG, - ENETDOWN => __WASI_ENETDOWN, - ENETRESET => __WASI_ENETRESET, - ENETUNREACH => __WASI_ENETUNREACH, - ENFILE => __WASI_ENFILE, - ENOBUFS => __WASI_ENOBUFS, - ENODEV => __WASI_ENODEV, - ENOENT => __WASI_ENOENT, - ENOEXEC => __WASI_ENOEXEC, - ENOLCK => __WASI_ENOLCK, - ENOLINK => __WASI_ENOLINK, - ENOMEM => __WASI_ENOMEM, - ENOMSG => __WASI_ENOMSG, - ENOPROTOOPT => __WASI_ENOPROTOOPT, - ENOSPC => __WASI_ENOSPC, - ENOSYS => __WASI_ENOSYS, - ENOTCONN => __WASI_ENOTCONN, - ENOTDIR => __WASI_ENOTDIR, - ENOTEMPTY => __WASI_ENOTEMPTY, - ENOTRECOVERABLE => __WASI_ENOTRECOVERABLE, - ENOTSOCK => __WASI_ENOTSOCK, - ENOTSUP => __WASI_ENOTSUP, - ENOTTY => __WASI_ENOTTY, - ENXIO => __WASI_ENXIO, - EOVERFLOW => __WASI_EOVERFLOW, - EOWNERDEAD => __WASI_EOWNERDEAD, - EPERM => __WASI_EPERM, - EPIPE => __WASI_EPIPE, - EPROTO => __WASI_EPROTO, - EPROTONOSUPPORT => __WASI_EPROTONOSUPPORT, - EPROTOTYPE => __WASI_EPROTOTYPE, - ERANGE => __WASI_ERANGE, - EROFS => __WASI_EROFS, - ESPIPE => __WASI_ESPIPE, - ESRCH => __WASI_ESRCH, - ESTALE => __WASI_ESTALE, - ETIMEDOUT => __WASI_ETIMEDOUT, - ETXTBSY => __WASI_ETXTBSY, - EXDEV => __WASI_EXDEV, - ENOTCAPABLE => __WASI_ENOTCAPABLE, -); +errno_set!{ + E2BIG = __WASI_E2BIG; + EACCES = __WASI_EACCES; + EADDRINUSE = __WASI_EADDRINUSE; + EADDRNOTAVAIL = __WASI_EADDRNOTAVAIL; + EAFNOSUPPORT = __WASI_EAFNOSUPPORT; + EAGAIN = __WASI_EAGAIN; + EALREADY = __WASI_EALREADY; + EBADF = __WASI_EBADF; + EBADMSG = __WASI_EBADMSG; + EBUSY = __WASI_EBUSY; + ECANCELED = __WASI_ECANCELED; + ECHILD = __WASI_ECHILD; + ECONNABORTED = __WASI_ECONNABORTED; + ECONNREFUSED = __WASI_ECONNREFUSED; + ECONNRESET = __WASI_ECONNRESET; + EDEADLK = __WASI_EDEADLK; + EDESTADDRREQ = __WASI_EDESTADDRREQ; + EDOM = __WASI_EDOM; + EDQUOT = __WASI_EDQUOT; + EEXIST = __WASI_EEXIST; + EFAULT = __WASI_EFAULT; + EFBIG = __WASI_EFBIG; + EHOSTUNREACH = __WASI_EHOSTUNREACH; + EIDRM = __WASI_EIDRM; + EILSEQ = __WASI_EILSEQ; + EINPROGRESS = __WASI_EINPROGRESS; + EINTR = __WASI_EINTR; + EINVAL = __WASI_EINVAL; + EIO = __WASI_EIO; + EISCONN = __WASI_EISCONN; + EISDIR = __WASI_EISDIR; + ELOOP = __WASI_ELOOP; + EMFILE = __WASI_EMFILE; + EMLINK = __WASI_EMLINK; + EMSGSIZE = __WASI_EMSGSIZE; + EMULTIHOP = __WASI_EMULTIHOP; + ENAMETOOLONG = __WASI_ENAMETOOLONG; + ENETDOWN = __WASI_ENETDOWN; + ENETRESET = __WASI_ENETRESET; + ENETUNREACH = __WASI_ENETUNREACH; + ENFILE = __WASI_ENFILE; + ENOBUFS = __WASI_ENOBUFS; + ENODEV = __WASI_ENODEV; + ENOENT = __WASI_ENOENT; + ENOEXEC = __WASI_ENOEXEC; + ENOLCK = __WASI_ENOLCK; + ENOLINK = __WASI_ENOLINK; + ENOMEM = __WASI_ENOMEM; + ENOMSG = __WASI_ENOMSG; + ENOPROTOOPT = __WASI_ENOPROTOOPT; + ENOSPC = __WASI_ENOSPC; + ENOSYS = __WASI_ENOSYS; + ENOTCONN = __WASI_ENOTCONN; + ENOTDIR = __WASI_ENOTDIR; + ENOTEMPTY = __WASI_ENOTEMPTY; + ENOTRECOVERABLE = __WASI_ENOTRECOVERABLE; + ENOTSOCK = __WASI_ENOTSOCK; + ENOTSUP = __WASI_ENOTSUP; + ENOTTY = __WASI_ENOTTY; + ENXIO = __WASI_ENXIO; + EOVERFLOW = __WASI_EOVERFLOW; + EOWNERDEAD = __WASI_EOWNERDEAD; + EPERM = __WASI_EPERM; + EPIPE = __WASI_EPIPE; + EPROTO = __WASI_EPROTO; + EPROTONOSUPPORT = __WASI_EPROTONOSUPPORT; + EPROTOTYPE = __WASI_EPROTOTYPE; + ERANGE = __WASI_ERANGE; + EROFS = __WASI_EROFS; + ESPIPE = __WASI_ESPIPE; + ESRCH = __WASI_ESRCH; + ESTALE = __WASI_ESTALE; + ETIMEDOUT = __WASI_ETIMEDOUT; + ETXTBSY = __WASI_ETXTBSY; + EXDEV = __WASI_EXDEV; + ENOTCAPABLE = __WASI_ENOTCAPABLE; +} pub const EVENT_FD_READWRITE_HANGUP: EventRwFlags = __WASI_EVENT_FD_READWRITE_HANGUP; pub const EVENTTYPE_CLOCK: EventType = __WASI_EVENTTYPE_CLOCK; @@ -302,15 +301,11 @@ pub fn clock_time_get(clock_id: ClockId, precision: Timestamp) -> Result Result { - unsafe_wrap!{ - __wasi_fd_pread(fd, iovs.as_ptr(), iovs.len(), offset) - } + unsafe_wrap!{ __wasi_fd_pread(fd, iovs.as_ptr(), iovs.len(), offset) } } pub fn fd_pwrite(fd: Fd, iovs: &[CIoVec], offset: FileSize) -> Result { - unsafe_wrap!{ - __wasi_fd_pwrite(fd, iovs.as_ptr(), iovs.len(), offset) - } + unsafe_wrap!{ __wasi_fd_pwrite(fd, iovs.as_ptr(), iovs.len(), offset) } } pub fn random_get(buf: &mut [u8]) -> Result<(), Error> { @@ -326,9 +321,7 @@ pub fn fd_datasync(fd: Fd) -> Result<(), Error> { } pub fn fd_read(fd: Fd, iovs: &[IoVec]) -> Result { - unsafe_wrap!{ - __wasi_fd_read(fd, iovs.as_ptr(), iovs.len()) - } + unsafe_wrap!{ __wasi_fd_read(fd, iovs.as_ptr(), iovs.len()) } } pub fn fd_renumber(from: Fd, to: Fd) -> Result<(), Error> { @@ -549,9 +542,7 @@ pub fn sock_recv(sock: Fd, ri_data: &[IoVec], ri_flags: RiFlags) -> Result<(usiz } pub fn sock_send(sock: Fd, si_data: &[CIoVec], si_flags: SiFlags) -> Result { - unsafe_wrap!{ - __wasi_sock_send(sock, si_data.as_ptr(), si_data.len(), si_flags) - } + unsafe_wrap!{ __wasi_sock_send(sock, si_data.as_ptr(), si_data.len(), si_flags) } } pub fn sock_shutdown(sock: Fd, how: SdFlags) -> Result<(), Error> { From 7a6469218a5302aa2be079241629d3aea351d3cd Mon Sep 17 00:00:00 2001 From: newpavlov Date: Fri, 16 Aug 2019 16:10:55 +0300 Subject: [PATCH 04/23] add #[inline] atrtibutes --- src/wasi_unstable/mod.rs | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index e2b2fca..2b5a22d 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -292,82 +292,102 @@ macro_rules! unsafe_wrap { }; } +#[inline] pub fn clock_res_get(clock_id: ClockId) -> Result { unsafe_wrap!{ __wasi_clock_res_get(clock_id) } } +#[inline] pub fn clock_time_get(clock_id: ClockId, precision: Timestamp) -> Result { unsafe_wrap!{ __wasi_clock_time_get(clock_id, precision) } } +#[inline] pub fn fd_pread(fd: Fd, iovs: &[IoVec], offset: FileSize) -> Result { unsafe_wrap!{ __wasi_fd_pread(fd, iovs.as_ptr(), iovs.len(), offset) } } +#[inline] pub fn fd_pwrite(fd: Fd, iovs: &[CIoVec], offset: FileSize) -> Result { unsafe_wrap!{ __wasi_fd_pwrite(fd, iovs.as_ptr(), iovs.len(), offset) } } +#[inline] pub fn random_get(buf: &mut [u8]) -> Result<(), Error> { unsafe_wrap0!{ __wasi_random_get(buf.as_mut_ptr() as *mut c_void, buf.len()) } } +#[inline] pub fn fd_close(fd: Fd) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_close(fd) } } +#[inline] pub fn fd_datasync(fd: Fd) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_datasync(fd) } } +#[inline] pub fn fd_read(fd: Fd, iovs: &[IoVec]) -> Result { unsafe_wrap!{ __wasi_fd_read(fd, iovs.as_ptr(), iovs.len()) } } +#[inline] pub fn fd_renumber(from: Fd, to: Fd) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_renumber(from, to) } } +#[inline] pub fn fd_seek(fd: Fd, offset: FileDelta, whence: Whence) -> Result { unsafe_wrap!{ __wasi_fd_seek(fd, offset, whence) } } +#[inline] pub fn fd_tell(fd: Fd) -> Result { unsafe_wrap!{ __wasi_fd_tell(fd) } } +#[inline] pub fn fd_fdstat_get(fd: Fd) -> Result { unsafe_wrap!{ __wasi_fd_fdstat_get(fd) } } +#[inline] pub fn fd_fdstat_set_flags(fd: Fd, flags: FdFlags) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_fdstat_set_flags(fd, flags) } } +#[inline] pub fn fd_fdstat_set_rights(fd: Fd, fs_rights_base: Rights, fs_rights_inheriting: Rights) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_fdstat_set_rights(fd, fs_rights_base, fs_rights_inheriting) } } +#[inline] pub fn fd_sync(fd: Fd) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_sync(fd) } } +#[inline] pub fn fd_write(fd: Fd, iovs: &[CIoVec]) -> Result { unsafe_wrap!{ __wasi_fd_write(fd, iovs.as_ptr(), iovs.len()) } } +#[inline] pub fn fd_advise(fd: Fd, offset: FileSize, len: FileSize, advice: Advice) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_advise(fd, offset, len, advice) } } +#[inline] pub fn fd_allocate(fd: Fd, offset: FileSize, len: FileSize) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_allocate(fd, offset, len) } } +#[inline] pub fn path_create_directory(fd: Fd, path: &[u8]) -> Result<(), Error> { unsafe_wrap0!{ __wasi_path_create_directory(fd, path.as_ptr(), path.len()) } } +#[inline] pub fn path_link( old_fd: Fd, old_flags: LookupFlags, @@ -388,6 +408,7 @@ pub fn path_link( } } +#[inline] pub fn path_open( dirfd: Fd, dirflags: LookupFlags, @@ -411,11 +432,13 @@ pub fn path_open( } } +#[inline] pub fn fd_readdir(fd: Fd, buf: &mut [u8], cookie: DirCookie) -> Result { let ptr = buf.as_mut_ptr() as *mut c_void; unsafe_wrap!{ __wasi_fd_readdir(fd, ptr, buf.len(), cookie) } } +#[inline] pub fn path_readlink(fd: Fd, path: &[u8], buf: &mut [u8]) -> Result { let ptr = buf.as_mut_ptr(); unsafe_wrap!{ @@ -423,6 +446,7 @@ pub fn path_readlink(fd: Fd, path: &[u8], buf: &mut [u8]) -> Result Result<(), Error> { unsafe_wrap0!{ __wasi_path_rename( @@ -436,10 +460,12 @@ pub fn path_rename(old_fd: Fd, old_path: &[u8], new_fd: Fd, new_path: &[u8]) -> } } +#[inline] pub fn fd_filestat_get(fd: Fd) -> Result { unsafe_wrap!{ __wasi_fd_filestat_get(fd) } } +#[inline] pub fn fd_filestat_set_times( fd: Fd, st_atim: Timestamp, @@ -449,16 +475,19 @@ pub fn fd_filestat_set_times( unsafe_wrap0!{ __wasi_fd_filestat_set_times(fd, st_atim, st_mtim, fstflags) } } +#[inline] pub fn fd_filestat_set_size(fd: Fd, st_size: FileSize) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_filestat_set_size(fd, st_size) } } +#[inline] pub fn path_filestat_get(fd: Fd, flags: LookupFlags, path: &[u8]) -> Result { unsafe_wrap!{ __wasi_path_filestat_get(fd, flags, path.as_ptr(), path.len()) } } +#[inline] pub fn path_filestat_set_times( fd: Fd, flags: LookupFlags, @@ -480,6 +509,7 @@ pub fn path_filestat_set_times( } } +#[inline] pub fn path_symlink(old_path: &[u8], fd: Fd, new_path: &[u8]) -> Result<(), Error> { unsafe_wrap0!{ __wasi_path_symlink( @@ -492,14 +522,17 @@ pub fn path_symlink(old_path: &[u8], fd: Fd, new_path: &[u8]) -> Result<(), Erro } } +#[inline] pub fn path_unlink_file(fd: Fd, path: &[u8]) -> Result<(), Error> { unsafe_wrap0!{ __wasi_path_unlink_file(fd, path.as_ptr(), path.len()) } } +#[inline] pub fn path_remove_directory(fd: Fd, path: &[u8]) -> Result<(), Error> { unsafe_wrap0!{ __wasi_path_remove_directory(fd, path.as_ptr(), path.len()) } } +#[inline] pub fn poll_oneoff(in_: &[Subscription], out: &mut [Event]) -> Result { assert!(out.len() >= in_.len()); let ptr = out.as_mut_ptr() as *mut __wasi_event_t; @@ -512,14 +545,17 @@ pub fn poll_oneoff(in_: &[Subscription], out: &mut [Event]) -> Result Result<(), Error> { unsafe_wrap0!{ __wasi_proc_raise(sig) } } +#[inline] pub fn sock_recv(sock: Fd, ri_data: &[IoVec], ri_flags: RiFlags) -> Result<(usize, RoFlags), Error> { let mut ro_datalen = MaybeUninit::::uninit(); let mut ro_flags = MaybeUninit::::uninit(); @@ -541,22 +577,27 @@ pub fn sock_recv(sock: Fd, ri_data: &[IoVec], ri_flags: RiFlags) -> Result<(usiz } } +#[inline] pub fn sock_send(sock: Fd, si_data: &[CIoVec], si_flags: SiFlags) -> Result { unsafe_wrap!{ __wasi_sock_send(sock, si_data.as_ptr(), si_data.len(), si_flags) } } +#[inline] pub fn sock_shutdown(sock: Fd, how: SdFlags) -> Result<(), Error> { unsafe_wrap0!{ __wasi_sock_shutdown(sock, how) } } +#[inline] pub fn sched_yield() -> Result<(), Error> { unsafe_wrap0!{ __wasi_sched_yield() } } +#[inline] pub fn fd_prestat_get(fd: Fd) -> Result { unsafe_wrap!{ __wasi_fd_prestat_get(fd) } } +#[inline] pub fn fd_prestat_dir_name(fd: Fd, path: &mut [u8]) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_prestat_dir_name(fd, path.as_mut_ptr(), path.len()) } } From 6a5fada525db1df44877df9f3253a44be38ab96d Mon Sep 17 00:00:00 2001 From: newpavlov Date: Fri, 16 Aug 2019 20:53:27 +0300 Subject: [PATCH 05/23] use Rust-style type names, fix typo --- src/wasi_unstable/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index 2b5a22d..7f5643d 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -57,16 +57,16 @@ pub type Prestat = __wasi_prestat_t; #[repr(C)] #[derive(Copy, Clone)] pub struct Event { - pub userdata: __wasi_userdata_t, + pub userdata: Userdata, pub res: Result<(), Error>, - pub type_: __wasi_eventtype_t, - pub u: __wasi_event_u, + pub type_: EventType, + pub u: Event, } // Assert that `Event` and `__wasi_event_t` have the same size const _ASSERT1: [(); 32] = [(); core::mem::size_of::<__wasi_event_t>()]; const _ASSERT2: [(); 32] = [(); core::mem::size_of::()]; -// Aassert that `__WASI_ESUCCESS` equals to 0 +// Assert that `__WASI_ESUCCESS` equals to 0 const _ASSERT3: [(); 0] = [(); __WASI_ESUCCESS as usize]; pub const ADVICE_NORMAL: Advice = __WASI_ADVICE_NORMAL; From 61465f1b5290bcb178754f6b82daf99785c18a3a Mon Sep 17 00:00:00 2001 From: newpavlov Date: Sun, 18 Aug 2019 07:48:46 +0300 Subject: [PATCH 06/23] add stdin, stdout and stderr constants --- src/wasi_unstable/mod.rs | 4 ++++ src/wasi_unstable/raw.rs | 3 +++ 2 files changed, 7 insertions(+) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index 7f5643d..20e9fce 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -81,6 +81,10 @@ pub const CLOCK_PROCESS_CPUTIME_ID: ClockId = __WASI_CLOCK_PROCESS_CPUTIME_ID; pub const CLOCK_THREAD_CPUTIME_ID: ClockId = __WASI_CLOCK_THREAD_CPUTIME_ID; pub const DIRCOOKIE_START: DirCookie = __WASI_DIRCOOKIE_START; +pub const STDIN_FD: Fd = __WASI_STDIN_FD; +pub const STDOUT_FD: Fd = __WASI_STDOUT_FD; +pub const STDERR_FD: Fd = __WASI_STDERR_FD; + macro_rules! errno_set { {$($safe_const:ident = $raw_const:ident;)*} => { $( diff --git a/src/wasi_unstable/raw.rs b/src/wasi_unstable/raw.rs index 1bccc76..1217864 100644 --- a/src/wasi_unstable/raw.rs +++ b/src/wasi_unstable/raw.rs @@ -342,6 +342,9 @@ pub const __WASI_SUBSCRIPTION_CLOCK_ABSTIME: u16 = 0x0001; pub const __WASI_WHENCE_CUR: u8 = 0; pub const __WASI_WHENCE_END: u8 = 1; pub const __WASI_WHENCE_SET: u8 = 2; +pub const __WASI_STDIN_FD: u32 = 0; +pub const __WASI_STDOUT_FD: u32 = 1; +pub const __WASI_STDERR_FD: u32 = 2; #[link(wasm_import_module = "wasi_unstable")] extern "C" { From ef5df09227fc058430dca8a4feab110908ad98b0 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Sun, 18 Aug 2019 07:54:15 +0300 Subject: [PATCH 07/23] fix Event type --- src/wasi_unstable/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index 20e9fce..e560164 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -60,7 +60,7 @@ pub struct Event { pub userdata: Userdata, pub res: Result<(), Error>, pub type_: EventType, - pub u: Event, + pub u: __wasi_event_u, } // Assert that `Event` and `__wasi_event_t` have the same size From 662dd1bf592d07b38c1ed7c644d5adc56d573ad1 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Mon, 19 Aug 2019 08:17:37 +0300 Subject: [PATCH 08/23] use ! for return type for proc_exit --- src/wasi_unstable/mod.rs | 2 +- src/wasi_unstable/raw.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index e560164..8c61bb4 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -550,7 +550,7 @@ pub fn poll_oneoff(in_: &[Subscription], out: &mut [Event]) -> Result ! { unsafe { __wasi_proc_exit(rval) } } diff --git a/src/wasi_unstable/raw.rs b/src/wasi_unstable/raw.rs index 1217864..b9d53ff 100644 --- a/src/wasi_unstable/raw.rs +++ b/src/wasi_unstable/raw.rs @@ -575,7 +575,7 @@ extern "C" { ) -> __wasi_errno_t; #[link_name = "proc_exit"] - pub fn __wasi_proc_exit(rval: __wasi_exitcode_t); + pub fn __wasi_proc_exit(rval: __wasi_exitcode_t) -> !; #[link_name = "proc_raise"] pub fn __wasi_proc_raise(sig: __wasi_signal_t) -> __wasi_errno_t; From 78129ebc5cfa1ef137228027be33be031e3dcd2b Mon Sep 17 00:00:00 2001 From: newpavlov Date: Mon, 19 Aug 2019 13:29:43 +0300 Subject: [PATCH 09/23] return old Event type --- src/wasi_unstable/mod.rs | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index 8c61bb4..3d8e8f8 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -50,24 +50,11 @@ pub type FileStat = __wasi_filestat_t; pub type CIoVec = __wasi_ciovec_t; pub type IoVec = __wasi_iovec_t; pub type Subscription = __wasi_subscription_t; +pub type Event = __wasi_event_t; pub type Prestat = __wasi_prestat_t; -// should have exactly the same layout as __wasi_event_t -// TODO: add a more thorough test -#[repr(C)] -#[derive(Copy, Clone)] -pub struct Event { - pub userdata: Userdata, - pub res: Result<(), Error>, - pub type_: EventType, - pub u: __wasi_event_u, -} - -// Assert that `Event` and `__wasi_event_t` have the same size -const _ASSERT1: [(); 32] = [(); core::mem::size_of::<__wasi_event_t>()]; -const _ASSERT2: [(); 32] = [(); core::mem::size_of::()]; // Assert that `__WASI_ESUCCESS` equals to 0 -const _ASSERT3: [(); 0] = [(); __WASI_ESUCCESS as usize]; +const _ASSERT1: [(); 0] = [(); __WASI_ESUCCESS as usize]; pub const ADVICE_NORMAL: Advice = __WASI_ADVICE_NORMAL; pub const ADVICE_SEQUENTIAL: Advice = __WASI_ADVICE_SEQUENTIAL; From 252bc1fc56430638810a7b39e337c96997e4c1a3 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Mon, 19 Aug 2019 15:00:27 +0300 Subject: [PATCH 10/23] add alloc feature, error_string and get_args functions --- Cargo.toml | 4 ++++ src/lib.rs | 2 ++ src/wasi_unstable/mod.rs | 41 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 99ac2b9..6fdead5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,5 +13,9 @@ documentation = "https://docs.rs/wasi" [dependencies] +[features] +default = ["alloc"] +alloc = [] + [badges] maintenance = { status = "experimental" } diff --git a/src/lib.rs b/src/lib.rs index 42e13ea..1be1809 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,5 +12,7 @@ ) )] #![no_std] +#[cfg(feature = "alloc")] +extern crate alloc; pub mod wasi_unstable; diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index 3d8e8f8..e6e74d1 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -593,6 +593,47 @@ pub fn fd_prestat_dir_name(fd: Fd, path: &mut [u8]) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_prestat_dir_name(fd, path.as_mut_ptr(), path.len()) } } +#[cfg(feature = "alloc")] +use alloc::{vec::Vec, string::String}; + +// TODO: add descriptions +#[cfg(feature = "alloc")] +pub fn error_string(errno: __wasi_errno_t) -> String { + use alloc::{format, string::ToString}; + match errno { + 0 => "Success", + n => return format!("Unknown error {}", n), + }.to_string() +} + +#[cfg(feature = "alloc")] +pub fn get_args() -> Result>, Error> { + use alloc::vec; + + let (mut argc, mut argv) = (0, 0); + let ret = unsafe { __wasi_args_sizes_get (&mut argc, &mut argv) }; + if let Some(err) = NonZeroU16::new(ret) { return Err(err); } + + let mut argc = vec![core::ptr::null_mut::(); argc]; + let mut argv = vec![0u8; argv]; + let ret = unsafe { __wasi_args_get(argc.as_mut_ptr(), argv.as_mut_ptr()) }; + if let Some(err) = NonZeroU16::new(ret) { return Err(err); } + + fn cstr2vec(ptr: *const u8) -> Vec { + let mut n: usize = 0; + loop { + unsafe { + if *ptr.offset(n as isize) == 0 { + return core::slice::from_raw_parts(ptr, n).to_vec(); + } + } + n += 1; + } + } + + Ok(argc.into_iter().map(|p| cstr2vec(p as *const u8)).collect()) +} + // TODO: Safe interfaces to the args and environ functions /* pub fn args_get(argv: *mut *mut u8, argv_buf: *mut u8) -> Errno {} From c96e16009f79eb6313a07026b616923d665c13a1 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Mon, 19 Aug 2019 15:16:24 +0300 Subject: [PATCH 11/23] add get_environ --- src/wasi_unstable/mod.rs | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index e6e74d1..dffb3cd 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -623,6 +623,7 @@ pub fn get_args() -> Result>, Error> { let mut n: usize = 0; loop { unsafe { + // replace with memchr? if *ptr.offset(n as isize) == 0 { return core::slice::from_raw_parts(ptr, n).to_vec(); } @@ -634,10 +635,30 @@ pub fn get_args() -> Result>, Error> { Ok(argc.into_iter().map(|p| cstr2vec(p as *const u8)).collect()) } -// TODO: Safe interfaces to the args and environ functions -/* -pub fn args_get(argv: *mut *mut u8, argv_buf: *mut u8) -> Errno {} -pub fn args_sizes_get(argc: *mut usize, argv_buf_size: *mut usize) -> Errno {} -pub fn environ_get(environ: *mut *mut u8, environ_buf: *mut u8) -> Errno {} -pub fn environ_sizes_get(environ_count: *mut usize, environ_buf_size: *mut usize) -> Errno {} -*/ +#[cfg(feature = "alloc")] +pub fn get_environ() -> Result>, Error> { + use alloc::vec; + + let (mut argc, mut argv) = (0, 0); + let ret = unsafe { __wasi_environ_sizes_get (&mut argc, &mut argv) }; + if let Some(err) = NonZeroU16::new(ret) { return Err(err); } + + let mut argc = vec![core::ptr::null_mut::(); argc]; + let mut argv = vec![0u8; argv]; + let ret = unsafe { __wasi_environ_get(argc.as_mut_ptr(), argv.as_mut_ptr()) }; + if let Some(err) = NonZeroU16::new(ret) { return Err(err); } + + fn cstr2vec(ptr: *const u8) -> Vec { + let mut n: usize = 0; + loop { + unsafe { + if *ptr.offset(n as isize) == 0 { + return core::slice::from_raw_parts(ptr, n).to_vec(); + } + } + n += 1; + } + } + + Ok(argc.into_iter().map(|p| cstr2vec(p as *const u8)).collect()) +} From abf318dad67b0cb9ce8bfa3f5bf182152764c10d Mon Sep 17 00:00:00 2001 From: newpavlov Date: Mon, 19 Aug 2019 15:26:22 +0300 Subject: [PATCH 12/23] add all error codes to error_string --- src/wasi_unstable/mod.rs | 100 +++++++++++++++++++++++++++++++++++---- 1 file changed, 90 insertions(+), 10 deletions(-) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index dffb3cd..698b6d8 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -596,16 +596,6 @@ pub fn fd_prestat_dir_name(fd: Fd, path: &mut [u8]) -> Result<(), Error> { #[cfg(feature = "alloc")] use alloc::{vec::Vec, string::String}; -// TODO: add descriptions -#[cfg(feature = "alloc")] -pub fn error_string(errno: __wasi_errno_t) -> String { - use alloc::{format, string::ToString}; - match errno { - 0 => "Success", - n => return format!("Unknown error {}", n), - }.to_string() -} - #[cfg(feature = "alloc")] pub fn get_args() -> Result>, Error> { use alloc::vec; @@ -662,3 +652,93 @@ pub fn get_environ() -> Result>, Error> { Ok(argc.into_iter().map(|p| cstr2vec(p as *const u8)).collect()) } + +// TODO: improve descriptions +#[cfg(feature = "alloc")] +pub fn error_string(errno: __wasi_errno_t) -> String { + use alloc::{format, string::ToString}; + let code = if let Some(code) = NonZeroU16::new(errno) { + code + } else { + return "Success".to_string(); + }; + match code { + E2BIG => "E2BIG", + EACCES => "EACCES", + EADDRINUSE => "EADDRINUSE", + EADDRNOTAVAIL => "EADDRNOTAVAIL", + EAFNOSUPPORT => "EAFNOSUPPORT", + EAGAIN => "EAGAIN", + EALREADY => "EALREADY", + EBADF => "EBADF", + EBADMSG => "EBADMSG", + EBUSY => "EBUSY", + ECANCELED => "ECANCELED", + ECHILD => "ECHILD", + ECONNABORTED => "ECONNABORTED", + ECONNREFUSED => "ECONNREFUSED", + ECONNRESET => "ECONNRESET", + EDEADLK => "EDEADLK", + EDESTADDRREQ => "EDESTADDRREQ", + EDOM => "EDOM", + EDQUOT => "EDQUOT", + EEXIST => "EEXIST", + EFAULT => "EFAULT", + EFBIG => "EFBIG", + EHOSTUNREACH => "EHOSTUNREACH", + EIDRM => "EIDRM", + EILSEQ => "EILSEQ", + EINPROGRESS => "EINPROGRESS", + EINTR => "EINTR", + EINVAL => "EINVAL", + EIO => "EIO", + EISCONN => "EISCONN", + EISDIR => "EISDIR", + ELOOP => "ELOOP", + EMFILE => "EMFILE", + EMLINK => "EMLINK", + EMSGSIZE => "EMSGSIZE", + EMULTIHOP => "EMULTIHOP", + ENAMETOOLONG => "ENAMETOOLONG", + ENETDOWN => "ENETDOWN", + ENETRESET => "ENETRESET", + ENETUNREACH => "ENETUNREACH", + ENFILE => "ENFILE", + ENOBUFS => "ENOBUFS", + ENODEV => "ENODEV", + ENOENT => "ENOENT", + ENOEXEC => "ENOEXEC", + ENOLCK => "ENOLCK", + ENOLINK => "ENOLINK", + ENOMEM => "ENOMEM", + ENOMSG => "ENOMSG", + ENOPROTOOPT => "ENOPROTOOPT", + ENOSPC => "ENOSPC", + ENOSYS => "ENOSYS", + ENOTCONN => "ENOTCONN", + ENOTDIR => "ENOTDIR", + ENOTEMPTY => "ENOTEMPTY", + ENOTRECOVERABLE => "ENOTRECOVERABLE", + ENOTSOCK => "ENOTSOCK", + ENOTSUP => "ENOTSUP", + ENOTTY => "ENOTTY", + ENXIO => "ENXIO", + EOVERFLOW => "EOVERFLOW", + EOWNERDEAD => "EOWNERDEAD", + EPERM => "EPERM", + EPIPE => "EPIPE", + EPROTO => "EPROTO", + EPROTONOSUPPORT => "EPROTONOSUPPORT", + EPROTOTYPE => "EPROTOTYPE", + ERANGE => "ERANGE", + EROFS => "EROFS", + ESPIPE => "ESPIPE", + ESRCH => "ESRCH", + ESTALE => "ESTALE", + ETIMEDOUT => "ETIMEDOUT", + ETXTBSY => "ETXTBSY", + EXDEV => "EXDEV", + ENOTCAPABLE => "ENOTCAPABLE", + n => return format!("Unknown error {}", n), + }.to_string() +} From ad8182a3a1ca5e8e00d8ade7d2b97a265ee7fb71 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Mon, 19 Aug 2019 19:03:00 +0300 Subject: [PATCH 13/23] improve decriptions --- src/wasi_unstable/mod.rs | 153 +++++++++++++++++++-------------------- 1 file changed, 76 insertions(+), 77 deletions(-) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index 698b6d8..95fa14b 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -653,7 +653,6 @@ pub fn get_environ() -> Result>, Error> { Ok(argc.into_iter().map(|p| cstr2vec(p as *const u8)).collect()) } -// TODO: improve descriptions #[cfg(feature = "alloc")] pub fn error_string(errno: __wasi_errno_t) -> String { use alloc::{format, string::ToString}; @@ -663,82 +662,82 @@ pub fn error_string(errno: __wasi_errno_t) -> String { return "Success".to_string(); }; match code { - E2BIG => "E2BIG", - EACCES => "EACCES", - EADDRINUSE => "EADDRINUSE", - EADDRNOTAVAIL => "EADDRNOTAVAIL", - EAFNOSUPPORT => "EAFNOSUPPORT", - EAGAIN => "EAGAIN", - EALREADY => "EALREADY", - EBADF => "EBADF", - EBADMSG => "EBADMSG", - EBUSY => "EBUSY", - ECANCELED => "ECANCELED", - ECHILD => "ECHILD", - ECONNABORTED => "ECONNABORTED", - ECONNREFUSED => "ECONNREFUSED", - ECONNRESET => "ECONNRESET", - EDEADLK => "EDEADLK", - EDESTADDRREQ => "EDESTADDRREQ", - EDOM => "EDOM", - EDQUOT => "EDQUOT", - EEXIST => "EEXIST", - EFAULT => "EFAULT", - EFBIG => "EFBIG", - EHOSTUNREACH => "EHOSTUNREACH", - EIDRM => "EIDRM", - EILSEQ => "EILSEQ", - EINPROGRESS => "EINPROGRESS", - EINTR => "EINTR", - EINVAL => "EINVAL", - EIO => "EIO", - EISCONN => "EISCONN", - EISDIR => "EISDIR", - ELOOP => "ELOOP", - EMFILE => "EMFILE", - EMLINK => "EMLINK", - EMSGSIZE => "EMSGSIZE", - EMULTIHOP => "EMULTIHOP", - ENAMETOOLONG => "ENAMETOOLONG", - ENETDOWN => "ENETDOWN", - ENETRESET => "ENETRESET", - ENETUNREACH => "ENETUNREACH", - ENFILE => "ENFILE", - ENOBUFS => "ENOBUFS", - ENODEV => "ENODEV", - ENOENT => "ENOENT", - ENOEXEC => "ENOEXEC", - ENOLCK => "ENOLCK", - ENOLINK => "ENOLINK", - ENOMEM => "ENOMEM", - ENOMSG => "ENOMSG", - ENOPROTOOPT => "ENOPROTOOPT", - ENOSPC => "ENOSPC", - ENOSYS => "ENOSYS", - ENOTCONN => "ENOTCONN", - ENOTDIR => "ENOTDIR", - ENOTEMPTY => "ENOTEMPTY", - ENOTRECOVERABLE => "ENOTRECOVERABLE", - ENOTSOCK => "ENOTSOCK", - ENOTSUP => "ENOTSUP", - ENOTTY => "ENOTTY", - ENXIO => "ENXIO", - EOVERFLOW => "EOVERFLOW", - EOWNERDEAD => "EOWNERDEAD", - EPERM => "EPERM", - EPIPE => "EPIPE", - EPROTO => "EPROTO", - EPROTONOSUPPORT => "EPROTONOSUPPORT", - EPROTOTYPE => "EPROTOTYPE", - ERANGE => "ERANGE", - EROFS => "EROFS", - ESPIPE => "ESPIPE", - ESRCH => "ESRCH", - ESTALE => "ESTALE", - ETIMEDOUT => "ETIMEDOUT", - ETXTBSY => "ETXTBSY", - EXDEV => "EXDEV", - ENOTCAPABLE => "ENOTCAPABLE", + E2BIG => "Argument list too long", + EACCES => "Permission denied", + EADDRINUSE => "Address in use", + EADDRNOTAVAIL => "Address not available", + EAFNOSUPPORT => "Address family not supported by protocol", + EAGAIN => "Resource temporarily unavailable", + EALREADY => "Operation already in progress", + EBADF => "Bad file descriptor", + EBADMSG => "Bad message", + EBUSY => "Resource busy", + ECANCELED => "Operation canceled", + ECHILD => "No child process", + ECONNABORTED => "Connection aborted", + ECONNREFUSED => "Connection refused", + ECONNRESET => "Connection reset by peer", + EDEADLK => "Resource deadlock would occur", + EDESTADDRREQ => "Destination address required", + EDOM => "Domain error", + EDQUOT => "Quota exceeded", + EEXIST => "File exists", + EFAULT => "Bad address", + EFBIG => "File too large", + EHOSTUNREACH => "Host is unreachable", + EIDRM => "Identifier removed", + EILSEQ => "Illegal byte sequence", + EINPROGRESS => "Operation in progress", + EINTR => "Interrupted system call", + EINVAL => "Invalid argument", + EIO => "Remote I/O error", + EISCONN => "Socket is connected", + EISDIR => "Is a directory", + ELOOP => "Symbolic link loop", + EMFILE => "No file descriptors available", + EMLINK => "Too many links", + EMSGSIZE => "Message too large", + EMULTIHOP => "Multihop attempted", + ENAMETOOLONG => "Filename too long", + ENETDOWN => "Network is down", + ENETRESET => "Connection reset by network", + ENETUNREACH => "Network unreachable", + ENFILE => "Too many open files in system", + ENOBUFS => "No buffer space available", + ENODEV => "No such device", + ENOENT => "No such file or directory", + ENOEXEC => "Exec format error", + ENOLCK => "No locks available", + ENOLINK => "Link has been severed", + ENOMEM => "Out of memory", + ENOMSG => "No message of desired type", + ENOPROTOOPT => "Protocol not available", + ENOSPC => "No space left on device", + ENOSYS => "Function not implemented", + ENOTCONN => "Socket not connected", + ENOTDIR => "Not a directory", + ENOTEMPTY => "Directory not empty", + ENOTRECOVERABLE => "State not recoverable", + ENOTSOCK => "Not a socket", + ENOTSUP => "Not supported", + ENOTTY => "Not a tty", + ENXIO => "No such device or address", + EOVERFLOW => "Value too large for data type", + EOWNERDEAD => "Previous owner died", + EPERM => "Operation not permitted", + EPIPE => "Broken pipe", + EPROTO => "Protocol error", + EPROTONOSUPPORT => "Protocol not supported", + EPROTOTYPE => "Protocol wrong type for socket", + ERANGE => "Result not representable", + EROFS => "Read-only file system", + ESPIPE => "Invalid seek", + ESRCH => "No such process", + ESTALE => "Stale file handle", + ETIMEDOUT => "Operation timed out", + ETXTBSY => "Text file busy", + EXDEV => "Cross-device link", + ENOTCAPABLE => "Capabilities insufficient", n => return format!("Unknown error {}", n), }.to_string() } From cfddda1ef2e493faed36cf1abf68e76c9cb4d8c4 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Tue, 20 Aug 2019 17:43:09 +0300 Subject: [PATCH 14/23] change error_string --- src/wasi_unstable/mod.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index 95fa14b..ac67be6 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -653,15 +653,8 @@ pub fn get_environ() -> Result>, Error> { Ok(argc.into_iter().map(|p| cstr2vec(p as *const u8)).collect()) } -#[cfg(feature = "alloc")] -pub fn error_string(errno: __wasi_errno_t) -> String { - use alloc::{format, string::ToString}; - let code = if let Some(code) = NonZeroU16::new(errno) { - code - } else { - return "Success".to_string(); - }; - match code { +pub fn error_str(err: Error) -> Option<&'static str> { + let desc = match err { E2BIG => "Argument list too long", EACCES => "Permission denied", EADDRINUSE => "Address in use", @@ -738,6 +731,7 @@ pub fn error_string(errno: __wasi_errno_t) -> String { ETXTBSY => "Text file busy", EXDEV => "Cross-device link", ENOTCAPABLE => "Capabilities insufficient", - n => return format!("Unknown error {}", n), - }.to_string() + _ => return None, + }; + Some(desc) } From 135ca27b44e91d95ce56004cebd62009845c771d Mon Sep 17 00:00:00 2001 From: newpavlov Date: Tue, 20 Aug 2019 18:11:12 +0300 Subject: [PATCH 15/23] use callback-based approach for get_args and get_environ --- src/wasi_unstable/mod.rs | 41 ++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index ac67be6..06df038 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -594,10 +594,7 @@ pub fn fd_prestat_dir_name(fd: Fd, path: &mut [u8]) -> Result<(), Error> { } #[cfg(feature = "alloc")] -use alloc::{vec::Vec, string::String}; - -#[cfg(feature = "alloc")] -pub fn get_args() -> Result>, Error> { +pub fn get_args(mut process_arg: impl FnMut(&[u8])) -> Result<(), Error> { use alloc::vec; let (mut argc, mut argv) = (0, 0); @@ -609,24 +606,24 @@ pub fn get_args() -> Result>, Error> { let ret = unsafe { __wasi_args_get(argc.as_mut_ptr(), argv.as_mut_ptr()) }; if let Some(err) = NonZeroU16::new(ret) { return Err(err); } - fn cstr2vec(ptr: *const u8) -> Vec { - let mut n: usize = 0; - loop { + for ptr in argc { + for n in 0.. { unsafe { // replace with memchr? if *ptr.offset(n as isize) == 0 { - return core::slice::from_raw_parts(ptr, n).to_vec(); + let slice = core::slice::from_raw_parts(ptr, n); + process_arg(slice); + break } } - n += 1; } } - Ok(argc.into_iter().map(|p| cstr2vec(p as *const u8)).collect()) + Ok(()) } #[cfg(feature = "alloc")] -pub fn get_environ() -> Result>, Error> { +pub fn get_environ(mut process_env: impl FnMut(&[u8], &[u8])) -> Result<(), Error> { use alloc::vec; let (mut argc, mut argv) = (0, 0); @@ -638,19 +635,27 @@ pub fn get_environ() -> Result>, Error> { let ret = unsafe { __wasi_environ_get(argc.as_mut_ptr(), argv.as_mut_ptr()) }; if let Some(err) = NonZeroU16::new(ret) { return Err(err); } - fn cstr2vec(ptr: *const u8) -> Vec { - let mut n: usize = 0; - loop { + for ptr in argc { + let mut key: &[u8] = &[]; + for n in 0.. { unsafe { - if *ptr.offset(n as isize) == 0 { - return core::slice::from_raw_parts(ptr, n).to_vec(); + // use memchr? + match *ptr.offset(n as isize) { + 0 => { + let val = core::slice::from_raw_parts(ptr, n); + process_env(key, val); + break + } + b'=' if key.is_empty() => { + key = core::slice::from_raw_parts(ptr, n); + } + _ => {} } } - n += 1; } } - Ok(argc.into_iter().map(|p| cstr2vec(p as *const u8)).collect()) + Ok(()) } pub fn error_str(err: Error) -> Option<&'static str> { From 2e7f314cf3f54ad92458fb8625e5038aad5134ea Mon Sep 17 00:00:00 2001 From: newpavlov Date: Tue, 20 Aug 2019 18:59:25 +0300 Subject: [PATCH 16/23] add TODO comments --- src/wasi_unstable/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index 06df038..8692571 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -601,6 +601,8 @@ pub fn get_args(mut process_arg: impl FnMut(&[u8])) -> Result<(), Error> { let ret = unsafe { __wasi_args_sizes_get (&mut argc, &mut argv) }; if let Some(err) = NonZeroU16::new(ret) { return Err(err); } + // TODO: remove allocations after stabilization of unsized rvalues, see: + // https://github.com/rust-lang/rust/issues/48055 let mut argc = vec![core::ptr::null_mut::(); argc]; let mut argv = vec![0u8; argv]; let ret = unsafe { __wasi_args_get(argc.as_mut_ptr(), argv.as_mut_ptr()) }; @@ -630,6 +632,8 @@ pub fn get_environ(mut process_env: impl FnMut(&[u8], &[u8])) -> Result<(), Erro let ret = unsafe { __wasi_environ_sizes_get (&mut argc, &mut argv) }; if let Some(err) = NonZeroU16::new(ret) { return Err(err); } + // TODO: remove allocations after stabilization of unsized rvalues, see: + // https://github.com/rust-lang/rust/issues/48055 let mut argc = vec![core::ptr::null_mut::(); argc]; let mut argv = vec![0u8; argv]; let ret = unsafe { __wasi_environ_get(argc.as_mut_ptr(), argv.as_mut_ptr()) }; From 75d0342ae5b49597a4487326037ba92dd3efe0e1 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Wed, 21 Aug 2019 04:07:52 +0300 Subject: [PATCH 17/23] change args and env retrieval --- src/wasi_unstable/mod.rs | 74 +++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 16 deletions(-) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index 8692571..373b659 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -593,25 +593,46 @@ pub fn fd_prestat_dir_name(fd: Fd, path: &mut [u8]) -> Result<(), Error> { unsafe_wrap0!{ __wasi_fd_prestat_dir_name(fd, path.as_mut_ptr(), path.len()) } } +#[derive(Copy, Clone)] +pub struct ArgsSizes { + count: usize, + buf_len: usize, +} + +impl ArgsSizes { + #[inline] + pub fn get_count(&self) -> usize { self.count } + #[inline] + pub fn get_buf_len(&self) -> usize { self.buf_len } +} + +#[inline] +pub fn args_sizes_get() -> Result { + let mut res = ArgsSizes { count: 0, buf_len: 0 }; + let code = unsafe { + __wasi_args_sizes_get(&mut res.count, &mut res.buf_len) + }; + if let Some(err) = NonZeroU16::new(code) { return Err(err); } + Ok(res) +} + #[cfg(feature = "alloc")] -pub fn get_args(mut process_arg: impl FnMut(&[u8])) -> Result<(), Error> { +#[inline] +pub fn get_args( + ars: ArgsSizes, mut process_arg: impl FnMut(&[u8]), +) -> Result<(), Error> { use alloc::vec; - let (mut argc, mut argv) = (0, 0); - let ret = unsafe { __wasi_args_sizes_get (&mut argc, &mut argv) }; - if let Some(err) = NonZeroU16::new(ret) { return Err(err); } - // TODO: remove allocations after stabilization of unsized rvalues, see: // https://github.com/rust-lang/rust/issues/48055 - let mut argc = vec![core::ptr::null_mut::(); argc]; - let mut argv = vec![0u8; argv]; + let mut argc = vec![core::ptr::null_mut::(); ars.count]; + let mut argv = vec![0u8; ars.buf_len]; let ret = unsafe { __wasi_args_get(argc.as_mut_ptr(), argv.as_mut_ptr()) }; if let Some(err) = NonZeroU16::new(ret) { return Err(err); } for ptr in argc { for n in 0.. { unsafe { - // replace with memchr? if *ptr.offset(n as isize) == 0 { let slice = core::slice::from_raw_parts(ptr, n); process_arg(slice); @@ -624,18 +645,40 @@ pub fn get_args(mut process_arg: impl FnMut(&[u8])) -> Result<(), Error> { Ok(()) } +#[derive(Copy, Clone)] +pub struct EnvironSizes { + count: usize, + buf_len: usize, +} + +impl EnvironSizes { + #[inline] + pub fn get_count(&self) -> usize { self.count } + #[inline] + pub fn get_buf_len(&self) -> usize { self.buf_len } +} + +#[inline] +pub fn environ_sizes_get() -> Result { + let mut res = EnvironSizes { count: 0, buf_len: 0 }; + let code = unsafe { + __wasi_environ_sizes_get(&mut res.count, &mut res.buf_len) + }; + if let Some(err) = NonZeroU16::new(code) { return Err(err); } + Ok(res) +} + #[cfg(feature = "alloc")] -pub fn get_environ(mut process_env: impl FnMut(&[u8], &[u8])) -> Result<(), Error> { +#[inline] +pub fn environ_get( + es: EnvironSizes, mut process_env: impl FnMut(&[u8], &[u8]), +) -> Result<(), Error> { use alloc::vec; - let (mut argc, mut argv) = (0, 0); - let ret = unsafe { __wasi_environ_sizes_get (&mut argc, &mut argv) }; - if let Some(err) = NonZeroU16::new(ret) { return Err(err); } - // TODO: remove allocations after stabilization of unsized rvalues, see: // https://github.com/rust-lang/rust/issues/48055 - let mut argc = vec![core::ptr::null_mut::(); argc]; - let mut argv = vec![0u8; argv]; + let mut argc = vec![core::ptr::null_mut::(); es.count]; + let mut argv = vec![0u8; es.buf_len]; let ret = unsafe { __wasi_environ_get(argc.as_mut_ptr(), argv.as_mut_ptr()) }; if let Some(err) = NonZeroU16::new(ret) { return Err(err); } @@ -643,7 +686,6 @@ pub fn get_environ(mut process_env: impl FnMut(&[u8], &[u8])) -> Result<(), Erro let mut key: &[u8] = &[]; for n in 0.. { unsafe { - // use memchr? match *ptr.offset(n as isize) { 0 => { let val = core::slice::from_raw_parts(ptr, n); From b3750ef369bfe796525aa80331a06abb8282bcba Mon Sep 17 00:00:00 2001 From: newpavlov Date: Wed, 21 Aug 2019 04:09:12 +0300 Subject: [PATCH 18/23] get_args -> args_get --- src/wasi_unstable/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index 373b659..f394991 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -618,7 +618,7 @@ pub fn args_sizes_get() -> Result { #[cfg(feature = "alloc")] #[inline] -pub fn get_args( +pub fn args_get( ars: ArgsSizes, mut process_arg: impl FnMut(&[u8]), ) -> Result<(), Error> { use alloc::vec; From 5fbdd4c7b0420d1a23bda86728a96308cee97664 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Wed, 21 Aug 2019 17:22:35 +0300 Subject: [PATCH 19/23] fix Cargo.toml --- Cargo.toml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 519e128..070cc67 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,13 +16,11 @@ documentation = "https://docs.rs/wasi" compiler_builtins = { version = "0.1", optional = true } core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" } -[features] -# Unstable feature to support being a libstd dependency -rustc-dep-of-std = ["compiler_builtins", "core"] - [features] default = ["alloc"] alloc = [] +# Unstable feature to support being a libstd dependency +rustc-dep-of-std = ["compiler_builtins", "core"] [badges] maintenance = { status = "experimental" } From ac4cdc5c9808b2891f7c0b0defa405e6b479f381 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Wed, 21 Aug 2019 17:31:15 +0300 Subject: [PATCH 20/23] add rustc-std-workspace-alloc --- Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 070cc67..7817ba1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,12 +15,13 @@ documentation = "https://docs.rs/wasi" # When built as part of libstd compiler_builtins = { version = "0.1", optional = true } core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" } +rustc-std-workspace-alloc = { version = "1.0", optional = true } [features] default = ["alloc"] alloc = [] # Unstable feature to support being a libstd dependency -rustc-dep-of-std = ["compiler_builtins", "core"] +rustc-dep-of-std = ["compiler_builtins", "core", "rustc-std-workspace-alloc"] [badges] maintenance = { status = "experimental" } From cf86d8e06c600be35182a4d94b6653037f9f8c7b Mon Sep 17 00:00:00 2001 From: newpavlov Date: Wed, 21 Aug 2019 17:33:40 +0300 Subject: [PATCH 21/23] fix alloc import --- src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 1be1809..64321e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,9 @@ ) )] #![no_std] -#[cfg(feature = "alloc")] +#[cfg(all(feature = "alloc", not(feature = "rustc-std-workspace-alloc")))] extern crate alloc; +#[cfg(all(feature = "alloc", feature = "rustc-std-workspace-alloc"))] +extern crate rustc_std_workspace_alloc as alloc; pub mod wasi_unstable; From efc4010393dca9bc70a71f5550279c436982e622 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Wed, 28 Aug 2019 14:00:45 +0300 Subject: [PATCH 22/23] review updates --- src/wasi_unstable/mod.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index f394991..cf7976c 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -19,6 +19,7 @@ pub type Advice = __wasi_advice_t; pub type ClockId = __wasi_clockid_t; pub type Device = __wasi_device_t; pub type DirCookie = __wasi_dircookie_t; +pub type Errno = __wasi_errno_t; pub type Error = NonZeroU16; pub type EventRwFlags = __wasi_eventrwflags_t; pub type EventType = __wasi_eventtype_t; @@ -625,12 +626,14 @@ pub fn args_get( // TODO: remove allocations after stabilization of unsized rvalues, see: // https://github.com/rust-lang/rust/issues/48055 - let mut argc = vec![core::ptr::null_mut::(); ars.count]; - let mut argv = vec![0u8; ars.buf_len]; - let ret = unsafe { __wasi_args_get(argc.as_mut_ptr(), argv.as_mut_ptr()) }; + let mut arg_ptrs = vec![core::ptr::null_mut::(); ars.count]; + let mut arg_buf = vec![0u8; ars.buf_len]; + let ret = unsafe { + __wasi_args_get(arg_ptrs.as_mut_ptr(), arg_buf.as_mut_ptr()) + }; if let Some(err) = NonZeroU16::new(ret) { return Err(err); } - for ptr in argc { + for ptr in arg_ptrs { for n in 0.. { unsafe { if *ptr.offset(n as isize) == 0 { @@ -677,12 +680,14 @@ pub fn environ_get( // TODO: remove allocations after stabilization of unsized rvalues, see: // https://github.com/rust-lang/rust/issues/48055 - let mut argc = vec![core::ptr::null_mut::(); es.count]; - let mut argv = vec![0u8; es.buf_len]; - let ret = unsafe { __wasi_environ_get(argc.as_mut_ptr(), argv.as_mut_ptr()) }; + let mut env_ptrs = vec![core::ptr::null_mut::(); es.count]; + let mut env_buf = vec![0u8; es.buf_len]; + let ret = unsafe { + __wasi_environ_get(env_ptrs.as_mut_ptr(), env_buf.as_mut_ptr()) + }; if let Some(err) = NonZeroU16::new(ret) { return Err(err); } - for ptr in argc { + for ptr in env_ptrs { let mut key: &[u8] = &[]; for n in 0.. { unsafe { From 35f7aab905ca23ae6b6ed504a64c2b38919c6454 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Thu, 29 Aug 2019 18:09:41 +0300 Subject: [PATCH 23/23] mark functions unsafe --- src/wasi_unstable/mod.rs | 206 +++++++++++++++++++-------------------- src/wasi_unstable/raw.rs | 3 - 2 files changed, 98 insertions(+), 111 deletions(-) diff --git a/src/wasi_unstable/mod.rs b/src/wasi_unstable/mod.rs index cf7976c..2ae35a1 100644 --- a/src/wasi_unstable/mod.rs +++ b/src/wasi_unstable/mod.rs @@ -258,136 +258,134 @@ pub const WHENCE_CUR: Whence = __WASI_WHENCE_CUR; pub const WHENCE_END: Whence = __WASI_WHENCE_END; pub const WHENCE_SET: Whence = __WASI_WHENCE_SET; -macro_rules! unsafe_wrap0 { +macro_rules! wrap0 { {$f:expr} => { - unsafe { - if let Some(code) = NonZeroU16::new($f) { - Err(code) - } else { - Ok(()) - } + if let Some(code) = NonZeroU16::new($f) { + Err(code) + } else { + Ok(()) } }; } -macro_rules! unsafe_wrap { +macro_rules! wrap { {$f:ident($($args:expr),* $(,)?)} => { let mut t = MaybeUninit::uninit(); - unsafe { - let r = $f($($args,)* t.as_mut_ptr()); - if let Some(code) = NonZeroU16::new(r) { - Err(code) - } else { - Ok(t.assume_init()) - } + let r = $f($($args,)* t.as_mut_ptr()); + if let Some(code) = NonZeroU16::new(r) { + Err(code) + } else { + Ok(t.assume_init()) } }; } #[inline] pub fn clock_res_get(clock_id: ClockId) -> Result { - unsafe_wrap!{ __wasi_clock_res_get(clock_id) } + unsafe { wrap!{ __wasi_clock_res_get(clock_id) } } } #[inline] pub fn clock_time_get(clock_id: ClockId, precision: Timestamp) -> Result { - unsafe_wrap!{ __wasi_clock_time_get(clock_id, precision) } + unsafe { wrap!{ __wasi_clock_time_get(clock_id, precision) } } } #[inline] -pub fn fd_pread(fd: Fd, iovs: &[IoVec], offset: FileSize) -> Result { - unsafe_wrap!{ __wasi_fd_pread(fd, iovs.as_ptr(), iovs.len(), offset) } +pub unsafe fn fd_pread(fd: Fd, iovs: &[IoVec], offset: FileSize) -> Result { + wrap!{ __wasi_fd_pread(fd, iovs.as_ptr(), iovs.len(), offset) } } #[inline] -pub fn fd_pwrite(fd: Fd, iovs: &[CIoVec], offset: FileSize) -> Result { - unsafe_wrap!{ __wasi_fd_pwrite(fd, iovs.as_ptr(), iovs.len(), offset) } +pub unsafe fn fd_pwrite(fd: Fd, iovs: &[CIoVec], offset: FileSize) -> Result { + wrap!{ __wasi_fd_pwrite(fd, iovs.as_ptr(), iovs.len(), offset) } } #[inline] pub fn random_get(buf: &mut [u8]) -> Result<(), Error> { - unsafe_wrap0!{ __wasi_random_get(buf.as_mut_ptr() as *mut c_void, buf.len()) } + unsafe { + wrap0!{ __wasi_random_get(buf.as_mut_ptr() as *mut c_void, buf.len()) } + } } #[inline] -pub fn fd_close(fd: Fd) -> Result<(), Error> { - unsafe_wrap0!{ __wasi_fd_close(fd) } +pub unsafe fn fd_close(fd: Fd) -> Result<(), Error> { + wrap0!{ __wasi_fd_close(fd) } } #[inline] -pub fn fd_datasync(fd: Fd) -> Result<(), Error> { - unsafe_wrap0!{ __wasi_fd_datasync(fd) } +pub unsafe fn fd_datasync(fd: Fd) -> Result<(), Error> { + wrap0!{ __wasi_fd_datasync(fd) } } #[inline] -pub fn fd_read(fd: Fd, iovs: &[IoVec]) -> Result { - unsafe_wrap!{ __wasi_fd_read(fd, iovs.as_ptr(), iovs.len()) } +pub unsafe fn fd_read(fd: Fd, iovs: &[IoVec]) -> Result { + wrap!{ __wasi_fd_read(fd, iovs.as_ptr(), iovs.len()) } } #[inline] -pub fn fd_renumber(from: Fd, to: Fd) -> Result<(), Error> { - unsafe_wrap0!{ __wasi_fd_renumber(from, to) } +pub unsafe fn fd_renumber(from: Fd, to: Fd) -> Result<(), Error> { + wrap0!{ __wasi_fd_renumber(from, to) } } #[inline] -pub fn fd_seek(fd: Fd, offset: FileDelta, whence: Whence) -> Result { - unsafe_wrap!{ __wasi_fd_seek(fd, offset, whence) } +pub unsafe fn fd_seek(fd: Fd, offset: FileDelta, whence: Whence) -> Result { + wrap!{ __wasi_fd_seek(fd, offset, whence) } } #[inline] -pub fn fd_tell(fd: Fd) -> Result { - unsafe_wrap!{ __wasi_fd_tell(fd) } +pub unsafe fn fd_tell(fd: Fd) -> Result { + wrap!{ __wasi_fd_tell(fd) } } #[inline] -pub fn fd_fdstat_get(fd: Fd) -> Result { - unsafe_wrap!{ __wasi_fd_fdstat_get(fd) } +pub unsafe fn fd_fdstat_get(fd: Fd) -> Result { + wrap!{ __wasi_fd_fdstat_get(fd) } } #[inline] -pub fn fd_fdstat_set_flags(fd: Fd, flags: FdFlags) -> Result<(), Error> { - unsafe_wrap0!{ __wasi_fd_fdstat_set_flags(fd, flags) } +pub unsafe fn fd_fdstat_set_flags(fd: Fd, flags: FdFlags) -> Result<(), Error> { + wrap0!{ __wasi_fd_fdstat_set_flags(fd, flags) } } #[inline] -pub fn fd_fdstat_set_rights(fd: Fd, fs_rights_base: Rights, fs_rights_inheriting: Rights) -> Result<(), Error> { - unsafe_wrap0!{ __wasi_fd_fdstat_set_rights(fd, fs_rights_base, fs_rights_inheriting) } +pub unsafe fn fd_fdstat_set_rights(fd: Fd, fs_rights_base: Rights, fs_rights_inheriting: Rights) -> Result<(), Error> { + wrap0!{ __wasi_fd_fdstat_set_rights(fd, fs_rights_base, fs_rights_inheriting) } } #[inline] -pub fn fd_sync(fd: Fd) -> Result<(), Error> { - unsafe_wrap0!{ __wasi_fd_sync(fd) } +pub unsafe fn fd_sync(fd: Fd) -> Result<(), Error> { + wrap0!{ __wasi_fd_sync(fd) } } #[inline] -pub fn fd_write(fd: Fd, iovs: &[CIoVec]) -> Result { - unsafe_wrap!{ __wasi_fd_write(fd, iovs.as_ptr(), iovs.len()) } +pub unsafe fn fd_write(fd: Fd, iovs: &[CIoVec]) -> Result { + wrap!{ __wasi_fd_write(fd, iovs.as_ptr(), iovs.len()) } } #[inline] -pub fn fd_advise(fd: Fd, offset: FileSize, len: FileSize, advice: Advice) -> Result<(), Error> { - unsafe_wrap0!{ __wasi_fd_advise(fd, offset, len, advice) } +pub unsafe fn fd_advise(fd: Fd, offset: FileSize, len: FileSize, advice: Advice) -> Result<(), Error> { + wrap0!{ __wasi_fd_advise(fd, offset, len, advice) } } #[inline] -pub fn fd_allocate(fd: Fd, offset: FileSize, len: FileSize) -> Result<(), Error> { - unsafe_wrap0!{ __wasi_fd_allocate(fd, offset, len) } +pub unsafe fn fd_allocate(fd: Fd, offset: FileSize, len: FileSize) -> Result<(), Error> { + wrap0!{ __wasi_fd_allocate(fd, offset, len) } } #[inline] -pub fn path_create_directory(fd: Fd, path: &[u8]) -> Result<(), Error> { - unsafe_wrap0!{ __wasi_path_create_directory(fd, path.as_ptr(), path.len()) } +pub unsafe fn path_create_directory(fd: Fd, path: &[u8]) -> Result<(), Error> { + wrap0!{ __wasi_path_create_directory(fd, path.as_ptr(), path.len()) } } #[inline] -pub fn path_link( +pub unsafe fn path_link( old_fd: Fd, old_flags: LookupFlags, old_path: &[u8], new_fd: Fd, new_path: &[u8], ) -> Result<(), Error> { - unsafe_wrap0!{ + wrap0!{ __wasi_path_link( old_fd, old_flags, @@ -401,7 +399,7 @@ pub fn path_link( } #[inline] -pub fn path_open( +pub unsafe fn path_open( dirfd: Fd, dirflags: LookupFlags, path: &[u8], @@ -410,7 +408,7 @@ pub fn path_open( fs_rights_inheriting: Rights, fs_flags: FdFlags, ) -> Result { - unsafe_wrap!{ + wrap!{ __wasi_path_open( dirfd, dirflags, @@ -425,22 +423,22 @@ pub fn path_open( } #[inline] -pub fn fd_readdir(fd: Fd, buf: &mut [u8], cookie: DirCookie) -> Result { +pub unsafe fn fd_readdir(fd: Fd, buf: &mut [u8], cookie: DirCookie) -> Result { let ptr = buf.as_mut_ptr() as *mut c_void; - unsafe_wrap!{ __wasi_fd_readdir(fd, ptr, buf.len(), cookie) } + wrap!{ __wasi_fd_readdir(fd, ptr, buf.len(), cookie) } } #[inline] -pub fn path_readlink(fd: Fd, path: &[u8], buf: &mut [u8]) -> Result { +pub unsafe fn path_readlink(fd: Fd, path: &[u8], buf: &mut [u8]) -> Result { let ptr = buf.as_mut_ptr(); - unsafe_wrap!{ + wrap!{ __wasi_path_readlink(fd, path.as_ptr(), path.len(), ptr, buf.len()) } } #[inline] -pub fn path_rename(old_fd: Fd, old_path: &[u8], new_fd: Fd, new_path: &[u8]) -> Result<(), Error> { - unsafe_wrap0!{ +pub unsafe fn path_rename(old_fd: Fd, old_path: &[u8], new_fd: Fd, new_path: &[u8]) -> Result<(), Error> { + wrap0!{ __wasi_path_rename( old_fd, old_path.as_ptr(), @@ -453,34 +451,34 @@ pub fn path_rename(old_fd: Fd, old_path: &[u8], new_fd: Fd, new_path: &[u8]) -> } #[inline] -pub fn fd_filestat_get(fd: Fd) -> Result { - unsafe_wrap!{ __wasi_fd_filestat_get(fd) } +pub unsafe fn fd_filestat_get(fd: Fd) -> Result { + wrap!{ __wasi_fd_filestat_get(fd) } } #[inline] -pub fn fd_filestat_set_times( +pub unsafe fn fd_filestat_set_times( fd: Fd, st_atim: Timestamp, st_mtim: Timestamp, fstflags: FstFlags, ) -> Result<(), Error> { - unsafe_wrap0!{ __wasi_fd_filestat_set_times(fd, st_atim, st_mtim, fstflags) } + wrap0!{ __wasi_fd_filestat_set_times(fd, st_atim, st_mtim, fstflags) } } #[inline] -pub fn fd_filestat_set_size(fd: Fd, st_size: FileSize) -> Result<(), Error> { - unsafe_wrap0!{ __wasi_fd_filestat_set_size(fd, st_size) } +pub unsafe fn fd_filestat_set_size(fd: Fd, st_size: FileSize) -> Result<(), Error> { + wrap0!{ __wasi_fd_filestat_set_size(fd, st_size) } } #[inline] -pub fn path_filestat_get(fd: Fd, flags: LookupFlags, path: &[u8]) -> Result { - unsafe_wrap!{ +pub unsafe fn path_filestat_get(fd: Fd, flags: LookupFlags, path: &[u8]) -> Result { + wrap!{ __wasi_path_filestat_get(fd, flags, path.as_ptr(), path.len()) } } #[inline] -pub fn path_filestat_set_times( +pub unsafe fn path_filestat_set_times( fd: Fd, flags: LookupFlags, path: &[u8], @@ -488,7 +486,7 @@ pub fn path_filestat_set_times( st_mtim: Timestamp, fstflags: FstFlags, ) -> Result<(), Error> { - unsafe_wrap0!{ + wrap0!{ __wasi_path_filestat_set_times( fd, flags, @@ -502,8 +500,8 @@ pub fn path_filestat_set_times( } #[inline] -pub fn path_symlink(old_path: &[u8], fd: Fd, new_path: &[u8]) -> Result<(), Error> { - unsafe_wrap0!{ +pub unsafe fn path_symlink(old_path: &[u8], fd: Fd, new_path: &[u8]) -> Result<(), Error> { + wrap0!{ __wasi_path_symlink( old_path.as_ptr(), old_path.len(), @@ -515,20 +513,20 @@ pub fn path_symlink(old_path: &[u8], fd: Fd, new_path: &[u8]) -> Result<(), Erro } #[inline] -pub fn path_unlink_file(fd: Fd, path: &[u8]) -> Result<(), Error> { - unsafe_wrap0!{ __wasi_path_unlink_file(fd, path.as_ptr(), path.len()) } +pub unsafe fn path_unlink_file(fd: Fd, path: &[u8]) -> Result<(), Error> { + wrap0!{ __wasi_path_unlink_file(fd, path.as_ptr(), path.len()) } } #[inline] -pub fn path_remove_directory(fd: Fd, path: &[u8]) -> Result<(), Error> { - unsafe_wrap0!{ __wasi_path_remove_directory(fd, path.as_ptr(), path.len()) } +pub unsafe fn path_remove_directory(fd: Fd, path: &[u8]) -> Result<(), Error> { + wrap0!{ __wasi_path_remove_directory(fd, path.as_ptr(), path.len()) } } #[inline] -pub fn poll_oneoff(in_: &[Subscription], out: &mut [Event]) -> Result { +pub unsafe fn poll_oneoff(in_: &[Subscription], out: &mut [Event]) -> Result { assert!(out.len() >= in_.len()); let ptr = out.as_mut_ptr() as *mut __wasi_event_t; - unsafe_wrap!{ + wrap!{ __wasi_poll_oneoff( in_.as_ptr(), ptr, @@ -543,55 +541,47 @@ pub fn proc_exit(rval: ExitCode) -> ! { } #[inline] -pub fn proc_raise(sig: Signal) -> Result<(), Error> { - unsafe_wrap0!{ __wasi_proc_raise(sig) } -} - -#[inline] -pub fn sock_recv(sock: Fd, ri_data: &[IoVec], ri_flags: RiFlags) -> Result<(usize, RoFlags), Error> { +pub unsafe fn sock_recv(sock: Fd, ri_data: &[IoVec], ri_flags: RiFlags) -> Result<(usize, RoFlags), Error> { let mut ro_datalen = MaybeUninit::::uninit(); let mut ro_flags = MaybeUninit::::uninit(); - - unsafe { - let r = __wasi_sock_recv( - sock, - ri_data.as_ptr(), - ri_data.len(), - ri_flags, - ro_datalen.as_mut_ptr(), - ro_flags.as_mut_ptr(), - ); - if let Some(code) = NonZeroU16::new(r) { - Err(code) - } else { - Ok((ro_datalen.assume_init(), ro_flags.assume_init())) - } + let r = __wasi_sock_recv( + sock, + ri_data.as_ptr(), + ri_data.len(), + ri_flags, + ro_datalen.as_mut_ptr(), + ro_flags.as_mut_ptr(), + ); + if let Some(code) = NonZeroU16::new(r) { + Err(code) + } else { + Ok((ro_datalen.assume_init(), ro_flags.assume_init())) } } #[inline] -pub fn sock_send(sock: Fd, si_data: &[CIoVec], si_flags: SiFlags) -> Result { - unsafe_wrap!{ __wasi_sock_send(sock, si_data.as_ptr(), si_data.len(), si_flags) } +pub unsafe fn sock_send(sock: Fd, si_data: &[CIoVec], si_flags: SiFlags) -> Result { + wrap!{ __wasi_sock_send(sock, si_data.as_ptr(), si_data.len(), si_flags) } } #[inline] -pub fn sock_shutdown(sock: Fd, how: SdFlags) -> Result<(), Error> { - unsafe_wrap0!{ __wasi_sock_shutdown(sock, how) } +pub unsafe fn sock_shutdown(sock: Fd, how: SdFlags) -> Result<(), Error> { + wrap0!{ __wasi_sock_shutdown(sock, how) } } #[inline] pub fn sched_yield() -> Result<(), Error> { - unsafe_wrap0!{ __wasi_sched_yield() } + unsafe { wrap0!{ __wasi_sched_yield() } } } #[inline] -pub fn fd_prestat_get(fd: Fd) -> Result { - unsafe_wrap!{ __wasi_fd_prestat_get(fd) } +pub unsafe fn fd_prestat_get(fd: Fd) -> Result { + wrap!{ __wasi_fd_prestat_get(fd) } } #[inline] -pub fn fd_prestat_dir_name(fd: Fd, path: &mut [u8]) -> Result<(), Error> { - unsafe_wrap0!{ __wasi_fd_prestat_dir_name(fd, path.as_mut_ptr(), path.len()) } +pub unsafe fn fd_prestat_dir_name(fd: Fd, path: &mut [u8]) -> Result<(), Error> { + wrap0!{ __wasi_fd_prestat_dir_name(fd, path.as_mut_ptr(), path.len()) } } #[derive(Copy, Clone)] diff --git a/src/wasi_unstable/raw.rs b/src/wasi_unstable/raw.rs index b9d53ff..962aef2 100644 --- a/src/wasi_unstable/raw.rs +++ b/src/wasi_unstable/raw.rs @@ -577,9 +577,6 @@ extern "C" { #[link_name = "proc_exit"] pub fn __wasi_proc_exit(rval: __wasi_exitcode_t) -> !; - #[link_name = "proc_raise"] - pub fn __wasi_proc_raise(sig: __wasi_signal_t) -> __wasi_errno_t; - #[link_name = "random_get"] pub fn __wasi_random_get(buf: *mut c_void, buf_len: usize) -> __wasi_errno_t;