Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/libcxxabi/src/cxa_exception.h
6174 views
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
20
namespace __cxxabiv1 {
21
22
#ifdef __EMSCRIPTEN_EXCEPTIONS__
23
24
struct _LIBCXXABI_HIDDEN __cxa_exception {
25
size_t referenceCount;
26
std::type_info *exceptionType;
27
// In wasm, destructors return 'this' as in ARM
28
void* (*exceptionDestructor)(void *);
29
uint8_t caught;
30
uint8_t rethrown;
31
void *adjustedPtr;
32
// Add padding to ensure that the size of __cxa_exception is a multiple of
33
// the maximum useful alignment for the target machine. This ensures that
34
// the thrown object that follows has that correct alignment.
35
void *padding;
36
};
37
38
static_assert(sizeof(__cxa_exception) % alignof(max_align_t) == 0, "__cxa_exception must have a size that is multiple of max alignment");
39
40
#else
41
42
static const uint64_t kOurExceptionClass = 0x434C4E47432B2B00; // CLNGC++\0
43
static const uint64_t kOurDependentExceptionClass = 0x434C4E47432B2B01; // CLNGC++\1
44
static const uint64_t get_vendor_and_language = 0xFFFFFFFFFFFFFF00; // mask for CLNGC++
45
46
_LIBCXXABI_HIDDEN uint64_t __getExceptionClass (const _Unwind_Exception*);
47
_LIBCXXABI_HIDDEN void __setExceptionClass ( _Unwind_Exception*, uint64_t);
48
_LIBCXXABI_HIDDEN bool __isOurExceptionClass(const _Unwind_Exception*);
49
50
struct _LIBCXXABI_HIDDEN __cxa_exception {
51
#if defined(__LP64__) || defined(_WIN64) || defined(_LIBCXXABI_ARM_EHABI)
52
// Now _Unwind_Exception is marked with __attribute__((aligned)),
53
// which implies __cxa_exception is also aligned. Insert padding
54
// in the beginning of the struct, rather than before unwindHeader.
55
void *reserve;
56
57
// This is a new field to support C++11 exception_ptr.
58
// For binary compatibility it is at the start of this
59
// struct which is prepended to the object thrown in
60
// __cxa_allocate_exception.
61
size_t referenceCount;
62
#endif
63
64
// Manage the exception object itself.
65
std::type_info *exceptionType;
66
#ifdef __wasm__
67
// In Wasm, a destructor returns its argument
68
void *(_LIBCXXABI_DTOR_FUNC *exceptionDestructor)(void *);
69
#else
70
void (_LIBCXXABI_DTOR_FUNC *exceptionDestructor)(void *);
71
#endif
72
std::unexpected_handler unexpectedHandler;
73
std::terminate_handler terminateHandler;
74
75
__cxa_exception *nextException;
76
77
int handlerCount;
78
79
#if defined(_LIBCXXABI_ARM_EHABI)
80
__cxa_exception* nextPropagatingException;
81
int propagationCount;
82
#else
83
int handlerSwitchValue;
84
const unsigned char *actionRecord;
85
const unsigned char *languageSpecificData;
86
void *catchTemp;
87
void *adjustedPtr;
88
#endif
89
90
#if !defined(__LP64__) && !defined(_WIN64) && !defined(_LIBCXXABI_ARM_EHABI)
91
// This is a new field to support C++11 exception_ptr.
92
// For binary compatibility it is placed where the compiler
93
// previously added padding to 64-bit align unwindHeader.
94
size_t referenceCount;
95
#endif
96
_Unwind_Exception unwindHeader;
97
};
98
99
// http://sourcery.mentor.com/archives/cxx-abi-dev/msg01924.html
100
// The layout of this structure MUST match the layout of __cxa_exception, with
101
// primaryException instead of referenceCount.
102
struct _LIBCXXABI_HIDDEN __cxa_dependent_exception {
103
#if defined(__LP64__) || defined(_WIN64) || defined(_LIBCXXABI_ARM_EHABI)
104
void* reserve; // padding.
105
void* primaryException;
106
#endif
107
108
std::type_info *exceptionType;
109
void (_LIBCXXABI_DTOR_FUNC *exceptionDestructor)(void *);
110
std::unexpected_handler unexpectedHandler;
111
std::terminate_handler terminateHandler;
112
113
__cxa_exception *nextException;
114
115
int handlerCount;
116
117
#if defined(_LIBCXXABI_ARM_EHABI)
118
__cxa_exception* nextPropagatingException;
119
int propagationCount;
120
#else
121
int handlerSwitchValue;
122
const unsigned char *actionRecord;
123
const unsigned char *languageSpecificData;
124
void * catchTemp;
125
void *adjustedPtr;
126
#endif
127
128
#if !defined(__LP64__) && !defined(_WIN64) && !defined(_LIBCXXABI_ARM_EHABI)
129
void* primaryException;
130
#endif
131
_Unwind_Exception unwindHeader;
132
};
133
134
// Verify the negative offsets of different fields.
135
static_assert(sizeof(_Unwind_Exception) +
136
offsetof(__cxa_exception, unwindHeader) ==
137
sizeof(__cxa_exception),
138
"unwindHeader has wrong negative offsets");
139
static_assert(sizeof(_Unwind_Exception) +
140
offsetof(__cxa_dependent_exception, unwindHeader) ==
141
sizeof(__cxa_dependent_exception),
142
"unwindHeader has wrong negative offsets");
143
144
#if defined(_LIBCXXABI_ARM_EHABI)
145
static_assert(offsetof(__cxa_exception, propagationCount) +
146
sizeof(_Unwind_Exception) + sizeof(void*) ==
147
sizeof(__cxa_exception),
148
"propagationCount has wrong negative offset");
149
static_assert(offsetof(__cxa_dependent_exception, propagationCount) +
150
sizeof(_Unwind_Exception) + sizeof(void*) ==
151
sizeof(__cxa_dependent_exception),
152
"propagationCount has wrong negative offset");
153
#elif defined(__LP64__) || defined(_WIN64)
154
static_assert(offsetof(__cxa_exception, adjustedPtr) +
155
sizeof(_Unwind_Exception) + sizeof(void*) ==
156
sizeof(__cxa_exception),
157
"adjustedPtr has wrong negative offset");
158
static_assert(offsetof(__cxa_dependent_exception, adjustedPtr) +
159
sizeof(_Unwind_Exception) + sizeof(void*) ==
160
sizeof(__cxa_dependent_exception),
161
"adjustedPtr has wrong negative offset");
162
#else
163
static_assert(offsetof(__cxa_exception, referenceCount) +
164
sizeof(_Unwind_Exception) + sizeof(void*) ==
165
sizeof(__cxa_exception),
166
"referenceCount has wrong negative offset");
167
static_assert(offsetof(__cxa_dependent_exception, primaryException) +
168
sizeof(_Unwind_Exception) + sizeof(void*) ==
169
sizeof(__cxa_dependent_exception),
170
"primaryException has wrong negative offset");
171
#endif
172
173
struct _LIBCXXABI_HIDDEN __cxa_eh_globals {
174
__cxa_exception * caughtExceptions;
175
unsigned int uncaughtExceptions;
176
#if defined(_LIBCXXABI_ARM_EHABI)
177
__cxa_exception* propagatingExceptions;
178
#endif
179
};
180
181
extern "C" _LIBCXXABI_FUNC_VIS __cxa_eh_globals * __cxa_get_globals ();
182
extern "C" _LIBCXXABI_FUNC_VIS __cxa_eh_globals * __cxa_get_globals_fast ();
183
184
extern "C" _LIBCXXABI_FUNC_VIS void * __cxa_allocate_dependent_exception ();
185
extern "C" _LIBCXXABI_FUNC_VIS void __cxa_free_dependent_exception (void * dependent_exception);
186
187
#endif // !__EMSCRIPTEN_EXCEPTIONS__
188
189
} // namespace __cxxabiv1
190
191
#endif // _CXA_EXCEPTION_H
192
193