Skip to content

Commit

Permalink
Cleanup wasm32-wasi target (#306)
Browse files Browse the repository at this point in the history
* Cleanup wasm32-wasi target

This change ensures that we only compile our WASI implementation for
32-bit targets. The interaction between the WASI proposal and the
memory64 proposal is not yet clear, [wasmtime does not yet support](
bytecodealliance/wasmtime#3594 (comment))
using WASI with memory64, and many of the interfaces use 32-bit values
for pointers.

This change also reduces the use of `unsafe` from the wasi
implementation. As noted in #253, changes to `Errno` mean that we can't
get the error message from the raw error code, but we can avoid using
unsafe when converting this code to a NonZeroU32. This handling also
makes WASI behave more like our other targets, which also manually check
that errno is non-zero.

Signed-off-by: Joe Richey <joerichey@google.com>

* Disable default features for WASI crate

Similar to this crate, the `wasi` crate just uses a `std` feature to
implement `std::error::Errno`, which we don't use.

Signed-off-by: Joe Richey <joerichey@google.com>

Signed-off-by: Joe Richey <joerichey@google.com>
  • Loading branch information
josephlr committed Oct 23, 2022
1 parent d87d339 commit ad08dd9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -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;
Expand Down
18 changes: 12 additions & 6 deletions src/wasi.rs
Expand Up @@ -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<u8>]) -> 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,
}
})
}

0 comments on commit ad08dd9

Please sign in to comment.