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 throw("deferproc: d.panic != nil after newdefer")
98 d
.panicStack
= getg()._panic
102 d
.makefunccanrecover
= false
105 // Allocate a Defer, usually using per-P pool.
106 // Each defer must be released with freedefer.
107 func newdefer() *_defer
{
111 if len(pp
.deferpool
) == 0 && sched
.deferpool
!= nil {
113 lock(&sched
.deferlock
)
114 for len(pp
.deferpool
) < cap(pp
.deferpool
)/2 && sched
.deferpool
!= nil {
116 sched
.deferpool
= d
.link
118 pp
.deferpool
= append(pp
.deferpool
, d
)
120 unlock(&sched
.deferlock
)
123 if n
:= len(pp
.deferpool
); n
> 0 {
124 d
= pp
.deferpool
[n
-1]
125 pp
.deferpool
[n
-1] = nil
126 pp
.deferpool
= pp
.deferpool
[:n
-1]
138 // Free the given defer.
139 // The defer cannot be used after this call.
141 // This must not grow the stack because there may be a frame without a
142 // stack map when this is called.
145 func freedefer(d
*_defer
) {
146 pp
:= getg().m
.p
.ptr()
147 if len(pp
.deferpool
) == cap(pp
.deferpool
) {
148 // Transfer half of local cache to the central cache.
150 // Take this slow path on the system stack so
151 // we don't grow freedefer's stack.
153 var first
, last
*_defer
154 for len(pp
.deferpool
) > cap(pp
.deferpool
)/2 {
155 n
:= len(pp
.deferpool
)
156 d
:= pp
.deferpool
[n
-1]
157 pp
.deferpool
[n
-1] = nil
158 pp
.deferpool
= pp
.deferpool
[:n
-1]
166 lock(&sched
.deferlock
)
167 last
.link
= sched
.deferpool
168 sched
.deferpool
= first
169 unlock(&sched
.deferlock
)
173 pp
.deferpool
= append(pp
.deferpool
, d
)
176 // deferreturn is called to undefer the stack.
177 // The compiler inserts a call to this function as a finally clause
178 // wrapped around the body of any function that calls defer.
179 // The frame argument points to the stack frame of the function.
180 func deferreturn(frame
*bool) {
182 for gp
._defer
!= nil && gp
._defer
.frame
== frame
{
188 // This is rather awkward.
189 // The gc compiler does this using assembler
191 var fn
func(unsafe
.Pointer
)
192 *(*uintptr)(unsafe
.Pointer(&fn
)) = uintptr(unsafe
.Pointer(&pfn
))
196 // If we are returning from a Go function called by a
197 // C function running in a C thread, g may now be nil,
198 // in which case CgocallBackDone will have cleared _defer.
199 // In that case some other goroutine may already be using gp.
209 // Since we are executing a defer function now, we
210 // know that we are returning from the calling
211 // function. If the calling function, or one of its
212 // callees, panicked, then the defer functions would
213 // be executed by panic.
218 // __builtin_extract_return_addr is a GCC intrinsic that converts an
219 // address returned by __builtin_return_address(0) to a real address.
220 // On most architectures this is a nop.
221 //extern __builtin_extract_return_addr
222 func __builtin_extract_return_addr(uintptr) uintptr
224 // setdeferretaddr records the address to which the deferred function
225 // returns. This is check by canrecover. The frontend relies on this
226 // function returning false.
227 func setdeferretaddr(retaddr
uintptr) bool {
229 if gp
._defer
!= nil {
230 gp
._defer
.retaddr
= __builtin_extract_return_addr(retaddr
)
235 // checkdefer is called by exception handlers used when unwinding the
236 // stack after a recovered panic. The exception handler is simply
239 // If we have not yet reached the frame we are looking for, we
240 // continue unwinding.
241 func checkdefer(frame
*bool) {
244 // We should never wind up here. Even if some other
245 // language throws an exception, the cgo code
246 // should ensure that g is set.
247 throw("no g in checkdefer")
248 } else if gp
.isforeign
{
249 // Some other language has thrown an exception.
250 // We need to run the local defer handlers.
251 // If they call recover, we stop unwinding here.
258 if d
== nil || d
.frame
!= frame || d
.pfn
== 0 {
265 var fn
func(unsafe
.Pointer
)
266 *(*uintptr)(unsafe
.Pointer(&fn
)) = uintptr(unsafe
.Pointer(&pfn
))
272 // The recover function caught the panic
273 // thrown by some other language.
278 recovered
:= p
.recovered
282 // Just return and continue executing Go code.
287 // We are panicking through this function.
289 } else if gp
._defer
!= nil && gp
._defer
.pfn
== 0 && gp
._defer
.frame
== frame
{
290 // This is the defer function that called recover.
291 // Simply return to stop the stack unwind, and let the
292 // Go code continue to execute.
297 // We are returning from this function.
303 // This is some other defer function. It was already run by
304 // the call to panic, or just above. Rethrow the exception.
306 throw("rethrowException returned")
309 // unwindStack starts unwinding the stack for a panic. We unwind
310 // function calls until we reach the one which used a defer function
311 // which called recover. Each function which uses a defer statement
312 // will have an exception handler, as shown above for checkdefer.
314 // Allocate the exception type used by the unwind ABI.
315 // It would be nice to define it in runtime_sysinfo.go,
316 // but current definitions don't work because the required
317 // alignment is larger than can be represented in Go.
318 // The type never contains any Go pointers.
319 size
:= unwindExceptionSize()
320 usize
:= uintptr(unsafe
.Sizeof(uintptr(0)))
321 c
:= (size
+ usize
- 1) / usize
322 s
:= make([]uintptr, c
)
323 getg().exception
= unsafe
.Pointer(&s
[0])
327 // Goexit terminates the goroutine that calls it. No other goroutine is affected.
328 // Goexit runs all deferred calls before terminating the goroutine. Because Goexit
329 // is not panic, however, any recover calls in those deferred functions will return nil.
331 // Calling Goexit from the main goroutine terminates that goroutine
332 // without func main returning. Since func main has not returned,
333 // the program continues execution of other goroutines.
334 // If all other goroutines exit, the program crashes.
336 // Run all deferred functions for the current goroutine.
337 // This code is similar to gopanic, see that implementation
338 // for detailed comments.
349 d
._panic
.aborted
= true
358 var fn
func(unsafe
.Pointer
)
359 *(*uintptr)(unsafe
.Pointer(&fn
)) = uintptr(unsafe
.Pointer(&pfn
))
363 throw("bad defer entry in Goexit")
368 // Note: we ignore recovers here because Goexit isn't a panic
373 // Call all Error and String methods before freezing the world.
374 // Used when crashing with panicking.
375 // This must match types handled by printany.
376 func preprintpanics(p
*_panic
) {
378 if recover() != nil {
379 throw("panic while printing panic value")
383 switch v
:= p
.arg
.(type) {
393 // Print all currently active panics. Used when crashing.
394 func printpanics(p
*_panic
) {
402 print(" [recovered]")
407 // The implementation of the predeclared function panic.
408 func gopanic(e
interface{}) {
414 throw("panic on system stack")
417 if gp
.m
.mallocing
!= 0 {
421 throw("panic during malloc")
423 if gp
.m
.preemptoff
!= "" {
427 print("preempt off reason: ")
428 print(gp
.m
.preemptoff
)
430 throw("panic during preemptoff")
436 throw("panic holding locks")
439 // The gc compiler allocates this new _panic struct on the
440 // stack. We can't do that, because when a deferred function
441 // recovers the panic we unwind the stack. We unlink this
442 // entry before unwinding the stack, but that doesn't help in
443 // the case where we panic, a deferred function recovers and
444 // then panics itself, that panic is in turn recovered, and
445 // unwinds the stack past this stack frame.
453 atomic
.Xadd(&runningPanicDefers
, 1)
463 // If defer was started by earlier panic or Goexit (and, since we're back here, that triggered a new panic),
464 // take defer off list. The earlier panic or Goexit will not continue running.
467 d
._panic
.aborted
= true
476 // Record the panic that is running the defer.
477 // If there is a new panic during the deferred call, that panic
478 // will find d in the list and will mark d._panic (this panic) aborted.
481 var fn
func(unsafe
.Pointer
)
482 *(*uintptr)(unsafe
.Pointer(&fn
)) = uintptr(unsafe
.Pointer(&pfn
))
486 throw("bad defer entry in panic")
491 atomic
.Xadd(&runningPanicDefers
, -1)
495 // Aborted panics are marked but remain on the g.panic list.
496 // Remove them from the list.
497 for gp
._panic
!= nil && gp
._panic
.aborted
{
498 gp
._panic
= gp
._panic
.link
500 if gp
._panic
== nil { // must be done with signal
504 // Unwind the stack by throwing an exception.
505 // The compiler has arranged to create
506 // exception handlers in each function
507 // that uses a defer statement. These
508 // exception handlers will check whether
509 // the entry on the top of the defer stack
510 // is from the current function. If it is,
511 // we have unwound the stack far enough.
514 throw("unwindStack returned")
517 // Because we executed that defer function by a panic,
518 // and it did not call recover, we know that we are
519 // not returning from the calling function--we are
520 // panicking through it.
523 // Deferred function did not panic. Remove d.
524 // In the p.recovered case, d will be removed by checkdefer.
530 // ran out of deferred calls - old-school panic now
531 // Because it is unsafe to call arbitrary user code after freezing
532 // the world, we call preprintpanics to invoke all necessary Error
533 // and String methods to prepare the panic strings before startpanic.
534 preprintpanics(gp
._panic
)
537 // startpanic set panicking, which will block main from exiting,
538 // so now OK to decrement runningPanicDefers.
539 atomic
.Xadd(&runningPanicDefers
, -1)
541 printpanics(gp
._panic
)
542 dopanic(0) // should not return
543 *(*int)(nil) = 0 // not reached
546 // currentDefer returns the top of the defer stack if it can be recovered.
547 // Otherwise it returns nil.
548 func currentDefer() *_defer
{
555 // The panic that would be recovered is the one on the top of
556 // the panic stack. We do not want to recover it if that panic
557 // was on the top of the panic stack when this function was
559 if d
.panicStack
== gp
._panic
{
563 // The deferred thunk will call setdeferretaddr. If this has
564 // not happened, then we have not been called via defer, and
565 // we can not recover.
573 // canrecover is called by a thunk to see if the real function would
574 // be permitted to recover a panic value. Recovering a value is
575 // permitted if the thunk was called directly by defer. retaddr is the
576 // return address of the function that is calling canrecover--that is,
578 func canrecover(retaddr
uintptr) bool {
584 ret
:= __builtin_extract_return_addr(retaddr
)
586 if ret
<= dret
&& ret
+16 >= dret
{
590 // On some systems, in some cases, the return address does not
591 // work reliably. See http://gcc.gnu.org/PR60406. If we are
592 // permitted to call recover, the call stack will look like this:
593 // runtime.gopanic, runtime.deferreturn, etc.
594 // thunk to call deferred function (calls __go_set_defer_retaddr)
595 // function that calls __go_can_recover (passing return address)
596 // runtime.canrecover
597 // Calling callers will skip the thunks. So if our caller's
598 // caller starts with "runtime.", then we are permitted to
600 var locs
[16]location
601 if callers(2, locs
[:2]) < 2 {
605 name
:= locs
[1].function
606 if hasprefix(name
, "runtime.") {
610 // If the function calling recover was created by reflect.MakeFunc,
611 // then makefuncfficanrecover will have set makefunccanrecover.
612 if !d
.makefunccanrecover
{
616 // We look up the stack, ignoring libffi functions and
617 // functions in the reflect package, until we find
618 // reflect.makeFuncStub or reflect.ffi_callback called by FFI
619 // functions. Then we check the caller of that function.
621 n
:= callers(3, locs
[:])
622 foundFFICallback
:= false
625 name
= locs
[i
].function
627 // No function name means this caller isn't Go code.
628 // Assume that this is libffi.
632 // Ignore function in libffi.
633 if hasprefix(name
, "ffi_") {
637 if foundFFICallback
{
641 if name
== "reflect.ffi_callback" {
642 foundFFICallback
= true
646 // Ignore other functions in the reflect package.
647 if hasprefix(name
, "reflect.") {
651 // We should now be looking at the real caller.
656 name
= locs
[i
].function
657 if hasprefix(name
, "runtime.") {
665 // This function is called when code is about to enter a function
666 // created by the libffi version of reflect.MakeFunc. This function is
667 // passed the names of the callers of the libffi code that called the
668 // stub. It uses them to decide whether it is permitted to call
669 // recover, and sets d.makefunccanrecover so that gorecover can make
670 // the same decision.
671 func makefuncfficanrecover(loc
[]location
) {
677 // If we are already in a call stack of MakeFunc functions,
678 // there is nothing we can usefully check here.
679 if d
.makefunccanrecover
{
683 // loc starts with the caller of our caller. That will be a thunk.
684 // If its caller was a function function, then it was called
685 // directly by defer.
690 name
:= loc
[1].function
691 if hasprefix(name
, "runtime.") {
692 d
.makefunccanrecover
= true
696 // makefuncreturning is called when code is about to exit a function
697 // created by reflect.MakeFunc. It is called by the function stub used
698 // by reflect.MakeFunc. It clears the makefunccanrecover field. It's
699 // OK to always clear this field, because canrecover will only be
700 // called by a stub created for a function that calls recover. That
701 // stub will not call a function created by reflect.MakeFunc, so by
702 // the time we get here any caller higher up on the call stack no
703 // longer needs the information.
704 func makefuncreturning() {
707 d
.makefunccanrecover
= false
711 // The implementation of the predeclared function recover.
712 func gorecover() interface{} {
715 if p
!= nil && !p
.recovered
{
722 // deferredrecover is called when a call to recover is deferred. That
723 // is, something like
726 // We need to handle this specially. In gc, the recover function
727 // looks up the stack frame. In particular, that means that a deferred
728 // recover will not recover a panic thrown in the same function that
729 // defers the recover. It will only recover a panic thrown in a
730 // function that defers the deferred call to recover.
735 // defer recover() // does not stop panic
741 // defer recover() // stops panic(0)
748 // defer recover() // does not stop panic
757 // defer recover() // stops panic(0)
764 // The interesting case here is f3. As can be seen from f2, the
765 // deferred recover could pick up panic(1). However, this does not
766 // happen because it is blocked by the panic(0).
768 // When a function calls recover, then when we invoke it we pass a
769 // hidden parameter indicating whether it should recover something.
770 // This parameter is set based on whether the function is being
771 // invoked directly from defer. The parameter winds up determining
772 // whether __go_recover or __go_deferred_recover is called at all.
774 // In the case of a deferred recover, the hidden parameter that
775 // controls the call is actually the one set up for the function that
776 // runs the defer recover() statement. That is the right thing in all
777 // the cases above except for f3. In f3 the function is permitted to
778 // call recover, but the deferred recover call is not. We address that
779 // here by checking for that specific case before calling recover. If
780 // this function was deferred when there is already a panic on the
781 // panic stack, then we can only recover that panic, not any other.
783 // Note that we can get away with using a special function here
784 // because you are not permitted to take the address of a predeclared
785 // function like recover.
786 func deferredrecover() interface{} {
788 if gp
._defer
== nil || gp
._defer
.panicStack
!= gp
._panic
{
794 //go:linkname sync_throw sync.throw
795 func sync_throw(s
string) {
800 func throw(s
string) {
801 print("fatal error: ", s
, "\n")
803 if gp
.m
.throwing
== 0 {
808 *(*int)(nil) = 0 // not reached
811 // runningPanicDefers is non-zero while running deferred functions for panic.
812 // runningPanicDefers is incremented and decremented atomically.
813 // This is used to try hard to get a panic stack trace out when exiting.
814 var runningPanicDefers
uint32
816 // panicking is non-zero when crashing the program for an unrecovered panic.
817 // panicking is incremented and decremented atomically.
820 // paniclk is held while printing the panic information and stack trace,
821 // so that two concurrent panics don't overlap their output.
826 // Uncomment when mheap_ is in Go.
827 // if mheap_.cachealloc.size == 0 { // very early
828 // print("runtime: panic before malloc heap initialized\n")
829 // _g_.m.mallocing = 1 // tell rest of panic not to try to malloc
831 if _g_
.m
.mcache
== nil { // can happen if called from signal handler or throw
832 _g_
.m
.mcache
= allocmcache()
839 atomic
.Xadd(&panicking
, 1)
841 if debug
.schedtrace
> 0 || debug
.scheddetail
> 0 {
847 // Something failed while panicking, probably the print of the
848 // argument to panic(). Just print a stack trace and exit.
850 print("panic during panic\n")
855 // This is a genuine bug in the runtime, we couldn't even
856 // print the stack trace successfully.
858 print("stack trace unavailable\n")
862 // Can't even print! Just exit.
870 func dopanic(unused
int) {
873 signame
:= signame(gp
.sig
)
875 print("[signal ", signame
)
877 print("[signal ", hex(gp
.sig
))
879 print(" code=", hex(gp
.sigcode0
), " addr=", hex(gp
.sigcode1
), " pc=", hex(gp
.sigpc
), "]\n")
882 level
, all
, docrash
:= gotraceback()
892 } else if level
>= 2 || _g_
.m
.throwing
> 0 {
893 print("\nruntime stack:\n")
896 if !didothers
&& all
{
903 if atomic
.Xadd(&panicking
, -1) != 0 {
904 // Some other m is panicking too.
905 // Let it print what it needs to print.
906 // Wait forever without chewing up cpu.
907 // It will exit when it's done.
920 func canpanic(gp
*g
) bool {
921 // Note that g is m->gsignal, different from gp.
922 // Note also that g->m can change at preemption, so m can go stale
923 // if this function ever makes a function call.
927 // Is it okay for gp to panic instead of crashing the program?
928 // Yes, as long as it is running Go code, not runtime code,
929 // and not stuck in a system call.
930 if gp
== nil || gp
!= _m_
.curg
{
933 if _m_
.locks
-_m_
.softfloat
!= 0 || _m_
.mallocing
!= 0 || _m_
.throwing
!= 0 || _m_
.preemptoff
!= "" || _m_
.dying
!= 0 {
936 status
:= readgstatus(gp
)
937 if status
&^_Gscan
!= _Grunning || gp
.syscallsp
!= 0 {