go-unwind.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* go-unwind.c -- unwind the stack for panic/recover.
  2. Copyright 2010 The Go Authors. All rights reserved.
  3. Use of this source code is governed by a BSD-style
  4. license that can be found in the LICENSE file. */
  5. #include "config.h"
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include "unwind.h"
  9. #define NO_SIZE_OF_ENCODED_VALUE
  10. #include "unwind-pe.h"
  11. #include "runtime.h"
  12. #include "go-alloc.h"
  13. #include "go-defer.h"
  14. #include "go-panic.h"
  15. /* The code for a Go exception. */
  16. #ifdef __ARM_EABI_UNWINDER__
  17. static const _Unwind_Exception_Class __go_exception_class =
  18. { 'G', 'N', 'U', 'C', 'G', 'O', '\0', '\0' };
  19. #else
  20. static const _Unwind_Exception_Class __go_exception_class =
  21. ((((((((_Unwind_Exception_Class) 'G'
  22. << 8 | (_Unwind_Exception_Class) 'N')
  23. << 8 | (_Unwind_Exception_Class) 'U')
  24. << 8 | (_Unwind_Exception_Class) 'C')
  25. << 8 | (_Unwind_Exception_Class) 'G')
  26. << 8 | (_Unwind_Exception_Class) 'O')
  27. << 8 | (_Unwind_Exception_Class) '\0')
  28. << 8 | (_Unwind_Exception_Class) '\0');
  29. #endif
  30. /* This function is called by exception handlers used when unwinding
  31. the stack after a recovered panic. The exception handler looks
  32. like this:
  33. __go_check_defer (frame);
  34. return;
  35. If we have not yet reached the frame we are looking for, we
  36. continue unwinding. */
  37. void
  38. __go_check_defer (_Bool *frame)
  39. {
  40. G *g;
  41. struct _Unwind_Exception *hdr;
  42. g = runtime_g ();
  43. if (g == NULL)
  44. {
  45. /* Some other language has thrown an exception. We know there
  46. are no defer handlers, so there is nothing to do. */
  47. }
  48. else if (g->is_foreign)
  49. {
  50. struct __go_panic_stack *n;
  51. _Bool was_recovered;
  52. /* Some other language has thrown an exception. We need to run
  53. the local defer handlers. If they call recover, we stop
  54. unwinding the stack here. */
  55. n = ((struct __go_panic_stack *)
  56. __go_alloc (sizeof (struct __go_panic_stack)));
  57. n->__arg.__type_descriptor = NULL;
  58. n->__arg.__object = NULL;
  59. n->__was_recovered = 0;
  60. n->__is_foreign = 1;
  61. n->__next = g->panic;
  62. g->panic = n;
  63. while (1)
  64. {
  65. struct __go_defer_stack *d;
  66. void (*pfn) (void *);
  67. d = g->defer;
  68. if (d == NULL || d->__frame != frame || d->__pfn == NULL)
  69. break;
  70. pfn = d->__pfn;
  71. g->defer = d->__next;
  72. (*pfn) (d->__arg);
  73. if (runtime_m () != NULL)
  74. runtime_freedefer (d);
  75. if (n->__was_recovered)
  76. {
  77. /* The recover function caught the panic thrown by some
  78. other language. */
  79. break;
  80. }
  81. }
  82. was_recovered = n->__was_recovered;
  83. g->panic = n->__next;
  84. __go_free (n);
  85. if (was_recovered)
  86. {
  87. /* Just return and continue executing Go code. */
  88. *frame = 1;
  89. return;
  90. }
  91. /* We are panicing through this function. */
  92. *frame = 0;
  93. }
  94. else if (g->defer != NULL
  95. && g->defer->__pfn == NULL
  96. && g->defer->__frame == frame)
  97. {
  98. struct __go_defer_stack *d;
  99. /* This is the defer function which called recover. Simply
  100. return to stop the stack unwind, and let the Go code continue
  101. to execute. */
  102. d = g->defer;
  103. g->defer = d->__next;
  104. if (runtime_m () != NULL)
  105. runtime_freedefer (d);
  106. /* We are returning from this function. */
  107. *frame = 1;
  108. return;
  109. }
  110. /* This is some other defer function. It was already run by the
  111. call to panic, or just above. Rethrow the exception. */
  112. hdr = (struct _Unwind_Exception *) g->exception;
  113. #ifdef LIBGO_SJLJ_EXCEPTIONS
  114. _Unwind_SjLj_Resume_or_Rethrow (hdr);
  115. #else
  116. #if defined(_LIBUNWIND_STD_ABI)
  117. _Unwind_RaiseException (hdr);
  118. #else
  119. _Unwind_Resume_or_Rethrow (hdr);
  120. #endif
  121. #endif
  122. /* Rethrowing the exception should not return. */
  123. abort();
  124. }
  125. /* Unwind function calls until we reach the one which used a defer
  126. function which called recover. Each function which uses a defer
  127. statement will have an exception handler, as shown above. */
  128. void
  129. __go_unwind_stack ()
  130. {
  131. struct _Unwind_Exception *hdr;
  132. hdr = ((struct _Unwind_Exception *)
  133. __go_alloc (sizeof (struct _Unwind_Exception)));
  134. __builtin_memcpy (&hdr->exception_class, &__go_exception_class,
  135. sizeof hdr->exception_class);
  136. hdr->exception_cleanup = NULL;
  137. runtime_g ()->exception = hdr;
  138. #ifdef __USING_SJLJ_EXCEPTIONS__
  139. _Unwind_SjLj_RaiseException (hdr);
  140. #else
  141. _Unwind_RaiseException (hdr);
  142. #endif
  143. /* Raising an exception should not return. */
  144. abort ();
  145. }
  146. /* The rest of this code is really similar to gcc/unwind-c.c and
  147. libjava/exception.cc. */
  148. typedef struct
  149. {
  150. _Unwind_Ptr Start;
  151. _Unwind_Ptr LPStart;
  152. _Unwind_Ptr ttype_base;
  153. const unsigned char *TType;
  154. const unsigned char *action_table;
  155. unsigned char ttype_encoding;
  156. unsigned char call_site_encoding;
  157. } lsda_header_info;
  158. static const unsigned char *
  159. parse_lsda_header (struct _Unwind_Context *context, const unsigned char *p,
  160. lsda_header_info *info)
  161. {
  162. _uleb128_t tmp;
  163. unsigned char lpstart_encoding;
  164. info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
  165. /* Find @LPStart, the base to which landing pad offsets are relative. */
  166. lpstart_encoding = *p++;
  167. if (lpstart_encoding != DW_EH_PE_omit)
  168. p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
  169. else
  170. info->LPStart = info->Start;
  171. /* Find @TType, the base of the handler and exception spec type data. */
  172. info->ttype_encoding = *p++;
  173. if (info->ttype_encoding != DW_EH_PE_omit)
  174. {
  175. p = read_uleb128 (p, &tmp);
  176. info->TType = p + tmp;
  177. }
  178. else
  179. info->TType = 0;
  180. /* The encoding and length of the call-site table; the action table
  181. immediately follows. */
  182. info->call_site_encoding = *p++;
  183. p = read_uleb128 (p, &tmp);
  184. info->action_table = p + tmp;
  185. return p;
  186. }
  187. /* The personality function is invoked when unwinding the stack due to
  188. a panic. Its job is to find the cleanup and exception handlers to
  189. run. We can't split the stack here, because we won't be able to
  190. unwind from that split. */
  191. #ifdef __ARM_EABI_UNWINDER__
  192. /* ARM EABI personality routines must also unwind the stack. */
  193. #define CONTINUE_UNWINDING \
  194. do \
  195. { \
  196. if (__gnu_unwind_frame (ue_header, context) != _URC_OK) \
  197. return _URC_FAILURE; \
  198. return _URC_CONTINUE_UNWIND; \
  199. } \
  200. while (0)
  201. #else
  202. #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
  203. #endif
  204. #ifdef __USING_SJLJ_EXCEPTIONS__
  205. #define PERSONALITY_FUNCTION __gccgo_personality_sj0
  206. #define __builtin_eh_return_data_regno(x) x
  207. #else
  208. #define PERSONALITY_FUNCTION __gccgo_personality_v0
  209. #endif
  210. #ifdef __ARM_EABI_UNWINDER__
  211. _Unwind_Reason_Code
  212. PERSONALITY_FUNCTION (_Unwind_State, struct _Unwind_Exception *,
  213. struct _Unwind_Context *)
  214. __attribute__ ((no_split_stack, flatten));
  215. _Unwind_Reason_Code
  216. PERSONALITY_FUNCTION (_Unwind_State state,
  217. struct _Unwind_Exception * ue_header,
  218. struct _Unwind_Context * context)
  219. #else
  220. _Unwind_Reason_Code
  221. PERSONALITY_FUNCTION (int, _Unwind_Action, _Unwind_Exception_Class,
  222. struct _Unwind_Exception *, struct _Unwind_Context *)
  223. __attribute__ ((no_split_stack, flatten));
  224. _Unwind_Reason_Code
  225. PERSONALITY_FUNCTION (int version,
  226. _Unwind_Action actions,
  227. _Unwind_Exception_Class exception_class,
  228. struct _Unwind_Exception *ue_header,
  229. struct _Unwind_Context *context)
  230. #endif
  231. {
  232. lsda_header_info info;
  233. const unsigned char *language_specific_data, *p, *action_record;
  234. _Unwind_Ptr landing_pad, ip;
  235. int ip_before_insn = 0;
  236. _Bool is_foreign;
  237. G *g;
  238. #ifdef __ARM_EABI_UNWINDER__
  239. _Unwind_Action actions;
  240. switch (state & _US_ACTION_MASK)
  241. {
  242. case _US_VIRTUAL_UNWIND_FRAME:
  243. actions = _UA_SEARCH_PHASE;
  244. break;
  245. case _US_UNWIND_FRAME_STARTING:
  246. actions = _UA_CLEANUP_PHASE;
  247. if (!(state & _US_FORCE_UNWIND)
  248. && ue_header->barrier_cache.sp == _Unwind_GetGR(context, 13))
  249. actions |= _UA_HANDLER_FRAME;
  250. break;
  251. case _US_UNWIND_FRAME_RESUME:
  252. CONTINUE_UNWINDING;
  253. break;
  254. default:
  255. abort();
  256. }
  257. actions |= state & _US_FORCE_UNWIND;
  258. is_foreign = 0;
  259. /* The dwarf unwinder assumes the context structure holds things like the
  260. function and LSDA pointers. The ARM implementation caches these in
  261. the exception header (UCB). To avoid rewriting everything we make the
  262. virtual IP register point at the UCB. */
  263. ip = (_Unwind_Ptr) ue_header;
  264. _Unwind_SetGR (context, 12, ip);
  265. #else
  266. if (version != 1)
  267. return _URC_FATAL_PHASE1_ERROR;
  268. is_foreign = exception_class != __go_exception_class;
  269. #endif
  270. language_specific_data = (const unsigned char *)
  271. _Unwind_GetLanguageSpecificData (context);
  272. /* If no LSDA, then there are no handlers or cleanups. */
  273. if (! language_specific_data)
  274. CONTINUE_UNWINDING;
  275. /* Parse the LSDA header. */
  276. p = parse_lsda_header (context, language_specific_data, &info);
  277. #ifdef HAVE_GETIPINFO
  278. ip = _Unwind_GetIPInfo (context, &ip_before_insn);
  279. #else
  280. ip = _Unwind_GetIP (context);
  281. #endif
  282. if (! ip_before_insn)
  283. --ip;
  284. landing_pad = 0;
  285. action_record = NULL;
  286. #ifdef __USING_SJLJ_EXCEPTIONS__
  287. /* The given "IP" is an index into the call-site table, with two
  288. exceptions -- -1 means no-action, and 0 means terminate. But
  289. since we're using uleb128 values, we've not got random access
  290. to the array. */
  291. if ((int) ip <= 0)
  292. return _URC_CONTINUE_UNWIND;
  293. else
  294. {
  295. _uleb128_t cs_lp, cs_action;
  296. do
  297. {
  298. p = read_uleb128 (p, &cs_lp);
  299. p = read_uleb128 (p, &cs_action);
  300. }
  301. while (--ip);
  302. /* Can never have null landing pad for sjlj -- that would have
  303. been indicated by a -1 call site index. */
  304. landing_pad = (_Unwind_Ptr)cs_lp + 1;
  305. if (cs_action)
  306. action_record = info.action_table + cs_action - 1;
  307. goto found_something;
  308. }
  309. #else
  310. /* Search the call-site table for the action associated with this IP. */
  311. while (p < info.action_table)
  312. {
  313. _Unwind_Ptr cs_start, cs_len, cs_lp;
  314. _uleb128_t cs_action;
  315. /* Note that all call-site encodings are "absolute" displacements. */
  316. p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
  317. p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
  318. p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
  319. p = read_uleb128 (p, &cs_action);
  320. /* The table is sorted, so if we've passed the ip, stop. */
  321. if (ip < info.Start + cs_start)
  322. p = info.action_table;
  323. else if (ip < info.Start + cs_start + cs_len)
  324. {
  325. if (cs_lp)
  326. landing_pad = info.LPStart + cs_lp;
  327. if (cs_action)
  328. action_record = info.action_table + cs_action - 1;
  329. goto found_something;
  330. }
  331. }
  332. #endif
  333. /* IP is not in table. No associated cleanups. */
  334. CONTINUE_UNWINDING;
  335. found_something:
  336. if (landing_pad == 0)
  337. {
  338. /* IP is present, but has a null landing pad.
  339. No handler to be run. */
  340. CONTINUE_UNWINDING;
  341. }
  342. if (actions & _UA_SEARCH_PHASE)
  343. {
  344. if (action_record == 0)
  345. {
  346. /* This indicates a cleanup rather than an exception
  347. handler. */
  348. CONTINUE_UNWINDING;
  349. }
  350. return _URC_HANDLER_FOUND;
  351. }
  352. /* It's possible for g to be NULL here for an exception thrown by a
  353. language other than Go. */
  354. g = runtime_g ();
  355. if (g == NULL)
  356. {
  357. if (!is_foreign)
  358. abort ();
  359. }
  360. else
  361. {
  362. g->exception = ue_header;
  363. g->is_foreign = is_foreign;
  364. }
  365. _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
  366. (_Unwind_Ptr) ue_header);
  367. _Unwind_SetGR (context, __builtin_eh_return_data_regno (1), 0);
  368. _Unwind_SetIP (context, landing_pad);
  369. return _URC_INSTALL_CONTEXT;
  370. }