blob: b521f1464c6015a0648abc492e35aa1d5f2a6ded [file] [log] [blame]
Chris Lattner79b81bf2003-08-30 22:47:59 +00001//===- C++-Exception.cpp - Exception handling support for C++ exceptions --===//
Chris Lattnerbbaaa112003-08-25 22:35:36 +00002//
3// This file defines the methods used to implement C++ exception handling in
4// terms of the invoke and %llvm.unwind intrinsic. These primitives implement
5// an exception handling ABI similar (but simpler and more efficient than) the
6// Itanium C++ ABI exception handling standard.
7//
8//===----------------------------------------------------------------------===//
9
Chris Lattner79b81bf2003-08-30 22:47:59 +000010#include "C++-Exception.h"
Chris Lattnerbbaaa112003-08-25 22:35:36 +000011#include <cstdlib>
Chris Lattner9dddce62003-08-27 23:00:11 +000012#include <cstdarg>
Chris Lattnerbbaaa112003-08-25 22:35:36 +000013
Chris Lattner4f083082003-08-28 19:58:51 +000014//#define DEBUG
15
16#ifdef DEBUG
17#include <stdio.h>
18#endif
19
Chris Lattner4f083082003-08-28 19:58:51 +000020// LastCaughtException - The last exception caught by this handler. This is for
21// implementation of _rethrow and _get_last_caught.
22//
Chris Lattner8d907492003-08-30 23:17:51 +000023// FIXME: This should be thread-local!
24//
Chris Lattner4f083082003-08-28 19:58:51 +000025static llvm_exception *LastCaughtException = 0;
Chris Lattnerbbaaa112003-08-25 22:35:36 +000026
Chris Lattnerbbaaa112003-08-25 22:35:36 +000027
Chris Lattner9dddce62003-08-27 23:00:11 +000028using namespace __cxxabiv1;
Chris Lattnerbbaaa112003-08-25 22:35:36 +000029
30// __llvm_cxxeh_allocate_exception - This function allocates space for the
31// specified number of bytes, plus a C++ exception object header.
32//
Chris Lattner9dddce62003-08-27 23:00:11 +000033void *__llvm_cxxeh_allocate_exception(unsigned NumBytes) throw() {
Chris Lattnerbbaaa112003-08-25 22:35:36 +000034 // FIXME: This should eventually have back-up buffers for out-of-memory
35 // situations.
36 //
37 llvm_cxx_exception *E =
38 (llvm_cxx_exception *)malloc(NumBytes+sizeof(llvm_cxx_exception));
Misha Brukman8b2bd4e2003-10-10 17:57:28 +000039 E->BaseException.ExceptionType = 0; // initialize to invalid
Chris Lattnerbbaaa112003-08-25 22:35:36 +000040
41 return E+1; // return the pointer after the header
42}
43
44// __llvm_cxxeh_free_exception - Low-level function to free an exception. This
45// is called directly from generated C++ code if evaluating the exception value
46// into the exception location throws. Otherwise it is called from the C++
47// exception object destructor.
48//
Chris Lattner9dddce62003-08-27 23:00:11 +000049void __llvm_cxxeh_free_exception(void *ObjectPtr) throw() {
Chris Lattnerbbaaa112003-08-25 22:35:36 +000050 llvm_cxx_exception *E = (llvm_cxx_exception *)ObjectPtr - 1;
51 free(E);
52}
53
54// cxx_destructor - This function is called through the generic
55// exception->ExceptionDestructor function pointer to destroy a caught
56// exception.
57//
Chris Lattner9dddce62003-08-27 23:00:11 +000058static void cxx_destructor(llvm_exception *LE) /* might throw */{
Chris Lattner4f083082003-08-28 19:58:51 +000059 assert(LE->Next == 0 && "On the uncaught stack??");
Chris Lattner5e22eb12003-08-27 04:51:26 +000060 llvm_cxx_exception *E = get_cxx_exception(LE);
Chris Lattnerbbaaa112003-08-25 22:35:36 +000061
Chris Lattnerbbaaa112003-08-25 22:35:36 +000062 struct ExceptionFreer {
63 void *Ptr;
64 ExceptionFreer(void *P) : Ptr(P) {}
65 ~ExceptionFreer() {
66 // Free the memory for the exception, when the function is left, even if
67 // the exception object dtor throws its own exception!
68 __llvm_cxxeh_free_exception(Ptr);
69 }
70 } EF(E+1);
71
72 // Run the exception object dtor if it exists. */
73 if (E->ExceptionObjectDestructor)
74 E->ExceptionObjectDestructor(E);
75}
76
77// __llvm_cxxeh_throw - Given a pointer to memory which has an exception object
78// evaluated into it, this sets up all of the fields of the exception allowing
79// it to be thrown. After calling this, the code should call %llvm.unwind
80//
Chris Lattner9dddce62003-08-27 23:00:11 +000081void __llvm_cxxeh_throw(void *ObjectPtr, void *TypeInfoPtr,
82 void (*DtorPtr)(void*)) throw() {
Chris Lattnerbbaaa112003-08-25 22:35:36 +000083 llvm_cxx_exception *E = (llvm_cxx_exception *)ObjectPtr - 1;
84 E->BaseException.ExceptionDestructor = cxx_destructor;
85 E->BaseException.ExceptionType = CXXException;
Chris Lattnerbbaaa112003-08-25 22:35:36 +000086 E->BaseException.HandlerCount = 0;
Chris Lattner4f083082003-08-28 19:58:51 +000087 E->BaseException.isRethrown = 0;
Chris Lattnerbbaaa112003-08-25 22:35:36 +000088
Chris Lattner9dddce62003-08-27 23:00:11 +000089 E->TypeInfo = (const std::type_info*)TypeInfoPtr;
Chris Lattnerbbaaa112003-08-25 22:35:36 +000090 E->ExceptionObjectDestructor = DtorPtr;
Chris Lattner9dddce62003-08-27 23:00:11 +000091 E->UnexpectedHandler = __unexpected_handler;
92 E->TerminateHandler = __terminate_handler;
Chris Lattner8d907492003-08-30 23:17:51 +000093
94 __llvm_eh_add_uncaught_exception(&E->BaseException);
Chris Lattnerbbaaa112003-08-25 22:35:36 +000095}
96
Chris Lattner9dddce62003-08-27 23:00:11 +000097
98// CXXExceptionISA - use the type info object stored in the exception to see if
99// TypeID matches and, if so, to adjust the exception object pointer.
Chris Lattnerbbaaa112003-08-25 22:35:36 +0000100//
Chris Lattner4f083082003-08-28 19:58:51 +0000101static void *CXXExceptionISA(llvm_cxx_exception *E,
102 const std::type_info *Type) throw() {
Chris Lattnerbbaaa112003-08-25 22:35:36 +0000103 // ThrownPtr is a pointer to the object being thrown...
104 void *ThrownPtr = E+1;
105 const std::type_info *ThrownType = E->TypeInfo;
106
Chris Lattner4f083082003-08-28 19:58:51 +0000107#if 0
Chris Lattnerbbaaa112003-08-25 22:35:36 +0000108 // FIXME: this code exists in the GCC exception handling library: I haven't
109 // thought about this yet, so it should be verified at some point!
Chris Lattner4f083082003-08-28 19:58:51 +0000110
Chris Lattnerbbaaa112003-08-25 22:35:36 +0000111 // Pointer types need to adjust the actual pointer, not
112 // the pointer to pointer that is the exception object.
113 // This also has the effect of passing pointer types
114 // "by value" through the __cxa_begin_catch return value.
115 if (ThrownType->__is_pointer_p())
116 ThrownPtr = *(void **)ThrownPtr;
117#endif
118
Chris Lattner4f083082003-08-28 19:58:51 +0000119 if (Type->__do_catch(ThrownType, &ThrownPtr, 1)) {
120#ifdef DEBUG
121 printf("isa<%s>(%s): 0x%p -> 0x%p\n", Type->name(), ThrownType->name(),
122 E+1, ThrownPtr);
123#endif
Chris Lattnerbbaaa112003-08-25 22:35:36 +0000124 return ThrownPtr;
Chris Lattner4f083082003-08-28 19:58:51 +0000125 }
Chris Lattnerbbaaa112003-08-25 22:35:36 +0000126
127 return 0;
128}
129
Chris Lattner9dddce62003-08-27 23:00:11 +0000130// __llvm_cxxeh_current_uncaught_exception_isa - This function checks to see if
131// the current uncaught exception is a C++ exception, and if it is of the
132// specified type id. If so, it returns a pointer to the object adjusted as
133// appropriate, otherwise it returns null.
134//
135void *__llvm_cxxeh_current_uncaught_exception_isa(void *CatchType) throw() {
Chris Lattner8d907492003-08-30 23:17:51 +0000136 void *EPtr = __llvm_eh_current_uncaught_exception_type(CXXException);
137 if (EPtr == 0) return 0; // If it's not a c++ exception, it doesn't match!
Chris Lattner9dddce62003-08-27 23:00:11 +0000138
139 // If it is a C++ exception, use the type info object stored in the exception
140 // to see if TypeID matches and, if so, to adjust the exception object
141 // pointer.
142 //
143 const std::type_info *Info = (const std::type_info *)CatchType;
Chris Lattner8d907492003-08-30 23:17:51 +0000144 return CXXExceptionISA((llvm_cxx_exception*)EPtr - 1, Info);
Chris Lattner9dddce62003-08-27 23:00:11 +0000145}
146
Chris Lattnerbbaaa112003-08-25 22:35:36 +0000147
Chris Lattner2b3383252003-08-26 23:46:53 +0000148// __llvm_cxxeh_begin_catch - This function is called by "exception handlers",
149// which transition an exception from being uncaught to being caught. It
150// returns a pointer to the exception object portion of the exception. This
151// function must work with foreign exceptions.
152//
Chris Lattner9dddce62003-08-27 23:00:11 +0000153void *__llvm_cxxeh_begin_catch() throw() {
Chris Lattner8d907492003-08-30 23:17:51 +0000154 llvm_exception *E = __llvm_eh_pop_from_uncaught_stack();
Chris Lattnerbbaaa112003-08-25 22:35:36 +0000155
Chris Lattner2b3383252003-08-26 23:46:53 +0000156 // The exception is now caught.
Chris Lattner4f083082003-08-28 19:58:51 +0000157 LastCaughtException = E;
158 E->Next = 0;
159 E->isRethrown = 0;
Chris Lattnerbbaaa112003-08-25 22:35:36 +0000160
Chris Lattner2b3383252003-08-26 23:46:53 +0000161 // Increment the handler count for this exception.
Chris Lattnerbbaaa112003-08-25 22:35:36 +0000162 E->HandlerCount++;
Chris Lattner4f083082003-08-28 19:58:51 +0000163
164#ifdef DEBUG
165 printf("Exiting begin_catch Ex=0x%p HandlerCount=%d!\n", E+1,
166 E->HandlerCount);
167#endif
Chris Lattner2b3383252003-08-26 23:46:53 +0000168
169 // Return a pointer to the raw exception object.
Chris Lattnerbbaaa112003-08-25 22:35:36 +0000170 return E+1;
171}
172
Chris Lattner2b3383252003-08-26 23:46:53 +0000173// __llvm_cxxeh_begin_catch_if_isa - This function checks to see if the current
174// uncaught exception is of the specified type. If not, it returns a null
175// pointer, otherwise it 'catches' the exception and returns a pointer to the
176// object of the specified type. This function does never succeeds with foreign
177// exceptions (because they can never be of type CatchType).
178//
Chris Lattner9dddce62003-08-27 23:00:11 +0000179void *__llvm_cxxeh_begin_catch_if_isa(void *CatchType) throw() {
Chris Lattner2b3383252003-08-26 23:46:53 +0000180 void *ObjPtr = __llvm_cxxeh_current_uncaught_exception_isa(CatchType);
181 if (!ObjPtr) return 0;
182
183 // begin_catch, meaning that the object is now "caught", not "uncaught"
184 __llvm_cxxeh_begin_catch();
185 return ObjPtr;
186}
187
Chris Lattner4f083082003-08-28 19:58:51 +0000188// __llvm_cxxeh_get_last_caught - Return the last exception that was caught by
189// ...begin_catch.
190//
191void *__llvm_cxxeh_get_last_caught() throw() {
192 assert(LastCaughtException && "No exception caught!!");
193 return LastCaughtException+1;
194}
Chris Lattner2b3383252003-08-26 23:46:53 +0000195
196// __llvm_cxxeh_end_catch - This function decrements the HandlerCount of the
197// top-level caught exception, destroying it if this is the last handler for the
198// exception.
199//
Chris Lattner4f083082003-08-28 19:58:51 +0000200void __llvm_cxxeh_end_catch(void *Ex) /* might throw */ {
201 llvm_exception *E = (llvm_exception*)Ex - 1;
Chris Lattnerbbaaa112003-08-25 22:35:36 +0000202 assert(E && "There are no caught exceptions!");
203
Chris Lattner2b3383252003-08-26 23:46:53 +0000204 // If this is the last handler using the exception, destroy it now!
Chris Lattner4f083082003-08-28 19:58:51 +0000205 if (--E->HandlerCount == 0 && !E->isRethrown) {
206#ifdef DEBUG
207 printf("Destroying exception!\n");
208#endif
Chris Lattner2b3383252003-08-26 23:46:53 +0000209 E->ExceptionDestructor(E); // Release memory for the exception
Chris Lattner4f083082003-08-28 19:58:51 +0000210 }
211#ifdef DEBUG
212 printf("Exiting end_catch Ex=0x%p HandlerCount=%d!\n", Ex, E->HandlerCount);
213#endif
Chris Lattnerbbaaa112003-08-25 22:35:36 +0000214}
215
Chris Lattner4f083082003-08-28 19:58:51 +0000216// __llvm_cxxeh_call_terminate - This function is called when the dtor for an
217// object being destroyed due to an exception throw throws an exception. This
218// is illegal because it would cause multiple exceptions to be active at one
219// time.
Chris Lattner3926a832003-08-28 14:35:52 +0000220void __llvm_cxxeh_call_terminate() throw() {
Chris Lattner4f083082003-08-28 19:58:51 +0000221 void (*Handler)(void) = __terminate_handler;
Chris Lattner8d907492003-08-30 23:17:51 +0000222 if (__llvm_eh_has_uncaught_exception())
223 if (void *EPtr = __llvm_eh_current_uncaught_exception_type(CXXException))
224 Handler = ((llvm_cxx_exception*)EPtr - 1)->TerminateHandler;
Chris Lattner4f083082003-08-28 19:58:51 +0000225 __terminate(Handler);
Chris Lattner3926a832003-08-28 14:35:52 +0000226}
227
Chris Lattner9dddce62003-08-27 23:00:11 +0000228
Chris Lattner2b3383252003-08-26 23:46:53 +0000229// __llvm_cxxeh_rethrow - This function turns the top-level caught exception
230// into an uncaught exception, in preparation for an llvm.unwind, which should
231// follow immediately after the call to this function. This function must be
232// prepared to deal with foreign exceptions.
233//
Chris Lattner9dddce62003-08-27 23:00:11 +0000234void __llvm_cxxeh_rethrow() throw() {
Chris Lattner4f083082003-08-28 19:58:51 +0000235 llvm_exception *E = LastCaughtException;
Chris Lattner9dddce62003-08-27 23:00:11 +0000236 if (E == 0)
Chris Lattner4f083082003-08-28 19:58:51 +0000237 // 15.1.8 - If there are no exceptions being thrown, 'throw;' should call
238 // terminate.
Chris Lattner2b3383252003-08-26 23:46:53 +0000239 //
Chris Lattner9dddce62003-08-27 23:00:11 +0000240 __terminate(__terminate_handler);
Chris Lattnerbbaaa112003-08-25 22:35:36 +0000241
Chris Lattner4f083082003-08-28 19:58:51 +0000242 // Otherwise we have an exception to rethrow. Mark the exception as such.
243 E->isRethrown = 1;
244
245 // Add the exception to the top of the uncaught stack, to preserve the
246 // invariant that the top of the uncaught stack is the current exception.
Chris Lattner8d907492003-08-30 23:17:51 +0000247 __llvm_eh_add_uncaught_exception(E);
Chris Lattner4f083082003-08-28 19:58:51 +0000248
Chris Lattner2b3383252003-08-26 23:46:53 +0000249 // Return to the caller, which should perform the unwind now.
Chris Lattnerbbaaa112003-08-25 22:35:36 +0000250}
251
Chris Lattner9dddce62003-08-27 23:00:11 +0000252static bool ExceptionSpecificationPermitsException(llvm_exception *E,
253 const std::type_info *Info,
254 va_list Args) {
255 // The only way it could match one of the types is if it is a C++ exception.
256 if (E->ExceptionType != CXXException) return false;
257
258 llvm_cxx_exception *Ex = get_cxx_exception(E);
259
260 // Scan the list of accepted types, checking to see if the uncaught
261 // exception is any of them.
262 do {
263 // Check to see if the exception matches one of the types allowed by the
264 // exception specification. If so, return to the caller to have the
265 // exception rethrown.
266 if (CXXExceptionISA(Ex, Info))
267 return true;
268
269 Info = va_arg(Args, std::type_info *);
270 } while (Info);
271 return false;
272}
273
274
275// __llvm_cxxeh_check_eh_spec - If a function with an exception specification is
276// throwing an exception, this function gets called with the list of type_info
277// objects that it is allowing to propagate. Check to see if the current
278// uncaught exception is one of these types, and if so, allow it to be thrown by
279// returning to the caller, which should immediately follow this call with
280// llvm.unwind.
281//
282// Note that this function does not throw any exceptions, but we can't put an
283// exception specification on it or else we'll get infinite loops!
284//
285void __llvm_cxxeh_check_eh_spec(void *Info, ...) {
286 const std::type_info *TypeInfo = (const std::type_info *)Info;
Chris Lattner9dddce62003-08-27 23:00:11 +0000287
288 if (TypeInfo == 0) { // Empty exception specification
289 // Whatever exception this is, it is not allowed by the (empty) spec, call
290 // unexpected, according to 15.4.8.
291 try {
Chris Lattner4f083082003-08-28 19:58:51 +0000292 void *Ex = __llvm_cxxeh_begin_catch(); // Start the catch
293 __llvm_cxxeh_end_catch(Ex); // Free the exception
Chris Lattner9dddce62003-08-27 23:00:11 +0000294 __unexpected(__unexpected_handler);
295 } catch (...) {
296 // Any exception thrown by unexpected cannot match the ehspec. Call
297 // terminate, according to 15.4.9.
298 __terminate(__terminate_handler);
299 }
300 }
301
Chris Lattner8d907492003-08-30 23:17:51 +0000302 llvm_exception *E = __llvm_eh_get_uncaught_exception();
303 assert(E && "No uncaught exceptions!");
304
Chris Lattner9dddce62003-08-27 23:00:11 +0000305 // Check to see if the exception matches one of the types allowed by the
306 // exception specification. If so, return to the caller to have the
307 // exception rethrown.
308
309 va_list Args;
310 va_start(Args, Info);
311 bool Ok = ExceptionSpecificationPermitsException(E, TypeInfo, Args);
312 va_end(Args);
313 if (Ok) return;
314
315 // Ok, now we know that the exception is either not a C++ exception (thus not
316 // permitted to pass through) or not a C++ exception that is allowed. Kill
317 // the exception and call the unexpected handler.
318 try {
Chris Lattner4f083082003-08-28 19:58:51 +0000319 void *Ex = __llvm_cxxeh_begin_catch(); // Start the catch
320 __llvm_cxxeh_end_catch(Ex); // Free the exception
Chris Lattner9dddce62003-08-27 23:00:11 +0000321 } catch (...) {
Chris Lattner4f083082003-08-28 19:58:51 +0000322 __terminate(__terminate_handler); // Exception dtor threw
Chris Lattner9dddce62003-08-27 23:00:11 +0000323 }
324
325 try {
326 __unexpected(__unexpected_handler);
327 } catch (...) {
328 // If the unexpected handler threw an exception, we will get here. Since
329 // entering the try block calls ..._begin_catch, we need to "rethrow" the
330 // exception to make it uncaught again. Exiting the catch will then leave
331 // it in the uncaught state.
332 __llvm_cxxeh_rethrow();
333 }
334
335 // Grab the newly caught exception. If this exception is permitted by the
336 // specification, allow it to be thrown.
Chris Lattner8d907492003-08-30 23:17:51 +0000337 E = __llvm_eh_get_uncaught_exception();
Chris Lattner9dddce62003-08-27 23:00:11 +0000338
339 va_start(Args, Info);
340 Ok = ExceptionSpecificationPermitsException(E, TypeInfo, Args);
341 va_end(Args);
342 if (Ok) return;
343
344 // Final case, check to see if we can throw an std::bad_exception.
345 try {
346 throw std::bad_exception();
347 } catch (...) {
348 __llvm_cxxeh_rethrow();
349 }
350
351 // Grab the new bad_exception...
Chris Lattner8d907492003-08-30 23:17:51 +0000352 E = __llvm_eh_get_uncaught_exception();
Chris Lattner9dddce62003-08-27 23:00:11 +0000353
354 // If it's permitted, allow it to be thrown instead.
355 va_start(Args, Info);
356 Ok = ExceptionSpecificationPermitsException(E, TypeInfo, Args);
357 va_end(Args);
358 if (Ok) return;
359
360 // Otherwise, we are out of options, terminate, according to 15.5.2.2.
361 __terminate(__terminate_handler);
362}