Skip to content

Commit

Permalink
Merge #910
Browse files Browse the repository at this point in the history
910: Use getrandom instead of rand r=MarkMcCaskey a=newpavlov

Closes #909

Note: I am reusing `__WASI_EIO` as an error code to denote a potential OS RNG failure.

Co-authored-by: newpavlov <[email protected]>
Co-authored-by: Artyom Pavlov <[email protected]>
  • Loading branch information
bors[bot] and newpavlov authored Oct 28, 2019
2 parents 8653216 + 5647559 commit d46e5d4
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/emscripten/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ time = "0.1"
wasmer-runtime-core = { path = "../runtime-core", version = "0.9.0" }

[target.'cfg(windows)'.dependencies]
rand = "0.7"
getrandom = "0.1"

[features]
debug = ["wasmer-runtime-core/debug"]
4 changes: 2 additions & 2 deletions lib/emscripten/src/syscalls/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::utils::{copy_cstr_into_wasm, get_cstr_path};
use crate::varargs::VarArgs;
use libc::mkdir;
use libc::open;
use rand::Rng;
use std::env;
use std::ffi::CString;
use std::fs::File;
Expand Down Expand Up @@ -39,7 +38,8 @@ pub fn ___syscall5(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int {
let ptr = tmp_dir_c_str.as_ptr() as *const i8;
let mut urandom_file = File::create(tmp_dir).unwrap();
// create some random bytes and put them into the file
let random_bytes = rand::thread_rng().gen::<[u8; 32]>();
let mut random_bytes = [0u8; 32];
getrandom::getrandom(&mut random_bytes).unwrap();
let _ = urandom_file.write_all(&random_bytes).unwrap();
// put the file path string into wasm memory
let urandom_file_offset = unsafe { copy_cstr_into_wasm(ctx, ptr) };
Expand Down
2 changes: 1 addition & 1 deletion lib/wasi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ byteorder = "1.3"
generational-arena = { version = "0.2", features = ["serde"] }
libc = "0.2.60"
log = "0.4"
rand = "0.7"
getrandom = "0.1"
time = "0.1"
typetag = "0.1"
serde = { version = "1", features = ["derive"] }
Expand Down
12 changes: 6 additions & 6 deletions lib/wasi/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use crate::{
},
ExitCode,
};
use rand::{thread_rng, Rng};
use std::borrow::Borrow;
use std::cell::Cell;
use std::convert::{Infallible, TryInto};
Expand Down Expand Up @@ -2453,17 +2452,18 @@ pub fn proc_raise(ctx: &mut Ctx, sig: __wasi_signal_t) -> __wasi_errno_t {
/// The number of bytes that will be written
pub fn random_get(ctx: &mut Ctx, buf: WasmPtr<u8, Array>, buf_len: u32) -> __wasi_errno_t {
debug!("wasi::random_get buf_len: {}", buf_len);
let mut rng = thread_rng();
let memory = ctx.memory(0);

let buf = wasi_try!(buf.deref(memory, 0, buf_len));

unsafe {
let res = unsafe {
let u8_buffer = &mut *(buf as *const [_] as *mut [_] as *mut [u8]);
thread_rng().fill(u8_buffer);
getrandom::getrandom(u8_buffer)
};
match res {
Ok(()) => __WASI_ESUCCESS,
Err(_) => __WASI_EIO,
}

__WASI_ESUCCESS
}

/// ### `sched_yield()`
Expand Down

0 comments on commit d46e5d4

Please sign in to comment.