emscripten-core / emscripten-core/emscripten
EH termination does not call __cxa_begin_catch
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
When an exception thrown ends up calling `std::terminate`, for example, because an exception is thrown within a `noexcept` function or an exception is thrown from `__cxa_end_catch` during handling the previous exception, the libc++abi spec says we are supposed to call `__cxa_begin_catch` before `std::terminate`: https://libcxxabi.llvm.org/spec.html
> When the personality routine encounters a termination condition, it will call `__cxa_begin_catch()` to mark the exception as handled and then call `terminate()`, which shall not return to its caller.
But currently Emscripten does NOT call `__cxa_begin_catch` before `terminate`. For example:
```cpp
#include
#include
int main() noexcept {
std::set_terminate([] {
auto ptr = std::current_exception();
if (ptr)
std::cerr << "exception_ptr is NOT null" << std::endl;
else
std::cerr << "exception_ptr is null" << std::endl;
std::abort();
});
throw 3;
}
```
When you call `std::current_exception()` within the custom terminate handler, it should print
```console
exception_ptr is NOT null
```
But Wasm EH prints
```console
exception_ptr is null
```
The reason is that we replaced the call to `__clang_call_terminate`, a generated function that calls `__cxa_begin_catch` and `std::terminate`, with just a call to `std::terminate` in https://github.com/llvm/llvm-project/commit/561abd83ffecc8d4ba8fcbbbcadb31efc55985c2 because this caused some tricky transformation problems for Wasm EH.
(Emscripten EH (accidentally) had the same problem after https://github.com/llvm/llvm-project/commit/561abd83ffecc8d4ba8fcbbbcadb31efc55985c2 but it should be fixed by https://github.com/llvm/llvm-project/pull/129020.)
This should be fixable with sufficient efforts but has not been a priority. This issues documents the problem so it is not forgotten and can possibly be fixed later.
This problem was first reported in #23720.
Contributor guide
Assessment
This issue has not been assessed yet.