PR fortran/77666
[official-gcc.git] / libgo / runtime / go-recover.c
blob4d2f0f9fc39b084bb9b9deba55656fe7c3706eb0
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"
11 /* If the top of the defer stack can be recovered, then return it.
12 Otherwise return NULL. */
14 static Defer *
15 current_defer ()
17 G *g;
18 Defer *d;
20 g = runtime_g ();
22 d = g->_defer;
23 if (d == NULL)
24 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)
30 return NULL;
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
34 not recover. */
35 if (d->retaddr == 0)
36 return NULL;
38 return d;
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. */
47 _Bool
48 __go_can_recover (void *retaddr)
50 Defer *d;
51 const char* ret;
52 const char* dret;
53 Location locs[16];
54 const byte *name;
55 intgo len;
56 int n;
57 int i;
58 _Bool found_ffi_callback;
60 d = current_defer ();
61 if (d == NULL)
62 return 0;
64 ret = (const char *) __builtin_extract_return_addr (retaddr);
66 dret = (const char *) (uintptr) d->retaddr;
67 if (ret <= dret && ret + 16 >= dret)
68 return 1;
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)
76 __go_can_recover
77 Calling runtime_callers will skip the thunks. So if our caller's
78 caller starts with __go, then we are permitted to call
79 recover. */
81 if (runtime_callers (1, &locs[0], 2, false) < 2)
82 return 0;
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
88 NUL-terminated. */
89 if (len > 4
90 && __builtin_strchr ((const char *) name, '.') == NULL
91 && __builtin_strncmp ((const char *) name, "__go_", 4) == 0)
92 return 1;
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)
101 return 0;
102 name = locs[0].function.str;
103 len = locs[0].function.len;
104 if (len > 4
105 && __builtin_strchr ((const char *) name, '.') == NULL
106 && __builtin_strncmp ((const char *) name, "__go_", 4) == 0)
107 return 1;
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)
114 return 0;
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++)
125 const byte *name;
127 if (locs[i].function.len == 0)
129 /* No function name means this caller isn't Go code. Assume
130 that this is libffi. */
131 continue;
134 /* Ignore functions in libffi. */
135 name = locs[i].function.str;
136 if (__builtin_strncmp ((const char *) name, "ffi_", 4) == 0)
137 continue;
139 if (found_ffi_callback)
140 break;
142 if (__builtin_strcmp ((const char *) name, "reflect.ffi_callback") == 0)
144 found_ffi_callback = 1;
145 continue;
148 if (__builtin_strcmp ((const char *) name, "reflect.makeFuncStub") == 0)
150 i++;
151 break;
154 /* Ignore other functions in the reflect package. */
155 if (__builtin_strncmp ((const char *) name, "reflect.", 8) == 0)
156 continue;
158 /* We should now be looking at the real caller. */
159 break;
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)
166 return 1;
169 return 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. */
177 void
178 __go_makefunc_can_recover (void *retaddr)
180 Defer *d;
182 d = current_defer ();
183 if (d == NULL)
184 return;
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)
189 return;
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. */
202 void
203 __go_makefunc_ffi_can_recover (struct location *loc, int n)
205 Defer *d;
206 const byte *name;
207 intgo len;
209 d = current_defer ();
210 if (d == NULL)
211 return;
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)
216 return;
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
220 by defer. */
222 if (n < 2)
223 return;
225 name = (loc + 1)->function.str;
226 len = (loc + 1)->function.len;
227 if (len > 4
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. */
242 void
243 __go_makefunc_returning (void)
245 Defer *d;
247 d = runtime_g ()->_defer;
248 if (d != NULL)
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
256 __go_recover ()
258 G *g;
259 Panic *p;
261 g = runtime_g ();
263 if (g->_panic == NULL || g->_panic->recovered)
265 struct __go_empty_interface ret;
267 ret.__type_descriptor = NULL;
268 ret.__object = NULL;
269 return ret;
271 p = g->_panic;
272 p->recovered = 1;
273 return p->arg;