Skip to content

Emscripten miscompile with -fwasm-exception, -O0, and tls access in handler #27448

Description

@paradust7

Version of emscripten/emsdk:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 6.0.4 (fe5be6a)
clang version 24.0.0git (https:/github.com/llvm/llvm-project c050c487e9edb97ef44f53cb29fe1d8bcddb8f76)
Target: wasm32-unknown-emscripten
Thread model: posix

Reproducer

// em++ -O0 -pthread -fwasm-exceptions repro.cc -o repro.js
// node repro.js

#include <cstdio>

struct Wrapped {
  int x;
  Wrapped();
};

Wrapped::Wrapped() : x(42) {}

thread_local Wrapped tlsobj;

struct MyErr { int code; };

__attribute__((noinline)) void do_throw() {
  static volatile int always = 1;
  if (always)
    throw MyErr{7};
}

void do_catch() {
  try {
    do_throw();
  } catch (MyErr &e) {
    tlsobj.x += e.code;
    std::printf("caught, tls now %d\n", tlsobj.x);
  }
}

int main() {
  do_catch();
  std::printf("survived: %d\n", tlsobj.x);
  return 0;
}

Output

$ em++ -O0 -pthread -fwasm-exceptions repro.cc -o repro.js
$ node repro.js
wasm://wasm/0008214e:1


RuntimeError: unreachable
    at wasm://wasm/0008214e:wasm-function[28]:0xd8f
    at wasm://wasm/0008214e:wasm-function[30]:0xde1
    at wasm://wasm/0008214e:wasm-function[32]:0xe63
    at /home/ltuser/ehproblem/repro.js:876:12
    at callMain (/home/ltuser/ehproblem/repro.js:2711:15)
    at run (/home/ltuser/ehproblem/repro.js:2764:22)
    at /home/ltuser/ehproblem/repro.js:2806:25

Node.js v24.18.0

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