Skip to main content

deterministic_wasi_ctx/
lib.rs

1mod clocks;
2mod scheduling;
3mod wasi_abi;
4
5use clocks::{DeterministicMonotonicClock, DeterministicWallClock};
6use rand_core::SeedableRng;
7use rand_pcg::Pcg64Mcg;
8pub use scheduling::{
9    replace_scheduling_functions, replace_scheduling_functions_for_wasi_preview_0,
10};
11use wasmtime_wasi::WasiCtxBuilder;
12
13pub fn add_determinism_to_wasi_ctx_builder(
14    wasi_builder: &mut WasiCtxBuilder,
15) -> &mut WasiCtxBuilder {
16    // Using Pcg64Mcg because it balances memory usage, performance, is adequately random, does not have major issues,
17    // and has reproducible results across different platforms. SmallRng and StdRng were considered but are documented
18    // as deterministic but not reproducible.
19    // See https://rust-random.github.io/book/guide-rngs.html#basic-pseudo-random-number-generators-prngs
20    // and https://docs.rs/rand_pcg/latest/rand_pcg/struct.Mcg128Xsl64.html for further details.
21    const RANDOM_SEED: u64 = 42; // the answer to life, the universe, and everything
22    let random = Pcg64Mcg::seed_from_u64(RANDOM_SEED);
23
24    wasi_builder
25        .allow_tcp(false)
26        .allow_udp(false)
27        .insecure_random(random.clone())
28        .secure_random(random)
29        .wall_clock(DeterministicWallClock)
30        .monotonic_clock(DeterministicMonotonicClock)
31}