go-unwind.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. #include "runtime.h"
  10. /* These constants are documented here:
  11. https://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA/dwarfext.html
  12. */
  13. #define DW_EH_PE_omit 0xff
  14. #define DW_EH_PE_absptr 0x00
  15. #define DW_EH_PE_uleb128 0x01
  16. #define DW_EH_PE_udata2 0x02
  17. #define DW_EH_PE_udata4 0x03
  18. #define DW_EH_PE_udata8 0x04
  19. #define DW_EH_PE_sleb128 0x09
  20. #define DW_EH_PE_sdata2 0x0A
  21. #define DW_EH_PE_sdata4 0x0B
  22. #define DW_EH_PE_sdata8 0x0C
  23. #define DW_EH_PE_pcrel 0x10
  24. #define DW_EH_PE_textrel 0x20
  25. #define DW_EH_PE_datarel 0x30
  26. #define DW_EH_PE_funcrel 0x40
  27. #define DW_EH_PE_aligned 0x50
  28. #define DW_EH_PE_indirect 0x80
  29. /* The code for a Go exception. */
  30. #ifdef __ARM_EABI_UNWINDER__
  31. static const _Unwind_Exception_Class __go_exception_class =
  32. { 'G', 'N', 'U', 'C', 'G', 'O', '\0', '\0' };
  33. #else
  34. static const _Unwind_Exception_Class __go_exception_class =
  35. ((((((((_Unwind_Exception_Class) 'G'
  36. << 8 | (_Unwind_Exception_Class) 'N')
  37. << 8 | (_Unwind_Exception_Class) 'U')
  38. << 8 | (_Unwind_Exception_Class) 'C')
  39. << 8 | (_Unwind_Exception_Class) 'G')
  40. << 8 | (_Unwind_Exception_Class) 'O')
  41. << 8 | (_Unwind_Exception_Class) '\0')
  42. << 8 | (_Unwind_Exception_Class) '\0');
  43. #endif
  44. /* Rethrow an exception. */
  45. void rethrowException (void) __asm__(GOSYM_PREFIX "runtime.rethrowException");
  46. void
  47. rethrowException ()
  48. {
  49. struct _Unwind_Exception *hdr;
  50. _Unwind_Reason_Code reason;
  51. hdr = (struct _Unwind_Exception *) runtime_g()->exception;
  52. #ifdef __USING_SJLJ_EXCEPTIONS__
  53. reason = _Unwind_SjLj_Resume_or_Rethrow (hdr);
  54. #else
  55. #if defined(_LIBUNWIND_STD_ABI)
  56. reason = _Unwind_RaiseException (hdr);
  57. #else
  58. reason = _Unwind_Resume_or_Rethrow (hdr);
  59. #endif
  60. #endif
  61. /* Rethrowing the exception should not return. */
  62. runtime_printf ("failed to rethrow unwind exception (reason=%d)\n", reason);
  63. abort();
  64. }
  65. /* Return the size of the type that holds an exception header, so that
  66. it can be allocated by Go code. */
  67. uintptr unwindExceptionSize(void)
  68. __asm__ (GOSYM_PREFIX "runtime.unwindExceptionSize");
  69. uintptr
  70. unwindExceptionSize ()
  71. {
  72. uintptr ret, align;
  73. ret = sizeof (struct _Unwind_Exception);
  74. /* Adjust the size fo make sure that we can get an aligned value. */
  75. align = __alignof__ (struct _Unwind_Exception);
  76. if (align > __alignof__ (uintptr))
  77. ret += align - __alignof__ (uintptr);
  78. return ret;
  79. }
  80. /* Throw an exception. This is called with g->exception pointing to
  81. an uninitialized _Unwind_Exception instance. */
  82. void throwException (void) __asm__(GOSYM_PREFIX "runtime.throwException");
  83. void
  84. throwException ()
  85. {
  86. struct _Unwind_Exception *hdr;
  87. uintptr align;
  88. _Unwind_Reason_Code reason;
  89. hdr = (struct _Unwind_Exception *)runtime_g ()->exception;
  90. /* Make sure the value is correctly aligned. It will be large
  91. enough, because of unwindExceptionSize. */
  92. align = __alignof__ (struct _Unwind_Exception);
  93. hdr = ((struct _Unwind_Exception *)
  94. (((uintptr) hdr + align - 1) &~ (align - 1)));
  95. __builtin_memcpy (&hdr->exception_class, &__go_exception_class,
  96. sizeof hdr->exception_class);
  97. hdr->exception_cleanup = NULL;
  98. #ifdef __USING_SJLJ_EXCEPTIONS__
  99. reason = _Unwind_SjLj_RaiseException (hdr);
  100. #else
  101. reason = _Unwind_RaiseException (hdr);
  102. #endif
  103. /* Raising an exception should not return. */
  104. runtime_printf ("failed to throw unwind exception (reason=%d)\n", reason);
  105. abort ();
  106. }
  107. static inline _Unwind_Ptr
  108. encoded_value_base (uint8_t encoding, struct _Unwind_Context *context)
  109. {
  110. if (encoding == DW_EH_PE_omit)
  111. return 0;
  112. switch (encoding & 0x70)
  113. {
  114. case DW_EH_PE_absptr:
  115. case DW_EH_PE_pcrel:
  116. case DW_EH_PE_aligned:
  117. return 0;
  118. case DW_EH_PE_textrel:
  119. return _Unwind_GetTextRelBase(context);
  120. case DW_EH_PE_datarel:
  121. return _Unwind_GetDataRelBase(context);
  122. case DW_EH_PE_funcrel:
  123. return _Unwind_GetRegionStart(context);
  124. }
  125. abort ();
  126. }
  127. /* Read an unsigned leb128 value. */
  128. static inline const uint8_t *
  129. read_uleb128 (const uint8_t *p, _uleb128_t *val)
  130. {
  131. unsigned int shift = 0;
  132. _uleb128_t result = 0;
  133. uint8_t byte;
  134. do
  135. {
  136. byte = *p++;
  137. result |= ((_uleb128_t)byte & 0x7f) << shift;
  138. shift += 7;
  139. }
  140. while (byte & 0x80);
  141. *val = result;
  142. return p;
  143. }
  144. /* Similar, but read a signed leb128 value. */
  145. static inline const uint8_t *
  146. read_sleb128 (const uint8_t *p, _sleb128_t *val)
  147. {
  148. unsigned int shift = 0;
  149. _uleb128_t result = 0;
  150. uint8_t byte;
  151. do
  152. {
  153. byte = *p++;
  154. result |= ((_uleb128_t)byte & 0x7f) << shift;
  155. shift += 7;
  156. }
  157. while (byte & 0x80);
  158. /* sign extension */
  159. if (shift < (8 * sizeof(result)) && (byte & 0x40) != 0)
  160. result |= (((_uleb128_t)~0) << shift);
  161. *val = (_sleb128_t)result;
  162. return p;
  163. }
  164. #define ROUND_UP_TO_PVB(x) (x + sizeof(void *) - 1) &- sizeof(void *)
  165. static inline const uint8_t *
  166. read_encoded_value (struct _Unwind_Context *context, uint8_t encoding,
  167. const uint8_t *p, _Unwind_Ptr *val)
  168. {
  169. _Unwind_Ptr base = encoded_value_base (encoding, context);
  170. _Unwind_Internal_Ptr decoded = 0;
  171. const uint8_t *origp = p;
  172. if (encoding == DW_EH_PE_aligned)
  173. {
  174. _Unwind_Internal_Ptr uip = (_Unwind_Internal_Ptr)p;
  175. uip = ROUND_UP_TO_PVB (uip);
  176. decoded = *(_Unwind_Internal_Ptr *)uip;
  177. p = (const uint8_t *)(uip + sizeof(void *));
  178. }
  179. else
  180. {
  181. switch (encoding & 0x0f)
  182. {
  183. case DW_EH_PE_sdata2:
  184. {
  185. int16_t result;
  186. __builtin_memcpy (&result, p, sizeof(int16_t));
  187. decoded = result;
  188. p += sizeof(int16_t);
  189. break;
  190. }
  191. case DW_EH_PE_udata2:
  192. {
  193. uint16_t result;
  194. __builtin_memcpy (&result, p, sizeof(uint16_t));
  195. decoded = result;
  196. p += sizeof(uint16_t);
  197. break;
  198. }
  199. case DW_EH_PE_sdata4:
  200. {
  201. int32_t result;
  202. __builtin_memcpy (&result, p, sizeof(int32_t));
  203. decoded = result;
  204. p += sizeof(int32_t);
  205. break;
  206. }
  207. case DW_EH_PE_udata4:
  208. {
  209. uint32_t result;
  210. __builtin_memcpy (&result, p, sizeof(uint32_t));
  211. decoded = result;
  212. p += sizeof(uint32_t);
  213. break;
  214. }
  215. case DW_EH_PE_sdata8:
  216. {
  217. int64_t result;
  218. __builtin_memcpy (&result, p, sizeof(int64_t));
  219. decoded = result;
  220. p += sizeof(int64_t);
  221. break;
  222. }
  223. case DW_EH_PE_udata8:
  224. {
  225. uint64_t result;
  226. __builtin_memcpy (&result, p, sizeof(uint64_t));
  227. decoded = result;
  228. p += sizeof(uint64_t);
  229. break;
  230. }
  231. case DW_EH_PE_uleb128:
  232. {
  233. _uleb128_t value;
  234. p = read_uleb128 (p, &value);
  235. decoded = (_Unwind_Internal_Ptr)value;
  236. break;
  237. }
  238. case DW_EH_PE_sleb128:
  239. {
  240. _sleb128_t value;
  241. p = read_sleb128 (p, &value);
  242. decoded = (_Unwind_Internal_Ptr)value;
  243. break;
  244. }
  245. case DW_EH_PE_absptr:
  246. __builtin_memcpy (&decoded, (const void *)p, sizeof(const void*));
  247. p += sizeof(void *);
  248. break;
  249. default:
  250. abort ();
  251. }
  252. if (decoded == 0)
  253. {
  254. *val = decoded;
  255. return p;
  256. }
  257. if ((encoding & 0x70) == DW_EH_PE_pcrel)
  258. decoded += ((_Unwind_Internal_Ptr)origp);
  259. else
  260. decoded += base;
  261. if ((encoding & DW_EH_PE_indirect) != 0)
  262. decoded = *(_Unwind_Internal_Ptr *)decoded;
  263. }
  264. *val = decoded;
  265. return p;
  266. }
  267. static inline int
  268. value_size (uint8_t encoding)
  269. {
  270. switch (encoding & 0x0f)
  271. {
  272. case DW_EH_PE_sdata2:
  273. case DW_EH_PE_udata2:
  274. return 2;
  275. case DW_EH_PE_sdata4:
  276. case DW_EH_PE_udata4:
  277. return 4;
  278. case DW_EH_PE_sdata8:
  279. case DW_EH_PE_udata8:
  280. return 8;
  281. case DW_EH_PE_absptr:
  282. return sizeof(uintptr);
  283. default:
  284. break;
  285. }
  286. abort ();
  287. }
  288. /* The rest of this code is really similar to gcc/unwind-c.c and
  289. libjava/exception.cc. */
  290. typedef struct
  291. {
  292. _Unwind_Ptr Start;
  293. _Unwind_Ptr LPStart;
  294. _Unwind_Ptr ttype_base;
  295. const unsigned char *TType;
  296. const unsigned char *action_table;
  297. unsigned char ttype_encoding;
  298. unsigned char call_site_encoding;
  299. } lsda_header_info;
  300. static const unsigned char *
  301. parse_lsda_header (struct _Unwind_Context *context, const unsigned char *p,
  302. lsda_header_info *info)
  303. {
  304. _uleb128_t tmp;
  305. unsigned char lpstart_encoding;
  306. info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
  307. /* Find @LPStart, the base to which landing pad offsets are relative. */
  308. lpstart_encoding = *p++;
  309. if (lpstart_encoding != DW_EH_PE_omit)
  310. p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
  311. else
  312. info->LPStart = info->Start;
  313. /* Find @TType, the base of the handler and exception spec type data. */
  314. info->ttype_encoding = *p++;
  315. if (info->ttype_encoding != DW_EH_PE_omit)
  316. {
  317. p = read_uleb128 (p, &tmp);
  318. info->TType = p + tmp;
  319. }
  320. else
  321. info->TType = 0;
  322. /* The encoding and length of the call-site table; the action table
  323. immediately follows. */
  324. info->call_site_encoding = *p++;
  325. p = read_uleb128 (p, &tmp);
  326. info->action_table = p + tmp;
  327. return p;
  328. }
  329. /* The personality function is invoked when unwinding the stack due to
  330. a panic. Its job is to find the cleanup and exception handlers to
  331. run. We can't split the stack here, because we won't be able to
  332. unwind from that split. */
  333. #ifdef __ARM_EABI_UNWINDER__
  334. /* ARM EABI personality routines must also unwind the stack. */
  335. #define CONTINUE_UNWINDING \
  336. do \
  337. { \
  338. if (__gnu_unwind_frame (ue_header, context) != _URC_OK) \
  339. return _URC_FAILURE; \
  340. return _URC_CONTINUE_UNWIND; \
  341. } \
  342. while (0)
  343. #else
  344. #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
  345. #endif
  346. #ifdef __ARM_EABI_UNWINDER__
  347. #define STOP_UNWINDING _URC_FAILURE
  348. #else
  349. #define STOP_UNWINDING _URC_NORMAL_STOP
  350. #endif
  351. #ifdef __USING_SJLJ_EXCEPTIONS__
  352. #define PERSONALITY_FUNCTION __gccgo_personality_sj0
  353. #define __builtin_eh_return_data_regno(x) x
  354. #else
  355. #define PERSONALITY_FUNCTION __gccgo_personality_v0
  356. #endif
  357. #ifdef __ARM_EABI_UNWINDER__
  358. _Unwind_Reason_Code
  359. PERSONALITY_FUNCTION (_Unwind_State, struct _Unwind_Exception *,
  360. struct _Unwind_Context *)
  361. __attribute__ ((no_split_stack, flatten, target ("general-regs-only")));
  362. _Unwind_Reason_Code
  363. PERSONALITY_FUNCTION (_Unwind_State state,
  364. struct _Unwind_Exception * ue_header,
  365. struct _Unwind_Context * context)
  366. #else
  367. _Unwind_Reason_Code
  368. PERSONALITY_FUNCTION (int, _Unwind_Action, _Unwind_Exception_Class,
  369. struct _Unwind_Exception *, struct _Unwind_Context *)
  370. __attribute__ ((no_split_stack, flatten));
  371. _Unwind_Reason_Code
  372. PERSONALITY_FUNCTION (int version,
  373. _Unwind_Action actions,
  374. _Unwind_Exception_Class exception_class,
  375. struct _Unwind_Exception *ue_header,
  376. struct _Unwind_Context *context)
  377. #endif
  378. {
  379. lsda_header_info info;
  380. const unsigned char *language_specific_data, *p, *action_record;
  381. _Unwind_Ptr landing_pad, ip;
  382. int ip_before_insn = 0;
  383. _Bool is_foreign;
  384. G *g;
  385. #ifdef __ARM_EABI_UNWINDER__
  386. _Unwind_Action actions;
  387. switch (state & _US_ACTION_MASK)
  388. {
  389. case _US_VIRTUAL_UNWIND_FRAME:
  390. if (state & _UA_FORCE_UNWIND)
  391. /* We are called from _Unwind_Backtrace. No handler to run. */
  392. CONTINUE_UNWINDING;
  393. actions = _UA_SEARCH_PHASE;
  394. break;
  395. case _US_UNWIND_FRAME_STARTING:
  396. actions = _UA_CLEANUP_PHASE;
  397. if (!(state & _US_FORCE_UNWIND)
  398. && ue_header->barrier_cache.sp == _Unwind_GetGR(context, 13))
  399. actions |= _UA_HANDLER_FRAME;
  400. break;
  401. case _US_UNWIND_FRAME_RESUME:
  402. CONTINUE_UNWINDING;
  403. break;
  404. default:
  405. abort();
  406. }
  407. actions |= state & _US_FORCE_UNWIND;
  408. is_foreign = 0;
  409. /* The dwarf unwinder assumes the context structure holds things like the
  410. function and LSDA pointers. The ARM implementation caches these in
  411. the exception header (UCB). To avoid rewriting everything we make the
  412. virtual IP register point at the UCB. */
  413. ip = (_Unwind_Ptr) ue_header;
  414. _Unwind_SetGR (context, 12, ip);
  415. #else
  416. if (version != 1)
  417. return _URC_FATAL_PHASE1_ERROR;
  418. is_foreign = exception_class != __go_exception_class;
  419. #endif
  420. language_specific_data = (const unsigned char *)
  421. _Unwind_GetLanguageSpecificData (context);
  422. /* If no LSDA, then there are no handlers or cleanups. */
  423. if (! language_specific_data)
  424. CONTINUE_UNWINDING;
  425. /* Parse the LSDA header. */
  426. p = parse_lsda_header (context, language_specific_data, &info);
  427. #ifdef HAVE_GETIPINFO
  428. ip = _Unwind_GetIPInfo (context, &ip_before_insn);
  429. #else
  430. ip = _Unwind_GetIP (context);
  431. #endif
  432. if (! ip_before_insn)
  433. --ip;
  434. landing_pad = 0;
  435. action_record = NULL;
  436. #ifdef __USING_SJLJ_EXCEPTIONS__
  437. /* The given "IP" is an index into the call-site table, with two
  438. exceptions -- -1 means no-action, and 0 means terminate. But
  439. since we're using uleb128 values, we've not got random access
  440. to the array. */
  441. if ((int) ip <= 0)
  442. return _URC_CONTINUE_UNWIND;
  443. else
  444. {
  445. _uleb128_t cs_lp, cs_action;
  446. do
  447. {
  448. p = read_uleb128 (p, &cs_lp);
  449. p = read_uleb128 (p, &cs_action);
  450. }
  451. while (--ip);
  452. /* Can never have null landing pad for sjlj -- that would have
  453. been indicated by a -1 call site index. */
  454. landing_pad = (_Unwind_Ptr)cs_lp + 1;
  455. if (cs_action)
  456. action_record = info.action_table + cs_action - 1;
  457. goto found_something;
  458. }
  459. #else
  460. /* Search the call-site table for the action associated with this IP. */
  461. while (p < info.action_table)
  462. {
  463. _Unwind_Ptr cs_start, cs_len, cs_lp;
  464. _uleb128_t cs_action;
  465. /* Note that all call-site encodings are "absolute" displacements. */
  466. p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
  467. p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
  468. p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
  469. p = read_uleb128 (p, &cs_action);
  470. /* The table is sorted, so if we've passed the ip, stop. */
  471. if (ip < info.Start + cs_start)
  472. p = info.action_table;
  473. else if (ip < info.Start + cs_start + cs_len)
  474. {
  475. if (cs_lp)
  476. landing_pad = info.LPStart + cs_lp;
  477. if (cs_action)
  478. action_record = info.action_table + cs_action - 1;
  479. goto found_something;
  480. }
  481. }
  482. #endif
  483. /* IP is not in table. No associated cleanups. */
  484. CONTINUE_UNWINDING;
  485. found_something:
  486. if (landing_pad == 0)
  487. {
  488. /* IP is present, but has a null landing pad.
  489. No handler to be run. */
  490. CONTINUE_UNWINDING;
  491. }
  492. if (actions & _UA_SEARCH_PHASE)
  493. {
  494. if (action_record == 0)
  495. {
  496. /* This indicates a cleanup rather than an exception
  497. handler. */
  498. CONTINUE_UNWINDING;
  499. }
  500. return _URC_HANDLER_FOUND;
  501. }
  502. /* It's possible for g to be NULL here for an exception thrown by a
  503. language other than Go. */
  504. g = runtime_g ();
  505. if (g == NULL)
  506. {
  507. if (!is_foreign)
  508. abort ();
  509. }
  510. else
  511. {
  512. g->exception = ue_header;
  513. g->isforeign = is_foreign;
  514. }
  515. _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
  516. (_Unwind_Ptr) ue_header);
  517. _Unwind_SetGR (context, __builtin_eh_return_data_regno (1), 0);
  518. _Unwind_SetIP (context, landing_pad);
  519. return _URC_INSTALL_CONTEXT;
  520. }
  521. // A dummy personality function, which doesn't capture any exception
  522. // and simply passes by. This is used for functions that don't
  523. // capture exceptions but need LSDA for stack maps.
  524. _Unwind_Reason_Code
  525. __gccgo_personality_dummy (int, _Unwind_Action, _Unwind_Exception_Class,
  526. struct _Unwind_Exception *, struct _Unwind_Context *)
  527. __attribute__ ((no_split_stack));
  528. _Unwind_Reason_Code
  529. #ifdef __ARM_EABI_UNWINDER__
  530. __attribute__ ((target ("general-regs-only")))
  531. #endif
  532. __gccgo_personality_dummy (int version __attribute__ ((unused)),
  533. _Unwind_Action actions __attribute__ ((unused)),
  534. _Unwind_Exception_Class exception_class __attribute__ ((unused)),
  535. struct _Unwind_Exception *ue_header __attribute__ ((unused)),
  536. struct _Unwind_Context *context __attribute__ ((unused)))
  537. {
  538. CONTINUE_UNWINDING;
  539. }
  540. // A sentinel value for Go functions.
  541. // A function is a Go function if it has LSDA, which has type info,
  542. // and the first (dummy) landing pad's type info is a pointer to
  543. // this value.
  544. #define GO_FUNC_SENTINEL ((uint64)'G' | ((uint64)'O'<<8) | \
  545. ((uint64)'.'<<16) | ((uint64)'.'<<24) | \
  546. ((uint64)'F'<<32) | ((uint64)'U'<<40) | \
  547. ((uint64)'N'<<48) | ((uint64)'C'<<56))
  548. struct _stackmap {
  549. uint32 len;
  550. uint8 data[1]; // variabe length
  551. };
  552. extern void
  553. runtime_scanstackblockwithmap (uintptr ip, uintptr sp, uintptr size, uint8 *ptrmask, void* gcw)
  554. __asm__ (GOSYM_PREFIX "runtime.scanstackblockwithmap");
  555. #define FOUND 0
  556. #define NOTFOUND_OK 1
  557. #define NOTFOUND_BAD 2
  558. // Helper function to search for stack maps in the unwinding records of a frame.
  559. // If found, populate ip, sp, and stackmap. Returns the #define'd values above.
  560. static int
  561. findstackmaps (struct _Unwind_Context *context, _Unwind_Ptr *ip, _Unwind_Ptr *sp, struct _stackmap **stackmap)
  562. {
  563. lsda_header_info info;
  564. const unsigned char *language_specific_data, *p, *action_record;
  565. bool first;
  566. struct _stackmap *stackmap1;
  567. _Unwind_Ptr ip1;
  568. int ip_before_insn = 0;
  569. _sleb128_t index;
  570. int size;
  571. #ifdef HAVE_GETIPINFO
  572. ip1 = _Unwind_GetIPInfo (context, &ip_before_insn);
  573. #else
  574. ip1 = _Unwind_GetIP (context);
  575. #endif
  576. if (! ip_before_insn)
  577. --ip1;
  578. if (ip != NULL)
  579. *ip = ip1;
  580. if (sp != NULL)
  581. *sp = _Unwind_GetCFA (context);
  582. #ifdef __ARM_EABI_UNWINDER__
  583. {
  584. _Unwind_Control_Block *ucbp;
  585. ucbp = (_Unwind_Control_Block *) _Unwind_GetGR (context, 12);
  586. if (*ucbp->pr_cache.ehtp & (1u << 31))
  587. // The "compact" model is used, with one of the predefined
  588. // personality functions. It doesn't have standard LSDA.
  589. return NOTFOUND_OK;
  590. }
  591. #endif
  592. language_specific_data = (const unsigned char *)
  593. _Unwind_GetLanguageSpecificData (context);
  594. /* If no LSDA, then there is no stack maps. */
  595. if (! language_specific_data)
  596. return NOTFOUND_OK;
  597. p = parse_lsda_header (context, language_specific_data, &info);
  598. if (info.TType == NULL)
  599. return NOTFOUND_OK;
  600. size = value_size (info.ttype_encoding);
  601. action_record = NULL;
  602. first = true;
  603. /* Search the call-site table for the action associated with this IP. */
  604. while (p < info.action_table)
  605. {
  606. _Unwind_Ptr cs_start, cs_len, cs_lp;
  607. _uleb128_t cs_action;
  608. /* Note that all call-site encodings are "absolute" displacements. */
  609. p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
  610. p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
  611. p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
  612. p = read_uleb128 (p, &cs_action);
  613. if (first)
  614. {
  615. // For a Go function, the first entry points to the sentinel value.
  616. // Check this here.
  617. const unsigned char *p1, *action1;
  618. uint64 *x;
  619. if (!cs_action)
  620. return NOTFOUND_OK;
  621. action1 = info.action_table + cs_action - 1;
  622. read_sleb128 (action1, &index);
  623. p1 = info.TType - index*size;
  624. read_encoded_value (context, info.ttype_encoding, p1, (_Unwind_Ptr*)&x);
  625. if (x == NULL || *x != GO_FUNC_SENTINEL)
  626. return NOTFOUND_OK;
  627. first = false;
  628. continue;
  629. }
  630. /* The table is sorted, so if we've passed the ip, stop. */
  631. if (ip1 < info.Start + cs_start)
  632. return NOTFOUND_BAD;
  633. else if (ip1 < info.Start + cs_start + cs_len)
  634. {
  635. if (cs_action)
  636. action_record = info.action_table + cs_action - 1;
  637. break;
  638. }
  639. }
  640. if (action_record == NULL)
  641. return NOTFOUND_BAD;
  642. read_sleb128 (action_record, &index);
  643. p = info.TType - index*size;
  644. read_encoded_value (context, info.ttype_encoding, p, (_Unwind_Ptr*)&stackmap1);
  645. if (stackmap1 == NULL)
  646. return NOTFOUND_BAD;
  647. if (stackmap != NULL)
  648. *stackmap = stackmap1;
  649. return FOUND;
  650. }
  651. struct scanstate {
  652. void* gcw; // the GC worker, passed into scanstackwithmap_callback
  653. uintptr lastsp; // the last (outermost) SP of Go function seen in a traceback, set by the callback
  654. };
  655. // Callback function to scan a stack frame with stack maps.
  656. // It skips non-Go functions.
  657. static _Unwind_Reason_Code
  658. scanstackwithmap_callback (struct _Unwind_Context *context, void *arg)
  659. {
  660. struct _stackmap *stackmap;
  661. _Unwind_Ptr ip, sp;
  662. G* gp;
  663. struct scanstate* state = (struct scanstate*) arg;
  664. void *gcw;
  665. gp = runtime_g ();
  666. gcw = state->gcw;
  667. switch (findstackmaps (context, &ip, &sp, &stackmap))
  668. {
  669. case NOTFOUND_OK:
  670. // Not a Go function. Skip this frame.
  671. return _URC_NO_REASON;
  672. case NOTFOUND_BAD:
  673. {
  674. // No stack map found.
  675. // If we're scanning from the signal stack, the goroutine
  676. // may be not stopped at a safepoint. Allow this case.
  677. if (gp != gp->m->gsignal)
  678. {
  679. // TODO: print gp, pc, sp
  680. runtime_throw ("no stack map");
  681. }
  682. return STOP_UNWINDING;
  683. }
  684. case FOUND:
  685. break;
  686. default:
  687. abort ();
  688. }
  689. state->lastsp = sp;
  690. runtime_scanstackblockwithmap (ip, sp, (uintptr)(stackmap->len) * sizeof(uintptr), stackmap->data, gcw);
  691. return _URC_NO_REASON;
  692. }
  693. // Scan the stack with stack maps. Return whether the scan
  694. // succeeded.
  695. bool
  696. scanstackwithmap (void *gcw)
  697. {
  698. _Unwind_Reason_Code code;
  699. bool ret;
  700. struct scanstate state;
  701. G* gp;
  702. G* curg;
  703. state.gcw = gcw;
  704. state.lastsp = 0;
  705. gp = runtime_g ();
  706. curg = gp->m->curg;
  707. runtime_xadd (&__go_runtime_in_callers, 1);
  708. code = _Unwind_Backtrace (scanstackwithmap_callback, (void*)&state);
  709. runtime_xadd (&__go_runtime_in_callers, -1);
  710. ret = (code == _URC_END_OF_STACK);
  711. if (ret && gp == gp->m->gsignal)
  712. {
  713. // For signal-triggered scan, the unwinder may not be able to unwind
  714. // the whole stack while it still reports _URC_END_OF_STACK (e.g.
  715. // signal is delivered in vdso). Check that we actually reached the
  716. // the end of the stack, that is, the SP on entry.
  717. if (state.lastsp != curg->entrysp)
  718. ret = false;
  719. }
  720. return ret;
  721. }
  722. // Returns whether stack map is enabled.
  723. bool
  724. usestackmaps ()
  725. {
  726. return runtime_usestackmaps;
  727. }
  728. // Callback function to probe if a stack frame has stack maps.
  729. static _Unwind_Reason_Code
  730. probestackmaps_callback (struct _Unwind_Context *context,
  731. void *arg __attribute__ ((unused)))
  732. {
  733. switch (findstackmaps (context, NULL, NULL, NULL))
  734. {
  735. case NOTFOUND_OK:
  736. case NOTFOUND_BAD:
  737. return _URC_NO_REASON;
  738. case FOUND:
  739. break;
  740. default:
  741. abort ();
  742. }
  743. // Found a stack map. No need to keep unwinding.
  744. runtime_usestackmaps = true;
  745. return STOP_UNWINDING;
  746. }
  747. // Try to find a stack map, store the result in global variable runtime_usestackmaps.
  748. // Called in start-up time from Go code, so there is a Go frame on the stack.
  749. bool
  750. probestackmaps ()
  751. {
  752. runtime_usestackmaps = false;
  753. _Unwind_Backtrace (probestackmaps_callback, NULL);
  754. return runtime_usestackmaps;
  755. }