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

Support wasm32-unknown-emscripten #444

Merged
merged 2 commits into from
May 14, 2018
Merged

Conversation

huangjj27
Copy link

this pr is trying to fix #442

@huangjj27 huangjj27 changed the title Support wasm32-unknown-emscripten WIP: Support wasm32-unknown-emscripten May 12, 2018
@pitdicker
Copy link
Contributor

First time someone opened a PR for me 😄.

It seems my fix didn't work because the combination of featurres and target-specific features is not yet supported by cargo rust-lang/cargo#1197. I'll try just duplicating the cargo.toml section next.

@CryZe
Copy link

CryZe commented May 12, 2018

This does not seem correct, as wasm32-unknown-emscripten is a Unix, and thus both the unix impl and the stdweb impl would get compiled in. I'm pretty sure the unix implementation works just fine, so I'm not entirely sure what this is trying to fix.

@pitdicker
Copy link
Contributor

@CryZe Yes, that is what I am looking at at the moment. But compiling LLVM for emscripten takes a while...

Do you already have a fix?

@CryZe
Copy link

CryZe commented May 12, 2018

The chrono / time crates ran into the target feature problem as well. Nothing related to emscripten though, idk what emscripten has to do with this.

@CryZe
Copy link

CryZe commented May 12, 2018

Alright, I read a bit further about what is going on here and it seems to me like you should just bring in stdweb when the feature is activated (even though it's unused) but keep the unix implementation for emscripten.

@pitdicker
Copy link
Contributor

I have fixed it up enough to compile with wasm32-unknown-emscripten. But I get a linker error when trying to run tests:

# after endless stream of warnings
error: unresolved symbol: llvm_ctpop_i8

I am not really jumping on finding the cause of that...

@CryZe
Copy link

CryZe commented May 12, 2018

Why would the emscripten target use the stdweb implementation though, it already has a working implementation :(

src/rngs/os.rs Outdated
@@ -147,6 +147,7 @@ impl RngCore for OsRng {

#[cfg(all(unix,
not(target_os = "cloudabi"),
not(all(target_os = "emscripten", feature = "stdweb")),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't check the architecture, but the other impl checks the architecture. This leaves a hole where no impl is compiled in.

Cargo.toml Outdated
@@ -50,6 +50,10 @@ fuchsia-zircon = { version = "0.3.2", optional = true }
# use with `--target wasm32-unknown-unknown --features=stdweb`
stdweb = { version = "0.4", optional = true }

[target.wasm32-unknown-emscripten.dependencies]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This completely ignores the existence of the asmjs target or the experimental target.

src/rngs/os.rs Outdated
@@ -660,8 +661,7 @@ mod imp {
}

#[cfg(all(target_arch = "wasm32",
not(target_os = "emscripten"),
not(feature = "stdweb")))]
not(any(target_os = "emscripten", feature = "stdweb"))))]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still have no reason why the stdweb implementation is even used for the emscripten target, which was already working perfectly fine.

@pitdicker
Copy link
Contributor

pitdicker commented May 12, 2018

@CryZe I don't care about the WASM/js targets at all and don't know much about them either t.b.h., just trying to help out.

Can you help me out with a little background?

As I understand it, emscripten emulates a basic Unix environment. Because we have a generic Unix implementation that reads from /dev/urandom, it works all right. (Although I think the docs should note it may not be of crypto-quality depending on the browser etc., see https://github.com/kripken/emscripten/blob/incoming/src/library_fs.js#L1291).

Stdweb is an alternative for 'bare' webassembly, which lives behind the stdweb feature flag in rand. If someone decides to use stdweb, even when the emcripten environment is also available, is that wrong?

What is the idea of the different targets? I can keep wasm32-unknown-unknown and wasm32-unknown-emscripten apart. Is asmjs-unknown-emscripten still used, and used in combination with stdweb? And what is an experimental target?

Would this be right?

if architecture is wasm32 or asmjs
    if feature is stdweb
        use stdweb
    else if os is emscripten
        use /dev/urandom
    else
        use "not implemented" stub

@CryZe
Copy link

CryZe commented May 12, 2018

In my opinion it should be:

if architecture is unix (all emscripten targets)
    use /dev/urandom
else if wasm and not emscripten (so only wasm32-unknown-unknown atm)
    if stdweb
        use stdweb
    else
        use "not implemented" stub

You may be right that the stdweb implementation may be more cryptographically correct, but until we are sure about that I'd trust the emscripten implementation more.

The thing is that with the cargo bug, the stdweb feature accidentally gets carried over to the emscripten targets. So having the stdweb feature activated on those targets isn't really an indicator that you actually want the stdweb feature over the actual emscripten implementation.

Rough explanation of the individual targets:

  • asmjs-unknown-emscripten: Uses emscripten and compiles to asm.js
  • wasm32-unknown-emscripten: Uses emscripten and compiles to asm.js and then uses binaryen to compile to wasm
  • wasm32-experimental-emscripten: Uses LLVM's wasm target in combination with emscripten
  • wasm32-unknown-unknown: Uses the raw LLVM wasm target

@pitdicker
Copy link
Contributor

Thank you! With that approach I see changing only the line you pointed out does the trick.

@CryZe
Copy link

CryZe commented May 12, 2018

This looks good now 👍

@huangjj27 huangjj27 changed the title WIP: Support wasm32-unknown-emscripten Support wasm32-unknown-emscripten May 13, 2018
@huangjj27
Copy link
Author

huangjj27 commented May 13, 2018

@CryZe @pitdicker Thanks for your fixes! I'll inform the yew's author to make a test for the game of life.|

Update:
I'm sorry that I haven't made it clear that what I want is not support stdweb feature in emscripten but make the feature not to influence the target, so it's ok not to compile with stdweb when using wasm32-unknown-emscripten target.

@dhardy
Copy link
Member

dhardy commented May 14, 2018

I don't know much about this issue, but it looks like this PR is ready. @huangjj27 you can further discuss in #442 I guess.

@dhardy dhardy merged commit 22faded into rust-random:master May 14, 2018
@pitdicker pitdicker deleted the emscripten branch May 28, 2018 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants