Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update wasi dependency #100

Merged
merged 3 commits into from
Aug 31, 2019
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 @@ -29,7 +29,7 @@ core = { version = "1.0", optional = true, package = "rustc-std-workspace-core"
libc = { version = "0.2.62", default-features = false }

[target.'cfg(target_os = "wasi")'.dependencies]
wasi = "0.5"
wasi = "0.7"

[target.wasm32-unknown-unknown.dependencies]
wasm-bindgen = { version = "0.2.29", optional = true }
Expand Down
37 changes: 22 additions & 15 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,29 @@ impl Error {
}
}

#[cfg(any(unix, target_os = "redox"))]
fn os_err_desc(errno: i32, buf: &mut [u8]) -> Option<&str> {
let buf_ptr = buf.as_mut_ptr() as *mut libc::c_char;
if unsafe { libc::strerror_r(errno, buf_ptr, buf.len()) } != 0 {
return None;
}

// Take up to trailing null byte
let n = buf.len();
let idx = buf.iter().position(|&b| b == 0).unwrap_or(n);
core::str::from_utf8(&buf[..idx]).ok()
}
cfg_if! {
if #[cfg(unix)] {
josephlr marked this conversation as resolved.
Show resolved Hide resolved
fn os_err_desc(errno: i32, buf: &mut [u8]) -> Option<&str> {
let buf_ptr = buf.as_mut_ptr() as *mut libc::c_char;
if unsafe { libc::strerror_r(errno, buf_ptr, buf.len()) } != 0 {
return None;
}

#[cfg(not(any(unix, target_os = "redox")))]
fn os_err_desc(_errno: i32, _buf: &mut [u8]) -> Option<&str> {
None
// Take up to trailing null byte
let n = buf.len();
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_desc(errno: i32, _buf: &mut [u8]) -> Option<&str> {
core::num::NonZeroU16::new(errno as u16)
.and_then(wasi::wasi_unstable::error_str)
}
} else {
fn os_err_desc(_errno: i32, _buf: &mut [u8]) -> Option<&str> {
None
}
}
}

impl fmt::Debug for Error {
Expand Down
13 changes: 5 additions & 8 deletions src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@

//! Implementation for WASI
use crate::Error;
use core::num::NonZeroU32;
use core::num;
use wasi::wasi_unstable::random_get;

pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
let ret = random_get(dest);
if let Some(code) = NonZeroU32::new(ret as u32) {
error!("WASI: random_get failed with return value {}", code);
Err(Error::from(code))
} else {
Ok(()) // Zero means success for WASI
}
random_get(dest).map_err(|e: num::NonZeroU16| {
// convert wasi's NonZeroU16 error into getrandom's NonZeroU32 error
num::NonZeroU32::new(e.get() as u32).unwrap().into()
})
}