cxa_exception.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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-exception
  6. //
  7. //
  8. // This file implements the "Exception Handling APIs"
  9. // https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef _CXA_EXCEPTION_H
  13. #define _CXA_EXCEPTION_H
  14. #include <exception> // for std::unexpected_handler and std::terminate_handler
  15. #include "cxxabi.h"
  16. #include "unwind.h"
  17. namespace __cxxabiv1 {
  18. #ifdef __USING_EMSCRIPTEN_EXCEPTIONS__
  19. struct _LIBCXXABI_HIDDEN __cxa_exception {
  20. size_t referenceCount;
  21. std::type_info *exceptionType;
  22. void (*exceptionDestructor)(void *);
  23. uint8_t caught;
  24. uint8_t rethrown;
  25. void *adjustedPtr;
  26. // Add padding to ensure that the size of __cxa_exception is a multiple of
  27. // the maximum useful alignment for the target machine. This ensures that
  28. // the thrown object that follows has that correct alignment.
  29. void *padding;
  30. };
  31. static_assert(sizeof(__cxa_exception) % alignof(max_align_t) == 0, "__cxa_exception must have a size that is multipl of max alignment");
  32. #else
  33. static const uint64_t kOurExceptionClass = 0x434C4E47432B2B00; // CLNGC++\0
  34. static const uint64_t kOurDependentExceptionClass = 0x434C4E47432B2B01; // CLNGC++\1
  35. static const uint64_t get_vendor_and_language = 0xFFFFFFFFFFFFFF00; // mask for CLNGC++
  36. _LIBCXXABI_HIDDEN uint64_t __getExceptionClass (const _Unwind_Exception*);
  37. _LIBCXXABI_HIDDEN void __setExceptionClass ( _Unwind_Exception*, uint64_t);
  38. _LIBCXXABI_HIDDEN bool __isOurExceptionClass(const _Unwind_Exception*);
  39. struct _LIBCXXABI_HIDDEN __cxa_exception {
  40. #if defined(__LP64__) || defined(_WIN64) || defined(_LIBCXXABI_ARM_EHABI)
  41. // Now _Unwind_Exception is marked with __attribute__((aligned)),
  42. // which implies __cxa_exception is also aligned. Insert padding
  43. // in the beginning of the struct, rather than before unwindHeader.
  44. void *reserve;
  45. // This is a new field to support C++ 0x exception_ptr.
  46. // For binary compatibility it is at the start of this
  47. // struct which is prepended to the object thrown in
  48. // __cxa_allocate_exception.
  49. size_t referenceCount;
  50. #endif
  51. // Manage the exception object itself.
  52. std::type_info *exceptionType;
  53. #ifdef __USING_WASM_EXCEPTIONS__
  54. // In wasm, destructors return their argument
  55. void *(*exceptionDestructor)(void *);
  56. #else
  57. void (*exceptionDestructor)(void *);
  58. #endif
  59. std::unexpected_handler unexpectedHandler;
  60. std::terminate_handler terminateHandler;
  61. __cxa_exception *nextException;
  62. int handlerCount;
  63. #if defined(_LIBCXXABI_ARM_EHABI)
  64. __cxa_exception* nextPropagatingException;
  65. int propagationCount;
  66. #else
  67. int handlerSwitchValue;
  68. const unsigned char *actionRecord;
  69. const unsigned char *languageSpecificData;
  70. void *catchTemp;
  71. void *adjustedPtr;
  72. #endif
  73. #if !defined(__LP64__) && !defined(_WIN64) && !defined(_LIBCXXABI_ARM_EHABI)
  74. // This is a new field to support C++ 0x exception_ptr.
  75. // For binary compatibility it is placed where the compiler
  76. // previously adding padded to 64-bit align unwindHeader.
  77. size_t referenceCount;
  78. #endif
  79. _Unwind_Exception unwindHeader;
  80. };
  81. // http://sourcery.mentor.com/archives/cxx-abi-dev/msg01924.html
  82. // The layout of this structure MUST match the layout of __cxa_exception, with
  83. // primaryException instead of referenceCount.
  84. struct _LIBCXXABI_HIDDEN __cxa_dependent_exception {
  85. #if defined(__LP64__) || defined(_WIN64) || defined(_LIBCXXABI_ARM_EHABI)
  86. void* reserve; // padding.
  87. void* primaryException;
  88. #endif
  89. std::type_info *exceptionType;
  90. void (*exceptionDestructor)(void *);
  91. std::unexpected_handler unexpectedHandler;
  92. std::terminate_handler terminateHandler;
  93. __cxa_exception *nextException;
  94. int handlerCount;
  95. #if defined(_LIBCXXABI_ARM_EHABI)
  96. __cxa_exception* nextPropagatingException;
  97. int propagationCount;
  98. #else
  99. int handlerSwitchValue;
  100. const unsigned char *actionRecord;
  101. const unsigned char *languageSpecificData;
  102. void * catchTemp;
  103. void *adjustedPtr;
  104. #endif
  105. #if !defined(__LP64__) && !defined(_WIN64) && !defined(_LIBCXXABI_ARM_EHABI)
  106. void* primaryException;
  107. #endif
  108. _Unwind_Exception unwindHeader;
  109. };
  110. // Verify the negative offsets of different fields.
  111. static_assert(sizeof(_Unwind_Exception) +
  112. offsetof(__cxa_exception, unwindHeader) ==
  113. sizeof(__cxa_exception),
  114. "unwindHeader has wrong negative offsets");
  115. static_assert(sizeof(_Unwind_Exception) +
  116. offsetof(__cxa_dependent_exception, unwindHeader) ==
  117. sizeof(__cxa_dependent_exception),
  118. "unwindHeader has wrong negative offsets");
  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");
  128. #elif defined(__LP64__) || defined(_WIN64)
  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. struct _LIBCXXABI_HIDDEN __cxa_eh_globals {
  148. __cxa_exception * caughtExceptions;
  149. unsigned int uncaughtExceptions;
  150. #if defined(_LIBCXXABI_ARM_EHABI)
  151. __cxa_exception* propagatingExceptions;
  152. #endif
  153. };
  154. extern "C" _LIBCXXABI_FUNC_VIS __cxa_eh_globals * __cxa_get_globals ();
  155. extern "C" _LIBCXXABI_FUNC_VIS __cxa_eh_globals * __cxa_get_globals_fast ();
  156. extern "C" _LIBCXXABI_FUNC_VIS void * __cxa_allocate_dependent_exception ();
  157. extern "C" _LIBCXXABI_FUNC_VIS void __cxa_free_dependent_exception (void * dependent_exception);
  158. #endif // !__USING_EMSCRIPTEN_EXCEPTIONS__
  159. } // namespace __cxxabiv1
  160. #endif // _CXA_EXCEPTION_H