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
26 changes: 26 additions & 0 deletions crates/test-programs/src/bin/p3_cli_random_limits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use test_programs::p3::wasi::random;

struct Component;

test_programs::p3::export!(Component);

impl test_programs::p3::exports::wasi::cli::run::Guest for Component {
async fn run() -> Result<(), ()> {
let args = std::env::args().collect::<Vec<_>>();
let args = args.iter().map(|s| s.as_str()).collect::<Vec<_>>();
match &args[1..] {
["random", n] => {
random::random::get_random_bytes(n.parse().unwrap());
}
["insecure", n] => {
random::insecure::get_insecure_random_bytes(n.parse().unwrap());
}
other => {
panic!("unexpected args: {other:?}");
}
}
Ok(())
}
}

fn main() {}
12 changes: 9 additions & 3 deletions crates/wasi/src/p3/random/host.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use cap_rand::Rng;
use cap_rand::distributions::Standard;

use crate::p3::bindings::random::{insecure, insecure_seed, random};
use crate::random::WasiRandomCtx;
use cap_rand::Rng;
use cap_rand::distributions::Standard;
use wasmtime::bail;

impl random::Host for WasiRandomCtx {
fn get_random_bytes(&mut self, len: u64) -> wasmtime::Result<Vec<u8>> {
if len > self.max_size {
bail!("requested len {len:?} exceeds limit {}", self.max_size);
}
Ok((&mut self.random)
.sample_iter(Standard)
.take(len as usize)
Expand All @@ -19,6 +22,9 @@ impl random::Host for WasiRandomCtx {

impl insecure::Host for WasiRandomCtx {
fn get_insecure_random_bytes(&mut self, len: u64) -> wasmtime::Result<Vec<u8>> {
if len > self.max_size {
bail!("requested len {len:?} exceeds limit {}", self.max_size);
}
Ok((&mut self.insecure_random)
.sample_iter(Standard)
.take(len as usize)
Expand Down
23 changes: 23 additions & 0 deletions tests/all/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2757,6 +2757,29 @@ start a print 1234
}
Ok(())
}

#[test]
fn p3_cli_random_limits() -> Result<()> {
let c = P3_CLI_RANDOM_LIMITS_COMPONENT;

for rand in ["random", "insecure"] {
run_wasmtime(&["run", "-Sp3", "-Wcomponent-model-async", c, rand, "256"])?;
assert!(
run_wasmtime(&[
"run",
"-Sp3",
"-Wcomponent-model-async",
"-Smax-random-size=255",
c,
rand,
"256"
])
.is_err()
);
}

Ok(())
}
}

#[test]
Expand Down