exception.c 15 KB

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