bytecodealliance / wasmtime-py

Python WebAssembly runtime powered by Wasmtime
https://bytecodealliance.github.io/wasmtime-py/
Apache License 2.0
381 stars 52 forks source link

Wasmtime-py and randomness #244

Open whitequark opened 6 days ago

whitequark commented 6 days ago

I am using wasmtime-py to create a fully deterministic sandbox (for reproducible build purposes) for a toolchain. The toolchain uses rand() or similar for Monte Carlo method computations, which AFAICT requests randomness from wasmtime.

Is there a way to tell wasmtime to only produce deterministic randomness? Is that something I can do via wasmtime-py? What does it do by default?

fitzgen commented 4 days ago

I think this would involve implementing a new version of https://docs.rs/wasmtime-wasi/latest/wasmtime_wasi/bindings/random/random/trait.Host.html and plumbing that through to wasmtime-py but I am not super familiar with this side of the code base.

Maybe @pchickey can give a better answer?

whitequark commented 4 days ago

So I just noticed this comment:

Deterministic environments must omit this function, rather than implementing it with deterministic data.

In this case I suppose wasmtime doesn't actually need to do anything; rather, I would have to go through my dependencies and see where, if anywhere, they use get_random_bytes. Let me do that quick...

fitzgen commented 4 days ago

The other thing you can do is virtualize the wasi:random/random interface with a component that uses deterministic seeds.

whitequark commented 4 days ago

That is what I'm doing for the Node/web hosted YoWASP! It uses a reasonable PRNG (xoroshiro* or something like that), with a seed of 1.

But it seems like I'm violating the interface by doing so. I don't think the application should actually link to get_random_bytes because it's intended to be deterministic, so this could be arguably a nextpnr bug...