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
13#ifndef _CXA_EXCEPTION_H
14#define _CXA_EXCEPTION_H
15
16#include <exception> // for std::unexpected_handler and std::terminate_handler
17#include "cxxabi.h"
18#include "unwind.h"
19
20namespace __cxxabiv1 {
21
22static const uint64_t kOurExceptionClass = 0x434C4E47432B2B00; // CLNGC++\0
23static const uint64_t kOurDependentExceptionClass = 0x434C4E47432B2B01; // CLNGC++\1
24static const uint64_t get_vendor_and_language = 0xFFFFFFFFFFFFFF00; // mask for CLNGC++
25
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*);
29
30struct _LIBCXXABI_HIDDEN __cxa_exception {
31#if defined(__LP64__) || defined(_WIN64) || defined(_LIBCXXABI_ARM_EHABI)
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
37 // This is a new field to support C++11 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.
41 size_t referenceCount;
42#endif
43
44 // Manage the exception object itself.
45 std::type_info *exceptionType;
46#ifdef __wasm__
47 // In Wasm, a destructor returns its argument
48 void *(_LIBCXXABI_DTOR_FUNC *exceptionDestructor)(void *);
49#else
50 void (_LIBCXXABI_DTOR_FUNC *__ptrauth_cxxabi_exception_destructor exceptionDestructor)(void *);
51#endif
52 std::unexpected_handler __ptrauth_cxxabi_unexpected_handler unexpectedHandler;
53 std::terminate_handler __ptrauth_cxxabi_terminate_handler terminateHandler;
54
55 __cxa_exception *nextException;
56
57 int handlerCount;
58
59#if defined(_LIBCXXABI_ARM_EHABI)
60 __cxa_exception* nextPropagatingException;
61 int propagationCount;
62#else
63 int handlerSwitchValue;
64 const unsigned char *__ptrauth_cxxabi_action_record actionRecord;
65 const unsigned char *__ptrauth_cxxabi_lsd languageSpecificData;
66 void *__ptrauth_cxxabi_catch_temp catchTemp;
67 void *__ptrauth_cxxabi_adjusted_ptr adjustedPtr;
68#endif
69
70#if !defined(__LP64__) && !defined(_WIN64) && !defined(_LIBCXXABI_ARM_EHABI)
71 // This is a new field to support C++11 exception_ptr.
72 // For binary compatibility it is placed where the compiler
73 // previously added padding to 64-bit align unwindHeader.
74 size_t referenceCount;
75#endif
76 _Unwind_Exception unwindHeader;
77};
78
79// http://sourcery.mentor.com/archives/cxx-abi-dev/msg01924.html
80// The layout of this structure MUST match the layout of __cxa_exception, with
81// primaryException instead of referenceCount.
82// The pointer authentication schemas specified here must also match those of
83// the corresponding members in __cxa_exception.
84struct _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
90 std::type_info *exceptionType;
91 void (_LIBCXXABI_DTOR_FUNC *__ptrauth_cxxabi_exception_destructor exceptionDestructor)(void *);
92 std::unexpected_handler __ptrauth_cxxabi_unexpected_handler unexpectedHandler;
93 std::terminate_handler __ptrauth_cxxabi_terminate_handler terminateHandler;
94
95 __cxa_exception *nextException;
96
97 int handlerCount;
98
99#if defined(_LIBCXXABI_ARM_EHABI)
100 __cxa_exception* nextPropagatingException;
101 int propagationCount;
102#else
103 int handlerSwitchValue;
104 const unsigned char *__ptrauth_cxxabi_action_record actionRecord;
105 const unsigned char *__ptrauth_cxxabi_lsd languageSpecificData;
106 void *__ptrauth_cxxabi_catch_temp catchTemp;
107 void *__ptrauth_cxxabi_adjusted_ptr adjustedPtr;
108#endif
109
110#if !defined(__LP64__) && !defined(_WIN64) && !defined(_LIBCXXABI_ARM_EHABI)
111 void* primaryException;
112#endif
113 _Unwind_Exception unwindHeader;
114};
115
116// Verify the negative offsets of different fields.
117static_assert(sizeof(_Unwind_Exception) +
118 offsetof(__cxa_exception, unwindHeader) ==
119 sizeof(__cxa_exception),
120 "unwindHeader has wrong negative offsets");
121static_assert(sizeof(_Unwind_Exception) +
122 offsetof(__cxa_dependent_exception, unwindHeader) ==
123 sizeof(__cxa_dependent_exception),
124 "unwindHeader has wrong negative offsets");
125
126#if defined(_LIBCXXABI_ARM_EHABI)
127static_assert(offsetof(__cxa_exception, propagationCount) +
128 sizeof(_Unwind_Exception) + sizeof(void*) ==
129 sizeof(__cxa_exception),
130 "propagationCount has wrong negative offset");
131static_assert(offsetof(__cxa_dependent_exception, propagationCount) +
132 sizeof(_Unwind_Exception) + sizeof(void*) ==
133 sizeof(__cxa_dependent_exception),
134 "propagationCount has wrong negative offset");
135#elif defined(__LP64__) || defined(_WIN64)
136static_assert(offsetof(__cxa_exception, adjustedPtr) +
137 sizeof(_Unwind_Exception) + sizeof(void*) ==
138 sizeof(__cxa_exception),
139 "adjustedPtr has wrong negative offset");
140static_assert(offsetof(__cxa_dependent_exception, adjustedPtr) +
141 sizeof(_Unwind_Exception) + sizeof(void*) ==
142 sizeof(__cxa_dependent_exception),
143 "adjustedPtr has wrong negative offset");
144#else
145static_assert(offsetof(__cxa_exception, referenceCount) +
146 sizeof(_Unwind_Exception) + sizeof(void*) ==
147 sizeof(__cxa_exception),
148 "referenceCount has wrong negative offset");
149static_assert(offsetof(__cxa_dependent_exception, primaryException) +
150 sizeof(_Unwind_Exception) + sizeof(void*) ==
151 sizeof(__cxa_dependent_exception),
152 "primaryException has wrong negative offset");
153#endif
154
155struct _LIBCXXABI_HIDDEN __cxa_eh_globals {
156 __cxa_exception * caughtExceptions;
157 unsigned int uncaughtExceptions;
158#if defined(_LIBCXXABI_ARM_EHABI)
159 __cxa_exception* propagatingExceptions;
160#endif
161};
162
163extern "C" _LIBCXXABI_FUNC_VIS __cxa_eh_globals * __cxa_get_globals ();
164extern "C" _LIBCXXABI_FUNC_VIS __cxa_eh_globals * __cxa_get_globals_fast ();
165
166extern "C" _LIBCXXABI_FUNC_VIS void * __cxa_allocate_dependent_exception ();
167extern "C" _LIBCXXABI_FUNC_VIS void __cxa_free_dependent_exception (void * dependent_exception);
168
169} // namespace __cxxabiv1
170
171#endif // _CXA_EXCEPTION_H
172