Skip to content

Commit 6a0909e

Browse files
committed
Fix destructor return type in __cxa_init_primary_exception
llvm/llvm-project#65534 introduces `__cxa_init_primary_exception` and uses this in libcxx. Like several other methods like the below in `cxa_exception.cpp`, https://github.com/emscripten-core/emscripten/blob/fbdd9249e939660cb0d20f00d6bc1897c2f3905e/system/lib/libcxxabi/src/cxa_exception.cpp#L264-L269 this new function takes a pointer to a destructor, and in Wasm destructor returns a `this` pointer, which requires `ifdef __USING_WASM_EXCEPTION__` on its signature. And that it is also used in libcxx means we need to guard some code in libcxx with `ifdef __USING_WASM_EXCEPTION__` for the signature difference, and we need to build libcxx with `-D__USING_WASM_EXCEPTIONS__` when Wasm EH is used.
1 parent 31ac683 commit 6a0909e

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

system/lib/libcxx/include/__exception/exception_ptr.h

+12-3
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ struct __cxa_exception;
3636
_LIBCPP_OVERRIDABLE_FUNC_VIS __cxa_exception* __cxa_init_primary_exception(
3737
void*,
3838
std::type_info*,
39-
void(
4039
# if defined(_WIN32)
41-
__thiscall
40+
void(__thiscall*)(void*)) throw();
41+
# elif defined(__USING_WASM_EXCEPTIONS__)
42+
void* (*)(void*)) throw();
43+
# else
44+
void (*)(void*)) throw();
4245
# endif
43-
*)(void*)) throw();
4446
}
4547

4648
} // namespace __cxxabiv1
@@ -88,8 +90,15 @@ _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
8890
using _Ep2 = __decay_t<_Ep>;
8991

9092
void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));
93+
# ifdef __USING_WASM_EXCEPTIONS__
94+
(void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) -> void* {
95+
# else
9196
(void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) {
97+
# endif
9298
std::__destroy_at(static_cast<_Ep2*>(__p));
99+
# ifdef __USING_WASM_EXCEPTIONS__
100+
return __p;
101+
# endif
93102
});
94103

95104
try {

system/lib/libcxxabi/include/cxxabi.h

+4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ extern _LIBCXXABI_FUNC_VIS void
4848
__cxa_free_exception(void *thrown_exception) throw();
4949
// This function is an LLVM extension, which mirrors the same extension in libsupc++ and libcxxrt
5050
extern _LIBCXXABI_FUNC_VIS __cxa_exception*
51+
#ifdef __USING_WASM_EXCEPTIONS__
52+
__cxa_init_primary_exception(void* object, std::type_info* tinfo, void*(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw();
53+
#else
5154
__cxa_init_primary_exception(void* object, std::type_info* tinfo, void(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw();
55+
#endif
5256

5357
// 2.4.3 Throwing the Exception Object
5458
extern _LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN void

system/lib/libcxxabi/src/cxa_exception.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,12 @@ void __cxa_free_exception(void *thrown_object) throw() {
207207
}
208208

209209
__cxa_exception* __cxa_init_primary_exception(void* object, std::type_info* tinfo,
210+
#ifdef __USING_WASM_EXCEPTIONS__
211+
// In Wasm, a destructor returns its argument
212+
void *(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw() {
213+
#else
210214
void(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw() {
215+
#endif
211216
__cxa_exception* exception_header = cxa_exception_from_thrown_object(object);
212217
exception_header->referenceCount = 0;
213218
exception_header->unexpectedHandler = std::get_unexpected();

tools/system_libs.py

+6
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,12 @@ class libcxx(NoExceptLibrary, MTLibrary):
16191619
'libdispatch.cpp',
16201620
]
16211621

1622+
def get_cflags(self):
1623+
cflags = super().get_cflags()
1624+
if self.eh_mode == Exceptions.WASM:
1625+
cflags.append('-D__USING_WASM_EXCEPTIONS__')
1626+
return cflags
1627+
16221628

16231629
class libunwind(NoExceptLibrary, MTLibrary):
16241630
name = 'libunwind'

0 commit comments

Comments
 (0)