emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

Exception can't be caught with `-sMAIN_MODULE` and `-sEMULATE_FUNCTION_POINTER_CASTS` and give Runtime error

Open hly2019 opened this issue 1 year ago • 2 comments

Please include the following in your bug report:

Version of emscripten/emsdk: 3.1.54

Failing command line in full: Hi, I was trying to use -sMAIN_MODULE for dynamic linking, and I found it seems to fail to catch the thrown exceptions when I also add both -sEMULATE_FUNCTION_POINTER_CASTS and -sMAIN_MODULE which will give RuntimeError: null function or function signature mismatch.

As the following code example shows, I'd expect to catch the thrown exception and print information, but it can't do so when I compile with -sMAIN_MODULE and -sEMULATE_FUNCTION_POINTER_CASTS and will give the Runtime error.

I appreciate it so much if you could take a look at it. Thank you very much!

Code example

Simply throw and catch. But if compiled with -sMAIN_MODULE and -sEMULATE_FUNCTION_POINTER_CASTS, it gives null function or function signature mismatch.

#include <iostream>
int main() {
    try {
        throw 1;
    }
    catch(...) {
        printf("error caught!\n");
    }

    return 0;
}

compilation

$ emcc test.cpp -sMAIN_MODULE -o main.js -sNO_DISABLE_EXCEPTION_CATCHING -g -sEMULATE_FUNCTION_POINTER_CASTS

result

with -sEMULATE_FUNCTION_POINTER_CASTS and -sMAIN_MODULE, we got:

# result with -sEMULATE_FUNCTION_POINTER_CASTS and -sMAIN_MODULE
/home/ubuntu/main.js:128
      throw ex;
      ^

RuntimeError: null function or function signature mismatch
    at main.wasm.dynCall_viii (wasm://wasm/main.wasm-027112ea:wasm-function[8350]:0x174020)
    at Object.dynCall_viii (/home/ubuntu/main.js:714:12)
    at invoke_viii (/home/ubuntu/main.js:38134:27)
    at main.wasm.__original_main (wasm://wasm/main.wasm-027112ea:wasm-function[151]:0x9c8fd)
    at main.wasm.main (wasm://wasm/main.wasm-027112ea:wasm-function[152]:0x9ca49)
    at callMain (/home/ubuntu/main.js:38885:15)
    at doRun (/home/ubuntu/main.js:38935:23)
    at run (/home/ubuntu/main.js:38950:5)
    at runCaller (/home/ubuntu/main.js:38858:19)
    at removeRunDependency (/home/ubuntu/main.js:652:7)

but of course, we expect to catch the exception and print:

# expected result:
error caught!

Another example with different error messages

Here's another case that seems to give different error messages when I use std::function and compile with -sEMULATE_FUNCTION_POINTER_CASTS and -sMAIN_MODULE:

#include <iostream>
#include <functional>

void call_throw() {
    printf("in call throw\n");
    throw int();
}

std::function<void()> test_func = []() {};
int main () {
    printf("begin\n");
    auto func = test_func; // no error if comment this line
    try {
        printf("before throw\n");
        call_throw();
    } catch (...) {
        printf("error caught\n");
    }
    printf("after catching\n");
}

If I compile with emcc test_func.cpp -o main.js -sEMULATE_FUNCTION_POINTER_CASTS -sNO_DISABLE_EXCEPTION_CATCHING -sMAIN_MODULE -g, then I get the following error, triggered by a different function (dynCall_v) than the first example (dynCall_viii).

begin
before throw
in call throw
error caught
/data/main.js:128
      throw ex;
      ^

RuntimeError: null function or function signature mismatch
    at main.wasm.dynCall_v (wasm://wasm/main.wasm-0275e636:wasm-function[8438]:0x176ec5)
    at Object.dynCall_v (/data/main.js:714:12)
    at invoke_v (/data/main.js:38141:24)
    at main.wasm.__original_main (wasm://wasm/main.wasm-0275e636:wasm-function[158]:0x9cef9)
    at main.wasm.main (wasm://wasm/main.wasm-0275e636:wasm-function[263]:0x9fbb3)
    at callMain (/data/main.js:38881:15)
    at doRun (/data/main.js:38931:23)
    at run (/data/main.js:38946:5)
    at runCaller (/data/main.js:38854:19)
    at removeRunDependency (/data/main.js:652:7)

If we comment that line auto func = test_func;, then there won't be such an issue:

# result if we comment "auto func = test_func;"
begin
before throw
in call throw
error caught
after catching

hly2019 avatar Jul 26 '24 02:07 hly2019

Taking a look now. Its possible this simply never worked.

Can I ask why you need/want EMULATE_FUNCTION_POINTER_CASTS? That setting doesn't get a lot of usage and has limited testing so it likely things like dynamic linking simply never worked.

sbc100 avatar Aug 08 '24 22:08 sbc100

EMULATE_FUNCTION_POINTER_CASTS has now been scheduled for removal. See #23953. If you are using these feature perhaps we can help you move away from it or find alternatives?

sbc100 avatar Apr 17 '25 21:04 sbc100