Skip to content

Commit ad08dd9

Browse files
authored
Cleanup wasm32-wasi target (#306)
* 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 <[email protected]> * 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 <[email protected]> Signed-off-by: Joe Richey <[email protected]>
1 parent d87d339 commit ad08dd9

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ core = { version = "1.0", optional = true, package = "rustc-std-workspace-core"
2121
libc = { version = "0.2.120", default-features = false }
2222

2323
[target.'cfg(target_os = "wasi")'.dependencies]
24-
wasi = "0.11"
24+
wasi = { version = "0.11", default-features = false }
2525

2626
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
2727
wasm-bindgen = { version = "0.2.62", default-features = false, optional = true }

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ cfg_if! {
238238
} else if #[cfg(target_os = "openbsd")] {
239239
mod util_libc;
240240
#[path = "openbsd.rs"] mod imp;
241-
} else if #[cfg(target_os = "wasi")] {
241+
} else if #[cfg(all(target_arch = "wasm32", target_os = "wasi"))] {
242242
#[path = "wasi.rs"] mod imp;
243243
} else if #[cfg(all(target_arch = "x86_64", target_os = "hermit"))] {
244244
#[path = "rdrand.rs"] mod imp;

src/wasi.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@
88

99
//! Implementation for WASI
1010
use crate::Error;
11-
use core::{mem::MaybeUninit, num::NonZeroU32};
12-
use wasi::wasi_snapshot_preview1::random_get;
11+
use core::{
12+
mem::MaybeUninit,
13+
num::{NonZeroU16, NonZeroU32},
14+
};
15+
use wasi::random_get;
1316

1417
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
15-
match unsafe { random_get(dest.as_mut_ptr() as i32, dest.len() as i32) } {
16-
0 => Ok(()),
17-
err => Err(unsafe { NonZeroU32::new_unchecked(err as u32) }.into()),
18-
}
18+
unsafe { random_get(dest.as_mut_ptr() as *mut u8, dest.len()) }.map_err(|e| {
19+
// The WASI errno will always be non-zero, but we check just in case.
20+
match NonZeroU16::new(e.raw()) {
21+
Some(r) => Error::from(NonZeroU32::from(r)),
22+
None => Error::ERRNO_NOT_POSITIVE,
23+
}
24+
})
1925
}

0 commit comments

Comments
 (0)