PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/project/libcxxabi/src/cxa_exception.cpp

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