1 //===------------------------- cxa_exception.cpp --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
9 // This file implements the "Exception Handling APIs"
10 // http://www.codesourcery.com/public/cxx-abi/abi-eh.html
12 //===----------------------------------------------------------------------===//
16 #include <exception> // for std::terminate
17 #include <cstdlib> // for malloc, free
18 #include <cstring> // for memset
21 #include "cxa_exception.hpp"
22 #include "cxa_handlers.hpp"
24 // +---------------------------+-----------------------------+---------------+
25 // | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object |
26 // +---------------------------+-----------------------------+---------------+
29 // +-------------------------------------------------------+
31 // +---------------------------+-----------------------------+
32 // | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 |
33 // +---------------------------+-----------------------------+
35 namespace __cxxabiv1 {
37 #pragma GCC visibility push(default)
43 cxa_exception_from_thrown_object(void* thrown_object)
45 return static_cast<__cxa_exception*>(thrown_object) - 1;
48 // Note: This is never called when exception_header is masquerading as a
49 // __cxa_dependent_exception.
53 thrown_object_from_cxa_exception(__cxa_exception* exception_header)
55 return static_cast<void*>(exception_header + 1);
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
64 cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception)
66 return cxa_exception_from_thrown_object(unwind_exception + 1 );
72 cxa_exception_size_from_exception_thrown_size(size_t size)
74 return size + sizeof (__cxa_exception);
77 static void setExceptionClass(_Unwind_Exception* unwind_exception) {
78 unwind_exception->exception_class = kOurExceptionClass;
81 static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) {
82 unwind_exception->exception_class = kOurDependentExceptionClass;
86 static bool isOurExceptionClass(const _Unwind_Exception* unwind_exception) {
87 return (unwind_exception->exception_class & get_vendor_and_language) ==
88 (kOurExceptionClass & get_vendor_and_language);
91 static bool isDependentException(_Unwind_Exception* unwind_exception) {
92 return (unwind_exception->exception_class & 0xFF) == 0x01;
95 // This does not need to be atomic
96 static inline int incrementHandlerCount(__cxa_exception *exception) {
97 return ++exception->handlerCount;
100 // This does not need to be atomic
101 static inline int decrementHandlerCount(__cxa_exception *exception) {
102 return --exception->handlerCount;
105 #include "fallback_malloc.ipp"
107 // Allocate some memory from _somewhere_
108 static void *do_malloc(size_t size) {
109 void *ptr = std::malloc(size);
110 if (NULL == ptr) // if malloc fails, fall back to emergency stash
111 ptr = fallback_malloc(size);
115 static void do_free(void *ptr) {
116 is_fallback_ptr(ptr) ? fallback_free(ptr) : std::free(ptr);
120 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
121 stored in exc is called. Otherwise the exceptionDestructor stored in
122 exc is called, and then the memory for the exception is deallocated.
124 This is never called for a __cxa_dependent_exception.
128 exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
130 __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception);
131 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
132 std::__terminate(exception_header->terminateHandler);
133 // Just in case there exists a dependent exception that is pointing to this,
134 // check the reference count and only destroy this if that count goes to zero.
135 __cxa_decrement_exception_refcount(unwind_exception + 1);
138 static LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) {
139 // Section 2.5.3 says:
140 // * For purposes of this ABI, several things are considered exception handlers:
141 // ** A terminate() call due to a throw.
143 // * Upon entry, Following initialization of the catch parameter,
144 // a handler must call:
145 // * void *__cxa_begin_catch(void *exceptionObject );
146 (void) __cxa_begin_catch(&exception_header->unwindHeader);
147 std::__terminate(exception_header->terminateHandler);
152 // Allocate a __cxa_exception object, and zero-fill it.
153 // Reserve "thrown_size" bytes on the end for the user's exception
154 // object. Zero-fill the object. If memory can't be allocated, call
155 // std::terminate. Return a pointer to the memory to be used for the
156 // user's exception object.
157 void * __cxa_allocate_exception (size_t thrown_size) throw() {
158 size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size);
159 __cxa_exception* exception_header = static_cast<__cxa_exception*>(do_malloc(actual_size));
160 if (NULL == exception_header)
162 std::memset(exception_header, 0, actual_size);
163 return thrown_object_from_cxa_exception(exception_header);
167 // Free a __cxa_exception object allocated with __cxa_allocate_exception.
168 void __cxa_free_exception (void * thrown_object) throw() {
169 do_free(cxa_exception_from_thrown_object(thrown_object));
173 // This function shall allocate a __cxa_dependent_exception and
174 // return a pointer to it. (Really to the object, not past its' end).
175 // Otherwise, it will work like __cxa_allocate_exception.
176 void * __cxa_allocate_dependent_exception () {
177 size_t actual_size = sizeof(__cxa_dependent_exception);
178 void *ptr = do_malloc(actual_size);
181 std::memset(ptr, 0, actual_size);
186 // This function shall free a dependent_exception.
187 // It does not affect the reference count of the primary exception.
188 void __cxa_free_dependent_exception (void * dependent_exception) {
189 do_free(dependent_exception);
193 // 2.4.3 Throwing the Exception Object
195 After constructing the exception object with the throw argument value,
196 the generated code calls the __cxa_throw runtime library routine. This
197 routine never returns.
199 The __cxa_throw routine will do the following:
201 * Obtain the __cxa_exception header from the thrown exception object address,
202 which can be computed as follows:
203 __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1);
204 * Save the current unexpected_handler and terminate_handler in the __cxa_exception header.
205 * Save the tinfo and dest arguments in the __cxa_exception header.
206 * Set the exception_class field in the unwind header. This is a 64-bit value
207 representing the ASCII string "XXXXC++\0", where "XXXX" is a
208 vendor-dependent string. That is, for implementations conforming to this
209 ABI, the low-order 4 bytes of this 64-bit value will be "C++\0".
210 * Increment the uncaught_exception flag.
211 * Call _Unwind_RaiseException in the system unwind library, Its argument is the
212 pointer to the thrown exception, which __cxa_throw itself received as an argument.
213 __Unwind_RaiseException begins the process of stack unwinding, described
214 in Section 2.5. In special cases, such as an inability to find a
215 handler, _Unwind_RaiseException may return. In that case, __cxa_throw
216 will call terminate, assuming that there was no handler for the
221 __cxa_throw(void* thrown_object, std::type_info* tinfo, void (*dest)(void*))
223 __cxa_eh_globals *globals = __cxa_get_globals();
224 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
226 exception_header->unexpectedHandler = std::get_unexpected();
227 exception_header->terminateHandler = std::get_terminate();
228 exception_header->exceptionType = tinfo;
229 exception_header->exceptionDestructor = dest;
230 setExceptionClass(&exception_header->unwindHeader);
231 exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety.
232 globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local
234 exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
236 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
238 _Unwind_RaiseException(&exception_header->unwindHeader);
240 // This only happens when there is no handler, or some unexpected unwinding
242 failed_throw(exception_header);
246 // 2.5.3 Exception Handlers
248 The adjusted pointer is computed by the personality routine during phase 1
249 and saved in the exception header (either __cxa_exception or
250 __cxa_dependent_exception).
252 Requires: exception is native
255 __cxa_get_exception_ptr(void* unwind_exception) throw()
257 return cxa_exception_from_exception_unwind_exception
259 static_cast<_Unwind_Exception*>(unwind_exception)
264 This routine can catch foreign or native exceptions. If native, the exception
265 can be a primary or dependent variety. This routine may remain blissfully
266 ignorant of whether the native exception is primary or dependent.
268 If the exception is native:
269 * Increment's the exception's handler count.
270 * Push the exception on the stack of currently-caught exceptions if it is not
271 already there (from a rethrow).
272 * Decrements the uncaught_exception count.
273 * Returns the adjusted pointer to the exception object, which is stored in
274 the __cxa_exception by the personality routine.
276 If the exception is foreign, this means it did not originate from one of throw
277 routines. The foreign exception does not necessarily have a __cxa_exception
278 header. However we can catch it here with a catch (...), or with a call
279 to terminate or unexpected during unwinding.
280 * Do not try to increment the exception's handler count, we don't know where
282 * Push the exception on the stack of currently-caught exceptions only if the
283 stack is empty. The foreign exception has no way to link to the current
284 top of stack. If the stack is not empty, call terminate. Even with an
285 empty stack, this is hacked in by pushing a pointer to an imaginary
286 __cxa_exception block in front of the foreign exception. It would be better
287 if the __cxa_eh_globals structure had a stack of _Unwind_Exception, but it
288 doesn't. It has a stack of __cxa_exception (which has a next* in it).
289 * Do not decrement the uncaught_exception count because we didn't increment it
290 in __cxa_throw (or one of our rethrow functions).
291 * If we haven't terminated, assume the exception object is just past the
292 _Unwind_Exception and return a pointer to that.
295 __cxa_begin_catch(void* unwind_arg) throw()
297 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
298 bool native_exception = isOurExceptionClass(unwind_exception);
299 __cxa_eh_globals* globals = __cxa_get_globals();
300 // exception_header is a hackish offset from a foreign exception, but it
301 // works as long as we're careful not to try to access any __cxa_exception
303 __cxa_exception* exception_header =
304 cxa_exception_from_exception_unwind_exception
306 static_cast<_Unwind_Exception*>(unwind_exception)
308 if (native_exception)
310 // Increment the handler count, removing the flag about being rethrown
311 exception_header->handlerCount = exception_header->handlerCount < 0 ?
312 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1;
313 // place the exception on the top of the stack if it's not already
314 // there by a previous rethrow
315 if (exception_header != globals->caughtExceptions)
317 exception_header->nextException = globals->caughtExceptions;
318 globals->caughtExceptions = exception_header;
320 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local
321 return exception_header->adjustedPtr;
323 // Else this is a foreign exception
324 // If the caughtExceptions stack is not empty, terminate
325 if (globals->caughtExceptions != 0)
327 // Push the foreign exception on to the stack
328 globals->caughtExceptions = exception_header;
329 return unwind_exception + 1;
334 Upon exit for any reason, a handler must call:
335 void __cxa_end_catch ();
337 This routine can be called for either a native or foreign exception.
338 For a native exception:
339 * Locates the most recently caught exception and decrements its handler count.
340 * Removes the exception from the caught exception stack, if the handler count goes to zero.
341 * If the handler count goes down to zero, and the exception was not re-thrown
342 by throw, it locates the primary exception (which may be the same as the one
343 it's handling) and decrements its reference count. If that reference count
344 goes to zero, the function destroys the exception. In any case, if the current
345 exception is a dependent exception, it destroys that.
347 For a foreign exception:
348 * If it has been rethrown, there is nothing to do.
349 * Otherwise delete the exception and pop the catch stack to empty.
351 void __cxa_end_catch()
353 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
354 "sizeof(__cxa_exception) must be equal to sizeof(__cxa_dependent_exception)");
355 __cxa_eh_globals* globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch
356 __cxa_exception* exception_header = globals->caughtExceptions;
357 // If we've rethrown a foreign exception, then globals->caughtExceptions
358 // will have been made an empty stack by __cxa_rethrow() and there is
359 // nothing more to be done. Do nothing!
360 if (NULL != exception_header)
362 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
363 if (native_exception)
365 // This is a native exception
366 if (exception_header->handlerCount < 0)
368 // The exception has been rethrown by __cxa_rethrow, so don't delete it
369 if (0 == incrementHandlerCount(exception_header))
371 // Remove from the chain of uncaught exceptions
372 globals->caughtExceptions = exception_header->nextException;
375 // Keep handlerCount negative in case there are nested catch's
376 // that need to be told that this exception is rethrown. Don't
377 // erase this rethrow flag until the exception is recaught.
381 // The native exception has not been rethrown
382 if (0 == decrementHandlerCount(exception_header))
384 // Remove from the chain of uncaught exceptions
385 globals->caughtExceptions = exception_header->nextException;
386 // Destroy this exception, being careful to distinguish
387 // between dependent and primary exceptions
388 if (isDependentException(&exception_header->unwindHeader))
390 // Reset exception_header to primaryException and deallocate the dependent exception
391 __cxa_dependent_exception* dep_exception_header =
392 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
394 cxa_exception_from_thrown_object(dep_exception_header->primaryException);
395 __cxa_free_dependent_exception(dep_exception_header);
397 // Destroy the primary exception only if its referenceCount goes to 0
398 // (this decrement must be atomic)
399 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header));
405 // The foreign exception has not been rethrown. Pop the stack
406 // and delete it. If there are nested catch's and they try
407 // to touch a foreign exception in any way, that is undefined
408 // behavior. They likely can't since the only way to catch
409 // a foreign exception is with catch (...)!
410 _Unwind_DeleteException(&globals->caughtExceptions->unwindHeader);
411 globals->caughtExceptions = 0;
416 // Note: exception_header may be masquerading as a __cxa_dependent_exception
417 // and that's ok. exceptionType is there too.
418 // However watch out for foreign exceptions. Return null for them.
419 std::type_info * __cxa_current_exception_type() {
420 // get the current exception
421 __cxa_eh_globals *globals = __cxa_get_globals_fast();
423 return NULL; // If there have never been any exceptions, there are none now.
424 __cxa_exception *exception_header = globals->caughtExceptions;
425 if (NULL == exception_header)
426 return NULL; // No current exception
427 if (!isOurExceptionClass(&exception_header->unwindHeader))
429 return exception_header->exceptionType;
432 // 2.5.4 Rethrowing Exceptions
433 /* This routine can rethrow native or foreign exceptions.
434 If the exception is native:
435 * marks the exception object on top of the caughtExceptions stack
436 (in an implementation-defined way) as being rethrown.
437 * If the caughtExceptions stack is empty, it calls terminate()
438 (see [C++FDIS] [except.throw], 15.1.8).
439 * It then calls _Unwind_RaiseException which should not return
440 (terminate if it does).
441 Note: exception_header may be masquerading as a __cxa_dependent_exception
448 __cxa_eh_globals* globals = __cxa_get_globals();
449 __cxa_exception* exception_header = globals->caughtExceptions;
450 if (NULL == exception_header)
451 std::terminate(); // throw; called outside of a exception handler
452 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
453 if (native_exception)
455 // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)
456 exception_header->handlerCount = -exception_header->handlerCount;
457 globals->uncaughtExceptions += 1;
458 // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary
460 else // this is a foreign exception
462 // The only way to communicate to __cxa_end_catch that we've rethrown
463 // a foreign exception, so don't delete us, is to pop the stack here
464 // which must be empty afterwards. Then __cxa_end_catch will do
466 globals->caughtExceptions = 0;
469 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
471 _Unwind_RaiseException(&exception_header->unwindHeader);
474 // If we get here, some kind of unwinding error has occurred.
475 // There is some weird code generation bug happening with
476 // Apple clang version 4.0 (tags/Apple/clang-418.0.2) (based on LLVM 3.1svn)
477 // If we call failed_throw here. Turns up with -O2 or higher, and -Os.
478 __cxa_begin_catch(&exception_header->unwindHeader);
479 if (native_exception)
480 std::__terminate(exception_header->terminateHandler);
481 // Foreign exception: can't get exception_header->terminateHandler
486 If thrown_object is not null, atomically increment the referenceCount field
487 of the __cxa_exception header associated with the thrown object referred to
490 Requires: If thrown_object is not NULL, it is a native exception.
493 __cxa_increment_exception_refcount(void* thrown_object) throw()
495 if (thrown_object != NULL )
497 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
498 __sync_add_and_fetch(&exception_header->referenceCount, 1);
503 If thrown_object is not null, atomically decrement the referenceCount field
504 of the __cxa_exception header associated with the thrown object referred to
505 by thrown_object. If the referenceCount drops to zero, destroy and
506 deallocate the exception.
508 Requires: If thrown_object is not NULL, it is a native exception.
511 __cxa_decrement_exception_refcount(void* thrown_object) throw()
513 if (thrown_object != NULL )
515 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
516 if (__sync_sub_and_fetch(&exception_header->referenceCount, size_t(1)) == 0)
518 if (NULL != exception_header->exceptionDestructor)
519 exception_header->exceptionDestructor(thrown_object);
520 __cxa_free_exception(thrown_object);
526 Returns a pointer to the thrown object (if any) at the top of the
527 caughtExceptions stack. Atomically increment the exception's referenceCount.
528 If there is no such thrown object or if the thrown object is foreign,
531 We can use __cxa_get_globals_fast here to get the globals because if there have
532 been no exceptions thrown, ever, on this thread, we can return NULL without
533 the need to allocate the exception-handling globals.
536 __cxa_current_primary_exception() throw()
538 // get the current exception
539 __cxa_eh_globals* globals = __cxa_get_globals_fast();
541 return NULL; // If there are no globals, there is no exception
542 __cxa_exception* exception_header = globals->caughtExceptions;
543 if (NULL == exception_header)
544 return NULL; // No current exception
545 if (!isOurExceptionClass(&exception_header->unwindHeader))
546 return NULL; // Can't capture a foreign exception (no way to refcount it)
547 if (isDependentException(&exception_header->unwindHeader)) {
548 __cxa_dependent_exception* dep_exception_header =
549 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
550 exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException);
552 void* thrown_object = thrown_object_from_cxa_exception(exception_header);
553 __cxa_increment_exception_refcount(thrown_object);
554 return thrown_object;
558 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
559 stored in exc is called. Otherwise the referenceCount stored in the
560 primary exception is decremented, destroying the primary if necessary.
561 Finally the dependent exception is destroyed.
565 dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
567 __cxa_dependent_exception* dep_exception_header =
568 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;
569 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
570 std::__terminate(dep_exception_header->terminateHandler);
571 __cxa_decrement_exception_refcount(dep_exception_header->primaryException);
572 __cxa_free_dependent_exception(dep_exception_header);
576 If thrown_object is not null, allocate, initialize and throw a dependent
580 __cxa_rethrow_primary_exception(void* thrown_object)
582 if ( thrown_object != NULL )
584 // thrown_object guaranteed to be native because
585 // __cxa_current_primary_exception returns NULL for foreign exceptions
586 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
587 __cxa_dependent_exception* dep_exception_header =
588 static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception());
589 dep_exception_header->primaryException = thrown_object;
590 __cxa_increment_exception_refcount(thrown_object);
591 dep_exception_header->exceptionType = exception_header->exceptionType;
592 dep_exception_header->unexpectedHandler = std::get_unexpected();
593 dep_exception_header->terminateHandler = std::get_terminate();
594 setDependentExceptionClass(&dep_exception_header->unwindHeader);
595 __cxa_get_globals()->uncaughtExceptions += 1;
596 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;
598 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);
600 _Unwind_RaiseException(&dep_exception_header->unwindHeader);
602 // Some sort of unwinding error. Note that terminate is a handler.
603 __cxa_begin_catch(&dep_exception_header->unwindHeader);
605 // If we return client will call terminate()
609 __cxa_uncaught_exception() throw()
611 // This does not report foreign exceptions in flight
612 __cxa_eh_globals* globals = __cxa_get_globals_fast();
615 return globals->uncaughtExceptions != 0;
620 #pragma GCC visibility pop