brintos

brintos / llvm-project-archived public Read only

0
0
Text · 60.2 KiB · b7eb0f2 Raw
1431 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//8//  This file implements the "Exception Handling APIs"9//  https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html10//  http://www.intel.com/design/itanium/downloads/245358.htm11//12//===----------------------------------------------------------------------===//13 14#include <assert.h>15#include <stdlib.h>16#include <string.h>17#include <typeinfo>18 19#include "__cxxabi_config.h"20#include "cxa_exception.h"21#include "cxa_handlers.h"22#include "private_typeinfo.h"23 24#if __has_feature(ptrauth_calls)25 26// CXXABI depends on defintions in libunwind as pointer auth couples the27// definitions28#  include "libunwind.h"29 30// The actual value of the discriminators listed below is not important.31// The derivation of the constants is only being included for the purpose32// of maintaining a record of how they were originally produced.33 34// ptrauth_string_discriminator("scan_results::languageSpecificData") == 0xE50D)35#  define __ptrauth_scan_results_lsd __ptrauth(ptrauth_key_process_dependent_code, 1, 0xE50D)36 37// ptrauth_string_discriminator("scan_results::actionRecord") == 0x982338#  define __ptrauth_scan_results_action_record __ptrauth(ptrauth_key_process_dependent_code, 1, 0x9823)39 40// scan result is broken up as we have a manual re-sign that requires each component41#  define __ptrauth_scan_results_landingpad_key ptrauth_key_process_dependent_code42// ptrauth_string_discriminator("scan_results::landingPad") == 0xD27C43#  define __ptrauth_scan_results_landingpad_disc 0xD27C44#  define __ptrauth_scan_results_landingpad                                                                            \45    __ptrauth(__ptrauth_scan_results_landingpad_key, 1, __ptrauth_scan_results_landingpad_disc)46 47// `__ptrauth_restricted_intptr` is a feature of apple clang that predates48// support for direct application of `__ptrauth` to integer types. This49// guard is necessary to support compilation with those compiler.50#  if __has_extension(ptrauth_restricted_intptr_qualifier)51#    define __ptrauth_scan_results_landingpad_intptr                                                                   \52      __ptrauth_restricted_intptr(__ptrauth_scan_results_landingpad_key, 1, __ptrauth_scan_results_landingpad_disc)53#  else54#    define __ptrauth_scan_results_landingpad_intptr                                                                   \55      __ptrauth(__ptrauth_scan_results_landingpad_key, 1, __ptrauth_scan_results_landingpad_disc)56#  endif57 58#else59#  define __ptrauth_scan_results_lsd60#  define __ptrauth_scan_results_action_record61#  define __ptrauth_scan_results_landingpad62#  define __ptrauth_scan_results_landingpad_intptr63#endif64 65// TODO: This is a temporary workaround for libc++abi to recognize that it's being66// built against LLVM's libunwind. LLVM's libunwind started reporting _LIBUNWIND_VERSION67// in LLVM 15 -- we can remove this workaround after shipping LLVM 17. Once we remove68// this workaround, it won't be possible to build libc++abi against libunwind headers69// from LLVM 14 and before anymore.70#if defined(____LIBUNWIND_CONFIG_H__) && !defined(_LIBUNWIND_VERSION)71#   define _LIBUNWIND_VERSION72#endif73 74#if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)75#include <windows.h>76#include <winnt.h>77 78extern "C" EXCEPTION_DISPOSITION _GCC_specific_handler(PEXCEPTION_RECORD,79                                                       void *, PCONTEXT,80                                                       PDISPATCHER_CONTEXT,81                                                       _Unwind_Personality_Fn);82#endif83 84/*85    Exception Header Layout:86 87+---------------------------+-----------------------------+---------------+88| __cxa_exception           | _Unwind_Exception CLNGC++\0 | thrown object |89+---------------------------+-----------------------------+---------------+90                                                          ^91                                                          |92  +-------------------------------------------------------+93  |94+---------------------------+-----------------------------+95| __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 |96+---------------------------+-----------------------------+97 98    Exception Handling Table Layout:99 100+-----------------+--------+101| lpStartEncoding | (char) |102+---------+-------+--------+---------------+-----------------------+103| lpStart | (encoded with lpStartEncoding) | defaults to funcStart |104+---------+-----+--------+-----------------+---------------+-------+105| ttypeEncoding | (char) | Encoding of the type_info table |106+---------------+-+------+----+----------------------------+----------------+107| classInfoOffset | (ULEB128) | Offset to type_info table, defaults to null |108+-----------------++--------+-+----------------------------+----------------+109| callSiteEncoding | (char) | Encoding for Call Site Table |110+------------------+--+-----+-----+------------------------+--------------------------+111| callSiteTableLength | (ULEB128) | Call Site Table length, used to find Action table |112+---------------------+-----------+---------------------------------------------------+113#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__WASM_EXCEPTIONS__)114+---------------------+-----------+------------------------------------------------+115| Beginning of Call Site Table            The current ip lies within the           |116| ...                                     (start, length) range of one of these    |117|                                         call sites. There may be action needed.  |118| +-------------+---------------------------------+------------------------------+ |119| | start       | (encoded with callSiteEncoding) | offset relative to funcStart | |120| | length      | (encoded with callSiteEncoding) | length of code fragment      | |121| | landingPad  | (encoded with callSiteEncoding) | offset relative to lpStart   | |122| | actionEntry | (ULEB128)                       | Action Table Index 1-based   | |123| |             |                                 | actionEntry == 0 -> cleanup  | |124| +-------------+---------------------------------+------------------------------+ |125| ...                                                                              |126+----------------------------------------------------------------------------------+127#else  // __USING_SJLJ_EXCEPTIONS__ || __WASM_EXCEPTIONS__128+---------------------+-----------+------------------------------------------------+129| Beginning of Call Site Table            The current ip is a 1-based index into   |130| ...                                     this table.  Or it is -1 meaning no      |131|                                         action is needed.  Or it is 0 meaning    |132|                                         terminate.                               |133| +-------------+---------------------------------+------------------------------+ |134| | landingPad  | (ULEB128)                       | offset relative to lpStart   | |135| | actionEntry | (ULEB128)                       | Action Table Index 1-based   | |136| |             |                                 | actionEntry == 0 -> cleanup  | |137| +-------------+---------------------------------+------------------------------+ |138| ...                                                                              |139+----------------------------------------------------------------------------------+140#endif // __USING_SJLJ_EXCEPTIONS__ || __WASM_EXCEPTIONS__141+---------------------------------------------------------------------+142| Beginning of Action Table       ttypeIndex == 0 : cleanup           |143| ...                             ttypeIndex  > 0 : catch             |144|                                 ttypeIndex  < 0 : exception spec    |145| +--------------+-----------+--------------------------------------+ |146| | ttypeIndex   | (SLEB128) | Index into type_info Table (1-based) | |147| | actionOffset | (SLEB128) | Offset into next Action Table entry  | |148| +--------------+-----------+--------------------------------------+ |149| ...                                                                 |150+---------------------------------------------------------------------+-----------------+151| type_info Table, but classInfoOffset does *not* point here!                           |152| +----------------+------------------------------------------------+-----------------+ |153| | Nth type_info* | Encoded with ttypeEncoding, 0 means catch(...) | ttypeIndex == N | |154| +----------------+------------------------------------------------+-----------------+ |155| ...                                                                                   |156| +----------------+------------------------------------------------+-----------------+ |157| | 1st type_info* | Encoded with ttypeEncoding, 0 means catch(...) | ttypeIndex == 1 | |158| +----------------+------------------------------------------------+-----------------+ |159| +---------------------------------------+-----------+------------------------------+  |160| | 1st ttypeIndex for 1st exception spec | (ULEB128) | classInfoOffset points here! |  |161| | ...                                   | (ULEB128) |                              |  |162| | Mth ttypeIndex for 1st exception spec | (ULEB128) |                              |  |163| | 0                                     | (ULEB128) |                              |  |164| +---------------------------------------+------------------------------------------+  |165| ...                                                                                   |166| +---------------------------------------+------------------------------------------+  |167| | 0                                     | (ULEB128) | throw()                      |  |168| +---------------------------------------+------------------------------------------+  |169| ...                                                                                   |170| +---------------------------------------+------------------------------------------+  |171| | 1st ttypeIndex for Nth exception spec | (ULEB128) |                              |  |172| | ...                                   | (ULEB128) |                              |  |173| | Mth ttypeIndex for Nth exception spec | (ULEB128) |                              |  |174| | 0                                     | (ULEB128) |                              |  |175| +---------------------------------------+------------------------------------------+  |176+---------------------------------------------------------------------------------------+177 178Notes:179 180*  ttypeIndex in the Action Table, and in the exception spec table, is an index,181     not a byte count, if positive.  It is a negative index offset of182     classInfoOffset and the sizeof entry depends on ttypeEncoding.183   But if ttypeIndex is negative, it is a positive 1-based byte offset into the184     type_info Table.185   And if ttypeIndex is zero, it refers to a catch (...).186 187*  landingPad can be 0, this implies there is nothing to be done.188 189*  landingPad != 0 and actionEntry == 0 implies a cleanup needs to be done190     @landingPad.191 192*  A cleanup can also be found under landingPad != 0 and actionEntry != 0 in193     the Action Table with ttypeIndex == 0.194*/195 196namespace __cxxabiv1197{198 199namespace200{201 202template <class AsType>203uintptr_t readPointerHelper(const uint8_t*& p) {204    AsType value;205    memcpy(&value, p, sizeof(AsType));206    p += sizeof(AsType);207    return static_cast<uintptr_t>(value);208}209 210} // namespace211 212extern "C"213{214 215// private API216 217// Heavily borrowed from llvm/examples/ExceptionDemo/ExceptionDemo.cpp218 219// DWARF Constants220enum221{222    DW_EH_PE_absptr   = 0x00,223    DW_EH_PE_uleb128  = 0x01,224    DW_EH_PE_udata2   = 0x02,225    DW_EH_PE_udata4   = 0x03,226    DW_EH_PE_udata8   = 0x04,227    DW_EH_PE_sleb128  = 0x09,228    DW_EH_PE_sdata2   = 0x0A,229    DW_EH_PE_sdata4   = 0x0B,230    DW_EH_PE_sdata8   = 0x0C,231    DW_EH_PE_pcrel    = 0x10,232    DW_EH_PE_textrel  = 0x20,233    DW_EH_PE_datarel  = 0x30,234    DW_EH_PE_funcrel  = 0x40,235    DW_EH_PE_aligned  = 0x50,236    DW_EH_PE_indirect = 0x80,237    DW_EH_PE_omit     = 0xFF238};239 240/// Read a uleb128 encoded value and advance pointer241/// See Variable Length Data Appendix C in:242/// @link http://dwarfstd.org/Dwarf4.pdf @unlink243/// @param data reference variable holding memory pointer to decode from244/// @returns decoded value245static246uintptr_t247readULEB128(const uint8_t** data)248{249    uintptr_t result = 0;250    uintptr_t shift = 0;251    unsigned char byte;252    const uint8_t *p = *data;253    do254    {255        byte = *p++;256        result |= static_cast<uintptr_t>(byte & 0x7F) << shift;257        shift += 7;258    } while (byte & 0x80);259    *data = p;260    return result;261}262 263/// Read a sleb128 encoded value and advance pointer264/// See Variable Length Data Appendix C in:265/// @link http://dwarfstd.org/Dwarf4.pdf @unlink266/// @param data reference variable holding memory pointer to decode from267/// @returns decoded value268static269intptr_t270readSLEB128(const uint8_t** data)271{272    uintptr_t result = 0;273    uintptr_t shift = 0;274    unsigned char byte;275    const uint8_t *p = *data;276    do277    {278        byte = *p++;279        result |= static_cast<uintptr_t>(byte & 0x7F) << shift;280        shift += 7;281    } while (byte & 0x80);282    *data = p;283    if ((byte & 0x40) && (shift < (sizeof(result) << 3)))284        result |= static_cast<uintptr_t>(~0) << shift;285    return static_cast<intptr_t>(result);286}287 288/// Read a pointer encoded value and advance pointer289/// See Variable Length Data in:290/// @link http://dwarfstd.org/Dwarf3.pdf @unlink291/// @param data reference variable holding memory pointer to decode from292/// @param encoding dwarf encoding type293/// @param base for adding relative offset, default to 0294/// @returns decoded value295static296uintptr_t297readEncodedPointer(const uint8_t** data, uint8_t encoding, uintptr_t base = 0)298{299    uintptr_t result = 0;300    if (encoding == DW_EH_PE_omit)301        return result;302    const uint8_t* p = *data;303    // first get value304    switch (encoding & 0x0F)305    {306    case DW_EH_PE_absptr:307        result = readPointerHelper<uintptr_t>(p);308        break;309    case DW_EH_PE_uleb128:310        result = readULEB128(&p);311        break;312    case DW_EH_PE_sleb128:313        result = static_cast<uintptr_t>(readSLEB128(&p));314        break;315    case DW_EH_PE_udata2:316        result = readPointerHelper<uint16_t>(p);317        break;318    case DW_EH_PE_udata4:319        result = readPointerHelper<uint32_t>(p);320        break;321    case DW_EH_PE_udata8:322        result = readPointerHelper<uint64_t>(p);323        break;324    case DW_EH_PE_sdata2:325        result = readPointerHelper<int16_t>(p);326        break;327    case DW_EH_PE_sdata4:328        result = readPointerHelper<int32_t>(p);329        break;330    case DW_EH_PE_sdata8:331        result = readPointerHelper<int64_t>(p);332        break;333    default:334        // not supported335        abort();336        break;337    }338    // then add relative offset339    switch (encoding & 0x70)340    {341    case DW_EH_PE_absptr:342        // do nothing343        break;344    case DW_EH_PE_pcrel:345        if (result)346            result += (uintptr_t)(*data);347        break;348    case DW_EH_PE_datarel:349        assert((base != 0) && "DW_EH_PE_datarel is invalid with a base of 0");350        if (result)351            result += base;352        break;353    case DW_EH_PE_textrel:354    case DW_EH_PE_funcrel:355    case DW_EH_PE_aligned:356    default:357        // not supported358        abort();359        break;360    }361    // then apply indirection362    if (result && (encoding & DW_EH_PE_indirect))363        result = *((uintptr_t*)result);364    *data = p;365    return result;366}367 368static369void370call_terminate(bool native_exception, _Unwind_Exception* unwind_exception)371{372    __cxa_begin_catch(unwind_exception);373    if (native_exception)374    {375        // Use the stored terminate_handler if possible376        __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1;377        std::__terminate(exception_header->terminateHandler);378    }379    std::terminate();380}381 382#if defined(_LIBCXXABI_ARM_EHABI)383static const void* read_target2_value(const void* ptr)384{385    uintptr_t offset = *reinterpret_cast<const uintptr_t*>(ptr);386    if (!offset)387        return 0;388    // "ARM EABI provides a TARGET2 relocation to describe these typeinfo389    // pointers. The reason being it allows their precise semantics to be390    // deferred to the linker. For bare-metal they turn into absolute391    // relocations. For linux they turn into GOT-REL relocations."392    // https://gcc.gnu.org/ml/gcc-patches/2009-08/msg00264.html393#if defined(LIBCXXABI_BAREMETAL)394    return reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(ptr) +395                                         offset);396#else397    return *reinterpret_cast<const void **>(reinterpret_cast<uintptr_t>(ptr) +398                                            offset);399#endif400}401 402static const __shim_type_info*403get_shim_type_info(uint64_t ttypeIndex, const uint8_t* classInfo,404                   uint8_t ttypeEncoding, bool native_exception,405                   _Unwind_Exception* unwind_exception, uintptr_t /*base*/ = 0)406{407    if (classInfo == 0)408    {409        // this should not happen.  Indicates corrupted eh_table.410        call_terminate(native_exception, unwind_exception);411    }412 413    assert(((ttypeEncoding == DW_EH_PE_absptr) ||  // LLVM or GCC 4.6414            (ttypeEncoding == DW_EH_PE_pcrel) ||  // GCC 4.7 baremetal415            (ttypeEncoding == (DW_EH_PE_pcrel | DW_EH_PE_indirect))) &&  // GCC 4.7 linux416           "Unexpected TTypeEncoding");417    (void)ttypeEncoding;418 419    const uint8_t* ttypePtr = classInfo - ttypeIndex * sizeof(uintptr_t);420    return reinterpret_cast<const __shim_type_info *>(421        read_target2_value(ttypePtr));422}423#else // !defined(_LIBCXXABI_ARM_EHABI)424static425const __shim_type_info*426get_shim_type_info(uint64_t ttypeIndex, const uint8_t* classInfo,427                   uint8_t ttypeEncoding, bool native_exception,428                   _Unwind_Exception* unwind_exception, uintptr_t base = 0)429{430    if (classInfo == 0)431    {432        // this should not happen.  Indicates corrupted eh_table.433        call_terminate(native_exception, unwind_exception);434    }435    switch (ttypeEncoding & 0x0F)436    {437    case DW_EH_PE_absptr:438        ttypeIndex *= sizeof(void*);439        break;440    case DW_EH_PE_udata2:441    case DW_EH_PE_sdata2:442        ttypeIndex *= 2;443        break;444    case DW_EH_PE_udata4:445    case DW_EH_PE_sdata4:446        ttypeIndex *= 4;447        break;448    case DW_EH_PE_udata8:449    case DW_EH_PE_sdata8:450        ttypeIndex *= 8;451        break;452    default:453        // this should not happen.   Indicates corrupted eh_table.454        call_terminate(native_exception, unwind_exception);455    }456    classInfo -= ttypeIndex;457    return (const __shim_type_info*)readEncodedPointer(&classInfo,458                                                       ttypeEncoding, base);459}460#endif // !defined(_LIBCXXABI_ARM_EHABI)461 462/*463    This is checking a thrown exception type, excpType, against a possibly empty464    list of catchType's which make up an exception spec.465 466    An exception spec acts like a catch handler, but in reverse.  This "catch467    handler" will catch an excpType if and only if none of the catchType's in468    the list will catch a excpType.  If any catchType in the list can catch an469    excpType, then this exception spec does not catch the excpType.470*/471#if defined(_LIBCXXABI_ARM_EHABI)472static473bool474exception_spec_can_catch(int64_t specIndex, const uint8_t* classInfo,475                         uint8_t ttypeEncoding, const __shim_type_info* excpType,476                         void* adjustedPtr, _Unwind_Exception* unwind_exception,477                         uintptr_t /*base*/ = 0)478{479    if (classInfo == 0)480    {481        // this should not happen.   Indicates corrupted eh_table.482        call_terminate(false, unwind_exception);483    }484 485    assert(((ttypeEncoding == DW_EH_PE_absptr) ||  // LLVM or GCC 4.6486            (ttypeEncoding == DW_EH_PE_pcrel) ||  // GCC 4.7 baremetal487            (ttypeEncoding == (DW_EH_PE_pcrel | DW_EH_PE_indirect))) &&  // GCC 4.7 linux488           "Unexpected TTypeEncoding");489    (void)ttypeEncoding;490 491    // specIndex is negative of 1-based byte offset into classInfo;492    specIndex = -specIndex;493    --specIndex;494    const void** temp = reinterpret_cast<const void**>(495        reinterpret_cast<uintptr_t>(classInfo) +496        static_cast<uintptr_t>(specIndex) * sizeof(uintptr_t));497    // If any type in the spec list can catch excpType, return false, else return true498    //    adjustments to adjustedPtr are ignored.499    while (true)500    {501        // ARM EHABI exception specification table (filter table) consists of502        // several pointers which will directly point to the type info object503        // (instead of ttypeIndex).  The table will be terminated with 0.504        const void** ttypePtr = temp++;505        if (*ttypePtr == 0)506            break;507        // We can get the __shim_type_info simply by performing a508        // R_ARM_TARGET2 relocation, and cast the result to __shim_type_info.509        const __shim_type_info* catchType =510            static_cast<const __shim_type_info*>(read_target2_value(ttypePtr));511        void* tempPtr = adjustedPtr;512        if (catchType->can_catch(excpType, tempPtr))513            return false;514    }515    return true;516}517#else518static519bool520exception_spec_can_catch(int64_t specIndex, const uint8_t* classInfo,521                         uint8_t ttypeEncoding, const __shim_type_info* excpType,522                         void* adjustedPtr, _Unwind_Exception* unwind_exception,523                         uintptr_t base = 0)524{525    if (classInfo == 0)526    {527        // this should not happen.   Indicates corrupted eh_table.528        call_terminate(false, unwind_exception);529    }530    // specIndex is negative of 1-based byte offset into classInfo;531    specIndex = -specIndex;532    --specIndex;533    const uint8_t* temp = classInfo + specIndex;534    // If any type in the spec list can catch excpType, return false, else return true535    //    adjustments to adjustedPtr are ignored.536    while (true)537    {538        uint64_t ttypeIndex = readULEB128(&temp);539        if (ttypeIndex == 0)540            break;541        const __shim_type_info* catchType = get_shim_type_info(ttypeIndex,542                                                               classInfo,543                                                               ttypeEncoding,544                                                               true,545                                                               unwind_exception,546                                                               base);547        void* tempPtr = adjustedPtr;548        if (catchType->can_catch(excpType, tempPtr))549            return false;550    }551    return true;552}553#endif554 555static556void*557get_thrown_object_ptr(_Unwind_Exception* unwind_exception)558{559    // Even for foreign exceptions, the exception object is *probably* at unwind_exception + 1560    //    Regardless, this library is prohibited from touching a foreign exception561    void* adjustedPtr = unwind_exception + 1;562    if (__getExceptionClass(unwind_exception) == kOurDependentExceptionClass)563        adjustedPtr = ((__cxa_dependent_exception*)adjustedPtr - 1)->primaryException;564    return adjustedPtr;565}566 567namespace568{569 570typedef const uint8_t *__ptrauth_scan_results_lsd lsd_ptr_t;571typedef const uint8_t *__ptrauth_scan_results_action_record action_ptr_t;572typedef uintptr_t __ptrauth_scan_results_landingpad_intptr landing_pad_t;573typedef void *__ptrauth_scan_results_landingpad landing_pad_ptr_t;574 575struct scan_results576{577    int64_t        ttypeIndex;   // > 0 catch handler, < 0 exception spec handler, == 0 a cleanup578    action_ptr_t   actionRecord; // Currently unused.  Retained to ease future maintenance.579    lsd_ptr_t      languageSpecificData; // Needed only for __cxa_call_unexpected580    landing_pad_t  landingPad;   // null -> nothing found, else something found581    void*          adjustedPtr;  // Used in cxa_exception.cpp582    _Unwind_Reason_Code reason;  // One of _URC_FATAL_PHASE1_ERROR,583                                 //        _URC_FATAL_PHASE2_ERROR,584                                 //        _URC_CONTINUE_UNWIND,585                                 //        _URC_HANDLER_FOUND586};587 588}  // unnamed namespace589 590static591void592set_registers(_Unwind_Exception* unwind_exception, _Unwind_Context* context,593              const scan_results& results)594{595#if defined(__USING_SJLJ_EXCEPTIONS__) || defined(__WASM_EXCEPTIONS__)596#define __builtin_eh_return_data_regno(regno) regno597#elif defined(__ibmxl__)598// IBM xlclang++ compiler does not support __builtin_eh_return_data_regno.599#define __builtin_eh_return_data_regno(regno) regno + 3600#endif601  _Unwind_SetGR(context, __builtin_eh_return_data_regno(0),602                reinterpret_cast<uintptr_t>(unwind_exception));603  _Unwind_SetGR(context, __builtin_eh_return_data_regno(1),604                static_cast<uintptr_t>(results.ttypeIndex));605#if __has_feature(ptrauth_calls)606  auto stackPointer = _Unwind_GetGR(context, UNW_REG_SP);607  // We manually re-sign the IP as the __ptrauth qualifiers cannot608  // express the required relationship with the destination address609  const auto existingDiscriminator =610      ptrauth_blend_discriminator(&results.landingPad,611                                  __ptrauth_scan_results_landingpad_disc);612  unw_word_t newIP /* opaque __ptrauth(ptrauth_key_return_address, stackPointer, 0) */ =613      (unw_word_t)ptrauth_auth_and_resign(*(void* const*)&results.landingPad,614                                          __ptrauth_scan_results_landingpad_key,615                                          existingDiscriminator,616                                          ptrauth_key_return_address,617                                          stackPointer);618  _Unwind_SetIP(context, newIP);619#else620  _Unwind_SetIP(context, results.landingPad);621#endif622}623 624/*625    There are 3 types of scans needed:626 627    1.  Scan for handler with native or foreign exception.  If handler found,628        save state and return _URC_HANDLER_FOUND, else return _URC_CONTINUE_UNWIND.629        May also report an error on invalid input.630        May terminate for invalid exception table.631        _UA_SEARCH_PHASE632 633    2.  Scan for handler with foreign exception.  Must return _URC_HANDLER_FOUND,634        or call terminate.635        _UA_CLEANUP_PHASE && _UA_HANDLER_FRAME && !native_exception636 637    3.  Scan for cleanups.  If a handler is found and this isn't forced unwind,638        then terminate, otherwise ignore the handler and keep looking for cleanup.639        If a cleanup is found, return _URC_HANDLER_FOUND, else return _URC_CONTINUE_UNWIND.640        May also report an error on invalid input.641        May terminate for invalid exception table.642        _UA_CLEANUP_PHASE && !_UA_HANDLER_FRAME643*/644 645static void scan_eh_tab(scan_results &results, _Unwind_Action actions,646                        bool native_exception,647                        _Unwind_Exception *unwind_exception,648                        _Unwind_Context *context) {649    // Initialize results to found nothing but an error650    results.ttypeIndex = 0;651    results.actionRecord = 0;652    results.languageSpecificData = 0;653    results.landingPad = 0;654    results.adjustedPtr = 0;655    results.reason = _URC_FATAL_PHASE1_ERROR;656    // Check for consistent actions657    if (actions & _UA_SEARCH_PHASE)658    {659        // Do Phase 1660        if (actions & (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME | _UA_FORCE_UNWIND))661        {662            // None of these flags should be set during Phase 1663            //   Client error664            results.reason = _URC_FATAL_PHASE1_ERROR;665            return;666        }667    }668    else if (actions & _UA_CLEANUP_PHASE)669    {670        if ((actions & _UA_HANDLER_FRAME) && (actions & _UA_FORCE_UNWIND))671        {672            // _UA_HANDLER_FRAME should only be set if phase 1 found a handler.673            // If _UA_FORCE_UNWIND is set, phase 1 shouldn't have happened.674            //    Client error675            results.reason = _URC_FATAL_PHASE2_ERROR;676            return;677        }678    }679    else // Neither _UA_SEARCH_PHASE nor _UA_CLEANUP_PHASE is set680    {681        // One of these should be set.682        //   Client error683        results.reason = _URC_FATAL_PHASE1_ERROR;684        return;685    }686    // Start scan by getting exception table address.687    const uint8_t *lsda = (const uint8_t *)_Unwind_GetLanguageSpecificData(context);688    if (lsda == 0)689    {690        // There is no exception table691        results.reason = _URC_CONTINUE_UNWIND;692        return;693    }694    results.languageSpecificData = lsda;695#if defined(_AIX)696    uintptr_t base = _Unwind_GetDataRelBase(context);697#else698    uintptr_t base = 0;699#endif700    // Get the current instruction pointer and offset it before next701    // instruction in the current frame which threw the exception.702    uintptr_t ip = _Unwind_GetIP(context) - 1;703    // Get beginning current frame's code (as defined by the704    // emitted dwarf code)705    uintptr_t funcStart = _Unwind_GetRegionStart(context);706#if defined(__USING_SJLJ_EXCEPTIONS__) || defined(__WASM_EXCEPTIONS__)707    if (ip == uintptr_t(-1))708    {709        // no action710        results.reason = _URC_CONTINUE_UNWIND;711        return;712    }713    else if (ip == 0)714        call_terminate(native_exception, unwind_exception);715    // ip is 1-based index into call site table716#else  // !__USING_SJLJ_EXCEPTIONS__ && !__WASM_EXCEPTIONS__717    uintptr_t ipOffset = ip - funcStart;718#endif // !__USING_SJLJ_EXCEPTIONS__ && !__WASM_EXCEPTIONS__719    const uint8_t* classInfo = NULL;720    // Note: See JITDwarfEmitter::EmitExceptionTable(...) for corresponding721    //       dwarf emission722    // Parse LSDA header.723    uint8_t lpStartEncoding = *lsda++;724    const uint8_t* lpStart = lpStartEncoding == DW_EH_PE_omit725                                 ? (const uint8_t*)funcStart726                                 : (const uint8_t*)readEncodedPointer(&lsda, lpStartEncoding, base);727    uint8_t ttypeEncoding = *lsda++;728    if (ttypeEncoding != DW_EH_PE_omit)729    {730        // Calculate type info locations in emitted dwarf code which731        // were flagged by type info arguments to llvm.eh.selector732        // intrinsic733        uintptr_t classInfoOffset = readULEB128(&lsda);734        classInfo = lsda + classInfoOffset;735    }736    // Walk call-site table looking for range that737    // includes current PC.738    uint8_t callSiteEncoding = *lsda++;739#if defined(__USING_SJLJ_EXCEPTIONS__) || defined(__WASM_EXCEPTIONS__)740    (void)callSiteEncoding;  // When using SjLj/Wasm exceptions, callSiteEncoding is never used741#endif742    uint32_t callSiteTableLength = static_cast<uint32_t>(readULEB128(&lsda));743    const uint8_t* callSiteTableStart = lsda;744    const uint8_t* callSiteTableEnd = callSiteTableStart + callSiteTableLength;745    const uint8_t* actionTableStart = callSiteTableEnd;746    const uint8_t* callSitePtr = callSiteTableStart;747    while (callSitePtr < callSiteTableEnd)748    {749        // There is one entry per call site.750#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__WASM_EXCEPTIONS__)751        // The call sites are non-overlapping in [start, start+length)752        // The call sites are ordered in increasing value of start753        uintptr_t start = readEncodedPointer(&callSitePtr, callSiteEncoding);754        uintptr_t length = readEncodedPointer(&callSitePtr, callSiteEncoding);755        landing_pad_t landingPad = readEncodedPointer(&callSitePtr, callSiteEncoding);756        uintptr_t actionEntry = readULEB128(&callSitePtr);757        if ((start <= ipOffset) && (ipOffset < (start + length)))758#else  // __USING_SJLJ_EXCEPTIONS__ || __WASM_EXCEPTIONS__759        // ip is 1-based index into this table760        landing_pad_t landingPad = readULEB128(&callSitePtr);761        uintptr_t actionEntry = readULEB128(&callSitePtr);762        if (--ip == 0)763#endif // __USING_SJLJ_EXCEPTIONS__ || __WASM_EXCEPTIONS__764        {765            // Found the call site containing ip.766#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__WASM_EXCEPTIONS__)767            if (landingPad == 0)768            {769                // No handler here770                results.reason = _URC_CONTINUE_UNWIND;771                return;772            }773            landingPad = (uintptr_t)lpStart + landingPad;774#else  // __USING_SJLJ_EXCEPTIONS__ || __WASM_EXCEPTIONS__775            ++landingPad;776#endif // __USING_SJLJ_EXCEPTIONS__ || __WASM_EXCEPTIONS__777            results.landingPad = landingPad;778            if (actionEntry == 0)779            {780                // Found a cleanup781                results.reason = (actions & _UA_SEARCH_PHASE) ? _URC_CONTINUE_UNWIND : _URC_HANDLER_FOUND;782                return;783            }784            // Convert 1-based byte offset into785            const uint8_t* action = actionTableStart + (actionEntry - 1);786            bool hasCleanup = false;787            // Scan action entries until you find a matching handler, cleanup, or the end of action list788            while (true)789            {790                const uint8_t* actionRecord = action;791                int64_t ttypeIndex = readSLEB128(&action);792                if (ttypeIndex > 0)793                {794                    // Found a catch, does it actually catch?795                    // First check for catch (...)796                    const __shim_type_info* catchType =797                        get_shim_type_info(static_cast<uint64_t>(ttypeIndex),798                                           classInfo, ttypeEncoding,799                                           native_exception, unwind_exception,800                                           base);801                    if (catchType == 0)802                    {803                        // Found catch (...) catches everything, including804                        // foreign exceptions. This is search phase, cleanup805                        // phase with foreign exception, or forced unwinding.806                        assert(actions & (_UA_SEARCH_PHASE | _UA_HANDLER_FRAME |807                                          _UA_FORCE_UNWIND));808                        results.ttypeIndex = ttypeIndex;809                        results.actionRecord = actionRecord;810                        results.adjustedPtr =811                            get_thrown_object_ptr(unwind_exception);812                        results.reason = _URC_HANDLER_FOUND;813                        return;814                    }815                    // Else this is a catch (T) clause and will never816                    //    catch a foreign exception817                    else if (native_exception)818                    {819                        __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1;820                        void* adjustedPtr = get_thrown_object_ptr(unwind_exception);821                        const __shim_type_info* excpType =822                            static_cast<const __shim_type_info*>(exception_header->exceptionType);823                        if (adjustedPtr == 0 || excpType == 0)824                        {825                            // Something very bad happened826                            call_terminate(native_exception, unwind_exception);827                        }828                        if (catchType->can_catch(excpType, adjustedPtr))829                        {830                            // Found a matching handler. This is either search831                            // phase or forced unwinding.832                            assert(actions &833                                   (_UA_SEARCH_PHASE | _UA_FORCE_UNWIND));834                            results.ttypeIndex = ttypeIndex;835                            results.actionRecord = actionRecord;836                            results.adjustedPtr = adjustedPtr;837                            results.reason = _URC_HANDLER_FOUND;838                            return;839                        }840                    }841                    // Scan next action ...842                }843                else if (ttypeIndex < 0)844                {845                    // Found an exception specification.846                    if (actions & _UA_FORCE_UNWIND) {847                        // Skip if forced unwinding.848                    } else if (native_exception) {849                        // Does the exception spec catch this native exception?850                        __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1;851                        void* adjustedPtr = get_thrown_object_ptr(unwind_exception);852                        const __shim_type_info* excpType =853                            static_cast<const __shim_type_info*>(exception_header->exceptionType);854                        if (adjustedPtr == 0 || excpType == 0)855                        {856                            // Something very bad happened857                            call_terminate(native_exception, unwind_exception);858                        }859                        if (exception_spec_can_catch(ttypeIndex, classInfo,860                                                     ttypeEncoding, excpType,861                                                     adjustedPtr,862                                                     unwind_exception, base))863                        {864                            // Native exception caught by exception865                            // specification.866                            assert(actions & _UA_SEARCH_PHASE);867                            results.ttypeIndex = ttypeIndex;868                            results.actionRecord = actionRecord;869                            results.adjustedPtr = adjustedPtr;870                            results.reason = _URC_HANDLER_FOUND;871                            return;872                        }873                    } else {874                        // foreign exception caught by exception spec875                        results.ttypeIndex = ttypeIndex;876                        results.actionRecord = actionRecord;877                        results.adjustedPtr =878                            get_thrown_object_ptr(unwind_exception);879                        results.reason = _URC_HANDLER_FOUND;880                        return;881                    }882                    // Scan next action ...883                } else {884                    hasCleanup = true;885                }886                const uint8_t* temp = action;887                int64_t actionOffset = readSLEB128(&temp);888                if (actionOffset == 0)889                {890                    // End of action list. If this is phase 2 and we have found891                    // a cleanup (ttypeIndex=0), return _URC_HANDLER_FOUND;892                    // otherwise return _URC_CONTINUE_UNWIND.893                    results.reason = hasCleanup && actions & _UA_CLEANUP_PHASE894                                         ? _URC_HANDLER_FOUND895                                         : _URC_CONTINUE_UNWIND;896                    return;897                }898                // Go to next action899                action += actionOffset;900            }  // there is no break out of this loop, only return901        }902#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__WASM_EXCEPTIONS__)903        else if (ipOffset < start)904        {905            // There is no call site for this ip906            // Something bad has happened.  We should never get here.907            // Possible stack corruption.908            call_terminate(native_exception, unwind_exception);909        }910#endif // !__USING_SJLJ_EXCEPTIONS__ && !__WASM_EXCEPTIONS__911    }  // there might be some tricky cases which break out of this loop912 913    // It is possible that no eh table entry specify how to handle914    // this exception. By spec, terminate it immediately.915    call_terminate(native_exception, unwind_exception);916}917 918// public API919 920/*921The personality function branches on actions like so:922 923_UA_SEARCH_PHASE924 925    If _UA_CLEANUP_PHASE or _UA_HANDLER_FRAME or _UA_FORCE_UNWIND there's926      an error from above, return _URC_FATAL_PHASE1_ERROR.927 928    Scan for anything that could stop unwinding:929 930       1.  A catch clause that will catch this exception931           (will never catch foreign).932       2.  A catch (...) (will always catch foreign).933       3.  An exception spec that will catch this exception934           (will always catch foreign).935    If a handler is found936        If not foreign937            Save state in header938        return _URC_HANDLER_FOUND939    Else a handler not found940        return _URC_CONTINUE_UNWIND941 942_UA_CLEANUP_PHASE943 944    If _UA_HANDLER_FRAME945        If _UA_FORCE_UNWIND946            How did this happen?  return _URC_FATAL_PHASE2_ERROR947        If foreign948            Do _UA_SEARCH_PHASE to recover state949        else950            Recover state from header951        Transfer control to landing pad.  return _URC_INSTALL_CONTEXT952 953    Else954 955        This branch handles both normal C++ non-catching handlers (cleanups)956          and forced unwinding.957        Scan for anything that can not stop unwinding:958 959            1.  A cleanup.960 961        If a cleanup is found962            transfer control to it. return _URC_INSTALL_CONTEXT963        Else a cleanup is not found: return _URC_CONTINUE_UNWIND964*/965 966#if !defined(_LIBCXXABI_ARM_EHABI)967 968// We use these helper functions to work around the behavior of casting between969// integers (even those that are authenticated) and authenticated pointers.970// Because the schemas being used are address discriminated we cannot use a971// trivial value union to coerce the types so instead we perform the re-signing972// manually.973using __cxa_catch_temp_type = decltype(__cxa_exception::catchTemp);974static inline void set_landing_pad(scan_results& results,975                                   const __cxa_catch_temp_type& source) {976#if __has_feature(ptrauth_calls)977  const uintptr_t sourceDiscriminator =978      ptrauth_blend_discriminator(&source, __ptrauth_cxxabi_catch_temp_disc);979  const uintptr_t targetDiscriminator =980      ptrauth_blend_discriminator(&results.landingPad,981                                  __ptrauth_scan_results_landingpad_disc);982  uintptr_t reauthenticatedLandingPad =983      (uintptr_t)ptrauth_auth_and_resign(*reinterpret_cast<void* const*>(&source),984                                         __ptrauth_cxxabi_catch_temp_key,985                                         sourceDiscriminator,986                                         __ptrauth_scan_results_landingpad_key,987                                         targetDiscriminator);988  memmove(reinterpret_cast<void *>(&results.landingPad),989          reinterpret_cast<void *>(&reauthenticatedLandingPad),990          sizeof(reauthenticatedLandingPad));991#else992  results.landingPad = reinterpret_cast<landing_pad_t>(source);993#endif994}995 996static inline void get_landing_pad(__cxa_catch_temp_type &dest,997                                   const scan_results &results) {998#if __has_feature(ptrauth_calls)999  const uintptr_t sourceDiscriminator =1000      ptrauth_blend_discriminator(&results.landingPad,1001                                  __ptrauth_scan_results_landingpad_disc);1002  const uintptr_t targetDiscriminator =1003      ptrauth_blend_discriminator(&dest, __ptrauth_cxxabi_catch_temp_disc);1004  uintptr_t reauthenticatedPointer =1005      (uintptr_t)ptrauth_auth_and_resign(*reinterpret_cast<void* const*>(&results.landingPad),1006                                         __ptrauth_scan_results_landingpad_key,1007                                         sourceDiscriminator,1008                                         __ptrauth_cxxabi_catch_temp_key,1009                                         targetDiscriminator);1010  memmove(reinterpret_cast<void *>(&dest),1011          reinterpret_cast<void *>(&reauthenticatedPointer),1012          sizeof(reauthenticatedPointer));1013#else1014  dest = reinterpret_cast<__cxa_catch_temp_type>(results.landingPad);1015#endif1016}1017 1018#ifdef __WASM_EXCEPTIONS__1019_Unwind_Reason_Code __gxx_personality_wasm01020#elif defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)1021static _Unwind_Reason_Code __gxx_personality_imp1022#else1023_LIBCXXABI_FUNC_VIS _Unwind_Reason_Code1024#ifdef __USING_SJLJ_EXCEPTIONS__1025__gxx_personality_sj01026#elif defined(__MVS__)1027__zos_cxx_personality_v21028#else1029__gxx_personality_v01030#endif1031#endif1032                    (int version, _Unwind_Action actions, uint64_t exceptionClass,1033                     _Unwind_Exception* unwind_exception, _Unwind_Context* context)1034{1035    if (version != 1 || unwind_exception == 0 || context == 0)1036        return _URC_FATAL_PHASE1_ERROR;1037 1038    bool native_exception = (exceptionClass     & get_vendor_and_language) ==1039                            (kOurExceptionClass & get_vendor_and_language);1040    scan_results results;1041    // Process a catch handler for a native exception first.1042    if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME) &&1043        native_exception) {1044        // Reload the results from the phase 1 cache.1045        __cxa_exception* exception_header =1046            (__cxa_exception*)(unwind_exception + 1) - 1;1047        results.ttypeIndex = exception_header->handlerSwitchValue;1048        results.actionRecord = exception_header->actionRecord;1049        results.languageSpecificData = exception_header->languageSpecificData;1050        set_landing_pad(results, exception_header->catchTemp);1051        results.adjustedPtr = exception_header->adjustedPtr;1052 1053        // Jump to the handler.1054        set_registers(unwind_exception, context, results);1055        // Cache base for calculating the address of ttype in1056        // __cxa_call_unexpected.1057        if (results.ttypeIndex < 0) {1058#if defined(_AIX)1059          exception_header->catchTemp = (void *)_Unwind_GetDataRelBase(context);1060#else1061          exception_header->catchTemp = 0;1062#endif1063        }1064        return _URC_INSTALL_CONTEXT;1065    }1066 1067    // In other cases we need to scan LSDA.1068    scan_eh_tab(results, actions, native_exception, unwind_exception, context);1069    if (results.reason == _URC_CONTINUE_UNWIND ||1070        results.reason == _URC_FATAL_PHASE1_ERROR)1071        return results.reason;1072 1073    if (actions & _UA_SEARCH_PHASE)1074    {1075        // Phase 1 search:  All we're looking for in phase 1 is a handler that1076        //   halts unwinding1077        assert(results.reason == _URC_HANDLER_FOUND);1078        if (native_exception) {1079            // For a native exception, cache the LSDA result.1080            __cxa_exception* exc = (__cxa_exception*)(unwind_exception + 1) - 1;1081            exc->handlerSwitchValue = static_cast<int>(results.ttypeIndex);1082            exc->actionRecord = results.actionRecord;1083            exc->languageSpecificData = results.languageSpecificData;1084            get_landing_pad(exc->catchTemp, results);1085            exc->adjustedPtr = results.adjustedPtr;1086#ifdef __WASM_EXCEPTIONS__1087            // Wasm only uses a single phase (_UA_SEARCH_PHASE), so save the1088            // results here.1089            set_registers(unwind_exception, context, results);1090#endif1091        }1092        return _URC_HANDLER_FOUND;1093    }1094 1095    assert(actions & _UA_CLEANUP_PHASE);1096    assert(results.reason == _URC_HANDLER_FOUND);1097    set_registers(unwind_exception, context, results);1098    // Cache base for calculating the address of ttype in __cxa_call_unexpected.1099    if (results.ttypeIndex < 0) {1100      __cxa_exception* exception_header =1101            (__cxa_exception*)(unwind_exception + 1) - 1;1102#if defined(_AIX)1103      exception_header->catchTemp = (void *)_Unwind_GetDataRelBase(context);1104#else1105      exception_header->catchTemp = 0;1106#endif1107    }1108    return _URC_INSTALL_CONTEXT;1109}1110 1111#if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)1112extern "C" _LIBCXXABI_FUNC_VIS EXCEPTION_DISPOSITION1113__gxx_personality_seh0(PEXCEPTION_RECORD ms_exc, void *this_frame,1114                       PCONTEXT ms_orig_context, PDISPATCHER_CONTEXT ms_disp)1115{1116  return _GCC_specific_handler(ms_exc, this_frame, ms_orig_context, ms_disp,1117                               __gxx_personality_imp);1118}1119#endif1120 1121#else1122 1123extern "C" _Unwind_Reason_Code __gnu_unwind_frame(_Unwind_Exception*,1124                                                  _Unwind_Context*);1125 1126// Helper function to unwind one frame.1127// ARM EHABI 7.3 and 7.4: If the personality function returns _URC_CONTINUE_UNWIND, the1128// personality routine should update the virtual register set (VRS) according to the1129// corresponding frame unwinding instructions (ARM EHABI 9.3.)1130static _Unwind_Reason_Code continue_unwind(_Unwind_Exception* unwind_exception,1131                                           _Unwind_Context* context)1132{1133  switch (__gnu_unwind_frame(unwind_exception, context)) {1134  case _URC_OK:1135    return _URC_CONTINUE_UNWIND;1136  case _URC_END_OF_STACK:1137    return _URC_END_OF_STACK;1138  default:1139    return _URC_FAILURE;1140  }1141}1142 1143// ARM register names1144#if !defined(_LIBUNWIND_VERSION)1145static const uint32_t REG_UCB = 12;  // Register to save _Unwind_Control_Block1146#endif1147static const uint32_t REG_SP = 13;1148 1149static void save_results_to_barrier_cache(_Unwind_Exception* unwind_exception,1150                                          const scan_results& results)1151{1152    unwind_exception->barrier_cache.bitpattern[0] = (uint32_t)results.adjustedPtr;1153    unwind_exception->barrier_cache.bitpattern[1] = (uint32_t)results.actionRecord;1154    unwind_exception->barrier_cache.bitpattern[2] = (uint32_t)results.languageSpecificData;1155    unwind_exception->barrier_cache.bitpattern[3] = (uint32_t)results.landingPad;1156    unwind_exception->barrier_cache.bitpattern[4] = (uint32_t)results.ttypeIndex;1157}1158 1159static void load_results_from_barrier_cache(scan_results& results,1160                                            const _Unwind_Exception* unwind_exception)1161{1162    results.adjustedPtr = (void*)unwind_exception->barrier_cache.bitpattern[0];1163    results.actionRecord = (const uint8_t*)unwind_exception->barrier_cache.bitpattern[1];1164    results.languageSpecificData = (const uint8_t*)unwind_exception->barrier_cache.bitpattern[2];1165    results.landingPad = (uintptr_t)unwind_exception->barrier_cache.bitpattern[3];1166    results.ttypeIndex = (int64_t)(int32_t)unwind_exception->barrier_cache.bitpattern[4];1167}1168 1169extern "C" _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code1170__gxx_personality_v0(_Unwind_State state,1171                     _Unwind_Exception* unwind_exception,1172                     _Unwind_Context* context)1173{1174    if (unwind_exception == 0 || context == 0)1175        return _URC_FATAL_PHASE1_ERROR;1176 1177    bool native_exception = __isOurExceptionClass(unwind_exception);1178 1179#if !defined(_LIBUNWIND_VERSION)1180    // Copy the address of _Unwind_Control_Block to r12 so that1181    // _Unwind_GetLanguageSpecificData() and _Unwind_GetRegionStart() can1182    // return correct address.1183    _Unwind_SetGR(context, REG_UCB, reinterpret_cast<uint32_t>(unwind_exception));1184#endif1185 1186    // Check the undocumented force unwinding behavior1187    bool is_force_unwinding = state & _US_FORCE_UNWIND;1188    state &= ~_US_FORCE_UNWIND;1189 1190    scan_results results;1191    switch (state) {1192    case _US_VIRTUAL_UNWIND_FRAME:1193        if (is_force_unwinding)1194            return continue_unwind(unwind_exception, context);1195 1196        // Phase 1 search:  All we're looking for in phase 1 is a handler that halts unwinding1197        scan_eh_tab(results, _UA_SEARCH_PHASE, native_exception, unwind_exception, context);1198        if (results.reason == _URC_HANDLER_FOUND)1199        {1200            unwind_exception->barrier_cache.sp = _Unwind_GetGR(context, REG_SP);1201            if (native_exception)1202                save_results_to_barrier_cache(unwind_exception, results);1203            return _URC_HANDLER_FOUND;1204        }1205        // Did not find the catch handler1206        if (results.reason == _URC_CONTINUE_UNWIND)1207            return continue_unwind(unwind_exception, context);1208        return results.reason;1209 1210    case _US_UNWIND_FRAME_STARTING:1211        // TODO: Support force unwinding in the phase 2 search.1212        // NOTE: In order to call the cleanup functions, _Unwind_ForcedUnwind()1213        // will call this personality function with (_US_FORCE_UNWIND |1214        // _US_UNWIND_FRAME_STARTING).1215 1216        // Phase 2 search1217        if (unwind_exception->barrier_cache.sp == _Unwind_GetGR(context, REG_SP))1218        {1219            // Found a catching handler in phase 11220            if (native_exception)1221            {1222                // Load the result from the native exception barrier cache.1223                load_results_from_barrier_cache(results, unwind_exception);1224                results.reason = _URC_HANDLER_FOUND;1225            }1226            else1227            {1228                // Search for the catching handler again for the foreign exception.1229                scan_eh_tab(results, static_cast<_Unwind_Action>(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME),1230                            native_exception, unwind_exception, context);1231                if (results.reason != _URC_HANDLER_FOUND)  // phase1 search should guarantee to find one1232                    call_terminate(native_exception, unwind_exception);1233            }1234 1235            // Install the context for the catching handler1236            set_registers(unwind_exception, context, results);1237            return _URC_INSTALL_CONTEXT;1238        }1239 1240        // Either we didn't do a phase 1 search (due to forced unwinding), or1241        // phase 1 reported no catching-handlers.1242        // Search for a (non-catching) cleanup1243        if (is_force_unwinding)1244          scan_eh_tab(1245              results,1246              static_cast<_Unwind_Action>(_UA_CLEANUP_PHASE | _UA_FORCE_UNWIND),1247              native_exception, unwind_exception, context);1248        else1249          scan_eh_tab(results, _UA_CLEANUP_PHASE, native_exception,1250                      unwind_exception, context);1251        if (results.reason == _URC_HANDLER_FOUND)1252        {1253            // Found a non-catching handler1254 1255            // ARM EHABI 8.4.2: Before we can jump to the cleanup handler, we have to setup some1256            // internal data structures, so that __cxa_end_cleanup() can get unwind_exception from1257            // __cxa_get_globals().1258            __cxa_begin_cleanup(unwind_exception);1259 1260            // Install the context for the cleanup handler1261            set_registers(unwind_exception, context, results);1262            return _URC_INSTALL_CONTEXT;1263        }1264 1265        // Did not find any handler1266        if (results.reason == _URC_CONTINUE_UNWIND)1267            return continue_unwind(unwind_exception, context);1268        return results.reason;1269 1270    case _US_UNWIND_FRAME_RESUME:1271        return continue_unwind(unwind_exception, context);1272    }1273 1274    // We were called improperly: neither a phase 1 or phase 2 search1275    return _URC_FATAL_PHASE1_ERROR;1276}1277#endif1278 1279 1280__attribute__((noreturn))1281_LIBCXXABI_FUNC_VIS void1282__cxa_call_unexpected(void* arg)1283{1284    _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(arg);1285    if (unwind_exception == 0)1286        call_terminate(false, unwind_exception);1287    __cxa_begin_catch(unwind_exception);1288    bool native_old_exception = __isOurExceptionClass(unwind_exception);1289    std::unexpected_handler u_handler;1290    std::terminate_handler t_handler;1291    __cxa_exception* old_exception_header = 0;1292    int64_t ttypeIndex;1293    const uint8_t* lsda;1294    uintptr_t base = 0;1295 1296    if (native_old_exception)1297    {1298        old_exception_header = (__cxa_exception*)(unwind_exception+1) - 1;1299        t_handler = old_exception_header->terminateHandler;1300        u_handler = old_exception_header->unexpectedHandler;1301        // If std::__unexpected(u_handler) rethrows the same exception,1302        //   these values get overwritten by the rethrow.  So save them now:1303#if defined(_LIBCXXABI_ARM_EHABI)1304        ttypeIndex = (int64_t)(int32_t)unwind_exception->barrier_cache.bitpattern[4];1305        lsda = (const uint8_t*)unwind_exception->barrier_cache.bitpattern[2];1306#else1307        ttypeIndex = old_exception_header->handlerSwitchValue;1308        lsda = old_exception_header->languageSpecificData;1309        base = (uintptr_t)old_exception_header->catchTemp;1310#endif1311    }1312    else1313    {1314        t_handler = std::get_terminate();1315        u_handler = std::get_unexpected();1316    }1317    try1318    {1319        std::__unexpected(u_handler);1320    }1321    catch (...)1322    {1323        // If the old exception is foreign, then all we can do is terminate.1324        //   We have no way to recover the needed old exception spec.  There's1325        //   no way to pass that information here.  And the personality routine1326        //   can't call us directly and do anything but terminate() if we throw1327        //   from here.1328        if (native_old_exception)1329        {1330            // Have:1331            //   old_exception_header->languageSpecificData1332            //   old_exception_header->actionRecord1333            //   old_exception_header->catchTemp, base for calculating ttype1334            // Need1335            //   const uint8_t* classInfo1336            //   uint8_t ttypeEncoding1337            uint8_t lpStartEncoding = *lsda++;1338            const uint8_t* lpStart =1339                (const uint8_t*)readEncodedPointer(&lsda, lpStartEncoding, base);1340            (void)lpStart;  // purposefully unused.  Just needed to increment lsda.1341            uint8_t ttypeEncoding = *lsda++;1342            if (ttypeEncoding == DW_EH_PE_omit)1343                std::__terminate(t_handler);1344            uintptr_t classInfoOffset = readULEB128(&lsda);1345            const uint8_t* classInfo = lsda + classInfoOffset;1346            // Is this new exception catchable by the exception spec at ttypeIndex?1347            // The answer is obviously yes if the new and old exceptions are the same exception1348            // If no1349            //    throw;1350            __cxa_eh_globals* globals = __cxa_get_globals_fast();1351            __cxa_exception* new_exception_header = globals->caughtExceptions;1352            if (new_exception_header == 0)1353                // This shouldn't be able to happen!1354                std::__terminate(t_handler);1355            bool native_new_exception = __isOurExceptionClass(&new_exception_header->unwindHeader);1356            void* adjustedPtr;1357            if (native_new_exception && (new_exception_header != old_exception_header))1358            {1359                const __shim_type_info* excpType =1360                    static_cast<const __shim_type_info*>(new_exception_header->exceptionType);1361                adjustedPtr =1362                    __getExceptionClass(&new_exception_header->unwindHeader) == kOurDependentExceptionClass ?1363                        ((__cxa_dependent_exception*)new_exception_header)->primaryException :1364                        new_exception_header + 1;1365                if (!exception_spec_can_catch(ttypeIndex, classInfo, ttypeEncoding,1366                                              excpType, adjustedPtr,1367                                              unwind_exception, base))1368                {1369                    // We need to __cxa_end_catch, but for the old exception,1370                    //   not the new one.  This is a little tricky ...1371                    // Disguise new_exception_header as a rethrown exception, but1372                    //   don't actually rethrow it.  This means you can temporarily1373                    //   end the catch clause enclosing new_exception_header without1374                    //   __cxa_end_catch destroying new_exception_header.1375                    new_exception_header->handlerCount = -new_exception_header->handlerCount;1376                    globals->uncaughtExceptions += 1;1377                    // Call __cxa_end_catch for new_exception_header1378                    __cxa_end_catch();1379                    // Call __cxa_end_catch for old_exception_header1380                    __cxa_end_catch();1381                    // Renter this catch clause with new_exception_header1382                    __cxa_begin_catch(&new_exception_header->unwindHeader);1383                    // Rethrow new_exception_header1384                    throw;1385                }1386            }1387            // Will a std::bad_exception be catchable by the exception spec at1388            //   ttypeIndex?1389            // If no1390            //    throw std::bad_exception();1391            const __shim_type_info* excpType =1392                static_cast<const __shim_type_info*>(&typeid(std::bad_exception));1393            std::bad_exception be;1394            adjustedPtr = &be;1395            if (!exception_spec_can_catch(ttypeIndex, classInfo, ttypeEncoding,1396                                          excpType, adjustedPtr,1397                                          unwind_exception, base))1398            {1399                // We need to __cxa_end_catch for both the old exception and the1400                //   new exception.  Technically we should do it in that order.1401                //   But it is expedient to do it in the opposite order:1402                // Call __cxa_end_catch for new_exception_header1403                __cxa_end_catch();1404                // Throw std::bad_exception will __cxa_end_catch for1405                //   old_exception_header1406                throw be;1407            }1408        }1409    }1410    std::__terminate(t_handler);1411}1412 1413#if defined(_AIX)1414// Personality routine for EH using the range table. Make it an alias of1415// __gxx_personality_v0().1416_LIBCXXABI_FUNC_VIS _Unwind_Reason_Code __xlcxx_personality_v1(1417    int version, _Unwind_Action actions, uint64_t exceptionClass,1418    _Unwind_Exception* unwind_exception, _Unwind_Context* context)1419    __attribute__((__alias__("__gxx_personality_v0")));1420#endif1421 1422} // extern "C"1423 1424}  // __cxxabiv11425 1426#if defined(_AIX)1427// Include implementation of the personality and helper functions for the1428// state table based EH used by IBM legacy compilers xlC and xlclang++ on AIX.1429#  include "aix_state_tab_eh.inc"1430#endif1431