exception.c revision 1.1.1.6 1 /* The implementation of exception handling primitives for Objective-C.
2 Copyright (C) 2004-2018 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
15
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
19
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
24
25 #include "objc-private/common.h"
26 #include <stdlib.h>
27 #include "config.h"
28 #include "objc/runtime.h"
29 #include "objc/objc-exception.h"
30 #include "unwind.h"
31 #include "unwind-pe.h"
32 #include <string.h> /* For memcpy */
33
34 /* 'is_kind_of_exception_matcher' is our default exception matcher -
35 it determines if the object 'exception' is of class 'catch_class',
36 or of a subclass. */
37 static int
38 is_kind_of_exception_matcher (Class catch_class, id exception)
39 {
40 /* NULL catch_class is catch-all (eg, @catch (id object)). */
41 if (catch_class == Nil)
42 return 1;
43
44 /* If exception is nil (eg, @throw nil;), then it can only be
45 catched by a catch-all (eg, @catch (id object)). */
46 if (exception != nil)
47 {
48 Class c;
49
50 for (c = exception->class_pointer; c != Nil;
51 c = class_getSuperclass (c))
52 if (c == catch_class)
53 return 1;
54 }
55 return 0;
56 }
57
58 /* The exception matcher currently in use. */
59 static objc_exception_matcher
60 __objc_exception_matcher = is_kind_of_exception_matcher;
61
62 objc_exception_matcher
63 objc_setExceptionMatcher (objc_exception_matcher new_matcher)
64 {
65 objc_exception_matcher old_matcher = __objc_exception_matcher;
66 __objc_exception_matcher = new_matcher;
67 return old_matcher;
68 }
69
70 /* The uncaught exception handler currently in use. */
71 static objc_uncaught_exception_handler
72 __objc_uncaught_exception_handler = NULL;
73
74 objc_uncaught_exception_handler
75 objc_setUncaughtExceptionHandler (objc_uncaught_exception_handler
76 new_handler)
77 {
78 objc_uncaught_exception_handler old_handler
79 = __objc_uncaught_exception_handler;
80 __objc_uncaught_exception_handler = new_handler;
81 return old_handler;
82 }
83
84
85
86 #ifdef __ARM_EABI_UNWINDER__
88
89 const _Unwind_Exception_Class __objc_exception_class
90 = {'G', 'N', 'U', 'C', 'O', 'B', 'J', 'C'};
91
92 #else
93
94 /* This is the exception class we report -- "GNUCOBJC". */
95 static const _Unwind_Exception_Class __objc_exception_class
96 = ((((((((_Unwind_Exception_Class) 'G'
97 << 8 | (_Unwind_Exception_Class) 'N')
98 << 8 | (_Unwind_Exception_Class) 'U')
99 << 8 | (_Unwind_Exception_Class) 'C')
100 << 8 | (_Unwind_Exception_Class) 'O')
101 << 8 | (_Unwind_Exception_Class) 'B')
102 << 8 | (_Unwind_Exception_Class) 'J')
103 << 8 | (_Unwind_Exception_Class) 'C');
104
105 #endif
106
107 /* This is the object that is passed around by the Objective C runtime
108 to represent the exception in flight. */
109 struct ObjcException
110 {
111 /* This bit is needed in order to interact with the unwind runtime. */
112 struct _Unwind_Exception base;
113
114 /* The actual object we want to throw. Note: must come immediately
115 after unwind header. */
116 id value;
117
118 #ifdef __ARM_EABI_UNWINDER__
119 /* Note: we use the barrier cache defined in the unwind control
120 block for ARM EABI. */
121 #else
122 /* Cache some internal unwind data between phase 1 and phase 2. */
123 _Unwind_Ptr landingPad;
124 int handlerSwitchValue;
125 #endif
126 };
127
128
129
131 struct lsda_header_info
132 {
133 _Unwind_Ptr Start;
134 _Unwind_Ptr LPStart;
135 _Unwind_Ptr ttype_base;
136 const unsigned char *TType;
137 const unsigned char *action_table;
138 unsigned char ttype_encoding;
139 unsigned char call_site_encoding;
140 };
141
142 static const unsigned char *
143 parse_lsda_header (struct _Unwind_Context *context, const unsigned char *p,
144 struct lsda_header_info *info)
145 {
146 _uleb128_t tmp;
147 unsigned char lpstart_encoding;
148
149 info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
150
151 /* Find @LPStart, the base to which landing pad offsets are
152 relative. */
153 lpstart_encoding = *p++;
154 if (lpstart_encoding != DW_EH_PE_omit)
155 p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
156 else
157 info->LPStart = info->Start;
158
159 /* Find @TType, the base of the handler and exception spec type
160 data. */
161 info->ttype_encoding = *p++;
162 if (info->ttype_encoding != DW_EH_PE_omit)
163 {
164 #if _GLIBCXX_OVERRIDE_TTYPE_ENCODING
165 /* Older ARM EABI toolchains set this value incorrectly, so use a
166 hardcoded OS-specific format. */
167 info->ttype_encoding = _GLIBCXX_OVERRIDE_TTYPE_ENCODING;
168 #endif
169 p = read_uleb128 (p, &tmp);
170 info->TType = p + tmp;
171 }
172 else
173 info->TType = 0;
174
175 /* The encoding and length of the call-site table; the action table
176 immediately follows. */
177 info->call_site_encoding = *p++;
178 p = read_uleb128 (p, &tmp);
179 info->action_table = p + tmp;
180
181 return p;
182 }
183
184 static Class
185 get_ttype_entry (struct lsda_header_info *info, _Unwind_Word i)
186 {
187 _Unwind_Ptr ptr;
188
189 i *= size_of_encoded_value (info->ttype_encoding);
190 read_encoded_value_with_base (info->ttype_encoding, info->ttype_base,
191 info->TType - i, &ptr);
192
193 /* NULL ptr means catch-all. Note that if the class is not found,
194 this will abort the program. */
195 if (ptr)
196 return objc_getRequiredClass ((const char *) ptr);
197 else
198 return 0;
199 }
200
201 /* Using a different personality function name causes link failures
202 when trying to mix code using different exception handling
203 models. */
204 #ifdef __USING_SJLJ_EXCEPTIONS__
205 #define PERSONALITY_FUNCTION __gnu_objc_personality_sj0
206 #define __builtin_eh_return_data_regno(x) x
207 #elif defined(__SEH__)
208 #define PERSONALITY_FUNCTION __gnu_objc_personality_imp
209 #else
210 #define PERSONALITY_FUNCTION __gnu_objc_personality_v0
211 #endif
212
213 #ifdef __ARM_EABI_UNWINDER__
214
215 #define CONTINUE_UNWINDING \
216 do \
217 { \
218 if (__gnu_unwind_frame(ue_header, context) != _URC_OK) \
219 return _URC_FAILURE; \
220 return _URC_CONTINUE_UNWIND; \
221 } \
222 while (0)
223
224 _Unwind_Reason_Code
225 PERSONALITY_FUNCTION (_Unwind_State state,
226 struct _Unwind_Exception *ue_header,
227 struct _Unwind_Context *context)
228 #else
229
230 #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
231
232 #if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
233 static
234 #endif
235 _Unwind_Reason_Code
236 PERSONALITY_FUNCTION (int version,
237 _Unwind_Action actions,
238 _Unwind_Exception_Class exception_class,
239 struct _Unwind_Exception *ue_header,
240 struct _Unwind_Context *context)
241 #endif
242 {
243 struct ObjcException *xh = (struct ObjcException *) ue_header;
244
245 struct lsda_header_info info;
246 const unsigned char *language_specific_data;
247 const unsigned char *action_record;
248 const unsigned char *p;
249 _Unwind_Ptr landing_pad, ip;
250 int handler_switch_value;
251 int saw_cleanup = 0, saw_handler, foreign_exception;
252 void *return_object;
253 int ip_before_insn = 0;
254
255 #ifdef __ARM_EABI_UNWINDER__
256 _Unwind_Action actions;
257
258 switch (state & _US_ACTION_MASK)
259 {
260 case _US_VIRTUAL_UNWIND_FRAME:
261 actions = _UA_SEARCH_PHASE;
262 break;
263
264 case _US_UNWIND_FRAME_STARTING:
265 actions = _UA_CLEANUP_PHASE;
266 if (!(state & _US_FORCE_UNWIND)
267 && ue_header->barrier_cache.sp == _Unwind_GetGR (context, 13))
268 actions |= _UA_HANDLER_FRAME;
269 break;
270
271 case _US_UNWIND_FRAME_RESUME:
272 CONTINUE_UNWINDING;
273 break;
274
275 default:
276 abort();
277 }
278 actions |= state & _US_FORCE_UNWIND;
279
280 /* TODO: Foreign exceptions need some attention (e.g. rethrowing
281 doesn't work). */
282 foreign_exception = 0;
283
284 /* The dwarf unwinder assumes the context structure holds things
285 like the function and LSDA pointers. The ARM implementation
286 caches these in the exception header (UCB). To avoid rewriting
287 everything we make the virtual IP register point at the UCB. */
288 ip = (_Unwind_Ptr) ue_header;
289 _Unwind_SetGR (context, 12, ip);
290
291 #else /* !__ARM_EABI_UNWINDER. */
292 /* Interface version check. */
293 if (version != 1)
294 return _URC_FATAL_PHASE1_ERROR;
295
296 foreign_exception = (exception_class != __objc_exception_class);
297 #endif
298
299 /* Shortcut for phase 2 found handler for domestic exception. */
300 if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
301 && !foreign_exception)
302 {
303 #ifdef __ARM_EABI_UNWINDER__
304 handler_switch_value = (int) ue_header->barrier_cache.bitpattern[1];
305 landing_pad = (_Unwind_Ptr) ue_header->barrier_cache.bitpattern[3];
306 #else
307 handler_switch_value = xh->handlerSwitchValue;
308 landing_pad = xh->landingPad;
309 #endif
310 goto install_context;
311 }
312
313 language_specific_data = (const unsigned char *)
314 _Unwind_GetLanguageSpecificData (context);
315
316 /* If no LSDA, then there are no handlers or cleanups. */
317 if (! language_specific_data)
318 CONTINUE_UNWINDING;
319
320 /* Parse the LSDA header. */
321 p = parse_lsda_header (context, language_specific_data, &info);
322 info.ttype_base = base_of_encoded_value (info.ttype_encoding, context);
323 #ifdef HAVE_GETIPINFO
324 ip = _Unwind_GetIPInfo (context, &ip_before_insn);
325 #else
326 ip = _Unwind_GetIP (context);
327 #endif
328 if (!ip_before_insn)
329 --ip;
330 landing_pad = 0;
331 action_record = 0;
332 handler_switch_value = 0;
333
334 #ifdef __USING_SJLJ_EXCEPTIONS__
335 /* The given "IP" is an index into the call-site table, with two
336 exceptions -- -1 means no-action, and 0 means terminate. But
337 since we're using uleb128 values, we've not got random access to
338 the array. */
339 if ((int) ip < 0)
340 return _URC_CONTINUE_UNWIND;
341 else
342 {
343 _uleb128_t cs_lp, cs_action;
344 do
345 {
346 p = read_uleb128 (p, &cs_lp);
347 p = read_uleb128 (p, &cs_action);
348 }
349 while (--ip);
350
351 /* Can never have null landing pad for sjlj -- that would have
352 been indicated by a -1 call site index. */
353 landing_pad = cs_lp + 1;
354 if (cs_action)
355 action_record = info.action_table + cs_action - 1;
356 goto found_something;
357 }
358 #else
359 /* Search the call-site table for the action associated with this
360 IP. */
361 while (p < info.action_table)
362 {
363 _Unwind_Ptr cs_start, cs_len, cs_lp;
364 _uleb128_t cs_action;
365
366 /* Note that all call-site encodings are "absolute"
367 displacements. */
368 p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
369 p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
370 p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
371 p = read_uleb128 (p, &cs_action);
372
373 /* The table is sorted, so if we've passed the ip, stop. */
374 if (ip < info.Start + cs_start)
375 p = info.action_table;
376 else if (ip < info.Start + cs_start + cs_len)
377 {
378 if (cs_lp)
379 landing_pad = info.LPStart + cs_lp;
380 if (cs_action)
381 action_record = info.action_table + cs_action - 1;
382 goto found_something;
383 }
384 }
385 #endif /* __USING_SJLJ_EXCEPTIONS__ */
386
387 /* If ip is not present in the table, C++ would call terminate. */
388 /* ??? As with Java, it's perhaps better to tweek the LSDA to that
389 no-action is mapped to no-entry. */
390 CONTINUE_UNWINDING;
391
392 found_something:
393 saw_cleanup = 0;
394 saw_handler = 0;
395
396 if (landing_pad == 0)
397 {
398 /* If ip is present, and has a null landing pad, there are no
399 cleanups or handlers to be run. */
400 }
401 else if (action_record == 0)
402 {
403 /* If ip is present, has a non-null landing pad, and a null
404 action table offset, then there are only cleanups present.
405 Cleanups use a zero switch value, as set above. */
406 saw_cleanup = 1;
407 }
408 else
409 {
410 /* Otherwise we have a catch handler. */
411 _sleb128_t ar_filter, ar_disp;
412
413 while (1)
414 {
415 p = action_record;
416 p = read_sleb128 (p, &ar_filter);
417 read_sleb128 (p, &ar_disp);
418
419 if (ar_filter == 0)
420 {
421 /* Zero filter values are cleanups. */
422 saw_cleanup = 1;
423 }
424
425 /* During forced unwinding, we only run cleanups. With a
426 foreign exception class, we have no class info to
427 match. */
428 else if ((actions & _UA_FORCE_UNWIND) || foreign_exception)
429 ;
430
431 else if (ar_filter > 0)
432 {
433 /* Positive filter values are handlers. */
434 Class catch_type = get_ttype_entry (&info, ar_filter);
435
436 if ((*__objc_exception_matcher) (catch_type, xh->value))
437 {
438 handler_switch_value = ar_filter;
439 saw_handler = 1;
440 break;
441 }
442 }
443 else
444 {
445 /* Negative filter values are exception specifications,
446 which Objective-C does not use. */
447 abort ();
448 }
449
450 if (ar_disp == 0)
451 break;
452 action_record = p + ar_disp;
453 }
454 }
455
456 if (! saw_handler && ! saw_cleanup)
457 CONTINUE_UNWINDING;
458
459 if (actions & _UA_SEARCH_PHASE)
460 {
461 if (!saw_handler)
462 CONTINUE_UNWINDING;
463
464 /* For domestic exceptions, we cache data from phase 1 for phase
465 2. */
466 if (!foreign_exception)
467 {
468 #ifdef __ARM_EABI_UNWINDER__
469 ue_header->barrier_cache.sp = _Unwind_GetGR (context, 13);
470 ue_header->barrier_cache.bitpattern[1] = (_uw) handler_switch_value;
471 ue_header->barrier_cache.bitpattern[3] = (_uw) landing_pad;
472 #else
473 xh->handlerSwitchValue = handler_switch_value;
474 xh->landingPad = landing_pad;
475 #endif
476 }
477 return _URC_HANDLER_FOUND;
478 }
479
480 install_context:
481 if (saw_cleanup == 0)
482 {
483 return_object = xh->value;
484 if (!(actions & _UA_SEARCH_PHASE))
485 _Unwind_DeleteException(&xh->base);
486 }
487
488 _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
489 __builtin_extend_pointer (saw_cleanup ? xh : return_object));
490 _Unwind_SetGR (context, __builtin_eh_return_data_regno (1),
491 handler_switch_value);
492 _Unwind_SetIP (context, landing_pad);
493 return _URC_INSTALL_CONTEXT;
494 }
495
496 static void
497 __objc_exception_cleanup (_Unwind_Reason_Code code __attribute__((unused)),
498 struct _Unwind_Exception *exc)
499 {
500 free (exc);
501 }
502
503 void
504 objc_exception_throw (id exception)
505 {
506 struct ObjcException *header = calloc (1, sizeof (*header));
507
508 memcpy (&header->base.exception_class, &__objc_exception_class,
509 sizeof (__objc_exception_class));
510 header->base.exception_cleanup = __objc_exception_cleanup;
511 header->value = exception;
512
513 #ifdef __USING_SJLJ_EXCEPTIONS__
514 _Unwind_SjLj_RaiseException (&header->base);
515 #else
516 _Unwind_RaiseException (&header->base);
517 #endif
518
519 /* No exception handler was installed. Call the uncaught exception
520 handler if any is defined. */
521 if (__objc_uncaught_exception_handler != 0)
522 {
523 (*__objc_uncaught_exception_handler) (exception);
524 }
525
526 abort ();
527 }
528
529 #if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
530 EXCEPTION_DISPOSITION
531 __gnu_objc_personality_seh0 (PEXCEPTION_RECORD ms_exc, void *this_frame,
532 PCONTEXT ms_orig_context,
533 PDISPATCHER_CONTEXT ms_disp)
534 {
535 return _GCC_specific_handler (ms_exc, this_frame, ms_orig_context,
536 ms_disp, __gnu_objc_personality_imp);
537 }
538 #endif
539