llvm.org GIT mirror llvm / b40d5e7
* Add proper support for rethrown exceptions. * Make catch_begin() produce a pointer that must be passed to catch_end() * Eliminate the caught exception stack. * Add optional debugging code that may be turned on. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@8184 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 17 years ago
6 changed file(s) with 170 addition(s) and 62 deletion(s). Raw diff Collapse all Expand all
1010 #include
1111 #include
1212
13 //#define DEBUG
14
15 #ifdef DEBUG
16 #include
17 #endif
18
1319 //===----------------------------------------------------------------------===//
1420 // Generic exception support
1521 //
1723 // Thread local state for exception handling.
1824 // FIXME: This should really be made thread-local!
1925 //
20 static llvm_exception *CaughtExceptionStack = 0;
26
27 // LastCaughtException - The last exception caught by this handler. This is for
28 // implementation of _rethrow and _get_last_caught.
29 //
30 static llvm_exception *LastCaughtException = 0;
2131
2232 // UncaughtExceptionStack - The stack of exceptions currently being thrown.
2333 static llvm_exception *UncaughtExceptionStack = 0;
7585 // exception.
7686 //
7787 static void cxx_destructor(llvm_exception *LE) /* might throw */{
88 assert(LE->Next == 0 && "On the uncaught stack??");
7889 llvm_cxx_exception *E = get_cxx_exception(LE);
79
80 // The exception is no longer caught.
81 assert(CaughtExceptionStack == LE &&
82 "Destroying an exception which is not the current caught exception?");
83 CaughtExceptionStack = LE->Next;
8490
8591 struct ExceptionFreer {
8692 void *Ptr;
109115 E->BaseException.Next = UncaughtExceptionStack;
110116 UncaughtExceptionStack = &E->BaseException;
111117 E->BaseException.HandlerCount = 0;
118 E->BaseException.isRethrown = 0;
112119
113120 E->TypeInfo = (const std::type_info*)TypeInfoPtr;
114121 E->ExceptionObjectDestructor = DtorPtr;
120127 // CXXExceptionISA - use the type info object stored in the exception to see if
121128 // TypeID matches and, if so, to adjust the exception object pointer.
122129 //
123 static void *CXXExceptionISA(llvm_cxx_exception *E, const std::type_info *Type){
130 static void *CXXExceptionISA(llvm_cxx_exception *E,
131 const std::type_info *Type) throw() {
124132 // ThrownPtr is a pointer to the object being thrown...
125133 void *ThrownPtr = E+1;
126134 const std::type_info *ThrownType = E->TypeInfo;
127135
136 #if 0
128137 // FIXME: this code exists in the GCC exception handling library: I haven't
129138 // thought about this yet, so it should be verified at some point!
130 #if 1
139
131140 // Pointer types need to adjust the actual pointer, not
132141 // the pointer to pointer that is the exception object.
133142 // This also has the effect of passing pointer types
136145 ThrownPtr = *(void **)ThrownPtr;
137146 #endif
138147
139 if (Type->__do_catch(ThrownType, &ThrownPtr, 1))
148 if (Type->__do_catch(ThrownType, &ThrownPtr, 1)) {
149 #ifdef DEBUG
150 printf("isa<%s>(%s): 0x%p -> 0x%p\n", Type->name(), ThrownType->name(),
151 E+1, ThrownPtr);
152 #endif
140153 return ThrownPtr;
154 }
141155
142156 return 0;
143157 }
174188 UncaughtExceptionStack = E->Next;
175189
176190 // The exception is now caught.
177 E->Next = CaughtExceptionStack;
178 CaughtExceptionStack = E;
191 LastCaughtException = E;
192 E->Next = 0;
193 E->isRethrown = 0;
179194
180195 // Increment the handler count for this exception.
181196 E->HandlerCount++;
197
198 #ifdef DEBUG
199 printf("Exiting begin_catch Ex=0x%p HandlerCount=%d!\n", E+1,
200 E->HandlerCount);
201 #endif
182202
183203 // Return a pointer to the raw exception object.
184204 return E+1;
199219 return ObjPtr;
200220 }
201221
222 // __llvm_cxxeh_get_last_caught - Return the last exception that was caught by
223 // ...begin_catch.
224 //
225 void *__llvm_cxxeh_get_last_caught() throw() {
226 assert(LastCaughtException && "No exception caught!!");
227 return LastCaughtException+1;
228 }
202229
203230 // __llvm_cxxeh_end_catch - This function decrements the HandlerCount of the
204231 // top-level caught exception, destroying it if this is the last handler for the
205232 // exception.
206233 //
207 void __llvm_cxxeh_end_catch() /* might throw */ {
208 llvm_exception *E = CaughtExceptionStack;
234 void __llvm_cxxeh_end_catch(void *Ex) /* might throw */ {
235 llvm_exception *E = (llvm_exception*)Ex - 1;
209236 assert(E && "There are no caught exceptions!");
210237
211238 // If this is the last handler using the exception, destroy it now!
212 if (--E->HandlerCount == 0)
239 if (--E->HandlerCount == 0 && !E->isRethrown) {
240 #ifdef DEBUG
241 printf("Destroying exception!\n");
242 #endif
213243 E->ExceptionDestructor(E); // Release memory for the exception
214 }
215
244 }
245 #ifdef DEBUG
246 printf("Exiting end_catch Ex=0x%p HandlerCount=%d!\n", Ex, E->HandlerCount);
247 #endif
248 }
249
250 // __llvm_cxxeh_call_terminate - This function is called when the dtor for an
251 // object being destroyed due to an exception throw throws an exception. This
252 // is illegal because it would cause multiple exceptions to be active at one
253 // time.
216254 void __llvm_cxxeh_call_terminate() throw() {
217 __terminate(__terminate_handler);
255 void (*Handler)(void) = __terminate_handler;
256 if (UncaughtExceptionStack)
257 if (UncaughtExceptionStack->ExceptionType == CXXException)
258 Handler = get_cxx_exception(UncaughtExceptionStack)->TerminateHandler;
259 __terminate(Handler);
218260 }
219261
220262
224266 // prepared to deal with foreign exceptions.
225267 //
226268 void __llvm_cxxeh_rethrow() throw() {
227 llvm_exception *E = CaughtExceptionStack;
269 llvm_exception *E = LastCaughtException;
228270 if (E == 0)
229 // 15.1.8 - If there are no uncaught exceptions being thrown, 'throw;'
230 // should call terminate.
271 // 15.1.8 - If there are no exceptions being thrown, 'throw;' should call
272 // terminate.
231273 //
232274 __terminate(__terminate_handler);
233275
234 // Otherwise we have an exception to rethrow. Move it back to the uncaught
235 // stack.
236 CaughtExceptionStack = E->Next;
276 // Otherwise we have an exception to rethrow. Mark the exception as such.
277 E->isRethrown = 1;
278
279 // Add the exception to the top of the uncaught stack, to preserve the
280 // invariant that the top of the uncaught stack is the current exception.
237281 E->Next = UncaughtExceptionStack;
238282 UncaughtExceptionStack = E;
239
283
240284 // Return to the caller, which should perform the unwind now.
241285 }
242286
282326 // Whatever exception this is, it is not allowed by the (empty) spec, call
283327 // unexpected, according to 15.4.8.
284328 try {
285 __llvm_cxxeh_begin_catch(); // Start the catch
286 __llvm_cxxeh_end_catch(); // Free the exception
329 void *Ex = __llvm_cxxeh_begin_catch(); // Start the catch
330 __llvm_cxxeh_end_catch(Ex); // Free the exception
287331 __unexpected(__unexpected_handler);
288332 } catch (...) {
289333 // Any exception thrown by unexpected cannot match the ehspec. Call
306350 // permitted to pass through) or not a C++ exception that is allowed. Kill
307351 // the exception and call the unexpected handler.
308352 try {
309 __llvm_cxxeh_begin_catch(); // Start the catch
310 __llvm_cxxeh_end_catch(); // Free the exception
353 void *Ex = __llvm_cxxeh_begin_catch(); // Start the catch
354 __llvm_cxxeh_end_catch(Ex); // Free the exception
311355 } catch (...) {
312 __terminate(__terminate_handler); // Exception dtor threw
356 __terminate(__terminate_handler); // Exception dtor threw
313357 }
314358
315359 try {
5454 namespace __cxxabiv1 {
5555 // Invokes given handler, dying appropriately if the user handler was
5656 // so inconsiderate as to return.
57 extern void __terminate(std::terminate_handler) __attribute__((noreturn));
57 extern void __terminate(std::terminate_handler) throw() __attribute__((noreturn));
5858 extern void __unexpected(std::unexpected_handler) __attribute__((noreturn));
5959
6060 // The current installed user handlers.
7373 throw();
7474 void *__llvm_cxxeh_begin_catch() throw();
7575 void *__llvm_cxxeh_begin_catch_if_isa(void *CatchType) throw();
76 void __llvm_cxxeh_end_catch() /* might throw */;
76 void __llvm_cxxeh_end_catch(void *Exception) /* might throw */;
7777 void __llvm_cxxeh_rethrow() throw();
78 void *__llvm_cxxeh_get_last_caught() throw();
79
7880 void __llvm_cxxeh_check_eh_spec(void *Info, ...);
7981 }
8082
3030 // falls to zero, the exception is destroyed.
3131 //
3232 unsigned HandlerCount;
33
34 // isRethrown - This field is set on an exception if it has been 'throw;'n.
35 // This is needed because the exception might exit through a number of the
36 // end_catch statements matching the number of begin_catch statements that
37 // have been processed. When this happens, the exception should become
38 // uncaught, not dead.
39 //
40 int isRethrown;
3341 };
3442
3543 enum {
1010 #include
1111 #include
1212
13 //#define DEBUG
14
15 #ifdef DEBUG
16 #include
17 #endif
18
1319 //===----------------------------------------------------------------------===//
1420 // Generic exception support
1521 //
1723 // Thread local state for exception handling.
1824 // FIXME: This should really be made thread-local!
1925 //
20 static llvm_exception *CaughtExceptionStack = 0;
26
27 // LastCaughtException - The last exception caught by this handler. This is for
28 // implementation of _rethrow and _get_last_caught.
29 //
30 static llvm_exception *LastCaughtException = 0;
2131
2232 // UncaughtExceptionStack - The stack of exceptions currently being thrown.
2333 static llvm_exception *UncaughtExceptionStack = 0;
7585 // exception.
7686 //
7787 static void cxx_destructor(llvm_exception *LE) /* might throw */{
88 assert(LE->Next == 0 && "On the uncaught stack??");
7889 llvm_cxx_exception *E = get_cxx_exception(LE);
79
80 // The exception is no longer caught.
81 assert(CaughtExceptionStack == LE &&
82 "Destroying an exception which is not the current caught exception?");
83 CaughtExceptionStack = LE->Next;
8490
8591 struct ExceptionFreer {
8692 void *Ptr;
109115 E->BaseException.Next = UncaughtExceptionStack;
110116 UncaughtExceptionStack = &E->BaseException;
111117 E->BaseException.HandlerCount = 0;
118 E->BaseException.isRethrown = 0;
112119
113120 E->TypeInfo = (const std::type_info*)TypeInfoPtr;
114121 E->ExceptionObjectDestructor = DtorPtr;
120127 // CXXExceptionISA - use the type info object stored in the exception to see if
121128 // TypeID matches and, if so, to adjust the exception object pointer.
122129 //
123 static void *CXXExceptionISA(llvm_cxx_exception *E, const std::type_info *Type){
130 static void *CXXExceptionISA(llvm_cxx_exception *E,
131 const std::type_info *Type) throw() {
124132 // ThrownPtr is a pointer to the object being thrown...
125133 void *ThrownPtr = E+1;
126134 const std::type_info *ThrownType = E->TypeInfo;
127135
136 #if 0
128137 // FIXME: this code exists in the GCC exception handling library: I haven't
129138 // thought about this yet, so it should be verified at some point!
130 #if 1
139
131140 // Pointer types need to adjust the actual pointer, not
132141 // the pointer to pointer that is the exception object.
133142 // This also has the effect of passing pointer types
136145 ThrownPtr = *(void **)ThrownPtr;
137146 #endif
138147
139 if (Type->__do_catch(ThrownType, &ThrownPtr, 1))
148 if (Type->__do_catch(ThrownType, &ThrownPtr, 1)) {
149 #ifdef DEBUG
150 printf("isa<%s>(%s): 0x%p -> 0x%p\n", Type->name(), ThrownType->name(),
151 E+1, ThrownPtr);
152 #endif
140153 return ThrownPtr;
154 }
141155
142156 return 0;
143157 }
174188 UncaughtExceptionStack = E->Next;
175189
176190 // The exception is now caught.
177 E->Next = CaughtExceptionStack;
178 CaughtExceptionStack = E;
191 LastCaughtException = E;
192 E->Next = 0;
193 E->isRethrown = 0;
179194
180195 // Increment the handler count for this exception.
181196 E->HandlerCount++;
197
198 #ifdef DEBUG
199 printf("Exiting begin_catch Ex=0x%p HandlerCount=%d!\n", E+1,
200 E->HandlerCount);
201 #endif
182202
183203 // Return a pointer to the raw exception object.
184204 return E+1;
199219 return ObjPtr;
200220 }
201221
222 // __llvm_cxxeh_get_last_caught - Return the last exception that was caught by
223 // ...begin_catch.
224 //
225 void *__llvm_cxxeh_get_last_caught() throw() {
226 assert(LastCaughtException && "No exception caught!!");
227 return LastCaughtException+1;
228 }
202229
203230 // __llvm_cxxeh_end_catch - This function decrements the HandlerCount of the
204231 // top-level caught exception, destroying it if this is the last handler for the
205232 // exception.
206233 //
207 void __llvm_cxxeh_end_catch() /* might throw */ {
208 llvm_exception *E = CaughtExceptionStack;
234 void __llvm_cxxeh_end_catch(void *Ex) /* might throw */ {
235 llvm_exception *E = (llvm_exception*)Ex - 1;
209236 assert(E && "There are no caught exceptions!");
210237
211238 // If this is the last handler using the exception, destroy it now!
212 if (--E->HandlerCount == 0)
239 if (--E->HandlerCount == 0 && !E->isRethrown) {
240 #ifdef DEBUG
241 printf("Destroying exception!\n");
242 #endif
213243 E->ExceptionDestructor(E); // Release memory for the exception
214 }
215
244 }
245 #ifdef DEBUG
246 printf("Exiting end_catch Ex=0x%p HandlerCount=%d!\n", Ex, E->HandlerCount);
247 #endif
248 }
249
250 // __llvm_cxxeh_call_terminate - This function is called when the dtor for an
251 // object being destroyed due to an exception throw throws an exception. This
252 // is illegal because it would cause multiple exceptions to be active at one
253 // time.
216254 void __llvm_cxxeh_call_terminate() throw() {
217 __terminate(__terminate_handler);
255 void (*Handler)(void) = __terminate_handler;
256 if (UncaughtExceptionStack)
257 if (UncaughtExceptionStack->ExceptionType == CXXException)
258 Handler = get_cxx_exception(UncaughtExceptionStack)->TerminateHandler;
259 __terminate(Handler);
218260 }
219261
220262
224266 // prepared to deal with foreign exceptions.
225267 //
226268 void __llvm_cxxeh_rethrow() throw() {
227 llvm_exception *E = CaughtExceptionStack;
269 llvm_exception *E = LastCaughtException;
228270 if (E == 0)
229 // 15.1.8 - If there are no uncaught exceptions being thrown, 'throw;'
230 // should call terminate.
271 // 15.1.8 - If there are no exceptions being thrown, 'throw;' should call
272 // terminate.
231273 //
232274 __terminate(__terminate_handler);
233275
234 // Otherwise we have an exception to rethrow. Move it back to the uncaught
235 // stack.
236 CaughtExceptionStack = E->Next;
276 // Otherwise we have an exception to rethrow. Mark the exception as such.
277 E->isRethrown = 1;
278
279 // Add the exception to the top of the uncaught stack, to preserve the
280 // invariant that the top of the uncaught stack is the current exception.
237281 E->Next = UncaughtExceptionStack;
238282 UncaughtExceptionStack = E;
239
283
240284 // Return to the caller, which should perform the unwind now.
241285 }
242286
282326 // Whatever exception this is, it is not allowed by the (empty) spec, call
283327 // unexpected, according to 15.4.8.
284328 try {
285 __llvm_cxxeh_begin_catch(); // Start the catch
286 __llvm_cxxeh_end_catch(); // Free the exception
329 void *Ex = __llvm_cxxeh_begin_catch(); // Start the catch
330 __llvm_cxxeh_end_catch(Ex); // Free the exception
287331 __unexpected(__unexpected_handler);
288332 } catch (...) {
289333 // Any exception thrown by unexpected cannot match the ehspec. Call
306350 // permitted to pass through) or not a C++ exception that is allowed. Kill
307351 // the exception and call the unexpected handler.
308352 try {
309 __llvm_cxxeh_begin_catch(); // Start the catch
310 __llvm_cxxeh_end_catch(); // Free the exception
353 void *Ex = __llvm_cxxeh_begin_catch(); // Start the catch
354 __llvm_cxxeh_end_catch(Ex); // Free the exception
311355 } catch (...) {
312 __terminate(__terminate_handler); // Exception dtor threw
356 __terminate(__terminate_handler); // Exception dtor threw
313357 }
314358
315359 try {
5454 namespace __cxxabiv1 {
5555 // Invokes given handler, dying appropriately if the user handler was
5656 // so inconsiderate as to return.
57 extern void __terminate(std::terminate_handler) __attribute__((noreturn));
57 extern void __terminate(std::terminate_handler) throw() __attribute__((noreturn));
5858 extern void __unexpected(std::unexpected_handler) __attribute__((noreturn));
5959
6060 // The current installed user handlers.
7373 throw();
7474 void *__llvm_cxxeh_begin_catch() throw();
7575 void *__llvm_cxxeh_begin_catch_if_isa(void *CatchType) throw();
76 void __llvm_cxxeh_end_catch() /* might throw */;
76 void __llvm_cxxeh_end_catch(void *Exception) /* might throw */;
7777 void __llvm_cxxeh_rethrow() throw();
78 void *__llvm_cxxeh_get_last_caught() throw();
79
7880 void __llvm_cxxeh_check_eh_spec(void *Info, ...);
7981 }
8082
3030 // falls to zero, the exception is destroyed.
3131 //
3232 unsigned HandlerCount;
33
34 // isRethrown - This field is set on an exception if it has been 'throw;'n.
35 // This is needed because the exception might exit through a number of the
36 // end_catch statements matching the number of begin_catch statements that
37 // have been processed. When this happens, the exception should become
38 // uncaught, not dead.
39 //
40 int isRethrown;
3341 };
3442
3543 enum {