emscripten-core / emscripten-core/emscripten
Terminate'ing doesn't prevent subsequent code execution
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
What's the expected behavior when the Emscripten module triggers an unhandled exception or `std::terminate()`/`std::abort()`? The docs contain only a vague description - one place is:
> When exception catching is disabled, a thrown exception terminates the application.
> https://emscripten.org/docs/optimizing/Optimizing-Code.html#c-exceptions
and another place is:
> // <...> When exceptions are
> // disabled, if an exception actually happens then it will not be caught
> // and the program will halt (so this will not introduce silent failures).
> https://github.com/emscripten-core/emscripten/blob/96b14d38f547092d9875b601aa60298a506ba550/src/settings.js#L560
I'd expect that words "terminate"/"halt" literally mean "no code is executed by the module anymore", however in reality the module, apparently, seems to still process incoming events from JS.
This can be repro'ed by this program:
```
#include
#include
struct A {
A() {
emscripten_run_script("console.error('Constructor called!');");
}
};
EMSCRIPTEN_BINDINGS(foo) {
emscripten::class_("A").constructor<>();
}
int main() {
std::terminate();
}
```
When loading this program as an Emscripten module, we can see the termination error in the console; however, if we execute the following JavaScript code we see that the "Constructor called!" message appears **after the crash**:
```
new Module.A()
```
This is confusing - then what do "terminate"/"halt" words mean in the doc? Also, isn't it a security issue that a code with an unhandled exception or other critical error can still process messages from JavaScript?
Contributor guide
Assessment
This issue has not been assessed yet.