emscripten-core / emscripten-core/emscripten

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

Offen
#22,285 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
C++
Sterne
27.6k
Forks
3.6k
Ø Merge
1 T. 14 Std.
Gemergte PRs (30 T.)
125

Beschreibung

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`.

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

return 0;
}
```
#### compilation
```shell
$ 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:
```shell
# 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:
```shell
# 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`:

```c++
#include
#include

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

std::function 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`).

```shell
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:
```shell
# result if we comment "auto func = test_func;"
begin
before throw
in call throw
error caught
after catching
```

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Reproduziere den Fehler aus test.cpp mit emcc unter Verwendung von -sMAIN_MODULE, -sEMULATE_FUNCTION_POINTER_CASTS und -sNO_DISABLE_EXCEPTION_CATCHING. Untersuche anschließend den Stack im generierten main.js rund um dynCall_viii und dynCall_v. Vergleiche beide Beispiele, einschließlich des std::function-Falls, und überprüfe, dass Ausnahmen abgefangen werden, ohne dass ein RuntimeError wegen einer null-Funktion oder einer nicht übereinstimmenden Funktionssignatur auftritt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
cpp, wasm
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.