eh_cpp.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* Copyright (C) 2009-2022 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Transactional Memory Library (libitm).
  4. Libitm is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "libitm_i.h"
  20. using namespace GTM;
  21. /* Exceptions can exist in three phases: (1) after having been allocated by
  22. __cxa_allocate_exception but before being handed off to __cxa_throw,
  23. (2) when they are in flight, so between __cxa_throw and __cxa_begin_catch,
  24. and (3) when they are being handled (between __cxa_begin_catch and
  25. __cxa_end_catch). Note that when an exception is re-thrown in (3), it is
  26. not moving back to (2) but handled as a special case of (3) by the EH
  27. runtime.
  28. We can get aborts in all three phases, for example in (1) during
  29. construction of the exception object, or in (2) in destructors called
  30. while unwinding the stack. The transaction that created an exception
  31. object can only commit in phase (3) by re-throwing the exception; it cannot
  32. commit in other phases because throw expressions and catch clauses are
  33. properly nested wrt transactions and because the compiler wraps
  34. transaction bodies in a try/catch-all construct.
  35. We handle phase (1) by dealing with exception objects similar to how we
  36. deal with other (de)allocations, which also ensures that we can have more
  37. than one exception object allocated at the same time (e.g., if the
  38. throw expression itself throws an exception and thus calls
  39. __cxa_allocate_exception). However, on the call to __cxa_begin_catch
  40. we hand off the exception to the special handling of phase (3) and
  41. remove the undo log entry of the allocation. Note that if the allocation
  42. happened outside of this transaction, we do not need to do anything.
  43. When an exception reaches phase (2) due to a call to __cxa_throw, the count
  44. of uncaught exceptions is incremented. We roll back this effect by saving
  45. and restoring this number in the structure returned from __cxa_get_globals.
  46. This also takes care of increments of this count when re-throwing an
  47. exception.
  48. For phase (3), we keep track of the number of times __cxa_begin_catch
  49. has been called without a matching call to __cxa_end_catch. This count
  50. is then used by __cxa_tm_cleanup to roll back the exception handling state
  51. by calling __cxa_end_catch for the exceptions that have not been finished
  52. yet (without running destructors though because we roll back the memory
  53. anyway).
  54. Once an exception that was allocated in this transaction enters phase (3),
  55. it does not need to be deallocated on abort anymore because the calls to
  56. __cxa_end_catch will take care of that.
  57. We require all code executed by the transaction to be transaction_safe (or
  58. transaction_pure, or to have wrappers) if the transaction is to be rolled
  59. back. However, we take care to not require this for transactions that
  60. just commit; this way, transactions that enter serial mode and then call
  61. uninstrumented code continue to work.
  62. */
  63. /* Everything from libstdc++ is weak, to avoid requiring that library
  64. to be linked into plain C applications using libitm.so. */
  65. #define WEAK __attribute__((weak))
  66. extern "C" {
  67. struct __cxa_eh_globals
  68. {
  69. void * caughtExceptions;
  70. unsigned int uncaughtExceptions;
  71. };
  72. extern void *__cxa_allocate_exception (size_t) _ITM_NOTHROW WEAK;
  73. extern void __cxa_free_exception (void *) _ITM_NOTHROW WEAK;
  74. extern void __cxa_throw (void *, void *, void (*) (void *)) WEAK;
  75. extern void *__cxa_begin_catch (void *) _ITM_NOTHROW WEAK;
  76. extern void __cxa_end_catch (void) WEAK;
  77. extern void __cxa_tm_cleanup (void *, void *, unsigned int) throw () WEAK;
  78. extern __cxa_eh_globals *__cxa_get_globals (void) _ITM_NOTHROW WEAK;
  79. #if !defined (HAVE_ELF_STYLE_WEAKREF)
  80. void *__cxa_allocate_exception (size_t) _ITM_NOTHROW { return NULL; }
  81. void __cxa_free_exception (void *) _ITM_NOTHROW { return; }
  82. void __cxa_throw (void *, void *, void (*) (void *)) { return; }
  83. void *__cxa_begin_catch (void *) _ITM_NOTHROW { return NULL; }
  84. void __cxa_end_catch (void) { return; }
  85. void __cxa_tm_cleanup (void *, void *, unsigned int) throw () { return; }
  86. void _Unwind_DeleteException (_Unwind_Exception *) { return; }
  87. __cxa_eh_globals *__cxa_get_globals (void) _ITM_NOTHROW { return NULL; }
  88. #endif /* HAVE_ELF_STYLE_WEAKREF */
  89. }
  90. static void
  91. free_any_exception (void *exc_ptr)
  92. {
  93. // The exception could be in phase (2) and thus calling just
  94. // _cxa_free_exception might not be sufficient.
  95. __cxa_tm_cleanup (NULL, exc_ptr, 0);
  96. }
  97. void *
  98. _ITM_cxa_allocate_exception (size_t size) _ITM_NOTHROW
  99. {
  100. void *r = __cxa_allocate_exception (size);
  101. gtm_thr()->record_allocation (r, free_any_exception);
  102. return r;
  103. }
  104. void
  105. _ITM_cxa_free_exception (void *exc_ptr) _ITM_NOTHROW
  106. {
  107. // __cxa_free_exception can be called from user code directly if
  108. // construction of an exception object throws another exception, in which
  109. // case we need to roll back the initial exception. We handle this similar
  110. // to dead allocations in that we deallocate the exception on both commit
  111. // and abort of an outermost transaction.
  112. gtm_thr()->forget_allocation (exc_ptr, free_any_exception);
  113. }
  114. void
  115. _ITM_cxa_throw (void *obj, void *tinfo, void (*dest) (void *))
  116. {
  117. // This used to be instrumented, but does not need to be anymore.
  118. __cxa_throw (obj, tinfo, dest);
  119. }
  120. void *
  121. _ITM_cxa_begin_catch (void *exc_ptr) _ITM_NOTHROW
  122. {
  123. // If this exception object has been allocated by this transaction, we
  124. // discard the undo log entry for the allocation; we are entering phase (3)
  125. // now and will handle this exception specially.
  126. // Note that this exception cannot have been allocated in a parent
  127. // transaction or enclosing nontransactional block because an atomic block
  128. // cannot contain just a catch clause but not the associated try clause.
  129. // The exception can have been allocated in a nested transaction, in which
  130. // case the commit of the nested transaction will have inserted the undo
  131. // log entry of the allocation in our undo log.
  132. // The exception can also have been allocated in a nested nontransactional
  133. // block, but then this transaction cannot abort anymore; functions that
  134. // are marked transaction_pure, for example, must not side-step the
  135. // transactional exception handling we implement here.
  136. gtm_thread *t = gtm_thr ();
  137. t->discard_allocation (exc_ptr);
  138. // Keep track of the number of unfinished catch handlers.
  139. t->cxa_catch_count++;
  140. return __cxa_begin_catch (exc_ptr);
  141. }
  142. void
  143. _ITM_cxa_end_catch (void)
  144. {
  145. // Keep track of the number of unfinished catch handlers.
  146. gtm_thr()->cxa_catch_count--;
  147. __cxa_end_catch ();
  148. }
  149. void
  150. GTM::gtm_thread::init_cpp_exceptions ()
  151. {
  152. // Only save and restore the number of uncaught exceptions if this is
  153. // actually used in the program.
  154. if (
  155. #if HAVE_ELF_STYLE_WEAKREF
  156. __cxa_get_globals != NULL &&
  157. #endif
  158. __cxa_get_globals () != 0)
  159. cxa_uncaught_count_ptr = &__cxa_get_globals ()->uncaughtExceptions;
  160. else
  161. cxa_uncaught_count_ptr = 0;
  162. }
  163. void
  164. GTM::gtm_thread::revert_cpp_exceptions (gtm_transaction_cp *cp)
  165. {
  166. if (cp)
  167. {
  168. // If rolling back a nested transaction, only clean up incompletely
  169. // caught exceptions since the last checkpoint.
  170. assert (cxa_catch_count >= cp->cxa_catch_count);
  171. uint32_t catch_count = cxa_catch_count - cp->cxa_catch_count;
  172. if (catch_count)
  173. {
  174. __cxa_tm_cleanup (NULL, NULL, catch_count);
  175. cxa_catch_count = cp->cxa_catch_count;
  176. }
  177. }
  178. else
  179. {
  180. // Both cxa_catch_count and cxa_unthrown are maximal because EH regions
  181. // and transactions are properly nested.
  182. if (cxa_catch_count)
  183. {
  184. __cxa_tm_cleanup (NULL, NULL, cxa_catch_count);
  185. cxa_catch_count = 0;
  186. }
  187. }
  188. // Reset the number of uncaught exceptions. Any allocations for these
  189. // exceptions have been rolled back already, if necessary.
  190. if (cxa_uncaught_count_ptr != 0)
  191. *cxa_uncaught_count_ptr = cxa_uncaught_count;
  192. // Always reset eh_in_flight because it just contains the argument provided
  193. // to _ITM_commitTransactionEH.
  194. eh_in_flight = NULL;
  195. }