Since WASI has a random_get() function, I expected to be able to use std::random_device from C++ when compiling with -s PURE_WASI.
Version of emscripten/emsdk:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.68 (ceee49d2ecdab36a3feb85a684f8e5a453dde910)
clang version 20.0.0git (https:/github.com/llvm/llvm-project 5cc64bf60bc04b9315de3c679eb753de4d554a8a)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /Users/geraintluff/Development/emsdk/upstream/bin
Failing command line in full:
CPP source:
#include <iostream>
#include <random>
int main() {
std::random_device device;
std::random_device::result_type v = device();
std::cout << "random value: " << v << "\n";
}
Compiled like:
emcc main.cpp -o main.wasm -s PURE_WASI
In a web-page, list the imports and then run:
<body>
<script type="module">
import {WASI} from 'https://cdn.jsdelivr.net/npm/@bjorn3/browser_wasi_shim@0.3.0/+esm';
let module = await WebAssembly.compileStreaming(fetch('./main.wasm'));
// List imports
console.log(WebAssembly.Module.imports(module));
let wasi = new WASI([], [], []);
let instance = await WebAssembly.instantiate(module, {
"wasi_snapshot_preview1": wasi.wasiImport,
});
wasi.start(instance);
</script>
</body>
The imports are: proc_exit, fd_seek, fd_write, fd_read, fd_close, environ_sizes_get, environ_get from wasi_snapshot_preview1. No random_get.
However running the instance throws:
main.wasm:0x4fd9 Uncaught RuntimeError: unreachable
at main.wasm._abort_js (main.wasm:0x4fd9)
at main.wasm.abort (main.wasm:0x24bc)
at main.wasm.getentropy (main.wasm:0x503d)
at main.wasm.std::__2::random_device::operator(tmp/)() (http://localhost:8000/main.wasm)
at main.wasm.__original_main (main.wasm:0xd97)
at main.wasm._start (main.wasm:0x2201)
at E.start (wasi.js:1:252)
at tmp/:13:8
If the call to device() in the C++ code is replaced with something else, it runs fine with no errors.
Since WASI has a
random_get()
function, I expected to be able to usestd::random_device
from C++ when compiling with-s PURE_WASI
.Version of emscripten/emsdk: emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.68 (ceee49d2ecdab36a3feb85a684f8e5a453dde910) clang version 20.0.0git (https:/github.com/llvm/llvm-project 5cc64bf60bc04b9315de3c679eb753de4d554a8a) Target: wasm32-unknown-emscripten Thread model: posix InstalledDir: /Users/geraintluff/Development/emsdk/upstream/bin
Failing command line in full:
CPP source:
Compiled like:
In a web-page, list the imports and then run:
The imports are:
proc_exit
,fd_seek
,fd_write
,fd_read
,fd_close
,environ_sizes_get
,environ_get
fromwasi_snapshot_preview1
. Norandom_get
.However running the instance throws:
If the call to
device()
in the C++ code is replaced with something else, it runs fine with no errors.Full link command and output with
-v
appended: