diff --git a/Cargo.toml b/Cargo.toml index 2024c8f9..6165cf11 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" libc = { version = "0.2.120", default-features = false } [target.'cfg(target_os = "wasi")'.dependencies] -wasi = "0.11" +wasi = { version = "0.11", default-features = false } [target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies] wasm-bindgen = { version = "0.2.62", default-features = false, optional = true } diff --git a/src/lib.rs b/src/lib.rs index e3ac1d87..36a49c01 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -238,7 +238,7 @@ cfg_if! { } else if #[cfg(target_os = "openbsd")] { mod util_libc; #[path = "openbsd.rs"] mod imp; - } else if #[cfg(target_os = "wasi")] { + } else if #[cfg(all(target_arch = "wasm32", target_os = "wasi"))] { #[path = "wasi.rs"] mod imp; } else if #[cfg(all(target_arch = "x86_64", target_os = "hermit"))] { #[path = "rdrand.rs"] mod imp; diff --git a/src/wasi.rs b/src/wasi.rs index f7a54f86..9276ee74 100644 --- a/src/wasi.rs +++ b/src/wasi.rs @@ -8,12 +8,18 @@ //! Implementation for WASI use crate::Error; -use core::{mem::MaybeUninit, num::NonZeroU32}; -use wasi::wasi_snapshot_preview1::random_get; +use core::{ + mem::MaybeUninit, + num::{NonZeroU16, NonZeroU32}, +}; +use wasi::random_get; pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { - match unsafe { random_get(dest.as_mut_ptr() as i32, dest.len() as i32) } { - 0 => Ok(()), - err => Err(unsafe { NonZeroU32::new_unchecked(err as u32) }.into()), - } + unsafe { random_get(dest.as_mut_ptr() as *mut u8, dest.len()) }.map_err(|e| { + // The WASI errno will always be non-zero, but we check just in case. + match NonZeroU16::new(e.raw()) { + Some(r) => Error::from(NonZeroU32::from(r)), + None => Error::ERRNO_NOT_POSITIVE, + } + }) }