implement randomness

This commit is contained in:
Pat Hickey
2021-01-04 12:20:42 -08:00
parent 01d74ceb73
commit 48554a1a5c
5 changed files with 50 additions and 4 deletions

View File

@@ -1,6 +1,4 @@
wasi_tests::big_random_buf
- no randomness yet
wasi_tests::clock_time_get wasi_tests::clock_time_get
- no clocks yet - no clocks yet
wasi_tests::dangling_symlink wasi_tests::dangling_symlink

View File

@@ -1,5 +1,6 @@
use crate::dir::{DirCaps, DirEntry, WasiDir}; use crate::dir::{DirCaps, DirEntry, WasiDir};
use crate::file::{FileCaps, FileEntry, WasiFile}; use crate::file::{FileCaps, FileEntry, WasiFile};
use crate::random::WasiRandom;
use crate::string_array::{StringArray, StringArrayError}; use crate::string_array::{StringArray, StringArrayError};
use crate::table::Table; use crate::table::Table;
use crate::Error; use crate::Error;
@@ -10,6 +11,7 @@ use std::rc::Rc;
pub struct WasiCtx { pub struct WasiCtx {
pub(crate) args: StringArray, pub(crate) args: StringArray,
pub(crate) env: StringArray, pub(crate) env: StringArray,
pub(crate) random: Box<dyn WasiRandom>,
table: Rc<RefCell<Table>>, table: Rc<RefCell<Table>>,
} }
@@ -22,6 +24,7 @@ impl WasiCtx {
WasiCtx { WasiCtx {
args: StringArray::new(), args: StringArray::new(),
env: StringArray::new(), env: StringArray::new(),
random: Box::new(crate::random::GetRandom),
table: Rc::new(RefCell::new(Table::new())), table: Rc::new(RefCell::new(Table::new())),
} }
} }
@@ -110,4 +113,9 @@ impl WasiCtxBuilder {
)))?; )))?;
Ok(self) Ok(self)
} }
pub fn random(&mut self, random: Box<dyn WasiRandom>) -> &mut Self {
self.0.random = random;
self
}
} }

View File

@@ -4,6 +4,7 @@ mod ctx;
mod dir; mod dir;
mod error; mod error;
mod file; mod file;
pub mod random;
pub mod snapshots; pub mod snapshots;
pub mod stdio; pub mod stdio;
mod string_array; mod string_array;

View File

@@ -0,0 +1,37 @@
use crate::Error;
use std::cell::RefCell;
pub trait WasiRandom {
fn get(&self, buf: &mut [u8]) -> Result<(), Error>;
}
pub struct GetRandom;
impl WasiRandom for GetRandom {
fn get(&self, buf: &mut [u8]) -> Result<(), Error> {
getrandom::getrandom(buf)?;
Ok(())
}
}
pub struct Deterministic {
sequence: RefCell<std::iter::Cycle<std::vec::IntoIter<u8>>>,
}
impl Deterministic {
pub fn new(bytes: Vec<u8>) -> Self {
Deterministic {
sequence: RefCell::new(bytes.into_iter().cycle()),
}
}
}
impl WasiRandom for Deterministic {
fn get(&self, buf: &mut [u8]) -> Result<(), Error> {
let mut s = self.sequence.borrow_mut();
for b in buf.iter_mut() {
*b = s.next().expect("infinite sequence");
}
Ok(())
}
}

View File

@@ -6,7 +6,7 @@ use fs_set_times::SystemTimeSpec;
use std::cell::{Ref, RefMut}; use std::cell::{Ref, RefMut};
use std::convert::{TryFrom, TryInto}; use std::convert::{TryFrom, TryInto};
use std::io::{IoSlice, IoSliceMut}; use std::io::{IoSlice, IoSliceMut};
use std::ops::Deref; use std::ops::{Deref, DerefMut};
use tracing::debug; use tracing::debug;
use wiggle::GuestPtr; use wiggle::GuestPtr;
@@ -801,7 +801,9 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
} }
fn random_get(&self, buf: &GuestPtr<u8>, buf_len: types::Size) -> Result<(), Error> { fn random_get(&self, buf: &GuestPtr<u8>, buf_len: types::Size) -> Result<(), Error> {
unimplemented!() let mut buf = buf.as_array(buf_len).as_slice_mut()?;
self.random.get(buf.deref_mut())?;
Ok(())
} }
fn sock_recv( fn sock_recv(