//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // // // This file implements the "Exception Handling APIs" // https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html // http://www.intel.com/design/itanium/downloads/245358.htm // //===----------------------------------------------------------------------===// #include <assert.h> #include <stdlib.h> #include <string.h> #include <typeinfo> #include "__cxxabi_config.h" #include "cxa_exception.h" #include "cxa_handlers.h" #include "private_typeinfo.h" #include "unwind.h" // TODO: This is a temporary workaround for libc++abi to recognize that it's being // built against LLVM's libunwind. LLVM's libunwind started reporting _LIBUNWIND_VERSION // in LLVM 15 -- we can remove this workaround after shipping LLVM 17. Once we remove // this workaround, it won't be possible to build libc++abi against libunwind headers // from LLVM 14 and before anymore. #if defined(____LIBUNWIND_CONFIG_H__) && !defined(_LIBUNWIND_VERSION) #define _LIBUNWIND_VERSION #endif #if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__) #include <windows.h> #include <winnt.h> extern "C" EXCEPTION_DISPOSITION _GCC_specific_handler(PEXCEPTION_RECORD, void *, PCONTEXT, PDISPATCHER_CONTEXT, _Unwind_Personality_Fn); #endif /* Exception Header Layout: +---------------------------+-----------------------------+---------------+ | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object | +---------------------------+-----------------------------+---------------+ ^ | +-------------------------------------------------------+ | +---------------------------+-----------------------------+ | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 | +---------------------------+-----------------------------+ Exception Handling Table Layout: +-----------------+--------+ | lpStartEncoding | (char) | +---------+-------+--------+---------------+-----------------------+ | lpStart | (encoded with lpStartEncoding) | defaults to funcStart | +---------+-----+--------+-----------------+---------------+-------+ | ttypeEncoding | (char) | Encoding of the type_info table | +---------------+-+------+----+----------------------------+----------------+ | classInfoOffset | (ULEB128) | Offset to type_info table, defaults to null | +-----------------++--------+-+----------------------------+----------------+ | callSiteEncoding | (char) | Encoding for Call Site Table | +------------------+--+-----+-----+------------------------+--------------------------+ | callSiteTableLength | (ULEB128) | Call Site Table length, used to find Action table | +---------------------+-----------+---------------------------------------------------+ #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 | | call sites. There may be action needed. | | +-------------+---------------------------------+------------------------------+ | | | start | (encoded with callSiteEncoding) | offset relative to funcStart | | | | length | (encoded with callSiteEncoding) | length of code fragment | | | | landingPad | (encoded with callSiteEncoding) | offset relative to lpStart | | | | actionEntry | (ULEB128) | Action Table Index 1-based | | | | | | actionEntry == 0 -> cleanup | | | +-------------+---------------------------------+------------------------------+ | | ... | +----------------------------------------------------------------------------------+ #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 | | action is needed. Or it is 0 meaning | | terminate. | | +-------------+---------------------------------+------------------------------+ | | | landingPad | (ULEB128) | offset relative to lpStart | | | | actionEntry | (ULEB128) | Action Table Index 1-based | | | | | | actionEntry == 0 -> cleanup | | | +-------------+---------------------------------+------------------------------+ | | ... | +----------------------------------------------------------------------------------+ #endif // __USING_SJLJ_EXCEPTIONS__ || __WASM_EXCEPTIONS__ +---------------------------------------------------------------------+ | Beginning of Action Table ttypeIndex == 0 : cleanup | | ... ttypeIndex > 0 : catch | | ttypeIndex < 0 : exception spec | | +--------------+-----------+--------------------------------------+ | | | ttypeIndex | (SLEB128) | Index into type_info Table (1-based) | | | | actionOffset | (SLEB128) | Offset into next Action Table entry | | | +--------------+-----------+--------------------------------------+ | | ... | +---------------------------------------------------------------------+-----------------+ | type_info Table, but classInfoOffset does *not* point here! | | +----------------+------------------------------------------------+-----------------+ | | | Nth type_info* | Encoded with ttypeEncoding, 0 means catch(...) | ttypeIndex == N | | | +----------------+------------------------------------------------+-----------------+ | | ... | | +----------------+------------------------------------------------+-----------------+ | | | 1st type_info* | Encoded with ttypeEncoding, 0 means catch(...) | ttypeIndex == 1 | | | +----------------+------------------------------------------------+-----------------+ | | +---------------------------------------+-----------+------------------------------+ | | | 1st ttypeIndex for 1st exception spec | (ULEB128) | classInfoOffset points here! | | | | ... | (ULEB128) | | | | | Mth ttypeIndex for 1st exception spec | (ULEB128) | | | | | 0 | (ULEB128) | | | | +---------------------------------------+------------------------------------------+ | | ... | | +---------------------------------------+------------------------------------------+ | | | 0 | (ULEB128) | throw() | | | +---------------------------------------+------------------------------------------+ | | ... | | +---------------------------------------+------------------------------------------+ | | | 1st ttypeIndex for Nth exception spec | (ULEB128) | | | | | ... | (ULEB128) | | | | | Mth ttypeIndex for Nth exception spec | (ULEB128) | | | | | 0 | (ULEB128) | | | | +---------------------------------------+------------------------------------------+ | +---------------------------------------------------------------------------------------+ Notes: * ttypeIndex in the Action Table, and in the exception spec table, is an index, not a byte count, if positive. It is a negative index offset of classInfoOffset and the sizeof entry depends on ttypeEncoding. But if ttypeIndex is negative, it is a positive 1-based byte offset into the type_info Table. And if ttypeIndex is zero, it refers to a catch (...). * landingPad can be 0, this implies there is nothing to be done. * landingPad != 0 and actionEntry == 0 implies a cleanup needs to be done @landingPad. * A cleanup can also be found under landingPad != 0 and actionEntry != 0 in the Action Table with ttypeIndex == 0. */ namespace __cxxabiv1 { namespace { template <class AsType> uintptr_t readPointerHelper(const uint8_t*& p) { … } } // end namespace extern "C" { // private API // Heavily borrowed from llvm/examples/ExceptionDemo/ExceptionDemo.cpp // DWARF Constants enum { … }; /// Read a uleb128 encoded value and advance pointer /// See Variable Length Data Appendix C in: /// @link http://dwarfstd.org/Dwarf4.pdf @unlink /// @param data reference variable holding memory pointer to decode from /// @returns decoded value static uintptr_t readULEB128(const uint8_t** data) { … } /// Read a sleb128 encoded value and advance pointer /// See Variable Length Data Appendix C in: /// @link http://dwarfstd.org/Dwarf4.pdf @unlink /// @param data reference variable holding memory pointer to decode from /// @returns decoded value static intptr_t readSLEB128(const uint8_t** data) { … } /// Read a pointer encoded value and advance pointer /// See Variable Length Data in: /// @link http://dwarfstd.org/Dwarf3.pdf @unlink /// @param data reference variable holding memory pointer to decode from /// @param encoding dwarf encoding type /// @param base for adding relative offset, default to 0 /// @returns decoded value static uintptr_t readEncodedPointer(const uint8_t** data, uint8_t encoding, uintptr_t base = 0) { … } static void call_terminate(bool native_exception, _Unwind_Exception* unwind_exception) { … } #if defined(_LIBCXXABI_ARM_EHABI) static const void* read_target2_value(const void* ptr) { uintptr_t offset = *reinterpret_cast<const uintptr_t*>(ptr); if (!offset) return 0; // "ARM EABI provides a TARGET2 relocation to describe these typeinfo // pointers. The reason being it allows their precise semantics to be // deferred to the linker. For bare-metal they turn into absolute // relocations. For linux they turn into GOT-REL relocations." // https://gcc.gnu.org/ml/gcc-patches/2009-08/msg00264.html #if defined(LIBCXXABI_BAREMETAL) return reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(ptr) + offset); #else return *reinterpret_cast<const void **>(reinterpret_cast<uintptr_t>(ptr) + offset); #endif } static const __shim_type_info* get_shim_type_info(uint64_t ttypeIndex, const uint8_t* classInfo, uint8_t ttypeEncoding, bool native_exception, _Unwind_Exception* unwind_exception, uintptr_t /*base*/ = 0) { if (classInfo == 0) { // this should not happen. Indicates corrupted eh_table. call_terminate(native_exception, unwind_exception); } assert(((ttypeEncoding == DW_EH_PE_absptr) || // LLVM or GCC 4.6 (ttypeEncoding == DW_EH_PE_pcrel) || // GCC 4.7 baremetal (ttypeEncoding == (DW_EH_PE_pcrel | DW_EH_PE_indirect))) && // GCC 4.7 linux "Unexpected TTypeEncoding"); (void)ttypeEncoding; const uint8_t* ttypePtr = classInfo - ttypeIndex * sizeof(uintptr_t); return reinterpret_cast<const __shim_type_info *>( read_target2_value(ttypePtr)); } #else // !defined(_LIBCXXABI_ARM_EHABI) static const __shim_type_info* get_shim_type_info(uint64_t ttypeIndex, const uint8_t* classInfo, uint8_t ttypeEncoding, bool native_exception, _Unwind_Exception* unwind_exception, uintptr_t base = 0) { … } #endif // !defined(_LIBCXXABI_ARM_EHABI) /* This is checking a thrown exception type, excpType, against a possibly empty list of catchType's which make up an exception spec. An exception spec acts like a catch handler, but in reverse. This "catch handler" will catch an excpType if and only if none of the catchType's in the list will catch a excpType. If any catchType in the list can catch an excpType, then this exception spec does not catch the excpType. */ #if defined(_LIBCXXABI_ARM_EHABI) static bool exception_spec_can_catch(int64_t specIndex, const uint8_t* classInfo, uint8_t ttypeEncoding, const __shim_type_info* excpType, void* adjustedPtr, _Unwind_Exception* unwind_exception, uintptr_t /*base*/ = 0) { if (classInfo == 0) { // this should not happen. Indicates corrupted eh_table. call_terminate(false, unwind_exception); } assert(((ttypeEncoding == DW_EH_PE_absptr) || // LLVM or GCC 4.6 (ttypeEncoding == DW_EH_PE_pcrel) || // GCC 4.7 baremetal (ttypeEncoding == (DW_EH_PE_pcrel | DW_EH_PE_indirect))) && // GCC 4.7 linux "Unexpected TTypeEncoding"); (void)ttypeEncoding; // specIndex is negative of 1-based byte offset into classInfo; specIndex = -specIndex; --specIndex; const void** temp = reinterpret_cast<const void**>( reinterpret_cast<uintptr_t>(classInfo) + static_cast<uintptr_t>(specIndex) * sizeof(uintptr_t)); // If any type in the spec list can catch excpType, return false, else return true // adjustments to adjustedPtr are ignored. while (true) { // ARM EHABI exception specification table (filter table) consists of // several pointers which will directly point to the type info object // (instead of ttypeIndex). The table will be terminated with 0. const void** ttypePtr = temp++; if (*ttypePtr == 0) break; // We can get the __shim_type_info simply by performing a // R_ARM_TARGET2 relocation, and cast the result to __shim_type_info. const __shim_type_info* catchType = static_cast<const __shim_type_info*>(read_target2_value(ttypePtr)); void* tempPtr = adjustedPtr; if (catchType->can_catch(excpType, tempPtr)) return false; } return true; } #else static bool exception_spec_can_catch(int64_t specIndex, const uint8_t* classInfo, uint8_t ttypeEncoding, const __shim_type_info* excpType, void* adjustedPtr, _Unwind_Exception* unwind_exception, uintptr_t base = 0) { … } #endif static void* get_thrown_object_ptr(_Unwind_Exception* unwind_exception) { … } namespace { struct scan_results { … }; } // unnamed namespace static void set_registers(_Unwind_Exception* unwind_exception, _Unwind_Context* context, const scan_results& results) { … } /* There are 3 types of scans needed: 1. Scan for handler with native or foreign exception. If handler found, save state and return _URC_HANDLER_FOUND, else return _URC_CONTINUE_UNWIND. May also report an error on invalid input. May terminate for invalid exception table. _UA_SEARCH_PHASE 2. Scan for handler with foreign exception. Must return _URC_HANDLER_FOUND, or call terminate. _UA_CLEANUP_PHASE && _UA_HANDLER_FRAME && !native_exception 3. Scan for cleanups. If a handler is found and this isn't forced unwind, then terminate, otherwise ignore the handler and keep looking for cleanup. If a cleanup is found, return _URC_HANDLER_FOUND, else return _URC_CONTINUE_UNWIND. May also report an error on invalid input. May terminate for invalid exception table. _UA_CLEANUP_PHASE && !_UA_HANDLER_FRAME */ static void scan_eh_tab(scan_results &results, _Unwind_Action actions, bool native_exception, _Unwind_Exception *unwind_exception, _Unwind_Context *context) { … } // public API /* The personality function branches on actions like so: _UA_SEARCH_PHASE If _UA_CLEANUP_PHASE or _UA_HANDLER_FRAME or _UA_FORCE_UNWIND there's an error from above, return _URC_FATAL_PHASE1_ERROR. Scan for anything that could stop unwinding: 1. A catch clause that will catch this exception (will never catch foreign). 2. A catch (...) (will always catch foreign). 3. An exception spec that will catch this exception (will always catch foreign). If a handler is found If not foreign Save state in header return _URC_HANDLER_FOUND Else a handler not found return _URC_CONTINUE_UNWIND _UA_CLEANUP_PHASE If _UA_HANDLER_FRAME If _UA_FORCE_UNWIND How did this happen? return _URC_FATAL_PHASE2_ERROR If foreign Do _UA_SEARCH_PHASE to recover state else Recover state from header Transfer control to landing pad. return _URC_INSTALL_CONTEXT Else This branch handles both normal C++ non-catching handlers (cleanups) and forced unwinding. Scan for anything that can not stop unwinding: 1. A cleanup. If a cleanup is found transfer control to it. return _URC_INSTALL_CONTEXT Else a cleanup is not found: return _URC_CONTINUE_UNWIND */ #if !defined(_LIBCXXABI_ARM_EHABI) #ifdef __WASM_EXCEPTIONS__ _Unwind_Reason_Code __gxx_personality_wasm0 #elif defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__) static _Unwind_Reason_Code __gxx_personality_imp #else _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code #ifdef __USING_SJLJ_EXCEPTIONS__ __gxx_personality_sj0 #elif defined(__MVS__) __zos_cxx_personality_v2 #else __gxx_personality_v0 #endif #endif (int version, _Unwind_Action actions, uint64_t exceptionClass, _Unwind_Exception* unwind_exception, _Unwind_Context* context) { … } #if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__) extern "C" _LIBCXXABI_FUNC_VIS EXCEPTION_DISPOSITION __gxx_personality_seh0(PEXCEPTION_RECORD ms_exc, void *this_frame, PCONTEXT ms_orig_context, PDISPATCHER_CONTEXT ms_disp) { return _GCC_specific_handler(ms_exc, this_frame, ms_orig_context, ms_disp, __gxx_personality_imp); } #endif #else extern "C" _Unwind_Reason_Code __gnu_unwind_frame(_Unwind_Exception*, _Unwind_Context*); // Helper function to unwind one frame. // ARM EHABI 7.3 and 7.4: If the personality function returns _URC_CONTINUE_UNWIND, the // personality routine should update the virtual register set (VRS) according to the // corresponding frame unwinding instructions (ARM EHABI 9.3.) static _Unwind_Reason_Code continue_unwind(_Unwind_Exception* unwind_exception, _Unwind_Context* context) { switch (__gnu_unwind_frame(unwind_exception, context)) { case _URC_OK: return _URC_CONTINUE_UNWIND; case _URC_END_OF_STACK: return _URC_END_OF_STACK; default: return _URC_FAILURE; } } // ARM register names #if !defined(_LIBUNWIND_VERSION) static const uint32_t REG_UCB = 12; // Register to save _Unwind_Control_Block #endif static const uint32_t REG_SP = 13; static void save_results_to_barrier_cache(_Unwind_Exception* unwind_exception, const scan_results& results) { unwind_exception->barrier_cache.bitpattern[0] = (uint32_t)results.adjustedPtr; unwind_exception->barrier_cache.bitpattern[1] = (uint32_t)results.actionRecord; unwind_exception->barrier_cache.bitpattern[2] = (uint32_t)results.languageSpecificData; unwind_exception->barrier_cache.bitpattern[3] = (uint32_t)results.landingPad; unwind_exception->barrier_cache.bitpattern[4] = (uint32_t)results.ttypeIndex; } static void load_results_from_barrier_cache(scan_results& results, const _Unwind_Exception* unwind_exception) { results.adjustedPtr = (void*)unwind_exception->barrier_cache.bitpattern[0]; results.actionRecord = (const uint8_t*)unwind_exception->barrier_cache.bitpattern[1]; results.languageSpecificData = (const uint8_t*)unwind_exception->barrier_cache.bitpattern[2]; results.landingPad = (uintptr_t)unwind_exception->barrier_cache.bitpattern[3]; results.ttypeIndex = (int64_t)(int32_t)unwind_exception->barrier_cache.bitpattern[4]; } extern "C" _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code __gxx_personality_v0(_Unwind_State state, _Unwind_Exception* unwind_exception, _Unwind_Context* context) { if (unwind_exception == 0 || context == 0) return _URC_FATAL_PHASE1_ERROR; bool native_exception = __isOurExceptionClass(unwind_exception); #if !defined(_LIBUNWIND_VERSION) // Copy the address of _Unwind_Control_Block to r12 so that // _Unwind_GetLanguageSpecificData() and _Unwind_GetRegionStart() can // return correct address. _Unwind_SetGR(context, REG_UCB, reinterpret_cast<uint32_t>(unwind_exception)); #endif // Check the undocumented force unwinding behavior bool is_force_unwinding = state & _US_FORCE_UNWIND; state &= ~_US_FORCE_UNWIND; scan_results results; switch (state) { case _US_VIRTUAL_UNWIND_FRAME: if (is_force_unwinding) return continue_unwind(unwind_exception, context); // Phase 1 search: All we're looking for in phase 1 is a handler that halts unwinding scan_eh_tab(results, _UA_SEARCH_PHASE, native_exception, unwind_exception, context); if (results.reason == _URC_HANDLER_FOUND) { unwind_exception->barrier_cache.sp = _Unwind_GetGR(context, REG_SP); if (native_exception) save_results_to_barrier_cache(unwind_exception, results); return _URC_HANDLER_FOUND; } // Did not find the catch handler if (results.reason == _URC_CONTINUE_UNWIND) return continue_unwind(unwind_exception, context); return results.reason; case _US_UNWIND_FRAME_STARTING: // TODO: Support force unwinding in the phase 2 search. // NOTE: In order to call the cleanup functions, _Unwind_ForcedUnwind() // will call this personality function with (_US_FORCE_UNWIND | // _US_UNWIND_FRAME_STARTING). // Phase 2 search if (unwind_exception->barrier_cache.sp == _Unwind_GetGR(context, REG_SP)) { // Found a catching handler in phase 1 if (native_exception) { // Load the result from the native exception barrier cache. load_results_from_barrier_cache(results, unwind_exception); results.reason = _URC_HANDLER_FOUND; } else { // Search for the catching handler again for the foreign exception. scan_eh_tab(results, static_cast<_Unwind_Action>(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME), native_exception, unwind_exception, context); if (results.reason != _URC_HANDLER_FOUND) // phase1 search should guarantee to find one call_terminate(native_exception, unwind_exception); } // Install the context for the catching handler set_registers(unwind_exception, context, results); return _URC_INSTALL_CONTEXT; } // Either we didn't do a phase 1 search (due to forced unwinding), or // phase 1 reported no catching-handlers. // Search for a (non-catching) cleanup if (is_force_unwinding) scan_eh_tab( results, static_cast<_Unwind_Action>(_UA_CLEANUP_PHASE | _UA_FORCE_UNWIND), native_exception, unwind_exception, context); else scan_eh_tab(results, _UA_CLEANUP_PHASE, native_exception, unwind_exception, context); if (results.reason == _URC_HANDLER_FOUND) { // Found a non-catching handler // ARM EHABI 8.4.2: Before we can jump to the cleanup handler, we have to setup some // internal data structures, so that __cxa_end_cleanup() can get unwind_exception from // __cxa_get_globals(). __cxa_begin_cleanup(unwind_exception); // Install the context for the cleanup handler set_registers(unwind_exception, context, results); return _URC_INSTALL_CONTEXT; } // Did not find any handler if (results.reason == _URC_CONTINUE_UNWIND) return continue_unwind(unwind_exception, context); return results.reason; case _US_UNWIND_FRAME_RESUME: return continue_unwind(unwind_exception, context); } // We were called improperly: neither a phase 1 or phase 2 search return _URC_FATAL_PHASE1_ERROR; } #endif __attribute__((noreturn)) _LIBCXXABI_FUNC_VIS void __cxa_call_unexpected(void* arg) { … } #if defined(_AIX) // Personality routine for EH using the range table. Make it an alias of // __gxx_personality_v0(). _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code __xlcxx_personality_v1( int version, _Unwind_Action actions, uint64_t exceptionClass, _Unwind_Exception* unwind_exception, _Unwind_Context* context) __attribute__((__alias__("__gxx_personality_v0"))); #endif } // extern "C" } // __cxxabiv1 #if defined(_AIX) // Include implementation of the personality and helper functions for the // state table based EH used by IBM legacy compilers xlC and xlclang++ on AIX. # include "aix_state_tab_eh.inc" #endif