/src/llvm-project-16.0.6.src/libcxxabi/src/cxa_exception.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #include "cxxabi.h" |
14 | | |
15 | | #include <exception> // for std::terminate |
16 | | #include <string.h> // for memset |
17 | | #include "cxa_exception.h" |
18 | | #include "cxa_handlers.h" |
19 | | #include "fallback_malloc.h" |
20 | | #include "include/atomic_support.h" // from libc++ |
21 | | |
22 | | #if __has_feature(address_sanitizer) |
23 | | #include <sanitizer/asan_interface.h> |
24 | | #endif |
25 | | |
26 | | // +---------------------------+-----------------------------+---------------+ |
27 | | // | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object | |
28 | | // +---------------------------+-----------------------------+---------------+ |
29 | | // ^ |
30 | | // | |
31 | | // +-------------------------------------------------------+ |
32 | | // | |
33 | | // +---------------------------+-----------------------------+ |
34 | | // | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 | |
35 | | // +---------------------------+-----------------------------+ |
36 | | |
37 | | namespace __cxxabiv1 { |
38 | | |
39 | | // Utility routines |
40 | | static |
41 | | inline |
42 | | __cxa_exception* |
43 | | cxa_exception_from_thrown_object(void* thrown_object) |
44 | 7.48M | { |
45 | 7.48M | return static_cast<__cxa_exception*>(thrown_object) - 1; |
46 | 7.48M | } |
47 | | |
48 | | // Note: This is never called when exception_header is masquerading as a |
49 | | // __cxa_dependent_exception. |
50 | | static |
51 | | inline |
52 | | void* |
53 | | thrown_object_from_cxa_exception(__cxa_exception* exception_header) |
54 | 3.73M | { |
55 | 3.73M | return static_cast<void*>(exception_header + 1); |
56 | 3.73M | } |
57 | | |
58 | | // Get the exception object from the unwind pointer. |
59 | | // Relies on the structure layout, where the unwind pointer is right in |
60 | | // front of the user's exception object |
61 | | static |
62 | | inline |
63 | | __cxa_exception* |
64 | | cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception) |
65 | 1.88M | { |
66 | 1.88M | return cxa_exception_from_thrown_object(unwind_exception + 1 ); |
67 | 1.88M | } |
68 | | |
69 | | // Round s up to next multiple of a. |
70 | | static inline |
71 | 1.86M | size_t aligned_allocation_size(size_t s, size_t a) { |
72 | 1.86M | return (s + a - 1) & ~(a - 1); |
73 | 1.86M | } |
74 | | |
75 | | static inline |
76 | 1.86M | size_t cxa_exception_size_from_exception_thrown_size(size_t size) { |
77 | 1.86M | return aligned_allocation_size(size + sizeof (__cxa_exception), |
78 | 1.86M | alignof(__cxa_exception)); |
79 | 1.86M | } |
80 | | |
81 | 1.86M | void __setExceptionClass(_Unwind_Exception* unwind_exception, uint64_t newValue) { |
82 | 1.86M | ::memcpy(&unwind_exception->exception_class, &newValue, sizeof(newValue)); |
83 | 1.86M | } |
84 | | |
85 | | |
86 | 1.86M | static void setOurExceptionClass(_Unwind_Exception* unwind_exception) { |
87 | 1.86M | __setExceptionClass(unwind_exception, kOurExceptionClass); |
88 | 1.86M | } |
89 | | |
90 | 0 | static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) { |
91 | 0 | __setExceptionClass(unwind_exception, kOurDependentExceptionClass); |
92 | 0 | } |
93 | | |
94 | | // Is it one of ours? |
95 | 7.92M | uint64_t __getExceptionClass(const _Unwind_Exception* unwind_exception) { |
96 | | // On x86 and some ARM unwinders, unwind_exception->exception_class is |
97 | | // a uint64_t. On other ARM unwinders, it is a char[8]. |
98 | | // See: http://infocenter.arm.com/help/topic/com.arm.doc.ihi0038b/IHI0038B_ehabi.pdf |
99 | | // So we just copy it into a uint64_t to be sure. |
100 | 7.92M | uint64_t exClass; |
101 | 7.92M | ::memcpy(&exClass, &unwind_exception->exception_class, sizeof(exClass)); |
102 | 7.92M | return exClass; |
103 | 7.92M | } |
104 | | |
105 | 3.78M | bool __isOurExceptionClass(const _Unwind_Exception* unwind_exception) { |
106 | 3.78M | return (__getExceptionClass(unwind_exception) & get_vendor_and_language) == |
107 | 3.78M | (kOurExceptionClass & get_vendor_and_language); |
108 | 3.78M | } |
109 | | |
110 | 1.87M | static bool isDependentException(_Unwind_Exception* unwind_exception) { |
111 | 1.87M | return (__getExceptionClass(unwind_exception) & 0xFF) == 0x01; |
112 | 1.87M | } |
113 | | |
114 | | // This does not need to be atomic |
115 | 15.9k | static inline int incrementHandlerCount(__cxa_exception *exception) { |
116 | 15.9k | return ++exception->handlerCount; |
117 | 15.9k | } |
118 | | |
119 | | // This does not need to be atomic |
120 | 1.87M | static inline int decrementHandlerCount(__cxa_exception *exception) { |
121 | 1.87M | return --exception->handlerCount; |
122 | 1.87M | } |
123 | | |
124 | | /* |
125 | | If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler |
126 | | stored in exc is called. Otherwise the exceptionDestructor stored in |
127 | | exc is called, and then the memory for the exception is deallocated. |
128 | | |
129 | | This is never called for a __cxa_dependent_exception. |
130 | | */ |
131 | | static |
132 | | void |
133 | | exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception) |
134 | 0 | { |
135 | 0 | __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception); |
136 | 0 | if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason) |
137 | 0 | std::__terminate(exception_header->terminateHandler); |
138 | | // Just in case there exists a dependent exception that is pointing to this, |
139 | | // check the reference count and only destroy this if that count goes to zero. |
140 | 0 | __cxa_decrement_exception_refcount(unwind_exception + 1); |
141 | 0 | } |
142 | | |
143 | 0 | static _LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) { |
144 | | // Section 2.5.3 says: |
145 | | // * For purposes of this ABI, several things are considered exception handlers: |
146 | | // ** A terminate() call due to a throw. |
147 | | // and |
148 | | // * Upon entry, Following initialization of the catch parameter, |
149 | | // a handler must call: |
150 | | // * void *__cxa_begin_catch(void *exceptionObject ); |
151 | 0 | (void) __cxa_begin_catch(&exception_header->unwindHeader); |
152 | 0 | std::__terminate(exception_header->terminateHandler); |
153 | 0 | } |
154 | | |
155 | | // Return the offset of the __cxa_exception header from the start of the |
156 | | // allocated buffer. If __cxa_exception's alignment is smaller than the maximum |
157 | | // useful alignment for the target machine, padding has to be inserted before |
158 | | // the header to ensure the thrown object that follows the header is |
159 | | // sufficiently aligned. This happens if _Unwind_exception isn't double-word |
160 | | // aligned (on Darwin, for example). |
161 | 3.73M | static size_t get_cxa_exception_offset() { |
162 | 3.73M | struct S { |
163 | 3.73M | } __attribute__((aligned)); |
164 | | |
165 | | // Compute the maximum alignment for the target machine. |
166 | 3.73M | constexpr size_t alignment = alignof(S); |
167 | 3.73M | constexpr size_t excp_size = sizeof(__cxa_exception); |
168 | 3.73M | constexpr size_t aligned_size = |
169 | 3.73M | (excp_size + alignment - 1) / alignment * alignment; |
170 | 3.73M | constexpr size_t offset = aligned_size - excp_size; |
171 | 3.73M | static_assert((offset == 0 || alignof(_Unwind_Exception) < alignment), |
172 | 3.73M | "offset is non-zero only if _Unwind_Exception isn't aligned"); |
173 | 3.73M | return offset; |
174 | 3.73M | } |
175 | | |
176 | | extern "C" { |
177 | | |
178 | | // Allocate a __cxa_exception object, and zero-fill it. |
179 | | // Reserve "thrown_size" bytes on the end for the user's exception |
180 | | // object. Zero-fill the object. If memory can't be allocated, call |
181 | | // std::terminate. Return a pointer to the memory to be used for the |
182 | | // user's exception object. |
183 | 1.86M | void *__cxa_allocate_exception(size_t thrown_size) throw() { |
184 | 1.86M | size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size); |
185 | | |
186 | | // Allocate extra space before the __cxa_exception header to ensure the |
187 | | // start of the thrown object is sufficiently aligned. |
188 | 1.86M | size_t header_offset = get_cxa_exception_offset(); |
189 | 1.86M | char *raw_buffer = |
190 | 1.86M | (char *)__aligned_malloc_with_fallback(header_offset + actual_size); |
191 | 1.86M | if (NULL == raw_buffer) |
192 | 0 | std::terminate(); |
193 | 1.86M | __cxa_exception *exception_header = |
194 | 1.86M | static_cast<__cxa_exception *>((void *)(raw_buffer + header_offset)); |
195 | 1.86M | ::memset(exception_header, 0, actual_size); |
196 | 1.86M | return thrown_object_from_cxa_exception(exception_header); |
197 | 1.86M | } |
198 | | |
199 | | |
200 | | // Free a __cxa_exception object allocated with __cxa_allocate_exception. |
201 | 1.87M | void __cxa_free_exception(void *thrown_object) throw() { |
202 | | // Compute the size of the padding before the header. |
203 | 1.87M | size_t header_offset = get_cxa_exception_offset(); |
204 | 1.87M | char *raw_buffer = |
205 | 1.87M | ((char *)cxa_exception_from_thrown_object(thrown_object)) - header_offset; |
206 | 1.87M | __aligned_free_with_fallback((void *)raw_buffer); |
207 | 1.87M | } |
208 | | |
209 | | |
210 | | // This function shall allocate a __cxa_dependent_exception and |
211 | | // return a pointer to it. (Really to the object, not past its' end). |
212 | | // Otherwise, it will work like __cxa_allocate_exception. |
213 | 0 | void * __cxa_allocate_dependent_exception () { |
214 | 0 | size_t actual_size = sizeof(__cxa_dependent_exception); |
215 | 0 | void *ptr = __aligned_malloc_with_fallback(actual_size); |
216 | 0 | if (NULL == ptr) |
217 | 0 | std::terminate(); |
218 | 0 | ::memset(ptr, 0, actual_size); |
219 | 0 | return ptr; |
220 | 0 | } |
221 | | |
222 | | |
223 | | // This function shall free a dependent_exception. |
224 | | // It does not affect the reference count of the primary exception. |
225 | 0 | void __cxa_free_dependent_exception (void * dependent_exception) { |
226 | 0 | __aligned_free_with_fallback(dependent_exception); |
227 | 0 | } |
228 | | |
229 | | |
230 | | // 2.4.3 Throwing the Exception Object |
231 | | /* |
232 | | After constructing the exception object with the throw argument value, |
233 | | the generated code calls the __cxa_throw runtime library routine. This |
234 | | routine never returns. |
235 | | |
236 | | The __cxa_throw routine will do the following: |
237 | | |
238 | | * Obtain the __cxa_exception header from the thrown exception object address, |
239 | | which can be computed as follows: |
240 | | __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1); |
241 | | * Save the current unexpected_handler and terminate_handler in the __cxa_exception header. |
242 | | * Save the tinfo and dest arguments in the __cxa_exception header. |
243 | | * Set the exception_class field in the unwind header. This is a 64-bit value |
244 | | representing the ASCII string "XXXXC++\0", where "XXXX" is a |
245 | | vendor-dependent string. That is, for implementations conforming to this |
246 | | ABI, the low-order 4 bytes of this 64-bit value will be "C++\0". |
247 | | * Increment the uncaught_exception flag. |
248 | | * Call _Unwind_RaiseException in the system unwind library, Its argument is the |
249 | | pointer to the thrown exception, which __cxa_throw itself received as an argument. |
250 | | __Unwind_RaiseException begins the process of stack unwinding, described |
251 | | in Section 2.5. In special cases, such as an inability to find a |
252 | | handler, _Unwind_RaiseException may return. In that case, __cxa_throw |
253 | | will call terminate, assuming that there was no handler for the |
254 | | exception. |
255 | | */ |
256 | | void |
257 | 1.86M | __cxa_throw(void *thrown_object, std::type_info *tinfo, void (_LIBCXXABI_DTOR_FUNC *dest)(void *)) { |
258 | 1.86M | __cxa_eh_globals *globals = __cxa_get_globals(); |
259 | 1.86M | __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); |
260 | | |
261 | 1.86M | exception_header->unexpectedHandler = std::get_unexpected(); |
262 | 1.86M | exception_header->terminateHandler = std::get_terminate(); |
263 | 1.86M | exception_header->exceptionType = tinfo; |
264 | 1.86M | exception_header->exceptionDestructor = dest; |
265 | 1.86M | setOurExceptionClass(&exception_header->unwindHeader); |
266 | 1.86M | exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety. |
267 | 1.86M | globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local |
268 | | |
269 | 1.86M | exception_header->unwindHeader.exception_cleanup = exception_cleanup_func; |
270 | | |
271 | | #if __has_feature(address_sanitizer) |
272 | | // Inform the ASan runtime that now might be a good time to clean stuff up. |
273 | | __asan_handle_no_return(); |
274 | | #endif |
275 | | |
276 | | #ifdef __USING_SJLJ_EXCEPTIONS__ |
277 | | _Unwind_SjLj_RaiseException(&exception_header->unwindHeader); |
278 | | #else |
279 | 1.86M | _Unwind_RaiseException(&exception_header->unwindHeader); |
280 | 1.86M | #endif |
281 | | // This only happens when there is no handler, or some unexpected unwinding |
282 | | // error happens. |
283 | 1.86M | failed_throw(exception_header); |
284 | 1.86M | } |
285 | | |
286 | | |
287 | | // 2.5.3 Exception Handlers |
288 | | /* |
289 | | The adjusted pointer is computed by the personality routine during phase 1 |
290 | | and saved in the exception header (either __cxa_exception or |
291 | | __cxa_dependent_exception). |
292 | | |
293 | | Requires: exception is native |
294 | | */ |
295 | 0 | void *__cxa_get_exception_ptr(void *unwind_exception) throw() { |
296 | | #if defined(_LIBCXXABI_ARM_EHABI) |
297 | | return reinterpret_cast<void*>( |
298 | | static_cast<_Unwind_Control_Block*>(unwind_exception)->barrier_cache.bitpattern[0]); |
299 | | #else |
300 | 0 | return cxa_exception_from_exception_unwind_exception( |
301 | 0 | static_cast<_Unwind_Exception*>(unwind_exception))->adjustedPtr; |
302 | 0 | #endif |
303 | 0 | } |
304 | | |
305 | | #if defined(_LIBCXXABI_ARM_EHABI) |
306 | | /* |
307 | | The routine to be called before the cleanup. This will save __cxa_exception in |
308 | | __cxa_eh_globals, so that __cxa_end_cleanup() can recover later. |
309 | | */ |
310 | | bool __cxa_begin_cleanup(void *unwind_arg) throw() { |
311 | | _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg); |
312 | | __cxa_eh_globals* globals = __cxa_get_globals(); |
313 | | __cxa_exception* exception_header = |
314 | | cxa_exception_from_exception_unwind_exception(unwind_exception); |
315 | | |
316 | | if (__isOurExceptionClass(unwind_exception)) |
317 | | { |
318 | | if (0 == exception_header->propagationCount) |
319 | | { |
320 | | exception_header->nextPropagatingException = globals->propagatingExceptions; |
321 | | globals->propagatingExceptions = exception_header; |
322 | | } |
323 | | ++exception_header->propagationCount; |
324 | | } |
325 | | else |
326 | | { |
327 | | // If the propagatingExceptions stack is not empty, since we can't |
328 | | // chain the foreign exception, terminate it. |
329 | | if (NULL != globals->propagatingExceptions) |
330 | | std::terminate(); |
331 | | globals->propagatingExceptions = exception_header; |
332 | | } |
333 | | return true; |
334 | | } |
335 | | |
336 | | /* |
337 | | The routine to be called after the cleanup has been performed. It will get the |
338 | | propagating __cxa_exception from __cxa_eh_globals, and continue the stack |
339 | | unwinding with _Unwind_Resume. |
340 | | |
341 | | According to ARM EHABI 8.4.1, __cxa_end_cleanup() should not clobber any |
342 | | register, thus we have to write this function in assembly so that we can save |
343 | | {r1, r2, r3}. We don't have to save r0 because it is the return value and the |
344 | | first argument to _Unwind_Resume(). The function also saves/restores r4 to |
345 | | keep the stack aligned and to provide a temp register. _Unwind_Resume never |
346 | | returns and we need to keep the original lr so just branch to it. When |
347 | | targeting bare metal, the function also clobbers ip/r12 to hold the address of |
348 | | _Unwind_Resume, which may be too far away for an ordinary branch. |
349 | | */ |
350 | | __attribute__((used)) static _Unwind_Exception * |
351 | | __cxa_end_cleanup_impl() |
352 | | { |
353 | | __cxa_eh_globals* globals = __cxa_get_globals(); |
354 | | __cxa_exception* exception_header = globals->propagatingExceptions; |
355 | | if (NULL == exception_header) |
356 | | { |
357 | | // It seems that __cxa_begin_cleanup() is not called properly. |
358 | | // We have no choice but terminate the program now. |
359 | | std::terminate(); |
360 | | } |
361 | | |
362 | | if (__isOurExceptionClass(&exception_header->unwindHeader)) |
363 | | { |
364 | | --exception_header->propagationCount; |
365 | | if (0 == exception_header->propagationCount) |
366 | | { |
367 | | globals->propagatingExceptions = exception_header->nextPropagatingException; |
368 | | exception_header->nextPropagatingException = NULL; |
369 | | } |
370 | | } |
371 | | else |
372 | | { |
373 | | globals->propagatingExceptions = NULL; |
374 | | } |
375 | | return &exception_header->unwindHeader; |
376 | | } |
377 | | |
378 | | asm(" .pushsection .text.__cxa_end_cleanup,\"ax\",%progbits\n" |
379 | | " .globl __cxa_end_cleanup\n" |
380 | | " .type __cxa_end_cleanup,%function\n" |
381 | | "__cxa_end_cleanup:\n" |
382 | | #if defined(__ARM_FEATURE_BTI_DEFAULT) |
383 | | " bti\n" |
384 | | #endif |
385 | | " push {r1, r2, r3, r4}\n" |
386 | | " mov r4, lr\n" |
387 | | " bl __cxa_end_cleanup_impl\n" |
388 | | " mov lr, r4\n" |
389 | | #if defined(LIBCXXABI_BAREMETAL) |
390 | | " ldr r4, =_Unwind_Resume\n" |
391 | | " mov ip, r4\n" |
392 | | #endif |
393 | | " pop {r1, r2, r3, r4}\n" |
394 | | #if defined(LIBCXXABI_BAREMETAL) |
395 | | " bx ip\n" |
396 | | #else |
397 | | " b _Unwind_Resume\n" |
398 | | #endif |
399 | | " .popsection"); |
400 | | #endif // defined(_LIBCXXABI_ARM_EHABI) |
401 | | |
402 | | /* |
403 | | This routine can catch foreign or native exceptions. If native, the exception |
404 | | can be a primary or dependent variety. This routine may remain blissfully |
405 | | ignorant of whether the native exception is primary or dependent. |
406 | | |
407 | | If the exception is native: |
408 | | * Increment's the exception's handler count. |
409 | | * Push the exception on the stack of currently-caught exceptions if it is not |
410 | | already there (from a rethrow). |
411 | | * Decrements the uncaught_exception count. |
412 | | * Returns the adjusted pointer to the exception object, which is stored in |
413 | | the __cxa_exception by the personality routine. |
414 | | |
415 | | If the exception is foreign, this means it did not originate from one of throw |
416 | | routines. The foreign exception does not necessarily have a __cxa_exception |
417 | | header. However we can catch it here with a catch (...), or with a call |
418 | | to terminate or unexpected during unwinding. |
419 | | * Do not try to increment the exception's handler count, we don't know where |
420 | | it is. |
421 | | * Push the exception on the stack of currently-caught exceptions only if the |
422 | | stack is empty. The foreign exception has no way to link to the current |
423 | | top of stack. If the stack is not empty, call terminate. Even with an |
424 | | empty stack, this is hacked in by pushing a pointer to an imaginary |
425 | | __cxa_exception block in front of the foreign exception. It would be better |
426 | | if the __cxa_eh_globals structure had a stack of _Unwind_Exception, but it |
427 | | doesn't. It has a stack of __cxa_exception (which has a next* in it). |
428 | | * Do not decrement the uncaught_exception count because we didn't increment it |
429 | | in __cxa_throw (or one of our rethrow functions). |
430 | | * If we haven't terminated, assume the exception object is just past the |
431 | | _Unwind_Exception and return a pointer to that. |
432 | | */ |
433 | | void* |
434 | | __cxa_begin_catch(void* unwind_arg) throw() |
435 | 1.88M | { |
436 | 1.88M | _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg); |
437 | 1.88M | bool native_exception = __isOurExceptionClass(unwind_exception); |
438 | 1.88M | __cxa_eh_globals* globals = __cxa_get_globals(); |
439 | | // exception_header is a hackish offset from a foreign exception, but it |
440 | | // works as long as we're careful not to try to access any __cxa_exception |
441 | | // parts. |
442 | 1.88M | __cxa_exception* exception_header = |
443 | 1.88M | cxa_exception_from_exception_unwind_exception |
444 | 1.88M | ( |
445 | 1.88M | static_cast<_Unwind_Exception*>(unwind_exception) |
446 | 1.88M | ); |
447 | | |
448 | | #if defined(__MVS__) |
449 | | // Remove the exception object from the linked list of exceptions that the z/OS unwinder |
450 | | // maintains before adding it to the libc++abi list of caught exceptions. |
451 | | // The libc++abi will manage the lifetime of the exception from this point forward. |
452 | | _UnwindZOS_PopException(); |
453 | | #endif |
454 | | |
455 | 1.88M | if (native_exception) |
456 | 1.88M | { |
457 | | // Increment the handler count, removing the flag about being rethrown |
458 | 1.88M | exception_header->handlerCount = exception_header->handlerCount < 0 ? |
459 | 1.88M | -exception_header->handlerCount + 1 : exception_header->handlerCount + 1; |
460 | | // place the exception on the top of the stack if it's not already |
461 | | // there by a previous rethrow |
462 | 1.88M | if (exception_header != globals->caughtExceptions) |
463 | 1.88M | { |
464 | 1.88M | exception_header->nextException = globals->caughtExceptions; |
465 | 1.88M | globals->caughtExceptions = exception_header; |
466 | 1.88M | } |
467 | 1.88M | globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local |
468 | | #if defined(_LIBCXXABI_ARM_EHABI) |
469 | | return reinterpret_cast<void*>(exception_header->unwindHeader.barrier_cache.bitpattern[0]); |
470 | | #else |
471 | 1.88M | return exception_header->adjustedPtr; |
472 | 1.88M | #endif |
473 | 1.88M | } |
474 | | // Else this is a foreign exception |
475 | | // If the caughtExceptions stack is not empty, terminate |
476 | 597 | if (globals->caughtExceptions != 0) |
477 | 0 | std::terminate(); |
478 | | // Push the foreign exception on to the stack |
479 | 597 | globals->caughtExceptions = exception_header; |
480 | 597 | return unwind_exception + 1; |
481 | 1.88M | } |
482 | | |
483 | | |
484 | | /* |
485 | | Upon exit for any reason, a handler must call: |
486 | | void __cxa_end_catch (); |
487 | | |
488 | | This routine can be called for either a native or foreign exception. |
489 | | For a native exception: |
490 | | * Locates the most recently caught exception and decrements its handler count. |
491 | | * Removes the exception from the caught exception stack, if the handler count goes to zero. |
492 | | * If the handler count goes down to zero, and the exception was not re-thrown |
493 | | by throw, it locates the primary exception (which may be the same as the one |
494 | | it's handling) and decrements its reference count. If that reference count |
495 | | goes to zero, the function destroys the exception. In any case, if the current |
496 | | exception is a dependent exception, it destroys that. |
497 | | |
498 | | For a foreign exception: |
499 | | * If it has been rethrown, there is nothing to do. |
500 | | * Otherwise delete the exception and pop the catch stack to empty. |
501 | | */ |
502 | 1.88M | void __cxa_end_catch() { |
503 | 1.88M | static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception), |
504 | 1.88M | "sizeof(__cxa_exception) must be equal to " |
505 | 1.88M | "sizeof(__cxa_dependent_exception)"); |
506 | 1.88M | static_assert(__builtin_offsetof(__cxa_exception, referenceCount) == |
507 | 1.88M | __builtin_offsetof(__cxa_dependent_exception, |
508 | 1.88M | primaryException), |
509 | 1.88M | "the layout of __cxa_exception must match the layout of " |
510 | 1.88M | "__cxa_dependent_exception"); |
511 | 1.88M | static_assert(__builtin_offsetof(__cxa_exception, handlerCount) == |
512 | 1.88M | __builtin_offsetof(__cxa_dependent_exception, handlerCount), |
513 | 1.88M | "the layout of __cxa_exception must match the layout of " |
514 | 1.88M | "__cxa_dependent_exception"); |
515 | 1.88M | __cxa_eh_globals* globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch |
516 | 1.88M | __cxa_exception* exception_header = globals->caughtExceptions; |
517 | | // If we've rethrown a foreign exception, then globals->caughtExceptions |
518 | | // will have been made an empty stack by __cxa_rethrow() and there is |
519 | | // nothing more to be done. Do nothing! |
520 | 1.88M | if (NULL != exception_header) |
521 | 1.88M | { |
522 | 1.88M | bool native_exception = __isOurExceptionClass(&exception_header->unwindHeader); |
523 | 1.88M | if (native_exception) |
524 | 1.88M | { |
525 | | // This is a native exception |
526 | 1.88M | if (exception_header->handlerCount < 0) |
527 | 15.9k | { |
528 | | // The exception has been rethrown by __cxa_rethrow, so don't delete it |
529 | 15.9k | if (0 == incrementHandlerCount(exception_header)) |
530 | 15.9k | { |
531 | | // Remove from the chain of uncaught exceptions |
532 | 15.9k | globals->caughtExceptions = exception_header->nextException; |
533 | | // but don't destroy |
534 | 15.9k | } |
535 | | // Keep handlerCount negative in case there are nested catch's |
536 | | // that need to be told that this exception is rethrown. Don't |
537 | | // erase this rethrow flag until the exception is recaught. |
538 | 15.9k | } |
539 | 1.87M | else |
540 | 1.87M | { |
541 | | // The native exception has not been rethrown |
542 | 1.87M | if (0 == decrementHandlerCount(exception_header)) |
543 | 1.87M | { |
544 | | // Remove from the chain of uncaught exceptions |
545 | 1.87M | globals->caughtExceptions = exception_header->nextException; |
546 | | // Destroy this exception, being careful to distinguish |
547 | | // between dependent and primary exceptions |
548 | 1.87M | if (isDependentException(&exception_header->unwindHeader)) |
549 | 0 | { |
550 | | // Reset exception_header to primaryException and deallocate the dependent exception |
551 | 0 | __cxa_dependent_exception* dep_exception_header = |
552 | 0 | reinterpret_cast<__cxa_dependent_exception*>(exception_header); |
553 | 0 | exception_header = |
554 | 0 | cxa_exception_from_thrown_object(dep_exception_header->primaryException); |
555 | 0 | __cxa_free_dependent_exception(dep_exception_header); |
556 | 0 | } |
557 | | // Destroy the primary exception only if its referenceCount goes to 0 |
558 | | // (this decrement must be atomic) |
559 | 1.87M | __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header)); |
560 | 1.87M | } |
561 | 1.87M | } |
562 | 1.88M | } |
563 | 15 | else |
564 | 15 | { |
565 | | // The foreign exception has not been rethrown. Pop the stack |
566 | | // and delete it. If there are nested catch's and they try |
567 | | // to touch a foreign exception in any way, that is undefined |
568 | | // behavior. They likely can't since the only way to catch |
569 | | // a foreign exception is with catch (...)! |
570 | 15 | _Unwind_DeleteException(&globals->caughtExceptions->unwindHeader); |
571 | 15 | globals->caughtExceptions = 0; |
572 | 15 | } |
573 | 1.88M | } |
574 | 1.88M | } |
575 | | |
576 | | // Note: exception_header may be masquerading as a __cxa_dependent_exception |
577 | | // and that's ok. exceptionType is there too. |
578 | | // However watch out for foreign exceptions. Return null for them. |
579 | 0 | std::type_info *__cxa_current_exception_type() { |
580 | | // get the current exception |
581 | 0 | __cxa_eh_globals *globals = __cxa_get_globals_fast(); |
582 | 0 | if (NULL == globals) |
583 | 0 | return NULL; // If there have never been any exceptions, there are none now. |
584 | 0 | __cxa_exception *exception_header = globals->caughtExceptions; |
585 | 0 | if (NULL == exception_header) |
586 | 0 | return NULL; // No current exception |
587 | 0 | if (!__isOurExceptionClass(&exception_header->unwindHeader)) |
588 | 0 | return NULL; |
589 | 0 | return exception_header->exceptionType; |
590 | 0 | } |
591 | | |
592 | | // 2.5.4 Rethrowing Exceptions |
593 | | /* This routine can rethrow native or foreign exceptions. |
594 | | If the exception is native: |
595 | | * marks the exception object on top of the caughtExceptions stack |
596 | | (in an implementation-defined way) as being rethrown. |
597 | | * If the caughtExceptions stack is empty, it calls terminate() |
598 | | (see [C++FDIS] [except.throw], 15.1.8). |
599 | | * It then calls _Unwind_RaiseException which should not return |
600 | | (terminate if it does). |
601 | | Note: exception_header may be masquerading as a __cxa_dependent_exception |
602 | | and that's ok. |
603 | | */ |
604 | 15.9k | void __cxa_rethrow() { |
605 | 15.9k | __cxa_eh_globals* globals = __cxa_get_globals(); |
606 | 15.9k | __cxa_exception* exception_header = globals->caughtExceptions; |
607 | 15.9k | if (NULL == exception_header) |
608 | 0 | std::terminate(); // throw; called outside of a exception handler |
609 | 15.9k | bool native_exception = __isOurExceptionClass(&exception_header->unwindHeader); |
610 | 15.9k | if (native_exception) |
611 | 15.9k | { |
612 | | // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch) |
613 | 15.9k | exception_header->handlerCount = -exception_header->handlerCount; |
614 | 15.9k | globals->uncaughtExceptions += 1; |
615 | | // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary |
616 | 15.9k | } |
617 | 0 | else // this is a foreign exception |
618 | 0 | { |
619 | | // The only way to communicate to __cxa_end_catch that we've rethrown |
620 | | // a foreign exception, so don't delete us, is to pop the stack here |
621 | | // which must be empty afterwards. Then __cxa_end_catch will do |
622 | | // nothing |
623 | 0 | globals->caughtExceptions = 0; |
624 | 0 | } |
625 | | #ifdef __USING_SJLJ_EXCEPTIONS__ |
626 | | _Unwind_SjLj_RaiseException(&exception_header->unwindHeader); |
627 | | #else |
628 | 15.9k | _Unwind_RaiseException(&exception_header->unwindHeader); |
629 | 15.9k | #endif |
630 | | |
631 | | // If we get here, some kind of unwinding error has occurred. |
632 | | // There is some weird code generation bug happening with |
633 | | // Apple clang version 4.0 (tags/Apple/clang-418.0.2) (based on LLVM 3.1svn) |
634 | | // If we call failed_throw here. Turns up with -O2 or higher, and -Os. |
635 | 15.9k | __cxa_begin_catch(&exception_header->unwindHeader); |
636 | 15.9k | if (native_exception) |
637 | 0 | std::__terminate(exception_header->terminateHandler); |
638 | | // Foreign exception: can't get exception_header->terminateHandler |
639 | 15.9k | std::terminate(); |
640 | 15.9k | } |
641 | | |
642 | | /* |
643 | | If thrown_object is not null, atomically increment the referenceCount field |
644 | | of the __cxa_exception header associated with the thrown object referred to |
645 | | by thrown_object. |
646 | | |
647 | | Requires: If thrown_object is not NULL, it is a native exception. |
648 | | */ |
649 | | void |
650 | 0 | __cxa_increment_exception_refcount(void *thrown_object) throw() { |
651 | 0 | if (thrown_object != NULL ) |
652 | 0 | { |
653 | 0 | __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); |
654 | 0 | std::__libcpp_atomic_add(&exception_header->referenceCount, size_t(1)); |
655 | 0 | } |
656 | 0 | } |
657 | | |
658 | | /* |
659 | | If thrown_object is not null, atomically decrement the referenceCount field |
660 | | of the __cxa_exception header associated with the thrown object referred to |
661 | | by thrown_object. If the referenceCount drops to zero, destroy and |
662 | | deallocate the exception. |
663 | | |
664 | | Requires: If thrown_object is not NULL, it is a native exception. |
665 | | */ |
666 | | _LIBCXXABI_NO_CFI |
667 | 1.87M | void __cxa_decrement_exception_refcount(void *thrown_object) throw() { |
668 | 1.87M | if (thrown_object != NULL ) |
669 | 1.87M | { |
670 | 1.87M | __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); |
671 | 1.87M | if (std::__libcpp_atomic_add(&exception_header->referenceCount, size_t(-1)) == 0) |
672 | 1.87M | { |
673 | 1.87M | if (NULL != exception_header->exceptionDestructor) |
674 | 1.87M | exception_header->exceptionDestructor(thrown_object); |
675 | 1.87M | __cxa_free_exception(thrown_object); |
676 | 1.87M | } |
677 | 1.87M | } |
678 | 1.87M | } |
679 | | |
680 | | /* |
681 | | Returns a pointer to the thrown object (if any) at the top of the |
682 | | caughtExceptions stack. Atomically increment the exception's referenceCount. |
683 | | If there is no such thrown object or if the thrown object is foreign, |
684 | | returns null. |
685 | | |
686 | | We can use __cxa_get_globals_fast here to get the globals because if there have |
687 | | been no exceptions thrown, ever, on this thread, we can return NULL without |
688 | | the need to allocate the exception-handling globals. |
689 | | */ |
690 | 0 | void *__cxa_current_primary_exception() throw() { |
691 | | // get the current exception |
692 | 0 | __cxa_eh_globals* globals = __cxa_get_globals_fast(); |
693 | 0 | if (NULL == globals) |
694 | 0 | return NULL; // If there are no globals, there is no exception |
695 | 0 | __cxa_exception* exception_header = globals->caughtExceptions; |
696 | 0 | if (NULL == exception_header) |
697 | 0 | return NULL; // No current exception |
698 | 0 | if (!__isOurExceptionClass(&exception_header->unwindHeader)) |
699 | 0 | return NULL; // Can't capture a foreign exception (no way to refcount it) |
700 | 0 | if (isDependentException(&exception_header->unwindHeader)) { |
701 | 0 | __cxa_dependent_exception* dep_exception_header = |
702 | 0 | reinterpret_cast<__cxa_dependent_exception*>(exception_header); |
703 | 0 | exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException); |
704 | 0 | } |
705 | 0 | void* thrown_object = thrown_object_from_cxa_exception(exception_header); |
706 | 0 | __cxa_increment_exception_refcount(thrown_object); |
707 | 0 | return thrown_object; |
708 | 0 | } |
709 | | |
710 | | /* |
711 | | If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler |
712 | | stored in exc is called. Otherwise the referenceCount stored in the |
713 | | primary exception is decremented, destroying the primary if necessary. |
714 | | Finally the dependent exception is destroyed. |
715 | | */ |
716 | | static |
717 | | void |
718 | | dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception) |
719 | 0 | { |
720 | 0 | __cxa_dependent_exception* dep_exception_header = |
721 | 0 | reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1; |
722 | 0 | if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason) |
723 | 0 | std::__terminate(dep_exception_header->terminateHandler); |
724 | 0 | __cxa_decrement_exception_refcount(dep_exception_header->primaryException); |
725 | 0 | __cxa_free_dependent_exception(dep_exception_header); |
726 | 0 | } |
727 | | |
728 | | /* |
729 | | If thrown_object is not null, allocate, initialize and throw a dependent |
730 | | exception. |
731 | | */ |
732 | | void |
733 | | __cxa_rethrow_primary_exception(void* thrown_object) |
734 | 0 | { |
735 | 0 | if ( thrown_object != NULL ) |
736 | 0 | { |
737 | | // thrown_object guaranteed to be native because |
738 | | // __cxa_current_primary_exception returns NULL for foreign exceptions |
739 | 0 | __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); |
740 | 0 | __cxa_dependent_exception* dep_exception_header = |
741 | 0 | static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception()); |
742 | 0 | dep_exception_header->primaryException = thrown_object; |
743 | 0 | __cxa_increment_exception_refcount(thrown_object); |
744 | 0 | dep_exception_header->exceptionType = exception_header->exceptionType; |
745 | 0 | dep_exception_header->unexpectedHandler = std::get_unexpected(); |
746 | 0 | dep_exception_header->terminateHandler = std::get_terminate(); |
747 | 0 | setDependentExceptionClass(&dep_exception_header->unwindHeader); |
748 | 0 | __cxa_get_globals()->uncaughtExceptions += 1; |
749 | 0 | dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup; |
750 | | #ifdef __USING_SJLJ_EXCEPTIONS__ |
751 | | _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader); |
752 | | #else |
753 | 0 | _Unwind_RaiseException(&dep_exception_header->unwindHeader); |
754 | 0 | #endif |
755 | | // Some sort of unwinding error. Note that terminate is a handler. |
756 | 0 | __cxa_begin_catch(&dep_exception_header->unwindHeader); |
757 | 0 | } |
758 | | // If we return client will call terminate() |
759 | 0 | } |
760 | | |
761 | | bool |
762 | 0 | __cxa_uncaught_exception() throw() { return __cxa_uncaught_exceptions() != 0; } |
763 | | |
764 | | unsigned int |
765 | | __cxa_uncaught_exceptions() throw() |
766 | 14.0k | { |
767 | | // This does not report foreign exceptions in flight |
768 | 14.0k | __cxa_eh_globals* globals = __cxa_get_globals_fast(); |
769 | 14.0k | if (globals == 0) |
770 | 0 | return 0; |
771 | 14.0k | return globals->uncaughtExceptions; |
772 | 14.0k | } |
773 | | |
774 | | } // extern "C" |
775 | | |
776 | | } // abi |