1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
8 "runtime/internal/atomic"
12 // For gccgo, use go:linkname to rename compiler-called functions to
13 // themselves, so that the compiler will export them.
15 //go:linkname deferproc runtime.deferproc
16 //go:linkname deferreturn runtime.deferreturn
17 //go:linkname setdeferretaddr runtime.setdeferretaddr
18 //go:linkname checkdefer runtime.checkdefer
19 //go:linkname gopanic runtime.gopanic
20 //go:linkname canrecover runtime.canrecover
21 //go:linkname makefuncfficanrecover runtime.makefuncfficanrecover
22 //go:linkname makefuncreturning runtime.makefuncreturning
23 //go:linkname gorecover runtime.gorecover
24 //go:linkname deferredrecover runtime.deferredrecover
25 // Temporary for C code to call:
26 //go:linkname throw runtime.throw
28 // Calling panic with one of the errors below will call errorString.Error
29 // which will call mallocgc to concatenate strings. That will fail if
30 // malloc is locked, causing a confusing error message. Throw a better
31 // error message instead.
32 func panicCheckMalloc(err error
) {
34 if gp
!= nil && gp
.m
!= nil && gp
.m
.mallocing
!= 0 {
35 throw(string(err
.(errorString
)))
39 var indexError
= error(errorString("index out of range"))
42 panicCheckMalloc(indexError
)
46 var sliceError
= error(errorString("slice bounds out of range"))
49 panicCheckMalloc(sliceError
)
53 var divideError
= error(errorString("integer divide by zero"))
56 panicCheckMalloc(divideError
)
60 var overflowError
= error(errorString("integer overflow"))
62 func panicoverflow() {
63 panicCheckMalloc(overflowError
)
67 var floatError
= error(errorString("floating point error"))
70 panicCheckMalloc(floatError
)
74 var memoryError
= error(errorString("invalid memory address or nil pointer dereference"))
77 panicCheckMalloc(memoryError
)
82 throw("recursive call during initialization - linker skew")
85 // deferproc creates a new deferred function.
86 // The compiler turns a defer statement into a call to this.
87 // frame points into the stack frame; it is used to determine which
88 // deferred functions are for the current stack frame, and whether we
89 // have already deferred functions for this frame.
90 // pfn is a C function pointer.
91 // arg is a value to pass to pfn.
92 func deferproc(frame
*bool, pfn
uintptr, arg unsafe
.Pointer
) {
95 n
._panic
= getg()._panic
99 n
.makefunccanrecover
= false
102 // Allocate a Defer, usually using per-P pool.
103 // Each defer must be released with freedefer.
104 func newdefer() *_defer
{
108 if len(pp
.deferpool
) == 0 && sched
.deferpool
!= nil {
110 lock(&sched
.deferlock
)
111 for len(pp
.deferpool
) < cap(pp
.deferpool
)/2 && sched
.deferpool
!= nil {
113 sched
.deferpool
= d
.link
115 pp
.deferpool
= append(pp
.deferpool
, d
)
117 unlock(&sched
.deferlock
)
120 if n
:= len(pp
.deferpool
); n
> 0 {
121 d
= pp
.deferpool
[n
-1]
122 pp
.deferpool
[n
-1] = nil
123 pp
.deferpool
= pp
.deferpool
[:n
-1]
135 // Free the given defer.
136 // The defer cannot be used after this call.
138 // This must not grow the stack because there may be a frame without a
139 // stack map when this is called.
142 func freedefer(d
*_defer
) {
143 // When C code calls a Go function on a non-Go thread, the
144 // deferred call to cgocallBackDone will set g to nil.
145 // Don't crash trying to put d on the free list; just let it
146 // be garbage collected.
151 pp
:= getg().m
.p
.ptr()
152 if len(pp
.deferpool
) == cap(pp
.deferpool
) {
153 // Transfer half of local cache to the central cache.
155 // Take this slow path on the system stack so
156 // we don't grow freedefer's stack.
158 var first
, last
*_defer
159 for len(pp
.deferpool
) > cap(pp
.deferpool
)/2 {
160 n
:= len(pp
.deferpool
)
161 d
:= pp
.deferpool
[n
-1]
162 pp
.deferpool
[n
-1] = nil
163 pp
.deferpool
= pp
.deferpool
[:n
-1]
171 lock(&sched
.deferlock
)
172 last
.link
= sched
.deferpool
173 sched
.deferpool
= first
174 unlock(&sched
.deferlock
)
178 pp
.deferpool
= append(pp
.deferpool
, d
)
181 // deferreturn is called to undefer the stack.
182 // The compiler inserts a call to this function as a finally clause
183 // wrapped around the body of any function that calls defer.
184 // The frame argument points to the stack frame of the function.
185 func deferreturn(frame
*bool) {
187 for gp
._defer
!= nil && gp
._defer
.frame
== frame
{
193 // This is rather awkward.
194 // The gc compiler does this using assembler
196 var fn
func(unsafe
.Pointer
)
197 *(**uintptr)(unsafe
.Pointer(&fn
)) = &pfn
205 // Since we are executing a defer function now, we
206 // know that we are returning from the calling
207 // function. If the calling function, or one of its
208 // callees, panicked, then the defer functions would
209 // be executed by panic.
214 // __builtin_extract_return_addr is a GCC intrinsic that converts an
215 // address returned by __builtin_return_address(0) to a real address.
216 // On most architectures this is a nop.
217 //extern __builtin_extract_return_addr
218 func __builtin_extract_return_addr(uintptr) uintptr
220 // setdeferretaddr records the address to which the deferred function
221 // returns. This is check by canrecover. The frontend relies on this
222 // function returning false.
223 func setdeferretaddr(retaddr
uintptr) bool {
225 if gp
._defer
!= nil {
226 gp
._defer
.retaddr
= __builtin_extract_return_addr(retaddr
)
231 // checkdefer is called by exception handlers used when unwinding the
232 // stack after a recovered panic. The exception handler is simply
235 // If we have not yet reached the frame we are looking for, we
236 // continue unwinding.
237 func checkdefer(frame
*bool) {
240 // We should never wind up here. Even if some other
241 // language throws an exception, the cgo code
242 // should ensure that g is set.
243 throw("no g in checkdefer")
244 } else if gp
.isforeign
{
245 // Some other language has thrown an exception.
246 // We need to run the local defer handlers.
247 // If they call recover, we stop unwinding here.
254 if d
== nil || d
.frame
!= frame || d
.pfn
== 0 {
261 var fn
func(unsafe
.Pointer
)
262 *(**uintptr)(unsafe
.Pointer(&fn
)) = &pfn
268 // The recover function caught the panic
269 // thrown by some other language.
274 recovered
:= p
.recovered
278 // Just return and continue executing Go code.
283 // We are panicking through this function.
285 } else if gp
._defer
!= nil && gp
._defer
.pfn
== 0 && gp
._defer
.frame
== frame
{
286 // This is the defer function that called recover.
287 // Simply return to stop the stack unwind, and let the
288 // Go code continue to execute.
293 // We are returning from this function.
299 // This is some other defer function. It was already run by
300 // the call to panic, or just above. Rethrow the exception.
302 throw("rethrowException returned")
305 // unwindStack starts unwinding the stack for a panic. We unwind
306 // function calls until we reach the one which used a defer function
307 // which called recover. Each function which uses a defer statement
308 // will have an exception handler, as shown above for checkdefer.
310 // Allocate the exception type used by the unwind ABI.
311 // It would be nice to define it in runtime_sysinfo.go,
312 // but current definitions don't work because the required
313 // alignment is larger than can be represented in Go.
314 // The type never contains any Go pointers.
315 size
:= unwindExceptionSize()
316 usize
:= uintptr(unsafe
.Sizeof(uintptr(0)))
317 c
:= (size
+ usize
- 1) / usize
318 s
:= make([]uintptr, c
)
319 getg().exception
= unsafe
.Pointer(&s
[0])
323 // Goexit terminates the goroutine that calls it. No other goroutine is affected.
324 // Goexit runs all deferred calls before terminating the goroutine. Because Goexit
325 // is not panic, however, any recover calls in those deferred functions will return nil.
327 // Calling Goexit from the main goroutine terminates that goroutine
328 // without func main returning. Since func main has not returned,
329 // the program continues execution of other goroutines.
330 // If all other goroutines exit, the program crashes.
332 // Run all deferred functions for the current goroutine.
333 // This code is similar to gopanic, see that implementation
334 // for detailed comments.
347 var fn
func(unsafe
.Pointer
)
348 *(**uintptr)(unsafe
.Pointer(&fn
)) = &pfn
353 // Note: we ignore recovers here because Goexit isn't a panic
358 // Call all Error and String methods before freezing the world.
359 // Used when crashing with panicking.
360 // This must match types handled by printany.
361 func preprintpanics(p
*_panic
) {
363 if recover() != nil {
364 throw("panic while printing panic value")
368 switch v
:= p
.arg
.(type) {
378 // Print all currently active panics. Used when crashing.
379 func printpanics(p
*_panic
) {
387 print(" [recovered]")
392 // The implementation of the predeclared function panic.
393 func gopanic(e
interface{}) {
399 throw("panic on system stack")
402 if gp
.m
.mallocing
!= 0 {
406 throw("panic during malloc")
408 if gp
.m
.preemptoff
!= "" {
412 print("preempt off reason: ")
413 print(gp
.m
.preemptoff
)
415 throw("panic during preemptoff")
421 throw("panic holding locks")
424 // The gc compiler allocates this new _panic struct on the
425 // stack. We can't do that, because when a deferred function
426 // recovers the panic we unwind the stack. We unlink this
427 // entry before unwinding the stack, but that doesn't help in
428 // the case where we panic, a deferred function recovers and
429 // then panics itself, that panic is in turn recovered, and
430 // unwinds the stack past this stack frame.
448 var fn
func(unsafe
.Pointer
)
449 *(**uintptr)(unsafe
.Pointer(&fn
)) = &pfn
453 // Some deferred function called recover.
454 // Stop running this panic.
457 // Unwind the stack by throwing an exception.
458 // The compiler has arranged to create
459 // exception handlers in each function
460 // that uses a defer statement. These
461 // exception handlers will check whether
462 // the entry on the top of the defer stack
463 // is from the current function. If it is,
464 // we have unwound the stack far enough.
467 throw("unwindStack returned")
470 // Because we executed that defer function by a panic,
471 // and it did not call recover, we know that we are
472 // not returning from the calling function--we are
473 // panicking through it.
481 // ran out of deferred calls - old-school panic now
482 // Because it is unsafe to call arbitrary user code after freezing
483 // the world, we call preprintpanics to invoke all necessary Error
484 // and String methods to prepare the panic strings before startpanic.
485 preprintpanics(gp
._panic
)
487 printpanics(gp
._panic
)
488 dopanic(0) // should not return
489 *(*int)(nil) = 0 // not reached
492 // currentDefer returns the top of the defer stack if it can be recovered.
493 // Otherwise it returns nil.
494 func currentDefer() *_defer
{
501 // The panic that would be recovered is the one on the top of
502 // the panic stack. We do not want to recover it if that panic
503 // was on the top of the panic stack when this function was
505 if d
._panic
== gp
._panic
{
509 // The deferred thunk will call setdeferretaddr. If this has
510 // not happened, then we have not been called via defer, and
511 // we can not recover.
519 // canrecover is called by a thunk to see if the real function would
520 // be permitted to recover a panic value. Recovering a value is
521 // permitted if the thunk was called directly by defer. retaddr is the
522 // return address of the function that is calling canrecover--that is,
524 func canrecover(retaddr
uintptr) bool {
530 ret
:= __builtin_extract_return_addr(retaddr
)
532 if ret
<= dret
&& ret
+16 >= dret
{
536 // On some systems, in some cases, the return address does not
537 // work reliably. See http://gcc.gnu.org/PR60406. If we are
538 // permitted to call recover, the call stack will look like this:
539 // runtime.gopanic, runtime.deferreturn, etc.
540 // thunk to call deferred function (calls __go_set_defer_retaddr)
541 // function that calls __go_can_recover (passing return address)
542 // runtime.canrecover
543 // Calling callers will skip the thunks. So if our caller's
544 // caller starts with "runtime.", then we are permitted to
546 var locs
[16]location
547 if callers(2, locs
[:2]) < 2 {
551 name
:= locs
[1].function
552 if hasprefix(name
, "runtime.") {
556 // If the function calling recover was created by reflect.MakeFunc,
557 // then makefuncfficanrecover will have set makefunccanrecover.
558 if !d
.makefunccanrecover
{
562 // We look up the stack, ignoring libffi functions and
563 // functions in the reflect package, until we find
564 // reflect.makeFuncStub or reflect.ffi_callback called by FFI
565 // functions. Then we check the caller of that function.
567 n
:= callers(3, locs
[:])
568 foundFFICallback
:= false
571 name
= locs
[i
].function
573 // No function name means this caller isn't Go code.
574 // Assume that this is libffi.
578 // Ignore function in libffi.
579 if hasprefix(name
, "ffi_") {
583 if foundFFICallback
{
587 if name
== "reflect.ffi_callback" {
588 foundFFICallback
= true
592 // Ignore other functions in the reflect package.
593 if hasprefix(name
, "reflect.") {
597 // We should now be looking at the real caller.
602 name
= locs
[i
].function
603 if hasprefix(name
, "runtime.") {
611 // This function is called when code is about to enter a function
612 // created by the libffi version of reflect.MakeFunc. This function is
613 // passed the names of the callers of the libffi code that called the
614 // stub. It uses them to decide whether it is permitted to call
615 // recover, and sets d.makefunccanrecover so that gorecover can make
616 // the same decision.
617 func makefuncfficanrecover(loc
[]location
) {
623 // If we are already in a call stack of MakeFunc functions,
624 // there is nothing we can usefully check here.
625 if d
.makefunccanrecover
{
629 // loc starts with the caller of our caller. That will be a thunk.
630 // If its caller was a function function, then it was called
631 // directly by defer.
636 name
:= loc
[1].function
637 if hasprefix(name
, "runtime.") {
638 d
.makefunccanrecover
= true
642 // makefuncreturning is called when code is about to exit a function
643 // created by reflect.MakeFunc. It is called by the function stub used
644 // by reflect.MakeFunc. It clears the makefunccanrecover field. It's
645 // OK to always clear this field, because canrecover will only be
646 // called by a stub created for a function that calls recover. That
647 // stub will not call a function created by reflect.MakeFunc, so by
648 // the time we get here any caller higher up on the call stack no
649 // longer needs the information.
650 func makefuncreturning() {
653 d
.makefunccanrecover
= false
657 // The implementation of the predeclared function recover.
658 func gorecover() interface{} {
661 if p
!= nil && !p
.recovered
{
668 // deferredrecover is called when a call to recover is deferred. That
669 // is, something like
672 // We need to handle this specially. In gc, the recover function
673 // looks up the stack frame. In particular, that means that a deferred
674 // recover will not recover a panic thrown in the same function that
675 // defers the recover. It will only recover a panic thrown in a
676 // function that defers the deferred call to recover.
681 // defer recover() // does not stop panic
687 // defer recover() // stops panic(0)
694 // defer recover() // does not stop panic
703 // defer recover() // stops panic(0)
710 // The interesting case here is f3. As can be seen from f2, the
711 // deferred recover could pick up panic(1). However, this does not
712 // happen because it is blocked by the panic(0).
714 // When a function calls recover, then when we invoke it we pass a
715 // hidden parameter indicating whether it should recover something.
716 // This parameter is set based on whether the function is being
717 // invoked directly from defer. The parameter winds up determining
718 // whether __go_recover or __go_deferred_recover is called at all.
720 // In the case of a deferred recover, the hidden parameter that
721 // controls the call is actually the one set up for the function that
722 // runs the defer recover() statement. That is the right thing in all
723 // the cases above except for f3. In f3 the function is permitted to
724 // call recover, but the deferred recover call is not. We address that
725 // here by checking for that specific case before calling recover. If
726 // this function was deferred when there is already a panic on the
727 // panic stack, then we can only recover that panic, not any other.
729 // Note that we can get away with using a special function here
730 // because you are not permitted to take the address of a predeclared
731 // function like recover.
732 func deferredrecover() interface{} {
734 if gp
._defer
== nil || gp
._defer
._panic
!= gp
._panic
{
740 //go:linkname sync_throw sync.throw
741 func sync_throw(s
string) {
746 func throw(s
string) {
747 print("fatal error: ", s
, "\n")
749 if gp
.m
.throwing
== 0 {
754 *(*int)(nil) = 0 // not reached
757 //uint32 runtime·panicking;
762 // Uncomment when mheap_ is in Go.
763 // if mheap_.cachealloc.size == 0 { // very early
764 // print("runtime: panic before malloc heap initialized\n")
765 // _g_.m.mallocing = 1 // tell rest of panic not to try to malloc
767 if _g_
.m
.mcache
== nil { // can happen if called from signal handler or throw
768 _g_
.m
.mcache
= allocmcache()
775 atomic
.Xadd(&panicking
, 1)
777 if debug
.schedtrace
> 0 || debug
.scheddetail
> 0 {
783 // Something failed while panicking, probably the print of the
784 // argument to panic(). Just print a stack trace and exit.
786 print("panic during panic\n")
791 // This is a genuine bug in the runtime, we couldn't even
792 // print the stack trace successfully.
794 print("stack trace unavailable\n")
798 // Can't even print! Just exit.
806 func dopanic(unused
int) {
809 signame
:= signame(gp
.sig
)
811 print("[signal ", signame
)
813 print("[signal ", hex(gp
.sig
))
815 print(" code=", hex(gp
.sigcode0
), " addr=", hex(gp
.sigcode1
), " pc=", hex(gp
.sigpc
), "]\n")
818 level
, all
, docrash
:= gotraceback()
828 } else if level
>= 2 || _g_
.m
.throwing
> 0 {
829 print("\nruntime stack:\n")
832 if !didothers
&& all
{
839 if atomic
.Xadd(&panicking
, -1) != 0 {
840 // Some other m is panicking too.
841 // Let it print what it needs to print.
842 // Wait forever without chewing up cpu.
843 // It will exit when it's done.
856 func canpanic(gp
*g
) bool {
857 // Note that g is m->gsignal, different from gp.
858 // Note also that g->m can change at preemption, so m can go stale
859 // if this function ever makes a function call.
863 // Is it okay for gp to panic instead of crashing the program?
864 // Yes, as long as it is running Go code, not runtime code,
865 // and not stuck in a system call.
866 if gp
== nil || gp
!= _m_
.curg
{
869 if _m_
.locks
-_m_
.softfloat
!= 0 || _m_
.mallocing
!= 0 || _m_
.throwing
!= 0 || _m_
.preemptoff
!= "" || _m_
.dying
!= 0 {
872 status
:= readgstatus(gp
)
873 if status
&^_Gscan
!= _Grunning || gp
.syscallsp
!= 0 {