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