emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.75k stars 3.3k forks source link

Standalone: implement getentropy() via wasi_snapshot_preview1.random_get() #22782

Open martijneken opened 3 hours ago

martijneken commented 3 hours ago

Please include the following in your bug report:

Version of emscripten/emsdk: 3.1.67 (and likely latest)

Failing command line in full:

Compiling absl::random code in standalone mode results in a wasm module that crashes. Sample code:

absl::BitGen bitgen;
int digit = absl::Uniform(bitgen, 0, 2);

The reason is that Abseil under Emscripten uses the getentropy() syscall as a seed: https://github.com/abseil/abseil-cpp/blob/07fff76ed619672bb7bf00939231ba45aef2b043/absl/random/internal/seed_material.cc#L60-L64

And standalone Emscripten defaults to a crashing implementation: https://github.com/emscripten-core/emscripten/blob/b53978ee3f540dc74761eba127aa7f1b8761a125/system/lib/standalone/standalone.c#L139-L143

Would it be reasonable to use wasi_snapshot_preview1.random_get() to implement getentropy()?

sbc100 commented 2 hours ago

Yes, I think that would make sense.

There is a simple implementation in wasi-libc already: https://github.com/WebAssembly/wasi-libc/blob/main/libc-bottom-half/sources/getentropy.c

Would you like the send a PR? I imagine the hardest part would be figuring out where/how to test it.