exception.c revision 1.1.1.9 1 /* The implementation of exception handling primitives for Objective-C.
2 Copyright (C) 2004-2019 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 __attribute__((target ("general-regs-only")))
226 PERSONALITY_FUNCTION (_Unwind_State state,
227 struct _Unwind_Exception *ue_header,
228 struct _Unwind_Context *context)
229 #else
230
231 #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
232
233 #if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
234 static
235 #endif
236 _Unwind_Reason_Code
237 PERSONALITY_FUNCTION (int version,
238 _Unwind_Action actions,
239 _Unwind_Exception_Class exception_class,
240 struct _Unwind_Exception *ue_header,
241 struct _Unwind_Context *context)
242 #endif
243 {
244 struct ObjcException *xh = (struct ObjcException *) ue_header;
245
246 struct lsda_header_info info;
247 const unsigned char *language_specific_data;
248 const unsigned char *action_record;
249 const unsigned char *p;
250 _Unwind_Ptr landing_pad, ip;
251 int handler_switch_value;
252 int saw_cleanup = 0, saw_handler, foreign_exception;
253 void *return_object;
254 int ip_before_insn = 0;
255
256 #ifdef __ARM_EABI_UNWINDER__
257 _Unwind_Action actions;
258
259 switch (state & _US_ACTION_MASK)
260 {
261 case _US_VIRTUAL_UNWIND_FRAME:
262 actions = _UA_SEARCH_PHASE;
263 break;
264
265 case _US_UNWIND_FRAME_STARTING:
266 actions = _UA_CLEANUP_PHASE;
267 if (!(state & _US_FORCE_UNWIND)
268 && ue_header->barrier_cache.sp == _Unwind_GetGR (context, 13))
269 actions |= _UA_HANDLER_FRAME;
270 break;
271
272 case _US_UNWIND_FRAME_RESUME:
273 CONTINUE_UNWINDING;
274 break;
275
276 default:
277 abort();
278 }
279 actions |= state & _US_FORCE_UNWIND;
280
281 /* TODO: Foreign exceptions need some attention (e.g. rethrowing
282 doesn't work). */
283 foreign_exception = 0;
284
285 /* The dwarf unwinder assumes the context structure holds things
286 like the function and LSDA pointers. The ARM implementation
287 caches these in the exception header (UCB). To avoid rewriting
288 everything we make the virtual IP register point at the UCB. */
289 ip = (_Unwind_Ptr) ue_header;
290 _Unwind_SetGR (context, 12, ip);
291
292 #else /* !__ARM_EABI_UNWINDER. */
293 /* Interface version check. */
294 if (version != 1)
295 return _URC_FATAL_PHASE1_ERROR;
296
297 foreign_exception = (exception_class != __objc_exception_class);
298 #endif
299
300 /* Shortcut for phase 2 found handler for domestic exception. */
301 if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
302 && !foreign_exception)
303 {
304 #ifdef __ARM_EABI_UNWINDER__
305 handler_switch_value = (int) ue_header->barrier_cache.bitpattern[1];
306 landing_pad = (_Unwind_Ptr) ue_header->barrier_cache.bitpattern[3];
307 #else
308 handler_switch_value = xh->handlerSwitchValue;
309 landing_pad = xh->landingPad;
310 #endif
311 goto install_context;
312 }
313
314 language_specific_data = (const unsigned char *)
315 _Unwind_GetLanguageSpecificData (context);
316
317 /* If no LSDA, then there are no handlers or cleanups. */
318 if (! language_specific_data)
319 CONTINUE_UNWINDING;
320
321 /* Parse the LSDA header. */
322 p = parse_lsda_header (context, language_specific_data, &info);
323 info.ttype_base = base_of_encoded_value (info.ttype_encoding, context);
324 #ifdef HAVE_GETIPINFO
325 ip = _Unwind_GetIPInfo (context, &ip_before_insn);
326 #else
327 ip = _Unwind_GetIP (context);
328 #endif
329 if (!ip_before_insn)
330 --ip;
331 landing_pad = 0;
332 action_record = 0;
333 handler_switch_value = 0;
334
335 #ifdef __USING_SJLJ_EXCEPTIONS__
336 /* The given "IP" is an index into the call-site table, with two
337 exceptions -- -1 means no-action, and 0 means terminate. But
338 since we're using uleb128 values, we've not got random access to
339 the array. */
340 if ((int) ip < 0)
341 return _URC_CONTINUE_UNWIND;
342 else
343 {
344 _uleb128_t cs_lp, cs_action;
345 do
346 {
347 p = read_uleb128 (p, &cs_lp);
348 p = read_uleb128 (p, &cs_action);
349 }
350 while (--ip);
351
352 /* Can never have null landing pad for sjlj -- that would have
353 been indicated by a -1 call site index. */
354 landing_pad = cs_lp + 1;
355 if (cs_action)
356 action_record = info.action_table + cs_action - 1;
357 goto found_something;
358 }
359 #else
360 /* Search the call-site table for the action associated with this
361 IP. */
362 while (p < info.action_table)
363 {
364 _Unwind_Ptr cs_start, cs_len, cs_lp;
365 _uleb128_t cs_action;
366
367 /* Note that all call-site encodings are "absolute"
368 displacements. */
369 p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
370 p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
371 p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
372 p = read_uleb128 (p, &cs_action);
373
374 /* The table is sorted, so if we've passed the ip, stop. */
375 if (ip < info.Start + cs_start)
376 p = info.action_table;
377 else if (ip < info.Start + cs_start + cs_len)
378 {
379 if (cs_lp)
380 landing_pad = info.LPStart + cs_lp;
381 if (cs_action)
382 action_record = info.action_table + cs_action - 1;
383 goto found_something;
384 }
385 }
386 #endif /* __USING_SJLJ_EXCEPTIONS__ */
387
388 /* If ip is not present in the table, C++ would call terminate. */
389 /* ??? As with Java, it's perhaps better to tweek the LSDA to that
390 no-action is mapped to no-entry. */
391 CONTINUE_UNWINDING;
392
393 found_something:
394 saw_cleanup = 0;
395 saw_handler = 0;
396
397 if (landing_pad == 0)
398 {
399 /* If ip is present, and has a null landing pad, there are no
400 cleanups or handlers to be run. */
401 }
402 else if (action_record == 0)
403 {
404 /* If ip is present, has a non-null landing pad, and a null
405 action table offset, then there are only cleanups present.
406 Cleanups use a zero switch value, as set above. */
407 saw_cleanup = 1;
408 }
409 else
410 {
411 /* Otherwise we have a catch handler. */
412 _sleb128_t ar_filter, ar_disp;
413
414 while (1)
415 {
416 p = action_record;
417 p = read_sleb128 (p, &ar_filter);
418 read_sleb128 (p, &ar_disp);
419
420 if (ar_filter == 0)
421 {
422 /* Zero filter values are cleanups. */
423 saw_cleanup = 1;
424 }
425
426 /* During forced unwinding, we only run cleanups. With a
427 foreign exception class, we have no class info to
428 match. */
429 else if ((actions & _UA_FORCE_UNWIND) || foreign_exception)
430 ;
431
432 else if (ar_filter > 0)
433 {
434 /* Positive filter values are handlers. */
435 Class catch_type = get_ttype_entry (&info, ar_filter);
436
437 if ((*__objc_exception_matcher) (catch_type, xh->value))
438 {
439 handler_switch_value = ar_filter;
440 saw_handler = 1;
441 break;
442 }
443 }
444 else
445 {
446 /* Negative filter values are exception specifications,
447 which Objective-C does not use. */
448 abort ();
449 }
450
451 if (ar_disp == 0)
452 break;
453 action_record = p + ar_disp;
454 }
455 }
456
457 if (! saw_handler && ! saw_cleanup)
458 CONTINUE_UNWINDING;
459
460 if (actions & _UA_SEARCH_PHASE)
461 {
462 if (!saw_handler)
463 CONTINUE_UNWINDING;
464
465 /* For domestic exceptions, we cache data from phase 1 for phase
466 2. */
467 if (!foreign_exception)
468 {
469 #ifdef __ARM_EABI_UNWINDER__
470 ue_header->barrier_cache.sp = _Unwind_GetGR (context, 13);
471 ue_header->barrier_cache.bitpattern[1] = (_uw) handler_switch_value;
472 ue_header->barrier_cache.bitpattern[3] = (_uw) landing_pad;
473 #else
474 xh->handlerSwitchValue = handler_switch_value;
475 xh->landingPad = landing_pad;
476 #endif
477 }
478 return _URC_HANDLER_FOUND;
479 }
480
481 install_context:
482 if (saw_cleanup == 0)
483 {
484 return_object = xh->value;
485 if (!(actions & _UA_SEARCH_PHASE))
486 _Unwind_DeleteException(&xh->base);
487 }
488
489 _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
490 __builtin_extend_pointer (saw_cleanup ? xh : return_object));
491 _Unwind_SetGR (context, __builtin_eh_return_data_regno (1),
492 handler_switch_value);
493 _Unwind_SetIP (context, landing_pad);
494 return _URC_INSTALL_CONTEXT;
495 }
496
497 static void
498 __objc_exception_cleanup (_Unwind_Reason_Code code __attribute__((unused)),
499 struct _Unwind_Exception *exc)
500 {
501 free (exc);
502 }
503
504 void
505 objc_exception_throw (id exception)
506 {
507 struct ObjcException *header = calloc (1, sizeof (*header));
508
509 memcpy (&header->base.exception_class, &__objc_exception_class,
510 sizeof (__objc_exception_class));
511 header->base.exception_cleanup = __objc_exception_cleanup;
512 header->value = exception;
513
514 #ifdef __USING_SJLJ_EXCEPTIONS__
515 _Unwind_SjLj_RaiseException (&header->base);
516 #else
517 _Unwind_RaiseException (&header->base);
518 #endif
519
520 /* No exception handler was installed. Call the uncaught exception
521 handler if any is defined. */
522 if (__objc_uncaught_exception_handler != 0)
523 {
524 (*__objc_uncaught_exception_handler) (exception);
525 }
526
527 abort ();
528 }
529
530 #if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
531 EXCEPTION_DISPOSITION
532 __gnu_objc_personality_seh0 (PEXCEPTION_RECORD ms_exc, void *this_frame,
533 PCONTEXT ms_orig_context,
534 PDISPATCHER_CONTEXT ms_disp)
535 {
536 return _GCC_specific_handler (ms_exc, this_frame, ms_orig_context,
537 ms_disp, __gnu_objc_personality_imp);
538 }
539 #endif
540