Skip to content

wasm32-emscripten: exceptions = "wasm" does not reach C units (setjmp links against emscripten_longjmp) #1029

Backlog

Description

@speak-agent

Found while moving openmui's Web programs to native Wasm exceptions (mcpp 2026.11.26.1, emsdk 6.0.9, Linux x64 host). A follow-up to #851's item 3: exceptions = "wasm" now exists, but it reaches only the C++ units.

What happens

With [target.'cfg(os = "emscripten")'.abi] exceptions = "wasm", a C++ unit is compiled with -fwasm-exceptions and the link carries -fwasm-exceptions (so Emscripten links the Wasm setjmp/longjmp model, SUPPORT_LONGJMP=wasm), but a C unit gets neither -fwasm-exceptions nor -sSUPPORT_LONGJMP=wasm. A C unit that calls setjmp is then compiled for the JavaScript model and the link fails:

wasm-ld: error: obj/jump.o: undefined symbol: emscripten_longjmp

The unit lines mcpp build --target wasm32-emscripten -v prints:

emcc -std=c11 -O0 -ffunction-sections -fdata-sections -c src/jump.c
em++ -std=c++23 -fwasm-exceptions … -O0 -ffunction-sections -fdata-sections … src/main.cpp
(link) … -fwasm-exceptions …

Reproduce (three files)

mcpp.toml:

[package]
name = "abi_repro"
version = "0.1.0"

[targets.abi-repro]
kind = "bin"
main = "src/main.cpp"

[build]
sources = ["src/*.c"]

[target.'cfg(os = "emscripten")'.abi]
exceptions = "wasm"

src/jump.c:

#include <setjmp.h>

static jmp_buf where;

static void fail(int code) { longjmp(where, code); }

int jumped(int code) {
    if (setjmp(where)) return 1;
    fail(code);
    return 0;
}

src/main.cpp:

#include <cstdio>
#include <stdexcept>

extern "C" int jumped(int code);

int main() {
    try {
        if (jumped(7) != 1) throw std::runtime_error("longjmp did not come back");
    } catch (const std::exception& error) {
        std::puts(error.what());
        return 1;
    }
    std::puts("ok");
    return 0;
}

mcpp build --target wasm32-emscripten fails as above. Adding cflags = ["-sSUPPORT_LONGJMP=wasm"] to [build] makes it link, and node target/wasm32-emscripten/*/bin/abi-repro.js prints ok — so the flag the C units miss is the whole difference.

Why it matters

Every C library that uses setjmp (libpng's error handling, libjpeg's, Lua's) is a C unit, mostly in index packages, where a consumer cannot add cflags to the package's own units. In our case libpng blocks the switch: a Web program of ours measured 4.3% smaller in transfer (gzip 2,086,161 → 1,995,519 B for the Wasm, launcher 54,163 → 51,793 B) with Wasm exceptions, and without the 108 invoke_* JavaScript trampolines.

What would fix it

exceptions = "wasm" applied to every unit of the graph for that target, C units of index packages included: -sSUPPORT_LONGJMP=wasm for C (or -fwasm-exceptions, which Emscripten maps to the same longjmp model), as the C++ units get -fwasm-exceptions — the ABI is the program's, not a unit's.

Related, not reduced to a reproduction here: a profile's or a target's cflags did not reach an index package's C units either (we tried [workspace.target.'cfg(os = "emscripten")'.build] cflags as a workaround for libpng), so there is currently no way to finish the switch from the consumer's side.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions