Skip to content

Commit b0ec4e1

Browse files
authored
[EH] Add __cxa_init_primary_exception to cxa_noexception.cpp (#22027)
tl;dr: This adds `__cxa_init_primary_exception` to `cxa_noexception.cpp`. Other targets build `cxa_exception.cpp` in `-fignore-exceptions` mode and build `cxa_noexception.cpp` only in `-fno-exeptions` mode. But we build `cxa_noexception.cpp` in `-fignore-exceptions` mode, which means it needs the definition for `__cxa_init_primary_exception` for linking to succeed when no EH argument is given (which means `-fignore-exceptions`). --- Long version (Feel free to skip): Background: After #21638, `__cxa_init_primary_exception` was added in libcxxabi: https://github.com/emscripten-core/emscripten/blob/28c10a1c5e2862edadd68ab627478204ae96d134/system/lib/libcxxabi/src/cxa_exception.cpp#L209-L226 https://github.com/emscripten-core/emscripten/blob/28c10a1c5e2862edadd68ab627478204ae96d134/system/lib/libcxxabi/src/cxa_exception_emscripten.cpp#L155-L162 Currently the files containing `__cxa_init_primary_exception`, `cxa_exception.cpp` and `cxa_exception_emscripten.cpp`, are only compiled when any of the EH mode is specified. `cxa_exception.cpp` is compiled when Wasm EH is selected, and `cxa_exception_emscripten.cpp` is compiled when Emscripten EH is selected: https://github.com/emscripten-core/emscripten/blob/28c10a1c5e2862edadd68ab627478204ae96d134/tools/system_libs.py#L1599-L1608 and this function is called from `make_exception_ptr` in libcxx: https://github.com/emscripten-core/emscripten/blob/28c10a1c5e2862edadd68ab627478204ae96d134/system/lib/libcxx/include/__exception/exception_ptr.h#L87-L99 And `make_exception_ptr` is called from `std::promise`'s destructor: https://github.com/emscripten-core/emscripten/blob/28c10a1c5e2862edadd68ab627478204ae96d134/system/lib/libcxx/include/future#L1161-L1168 --- Bug: Currently any program that calls `std::promise`'s destructor without specifying any exception-related arguments fails, saying `undefined symbol: __cxa_init_primary_exception`. Not specifying any exception arguments, meaning not specifying any among `-fno-exceptions`, `-fwasm-exceptions`, `-fexceptions`, or `-sDISABLE_EXCEPTION_CATCHING=0`, defaults to `-fignore-exceptions`, which allows throwing but not catching. --- Analysis: The callsite of `__cxa_init_primary_exception` in `make_exception_ptr` is guarded with `#ifndef _LIBCPP_HAS_NO_EXCEPTIONS`, so it seems it is supposed to be included only when exceptions are enabled. This `_LIBCPP_HAS_NO_EXCEPTIONS` is defined when `__cpp_exceptions` is defined: https://github.com/emscripten-core/emscripten/blob/28c10a1c5e2862edadd68ab627478204ae96d134/system/lib/libcxx/include/__config#L644-L646 And that `__cpp_exceptions` is defined in clang, when `-fcxx-exceptions` is given: https://github.com/llvm/llvm-project/blob/be566d2eacdaed972b90d2eeb1e66d732c9fe7c1/clang/lib/Frontend/InitPreprocessor.cpp#L638-L639 And that `-fcxx-exceptions` can be specified in command line, but it is also programmatically added here: https://github.com/llvm/llvm-project/blob/be566d2eacdaed972b90d2eeb1e66d732c9fe7c1/clang/lib/Driver/ToolChains/Clang.cpp#L371-L388 You can see it is added by default unless the arch is XCore or PS4/PS5, which means for Wasm it has been always added so far, unless `-fno-exceptions` is explicitly specified. So I tried checking the arguments there in Clang and adding `-fcxx-exceptions` only if either of `-fwasm-exceptions` or `-enable-emscripten-cxx-exceptions` is given. But this fails when none of EH is selected (which means `-fignore-exceptions`), because if `-fcxx-exceptions` is not added, we can't use keywords like `throw`. So basically the problem is, other targets build `cxa_exception.cpp` in `-fignore-exceptions` mode and build `cxa_noexception.cpp` only in `-fno-exeptions` mode. But we build `cxa_noexception.cpp` in `-fignore-exceptions` mode, which means it needs the definition for `__cxa_init_primary_exception` for linking to succeed, because `_LIBCPP_HAS_NO_EXCEPTIONS` cannot be defined in `-fignore-exceptions` (because we couldn't disable `-fcxx-exceptions` above in Clang to use `throw`) So this adds `__cxa_init_primary_exception` to `cxa_noexception.cpp`.
1 parent 28c10a1 commit b0ec4e1

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

system/lib/libcxxabi/src/cxa_noexception.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ void __cxa_free_exception(void *thrown_object) throw() {
7474
((char *)cxa_exception_from_thrown_object(thrown_object));
7575
free((void *)raw_buffer);
7676
}
77+
78+
__cxa_exception*
79+
__cxa_init_primary_exception(void* object,
80+
std::type_info* tinfo,
81+
void*(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw() {
82+
__cxa_exception* exception_header = cxa_exception_from_thrown_object(object);
83+
return exception_header;
84+
}
7785
#endif
7886

7987
} // extern "C"

test/test_other.py

+20
Original file line numberDiff line numberDiff line change
@@ -14891,3 +14891,23 @@ def test_mimalloc_headers(self):
1489114891
def test_SUPPORT_BIG_ENDIAN(self):
1489214892
# Just a simple build-only test for now
1489314893
self.run_process([EMCC, '-sSUPPORT_BIG_ENDIAN', test_file('hello_world.c')])
14894+
14895+
@parameterized({
14896+
'noexcept': ['-fno-exceptions'],
14897+
'default': [],
14898+
'except': ['-sDISABLE_EXCEPTION_CATCHING=0'],
14899+
'except_wasm': ['-fwasm-exceptions'],
14900+
'except_wasm_exnref': ['-fwasm-exceptions', '-sWASM_EXNREF']
14901+
})
14902+
def test_std_promise_link(self, *args):
14903+
# Regression test for a bug where std::promise's destructor caused a link
14904+
# error with __cxa_init_primary_exception when no exception argument was
14905+
# given (which defaults to -fignore-exceptions)
14906+
create_file('src.cpp', r'''
14907+
#include <future>
14908+
int main() {
14909+
std::promise<int> p;
14910+
return 0;
14911+
}
14912+
''')
14913+
self.run_process([EMXX, 'src.cpp', '-pthread'] + list(args))

0 commit comments

Comments
 (0)