annotate libitm/eh_cpp.cc @ 158:494b0b89df80 default tip

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