2010-12-03 05:34:57 +01:00
|
|
|
/* 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. */
|
|
|
|
|
2011-11-11 22:02:48 +01:00
|
|
|
#include "runtime.h"
|
2010-12-03 05:34:57 +01:00
|
|
|
#include "interface.h"
|
|
|
|
#include "go-panic.h"
|
|
|
|
#include "go-defer.h"
|
|
|
|
|
|
|
|
/* 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 (const void* retaddr)
|
|
|
|
{
|
2011-11-28 06:45:49 +01:00
|
|
|
G *g;
|
2010-12-03 05:34:57 +01:00
|
|
|
struct __go_defer_stack *d;
|
|
|
|
const char* ret;
|
|
|
|
const char* dret;
|
|
|
|
|
2011-11-28 06:45:49 +01:00
|
|
|
g = runtime_g ();
|
|
|
|
|
2011-11-11 22:02:48 +01:00
|
|
|
d = g->defer;
|
2010-12-03 05:34:57 +01:00
|
|
|
if (d == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* The panic which this function would recover 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. */
|
2011-11-11 22:02:48 +01:00
|
|
|
if (d->__panic == g->panic)
|
2010-12-03 05:34:57 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* D->__RETADDR is the address of a label immediately following the
|
|
|
|
call to the thunk. We can recover a panic if that is the same as
|
|
|
|
the return address of the thunk. We permit a bit of slack in
|
|
|
|
case there is any code between the function return and the label,
|
|
|
|
such as an instruction to adjust the stack pointer. */
|
|
|
|
|
|
|
|
ret = (const char *) retaddr;
|
2012-02-11 01:15:54 +01:00
|
|
|
|
|
|
|
#ifdef __sparc__
|
|
|
|
/* On SPARC the address we get, from __builtin_return_address, is
|
|
|
|
the address of the call instruction. Adjust forward, also
|
|
|
|
skipping the delayed instruction following the call. */
|
|
|
|
ret += 8;
|
|
|
|
#endif
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
dret = (const char *) d->__retaddr;
|
|
|
|
return ret <= dret && ret + 16 >= dret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 ()
|
|
|
|
{
|
2011-11-28 06:45:49 +01:00
|
|
|
G *g;
|
2010-12-03 05:34:57 +01:00
|
|
|
struct __go_panic_stack *p;
|
|
|
|
|
2011-11-28 06:45:49 +01:00
|
|
|
g = runtime_g ();
|
|
|
|
|
2011-11-11 22:02:48 +01:00
|
|
|
if (g->panic == NULL || g->panic->__was_recovered)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
struct __go_empty_interface ret;
|
|
|
|
|
|
|
|
ret.__type_descriptor = NULL;
|
|
|
|
ret.__object = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
2011-11-11 22:02:48 +01:00
|
|
|
p = g->panic;
|
2010-12-03 05:34:57 +01:00
|
|
|
p->__was_recovered = 1;
|
|
|
|
return p->__arg;
|
|
|
|
}
|