From 0949abcdfcb626528ca63f422761030b8b3687eb Mon Sep 17 00:00:00 2001 From: newpavlov Date: Thu, 29 Aug 2019 18:45:10 +0300 Subject: [PATCH 1/3] update wasi dependency --- Cargo.toml | 2 +- src/wasi.rs | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cdf4e3cb..fe39d62f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" libc = { version = "0.2.62", default-features = false } [target.'cfg(target_os = "wasi")'.dependencies] -wasi = "0.5" +wasi = "0.7" [target.wasm32-unknown-unknown.dependencies] wasm-bindgen = { version = "0.2.29", optional = true } diff --git a/src/wasi.rs b/src/wasi.rs index a050afc8..735d33f1 100644 --- a/src/wasi.rs +++ b/src/wasi.rs @@ -12,11 +12,9 @@ use core::num::NonZeroU32; use wasi::wasi_unstable::random_get; pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { - let ret = random_get(dest); - if let Some(code) = NonZeroU32::new(ret as u32) { - error!("WASI: random_get failed with return value {}", code); - Err(Error::from(code)) - } else { - Ok(()) // Zero means success for WASI - } + // wasi uses NonZeroU16 for error codes + // here compiler is able to eliminate `unwrap` + random_get(dest).map_err(|e| { + Error(NonZeroU32::new(e.get() as u32).unwrap()) + }) } From 92b3a9fc96d8ea9ab7af51b00925fb470aafa3d9 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Thu, 29 Aug 2019 19:01:01 +0300 Subject: [PATCH 2/3] fix --- src/wasi.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wasi.rs b/src/wasi.rs index 735d33f1..d83dcd60 100644 --- a/src/wasi.rs +++ b/src/wasi.rs @@ -15,6 +15,6 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { // wasi uses NonZeroU16 for error codes // here compiler is able to eliminate `unwrap` random_get(dest).map_err(|e| { - Error(NonZeroU32::new(e.get() as u32).unwrap()) + NonZeroU32::new(e.get() as u32).unwrap().into() }) } From 9d2c82b0ad2f34451ad0d90c13b091959e2b2286 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Thu, 29 Aug 2019 19:22:12 +0300 Subject: [PATCH 3/3] add error descriptions, change comment --- src/error.rs | 37 ++++++++++++++++++++++--------------- src/wasi.rs | 9 ++++----- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/error.rs b/src/error.rs index c68a7677..a27a750e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -60,22 +60,29 @@ impl Error { } } -#[cfg(any(unix, target_os = "redox"))] -fn os_err_desc(errno: i32, buf: &mut [u8]) -> Option<&str> { - let buf_ptr = buf.as_mut_ptr() as *mut libc::c_char; - if unsafe { libc::strerror_r(errno, buf_ptr, buf.len()) } != 0 { - return None; - } - - // Take up to trailing null byte - let n = buf.len(); - let idx = buf.iter().position(|&b| b == 0).unwrap_or(n); - core::str::from_utf8(&buf[..idx]).ok() -} +cfg_if! { + if #[cfg(unix)] { + fn os_err_desc(errno: i32, buf: &mut [u8]) -> Option<&str> { + let buf_ptr = buf.as_mut_ptr() as *mut libc::c_char; + if unsafe { libc::strerror_r(errno, buf_ptr, buf.len()) } != 0 { + return None; + } -#[cfg(not(any(unix, target_os = "redox")))] -fn os_err_desc(_errno: i32, _buf: &mut [u8]) -> Option<&str> { - None + // Take up to trailing null byte + let n = buf.len(); + let idx = buf.iter().position(|&b| b == 0).unwrap_or(n); + core::str::from_utf8(&buf[..idx]).ok() + } + } else if #[cfg(target_os = "wasi")] { + fn os_err_desc(errno: i32, _buf: &mut [u8]) -> Option<&str> { + core::num::NonZeroU16::new(errno as u16) + .and_then(wasi::wasi_unstable::error_str) + } + } else { + fn os_err_desc(_errno: i32, _buf: &mut [u8]) -> Option<&str> { + None + } + } } impl fmt::Debug for Error { diff --git a/src/wasi.rs b/src/wasi.rs index d83dcd60..713c1ab9 100644 --- a/src/wasi.rs +++ b/src/wasi.rs @@ -8,13 +8,12 @@ //! Implementation for WASI use crate::Error; -use core::num::NonZeroU32; +use core::num; use wasi::wasi_unstable::random_get; pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { - // wasi uses NonZeroU16 for error codes - // here compiler is able to eliminate `unwrap` - random_get(dest).map_err(|e| { - NonZeroU32::new(e.get() as u32).unwrap().into() + random_get(dest).map_err(|e: num::NonZeroU16| { + // convert wasi's NonZeroU16 error into getrandom's NonZeroU32 error + num::NonZeroU32::new(e.get() as u32).unwrap().into() }) }