* Add TARGET_ATOMIC_ASSIGN_EXPAND_FENV hook for sh.
[official-gcc.git] / libgo / runtime / go-recover.c
blobfc66f61cab325c3dabba03bdfc266d5a15909d8f
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. */
7 #include "runtime.h"
8 #include "interface.h"
9 #include "go-panic.h"
10 #include "go-defer.h"
12 /* If the top of the defer stack can be recovered, then return it.
13 Otherwise return NULL. */
15 static struct __go_defer_stack *
16 current_defer ()
18 G *g;
19 struct __go_defer_stack *d;
21 g = runtime_g ();
23 d = g->defer;
24 if (d == NULL)
25 return NULL;
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;
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;
39 return d;
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. */
48 _Bool
49 __go_can_recover (void *retaddr)
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;
61 d = current_defer ();
62 if (d == NULL)
63 return 0;
65 ret = (const char *) __builtin_extract_return_addr (retaddr);
67 dret = (const char *) d->__retaddr;
68 if (ret <= dret && ret + 16 >= dret)
69 return 1;
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. */
82 if (runtime_callers (1, &locs[0], 2, false) < 2)
83 return 0;
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
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;
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)
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;
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;
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++)
126 const byte *name;
128 if (locs[i].function.len == 0)
130 /* No function name means this caller isn't Go code. Assume
131 that this is libffi. */
132 continue;
135 /* Ignore functions in libffi. */
136 name = locs[i].function.str;
137 if (__builtin_strncmp ((const char *) name, "ffi_", 4) == 0)
138 continue;
140 if (found_ffi_callback)
141 break;
143 if (__builtin_strcmp ((const char *) name, "reflect.ffi_callback") == 0)
145 found_ffi_callback = 1;
146 continue;
149 if (__builtin_strcmp ((const char *) name, "reflect.makeFuncStub") == 0)
151 i++;
152 break;
155 /* Ignore other functions in the reflect package. */
156 if (__builtin_strncmp ((const char *) name, "reflect.", 8) == 0)
157 continue;
159 /* We should now be looking at the real caller. */
160 break;
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)
167 return 1;
170 return 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. */
178 void
179 __go_makefunc_can_recover (void *retaddr)
181 struct __go_defer_stack *d;
183 d = current_defer ();
184 if (d == NULL)
185 return;
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;
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. */
203 void
204 __go_makefunc_ffi_can_recover (struct Location *loc, int n)
206 struct __go_defer_stack *d;
207 const byte *name;
208 intgo len;
210 d = current_defer ();
211 if (d == NULL)
212 return;
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;
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. */
223 if (n < 2)
224 return;
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;
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. */
243 void
244 __go_makefunc_returning (void)
246 struct __go_defer_stack *d;
248 d = runtime_g ()->defer;
249 if (d != NULL)
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
257 __go_recover ()
259 G *g;
260 struct __go_panic_stack *p;
262 g = runtime_g ();
264 if (g->panic == NULL || g->panic->__was_recovered)
266 struct __go_empty_interface ret;
268 ret.__type_descriptor = NULL;
269 ret.__object = NULL;
270 return ret;
272 p = g->panic;
273 p->__was_recovered = 1;
274 return p->__arg;