Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use wasi::random_get?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the new Errno from wasi, which replaced Error, can actually hold 0. Errno::as_raw returns u16, which may be zero, thus invalidating the previous code calling NonZeroU32::new_unchecked. However handling zero ourselves ensures we can call NonZeroU32::new_unchecked again.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it won't actually be zero if the function returns Err, so it seems fine.

Copy link
Member

@newpavlov newpavlov Mar 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would mean that soundness of our code relies on an implementation detail of wasi. I am not sure we can rely on it, even though it's a very reasonable assumption.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would mean that soundness of our code relies on an implementation detail of wasi. I am not sure we can rely on it, even though it's a very reasonable assumption.

This is the exact reason I decided to switch.


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()),
}
}