Skip to content

Commit

Permalink
Update to wasi 0.11 (#253)
Browse files Browse the repository at this point in the history
* Update to wasi 0.11

The main breaking change between v0.10 and v0.11 is that Error is
removed in favour of Errno. Unfortunately we can't create an Errno from
outside the wasi create so we're loosing some debug information for
errors.

I've opened an issue to add back such a constructor, see
<bytecodealliance/wasi-rs#64>.

* Use libc::strerror to get the error message on wasi

Since wasi v0.11 doesn't (yet) provided a way to create Errno, see
<bytecodealliance/wasi-rs#64>.

* Remove libc dependency for WASI

This does mean that we won't get an error message for the error type.
  • Loading branch information
Thomasdezeeuw authored Apr 15, 2022
1 parent 4882ac8 commit 2d65a40
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
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.10"
wasi = "0.11"

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
wasm-bindgen = { version = "0.2.62", default-features = false, optional = true }
Expand Down
4 changes: 0 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,6 @@ cfg_if! {
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(errno: i32, _buf: &mut [u8]) -> Option<wasi::Error> {
wasi::Error::from_raw_error(errno as _)
}
} else {
fn os_err(_errno: i32, _buf: &mut [u8]) -> Option<&str> {
None
Expand Down
12 changes: 4 additions & 8 deletions src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@
//! Implementation for WASI
use crate::Error;
use core::num::NonZeroU32;
use wasi::random_get;
use wasi::wasi_snapshot_preview1::random_get;

pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
unsafe {
random_get(dest.as_mut_ptr(), dest.len()).map_err(|e: wasi::Error| {
// convert wasi's Error into getrandom's NonZeroU32 error
// SAFETY: `wasi::Error` is `NonZeroU16` internally, so `e.raw_error()`
// will never return 0
NonZeroU32::new_unchecked(e.raw_error() as u32).into()
})
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()),
}
}

0 comments on commit 2d65a40

Please sign in to comment.