Skip to content

EH termination does not call __cxa_begin_catch #23779

Open
@aheejin

Description

@aheejin

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:

#include <iostream>
#include <exception>

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

exception_ptr is NOT null

But Wasm EH prints

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 llvm/llvm-project@561abd8 because this caused some tricky transformation problems for Wasm EH.

(Emscripten EH (accidentally) had the same problem after llvm/llvm-project@561abd8 but it should be fixed by llvm/llvm-project#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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions