Nico Weber | 086048d | 2019-08-12 19:11:23 +0000 | [diff] [blame] | 1 | //===------------------------- cxa_exception.h ----------------------------===// |
Howard Hinnant | 987afbe | 2011-12-06 18:01:47 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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-exception |
Howard Hinnant | 987afbe | 2011-12-06 18:01:47 +0000 | [diff] [blame] | 6 | // |
Louis Dionne | 2cee0e2 | 2019-10-01 18:28:20 +0000 | [diff] [blame] | 7 | // |
Howard Hinnant | 987afbe | 2011-12-06 18:01:47 +0000 | [diff] [blame] | 8 | // This file implements the "Exception Handling APIs" |
Louis Dionne | 2b0da3d | 2019-04-11 16:37:07 +0000 | [diff] [blame] | 9 | // https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html |
Louis Dionne | 2cee0e2 | 2019-10-01 18:28:20 +0000 | [diff] [blame] | 10 | // |
Howard Hinnant | 987afbe | 2011-12-06 18:01:47 +0000 | [diff] [blame] | 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Howard Hinnant | 4ac72dd | 2012-03-19 16:20:34 +0000 | [diff] [blame] | 13 | #ifndef _CXA_EXCEPTION_H |
| 14 | #define _CXA_EXCEPTION_H |
| 15 | |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 16 | #include <exception> // for std::unexpected_handler and std::terminate_handler |
Mehdi Amini | 5a40358 | 2017-04-04 05:38:38 +0000 | [diff] [blame] | 17 | #include "cxxabi.h" |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 18 | #include "unwind.h" |
| 19 | |
| 20 | namespace __cxxabiv1 { |
| 21 | |
Howard Hinnant | 6830b2a | 2012-01-24 18:15:20 +0000 | [diff] [blame] | 22 | static const uint64_t kOurExceptionClass = 0x434C4E47432B2B00; // CLNGC++\0 |
| 23 | static const uint64_t kOurDependentExceptionClass = 0x434C4E47432B2B01; // CLNGC++\1 |
Dan Albert | b98c20c | 2015-02-05 23:48:06 +0000 | [diff] [blame] | 24 | static const uint64_t get_vendor_and_language = 0xFFFFFFFFFFFFFF00; // mask for CLNGC++ |
| 25 | |
Louis Dionne | 6f9459f | 2019-10-03 14:24:53 +0000 | [diff] [blame] | 26 | _LIBCXXABI_HIDDEN uint64_t __getExceptionClass (const _Unwind_Exception*); |
| 27 | _LIBCXXABI_HIDDEN void __setExceptionClass ( _Unwind_Exception*, uint64_t); |
| 28 | _LIBCXXABI_HIDDEN bool __isOurExceptionClass(const _Unwind_Exception*); |
Marshall Clow | 611a55a | 2018-10-10 16:18:37 +0000 | [diff] [blame] | 29 | |
Shoaib Meenai | fe989a9 | 2017-03-01 03:55:57 +0000 | [diff] [blame] | 30 | struct _LIBCXXABI_HIDDEN __cxa_exception { |
Martin Storsjö | 09dc884 | 2020-02-01 13:32:57 +0200 | [diff] [blame] | 31 | #if defined(__LP64__) || defined(_WIN64) || defined(_LIBCXXABI_ARM_EHABI) |
Steven Wu | f2a4360 | 2020-01-30 10:02:23 -0800 | [diff] [blame] | 32 | // Now _Unwind_Exception is marked with __attribute__((aligned)), |
| 33 | // which implies __cxa_exception is also aligned. Insert padding |
| 34 | // in the beginning of the struct, rather than before unwindHeader. |
| 35 | void *reserve; |
| 36 | |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 37 | // This is a new field to support C++ 0x exception_ptr. |
| 38 | // For binary compatibility it is at the start of this |
| 39 | // struct which is prepended to the object thrown in |
| 40 | // __cxa_allocate_exception. |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 41 | size_t referenceCount; |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 42 | #endif |
Dan Albert | b98c20c | 2015-02-05 23:48:06 +0000 | [diff] [blame] | 43 | |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 44 | // Manage the exception object itself. |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 45 | std::type_info *exceptionType; |
Dan Albert | b98c20c | 2015-02-05 23:48:06 +0000 | [diff] [blame] | 46 | void (*exceptionDestructor)(void *); |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 47 | std::unexpected_handler unexpectedHandler; |
| 48 | std::terminate_handler terminateHandler; |
| 49 | |
| 50 | __cxa_exception *nextException; |
| 51 | |
| 52 | int handlerCount; |
| 53 | |
Ranjeet Singh | ef6e672 | 2017-03-01 11:42:01 +0000 | [diff] [blame] | 54 | #if defined(_LIBCXXABI_ARM_EHABI) |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 55 | __cxa_exception* nextPropagatingException; |
| 56 | int propagationCount; |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 57 | #else |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 58 | int handlerSwitchValue; |
| 59 | const unsigned char *actionRecord; |
| 60 | const unsigned char *languageSpecificData; |
| 61 | void *catchTemp; |
| 62 | void *adjustedPtr; |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
Martin Storsjö | 09dc884 | 2020-02-01 13:32:57 +0200 | [diff] [blame] | 65 | #if !defined(__LP64__) && !defined(_WIN64) && !defined(_LIBCXXABI_ARM_EHABI) |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 66 | // This is a new field to support C++ 0x exception_ptr. |
| 67 | // For binary compatibility it is placed where the compiler |
| 68 | // previously adding padded to 64-bit align unwindHeader. |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 69 | size_t referenceCount; |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 70 | #endif |
Akira Hatanaka | c57477b2 | 2017-05-13 06:28:17 +0000 | [diff] [blame] | 71 | _Unwind_Exception unwindHeader; |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 72 | }; |
Howard Hinnant | 6ccae15 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 73 | |
| 74 | // http://sourcery.mentor.com/archives/cxx-abi-dev/msg01924.html |
Nico Weber | ae54387 | 2014-06-25 23:52:07 +0000 | [diff] [blame] | 75 | // The layout of this structure MUST match the layout of __cxa_exception, with |
| 76 | // primaryException instead of referenceCount. |
Shoaib Meenai | fe989a9 | 2017-03-01 03:55:57 +0000 | [diff] [blame] | 77 | struct _LIBCXXABI_HIDDEN __cxa_dependent_exception { |
Martin Storsjö | 09dc884 | 2020-02-01 13:32:57 +0200 | [diff] [blame] | 78 | #if defined(__LP64__) || defined(_WIN64) || defined(_LIBCXXABI_ARM_EHABI) |
Steven Wu | f2a4360 | 2020-01-30 10:02:23 -0800 | [diff] [blame] | 79 | void* reserve; // padding. |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 80 | void* primaryException; |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 81 | #endif |
Dan Albert | b98c20c | 2015-02-05 23:48:06 +0000 | [diff] [blame] | 82 | |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 83 | std::type_info *exceptionType; |
Dan Albert | b98c20c | 2015-02-05 23:48:06 +0000 | [diff] [blame] | 84 | void (*exceptionDestructor)(void *); |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 85 | std::unexpected_handler unexpectedHandler; |
| 86 | std::terminate_handler terminateHandler; |
| 87 | |
| 88 | __cxa_exception *nextException; |
| 89 | |
| 90 | int handlerCount; |
Dan Albert | b98c20c | 2015-02-05 23:48:06 +0000 | [diff] [blame] | 91 | |
Ranjeet Singh | ef6e672 | 2017-03-01 11:42:01 +0000 | [diff] [blame] | 92 | #if defined(_LIBCXXABI_ARM_EHABI) |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 93 | __cxa_exception* nextPropagatingException; |
| 94 | int propagationCount; |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 95 | #else |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 96 | int handlerSwitchValue; |
| 97 | const unsigned char *actionRecord; |
| 98 | const unsigned char *languageSpecificData; |
| 99 | void * catchTemp; |
| 100 | void *adjustedPtr; |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 101 | #endif |
Dan Albert | b98c20c | 2015-02-05 23:48:06 +0000 | [diff] [blame] | 102 | |
Martin Storsjö | 09dc884 | 2020-02-01 13:32:57 +0200 | [diff] [blame] | 103 | #if !defined(__LP64__) && !defined(_WIN64) && !defined(_LIBCXXABI_ARM_EHABI) |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 104 | void* primaryException; |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 105 | #endif |
Akira Hatanaka | c57477b2 | 2017-05-13 06:28:17 +0000 | [diff] [blame] | 106 | _Unwind_Exception unwindHeader; |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
Steven Wu | f2a4360 | 2020-01-30 10:02:23 -0800 | [diff] [blame] | 109 | // Verify the negative offsets of different fields. |
| 110 | static_assert(sizeof(_Unwind_Exception) + |
| 111 | offsetof(__cxa_exception, unwindHeader) == |
| 112 | sizeof(__cxa_exception), |
| 113 | "unwindHeader has wrong negative offsets"); |
| 114 | static_assert(sizeof(_Unwind_Exception) + |
| 115 | offsetof(__cxa_dependent_exception, unwindHeader) == |
| 116 | sizeof(__cxa_dependent_exception), |
| 117 | "unwindHeader has wrong negative offsets"); |
| 118 | |
| 119 | #if defined(_LIBCXXABI_ARM_EHABI) |
| 120 | static_assert(offsetof(__cxa_exception, propagationCount) + |
| 121 | sizeof(_Unwind_Exception) + sizeof(void*) == |
| 122 | sizeof(__cxa_exception), |
| 123 | "propagationCount has wrong negative offset"); |
| 124 | static_assert(offsetof(__cxa_dependent_exception, propagationCount) + |
| 125 | sizeof(_Unwind_Exception) + sizeof(void*) == |
| 126 | sizeof(__cxa_dependent_exception), |
| 127 | "propagationCount has wrong negative offset"); |
Martin Storsjö | 09dc884 | 2020-02-01 13:32:57 +0200 | [diff] [blame] | 128 | #elif defined(__LP64__) || defined(_WIN64) |
Steven Wu | f2a4360 | 2020-01-30 10:02:23 -0800 | [diff] [blame] | 129 | static_assert(offsetof(__cxa_exception, adjustedPtr) + |
| 130 | sizeof(_Unwind_Exception) + sizeof(void*) == |
| 131 | sizeof(__cxa_exception), |
| 132 | "adjustedPtr has wrong negative offset"); |
| 133 | static_assert(offsetof(__cxa_dependent_exception, adjustedPtr) + |
| 134 | sizeof(_Unwind_Exception) + sizeof(void*) == |
| 135 | sizeof(__cxa_dependent_exception), |
| 136 | "adjustedPtr has wrong negative offset"); |
| 137 | #else |
| 138 | static_assert(offsetof(__cxa_exception, referenceCount) + |
| 139 | sizeof(_Unwind_Exception) + sizeof(void*) == |
| 140 | sizeof(__cxa_exception), |
| 141 | "referenceCount has wrong negative offset"); |
| 142 | static_assert(offsetof(__cxa_dependent_exception, primaryException) + |
| 143 | sizeof(_Unwind_Exception) + sizeof(void*) == |
| 144 | sizeof(__cxa_dependent_exception), |
| 145 | "primaryException has wrong negative offset"); |
| 146 | #endif |
| 147 | |
Shoaib Meenai | fe989a9 | 2017-03-01 03:55:57 +0000 | [diff] [blame] | 148 | struct _LIBCXXABI_HIDDEN __cxa_eh_globals { |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 149 | __cxa_exception * caughtExceptions; |
| 150 | unsigned int uncaughtExceptions; |
Ranjeet Singh | ef6e672 | 2017-03-01 11:42:01 +0000 | [diff] [blame] | 151 | #if defined(_LIBCXXABI_ARM_EHABI) |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 152 | __cxa_exception* propagatingExceptions; |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 153 | #endif |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 154 | }; |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 155 | |
Shoaib Meenai | fe989a9 | 2017-03-01 03:55:57 +0000 | [diff] [blame] | 156 | extern "C" _LIBCXXABI_FUNC_VIS __cxa_eh_globals * __cxa_get_globals (); |
| 157 | extern "C" _LIBCXXABI_FUNC_VIS __cxa_eh_globals * __cxa_get_globals_fast (); |
Howard Hinnant | eaa65af | 2012-02-02 20:47:28 +0000 | [diff] [blame] | 158 | |
Shoaib Meenai | fe989a9 | 2017-03-01 03:55:57 +0000 | [diff] [blame] | 159 | extern "C" _LIBCXXABI_FUNC_VIS void * __cxa_allocate_dependent_exception (); |
| 160 | extern "C" _LIBCXXABI_FUNC_VIS void __cxa_free_dependent_exception (void * dependent_exception); |
Logan Chien | aacc1c7 | 2014-04-12 11:56:41 +0000 | [diff] [blame] | 161 | |
| 162 | } // namespace __cxxabiv1 |
Howard Hinnant | 4ac72dd | 2012-03-19 16:20:34 +0000 | [diff] [blame] | 163 | |
| 164 | #endif // _CXA_EXCEPTION_H |