75791bab05
Use the new -fgo-c-header option to build a header file for the Go runtime code in libgo/go/runtime, and use the new header file in the C runtime code in libgo/runtime. This will ensure that the Go code and C code share the same data structures as we convert the runtime from C to Go. The new file libgo/go/runtime/runtime2.go is copied from the Go 1.7 release, and then edited to remove unnecessary data structures and modify others for use with libgo. The new file libgo/go/runtime/mcache.go is an initial version of the same files in the Go 1.7 release, and will be replaced by the Go 1.7 file when we convert to the new memory allocator. The new file libgo/go/runtime/type.go describes the gccgo version of the reflection data structures, and replaces the Go 1.7 runtime file which describes the gc version of those structures. Using the new header file means changing a number of struct fields to use Go naming conventions (that is, no underscores) and to rename constants to have a leading underscore so that they are not exported from the Go package. These names were updated in the C code. The C code was also changed to drop the thread-local variable m, as was done some time ago in the gc sources. Now the m field is always accessed using g->m, where g is the single remaining thread-local variable. This in turn required some adjustments to set g->m correctly in all cases. Also pass the new -fgo-compiling-runtime option when compiling the runtime package, although that option doesn't do anything yet. Reviewed-on: https://go-review.googlesource.com/28051 From-SVN: r239872
275 lines
7.4 KiB
C
275 lines
7.4 KiB
C
/* go-recover.c -- support for the go recover function.
|
|
|
|
Copyright 2010 The Go Authors. All rights reserved.
|
|
Use of this source code is governed by a BSD-style
|
|
license that can be found in the LICENSE file. */
|
|
|
|
#include "runtime.h"
|
|
#include "interface.h"
|
|
#include "go-panic.h"
|
|
|
|
/* If the top of the defer stack can be recovered, then return it.
|
|
Otherwise return NULL. */
|
|
|
|
static Defer *
|
|
current_defer ()
|
|
{
|
|
G *g;
|
|
Defer *d;
|
|
|
|
g = runtime_g ();
|
|
|
|
d = g->_defer;
|
|
if (d == NULL)
|
|
return NULL;
|
|
|
|
/* The panic which would be recovered is the one on the top of the
|
|
panic stack. We do not want to recover it if that panic was on
|
|
the top of the panic stack when this function was deferred. */
|
|
if (d->_panic == g->_panic)
|
|
return NULL;
|
|
|
|
/* The deferred thunk will call _go_set_defer_retaddr. If this has
|
|
not happened, then we have not been called via defer, and we can
|
|
not recover. */
|
|
if (d->retaddr == 0)
|
|
return NULL;
|
|
|
|
return d;
|
|
}
|
|
|
|
/* This is called by a thunk to see if the real function should be
|
|
permitted to recover a panic value. Recovering a value is
|
|
permitted if the thunk was called directly by defer. RETADDR is
|
|
the return address of the function which is calling
|
|
__go_can_recover--this is, the thunk. */
|
|
|
|
_Bool
|
|
__go_can_recover (void *retaddr)
|
|
{
|
|
Defer *d;
|
|
const char* ret;
|
|
const char* dret;
|
|
Location locs[16];
|
|
const byte *name;
|
|
intgo len;
|
|
int n;
|
|
int i;
|
|
_Bool found_ffi_callback;
|
|
|
|
d = current_defer ();
|
|
if (d == NULL)
|
|
return 0;
|
|
|
|
ret = (const char *) __builtin_extract_return_addr (retaddr);
|
|
|
|
dret = (const char *) (uintptr) d->retaddr;
|
|
if (ret <= dret && ret + 16 >= dret)
|
|
return 1;
|
|
|
|
/* On some systems, in some cases, the return address does not work
|
|
reliably. See http://gcc.gnu.org/PR60406. If we are permitted
|
|
to call recover, the call stack will look like this:
|
|
__go_panic, __go_undefer, etc.
|
|
thunk to call deferred function (calls __go_set_defer_retaddr)
|
|
function that calls __go_can_recover (passing return address)
|
|
__go_can_recover
|
|
Calling runtime_callers will skip the thunks. So if our caller's
|
|
caller starts with __go, then we are permitted to call
|
|
recover. */
|
|
|
|
if (runtime_callers (1, &locs[0], 2, false) < 2)
|
|
return 0;
|
|
|
|
name = locs[1].function.str;
|
|
len = locs[1].function.len;
|
|
|
|
/* Although locs[1].function is a Go string, we know it is
|
|
NUL-terminated. */
|
|
if (len > 4
|
|
&& __builtin_strchr ((const char *) name, '.') == NULL
|
|
&& __builtin_strncmp ((const char *) name, "__go_", 4) == 0)
|
|
return 1;
|
|
|
|
/* If we are called from __go_makefunc_can_recover, then we need to
|
|
look one level higher. */
|
|
if (locs[0].function.len > 0
|
|
&& __builtin_strcmp ((const char *) locs[0].function.str,
|
|
"__go_makefunc_can_recover") == 0)
|
|
{
|
|
if (runtime_callers (3, &locs[0], 1, false) < 1)
|
|
return 0;
|
|
name = locs[0].function.str;
|
|
len = locs[0].function.len;
|
|
if (len > 4
|
|
&& __builtin_strchr ((const char *) name, '.') == NULL
|
|
&& __builtin_strncmp ((const char *) name, "__go_", 4) == 0)
|
|
return 1;
|
|
}
|
|
|
|
/* If the function calling recover was created by reflect.MakeFunc,
|
|
then __go_makefunc_can_recover or __go_makefunc_ffi_can_recover
|
|
will have set the __makefunc_can_recover field. */
|
|
if (!d->makefunccanrecover)
|
|
return 0;
|
|
|
|
/* We look up the stack, ignoring libffi functions and functions in
|
|
the reflect package, until we find reflect.makeFuncStub or
|
|
reflect.ffi_callback called by FFI functions. Then we check the
|
|
caller of that function. */
|
|
|
|
n = runtime_callers (2, &locs[0], sizeof locs / sizeof locs[0], false);
|
|
found_ffi_callback = 0;
|
|
for (i = 0; i < n; i++)
|
|
{
|
|
const byte *name;
|
|
|
|
if (locs[i].function.len == 0)
|
|
{
|
|
/* No function name means this caller isn't Go code. Assume
|
|
that this is libffi. */
|
|
continue;
|
|
}
|
|
|
|
/* Ignore functions in libffi. */
|
|
name = locs[i].function.str;
|
|
if (__builtin_strncmp ((const char *) name, "ffi_", 4) == 0)
|
|
continue;
|
|
|
|
if (found_ffi_callback)
|
|
break;
|
|
|
|
if (__builtin_strcmp ((const char *) name, "reflect.ffi_callback") == 0)
|
|
{
|
|
found_ffi_callback = 1;
|
|
continue;
|
|
}
|
|
|
|
if (__builtin_strcmp ((const char *) name, "reflect.makeFuncStub") == 0)
|
|
{
|
|
i++;
|
|
break;
|
|
}
|
|
|
|
/* Ignore other functions in the reflect package. */
|
|
if (__builtin_strncmp ((const char *) name, "reflect.", 8) == 0)
|
|
continue;
|
|
|
|
/* We should now be looking at the real caller. */
|
|
break;
|
|
}
|
|
|
|
if (i < n && locs[i].function.len > 0)
|
|
{
|
|
name = locs[i].function.str;
|
|
if (__builtin_strncmp ((const char *) name, "__go_", 4) == 0)
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* This function is called when code is about to enter a function
|
|
created by reflect.MakeFunc. It is called by the function stub
|
|
used by MakeFunc. If the stub is permitted to call recover, then a
|
|
real MakeFunc function is permitted to call recover. */
|
|
|
|
void
|
|
__go_makefunc_can_recover (void *retaddr)
|
|
{
|
|
Defer *d;
|
|
|
|
d = current_defer ();
|
|
if (d == NULL)
|
|
return;
|
|
|
|
/* If we are already in a call stack of MakeFunc functions, there is
|
|
nothing we can usefully check here. */
|
|
if (d->makefunccanrecover)
|
|
return;
|
|
|
|
if (__go_can_recover (retaddr))
|
|
d->makefunccanrecover = 1;
|
|
}
|
|
|
|
/* This function is called when code is about to enter a function
|
|
created by the libffi version of reflect.MakeFunc. This function
|
|
is passed the names of the callers of the libffi code that called
|
|
the stub. It uses to decide whether it is permitted to call
|
|
recover, and sets d->makefunccanrecover so that __go_recover can
|
|
make the same decision. */
|
|
|
|
void
|
|
__go_makefunc_ffi_can_recover (struct location *loc, int n)
|
|
{
|
|
Defer *d;
|
|
const byte *name;
|
|
intgo len;
|
|
|
|
d = current_defer ();
|
|
if (d == NULL)
|
|
return;
|
|
|
|
/* If we are already in a call stack of MakeFunc functions, there is
|
|
nothing we can usefully check here. */
|
|
if (d->makefunccanrecover)
|
|
return;
|
|
|
|
/* LOC points to the caller of our caller. That will be a thunk.
|
|
If its caller was a runtime function, then it was called directly
|
|
by defer. */
|
|
|
|
if (n < 2)
|
|
return;
|
|
|
|
name = (loc + 1)->function.str;
|
|
len = (loc + 1)->function.len;
|
|
if (len > 4
|
|
&& __builtin_strchr ((const char *) name, '.') == NULL
|
|
&& __builtin_strncmp ((const char *) name, "__go_", 4) == 0)
|
|
d->makefunccanrecover = 1;
|
|
}
|
|
|
|
/* This function is called when code is about to exit a function
|
|
created by reflect.MakeFunc. It is called by the function stub
|
|
used by MakeFunc. It clears the makefunccanrecover field. It's OK
|
|
to always clear this field, because __go_can_recover will only be
|
|
called by a stub created for a function that calls recover. That
|
|
stub will not call a function created by reflect.MakeFunc, so by
|
|
the time we get here any caller higher up on the call stack no
|
|
longer needs the information. */
|
|
|
|
void
|
|
__go_makefunc_returning (void)
|
|
{
|
|
Defer *d;
|
|
|
|
d = runtime_g ()->_defer;
|
|
if (d != NULL)
|
|
d->makefunccanrecover = 0;
|
|
}
|
|
|
|
/* This is only called when it is valid for the caller to recover the
|
|
value on top of the panic stack, if there is one. */
|
|
|
|
struct __go_empty_interface
|
|
__go_recover ()
|
|
{
|
|
G *g;
|
|
Panic *p;
|
|
|
|
g = runtime_g ();
|
|
|
|
if (g->_panic == NULL || g->_panic->recovered)
|
|
{
|
|
struct __go_empty_interface ret;
|
|
|
|
ret.__type_descriptor = NULL;
|
|
ret.__object = NULL;
|
|
return ret;
|
|
}
|
|
p = g->_panic;
|
|
p->recovered = 1;
|
|
return p->arg;
|
|
}
|