From 558b0195c7b3395edc386f5e3c7d53b4bd25aaa1 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Thu, 29 Aug 2019 18:09:41 +0300 Subject: [PATCH] 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;