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. */
11 /* If the top of the defer stack can be recovered, then return it.
12 Otherwise return NULL. */
26 /* The panic which would be recovered is the one on the top of the
27 panic stack. We do not want to recover it if that panic was on
28 the top of the panic stack when this function was deferred. */
29 if (d
->_panic
== g
->_panic
)
32 /* The deferred thunk will call _go_set_defer_retaddr. If this has
33 not happened, then we have not been called via defer, and we can
41 /* This is called by a thunk to see if the real function should be
42 permitted to recover a panic value. Recovering a value is
43 permitted if the thunk was called directly by defer. RETADDR is
44 the return address of the function which is calling
45 __go_can_recover--this is, the thunk. */
48 __go_can_recover (void *retaddr
)
58 _Bool found_ffi_callback
;
64 ret
= (const char *) __builtin_extract_return_addr (retaddr
);
66 dret
= (const char *) (uintptr
) d
->retaddr
;
67 if (ret
<= dret
&& ret
+ 16 >= dret
)
70 /* On some systems, in some cases, the return address does not work
71 reliably. See http://gcc.gnu.org/PR60406. If we are permitted
72 to call recover, the call stack will look like this:
73 __go_panic, __go_undefer, etc.
74 thunk to call deferred function (calls __go_set_defer_retaddr)
75 function that calls __go_can_recover (passing return address)
77 Calling runtime_callers will skip the thunks. So if our caller's
78 caller starts with __go, then we are permitted to call
81 if (runtime_callers (1, &locs
[0], 2, false) < 2)
84 name
= locs
[1].function
.str
;
85 len
= locs
[1].function
.len
;
87 /* Although locs[1].function is a Go string, we know it is
90 && __builtin_strchr ((const char *) name
, '.') == NULL
91 && __builtin_strncmp ((const char *) name
, "__go_", 4) == 0)
94 /* If we are called from __go_makefunc_can_recover, then we need to
95 look one level higher. */
96 if (locs
[0].function
.len
> 0
97 && __builtin_strcmp ((const char *) locs
[0].function
.str
,
98 "__go_makefunc_can_recover") == 0)
100 if (runtime_callers (3, &locs
[0], 1, false) < 1)
102 name
= locs
[0].function
.str
;
103 len
= locs
[0].function
.len
;
105 && __builtin_strchr ((const char *) name
, '.') == NULL
106 && __builtin_strncmp ((const char *) name
, "__go_", 4) == 0)
110 /* If the function calling recover was created by reflect.MakeFunc,
111 then __go_makefunc_can_recover or __go_makefunc_ffi_can_recover
112 will have set the __makefunc_can_recover field. */
113 if (!d
->makefunccanrecover
)
116 /* We look up the stack, ignoring libffi functions and functions in
117 the reflect package, until we find reflect.makeFuncStub or
118 reflect.ffi_callback called by FFI functions. Then we check the
119 caller of that function. */
121 n
= runtime_callers (2, &locs
[0], sizeof locs
/ sizeof locs
[0], false);
122 found_ffi_callback
= 0;
123 for (i
= 0; i
< n
; i
++)
127 if (locs
[i
].function
.len
== 0)
129 /* No function name means this caller isn't Go code. Assume
130 that this is libffi. */
134 /* Ignore functions in libffi. */
135 name
= locs
[i
].function
.str
;
136 if (__builtin_strncmp ((const char *) name
, "ffi_", 4) == 0)
139 if (found_ffi_callback
)
142 if (__builtin_strcmp ((const char *) name
, "reflect.ffi_callback") == 0)
144 found_ffi_callback
= 1;
148 if (__builtin_strcmp ((const char *) name
, "reflect.makeFuncStub") == 0)
154 /* Ignore other functions in the reflect package. */
155 if (__builtin_strncmp ((const char *) name
, "reflect.", 8) == 0)
158 /* We should now be looking at the real caller. */
162 if (i
< n
&& locs
[i
].function
.len
> 0)
164 name
= locs
[i
].function
.str
;
165 if (__builtin_strncmp ((const char *) name
, "__go_", 4) == 0)
172 /* This function is called when code is about to enter a function
173 created by reflect.MakeFunc. It is called by the function stub
174 used by MakeFunc. If the stub is permitted to call recover, then a
175 real MakeFunc function is permitted to call recover. */
178 __go_makefunc_can_recover (void *retaddr
)
182 d
= current_defer ();
186 /* If we are already in a call stack of MakeFunc functions, there is
187 nothing we can usefully check here. */
188 if (d
->makefunccanrecover
)
191 if (__go_can_recover (retaddr
))
192 d
->makefunccanrecover
= 1;
195 /* This function is called when code is about to enter a function
196 created by the libffi version of reflect.MakeFunc. This function
197 is passed the names of the callers of the libffi code that called
198 the stub. It uses to decide whether it is permitted to call
199 recover, and sets d->makefunccanrecover so that __go_recover can
200 make the same decision. */
203 __go_makefunc_ffi_can_recover (struct location
*loc
, int n
)
209 d
= current_defer ();
213 /* If we are already in a call stack of MakeFunc functions, there is
214 nothing we can usefully check here. */
215 if (d
->makefunccanrecover
)
218 /* LOC points to the caller of our caller. That will be a thunk.
219 If its caller was a runtime function, then it was called directly
225 name
= (loc
+ 1)->function
.str
;
226 len
= (loc
+ 1)->function
.len
;
228 && __builtin_strchr ((const char *) name
, '.') == NULL
229 && __builtin_strncmp ((const char *) name
, "__go_", 4) == 0)
230 d
->makefunccanrecover
= 1;
233 /* This function is called when code is about to exit a function
234 created by reflect.MakeFunc. It is called by the function stub
235 used by MakeFunc. It clears the makefunccanrecover field. It's OK
236 to always clear this field, because __go_can_recover will only be
237 called by a stub created for a function that calls recover. That
238 stub will not call a function created by reflect.MakeFunc, so by
239 the time we get here any caller higher up on the call stack no
240 longer needs the information. */
243 __go_makefunc_returning (void)
247 d
= runtime_g ()->_defer
;
249 d
->makefunccanrecover
= 0;
252 /* This is only called when it is valid for the caller to recover the
253 value on top of the panic stack, if there is one. */
255 struct __go_empty_interface
263 if (g
->_panic
== NULL
|| g
->_panic
->recovered
)
265 struct __go_empty_interface ret
;
267 ret
.__type_descriptor
= NULL
;