go-recover.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* go-recover.c -- support for the go recover function.
  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 "runtime.h"
  6. #include "interface.h"
  7. #include "go-panic.h"
  8. #include "go-defer.h"
  9. /* If the top of the defer stack can be recovered, then return it.
  10. Otherwise return NULL. */
  11. static struct __go_defer_stack *
  12. current_defer ()
  13. {
  14. G *g;
  15. struct __go_defer_stack *d;
  16. g = runtime_g ();
  17. d = g->defer;
  18. if (d == NULL)
  19. return NULL;
  20. /* The panic which would be recovered is the one on the top of the
  21. panic stack. We do not want to recover it if that panic was on
  22. the top of the panic stack when this function was deferred. */
  23. if (d->__panic == g->panic)
  24. return NULL;
  25. /* The deferred thunk will call _go_set_defer_retaddr. If this has
  26. not happened, then we have not been called via defer, and we can
  27. not recover. */
  28. if (d->__retaddr == NULL)
  29. return NULL;
  30. return d;
  31. }
  32. /* This is called by a thunk to see if the real function should be
  33. permitted to recover a panic value. Recovering a value is
  34. permitted if the thunk was called directly by defer. RETADDR is
  35. the return address of the function which is calling
  36. __go_can_recover--this is, the thunk. */
  37. _Bool
  38. __go_can_recover (void *retaddr)
  39. {
  40. struct __go_defer_stack *d;
  41. const char* ret;
  42. const char* dret;
  43. Location locs[16];
  44. const byte *name;
  45. intgo len;
  46. int n;
  47. int i;
  48. _Bool found_ffi_callback;
  49. d = current_defer ();
  50. if (d == NULL)
  51. return 0;
  52. ret = (const char *) __builtin_extract_return_addr (retaddr);
  53. dret = (const char *) d->__retaddr;
  54. if (ret <= dret && ret + 16 >= dret)
  55. return 1;
  56. /* On some systems, in some cases, the return address does not work
  57. reliably. See http://gcc.gnu.org/PR60406. If we are permitted
  58. to call recover, the call stack will look like this:
  59. __go_panic, __go_undefer, etc.
  60. thunk to call deferred function (calls __go_set_defer_retaddr)
  61. function that calls __go_can_recover (passing return address)
  62. __go_can_recover
  63. Calling runtime_callers will skip the thunks. So if our caller's
  64. caller starts with __go, then we are permitted to call
  65. recover. */
  66. if (runtime_callers (1, &locs[0], 2, false) < 2)
  67. return 0;
  68. name = locs[1].function.str;
  69. len = locs[1].function.len;
  70. /* Although locs[1].function is a Go string, we know it is
  71. NUL-terminated. */
  72. if (len > 4
  73. && __builtin_strchr ((const char *) name, '.') == NULL
  74. && __builtin_strncmp ((const char *) name, "__go_", 4) == 0)
  75. return 1;
  76. /* If we are called from __go_makefunc_can_recover, then we need to
  77. look one level higher. */
  78. if (locs[0].function.len > 0
  79. && __builtin_strcmp ((const char *) locs[0].function.str,
  80. "__go_makefunc_can_recover") == 0)
  81. {
  82. if (runtime_callers (3, &locs[0], 1, false) < 1)
  83. return 0;
  84. name = locs[0].function.str;
  85. len = locs[0].function.len;
  86. if (len > 4
  87. && __builtin_strchr ((const char *) name, '.') == NULL
  88. && __builtin_strncmp ((const char *) name, "__go_", 4) == 0)
  89. return 1;
  90. }
  91. /* If the function calling recover was created by reflect.MakeFunc,
  92. then __go_makefunc_can_recover or __go_makefunc_ffi_can_recover
  93. will have set the __makefunc_can_recover field. */
  94. if (!d->__makefunc_can_recover)
  95. return 0;
  96. /* We look up the stack, ignoring libffi functions and functions in
  97. the reflect package, until we find reflect.makeFuncStub or
  98. reflect.ffi_callback called by FFI functions. Then we check the
  99. caller of that function. */
  100. n = runtime_callers (2, &locs[0], sizeof locs / sizeof locs[0], false);
  101. found_ffi_callback = 0;
  102. for (i = 0; i < n; i++)
  103. {
  104. const byte *name;
  105. if (locs[i].function.len == 0)
  106. {
  107. /* No function name means this caller isn't Go code. Assume
  108. that this is libffi. */
  109. continue;
  110. }
  111. /* Ignore functions in libffi. */
  112. name = locs[i].function.str;
  113. if (__builtin_strncmp ((const char *) name, "ffi_", 4) == 0)
  114. continue;
  115. if (found_ffi_callback)
  116. break;
  117. if (__builtin_strcmp ((const char *) name, "reflect.ffi_callback") == 0)
  118. {
  119. found_ffi_callback = 1;
  120. continue;
  121. }
  122. if (__builtin_strcmp ((const char *) name, "reflect.makeFuncStub") == 0)
  123. {
  124. i++;
  125. break;
  126. }
  127. /* Ignore other functions in the reflect package. */
  128. if (__builtin_strncmp ((const char *) name, "reflect.", 8) == 0)
  129. continue;
  130. /* We should now be looking at the real caller. */
  131. break;
  132. }
  133. if (i < n && locs[i].function.len > 0)
  134. {
  135. name = locs[i].function.str;
  136. if (__builtin_strncmp ((const char *) name, "__go_", 4) == 0)
  137. return 1;
  138. }
  139. return 0;
  140. }
  141. /* This function is called when code is about to enter a function
  142. created by reflect.MakeFunc. It is called by the function stub
  143. used by MakeFunc. If the stub is permitted to call recover, then a
  144. real MakeFunc function is permitted to call recover. */
  145. void
  146. __go_makefunc_can_recover (void *retaddr)
  147. {
  148. struct __go_defer_stack *d;
  149. d = current_defer ();
  150. if (d == NULL)
  151. return;
  152. /* If we are already in a call stack of MakeFunc functions, there is
  153. nothing we can usefully check here. */
  154. if (d->__makefunc_can_recover)
  155. return;
  156. if (__go_can_recover (retaddr))
  157. d->__makefunc_can_recover = 1;
  158. }
  159. /* This function is called when code is about to enter a function
  160. created by the libffi version of reflect.MakeFunc. This function
  161. is passed the names of the callers of the libffi code that called
  162. the stub. It uses to decide whether it is permitted to call
  163. recover, and sets d->__makefunc_can_recover so that __go_recover
  164. can make the same decision. */
  165. void
  166. __go_makefunc_ffi_can_recover (struct Location *loc, int n)
  167. {
  168. struct __go_defer_stack *d;
  169. const byte *name;
  170. intgo len;
  171. d = current_defer ();
  172. if (d == NULL)
  173. return;
  174. /* If we are already in a call stack of MakeFunc functions, there is
  175. nothing we can usefully check here. */
  176. if (d->__makefunc_can_recover)
  177. return;
  178. /* LOC points to the caller of our caller. That will be a thunk.
  179. If its caller was a runtime function, then it was called directly
  180. by defer. */
  181. if (n < 2)
  182. return;
  183. name = (loc + 1)->function.str;
  184. len = (loc + 1)->function.len;
  185. if (len > 4
  186. && __builtin_strchr ((const char *) name, '.') == NULL
  187. && __builtin_strncmp ((const char *) name, "__go_", 4) == 0)
  188. d->__makefunc_can_recover = 1;
  189. }
  190. /* This function is called when code is about to exit a function
  191. created by reflect.MakeFunc. It is called by the function stub
  192. used by MakeFunc. It clears the __makefunc_can_recover field.
  193. It's OK to always clear this field, because __go_can_recover will
  194. only be called by a stub created for a function that calls recover.
  195. That stub will not call a function created by reflect.MakeFunc, so
  196. by the time we get here any caller higher up on the call stack no
  197. longer needs the information. */
  198. void
  199. __go_makefunc_returning (void)
  200. {
  201. struct __go_defer_stack *d;
  202. d = runtime_g ()->defer;
  203. if (d != NULL)
  204. d->__makefunc_can_recover = 0;
  205. }
  206. /* This is only called when it is valid for the caller to recover the
  207. value on top of the panic stack, if there is one. */
  208. struct __go_empty_interface
  209. __go_recover ()
  210. {
  211. G *g;
  212. struct __go_panic_stack *p;
  213. g = runtime_g ();
  214. if (g->panic == NULL || g->panic->__was_recovered)
  215. {
  216. struct __go_empty_interface ret;
  217. ret.__type_descriptor = NULL;
  218. ret.__object = NULL;
  219. return ret;
  220. }
  221. p = g->panic;
  222. p->__was_recovered = 1;
  223. return p->__arg;
  224. }