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

Emscripten wasm32 compilation broken #85821

Closed
creikey opened this issue May 29, 2021 · 9 comments · Fixed by #97888
Closed

Emscripten wasm32 compilation broken #85821

creikey opened this issue May 29, 2021 · 9 comments · Fixed by #97888
Labels
C-bug Category: This is a bug. O-wasm Target: WASM (WebAssembly), http://webassembly.org/ P-high High priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@creikey
Copy link

creikey commented May 29, 2021

I tried this code:

fn main() {
    println!("Hello, world!");
}

I expected to see this happen: Compiling with rustc --target=wasm32-unknown-emscripten main.rs , after the emsdk is activated and emcc works, to compile properly

Instead, this happened: Prints this error:

  = note: error: undefined symbol: __gxx_personality_v0 (referenced by top-level compiled C/C++ code)
          warning: Link with `-s LLD_REPORT_UNDEFINED` to get more information on undefined symbols
          warning: To disable errors for undefined symbols use `-s ERROR_ON_UNDEFINED_SYMBOLS=0`
          warning: ___gxx_personality_v0 may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
          Error: Aborting compilation due to previous errors
          emcc: error: 'C:/Users/camer/Documents/Tech/emsdk/node/14.15.5_64bit/bin/node.exe C:\Users\camer\Documents\Tech\emsdk\upstream\emscripten\src\compiler.js C:\Users\camer\AppData\Local\Temp\tmprxn1kc58.txt' failed (returned 1)

I've tried adding -lstdc++ to no avail

Meta

rustc --version --verbose:

rustc 1.54.0-nightly (f58631b45 2021-05-28)
binary: rustc
commit-hash: f58631b4503b5cd34163cf9c3ff19dc3e0c8a09e
commit-date: 2021-05-28
host: x86_64-pc-windows-msvc
release: 1.54.0-nightly
LLVM version: 12.0.1
Backtrace

error: linking with `emcc.bat` failed: exit code: 1
  |
  = note: "cmd" "/c" "emcc.bat" "-s" "DISABLE_EXCEPTION_CATCHING=0" "-L" "C:\\Users\\camer\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\wasm32-unknown-emscripten\\lib" "-L" "C:\\Users\\camer\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\wasm32-unknown-emscripten\\lib\\self-contained" "main.main.7rcbfp3g-cgu.0.rcgu.o" "main.main.7rcbfp3g-cgu.1.rcgu.o" "main.main.7rcbfp3g-cgu.2.rcgu.o" "main.main.7rcbfp3g-cgu.3.rcgu.o" "main.main.7rcbfp3g-cgu.4.rcgu.o" "main.main.7rcbfp3g-cgu.5.rcgu.o" "main.main.7rcbfp3g-cgu.6.rcgu.o" "main.main.7rcbfp3g-cgu.7.rcgu.o" "-o" "main.js" "-s" "EXPORTED_FUNCTIONS=[\"_main\",\"_rust_eh_personality\"]" "main.4s37gsrti678ik8u.rcgu.o" "-O0" "--memory-init-file" "0" "-g0" "-s" "DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=[]" "-L" "C:\\Users\\camer\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\wasm32-unknown-emscripten\\lib" "C:\\Users\\camer\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\wasm32-unknown-emscripten\\lib\\libstd-fb3d38fb81b27f3a.rlib" "C:\\Users\\camer\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\wasm32-unknown-emscripten\\lib\\libpanic_unwind-d2cae8b45410ca8e.rlib" "C:\\Users\\camer\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\wasm32-unknown-emscripten\\lib\\libstd_detect-59eadd895a0069e7.rlib" "C:\\Users\\camer\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\wasm32-unknown-emscripten\\lib\\librustc_demangle-034447b1a63c6379.rlib" "C:\\Users\\camer\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\wasm32-unknown-emscripten\\lib\\libhashbrown-3b180b71fa933791.rlib" "C:\\Users\\camer\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\wasm32-unknown-emscripten\\lib\\librustc_std_workspace_alloc-5d053d604e1da3fd.rlib" "C:\\Users\\camer\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\wasm32-unknown-emscripten\\lib\\libunwind-835bbc670368e525.rlib" "C:\\Users\\camer\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\wasm32-unknown-emscripten\\lib\\libcfg_if-8111a663608e727d.rlib" "C:\\Users\\camer\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\wasm32-unknown-emscripten\\lib\\liblibc-35dde82b6021f4d8.rlib" "C:\\Users\\camer\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\wasm32-unknown-emscripten\\lib\\liballoc-a187fd59d7a86f05.rlib" "C:\\Users\\camer\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\wasm32-unknown-emscripten\\lib\\librustc_std_workspace_core-81a0f0dbfa7b5ebf.rlib" "C:\\Users\\camer\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\wasm32-unknown-emscripten\\lib\\libcore-ced9603eedaff686.rlib" "C:\\Users\\camer\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\wasm32-unknown-emscripten\\lib\\libcompiler_builtins-2960d62a9b1d31c1.rlib" "-l" "c" "-s" "ERROR_ON_UNDEFINED_SYMBOLS=1" "-s" "ASSERTIONS=1" "-s" "ABORTING_MALLOC=0" "-Wl,--fatal-warnings"
  = note: error: undefined symbol: __gxx_personality_v0 (referenced by top-level compiled C/C++ code)
          warning: Link with `-s LLD_REPORT_UNDEFINED` to get more information on undefined symbols
          warning: To disable errors for undefined symbols use `-s ERROR_ON_UNDEFINED_SYMBOLS=0`
          warning: ___gxx_personality_v0 may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
          Error: Aborting compilation due to previous errors
          emcc: error: 'C:/Users/camer/Documents/Tech/emsdk/node/14.15.5_64bit/bin/node.exe C:\Users\camer\Documents\Tech\emsdk\upstream\emscripten\src\compiler.js C:\Users\camer\AppData\Local\Temp\tmp6dw5_wby.txt' failed (returned 1)


error: aborting due to previous error

@creikey creikey added the C-bug Category: This is a bug. label May 29, 2021
@creikey
Copy link
Author

creikey commented May 29, 2021

emcc --check
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 2.0.23 (b15ca40ca02f469de3bf9a4de67a2655aede8974)

runs successfully

@creikey
Copy link
Author

creikey commented May 30, 2021

Also is broken on ubuntu with the same error message (at least windows subsystem for linux)

@creikey creikey changed the title Emscripten compilation broken on windows Emscripten wasm32 compilation broken May 30, 2021
@strager
Copy link

strager commented Jun 1, 2021

rustc 1.53.0-nightly (42816d6 2021-04-24)
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 2.0.12 (d0e647bf266caad50943e78c9841e05e9c499a5d)

Here's some info I gathered:

I successfully compiled hello world with emscripten SDK version 2.0.9. That version still has the __gxx_personality_v0 stub.

@inquisitivecrystal
Copy link
Contributor

@rustbot label I-prioritize T-compiler O-wasm

@rustbot rustbot added I-prioritize Issue: Indicates that prioritization has been requested for this issue. O-wasm Target: WASM (WebAssembly), http://webassembly.org/ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 5, 2021
@apiraino
Copy link
Contributor

apiraino commented Jun 9, 2021

thanks @strager for the analysis.

Assigning priority as discussed in the Zulip thread of the Prioritization Working Group. Let's find someone who can have a look at this.

@rustbot label -I-prioritize +P-high +I-nominated

@rustbot rustbot added I-nominated P-high High priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jun 9, 2021
@lopopolo
Copy link
Contributor

lopopolo commented Aug 5, 2021

I recall with the LLVM 11 upgrade, the compat notes mentioned which version of Emscripten to use when compiling with the wasm32-unknown-emscripten target.

https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1470-2020-10-08

Bumped the minimum supported Emscripten version to 1.39.20.

Once this bug is resolved, can we add a similar note for the releases that upgrade Rust to LLVM 12?

lopopolo added a commit to artichoke/artichoke that referenced this issue Aug 5, 2021
Followup to #1305 and #1306.

    rm -r artichoke-backend/vendor/emscripten/system/*
    cp -r ../playground/emsdk/upstream/emscripten/system/* artichoke-backend/vendor/emscripten/system/

These headers will match the version of emscripten used to compile the
playground with Rust 1.54.0 which requires LLVM 12.

See also rust-lang/rust#85821 for how this emsdk version was chosen.
lopopolo added a commit to artichoke/playground that referenced this issue Aug 5, 2021
Artichoke only compiles with Rust 1.54.0. Rust 1.54.0 is based on LLVM
12 (rust-lang/rust#81451). To ensure the `emcc` compiled sources interop
with Rust code, it is important to use an `emsdk` version that is ~close
to when it was branched from LLVM 12.

According to the release notes [0], 2.0.23 - 05/26/2021 release is the
first to be based on LLVM 12.

However, Rust depends on a stubbed function in the emsdk c++ ABI that
was removed in 2.0.10. According to rust-lang/rust#85821, we might have
luck compiling with 2.0.9

[0]: https://emscripten.org/docs/introducing_emscripten/release_notes.html

emscripten-toolchain
@nedv-eu
Copy link

nedv-eu commented Oct 7, 2021

As @strager mentioned the panic=unwind strategy is broken on wasm32-unknown-emscripten. I recommend to switch default to panic=abort. Note that also rust std lib has to be rebuilt with panic=abort to fix the undefined symbol errors. Testing on my side showed that the problem could be fixed by changing TargetOptions on line 38 to "panic_strategy: PanicStrategy::Abort," in rust/compiler/rustc_target/src/spec/wasm32_unknown_emscripten.rs file. Unfortunatelly I do not have experience in preparing pull request and have no capacity to get into it now.

@tmarkovski
Copy link

tmarkovski commented Nov 15, 2021

This issue can be solved by setting the env var export EMMAKEN_CFLAGS="-s ERROR_ON_UNDEFINED_SYMBOLS=0" which will override any flags passed to emcc.

See this comment - #41750 (comment)

Passing these flags using the .cargo/config won't work, as they are not passed to the linker.

EMMAKEN_CFLAGS="-s ERROR_ON_UNDEFINED_SYMBOLS=0 --no-entry" cargo build --target wasm32-unknown-emscripten

I received this error in two places, one for symbol __gxx_personality_v0 and one for main, hence the added --no-entry flag.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 13, 2021
Change default panic strategy to abort for wasm32-unknown-emscripten

Emscripten v2.0.10 removed __gxx_personality_v0 function stub that panic-unwind in wam32-unknown-emscripten target depends on. This causes linker error when using newer versions of emscripten compiler. As mentioned in rust-lang#85821 (comment) the __gxx_personality_v0 function was just a stub in emscripten for several years and therefor the panic-unwind strategy was broken all the time. Changing default to abort fixes builds  (issue 85821) with recent version of emscripten yet we are not loosing any functionality as the panic-unwind was broken anyway. Fixes  rust-lang#85821
flupke added a commit to jitter-video/rust-skia that referenced this issue Jan 16, 2022
This is adapted from https://github.com/tuxmark5/rust-skia.

Build is working with emscripten 3.1.1, but requires setting some
environment:

    EMCC_CFLAGS="-s ERROR_ON_UNDEFINED_SYMBOLS=0" EMSDK=~/.asdf/installs/emsdk/3.1.1 cargo build --target wasm32-unknown-emscripten

EMCC_CFLAGS=... is a workaround for:

    warning: ___gxx_personality_v0 may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library

This error appeared with emscripten 2.0.10 and has yet to be fixed in
rust: rust-lang/rust#85821 (comment)

EMSDK=... is for building skia-bindings against emscripten's platform
includes.
flupke added a commit to jitter-video/rust-skia that referenced this issue Jan 16, 2022
This is adapted from https://github.com/tuxmark5/rust-skia.

Build is working with emscripten 3.1.1, but requires setting some
environment variables:

    EMCC_CFLAGS="-s ERROR_ON_UNDEFINED_SYMBOLS=0" EMSDK=~/.asdf/installs/emsdk/3.1.1 cargo build --target wasm32-unknown-emscripten

EMCC_CFLAGS=... is a workaround for:

    warning: ___gxx_personality_v0 may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library

This error appeared with emscripten 2.0.10 and has yet to be fixed in
rust: rust-lang/rust#85821 (comment)

EMSDK=... is for building skia-bindings against emscripten's platform
includes.
flupke added a commit to jitter-video/rust-skia that referenced this issue Jan 17, 2022
Build is working with emscripten 3.1.1, but requires setting some
environment variables:

    EMCC_CFLAGS="-s ERROR_ON_UNDEFINED_SYMBOLS=0" EMSDK=~/.asdf/installs/emsdk/3.1.1 cargo build --target wasm32-unknown-emscripten

EMCC_CFLAGS=... is a workaround for:

    warning: ___gxx_personality_v0 may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library

This error appeared with emscripten 2.0.10 and has yet to be fixed in
rust: rust-lang/rust#85821 (comment)

EMSDK=... is for building skia-bindings against emscripten's platform
includes.

This is adapted from https://github.com/tuxmark5/rust-skia.
flupke added a commit to jitter-video/rust-skia that referenced this issue Jan 25, 2022
Build is working with emscripten 3.1.1, but requires setting some
environment variables:

    EMCC_CFLAGS="-s ERROR_ON_UNDEFINED_SYMBOLS=0" EMSDK=~/.asdf/installs/emsdk/3.1.1 cargo build --target wasm32-unknown-emscripten

EMCC_CFLAGS=... is a workaround for:

    warning: ___gxx_personality_v0 may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library

This error appeared with emscripten 2.0.10 and has yet to be fixed in
rust: rust-lang/rust#85821 (comment)

EMSDK=... is for building skia-bindings against emscripten's platform
includes.

This is adapted from https://github.com/tuxmark5/rust-skia.
flupke added a commit to jitter-video/rust-skia that referenced this issue Jan 26, 2022
Build is working with emscripten 3.1.1, but requires setting some
environment variables:

    EMCC_CFLAGS="-s ERROR_ON_UNDEFINED_SYMBOLS=0" EMSDK=~/.asdf/installs/emsdk/3.1.1 cargo build --target wasm32-unknown-emscripten

ERROR_ON_UNDEFINED_SYMBOLS is a workaround for:

    warning: ___gxx_personality_v0 may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library

This error appeared with emscripten 2.0.10 and has yet to be fixed in
rust: rust-lang/rust#85821 (comment)

EMSDK is for building skia-bindings against emscripten's platform
includes.

This is adapted from https://github.com/tuxmark5/rust-skia.
flupke added a commit to jitter-video/rust-skia that referenced this issue Jan 27, 2022
Build is working with emscripten 3.1.1, but requires setting some
environment variables:

    EMCC_CFLAGS="-s ERROR_ON_UNDEFINED_SYMBOLS=0" EMSDK=~/.asdf/installs/emsdk/3.1.1 cargo build --target wasm32-unknown-emscripten

ERROR_ON_UNDEFINED_SYMBOLS is a workaround for:

    warning: ___gxx_personality_v0 may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library

This error appeared with emscripten 2.0.10 and has yet to be fixed in
rust: rust-lang/rust#85821 (comment)

EMSDK is for building skia-bindings against emscripten's platform
includes.

This is adapted from https://github.com/tuxmark5/rust-skia.
flupke added a commit to jitter-video/rust-skia that referenced this issue Jan 27, 2022
Build is working with emscripten 3.1.1, but requires setting some
environment variables:

    EMCC_CFLAGS="-s ERROR_ON_UNDEFINED_SYMBOLS=0" EMSDK=~/.asdf/installs/emsdk/3.1.1 cargo build --target wasm32-unknown-emscripten

ERROR_ON_UNDEFINED_SYMBOLS is a workaround for:

    warning: ___gxx_personality_v0 may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library

This error appeared with emscripten 2.0.10 and has yet to be fixed in
rust: rust-lang/rust#85821 (comment)

EMSDK is for building skia-bindings against emscripten's platform
includes.

This is adapted from https://github.com/tuxmark5/rust-skia.
@hoodmane
Copy link
Contributor

hoodmane commented Jun 2, 2022

Reported this on Emscripten:
emscripten-core/emscripten#17128

JohnTitor added a commit to JohnTitor/rust that referenced this issue Jun 9, 2022
… r=Amanieu

Don't use __gxx_personality_v0 in panic_unwind on emscripten target

This resolves rust-lang#85821. See also the discussion here:
emscripten-core/emscripten#17128

The consensus seems to be that rust_eh_personality is never invoked.
I patched __gxx_personality_v0 to log invocations and then ran
various panic tests and it was never called, so this analysis matches
what seems to happen in practice. This replaces the definition with
an abort, modeled on the structured exception handling implementation.
JohnTitor added a commit to JohnTitor/rust that referenced this issue Jun 10, 2022
… r=Amanieu

Don't use __gxx_personality_v0 in panic_unwind on emscripten target

This resolves rust-lang#85821. See also the discussion here:
emscripten-core/emscripten#17128

The consensus seems to be that rust_eh_personality is never invoked.
I patched __gxx_personality_v0 to log invocations and then ran
various panic tests and it was never called, so this analysis matches
what seems to happen in practice. This replaces the definition with
an abort, modeled on the structured exception handling implementation.
@bors bors closed this as completed in 2ecbdc1 Jun 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. O-wasm Target: WASM (WebAssembly), http://webassembly.org/ P-high High priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
9 participants