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