exception.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // Functions for Exception Support for Java.
  2. /* Copyright (C) 1998, 1999, 2001, 2002, 2006, 2010, 2011
  3. Free Software Foundation
  4. This file is part of libgcj.
  5. This software is copyrighted work licensed under the terms of the
  6. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  7. details. */
  8. #include <config.h>
  9. #include <stddef.h>
  10. #include <stdlib.h>
  11. #include <java/lang/Class.h>
  12. #include <java/lang/NullPointerException.h>
  13. #include <gnu/gcj/RawData.h>
  14. #include <gcj/cni.h>
  15. #include <jvm.h>
  16. // unwind-pe.h uses std::abort(), but sometimes we compile libjava
  17. // without libstdc++-v3. The following hack forces it to use
  18. // stdlib.h's abort().
  19. namespace std
  20. {
  21. __attribute__ ((__noreturn__)) void
  22. abort ()
  23. {
  24. ::abort ();
  25. }
  26. }
  27. #include "unwind.h"
  28. struct alignment_test_struct
  29. {
  30. char space;
  31. char end[0] __attribute__((aligned));
  32. };
  33. struct java_exception_header
  34. {
  35. /* Cache handler details between Phase 1 and Phase 2. */
  36. _Unwind_Ptr landingPad;
  37. int handlerSwitchValue;
  38. /* The object being thrown. Compiled code expects this to be immediately
  39. before the generic exception header. Which is complicated by the fact
  40. that _Unwind_Exception is ((aligned)). */
  41. char pad[sizeof(jthrowable) < sizeof(alignment_test_struct)
  42. ? sizeof(alignment_test_struct) - sizeof(jthrowable) : 0]
  43. __attribute__((aligned));
  44. jthrowable value;
  45. /* The generic exception header. */
  46. _Unwind_Exception unwindHeader;
  47. };
  48. #ifdef __ARM_EABI_UNWINDER__
  49. // This is the exception class we report -- "GNUCJAVA".
  50. const _Unwind_Exception_Class __gcj_exception_class
  51. = {'G', 'N', 'U', 'C', 'J', 'A', 'V', 'A'};
  52. static inline java_exception_header *
  53. get_exception_header_from_ue (_Unwind_Exception *exc)
  54. {
  55. return reinterpret_cast<java_exception_header *>(exc + 1) - 1;
  56. }
  57. extern "C" void __cxa_begin_cleanup (_Unwind_Exception*);
  58. #else // !__ARM_EABI_UNWINDER__
  59. // This is the exception class we report -- "GNUCJAVA".
  60. const _Unwind_Exception_Class __gcj_exception_class
  61. = ((((((((_Unwind_Exception_Class) 'G'
  62. << 8 | (_Unwind_Exception_Class) 'N')
  63. << 8 | (_Unwind_Exception_Class) 'U')
  64. << 8 | (_Unwind_Exception_Class) 'C')
  65. << 8 | (_Unwind_Exception_Class) 'J')
  66. << 8 | (_Unwind_Exception_Class) 'A')
  67. << 8 | (_Unwind_Exception_Class) 'V')
  68. << 8 | (_Unwind_Exception_Class) 'A');
  69. static inline java_exception_header *
  70. get_exception_header_from_ue (_Unwind_Exception *exc)
  71. {
  72. return reinterpret_cast<java_exception_header *>(exc + 1) - 1;
  73. }
  74. #endif // !__ARM_EABI_UNWINDER__
  75. /* Perform a throw, Java style. Throw will unwind through this call,
  76. so there better not be any handlers or exception thrown here. */
  77. extern "C" void
  78. _Jv_Throw (jthrowable value)
  79. {
  80. java_exception_header *xh
  81. = static_cast<java_exception_header *>(_Jv_AllocRawObj (sizeof (*xh)));
  82. if (value == NULL)
  83. value = new java::lang::NullPointerException ();
  84. xh->value = value;
  85. memcpy (&xh->unwindHeader.exception_class, &__gcj_exception_class,
  86. sizeof xh->unwindHeader.exception_class);
  87. xh->unwindHeader.exception_cleanup = NULL;
  88. /* We're happy with setjmp/longjmp exceptions or region-based
  89. exception handlers: entry points are provided here for both. */
  90. #ifdef SJLJ_EXCEPTIONS
  91. _Unwind_SjLj_RaiseException (&xh->unwindHeader);
  92. #else
  93. _Unwind_RaiseException (&xh->unwindHeader);
  94. #endif
  95. /* If code == _URC_END_OF_STACK, then we reached top of stack without
  96. finding a handler for the exception. Since each thread is run in
  97. a try/catch, this oughtn't happen. If code is something else, we
  98. encountered some sort of heinous lossage from which we could not
  99. recover. As is the way of such things, almost certainly we will have
  100. crashed before now, rather than actually being able to diagnose the
  101. problem. */
  102. abort();
  103. }
  104. #include "unwind-pe.h"
  105. struct lsda_header_info
  106. {
  107. _Unwind_Ptr Start;
  108. _Unwind_Ptr LPStart;
  109. const unsigned char *TType;
  110. const unsigned char *action_table;
  111. unsigned char ttype_encoding;
  112. unsigned char call_site_encoding;
  113. };
  114. static const unsigned char *
  115. parse_lsda_header (_Unwind_Context *context, const unsigned char *p,
  116. lsda_header_info *info)
  117. {
  118. _uleb128_t tmp;
  119. unsigned char lpstart_encoding;
  120. info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
  121. // Find @LPStart, the base to which landing pad offsets are 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 data.
  128. info->ttype_encoding = *p++;
  129. if (info->ttype_encoding != DW_EH_PE_omit)
  130. {
  131. #if _GLIBCXX_OVERRIDE_TTYPE_ENCODING
  132. /* Older ARM EABI toolchains set this value incorrectly, so use a
  133. hardcoded OS-specific format. */
  134. info->ttype_encoding = _GLIBCXX_OVERRIDE_TTYPE_ENCODING;
  135. #endif
  136. p = read_uleb128 (p, &tmp);
  137. info->TType = p + tmp;
  138. }
  139. else
  140. info->TType = 0;
  141. // The encoding and length of the call-site table; the action table
  142. // immediately follows.
  143. info->call_site_encoding = *p++;
  144. p = read_uleb128 (p, &tmp);
  145. info->action_table = p + tmp;
  146. return p;
  147. }
  148. static void **
  149. get_ttype_entry (_Unwind_Context *context, lsda_header_info *info, long i)
  150. {
  151. _Unwind_Ptr ptr;
  152. i *= size_of_encoded_value (info->ttype_encoding);
  153. read_encoded_value (context, info->ttype_encoding, info->TType - i, &ptr);
  154. return reinterpret_cast<void **>(ptr);
  155. }
  156. // Using a different personality function name causes link failures
  157. // when trying to mix code using different exception handling models.
  158. #ifdef SJLJ_EXCEPTIONS
  159. #define PERSONALITY_FUNCTION __gcj_personality_sj0
  160. #define __builtin_eh_return_data_regno(x) x
  161. #elif defined (__SEH__)
  162. #define PERSONALITY_FUNCTION __gcj_personality_imp
  163. #else
  164. #define PERSONALITY_FUNCTION __gcj_personality_v0
  165. #endif
  166. #ifdef __ARM_EABI_UNWINDER__
  167. #define CONTINUE_UNWINDING \
  168. do \
  169. { \
  170. if (__gnu_unwind_frame(ue_header, context) != _URC_OK) \
  171. return _URC_FAILURE; \
  172. return _URC_CONTINUE_UNWIND; \
  173. } \
  174. while (0)
  175. extern "C" _Unwind_Reason_Code
  176. PERSONALITY_FUNCTION (_Unwind_State state,
  177. struct _Unwind_Exception* ue_header,
  178. struct _Unwind_Context* context)
  179. #else
  180. #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
  181. #ifdef __SEH__
  182. static
  183. #else
  184. extern "C"
  185. #endif
  186. _Unwind_Reason_Code
  187. PERSONALITY_FUNCTION (int version,
  188. _Unwind_Action actions,
  189. _Unwind_Exception_Class exception_class,
  190. struct _Unwind_Exception *ue_header,
  191. struct _Unwind_Context *context)
  192. #endif
  193. {
  194. java_exception_header *xh = get_exception_header_from_ue (ue_header);
  195. lsda_header_info info;
  196. const unsigned char *language_specific_data;
  197. const unsigned char *action_record;
  198. const unsigned char *p;
  199. _Unwind_Ptr landing_pad, ip;
  200. int handler_switch_value;
  201. bool saw_cleanup;
  202. bool saw_handler;
  203. bool foreign_exception;
  204. int ip_before_insn = 0;
  205. #ifdef __ARM_EABI_UNWINDER__
  206. _Unwind_Action actions;
  207. switch (state & _US_ACTION_MASK)
  208. {
  209. case _US_VIRTUAL_UNWIND_FRAME:
  210. actions = _UA_SEARCH_PHASE;
  211. break;
  212. case _US_UNWIND_FRAME_STARTING:
  213. actions = _UA_CLEANUP_PHASE;
  214. if (!(state & _US_FORCE_UNWIND)
  215. && ue_header->barrier_cache.sp == _Unwind_GetGR(context, 13))
  216. actions |= _UA_HANDLER_FRAME;
  217. break;
  218. case _US_UNWIND_FRAME_RESUME:
  219. CONTINUE_UNWINDING;
  220. break;
  221. default:
  222. std::abort();
  223. }
  224. actions |= state & _US_FORCE_UNWIND;
  225. // We don't know which runtime we're working with, so can't check this.
  226. // However the ABI routines hide this from us, and we don't actually need
  227. // to know.
  228. foreign_exception = false;
  229. // The dwarf unwinder assumes the context structure holds things like the
  230. // function and LSDA pointers. The ARM implementation caches these in
  231. // the exception header (UCB). To avoid rewriting everything we make the
  232. // virtual IP register point at the UCB.
  233. ip = (_Unwind_Ptr) ue_header;
  234. _Unwind_SetGR(context, 12, ip);
  235. #else
  236. // Interface version check.
  237. if (version != 1)
  238. return _URC_FATAL_PHASE1_ERROR;
  239. foreign_exception = exception_class != __gcj_exception_class;
  240. #endif
  241. // Shortcut for phase 2 found handler for domestic exception.
  242. if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
  243. && !foreign_exception)
  244. {
  245. handler_switch_value = xh->handlerSwitchValue;
  246. landing_pad = xh->landingPad;
  247. goto install_context;
  248. }
  249. // FIXME: In Phase 1, record _Unwind_GetIPInfo in xh->obj as a part of
  250. // the stack trace for this exception. This will only collect Java
  251. // frames, but perhaps that is acceptable.
  252. // FIXME2: _Unwind_GetIPInfo is nonsensical for SJLJ, being a call-site
  253. // index instead of a PC value. We could perhaps arrange for
  254. // _Unwind_GetRegionStart to return context->fc->jbuf[1], which
  255. // is the address of the handler label for __builtin_longjmp, but
  256. // there is no solution for DONT_USE_BUILTIN_SETJMP.
  257. language_specific_data = (const unsigned char *)
  258. _Unwind_GetLanguageSpecificData (context);
  259. // If no LSDA, then there are no handlers or cleanups.
  260. if (! language_specific_data)
  261. CONTINUE_UNWINDING;
  262. // Parse the LSDA header.
  263. p = parse_lsda_header (context, language_specific_data, &info);
  264. #ifdef HAVE_GETIPINFO
  265. ip = _Unwind_GetIPInfo (context, &ip_before_insn);
  266. #else
  267. ip = _Unwind_GetIP (context);
  268. #endif
  269. if (! ip_before_insn)
  270. --ip;
  271. landing_pad = 0;
  272. action_record = 0;
  273. handler_switch_value = 0;
  274. #ifdef SJLJ_EXCEPTIONS
  275. // The given "IP" is an index into the call-site table, with two
  276. // exceptions -- -1 means no-action, and 0 means terminate. But
  277. // since we're using uleb128 values, we've not got random access
  278. // to the array.
  279. if ((int) ip <= 0)
  280. return _URC_CONTINUE_UNWIND;
  281. else
  282. {
  283. _uleb128_t cs_lp, cs_action;
  284. do
  285. {
  286. p = read_uleb128 (p, &cs_lp);
  287. p = read_uleb128 (p, &cs_action);
  288. }
  289. while (--ip);
  290. // Can never have null landing pad for sjlj -- that would have
  291. // been indicated by a -1 call site index.
  292. landing_pad = cs_lp + 1;
  293. if (cs_action)
  294. action_record = info.action_table + cs_action - 1;
  295. goto found_something;
  296. }
  297. #else
  298. // Search the call-site table for the action associated with this IP.
  299. while (p < info.action_table)
  300. {
  301. _Unwind_Ptr cs_start, cs_len, cs_lp;
  302. _uleb128_t cs_action;
  303. // Note that all call-site encodings are "absolute" displacements.
  304. p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
  305. p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
  306. p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
  307. p = read_uleb128 (p, &cs_action);
  308. // The table is sorted, so if we've passed the ip, stop.
  309. if (ip < info.Start + cs_start)
  310. p = info.action_table;
  311. else if (ip < info.Start + cs_start + cs_len)
  312. {
  313. if (cs_lp)
  314. landing_pad = info.LPStart + cs_lp;
  315. if (cs_action)
  316. action_record = info.action_table + cs_action - 1;
  317. goto found_something;
  318. }
  319. }
  320. #endif // SJLJ_EXCEPTIONS
  321. // If ip is not present in the table, C++ would call terminate.
  322. // ??? It is perhaps better to tweek the LSDA so that no-action
  323. // is mapped to no-entry for Java.
  324. CONTINUE_UNWINDING;
  325. found_something:
  326. saw_cleanup = false;
  327. saw_handler = false;
  328. if (landing_pad == 0)
  329. {
  330. // If ip is present, and has a null landing pad, there are
  331. // no cleanups or handlers to be run.
  332. }
  333. else if (action_record == 0)
  334. {
  335. // If ip is present, has a non-null landing pad, and a null
  336. // action table offset, then there are only cleanups present.
  337. // Cleanups use a zero switch value, as set above.
  338. saw_cleanup = true;
  339. }
  340. else
  341. {
  342. // Otherwise we have a catch handler.
  343. _sleb128_t ar_filter, ar_disp;
  344. while (1)
  345. {
  346. p = action_record;
  347. p = read_sleb128 (p, &ar_filter);
  348. read_sleb128 (p, &ar_disp);
  349. if (ar_filter == 0)
  350. {
  351. // Zero filter values are cleanups.
  352. saw_cleanup = true;
  353. }
  354. // During forced unwinding, we only run cleanups. With a
  355. // foreign exception class, we have no class info to match.
  356. else if ((actions & _UA_FORCE_UNWIND)
  357. || foreign_exception)
  358. ;
  359. else if (ar_filter > 0)
  360. {
  361. // Positive filter values are handlers.
  362. void **catch_word = get_ttype_entry (context, &info, ar_filter);
  363. jclass catch_type = (jclass)*catch_word;
  364. // FIXME: This line is a kludge to work around exception
  365. // handlers written in C++, which don't yet use indirect
  366. // dispatch.
  367. if (catch_type == *(void **)&java::lang::Class::class$)
  368. catch_type = (jclass)catch_word;
  369. if (_Jv_IsInstanceOf (xh->value, catch_type))
  370. {
  371. handler_switch_value = ar_filter;
  372. saw_handler = true;
  373. break;
  374. }
  375. }
  376. else
  377. {
  378. // Negative filter values are exception specifications,
  379. // which Java does not use.
  380. // ??? Perhaps better to make them an index into a table
  381. // of null-terminated strings instead of playing games
  382. // with Utf8Const+1 as above.
  383. abort ();
  384. }
  385. if (ar_disp == 0)
  386. break;
  387. action_record = p + ar_disp;
  388. }
  389. }
  390. if (! saw_handler && ! saw_cleanup)
  391. CONTINUE_UNWINDING;
  392. if (actions & _UA_SEARCH_PHASE)
  393. {
  394. if (! saw_handler)
  395. CONTINUE_UNWINDING;
  396. // For domestic exceptions, we cache data from phase 1 for phase 2.
  397. if (! foreign_exception)
  398. {
  399. xh->handlerSwitchValue = handler_switch_value;
  400. xh->landingPad = landing_pad;
  401. }
  402. return _URC_HANDLER_FOUND;
  403. }
  404. install_context:
  405. _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
  406. (_Unwind_Ptr) &xh->unwindHeader);
  407. _Unwind_SetGR (context, __builtin_eh_return_data_regno (1),
  408. handler_switch_value);
  409. _Unwind_SetIP (context, landing_pad);
  410. #ifdef __ARM_EABI_UNWINDER__
  411. if (saw_cleanup)
  412. __cxa_begin_cleanup(ue_header);
  413. #endif
  414. return _URC_INSTALL_CONTEXT;
  415. }
  416. #ifdef __SEH__
  417. extern "C"
  418. EXCEPTION_DISPOSITION
  419. __gcj_personality_seh0 (PEXCEPTION_RECORD ms_exc, void *this_frame,
  420. PCONTEXT ms_orig_context, PDISPATCHER_CONTEXT ms_disp)
  421. {
  422. return _GCC_specific_handler (ms_exc, this_frame, ms_orig_context,
  423. ms_disp, __gcj_personality_imp);
  424. }
  425. #endif /* SEH */