Skip to content

Commit

Permalink
implement random_get()
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McCaskey committed Mar 29, 2019
1 parent 28d9d1f commit 88212d3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion lib/wasi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ edition = "2018"

[dependencies]
wasmer-runtime-core = { path = "../runtime-core", version = "0.2.1" }
libc = "0.2.50"
libc = "0.2.50"
rand = "0.6.5"
29 changes: 27 additions & 2 deletions lib/wasi/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
ptr::{Array, WasmPtr},
state::WasiState,
};
use rand::{thread_rng, Rng};
use wasmer_runtime_core::{memory::Memory, vm::Ctx};

#[cfg(any(target_os = "linux", target_os = "macos"))]
Expand Down Expand Up @@ -451,12 +452,36 @@ pub fn proc_exit(ctx: &mut Ctx, rval: __wasi_exitcode_t) {
pub fn proc_raise(ctx: &mut Ctx, sig: __wasi_signal_t) -> __wasi_errno_t {
unimplemented!()
}

/// ### `random_get()`
/// Fill buffer with high-quality random data. This function may be slow and block
/// Inputs:
/// - `void *buf`
/// A pointer to a buffer where the random bytes will be written
/// - `size_t buf_len`
/// 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 {
unimplemented!()
let mut rng = thread_rng();
let memory = ctx.memory(0);

if let Some(buf) = buf.deref(memory, 0, buf_len) {
for i in 0..(buf_len as usize) {
let random_byte = rng.gen::<u8>();
buf[i].set(random_byte);
}
} else {
return __WASI_EFAULT;
}

__WASI_ESUCCESS
}

/// ### `sched_yield()`
/// Yields execution of the thread
pub fn sched_yield(ctx: &mut Ctx) -> __wasi_errno_t {
unimplemented!()
__WASI_ESUCCESS
}

pub fn sock_recv(
ctx: &mut Ctx,
sock: __wasi_fd_t,
Expand Down

0 comments on commit 88212d3

Please sign in to comment.