Skip to content

Emscripten calls null GOT entry when side module loaded with allowUndefined: true #22052

@hoodmane

Description

@hoodmane

The following code example looks up GOT.__cxa_throw which is 0 and then calls it which throws TypeError: getWasmTableEntry(...) is not a function. Calling reportUndefinedSymbols or removing allowUndefined: true resolves the crash.

module.cpp

#include <stdexcept>

extern "C"
int cpp_func() { 
    try { 
        throw std::runtime_error(""); // crashes trying to throw this
    } catch (const std::exception &e) {
        return 0;
    }
    return 1;
} 

main.c

#include <dlfcn.h>
#include "stdio.h"
#include "emscripten.h"

typedef int (*InitFunc)(void);

int main(void) {
    EM_ASM({
        loadDynamicLibrary("module.so", { allowUndefined: true });
    });

    void* handle = dlopen("module.so", RTLD_NOW);
    printf("handle: %p\n", handle);
    if (!handle) {
        return 1;
    }
    InitFunc init_func = (InitFunc)dlsym(handle, "cpp_func");
    printf("init_func: %p\n", init_func);
    if (!init_func) {
        return 1;
    }
    int res = init_func(); // Crashes in init_func
    printf("res: %d\n", res);
    return 0;
}

build.sh

em++ -c module.cpp -fPIC -fexceptions -g2 -O2 
em++ module.o -o module.so \
    -sWASM_BIGINT -sSIDE_MODULE=1 -fexceptions -g2 -O2

emcc -c main.c -fPIC -fexceptions -g2 -O2 
emcc main.o -o main.js \
    -sWASM_BIGINT -sMAIN_MODULE=1 -fexceptions -g2 -O2

Then run with node main.js.

Version of emscripten/emsdk:
Checked on 3.1.52, 3.1.58 and tip of tree:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.62-git (0a0fa668c63cdaf14cfccb2ce62bc5afa46b4170)
clang version 19.0.0git (https:/github.com/llvm/llvm-project 0e62d5cf55479981da5e05e406bbca4afb3cdc4f)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/rchatham/Documents/programming/emsdk/upstream/bin
Build config: +assertions

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions