diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp index 0461b1cc36e25..1a95913fe74b7 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp @@ -1251,7 +1251,7 @@ uptr GetPageSize() { # endif // !SANITIZER_ANDROID # if SANITIZER_EMSCRIPTEN -extern "C" uptr emscripten_get_module_name(char *buf, uptr buf_len); +extern "C" void _emscripten_get_progname(char *buf, int buf_len); # endif uptr ReadBinaryName(/*out*/ char *buf, uptr buf_len) { @@ -1260,7 +1260,8 @@ uptr ReadBinaryName(/*out*/ char *buf, uptr buf_len) { CHECK_NE(default_module_name, NULL); return internal_snprintf(buf, buf_len, "%s", default_module_name); # elif SANITIZER_EMSCRIPTEN - return emscripten_get_module_name(buf, buf_len); + _emscripten_get_progname(buf, buf_len); + return internal_strlen(buf); # else # if SANITIZER_FREEBSD || SANITIZER_NETBSD # if SANITIZER_FREEBSD diff --git a/libcxxabi/include/cxxabi.h b/libcxxabi/include/cxxabi.h index e88f6d800726f..0e3969084e04f 100644 --- a/libcxxabi/include/cxxabi.h +++ b/libcxxabi/include/cxxabi.h @@ -58,7 +58,7 @@ __cxa_init_primary_exception(void* object, std::type_info* tinfo, void(_LIBCXXAB // 2.4.3 Throwing the Exception Object extern _LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN void __cxa_throw(void *thrown_exception, std::type_info *tinfo, -#ifdef __USING_WASM_EXCEPTIONS__ +#ifdef __wasm__ void *(_LIBCXXABI_DTOR_FUNC *dest)(void *)); #else void (_LIBCXXABI_DTOR_FUNC *dest)(void *)); diff --git a/libcxxabi/src/cxa_exception.cpp b/libcxxabi/src/cxa_exception.cpp index 47ca6a52ae144..e06756184aa79 100644 --- a/libcxxabi/src/cxa_exception.cpp +++ b/libcxxabi/src/cxa_exception.cpp @@ -207,7 +207,7 @@ void __cxa_free_exception(void *thrown_object) throw() { } __cxa_exception* __cxa_init_primary_exception(void* object, std::type_info* tinfo, -#ifdef __USING_WASM_EXCEPTIONS__ +#ifdef __wasm__ // In Wasm, a destructor returns its argument void *(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw() { #else @@ -272,14 +272,14 @@ will call terminate, assuming that there was no handler for the exception. */ -#if defined(__EMSCRIPTEN__) && defined(__USING_WASM_EXCEPTIONS__) && !defined(NDEBUG) +#if defined(__EMSCRIPTEN__) && defined(__WASM_EXCEPTIONS__) && !defined(NDEBUG) extern "C" { void __throw_exception_with_stack_trace(_Unwind_Exception*); } // extern "C" #endif void -#ifdef __USING_WASM_EXCEPTIONS__ +#ifdef __wasm__ // In Wasm, a destructor returns its argument __cxa_throw(void *thrown_object, std::type_info *tinfo, void *(_LIBCXXABI_DTOR_FUNC *dest)(void *)) { #else @@ -298,7 +298,7 @@ __cxa_throw(void *thrown_object, std::type_info *tinfo, void (_LIBCXXABI_DTOR_FU #ifdef __USING_SJLJ_EXCEPTIONS__ _Unwind_SjLj_RaiseException(&exception_header->unwindHeader); -#elif defined(__EMSCRIPTEN__) && defined(__USING_WASM_EXCEPTIONS__) && !defined(NDEBUG) +#elif defined(__EMSCRIPTEN__) && defined(__WASM_EXCEPTIONS__) && !defined(NDEBUG) // In debug mode, call a JS library function to use WebAssembly.Exception JS // API, which enables us to include stack traces __throw_exception_with_stack_trace(&exception_header->unwindHeader); @@ -651,7 +651,7 @@ void __cxa_rethrow() { } #ifdef __USING_SJLJ_EXCEPTIONS__ _Unwind_SjLj_RaiseException(&exception_header->unwindHeader); -#elif defined(__EMSCRIPTEN__) && defined(__USING_WASM_EXCEPTIONS__) && !defined(NDEBUG) +#elif defined(__EMSCRIPTEN__) && defined(__WASM_EXCEPTIONS__) && !defined(NDEBUG) // In debug mode, call a JS library function to use WebAssembly.Exception JS // API, which enables us to include stack traces __throw_exception_with_stack_trace(&exception_header->unwindHeader); @@ -780,7 +780,7 @@ __cxa_rethrow_primary_exception(void* thrown_object) dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup; #ifdef __USING_SJLJ_EXCEPTIONS__ _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader); -#elif defined(__EMSCRIPTEN__) && defined(__USING_WASM_EXCEPTIONS__) && !defined(NDEBUG) +#elif defined(__EMSCRIPTEN__) && defined(__WASM_EXCEPTIONS__) && !defined(NDEBUG) // In debug mode, call a JS library function to use // WebAssembly.Exception JS API, which enables us to include stack // traces diff --git a/libcxxabi/src/cxa_exception.h b/libcxxabi/src/cxa_exception.h index 098e0bf95aef8..527fc57086bac 100644 --- a/libcxxabi/src/cxa_exception.h +++ b/libcxxabi/src/cxa_exception.h @@ -19,7 +19,7 @@ namespace __cxxabiv1 { -#ifdef __USING_EMSCRIPTEN_EXCEPTIONS__ +#ifdef __EMSCRIPTEN_EXCEPTIONS__ struct _LIBCXXABI_HIDDEN __cxa_exception { size_t referenceCount; @@ -63,7 +63,7 @@ struct _LIBCXXABI_HIDDEN __cxa_exception { // Manage the exception object itself. std::type_info *exceptionType; -#ifdef __USING_WASM_EXCEPTIONS__ +#ifdef __wasm__ // In Wasm, a destructor returns its argument void *(_LIBCXXABI_DTOR_FUNC *exceptionDestructor)(void *); #else @@ -184,7 +184,7 @@ extern "C" _LIBCXXABI_FUNC_VIS __cxa_eh_globals * __cxa_get_globals_fast (); extern "C" _LIBCXXABI_FUNC_VIS void * __cxa_allocate_dependent_exception (); extern "C" _LIBCXXABI_FUNC_VIS void __cxa_free_dependent_exception (void * dependent_exception); -#endif // !__USING_EMSCRIPTEN_EXCEPTIONS__ +#endif // !__EMSCRIPTEN_EXCEPTIONS__ } // namespace __cxxabiv1 diff --git a/libcxxabi/src/cxa_handlers.cpp b/libcxxabi/src/cxa_handlers.cpp index 07913fba397e6..92b203c2bfdaf 100644 --- a/libcxxabi/src/cxa_handlers.cpp +++ b/libcxxabi/src/cxa_handlers.cpp @@ -73,7 +73,7 @@ __attribute__((noreturn)) void terminate() noexcept { -#if !defined(_LIBCXXABI_NO_EXCEPTIONS) && !defined(__USING_EMSCRIPTEN_EXCEPTIONS__) +#if !defined(_LIBCXXABI_NO_EXCEPTIONS) && !defined(__EMSCRIPTEN_EXCEPTIONS__) // If there might be an uncaught exception using namespace __cxxabiv1; __cxa_eh_globals* globals = __cxa_get_globals_fast(); diff --git a/libcxxabi/src/cxa_noexception.cpp b/libcxxabi/src/cxa_noexception.cpp index 47c0db67c0fe9..182253446393a 100644 --- a/libcxxabi/src/cxa_noexception.cpp +++ b/libcxxabi/src/cxa_noexception.cpp @@ -74,6 +74,20 @@ void __cxa_free_exception(void *thrown_object) throw() { ((char *)cxa_exception_from_thrown_object(thrown_object)); free((void *)raw_buffer); } + +// This function is called from make_exception_ptr in libcxx unless +// -fno-exceptions is not given. We have definitions of this function in +// cxa_exception.cpp and cxa_exception_emscripten.cpp, but unlike other +// platforms, we use those files only when one of Emscripten EH or Wasm EH is +// used, and we use this cxa_noexceptions.cpp in case of -fignore-exceptions, +// which is our default. So we add a definition here to prevent a link failure. +__cxa_exception* +__cxa_init_primary_exception(void* object, + std::type_info* tinfo, + void*(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw() { + __cxa_exception* exception_header = cxa_exception_from_thrown_object(object); + return exception_header; +} #endif } // extern "C" diff --git a/libcxxabi/src/cxa_personality.cpp b/libcxxabi/src/cxa_personality.cpp index bac51058de00a..7773eed41e9bf 100644 --- a/libcxxabi/src/cxa_personality.cpp +++ b/libcxxabi/src/cxa_personality.cpp @@ -70,7 +70,7 @@ extern "C" EXCEPTION_DISPOSITION _GCC_specific_handler(PEXCEPTION_RECORD, +------------------+--+-----+-----+------------------------+--------------------------+ | callSiteTableLength | (ULEB128) | Call Site Table length, used to find Action table | +---------------------+-----------+---------------------------------------------------+ -#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__USING_WASM_EXCEPTIONS__) +#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__WASM_EXCEPTIONS__) +---------------------+-----------+------------------------------------------------+ | Beginning of Call Site Table The current ip lies within the | | ... (start, length) range of one of these | @@ -84,7 +84,7 @@ extern "C" EXCEPTION_DISPOSITION _GCC_specific_handler(PEXCEPTION_RECORD, | +-------------+---------------------------------+------------------------------+ | | ... | +----------------------------------------------------------------------------------+ -#else // __USING_SJLJ_EXCEPTIONS__ || __USING_WASM_EXCEPTIONS__ +#else // __USING_SJLJ_EXCEPTIONS__ || __WASM_EXCEPTIONS__ +---------------------+-----------+------------------------------------------------+ | Beginning of Call Site Table The current ip is a 1-based index into | | ... this table. Or it is -1 meaning no | @@ -97,7 +97,7 @@ extern "C" EXCEPTION_DISPOSITION _GCC_specific_handler(PEXCEPTION_RECORD, | +-------------+---------------------------------+------------------------------+ | | ... | +----------------------------------------------------------------------------------+ -#endif // __USING_SJLJ_EXCEPTIONS__ || __USING_WASM_EXCEPTIONS__ +#endif // __USING_SJLJ_EXCEPTIONS__ || __WASM_EXCEPTIONS__ +---------------------------------------------------------------------+ | Beginning of Action Table ttypeIndex == 0 : cleanup | | ... ttypeIndex > 0 : catch | @@ -547,7 +547,7 @@ void set_registers(_Unwind_Exception* unwind_exception, _Unwind_Context* context, const scan_results& results) { -#if defined(__USING_SJLJ_EXCEPTIONS__) || defined(__USING_WASM_EXCEPTIONS__) +#if defined(__USING_SJLJ_EXCEPTIONS__) || defined(__WASM_EXCEPTIONS__) #define __builtin_eh_return_data_regno(regno) regno #elif defined(__ibmxl__) // IBM xlclang++ compiler does not support __builtin_eh_return_data_regno. @@ -642,7 +642,7 @@ static void scan_eh_tab(scan_results &results, _Unwind_Action actions, // Get beginning current frame's code (as defined by the // emitted dwarf code) uintptr_t funcStart = _Unwind_GetRegionStart(context); -#if defined(__USING_SJLJ_EXCEPTIONS__) || defined(__USING_WASM_EXCEPTIONS__) +#if defined(__USING_SJLJ_EXCEPTIONS__) || defined(__WASM_EXCEPTIONS__) if (ip == uintptr_t(-1)) { // no action @@ -652,9 +652,9 @@ static void scan_eh_tab(scan_results &results, _Unwind_Action actions, else if (ip == 0) call_terminate(native_exception, unwind_exception); // ip is 1-based index into call site table -#else // !__USING_SJLJ_EXCEPTIONS__ && !__USING_WASM_EXCEPTIONS__ +#else // !__USING_SJLJ_EXCEPTIONS__ && !__WASM_EXCEPTIONS__ uintptr_t ipOffset = ip - funcStart; -#endif // !__USING_SJLJ_EXCEPTIONS__ && !__USING_WASM_EXCEPTIONS__ +#endif // !__USING_SJLJ_EXCEPTIONS__ && !__WASM_EXCEPTIONS__ const uint8_t* classInfo = NULL; // Note: See JITDwarfEmitter::EmitExceptionTable(...) for corresponding // dwarf emission @@ -676,7 +676,7 @@ static void scan_eh_tab(scan_results &results, _Unwind_Action actions, // Walk call-site table looking for range that // includes current PC. uint8_t callSiteEncoding = *lsda++; -#if defined(__USING_SJLJ_EXCEPTIONS__) || defined(__USING_WASM_EXCEPTIONS__) +#if defined(__USING_SJLJ_EXCEPTIONS__) || defined(__WASM_EXCEPTIONS__) (void)callSiteEncoding; // Unused when using SjLj/Wasm exceptions #endif uint32_t callSiteTableLength = static_cast(readULEB128(&lsda)); @@ -687,7 +687,7 @@ static void scan_eh_tab(scan_results &results, _Unwind_Action actions, while (callSitePtr < callSiteTableEnd) { // There is one entry per call site. -#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__USING_WASM_EXCEPTIONS__) +#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__WASM_EXCEPTIONS__) // The call sites are non-overlapping in [start, start+length) // The call sites are ordered in increasing value of start uintptr_t start = readEncodedPointer(&callSitePtr, callSiteEncoding); @@ -695,15 +695,15 @@ static void scan_eh_tab(scan_results &results, _Unwind_Action actions, uintptr_t landingPad = readEncodedPointer(&callSitePtr, callSiteEncoding); uintptr_t actionEntry = readULEB128(&callSitePtr); if ((start <= ipOffset) && (ipOffset < (start + length))) -#else // __USING_SJLJ_EXCEPTIONS__ || __USING_WASM_EXCEPTIONS__ +#else // __USING_SJLJ_EXCEPTIONS__ || __WASM_EXCEPTIONS__ // ip is 1-based index into this table uintptr_t landingPad = readULEB128(&callSitePtr); uintptr_t actionEntry = readULEB128(&callSitePtr); if (--ip == 0) -#endif // __USING_SJLJ_EXCEPTIONS__ || __USING_WASM_EXCEPTIONS__ +#endif // __USING_SJLJ_EXCEPTIONS__ || __WASM_EXCEPTIONS__ { // Found the call site containing ip. -#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__USING_WASM_EXCEPTIONS__) +#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__WASM_EXCEPTIONS__) if (landingPad == 0) { // No handler here @@ -711,9 +711,9 @@ static void scan_eh_tab(scan_results &results, _Unwind_Action actions, return; } landingPad = (uintptr_t)lpStart + landingPad; -#else // __USING_SJLJ_EXCEPTIONS__ || __USING_WASM_EXCEPTIONS__ +#else // __USING_SJLJ_EXCEPTIONS__ || __WASM_EXCEPTIONS__ ++landingPad; -#endif // __USING_SJLJ_EXCEPTIONS__ || __USING_WASM_EXCEPTIONS__ +#endif // __USING_SJLJ_EXCEPTIONS__ || __WASM_EXCEPTIONS__ results.landingPad = landingPad; if (actionEntry == 0) { @@ -841,7 +841,7 @@ static void scan_eh_tab(scan_results &results, _Unwind_Action actions, action += actionOffset; } // there is no break out of this loop, only return } -#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__USING_WASM_EXCEPTIONS__) +#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__WASM_EXCEPTIONS__) else if (ipOffset < start) { // There is no call site for this ip @@ -849,7 +849,7 @@ static void scan_eh_tab(scan_results &results, _Unwind_Action actions, // Possible stack corruption. call_terminate(native_exception, unwind_exception); } -#endif // !__USING_SJLJ_EXCEPTIONS__ && !__USING_WASM_EXCEPTIONS__ +#endif // !__USING_SJLJ_EXCEPTIONS__ && !__WASM_EXCEPTIONS__ } // there might be some tricky cases which break out of this loop // It is possible that no eh table entry specify how to handle @@ -906,7 +906,7 @@ _UA_CLEANUP_PHASE */ #if !defined(_LIBCXXABI_ARM_EHABI) -#ifdef __USING_WASM_EXCEPTIONS__ +#ifdef __WASM_EXCEPTIONS__ _Unwind_Reason_Code __gxx_personality_wasm0 #elif defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__) static _Unwind_Reason_Code __gxx_personality_imp @@ -975,7 +975,7 @@ __gxx_personality_v0 exc->languageSpecificData = results.languageSpecificData; exc->catchTemp = reinterpret_cast(results.landingPad); exc->adjustedPtr = results.adjustedPtr; -#ifdef __USING_WASM_EXCEPTIONS__ +#ifdef __WASM_EXCEPTIONS__ // Wasm only uses a single phase (_UA_SEARCH_PHASE), so save the // results here. set_registers(unwind_exception, context, results); diff --git a/libcxxabi/src/private_typeinfo.cpp b/libcxxabi/src/private_typeinfo.cpp index 88fa7b5d65ef0..8b0c8ffd41bfb 100644 --- a/libcxxabi/src/private_typeinfo.cpp +++ b/libcxxabi/src/private_typeinfo.cpp @@ -1530,10 +1530,16 @@ __base_class_type_info::search_below_dst(__dynamic_cast_info* info, not_public_path, use_strcmp); } +} // __cxxabiv1 + // XXX EMSCRIPTEN +#ifndef __WASM_EXCEPTIONS__ + +#include "cxa_exception.h" -#ifndef __USING_WASM_EXCEPTIONS__ +namespace __cxxabiv1 +{ // These functions are used by the emscripten-style exception handling // mechanism. @@ -1555,11 +1561,33 @@ int __cxa_can_catch(__shim_type_info* catchType, __shim_type_info* excpType, voi return ret; } -int __cxa_is_pointer_type(__shim_type_info* type) { - return !!dynamic_cast<__pointer_type_info*>(type); +static +inline +__cxa_exception* +cxa_exception_from_thrown_object(void* thrown_object) +{ + return static_cast<__cxa_exception*>(thrown_object) - 1; +} + +void *__cxa_get_exception_ptr(void *thrown_object) throw() { + // Get pointer which is expected to be received by catch clause in C++ code. + // It may be adjusted when the pointer is casted to some of the exception + // object base classes (e.g. when virtual inheritance is used). When a pointer + // is thrown this method should return the thrown pointer itself. + // Work around a fastcomp bug, this code is still included for some reason in + // a build without exceptions support. + __cxa_exception* ex = cxa_exception_from_thrown_object(thrown_object); + bool is_pointer = !!dynamic_cast<__pointer_type_info*>(ex->exceptionType); + if (is_pointer) + return *(void**)thrown_object; + if (ex->adjustedPtr) + return ex->adjustedPtr; + return ex; } } -#endif // __USING_EMSCRIPTEN_EXCEPTIONS__ } // __cxxabiv1 + +#endif // !__WASM_EXCEPTIONS__ + diff --git a/libunwind/src/Unwind-wasm.c b/libunwind/src/Unwind-wasm.c index f7f39d38b59c1..b18b32c5d1784 100644 --- a/libunwind/src/Unwind-wasm.c +++ b/libunwind/src/Unwind-wasm.c @@ -14,7 +14,7 @@ #include "config.h" -#ifdef __USING_WASM_EXCEPTIONS__ +#ifdef __WASM_EXCEPTIONS__ #include "unwind.h" #include @@ -120,4 +120,4 @@ _Unwind_GetRegionStart(struct _Unwind_Context *context) { return 0; } -#endif // defined(__USING_WASM_EXCEPTIONS__) +#endif // defined(__WASM_EXCEPTIONS__) diff --git a/libunwind/src/libunwind.cpp b/libunwind/src/libunwind.cpp index 217dde9098637..561c68eb33e28 100644 --- a/libunwind/src/libunwind.cpp +++ b/libunwind/src/libunwind.cpp @@ -26,7 +26,7 @@ #include #endif -#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__USING_WASM_EXCEPTIONS__) +#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__WASM_EXCEPTIONS__) #include "AddressSpace.hpp" #include "UnwindCursor.hpp" @@ -348,7 +348,7 @@ void __unw_remove_dynamic_eh_frame_section(unw_word_t eh_frame_start) { #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) #endif // !defined(__USING_SJLJ_EXCEPTIONS__) && - // !defined(__USING_WASM_EXCEPTIONS__) + // !defined(__WASM_EXCEPTIONS__) #ifdef __APPLE__