Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

thread_rng can't get random number in a WASI module #460

Closed
autodidaddict opened this issue May 22, 2019 · 12 comments
Closed

thread_rng can't get random number in a WASI module #460

autodidaddict opened this issue May 22, 2019 · 12 comments

Comments

@autodidaddict
Copy link

Runtime panic (trap) when getting random number

Rust code in the compiled WASI module: let mut rng = thread_rng();

My system:

wasmer 0.4.2 | rustc 1.34.2 (6c2484dc3 2019-05-13) | x86_64

Steps to reproduce

  1. Create a Rust project
  2. Have that project invoke thread_rng() to produce a RNG
  3. Compile Rust target to WASI
  4. attempt to run module with wasmer

Expected behavior

According to the WASI spec, there's a __wasi_rnd_get() function that I figured would be invoked underneath the rand crate's compiled WASI code.

Actual behavior

thread 'main' panicked at 'could not initialize thread_rng: All entropy sources failed (permanently unavailable); cause: OsRng: support for wasm32 requires emscripten, stdweb or wasm-bindgen (permanently unavailable)', /home/kevin/.cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.6.5/src/rngs/thread.rs:80:17

This error message reads like someone's compile-time error message that doesn't take into account the shunting being done by the WASI code and its relevant imports.

If we can't use random numbers, things are going to be difficult compiling to WASI.

Additional context

I'm perfectly fine with cheap workarounds. I don't need cryptographic random, I'm just using this to shuffle a list of servers to connect with, so if there are some lines of Rust I can replace my code with that will pick a random number between 0 and n or let me invoke shuffle on a vec and not cause a wasmer-time trap, that'd be awesome.

p.s. I don't know if the problem is in the runtime wasmer's providing or in the code that Rust is generating via the wasm32-wasi target. Apologies if this is not your issue at all.

@autodidaddict autodidaddict added the bug Something isn't working label May 22, 2019
@autodidaddict
Copy link
Author

I've completely removed all random number generation from my own code, but it looks like somewhere in the dependency tree there's still some other code using thread_rng from the rand crate.

@Hywan
Copy link
Contributor

Hywan commented May 22, 2019

Did you consider using https://github.com/rust-random/rand to get random number with WebAssembly?

@autodidaddict
Copy link
Author

@Hywan yeah, like I said I removed my own random code. Problem is the use of the rand crate is littered throughout my dependency tree.

@Hywan
Copy link
Contributor

Hywan commented May 22, 2019

Try to run cargo update or even cargo clean at some point.

@autodidaddict
Copy link
Author

I've done both. I can actually compile the wasm to the wasm32-wasi target. It just panics as soon as wasmer 's execution hits a spot that tries to invoke thread_rng()

@MarkMcCaskey
Copy link
Contributor

Thanks for the report! I'll investigate and write a test covering this case

@MarkMcCaskey
Copy link
Contributor

The error message is originating past the rand crate.

See rust-random/rand#769 for more information.

And no, we don't have an ETA on the Rand 0.7 release. See #715 and the milestone. Ideally it will be within a couple of months.

That was early April, so hopefully WASI support will be coming soon!

@huangjj27
Copy link

Encountering the same issue.

@jedisct1
Copy link

You can use the getrandom crate instead.

@MarkMcCaskey MarkMcCaskey removed the bug Something isn't working label Jun 4, 2019
@kubkon
Copy link
Contributor

kubkon commented Jun 12, 2019

The error message is originating past the rand crate.

See rust-random/rand#769 for more information.

And no, we don't have an ETA on the Rand 0.7 release. See #715 and the milestone. Ideally it will be within a couple of months.

That was early April, so hopefully WASI support will be coming soon!

Hey guys! I don't know whether this issue is still relevant, but just wanted to let you know that the prelease version 0.7.0-pre.0 of rust-random/rand comes with wasm32-wasi support :-) So you can just specify it in your Cargo.toml as:

[dependencies]                                                                                                          
rand = "0.7.0-pre.0"

@huangjj27
Copy link

huangjj27 commented Jun 15, 2019

Hey guys! I don't know whether this issue is still relevant, but just wanted to let you know that the prelease version 0.7.0-pre.0 of rust-random/rand comes with wasm32-wasi support

Error has changed. and I found that getrandom should support the WASI. It just doesn't support for wasm32-unknown-wasi, but works with wasm32-wasi

thread 'main' panicked at 'could not initialize thread_rng: getrandom: unavailable', C:\Users\34937\.cargo\registry\src\mirrors.sjtug.sjtu.edu.cn-7a04d2510079875b\rand-0.7.0-pre.1\src\rngs\thread.rs:67:17
thread 'main' panicked at 'wasm trap occured: unreachable', src\bin\wasmer.rs:515:51

@syrusakbary
Copy link
Member

This is fixed in the new version of rand: 0.7
rust-random/rand#715

Closing the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants