Black Lives Matter. Support the Equal Justice Initiative.

Source file src/runtime/panic.go

Documentation: runtime

     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.
     4  
     5  package runtime
     6  
     7  import (
     8  	"runtime/internal/atomic"
     9  	"runtime/internal/sys"
    10  	"unsafe"
    11  )
    12  
    13  // We have two different ways of doing defers. The older way involves creating a
    14  // defer record at the time that a defer statement is executing and adding it to a
    15  // defer chain. This chain is inspected by the deferreturn call at all function
    16  // exits in order to run the appropriate defer calls. A cheaper way (which we call
    17  // open-coded defers) is used for functions in which no defer statements occur in
    18  // loops. In that case, we simply store the defer function/arg information into
    19  // specific stack slots at the point of each defer statement, as well as setting a
    20  // bit in a bitmask. At each function exit, we add inline code to directly make
    21  // the appropriate defer calls based on the bitmask and fn/arg information stored
    22  // on the stack. During panic/Goexit processing, the appropriate defer calls are
    23  // made using extra funcdata info that indicates the exact stack slots that
    24  // contain the bitmask and defer fn/args.
    25  
    26  // Check to make sure we can really generate a panic. If the panic
    27  // was generated from the runtime, or from inside malloc, then convert
    28  // to a throw of msg.
    29  // pc should be the program counter of the compiler-generated code that
    30  // triggered this panic.
    31  func panicCheck1(pc uintptr, msg string) {
    32  	if sys.GoarchWasm == 0 && hasPrefix(funcname(findfunc(pc)), "runtime.") {
    33  		// Note: wasm can't tail call, so we can't get the original caller's pc.
    34  		throw(msg)
    35  	}
    36  	// TODO: is this redundant? How could we be in malloc
    37  	// but not in the runtime? runtime/internal/*, maybe?
    38  	gp := getg()
    39  	if gp != nil && gp.m != nil && gp.m.mallocing != 0 {
    40  		throw(msg)
    41  	}
    42  }
    43  
    44  // Same as above, but calling from the runtime is allowed.
    45  //
    46  // Using this function is necessary for any panic that may be
    47  // generated by runtime.sigpanic, since those are always called by the
    48  // runtime.
    49  func panicCheck2(err string) {
    50  	// panic allocates, so to avoid recursive malloc, turn panics
    51  	// during malloc into throws.
    52  	gp := getg()
    53  	if gp != nil && gp.m != nil && gp.m.mallocing != 0 {
    54  		throw(err)
    55  	}
    56  }
    57  
    58  // Many of the following panic entry-points turn into throws when they
    59  // happen in various runtime contexts. These should never happen in
    60  // the runtime, and if they do, they indicate a serious issue and
    61  // should not be caught by user code.
    62  //
    63  // The panic{Index,Slice,divide,shift} functions are called by
    64  // code generated by the compiler for out of bounds index expressions,
    65  // out of bounds slice expressions, division by zero, and shift by negative.
    66  // The panicdivide (again), panicoverflow, panicfloat, and panicmem
    67  // functions are called by the signal handler when a signal occurs
    68  // indicating the respective problem.
    69  //
    70  // Since panic{Index,Slice,shift} are never called directly, and
    71  // since the runtime package should never have an out of bounds slice
    72  // or array reference or negative shift, if we see those functions called from the
    73  // runtime package we turn the panic into a throw. That will dump the
    74  // entire runtime stack for easier debugging.
    75  //
    76  // The entry points called by the signal handler will be called from
    77  // runtime.sigpanic, so we can't disallow calls from the runtime to
    78  // these (they always look like they're called from the runtime).
    79  // Hence, for these, we just check for clearly bad runtime conditions.
    80  //
    81  // The panic{Index,Slice} functions are implemented in assembly and tail call
    82  // to the goPanic{Index,Slice} functions below. This is done so we can use
    83  // a space-minimal register calling convention.
    84  
    85  // failures in the comparisons for s[x], 0 <= x < y (y == len(s))
    86  func goPanicIndex(x int, y int) {
    87  	panicCheck1(getcallerpc(), "index out of range")
    88  	panic(boundsError{x: int64(x), signed: true, y: y, code: boundsIndex})
    89  }
    90  func goPanicIndexU(x uint, y int) {
    91  	panicCheck1(getcallerpc(), "index out of range")
    92  	panic(boundsError{x: int64(x), signed: false, y: y, code: boundsIndex})
    93  }
    94  
    95  // failures in the comparisons for s[:x], 0 <= x <= y (y == len(s) or cap(s))
    96  func goPanicSliceAlen(x int, y int) {
    97  	panicCheck1(getcallerpc(), "slice bounds out of range")
    98  	panic(boundsError{x: int64(x), signed: true, y: y, code: boundsSliceAlen})
    99  }
   100  func goPanicSliceAlenU(x uint, y int) {
   101  	panicCheck1(getcallerpc(), "slice bounds out of range")
   102  	panic(boundsError{x: int64(x), signed: false, y: y, code: boundsSliceAlen})
   103  }
   104  func goPanicSliceAcap(x int, y int) {
   105  	panicCheck1(getcallerpc(), "slice bounds out of range")
   106  	panic(boundsError{x: int64(x), signed: true, y: y, code: boundsSliceAcap})
   107  }
   108  func goPanicSliceAcapU(x uint, y int) {
   109  	panicCheck1(getcallerpc(), "slice bounds out of range")
   110  	panic(boundsError{x: int64(x), signed: false, y: y, code: boundsSliceAcap})
   111  }
   112  
   113  // failures in the comparisons for s[x:y], 0 <= x <= y
   114  func goPanicSliceB(x int, y int) {
   115  	panicCheck1(getcallerpc(), "slice bounds out of range")
   116  	panic(boundsError{x: int64(x), signed: true, y: y, code: boundsSliceB})
   117  }
   118  func goPanicSliceBU(x uint, y int) {
   119  	panicCheck1(getcallerpc(), "slice bounds out of range")
   120  	panic(boundsError{x: int64(x), signed: false, y: y, code: boundsSliceB})
   121  }
   122  
   123  // failures in the comparisons for s[::x], 0 <= x <= y (y == len(s) or cap(s))
   124  func goPanicSlice3Alen(x int, y int) {
   125  	panicCheck1(getcallerpc(), "slice bounds out of range")
   126  	panic(boundsError{x: int64(x), signed: true, y: y, code: boundsSlice3Alen})
   127  }
   128  func goPanicSlice3AlenU(x uint, y int) {
   129  	panicCheck1(getcallerpc(), "slice bounds out of range")
   130  	panic(boundsError{x: int64(x), signed: false, y: y, code: boundsSlice3Alen})
   131  }
   132  func goPanicSlice3Acap(x int, y int) {
   133  	panicCheck1(getcallerpc(), "slice bounds out of range")
   134  	panic(boundsError{x: int64(x), signed: true, y: y, code: boundsSlice3Acap})
   135  }
   136  func goPanicSlice3AcapU(x uint, y int) {
   137  	panicCheck1(getcallerpc(), "slice bounds out of range")
   138  	panic(boundsError{x: int64(x), signed: false, y: y, code: boundsSlice3Acap})
   139  }
   140  
   141  // failures in the comparisons for s[:x:y], 0 <= x <= y
   142  func goPanicSlice3B(x int, y int) {
   143  	panicCheck1(getcallerpc(), "slice bounds out of range")
   144  	panic(boundsError{x: int64(x), signed: true, y: y, code: boundsSlice3B})
   145  }
   146  func goPanicSlice3BU(x uint, y int) {
   147  	panicCheck1(getcallerpc(), "slice bounds out of range")
   148  	panic(boundsError{x: int64(x), signed: false, y: y, code: boundsSlice3B})
   149  }
   150  
   151  // failures in the comparisons for s[x:y:], 0 <= x <= y
   152  func goPanicSlice3C(x int, y int) {
   153  	panicCheck1(getcallerpc(), "slice bounds out of range")
   154  	panic(boundsError{x: int64(x), signed: true, y: y, code: boundsSlice3C})
   155  }
   156  func goPanicSlice3CU(x uint, y int) {
   157  	panicCheck1(getcallerpc(), "slice bounds out of range")
   158  	panic(boundsError{x: int64(x), signed: false, y: y, code: boundsSlice3C})
   159  }
   160  
   161  // Implemented in assembly, as they take arguments in registers.
   162  // Declared here to mark them as ABIInternal.
   163  func panicIndex(x int, y int)
   164  func panicIndexU(x uint, y int)
   165  func panicSliceAlen(x int, y int)
   166  func panicSliceAlenU(x uint, y int)
   167  func panicSliceAcap(x int, y int)
   168  func panicSliceAcapU(x uint, y int)
   169  func panicSliceB(x int, y int)
   170  func panicSliceBU(x uint, y int)
   171  func panicSlice3Alen(x int, y int)
   172  func panicSlice3AlenU(x uint, y int)
   173  func panicSlice3Acap(x int, y int)
   174  func panicSlice3AcapU(x uint, y int)
   175  func panicSlice3B(x int, y int)
   176  func panicSlice3BU(x uint, y int)
   177  func panicSlice3C(x int, y int)
   178  func panicSlice3CU(x uint, y int)
   179  
   180  var shiftError = error(errorString("negative shift amount"))
   181  
   182  func panicshift() {
   183  	panicCheck1(getcallerpc(), "negative shift amount")
   184  	panic(shiftError)
   185  }
   186  
   187  var divideError = error(errorString("integer divide by zero"))
   188  
   189  func panicdivide() {
   190  	panicCheck2("integer divide by zero")
   191  	panic(divideError)
   192  }
   193  
   194  var overflowError = error(errorString("integer overflow"))
   195  
   196  func panicoverflow() {
   197  	panicCheck2("integer overflow")
   198  	panic(overflowError)
   199  }
   200  
   201  var floatError = error(errorString("floating point error"))
   202  
   203  func panicfloat() {
   204  	panicCheck2("floating point error")
   205  	panic(floatError)
   206  }
   207  
   208  var memoryError = error(errorString("invalid memory address or nil pointer dereference"))
   209  
   210  func panicmem() {
   211  	panicCheck2("invalid memory address or nil pointer dereference")
   212  	panic(memoryError)
   213  }
   214  
   215  // Create a new deferred function fn with siz bytes of arguments.
   216  // The compiler turns a defer statement into a call to this.
   217  //go:nosplit
   218  func deferproc(siz int32, fn *funcval) { // arguments of fn follow fn
   219  	gp := getg()
   220  	if gp.m.curg != gp {
   221  		// go code on the system stack can't defer
   222  		throw("defer on system stack")
   223  	}
   224  
   225  	// the arguments of fn are in a perilous state. The stack map
   226  	// for deferproc does not describe them. So we can't let garbage
   227  	// collection or stack copying trigger until we've copied them out
   228  	// to somewhere safe. The memmove below does that.
   229  	// Until the copy completes, we can only call nosplit routines.
   230  	sp := getcallersp()
   231  	argp := uintptr(unsafe.Pointer(&fn)) + unsafe.Sizeof(fn)
   232  	callerpc := getcallerpc()
   233  
   234  	d := newdefer(siz)
   235  	if d._panic != nil {
   236  		throw("deferproc: d.panic != nil after newdefer")
   237  	}
   238  	d.link = gp._defer
   239  	gp._defer = d
   240  	d.fn = fn
   241  	d.pc = callerpc
   242  	d.sp = sp
   243  	switch siz {
   244  	case 0:
   245  		// Do nothing.
   246  	case sys.PtrSize:
   247  		*(*uintptr)(deferArgs(d)) = *(*uintptr)(unsafe.Pointer(argp))
   248  	default:
   249  		memmove(deferArgs(d), unsafe.Pointer(argp), uintptr(siz))
   250  	}
   251  
   252  	// deferproc returns 0 normally.
   253  	// a deferred func that stops a panic
   254  	// makes the deferproc return 1.
   255  	// the code the compiler generates always
   256  	// checks the return value and jumps to the
   257  	// end of the function if deferproc returns != 0.
   258  	return0()
   259  	// No code can go here - the C return register has
   260  	// been set and must not be clobbered.
   261  }
   262  
   263  // deferprocStack queues a new deferred function with a defer record on the stack.
   264  // The defer record must have its siz and fn fields initialized.
   265  // All other fields can contain junk.
   266  // The defer record must be immediately followed in memory by
   267  // the arguments of the defer.
   268  // Nosplit because the arguments on the stack won't be scanned
   269  // until the defer record is spliced into the gp._defer list.
   270  //go:nosplit
   271  func deferprocStack(d *_defer) {
   272  	gp := getg()
   273  	if gp.m.curg != gp {
   274  		// go code on the system stack can't defer
   275  		throw("defer on system stack")
   276  	}
   277  	// siz and fn are already set.
   278  	// The other fields are junk on entry to deferprocStack and
   279  	// are initialized here.
   280  	d.started = false
   281  	d.heap = false
   282  	d.openDefer = false
   283  	d.sp = getcallersp()
   284  	d.pc = getcallerpc()
   285  	d.framepc = 0
   286  	d.varp = 0
   287  	// The lines below implement:
   288  	//   d.panic = nil
   289  	//   d.fd = nil
   290  	//   d.link = gp._defer
   291  	//   gp._defer = d
   292  	// But without write barriers. The first three are writes to
   293  	// the stack so they don't need a write barrier, and furthermore
   294  	// are to uninitialized memory, so they must not use a write barrier.
   295  	// The fourth write does not require a write barrier because we
   296  	// explicitly mark all the defer structures, so we don't need to
   297  	// keep track of pointers to them with a write barrier.
   298  	*(*uintptr)(unsafe.Pointer(&d._panic)) = 0
   299  	*(*uintptr)(unsafe.Pointer(&d.fd)) = 0
   300  	*(*uintptr)(unsafe.Pointer(&d.link)) = uintptr(unsafe.Pointer(gp._defer))
   301  	*(*uintptr)(unsafe.Pointer(&gp._defer)) = uintptr(unsafe.Pointer(d))
   302  
   303  	return0()
   304  	// No code can go here - the C return register has
   305  	// been set and must not be clobbered.
   306  }
   307  
   308  // Small malloc size classes >= 16 are the multiples of 16: 16, 32, 48, 64, 80, 96, 112, 128, 144, ...
   309  // Each P holds a pool for defers with small arg sizes.
   310  // Assign defer allocations to pools by rounding to 16, to match malloc size classes.
   311  
   312  const (
   313  	deferHeaderSize = unsafe.Sizeof(_defer{})
   314  	minDeferAlloc   = (deferHeaderSize + 15) &^ 15
   315  	minDeferArgs    = minDeferAlloc - deferHeaderSize
   316  )
   317  
   318  // defer size class for arg size sz
   319  //go:nosplit
   320  func deferclass(siz uintptr) uintptr {
   321  	if siz <= minDeferArgs {
   322  		return 0
   323  	}
   324  	return (siz - minDeferArgs + 15) / 16
   325  }
   326  
   327  // total size of memory block for defer with arg size sz
   328  func totaldefersize(siz uintptr) uintptr {
   329  	if siz <= minDeferArgs {
   330  		return minDeferAlloc
   331  	}
   332  	return deferHeaderSize + siz
   333  }
   334  
   335  // Ensure that defer arg sizes that map to the same defer size class
   336  // also map to the same malloc size class.
   337  func testdefersizes() {
   338  	var m [len(p{}.deferpool)]int32
   339  
   340  	for i := range m {
   341  		m[i] = -1
   342  	}
   343  	for i := uintptr(0); ; i++ {
   344  		defersc := deferclass(i)
   345  		if defersc >= uintptr(len(m)) {
   346  			break
   347  		}
   348  		siz := roundupsize(totaldefersize(i))
   349  		if m[defersc] < 0 {
   350  			m[defersc] = int32(siz)
   351  			continue
   352  		}
   353  		if m[defersc] != int32(siz) {
   354  			print("bad defer size class: i=", i, " siz=", siz, " defersc=", defersc, "\n")
   355  			throw("bad defer size class")
   356  		}
   357  	}
   358  }
   359  
   360  // The arguments associated with a deferred call are stored
   361  // immediately after the _defer header in memory.
   362  //go:nosplit
   363  func deferArgs(d *_defer) unsafe.Pointer {
   364  	if d.siz == 0 {
   365  		// Avoid pointer past the defer allocation.
   366  		return nil
   367  	}
   368  	return add(unsafe.Pointer(d), unsafe.Sizeof(*d))
   369  }
   370  
   371  var deferType *_type // type of _defer struct
   372  
   373  func init() {
   374  	var x interface{}
   375  	x = (*_defer)(nil)
   376  	deferType = (*(**ptrtype)(unsafe.Pointer(&x))).elem
   377  }
   378  
   379  // Allocate a Defer, usually using per-P pool.
   380  // Each defer must be released with freedefer.  The defer is not
   381  // added to any defer chain yet.
   382  //
   383  // This must not grow the stack because there may be a frame without
   384  // stack map information when this is called.
   385  //
   386  //go:nosplit
   387  func newdefer(siz int32) *_defer {
   388  	var d *_defer
   389  	sc := deferclass(uintptr(siz))
   390  	gp := getg()
   391  	if sc < uintptr(len(p{}.deferpool)) {
   392  		pp := gp.m.p.ptr()
   393  		if len(pp.deferpool[sc]) == 0 && sched.deferpool[sc] != nil {
   394  			// Take the slow path on the system stack so
   395  			// we don't grow newdefer's stack.
   396  			systemstack(func() {
   397  				lock(&sched.deferlock)
   398  				for len(pp.deferpool[sc]) < cap(pp.deferpool[sc])/2 && sched.deferpool[sc] != nil {
   399  					d := sched.deferpool[sc]
   400  					sched.deferpool[sc] = d.link
   401  					d.link = nil
   402  					pp.deferpool[sc] = append(pp.deferpool[sc], d)
   403  				}
   404  				unlock(&sched.deferlock)
   405  			})
   406  		}
   407  		if n := len(pp.deferpool[sc]); n > 0 {
   408  			d = pp.deferpool[sc][n-1]
   409  			pp.deferpool[sc][n-1] = nil
   410  			pp.deferpool[sc] = pp.deferpool[sc][:n-1]
   411  		}
   412  	}
   413  	if d == nil {
   414  		// Allocate new defer+args.
   415  		systemstack(func() {
   416  			total := roundupsize(totaldefersize(uintptr(siz)))
   417  			d = (*_defer)(mallocgc(total, deferType, true))
   418  		})
   419  		if debugCachedWork {
   420  			// Duplicate the tail below so if there's a
   421  			// crash in checkPut we can tell if d was just
   422  			// allocated or came from the pool.
   423  			d.siz = siz
   424  			d.link = gp._defer
   425  			gp._defer = d
   426  			return d
   427  		}
   428  	}
   429  	d.siz = siz
   430  	d.heap = true
   431  	return d
   432  }
   433  
   434  // Free the given defer.
   435  // The defer cannot be used after this call.
   436  //
   437  // This must not grow the stack because there may be a frame without a
   438  // stack map when this is called.
   439  //
   440  //go:nosplit
   441  func freedefer(d *_defer) {
   442  	if d._panic != nil {
   443  		freedeferpanic()
   444  	}
   445  	if d.fn != nil {
   446  		freedeferfn()
   447  	}
   448  	if !d.heap {
   449  		return
   450  	}
   451  	sc := deferclass(uintptr(d.siz))
   452  	if sc >= uintptr(len(p{}.deferpool)) {
   453  		return
   454  	}
   455  	pp := getg().m.p.ptr()
   456  	if len(pp.deferpool[sc]) == cap(pp.deferpool[sc]) {
   457  		// Transfer half of local cache to the central cache.
   458  		//
   459  		// Take this slow path on the system stack so
   460  		// we don't grow freedefer's stack.
   461  		systemstack(func() {
   462  			var first, last *_defer
   463  			for len(pp.deferpool[sc]) > cap(pp.deferpool[sc])/2 {
   464  				n := len(pp.deferpool[sc])
   465  				d := pp.deferpool[sc][n-1]
   466  				pp.deferpool[sc][n-1] = nil
   467  				pp.deferpool[sc] = pp.deferpool[sc][:n-1]
   468  				if first == nil {
   469  					first = d
   470  				} else {
   471  					last.link = d
   472  				}
   473  				last = d
   474  			}
   475  			lock(&sched.deferlock)
   476  			last.link = sched.deferpool[sc]
   477  			sched.deferpool[sc] = first
   478  			unlock(&sched.deferlock)
   479  		})
   480  	}
   481  
   482  	// These lines used to be simply `*d = _defer{}` but that
   483  	// started causing a nosplit stack overflow via typedmemmove.
   484  	d.siz = 0
   485  	d.started = false
   486  	d.openDefer = false
   487  	d.sp = 0
   488  	d.pc = 0
   489  	d.framepc = 0
   490  	d.varp = 0
   491  	d.fd = nil
   492  	// d._panic and d.fn must be nil already.
   493  	// If not, we would have called freedeferpanic or freedeferfn above,
   494  	// both of which throw.
   495  	d.link = nil
   496  
   497  	pp.deferpool[sc] = append(pp.deferpool[sc], d)
   498  }
   499  
   500  // Separate function so that it can split stack.
   501  // Windows otherwise runs out of stack space.
   502  func freedeferpanic() {
   503  	// _panic must be cleared before d is unlinked from gp.
   504  	throw("freedefer with d._panic != nil")
   505  }
   506  
   507  func freedeferfn() {
   508  	// fn must be cleared before d is unlinked from gp.
   509  	throw("freedefer with d.fn != nil")
   510  }
   511  
   512  // Run a deferred function if there is one.
   513  // The compiler inserts a call to this at the end of any
   514  // function which calls defer.
   515  // If there is a deferred function, this will call runtime¬∑jmpdefer,
   516  // which will jump to the deferred function such that it appears
   517  // to have been called by the caller of deferreturn at the point
   518  // just before deferreturn was called. The effect is that deferreturn
   519  // is called again and again until there are no more deferred functions.
   520  //
   521  // Declared as nosplit, because the function should not be preempted once we start
   522  // modifying the caller's frame in order to reuse the frame to call the deferred
   523  // function.
   524  //
   525  // The single argument isn't actually used - it just has its address
   526  // taken so it can be matched against pending defers.
   527  //go:nosplit
   528  func deferreturn(arg0 uintptr) {
   529  	gp := getg()
   530  	d := gp._defer
   531  	if d == nil {
   532  		return
   533  	}
   534  	sp := getcallersp()
   535  	if d.sp != sp {
   536  		return
   537  	}
   538  	if d.openDefer {
   539  		done := runOpenDeferFrame(gp, d)
   540  		if !done {
   541  			throw("unfinished open-coded defers in deferreturn")
   542  		}
   543  		gp._defer = d.link
   544  		freedefer(d)
   545  		return
   546  	}
   547  
   548  	// Moving arguments around.
   549  	//
   550  	// Everything called after this point must be recursively
   551  	// nosplit because the garbage collector won't know the form
   552  	// of the arguments until the jmpdefer can flip the PC over to
   553  	// fn.
   554  	switch d.siz {
   555  	case 0:
   556  		// Do nothing.
   557  	case sys.PtrSize:
   558  		*(*uintptr)(unsafe.Pointer(&arg0)) = *(*uintptr)(deferArgs(d))
   559  	default:
   560  		memmove(unsafe.Pointer(&arg0), deferArgs(d), uintptr(d.siz))
   561  	}
   562  	fn := d.fn
   563  	d.fn = nil
   564  	gp._defer = d.link
   565  	freedefer(d)
   566  	// If the defer function pointer is nil, force the seg fault to happen
   567  	// here rather than in jmpdefer. gentraceback() throws an error if it is
   568  	// called with a callback on an LR architecture and jmpdefer is on the
   569  	// stack, because the stack trace can be incorrect in that case - see
   570  	// issue #8153).
   571  	_ = fn.fn
   572  	jmpdefer(fn, uintptr(unsafe.Pointer(&arg0)))
   573  }
   574  
   575  // Goexit terminates the goroutine that calls it. No other goroutine is affected.
   576  // Goexit runs all deferred calls before terminating the goroutine. Because Goexit
   577  // is not a panic, any recover calls in those deferred functions will return nil.
   578  //
   579  // Calling Goexit from the main goroutine terminates that goroutine
   580  // without func main returning. Since func main has not returned,
   581  // the program continues execution of other goroutines.
   582  // If all other goroutines exit, the program crashes.
   583  func Goexit() {
   584  	// Run all deferred functions for the current goroutine.
   585  	// This code is similar to gopanic, see that implementation
   586  	// for detailed comments.
   587  	gp := getg()
   588  
   589  	// Create a panic object for Goexit, so we can recognize when it might be
   590  	// bypassed by a recover().
   591  	var p _panic
   592  	p.goexit = true
   593  	p.link = gp._panic
   594  	gp._panic = (*_panic)(noescape(unsafe.Pointer(&p)))
   595  
   596  	addOneOpenDeferFrame(gp, getcallerpc(), unsafe.Pointer(getcallersp()))
   597  	for {
   598  		d := gp._defer
   599  		if d == nil {
   600  			break
   601  		}
   602  		if d.started {
   603  			if d._panic != nil {
   604  				d._panic.aborted = true
   605  				d._panic = nil
   606  			}
   607  			if !d.openDefer {
   608  				d.fn = nil
   609  				gp._defer = d.link
   610  				freedefer(d)
   611  				continue
   612  			}
   613  		}
   614  		d.started = true
   615  		d._panic = (*_panic)(noescape(unsafe.Pointer(&p)))
   616  		if d.openDefer {
   617  			done := runOpenDeferFrame(gp, d)
   618  			if !done {
   619  				// We should always run all defers in the frame,
   620  				// since there is no panic associated with this
   621  				// defer that can be recovered.
   622  				throw("unfinished open-coded defers in Goexit")
   623  			}
   624  			if p.aborted {
   625  				// Since our current defer caused a panic and may
   626  				// have been already freed, just restart scanning
   627  				// for open-coded defers from this frame again.
   628  				addOneOpenDeferFrame(gp, getcallerpc(), unsafe.Pointer(getcallersp()))
   629  			} else {
   630  				addOneOpenDeferFrame(gp, 0, nil)
   631  			}
   632  		} else {
   633  
   634  			// Save the pc/sp in reflectcallSave(), so we can "recover" back to this
   635  			// loop if necessary.
   636  			reflectcallSave(&p, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz))
   637  		}
   638  		if p.aborted {
   639  			// We had a recursive panic in the defer d we started, and
   640  			// then did a recover in a defer that was further down the
   641  			// defer chain than d. In the case of an outstanding Goexit,
   642  			// we force the recover to return back to this loop. d will
   643  			// have already been freed if completed, so just continue
   644  			// immediately to the next defer on the chain.
   645  			p.aborted = false
   646  			continue
   647  		}
   648  		if gp._defer != d {
   649  			throw("bad defer entry in Goexit")
   650  		}
   651  		d._panic = nil
   652  		d.fn = nil
   653  		gp._defer = d.link
   654  		freedefer(d)
   655  		// Note: we ignore recovers here because Goexit isn't a panic
   656  	}
   657  	goexit1()
   658  }
   659  
   660  // Call all Error and String methods before freezing the world.
   661  // Used when crashing with panicking.
   662  func preprintpanics(p *_panic) {
   663  	defer func() {
   664  		if recover() != nil {
   665  			throw("panic while printing panic value")
   666  		}
   667  	}()
   668  	for p != nil {
   669  		switch v := p.arg.(type) {
   670  		case error:
   671  			p.arg = v.Error()
   672  		case stringer:
   673  			p.arg = v.String()
   674  		}
   675  		p = p.link
   676  	}
   677  }
   678  
   679  // Print all currently active panics. Used when crashing.
   680  // Should only be called after preprintpanics.
   681  func printpanics(p *_panic) {
   682  	if p.link != nil {
   683  		printpanics(p.link)
   684  		if !p.link.goexit {
   685  			print("\t")
   686  		}
   687  	}
   688  	if p.goexit {
   689  		return
   690  	}
   691  	print("panic: ")
   692  	printany(p.arg)
   693  	if p.recovered {
   694  		print(" [recovered]")
   695  	}
   696  	print("\n")
   697  }
   698  
   699  // addOneOpenDeferFrame scans the stack for the first frame (if any) with
   700  // open-coded defers and if it finds one, adds a single record to the defer chain
   701  // for that frame. If sp is non-nil, it starts the stack scan from the frame
   702  // specified by sp. If sp is nil, it uses the sp from the current defer record
   703  // (which has just been finished). Hence, it continues the stack scan from the
   704  // frame of the defer that just finished. It skips any frame that already has an
   705  // open-coded _defer record, which would have been been created from a previous
   706  // (unrecovered) panic.
   707  //
   708  // Note: All entries of the defer chain (including this new open-coded entry) have
   709  // their pointers (including sp) adjusted properly if the stack moves while
   710  // running deferred functions. Also, it is safe to pass in the sp arg (which is
   711  // the direct result of calling getcallersp()), because all pointer variables
   712  // (including arguments) are adjusted as needed during stack copies.
   713  func addOneOpenDeferFrame(gp *g, pc uintptr, sp unsafe.Pointer) {
   714  	var prevDefer *_defer
   715  	if sp == nil {
   716  		prevDefer = gp._defer
   717  		pc = prevDefer.framepc
   718  		sp = unsafe.Pointer(prevDefer.sp)
   719  	}
   720  	systemstack(func() {
   721  		gentraceback(pc, uintptr(sp), 0, gp, 0, nil, 0x7fffffff,
   722  			func(frame *stkframe, unused unsafe.Pointer) bool {
   723  				if prevDefer != nil && prevDefer.sp == frame.sp {
   724  					// Skip the frame for the previous defer that
   725  					// we just finished (and was used to set
   726  					// where we restarted the stack scan)
   727  					return true
   728  				}
   729  				f := frame.fn
   730  				fd := funcdata(f, _FUNCDATA_OpenCodedDeferInfo)
   731  				if fd == nil {
   732  					return true
   733  				}
   734  				// Insert the open defer record in the
   735  				// chain, in order sorted by sp.
   736  				d := gp._defer
   737  				var prev *_defer
   738  				for d != nil {
   739  					dsp := d.sp
   740  					if frame.sp < dsp {
   741  						break
   742  					}
   743  					if frame.sp == dsp {
   744  						if !d.openDefer {
   745  							throw("duplicated defer entry")
   746  						}
   747  						return true
   748  					}
   749  					prev = d
   750  					d = d.link
   751  				}
   752  				if frame.fn.deferreturn == 0 {
   753  					throw("missing deferreturn")
   754  				}
   755  
   756  				maxargsize, _ := readvarintUnsafe(fd)
   757  				d1 := newdefer(int32(maxargsize))
   758  				d1.openDefer = true
   759  				d1._panic = nil
   760  				// These are the pc/sp to set after we've
   761  				// run a defer in this frame that did a
   762  				// recover. We return to a special
   763  				// deferreturn that runs any remaining
   764  				// defers and then returns from the
   765  				// function.
   766  				d1.pc = frame.fn.entry + uintptr(frame.fn.deferreturn)
   767  				d1.varp = frame.varp
   768  				d1.fd = fd
   769  				// Save the SP/PC associated with current frame,
   770  				// so we can continue stack trace later if needed.
   771  				d1.framepc = frame.pc
   772  				d1.sp = frame.sp
   773  				d1.link = d
   774  				if prev == nil {
   775  					gp._defer = d1
   776  				} else {
   777  					prev.link = d1
   778  				}
   779  				// Stop stack scanning after adding one open defer record
   780  				return false
   781  			},
   782  			nil, 0)
   783  	})
   784  }
   785  
   786  // readvarintUnsafe reads the uint32 in varint format starting at fd, and returns the
   787  // uint32 and a pointer to the byte following the varint.
   788  //
   789  // There is a similar function runtime.readvarint, which takes a slice of bytes,
   790  // rather than an unsafe pointer. These functions are duplicated, because one of
   791  // the two use cases for the functions would get slower if the functions were
   792  // combined.
   793  func readvarintUnsafe(fd unsafe.Pointer) (uint32, unsafe.Pointer) {
   794  	var r uint32
   795  	var shift int
   796  	for {
   797  		b := *(*uint8)((unsafe.Pointer(fd)))
   798  		fd = add(fd, unsafe.Sizeof(b))
   799  		if b < 128 {
   800  			return r + uint32(b)<<shift, fd
   801  		}
   802  		r += ((uint32(b) &^ 128) << shift)
   803  		shift += 7
   804  		if shift > 28 {
   805  			panic("Bad varint")
   806  		}
   807  	}
   808  }
   809  
   810  // runOpenDeferFrame runs the active open-coded defers in the frame specified by
   811  // d. It normally processes all active defers in the frame, but stops immediately
   812  // if a defer does a successful recover. It returns true if there are no
   813  // remaining defers to run in the frame.
   814  func runOpenDeferFrame(gp *g, d *_defer) bool {
   815  	done := true
   816  	fd := d.fd
   817  
   818  	// Skip the maxargsize
   819  	_, fd = readvarintUnsafe(fd)
   820  	deferBitsOffset, fd := readvarintUnsafe(fd)
   821  	nDefers, fd := readvarintUnsafe(fd)
   822  	deferBits := *(*uint8)(unsafe.Pointer(d.varp - uintptr(deferBitsOffset)))
   823  
   824  	for i := int(nDefers) - 1; i >= 0; i-- {
   825  		// read the funcdata info for this defer
   826  		var argWidth, closureOffset, nArgs uint32
   827  		argWidth, fd = readvarintUnsafe(fd)
   828  		closureOffset, fd = readvarintUnsafe(fd)
   829  		nArgs, fd = readvarintUnsafe(fd)
   830  		if deferBits&(1<<i) == 0 {
   831  			for j := uint32(0); j < nArgs; j++ {
   832  				_, fd = readvarintUnsafe(fd)
   833  				_, fd = readvarintUnsafe(fd)
   834  				_, fd = readvarintUnsafe(fd)
   835  			}
   836  			continue
   837  		}
   838  		closure := *(**funcval)(unsafe.Pointer(d.varp - uintptr(closureOffset)))
   839  		d.fn = closure
   840  		deferArgs := deferArgs(d)
   841  		// If there is an interface receiver or method receiver, it is
   842  		// described/included as the first arg.
   843  		for j := uint32(0); j < nArgs; j++ {
   844  			var argOffset, argLen, argCallOffset uint32
   845  			argOffset, fd = readvarintUnsafe(fd)
   846  			argLen, fd = readvarintUnsafe(fd)
   847  			argCallOffset, fd = readvarintUnsafe(fd)
   848  			memmove(unsafe.Pointer(uintptr(deferArgs)+uintptr(argCallOffset)),
   849  				unsafe.Pointer(d.varp-uintptr(argOffset)),
   850  				uintptr(argLen))
   851  		}
   852  		deferBits = deferBits &^ (1 << i)
   853  		*(*uint8)(unsafe.Pointer(d.varp - uintptr(deferBitsOffset))) = deferBits
   854  		p := d._panic
   855  		reflectcallSave(p, unsafe.Pointer(closure), deferArgs, argWidth)
   856  		if p != nil && p.aborted {
   857  			break
   858  		}
   859  		d.fn = nil
   860  		// These args are just a copy, so can be cleared immediately
   861  		memclrNoHeapPointers(deferArgs, uintptr(argWidth))
   862  		if d._panic != nil && d._panic.recovered {
   863  			done = deferBits == 0
   864  			break
   865  		}
   866  	}
   867  
   868  	return done
   869  }
   870  
   871  // reflectcallSave calls reflectcall after saving the caller's pc and sp in the
   872  // panic record. This allows the runtime to return to the Goexit defer processing
   873  // loop, in the unusual case where the Goexit may be bypassed by a successful
   874  // recover.
   875  func reflectcallSave(p *_panic, fn, arg unsafe.Pointer, argsize uint32) {
   876  	if p != nil {
   877  		p.argp = unsafe.Pointer(getargp(0))
   878  		p.pc = getcallerpc()
   879  		p.sp = unsafe.Pointer(getcallersp())
   880  	}
   881  	reflectcall(nil, fn, arg, argsize, argsize)
   882  	if p != nil {
   883  		p.pc = 0
   884  		p.sp = unsafe.Pointer(nil)
   885  	}
   886  }
   887  
   888  // The implementation of the predeclared function panic.
   889  func gopanic(e interface{}) {
   890  	gp := getg()
   891  	if gp.m.curg != gp {
   892  		print("panic: ")
   893  		printany(e)
   894  		print("\n")
   895  		throw("panic on system stack")
   896  	}
   897  
   898  	if gp.m.mallocing != 0 {
   899  		print("panic: ")
   900  		printany(e)
   901  		print("\n")
   902  		throw("panic during malloc")
   903  	}
   904  	if gp.m.preemptoff != "" {
   905  		print("panic: ")
   906  		printany(e)
   907  		print("\n")
   908  		print("preempt off reason: ")
   909  		print(gp.m.preemptoff)
   910  		print("\n")
   911  		throw("panic during preemptoff")
   912  	}
   913  	if gp.m.locks != 0 {
   914  		print("panic: ")
   915  		printany(e)
   916  		print("\n")
   917  		throw("panic holding locks")
   918  	}
   919  
   920  	var p _panic
   921  	p.arg = e
   922  	p.link = gp._panic
   923  	gp._panic = (*_panic)(noescape(unsafe.Pointer(&p)))
   924  
   925  	atomic.Xadd(&runningPanicDefers, 1)
   926  
   927  	// By calculating getcallerpc/getcallersp here, we avoid scanning the
   928  	// gopanic frame (stack scanning is slow...)
   929  	addOneOpenDeferFrame(gp, getcallerpc(), unsafe.Pointer(getcallersp()))
   930  
   931  	for {
   932  		d := gp._defer
   933  		if d == nil {
   934  			break
   935  		}
   936  
   937  		// If defer was started by earlier panic or Goexit (and, since we're back here, that triggered a new panic),
   938  		// take defer off list. An earlier panic will not continue running, but we will make sure below that an
   939  		// earlier Goexit does continue running.
   940  		if d.started {
   941  			if d._panic != nil {
   942  				d._panic.aborted = true
   943  			}
   944  			d._panic = nil
   945  			if !d.openDefer {
   946  				// For open-coded defers, we need to process the
   947  				// defer again, in case there are any other defers
   948  				// to call in the frame (not including the defer
   949  				// call that caused the panic).
   950  				d.fn = nil
   951  				gp._defer = d.link
   952  				freedefer(d)
   953  				continue
   954  			}
   955  		}
   956  
   957  		// Mark defer as started, but keep on list, so that traceback
   958  		// can find and update the defer's argument frame if stack growth
   959  		// or a garbage collection happens before reflectcall starts executing d.fn.
   960  		d.started = true
   961  
   962  		// Record the panic that is running the defer.
   963  		// If there is a new panic during the deferred call, that panic
   964  		// will find d in the list and will mark d._panic (this panic) aborted.
   965  		d._panic = (*_panic)(noescape(unsafe.Pointer(&p)))
   966  
   967  		done := true
   968  		if d.openDefer {
   969  			done = runOpenDeferFrame(gp, d)
   970  			if done && !d._panic.recovered {
   971  				addOneOpenDeferFrame(gp, 0, nil)
   972  			}
   973  		} else {
   974  			p.argp = unsafe.Pointer(getargp(0))
   975  			reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
   976  		}
   977  		p.argp = nil
   978  
   979  		// reflectcall did not panic. Remove d.
   980  		if gp._defer != d {
   981  			throw("bad defer entry in panic")
   982  		}
   983  		d._panic = nil
   984  
   985  		// trigger shrinkage to test stack copy. See stack_test.go:TestStackPanic
   986  		//GC()
   987  
   988  		pc := d.pc
   989  		sp := unsafe.Pointer(d.sp) // must be pointer so it gets adjusted during stack copy
   990  		if done {
   991  			d.fn = nil
   992  			gp._defer = d.link
   993  			freedefer(d)
   994  		}
   995  		if p.recovered {
   996  			gp._panic = p.link
   997  			if gp._panic != nil && gp._panic.goexit && gp._panic.aborted {
   998  				// A normal recover would bypass/abort the Goexit.  Instead,
   999  				// we return to the processing loop of the Goexit.
  1000  				gp.sigcode0 = uintptr(gp._panic.sp)
  1001  				gp.sigcode1 = uintptr(gp._panic.pc)
  1002  				mcall(recovery)
  1003  				throw("bypassed recovery failed") // mcall should not return
  1004  			}
  1005  			atomic.Xadd(&runningPanicDefers, -1)
  1006  
  1007  			if done {
  1008  				// Remove any remaining non-started, open-coded
  1009  				// defer entries after a recover, since the
  1010  				// corresponding defers will be executed normally
  1011  				// (inline). Any such entry will become stale once
  1012  				// we run the corresponding defers inline and exit
  1013  				// the associated stack frame.
  1014  				d := gp._defer
  1015  				var prev *_defer
  1016  				for d != nil {
  1017  					if d.openDefer {
  1018  						if d.started {
  1019  							// This defer is started but we
  1020  							// are in the middle of a
  1021  							// defer-panic-recover inside of
  1022  							// it, so don't remove it or any
  1023  							// further defer entries
  1024  							break
  1025  						}
  1026  						if prev == nil {
  1027  							gp._defer = d.link
  1028  						} else {
  1029  							prev.link = d.link
  1030  						}
  1031  						newd := d.link
  1032  						freedefer(d)
  1033  						d = newd
  1034  					} else {
  1035  						prev = d
  1036  						d = d.link
  1037  					}
  1038  				}
  1039  			}
  1040  
  1041  			gp._panic = p.link
  1042  			// Aborted panics are marked but remain on the g.panic list.
  1043  			// Remove them from the list.
  1044  			for gp._panic != nil && gp._panic.aborted {
  1045  				gp._panic = gp._panic.link
  1046  			}
  1047  			if gp._panic == nil { // must be done with signal
  1048  				gp.sig = 0
  1049  			}
  1050  			// Pass information about recovering frame to recovery.
  1051  			gp.sigcode0 = uintptr(sp)
  1052  			gp.sigcode1 = pc
  1053  			mcall(recovery)
  1054  			throw("recovery failed") // mcall should not return
  1055  		}
  1056  	}
  1057  
  1058  	// ran out of deferred calls - old-school panic now
  1059  	// Because it is unsafe to call arbitrary user code after freezing
  1060  	// the world, we call preprintpanics to invoke all necessary Error
  1061  	// and String methods to prepare the panic strings before startpanic.
  1062  	preprintpanics(gp._panic)
  1063  
  1064  	fatalpanic(gp._panic) // should not return
  1065  	*(*int)(nil) = 0      // not reached
  1066  }
  1067  
  1068  // getargp returns the location where the caller
  1069  // writes outgoing function call arguments.
  1070  //go:nosplit
  1071  //go:noinline
  1072  func getargp(x int) uintptr {
  1073  	// x is an argument mainly so that we can return its address.
  1074  	return uintptr(noescape(unsafe.Pointer(&x)))
  1075  }
  1076  
  1077  // The implementation of the predeclared function recover.
  1078  // Cannot split the stack because it needs to reliably
  1079  // find the stack segment of its caller.
  1080  //
  1081  // TODO(rsc): Once we commit to CopyStackAlways,
  1082  // this doesn't need to be nosplit.
  1083  //go:nosplit
  1084  func gorecover(argp uintptr) interface{} {
  1085  	// Must be in a function running as part of a deferred call during the panic.
  1086  	// Must be called from the topmost function of the call
  1087  	// (the function used in the defer statement).
  1088  	// p.argp is the argument pointer of that topmost deferred function call.
  1089  	// Compare against argp reported by caller.
  1090  	// If they match, the caller is the one who can recover.
  1091  	gp := getg()
  1092  	p := gp._panic
  1093  	if p != nil && !p.goexit && !p.recovered && argp == uintptr(p.argp) {
  1094  		p.recovered = true
  1095  		return p.arg
  1096  	}
  1097  	return nil
  1098  }
  1099  
  1100  //go:linkname sync_throw sync.throw
  1101  func sync_throw(s string) {
  1102  	throw(s)
  1103  }
  1104  
  1105  //go:nosplit
  1106  func throw(s string) {
  1107  	// Everything throw does should be recursively nosplit so it
  1108  	// can be called even when it's unsafe to grow the stack.
  1109  	systemstack(func() {
  1110  		print("fatal error: ", s, "\n")
  1111  	})
  1112  	gp := getg()
  1113  	if gp.m.throwing == 0 {
  1114  		gp.m.throwing = 1
  1115  	}
  1116  	fatalthrow()
  1117  	*(*int)(nil) = 0 // not reached
  1118  }
  1119  
  1120  // runningPanicDefers is non-zero while running deferred functions for panic.
  1121  // runningPanicDefers is incremented and decremented atomically.
  1122  // This is used to try hard to get a panic stack trace out when exiting.
  1123  var runningPanicDefers uint32
  1124  
  1125  // panicking is non-zero when crashing the program for an unrecovered panic.
  1126  // panicking is incremented and decremented atomically.
  1127  var panicking uint32
  1128  
  1129  // paniclk is held while printing the panic information and stack trace,
  1130  // so that two concurrent panics don't overlap their output.
  1131  var paniclk mutex
  1132  
  1133  // Unwind the stack after a deferred function calls recover
  1134  // after a panic. Then arrange to continue running as though
  1135  // the caller of the deferred function returned normally.
  1136  func recovery(gp *g) {
  1137  	// Info about defer passed in G struct.
  1138  	sp := gp.sigcode0
  1139  	pc := gp.sigcode1
  1140  
  1141  	// d's arguments need to be in the stack.
  1142  	if sp != 0 && (sp < gp.stack.lo || gp.stack.hi < sp) {
  1143  		print("recover: ", hex(sp), " not in [", hex(gp.stack.lo), ", ", hex(gp.stack.hi), "]\n")
  1144  		throw("bad recovery")
  1145  	}
  1146  
  1147  	// Make the deferproc for this d return again,
  1148  	// this time returning 1. The calling function will
  1149  	// jump to the standard return epilogue.
  1150  	gp.sched.sp = sp
  1151  	gp.sched.pc = pc
  1152  	gp.sched.lr = 0
  1153  	gp.sched.ret = 1
  1154  	gogo(&gp.sched)
  1155  }
  1156  
  1157  // fatalthrow implements an unrecoverable runtime throw. It freezes the
  1158  // system, prints stack traces starting from its caller, and terminates the
  1159  // process.
  1160  //
  1161  //go:nosplit
  1162  func fatalthrow() {
  1163  	pc := getcallerpc()
  1164  	sp := getcallersp()
  1165  	gp := getg()
  1166  	// Switch to the system stack to avoid any stack growth, which
  1167  	// may make things worse if the runtime is in a bad state.
  1168  	systemstack(func() {
  1169  		startpanic_m()
  1170  
  1171  		if dopanic_m(gp, pc, sp) {
  1172  			// crash uses a decent amount of nosplit stack and we're already
  1173  			// low on stack in throw, so crash on the system stack (unlike
  1174  			// fatalpanic).
  1175  			crash()
  1176  		}
  1177  
  1178  		exit(2)
  1179  	})
  1180  
  1181  	*(*int)(nil) = 0 // not reached
  1182  }
  1183  
  1184  // fatalpanic implements an unrecoverable panic. It is like fatalthrow, except
  1185  // that if msgs != nil, fatalpanic also prints panic messages and decrements
  1186  // runningPanicDefers once main is blocked from exiting.
  1187  //
  1188  //go:nosplit
  1189  func fatalpanic(msgs *_panic) {
  1190  	pc := getcallerpc()
  1191  	sp := getcallersp()
  1192  	gp := getg()
  1193  	var docrash bool
  1194  	// Switch to the system stack to avoid any stack growth, which
  1195  	// may make things worse if the runtime is in a bad state.
  1196  	systemstack(func() {
  1197  		if startpanic_m() && msgs != nil {
  1198  			// There were panic messages and startpanic_m
  1199  			// says it's okay to try to print them.
  1200  
  1201  			// startpanic_m set panicking, which will
  1202  			// block main from exiting, so now OK to
  1203  			// decrement runningPanicDefers.
  1204  			atomic.Xadd(&runningPanicDefers, -1)
  1205  
  1206  			printpanics(msgs)
  1207  		}
  1208  
  1209  		docrash = dopanic_m(gp, pc, sp)
  1210  	})
  1211  
  1212  	if docrash {
  1213  		// By crashing outside the above systemstack call, debuggers
  1214  		// will not be confused when generating a backtrace.
  1215  		// Function crash is marked nosplit to avoid stack growth.
  1216  		crash()
  1217  	}
  1218  
  1219  	systemstack(func() {
  1220  		exit(2)
  1221  	})
  1222  
  1223  	*(*int)(nil) = 0 // not reached
  1224  }
  1225  
  1226  // startpanic_m prepares for an unrecoverable panic.
  1227  //
  1228  // It returns true if panic messages should be printed, or false if
  1229  // the runtime is in bad shape and should just print stacks.
  1230  //
  1231  // It must not have write barriers even though the write barrier
  1232  // explicitly ignores writes once dying > 0. Write barriers still
  1233  // assume that g.m.p != nil, and this function may not have P
  1234  // in some contexts (e.g. a panic in a signal handler for a signal
  1235  // sent to an M with no P).
  1236  //
  1237  //go:nowritebarrierrec
  1238  func startpanic_m() bool {
  1239  	_g_ := getg()
  1240  	if mheap_.cachealloc.size == 0 { // very early
  1241  		print("runtime: panic before malloc heap initialized\n")
  1242  	}
  1243  	// Disallow malloc during an unrecoverable panic. A panic
  1244  	// could happen in a signal handler, or in a throw, or inside
  1245  	// malloc itself. We want to catch if an allocation ever does
  1246  	// happen (even if we're not in one of these situations).
  1247  	_g_.m.mallocing++
  1248  
  1249  	// If we're dying because of a bad lock count, set it to a
  1250  	// good lock count so we don't recursively panic below.
  1251  	if _g_.m.locks < 0 {
  1252  		_g_.m.locks = 1
  1253  	}
  1254  
  1255  	switch _g_.m.dying {
  1256  	case 0:
  1257  		// Setting dying >0 has the side-effect of disabling this G's writebuf.
  1258  		_g_.m.dying = 1
  1259  		atomic.Xadd(&panicking, 1)
  1260  		lock(&paniclk)
  1261  		if debug.schedtrace > 0 || debug.scheddetail > 0 {
  1262  			schedtrace(true)
  1263  		}
  1264  		freezetheworld()
  1265  		return true
  1266  	case 1:
  1267  		// Something failed while panicking.
  1268  		// Just print a stack trace and exit.
  1269  		_g_.m.dying = 2
  1270  		print("panic during panic\n")
  1271  		return false
  1272  	case 2:
  1273  		// This is a genuine bug in the runtime, we couldn't even
  1274  		// print the stack trace successfully.
  1275  		_g_.m.dying = 3
  1276  		print("stack trace unavailable\n")
  1277  		exit(4)
  1278  		fallthrough
  1279  	default:
  1280  		// Can't even print! Just exit.
  1281  		exit(5)
  1282  		return false // Need to return something.
  1283  	}
  1284  }
  1285  
  1286  // throwReportQuirk, if non-nil, is called by throw after dumping the stacks.
  1287  //
  1288  // TODO(austin): Remove this after Go 1.15 when we remove the
  1289  // mlockGsignal workaround.
  1290  var throwReportQuirk func()
  1291  
  1292  var didothers bool
  1293  var deadlock mutex
  1294  
  1295  func dopanic_m(gp *g, pc, sp uintptr) bool {
  1296  	if gp.sig != 0 {
  1297  		signame := signame(gp.sig)
  1298  		if signame != "" {
  1299  			print("[signal ", signame)
  1300  		} else {
  1301  			print("[signal ", hex(gp.sig))
  1302  		}
  1303  		print(" code=", hex(gp.sigcode0), " addr=", hex(gp.sigcode1), " pc=", hex(gp.sigpc), "]\n")
  1304  	}
  1305  
  1306  	level, all, docrash := gotraceback()
  1307  	_g_ := getg()
  1308  	if level > 0 {
  1309  		if gp != gp.m.curg {
  1310  			all = true
  1311  		}
  1312  		if gp != gp.m.g0 {
  1313  			print("\n")
  1314  			goroutineheader(gp)
  1315  			traceback(pc, sp, 0, gp)
  1316  		} else if level >= 2 || _g_.m.throwing > 0 {
  1317  			print("\nruntime stack:\n")
  1318  			traceback(pc, sp, 0, gp)
  1319  		}
  1320  		if !didothers && all {
  1321  			didothers = true
  1322  			tracebackothers(gp)
  1323  		}
  1324  	}
  1325  	unlock(&paniclk)
  1326  
  1327  	if atomic.Xadd(&panicking, -1) != 0 {
  1328  		// Some other m is panicking too.
  1329  		// Let it print what it needs to print.
  1330  		// Wait forever without chewing up cpu.
  1331  		// It will exit when it's done.
  1332  		lock(&deadlock)
  1333  		lock(&deadlock)
  1334  	}
  1335  
  1336  	printDebugLog()
  1337  
  1338  	if throwReportQuirk != nil {
  1339  		throwReportQuirk()
  1340  	}
  1341  
  1342  	return docrash
  1343  }
  1344  
  1345  // canpanic returns false if a signal should throw instead of
  1346  // panicking.
  1347  //
  1348  //go:nosplit
  1349  func canpanic(gp *g) bool {
  1350  	// Note that g is m->gsignal, different from gp.
  1351  	// Note also that g->m can change at preemption, so m can go stale
  1352  	// if this function ever makes a function call.
  1353  	_g_ := getg()
  1354  	_m_ := _g_.m
  1355  
  1356  	// Is it okay for gp to panic instead of crashing the program?
  1357  	// Yes, as long as it is running Go code, not runtime code,
  1358  	// and not stuck in a system call.
  1359  	if gp == nil || gp != _m_.curg {
  1360  		return false
  1361  	}
  1362  	if _m_.locks != 0 || _m_.mallocing != 0 || _m_.throwing != 0 || _m_.preemptoff != "" || _m_.dying != 0 {
  1363  		return false
  1364  	}
  1365  	status := readgstatus(gp)
  1366  	if status&^_Gscan != _Grunning || gp.syscallsp != 0 {
  1367  		return false
  1368  	}
  1369  	if GOOS == "windows" && _m_.libcallsp != 0 {
  1370  		return false
  1371  	}
  1372  	return true
  1373  }
  1374  
  1375  // shouldPushSigpanic reports whether pc should be used as sigpanic's
  1376  // return PC (pushing a frame for the call). Otherwise, it should be
  1377  // left alone so that LR is used as sigpanic's return PC, effectively
  1378  // replacing the top-most frame with sigpanic. This is used by
  1379  // preparePanic.
  1380  func shouldPushSigpanic(gp *g, pc, lr uintptr) bool {
  1381  	if pc == 0 {
  1382  		// Probably a call to a nil func. The old LR is more
  1383  		// useful in the stack trace. Not pushing the frame
  1384  		// will make the trace look like a call to sigpanic
  1385  		// instead. (Otherwise the trace will end at sigpanic
  1386  		// and we won't get to see who faulted.)
  1387  		return false
  1388  	}
  1389  	// If we don't recognize the PC as code, but we do recognize
  1390  	// the link register as code, then this assumes the panic was
  1391  	// caused by a call to non-code. In this case, we want to
  1392  	// ignore this call to make unwinding show the context.
  1393  	//
  1394  	// If we running C code, we're not going to recognize pc as a
  1395  	// Go function, so just assume it's good. Otherwise, traceback
  1396  	// may try to read a stale LR that looks like a Go code
  1397  	// pointer and wander into the woods.
  1398  	if gp.m.incgo || findfunc(pc).valid() {
  1399  		// This wasn't a bad call, so use PC as sigpanic's
  1400  		// return PC.
  1401  		return true
  1402  	}
  1403  	if findfunc(lr).valid() {
  1404  		// This was a bad call, but the LR is good, so use the
  1405  		// LR as sigpanic's return PC.
  1406  		return false
  1407  	}
  1408  	// Neither the PC or LR is good. Hopefully pushing a frame
  1409  	// will work.
  1410  	return true
  1411  }
  1412  
  1413  // isAbortPC reports whether pc is the program counter at which
  1414  // runtime.abort raises a signal.
  1415  //
  1416  // It is nosplit because it's part of the isgoexception
  1417  // implementation.
  1418  //
  1419  //go:nosplit
  1420  func isAbortPC(pc uintptr) bool {
  1421  	return pc == funcPC(abort) || ((GOARCH == "arm" || GOARCH == "arm64") && pc == funcPC(abort)+sys.PCQuantum)
  1422  }
  1423  

View as plain text