rust-lang / rust-lang/rust

[wasm32-unknown-emscripten] undefined symbols: longjmp, setjmp

Open
#118,570 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug O-emscripten O-wasm
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

When the target is wasm32-unknown-emscripten, unable to find symbols: longjmp, setjmp, setjmp-longjmp support is enabled by default in Emscripten.
https://emscripten.org/docs/porting/setjmp-longjmp.html

I tried this code:

use ::libc;
extern "C" {
    fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
    fn setjmp(_: *mut libc::c_void) -> libc::c_int;
    fn longjmp(_: *mut libc::c_void, _: libc::c_int) -> !;
}


pub static mut exception_buffer: [u8; 100] = [0u8; 100];

pub unsafe extern "C" fn do_something(value: libc::c_int) {
    if value < 0 as libc::c_int {
        printf(
            b"do_something: Error! Value cannot be negative.\n\0" as *const u8
                as *const libc::c_char,
        );
        longjmp(
            exception_buffer.as_mut_ptr() as *mut libc::c_void,
            1 as libc::c_int,
        );
    }
    printf(
        b"do_something: Value is %d\n\0" as *const u8 as *const libc::c_char,
        value,
    );
}
fn main() {
    unsafe {
        let value: libc::c_int = -(5 as libc::c_int);
        if setjmp(exception_buffer.as_mut_ptr() as *mut libc::c_void) == 0 as libc::c_int {
            printf(
                b"Main: Setting up exception handling...\n\0" as *const u8 as *const libc::c_char,
            );
            do_something(value);
        } else {
            printf(b"Main: Handling exception...\n\0" as *const u8 as *const libc::c_char);
        }
        printf(
            b"Main: Program continues after exception handling.\n\0" as *const u8
                as *const libc::c_char,
        );
    }
}

I expected to see this happen: explanation
cargo run

Main: Setting up exception handling...
do_something: Error! Value cannot be negative.
Main: Handling exception...
Main: Program continues after exception handling.

Instead, this happened: explanation
cargo run --target=wasm32-unknown-emscripten

  = note: wasm-ld: error: /root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.5eeji6ymekqs85b7.rcgu.o: undefined symbol: longjmp
          wasm-ld: error: /root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.5eeji6ymekqs85b7.rcgu.o: undefined symbol: setjmp

Meta

rustc --version --verbose:

rustc 1.76.0-nightly (2b603f95a 2023-11-12)
binary: rustc
commit-hash: 2b603f95a48f10f931a61dd208fe3e5ffd64e491
commit-date: 2023-11-12
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.4


rustc 1.74.0 (79e9716c9 2023-11-13)
binary: rustc
commit-hash: 79e9716c980570bfd1f666e3b16ac583f0168962
commit-date: 2023-11-13
host: x86_64-unknown-linux-gnu
release: 1.74.0
LLVM version: 17.0.4
Backtrace

RUST_BACKTRACE=1 cargo build --target=wasm32-unknown-emscripten#                                                                                                            
u23#  RUST_BACKTRACE=1 cargo build --target=wasm32-unknown-emscripten
   Compiling emsdk-demo v0.1.0 (/root/code/emsdk-demo)
warning: static variable `exception_buffer` should have an upper case name
 --> src/main.rs:9:16
  |
9 | pub static mut exception_buffer: [u8; 100] = [0u8; 100];
  |                ^^^^^^^^^^^^^^^^ help: convert the identifier to upper case: `EXCEPTION_BUFFER`
  |
  = note: `#[warn(non_upper_case_globals)]` on by default

error: linking with `emcc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin/self-contained:/root/code/emsdk:/root/code/emsdk/upstream/emscripten:/run/user/0/fnm_multishells/51368_1701605570612/bin:/root/.vscode-server/bin/1a5daa3a0231a0fbba4f14db7ec463cf99d7768e/bin/remote-cli:/root/.local/share/pnpm:/root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin" VSLANG="1033" "emcc" "-s" "EXPORTED_FUNCTIONS=[\"_main\"]" "/root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.1wof4idzl24j4p0a.rcgu.o" "/root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.2nt1liv8b0dfeizm.rcgu.o" "/root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.43djbu2r1eeqpn12.rcgu.o" "/root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.5eeji6ymekqs85b7.rcgu.o" "/root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.vg9f46yl3mh9xiu.rcgu.o" "/root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.2t7mycb9l7k0tgy.rcgu.o" "-L" "/root/ct/wasm32-unknown-emscripten/debug/deps" "-L" "/root/ct/debug/deps" "-L" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib" "/root/ct/wasm32-unknown-emscripten/debug/deps/liblibc-9bc98c84e1a910dc.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libstd-61e93cdfb239f7c2.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libpanic_unwind-c51bd600d3569b2d.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/librustc_demangle-8e67090878f28f69.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libstd_detect-3fb9a4c301477b79.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libhashbrown-5c1b3f61e45e4bf0.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/librustc_std_workspace_alloc-f99cb7d3c28bae00.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libminiz_oxide-942831f28a82cd36.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libadler-8e620a0166013244.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libunwind-b17b8f660cd36185.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libcfg_if-0c7bacfbf87cda18.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/liblibc-48b2b822b890a628.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/liballoc-e119f963323764f2.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/librustc_std_workspace_core-dccfe230458f39f9.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libcore-ff850c1994b8a1ec.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libcompiler_builtins-295fcaa60ab90f2f.rlib" "-l" "c" "-B/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin/gcc-ld" "-fuse-ld=lld" "--target=wasm32-unknown-emscripten" "-s" "DISABLE_EXCEPTION_CATCHING=0" "-L" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib" "-L" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/self-contained" "-o" "/root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.js" "-O0" "-g" "-sABORTING_MALLOC=0" "-Wl,--fatal-warnings"
  = note: wasm-ld: error: /root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.5eeji6ymekqs85b7.rcgu.o: undefined symbol: longjmp
          wasm-ld: error: /root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.5eeji6ymekqs85b7.rcgu.o: undefined symbol: setjmp
          emcc: error: '/root/code/emsdk/upstream/bin/wasm-ld -o /root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.wasm /root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.1wof4idzl24j4p0a.rcgu.o /root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.2nt1liv8b0dfeizm.rcgu.o /root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.43djbu2r1eeqpn12.rcgu.o /root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.5eeji6ymekqs85b7.rcgu.o /root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.vg9f46yl3mh9xiu.rcgu.o /root/ct/wasm32-unknown-emscripten/debug/deps/emsdk_demo.2t7mycb9l7k0tgy.rcgu.o -L/root/ct/wasm32-unknown-emscripten/debug/deps -L/root/ct/debug/deps -L/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib /root/ct/wasm32-unknown-emscripten/debug/deps/liblibc-9bc98c84e1a910dc.rlib /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libstd-61e93cdfb239f7c2.rlib /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libpanic_unwind-c51bd600d3569b2d.rlib /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/librustc_demangle-8e67090878f28f69.rlib /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libstd_detect-3fb9a4c301477b79.rlib /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libhashbrown-5c1b3f61e45e4bf0.rlib /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/librustc_std_workspace_alloc-f99cb7d3c28bae00.rlib /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libminiz_oxide-942831f28a82cd36.rlib /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libadler-8e620a0166013244.rlib /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libunwind-b17b8f660cd36185.rlib /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libcfg_if-0c7bacfbf87cda18.rlib /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/liblibc-48b2b822b890a628.rlib /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/liballoc-e119f963323764f2.rlib /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/librustc_std_workspace_core-dccfe230458f39f9.rlib /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libcore-ff850c1994b8a1ec.rlib /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/libcompiler_builtins-295fcaa60ab90f2f.rlib -lc-debug -L/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib -L/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-emscripten/lib/self-contained --fatal-warnings -L/root/code/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten -lGL -lal -lhtml5 -lstubs-debug -lnoexit -lc-debug -ldlmalloc -lcompiler_rt -lc++ -lc++abi-debug -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-cxx-exceptions -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr /tmp/tmpg1ciymx5libemscripten_js_symbols.so --export-if-defined=main --export-if-defined=__get_exception_message --export-if-defined=free --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=__main_argc_argv --export-if-defined=fflush --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_get_current --export=emscripten_stack_init --export=stackSave --export=stackRestore --export=stackAlloc --export=__errno_location --export=__get_temp_ret --export=__set_temp_ret --export=__cxa_is_pointer_type --export=__cxa_can_catch --export=__cxa_increment_exception_refcount --export=__cxa_decrement_exception_refcount --export=setThrew --export=__cxa_free_exception --export=__wasm_call_ctors --export=__get_exception_message --export=free --export-table -z stack-size=65536 --initial-memory=16777216 --max-memory=16777216 --no-entry --stack-first --table-base=1' failed (returned 1)
          

warning: `emsdk-demo` (bin "emsdk-demo") generated 1 warning
error: could not compile `emsdk-demo` (bin "emsdk-demo") due to previous error; 1 warning emitted```

</p>
</details>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with the Rust example and cargo run --target=wasm32-unknown-emscripten, then inspect the Emscripten target-linking path involved in the emcc and wasm-ld invocation. Done means the example links without undefined setjmp and longjmp symbols and produces the expected output.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, wasm
Domain
build-system, compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.