exception.c 15 KB

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