1 /* go-recover.c -- support for the go recover function.
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. */
12 /* If the top of the defer stack can be recovered, then return it.
13 Otherwise return NULL. */
15 static struct __go_defer_stack
*
19 struct __go_defer_stack
*d
;
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
)
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
36 if (d
->__retaddr
== NULL
)
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. */
49 __go_can_recover (void *retaddr
)
51 struct __go_defer_stack
*d
;
59 _Bool found_ffi_callback
;
65 ret
= (const char *) __builtin_extract_return_addr (retaddr
);
67 dret
= (const char *) d
->__retaddr
;
68 if (ret
<= dret
&& ret
+ 16 >= dret
)
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)
78 Calling runtime_callers will skip the thunks. So if our caller's
79 caller starts with __go, then we are permitted to call
82 if (runtime_callers (1, &locs
[0], 2, false) < 2)
85 name
= locs
[1].function
.str
;
86 len
= locs
[1].function
.len
;
88 /* Although locs[1].function is a Go string, we know it is
91 && __builtin_strchr ((const char *) name
, '.') == NULL
92 && __builtin_strncmp ((const char *) name
, "__go_", 4) == 0)
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)
101 if (runtime_callers (3, &locs
[0], 1, false) < 1)
103 name
= locs
[0].function
.str
;
104 len
= locs
[0].function
.len
;
106 && __builtin_strchr ((const char *) name
, '.') == NULL
107 && __builtin_strncmp ((const char *) name
, "__go_", 4) == 0)
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
)
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. */
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
++)
128 if (locs
[i
].function
.len
== 0)
130 /* No function name means this caller isn't Go code. Assume
131 that this is libffi. */
135 /* Ignore functions in libffi. */
136 name
= locs
[i
].function
.str
;
137 if (__builtin_strncmp ((const char *) name
, "ffi_", 4) == 0)
140 if (found_ffi_callback
)
143 if (__builtin_strcmp ((const char *) name
, "reflect.ffi_callback") == 0)
145 found_ffi_callback
= 1;
149 if (__builtin_strcmp ((const char *) name
, "reflect.makeFuncStub") == 0)
155 /* Ignore other functions in the reflect package. */
156 if (__builtin_strncmp ((const char *) name
, "reflect.", 8) == 0)
159 /* We should now be looking at the real caller. */
163 if (i
< n
&& locs
[i
].function
.len
> 0)
165 name
= locs
[i
].function
.str
;
166 if (__builtin_strncmp ((const char *) name
, "__go_", 4) == 0)
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. */
179 __go_makefunc_can_recover (void *retaddr
)
181 struct __go_defer_stack
*d
;
183 d
= current_defer ();
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
)
192 if (__go_can_recover (retaddr
))
193 d
->__makefunc_can_recover
= 1;
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. */
204 __go_makefunc_ffi_can_recover (struct Location
*loc
, int n
)
206 struct __go_defer_stack
*d
;
210 d
= current_defer ();
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
)
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
226 name
= (loc
+ 1)->function
.str
;
227 len
= (loc
+ 1)->function
.len
;
229 && __builtin_strchr ((const char *) name
, '.') == NULL
230 && __builtin_strncmp ((const char *) name
, "__go_", 4) == 0)
231 d
->__makefunc_can_recover
= 1;
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. */
244 __go_makefunc_returning (void)
246 struct __go_defer_stack
*d
;
248 d
= runtime_g ()->defer
;
250 d
->__makefunc_can_recover
= 0;
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. */
256 struct __go_empty_interface
260 struct __go_panic_stack
*p
;
264 if (g
->panic
== NULL
|| g
->panic
->__was_recovered
)
266 struct __go_empty_interface ret
;
268 ret
.__type_descriptor
= NULL
;
273 p
->__was_recovered
= 1;