Black Lives Matter. Support the Equal Justice Initiative.

Source file src/runtime/signal_unix.go

Documentation: runtime

     1  // Copyright 2012 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  // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
     6  
     7  package runtime
     8  
     9  import (
    10  	"runtime/internal/atomic"
    11  	"unsafe"
    12  )
    13  
    14  // sigTabT is the type of an entry in the global sigtable array.
    15  // sigtable is inherently system dependent, and appears in OS-specific files,
    16  // but sigTabT is the same for all Unixy systems.
    17  // The sigtable array is indexed by a system signal number to get the flags
    18  // and printable name of each signal.
    19  type sigTabT struct {
    20  	flags int32
    21  	name  string
    22  }
    23  
    24  //go:linkname os_sigpipe os.sigpipe
    25  func os_sigpipe() {
    26  	systemstack(sigpipe)
    27  }
    28  
    29  func signame(sig uint32) string {
    30  	if sig >= uint32(len(sigtable)) {
    31  		return ""
    32  	}
    33  	return sigtable[sig].name
    34  }
    35  
    36  const (
    37  	_SIG_DFL uintptr = 0
    38  	_SIG_IGN uintptr = 1
    39  )
    40  
    41  // sigPreempt is the signal used for non-cooperative preemption.
    42  //
    43  // There's no good way to choose this signal, but there are some
    44  // heuristics:
    45  //
    46  // 1. It should be a signal that's passed-through by debuggers by
    47  // default. On Linux, this is SIGALRM, SIGURG, SIGCHLD, SIGIO,
    48  // SIGVTALRM, SIGPROF, and SIGWINCH, plus some glibc-internal signals.
    49  //
    50  // 2. It shouldn't be used internally by libc in mixed Go/C binaries
    51  // because libc may assume it's the only thing that can handle these
    52  // signals. For example SIGCANCEL or SIGSETXID.
    53  //
    54  // 3. It should be a signal that can happen spuriously without
    55  // consequences. For example, SIGALRM is a bad choice because the
    56  // signal handler can't tell if it was caused by the real process
    57  // alarm or not (arguably this means the signal is broken, but I
    58  // digress). SIGUSR1 and SIGUSR2 are also bad because those are often
    59  // used in meaningful ways by applications.
    60  //
    61  // 4. We need to deal with platforms without real-time signals (like
    62  // macOS), so those are out.
    63  //
    64  // We use SIGURG because it meets all of these criteria, is extremely
    65  // unlikely to be used by an application for its "real" meaning (both
    66  // because out-of-band data is basically unused and because SIGURG
    67  // doesn't report which socket has the condition, making it pretty
    68  // useless), and even if it is, the application has to be ready for
    69  // spurious SIGURG. SIGIO wouldn't be a bad choice either, but is more
    70  // likely to be used for real.
    71  const sigPreempt = _SIGURG
    72  
    73  // Stores the signal handlers registered before Go installed its own.
    74  // These signal handlers will be invoked in cases where Go doesn't want to
    75  // handle a particular signal (e.g., signal occurred on a non-Go thread).
    76  // See sigfwdgo for more information on when the signals are forwarded.
    77  //
    78  // This is read by the signal handler; accesses should use
    79  // atomic.Loaduintptr and atomic.Storeuintptr.
    80  var fwdSig [_NSIG]uintptr
    81  
    82  // handlingSig is indexed by signal number and is non-zero if we are
    83  // currently handling the signal. Or, to put it another way, whether
    84  // the signal handler is currently set to the Go signal handler or not.
    85  // This is uint32 rather than bool so that we can use atomic instructions.
    86  var handlingSig [_NSIG]uint32
    87  
    88  // channels for synchronizing signal mask updates with the signal mask
    89  // thread
    90  var (
    91  	disableSigChan  chan uint32
    92  	enableSigChan   chan uint32
    93  	maskUpdatedChan chan struct{}
    94  )
    95  
    96  func init() {
    97  	// _NSIG is the number of signals on this operating system.
    98  	// sigtable should describe what to do for all the possible signals.
    99  	if len(sigtable) != _NSIG {
   100  		print("runtime: len(sigtable)=", len(sigtable), " _NSIG=", _NSIG, "\n")
   101  		throw("bad sigtable len")
   102  	}
   103  }
   104  
   105  var signalsOK bool
   106  
   107  // Initialize signals.
   108  // Called by libpreinit so runtime may not be initialized.
   109  //go:nosplit
   110  //go:nowritebarrierrec
   111  func initsig(preinit bool) {
   112  	if !preinit {
   113  		// It's now OK for signal handlers to run.
   114  		signalsOK = true
   115  	}
   116  
   117  	// For c-archive/c-shared this is called by libpreinit with
   118  	// preinit == true.
   119  	if (isarchive || islibrary) && !preinit {
   120  		return
   121  	}
   122  
   123  	for i := uint32(0); i < _NSIG; i++ {
   124  		t := &sigtable[i]
   125  		if t.flags == 0 || t.flags&_SigDefault != 0 {
   126  			continue
   127  		}
   128  
   129  		// We don't need to use atomic operations here because
   130  		// there shouldn't be any other goroutines running yet.
   131  		fwdSig[i] = getsig(i)
   132  
   133  		if !sigInstallGoHandler(i) {
   134  			// Even if we are not installing a signal handler,
   135  			// set SA_ONSTACK if necessary.
   136  			if fwdSig[i] != _SIG_DFL && fwdSig[i] != _SIG_IGN {
   137  				setsigstack(i)
   138  			} else if fwdSig[i] == _SIG_IGN {
   139  				sigInitIgnored(i)
   140  			}
   141  			continue
   142  		}
   143  
   144  		handlingSig[i] = 1
   145  		setsig(i, funcPC(sighandler))
   146  	}
   147  }
   148  
   149  //go:nosplit
   150  //go:nowritebarrierrec
   151  func sigInstallGoHandler(sig uint32) bool {
   152  	// For some signals, we respect an inherited SIG_IGN handler
   153  	// rather than insist on installing our own default handler.
   154  	// Even these signals can be fetched using the os/signal package.
   155  	switch sig {
   156  	case _SIGHUP, _SIGINT:
   157  		if atomic.Loaduintptr(&fwdSig[sig]) == _SIG_IGN {
   158  			return false
   159  		}
   160  	}
   161  
   162  	t := &sigtable[sig]
   163  	if t.flags&_SigSetStack != 0 {
   164  		return false
   165  	}
   166  
   167  	// When built using c-archive or c-shared, only install signal
   168  	// handlers for synchronous signals and SIGPIPE.
   169  	if (isarchive || islibrary) && t.flags&_SigPanic == 0 && sig != _SIGPIPE {
   170  		return false
   171  	}
   172  
   173  	return true
   174  }
   175  
   176  // sigenable enables the Go signal handler to catch the signal sig.
   177  // It is only called while holding the os/signal.handlers lock,
   178  // via os/signal.enableSignal and signal_enable.
   179  func sigenable(sig uint32) {
   180  	if sig >= uint32(len(sigtable)) {
   181  		return
   182  	}
   183  
   184  	// SIGPROF is handled specially for profiling.
   185  	if sig == _SIGPROF {
   186  		return
   187  	}
   188  
   189  	t := &sigtable[sig]
   190  	if t.flags&_SigNotify != 0 {
   191  		ensureSigM()
   192  		enableSigChan <- sig
   193  		<-maskUpdatedChan
   194  		if atomic.Cas(&handlingSig[sig], 0, 1) {
   195  			atomic.Storeuintptr(&fwdSig[sig], getsig(sig))
   196  			setsig(sig, funcPC(sighandler))
   197  		}
   198  	}
   199  }
   200  
   201  // sigdisable disables the Go signal handler for the signal sig.
   202  // It is only called while holding the os/signal.handlers lock,
   203  // via os/signal.disableSignal and signal_disable.
   204  func sigdisable(sig uint32) {
   205  	if sig >= uint32(len(sigtable)) {
   206  		return
   207  	}
   208  
   209  	// SIGPROF is handled specially for profiling.
   210  	if sig == _SIGPROF {
   211  		return
   212  	}
   213  
   214  	t := &sigtable[sig]
   215  	if t.flags&_SigNotify != 0 {
   216  		ensureSigM()
   217  		disableSigChan <- sig
   218  		<-maskUpdatedChan
   219  
   220  		// If initsig does not install a signal handler for a
   221  		// signal, then to go back to the state before Notify
   222  		// we should remove the one we installed.
   223  		if !sigInstallGoHandler(sig) {
   224  			atomic.Store(&handlingSig[sig], 0)
   225  			setsig(sig, atomic.Loaduintptr(&fwdSig[sig]))
   226  		}
   227  	}
   228  }
   229  
   230  // sigignore ignores the signal sig.
   231  // It is only called while holding the os/signal.handlers lock,
   232  // via os/signal.ignoreSignal and signal_ignore.
   233  func sigignore(sig uint32) {
   234  	if sig >= uint32(len(sigtable)) {
   235  		return
   236  	}
   237  
   238  	// SIGPROF is handled specially for profiling.
   239  	if sig == _SIGPROF {
   240  		return
   241  	}
   242  
   243  	t := &sigtable[sig]
   244  	if t.flags&_SigNotify != 0 {
   245  		atomic.Store(&handlingSig[sig], 0)
   246  		setsig(sig, _SIG_IGN)
   247  	}
   248  }
   249  
   250  // clearSignalHandlers clears all signal handlers that are not ignored
   251  // back to the default. This is called by the child after a fork, so that
   252  // we can enable the signal mask for the exec without worrying about
   253  // running a signal handler in the child.
   254  //go:nosplit
   255  //go:nowritebarrierrec
   256  func clearSignalHandlers() {
   257  	for i := uint32(0); i < _NSIG; i++ {
   258  		if atomic.Load(&handlingSig[i]) != 0 {
   259  			setsig(i, _SIG_DFL)
   260  		}
   261  	}
   262  }
   263  
   264  // setProcessCPUProfiler is called when the profiling timer changes.
   265  // It is called with prof.lock held. hz is the new timer, and is 0 if
   266  // profiling is being disabled. Enable or disable the signal as
   267  // required for -buildmode=c-archive.
   268  func setProcessCPUProfiler(hz int32) {
   269  	if hz != 0 {
   270  		// Enable the Go signal handler if not enabled.
   271  		if atomic.Cas(&handlingSig[_SIGPROF], 0, 1) {
   272  			atomic.Storeuintptr(&fwdSig[_SIGPROF], getsig(_SIGPROF))
   273  			setsig(_SIGPROF, funcPC(sighandler))
   274  		}
   275  	} else {
   276  		// If the Go signal handler should be disabled by default,
   277  		// switch back to the signal handler that was installed
   278  		// when we enabled profiling. We don't try to handle the case
   279  		// of a program that changes the SIGPROF handler while Go
   280  		// profiling is enabled.
   281  		//
   282  		// If no signal handler was installed before, then start
   283  		// ignoring SIGPROF signals. We do this, rather than change
   284  		// to SIG_DFL, because there may be a pending SIGPROF
   285  		// signal that has not yet been delivered to some other thread.
   286  		// If we change to SIG_DFL here, the program will crash
   287  		// when that SIGPROF is delivered. We assume that programs
   288  		// that use profiling don't want to crash on a stray SIGPROF.
   289  		// See issue 19320.
   290  		if !sigInstallGoHandler(_SIGPROF) {
   291  			if atomic.Cas(&handlingSig[_SIGPROF], 1, 0) {
   292  				h := atomic.Loaduintptr(&fwdSig[_SIGPROF])
   293  				if h == _SIG_DFL {
   294  					h = _SIG_IGN
   295  				}
   296  				setsig(_SIGPROF, h)
   297  			}
   298  		}
   299  	}
   300  }
   301  
   302  // setThreadCPUProfiler makes any thread-specific changes required to
   303  // implement profiling at a rate of hz.
   304  func setThreadCPUProfiler(hz int32) {
   305  	var it itimerval
   306  	if hz == 0 {
   307  		setitimer(_ITIMER_PROF, &it, nil)
   308  	} else {
   309  		it.it_interval.tv_sec = 0
   310  		it.it_interval.set_usec(1000000 / hz)
   311  		it.it_value = it.it_interval
   312  		setitimer(_ITIMER_PROF, &it, nil)
   313  	}
   314  	_g_ := getg()
   315  	_g_.m.profilehz = hz
   316  }
   317  
   318  func sigpipe() {
   319  	if signal_ignored(_SIGPIPE) || sigsend(_SIGPIPE) {
   320  		return
   321  	}
   322  	dieFromSignal(_SIGPIPE)
   323  }
   324  
   325  // doSigPreempt handles a preemption signal on gp.
   326  func doSigPreempt(gp *g, ctxt *sigctxt) {
   327  	// Check if this G wants to be preempted and is safe to
   328  	// preempt.
   329  	if wantAsyncPreempt(gp) {
   330  		if ok, newpc := isAsyncSafePoint(gp, ctxt.sigpc(), ctxt.sigsp(), ctxt.siglr()); ok {
   331  			// Adjust the PC and inject a call to asyncPreempt.
   332  			ctxt.pushCall(funcPC(asyncPreempt), newpc)
   333  		}
   334  	}
   335  
   336  	// Acknowledge the preemption.
   337  	atomic.Xadd(&gp.m.preemptGen, 1)
   338  	atomic.Store(&gp.m.signalPending, 0)
   339  
   340  	if GOOS == "darwin" {
   341  		atomic.Xadd(&pendingPreemptSignals, -1)
   342  	}
   343  }
   344  
   345  const preemptMSupported = true
   346  
   347  // preemptM sends a preemption request to mp. This request may be
   348  // handled asynchronously and may be coalesced with other requests to
   349  // the M. When the request is received, if the running G or P are
   350  // marked for preemption and the goroutine is at an asynchronous
   351  // safe-point, it will preempt the goroutine. It always atomically
   352  // increments mp.preemptGen after handling a preemption request.
   353  func preemptM(mp *m) {
   354  	if GOOS == "darwin" && GOARCH == "arm64" && !iscgo {
   355  		// On darwin, we use libc calls, and cgo is required on ARM64
   356  		// so we have TLS set up to save/restore G during C calls. If cgo is
   357  		// absent, we cannot save/restore G in TLS, and if a signal is
   358  		// received during C execution we cannot get the G. Therefore don't
   359  		// send signals.
   360  		// This can only happen in the go_bootstrap program (otherwise cgo is
   361  		// required).
   362  		return
   363  	}
   364  
   365  	// On Darwin, don't try to preempt threads during exec.
   366  	// Issue #41702.
   367  	if GOOS == "darwin" {
   368  		execLock.rlock()
   369  	}
   370  
   371  	if atomic.Cas(&mp.signalPending, 0, 1) {
   372  		if GOOS == "darwin" {
   373  			atomic.Xadd(&pendingPreemptSignals, 1)
   374  		}
   375  
   376  		// If multiple threads are preempting the same M, it may send many
   377  		// signals to the same M such that it hardly make progress, causing
   378  		// live-lock problem. Apparently this could happen on darwin. See
   379  		// issue #37741.
   380  		// Only send a signal if there isn't already one pending.
   381  		signalM(mp, sigPreempt)
   382  	}
   383  
   384  	if GOOS == "darwin" {
   385  		execLock.runlock()
   386  	}
   387  }
   388  
   389  // sigFetchG fetches the value of G safely when running in a signal handler.
   390  // On some architectures, the g value may be clobbered when running in a VDSO.
   391  // See issue #32912.
   392  //
   393  //go:nosplit
   394  func sigFetchG(c *sigctxt) *g {
   395  	switch GOARCH {
   396  	case "arm", "arm64":
   397  		if !iscgo && inVDSOPage(c.sigpc()) {
   398  			// When using cgo, we save the g on TLS and load it from there
   399  			// in sigtramp. Just use that.
   400  			// Otherwise, before making a VDSO call we save the g to the
   401  			// bottom of the signal stack. Fetch from there.
   402  			// TODO: in efence mode, stack is sysAlloc'd, so this wouldn't
   403  			// work.
   404  			sp := getcallersp()
   405  			s := spanOf(sp)
   406  			if s != nil && s.state.get() == mSpanManual && s.base() < sp && sp < s.limit {
   407  				gp := *(**g)(unsafe.Pointer(s.base()))
   408  				return gp
   409  			}
   410  			return nil
   411  		}
   412  	}
   413  	return getg()
   414  }
   415  
   416  // sigtrampgo is called from the signal handler function, sigtramp,
   417  // written in assembly code.
   418  // This is called by the signal handler, and the world may be stopped.
   419  //
   420  // It must be nosplit because getg() is still the G that was running
   421  // (if any) when the signal was delivered, but it's (usually) called
   422  // on the gsignal stack. Until this switches the G to gsignal, the
   423  // stack bounds check won't work.
   424  //
   425  //go:nosplit
   426  //go:nowritebarrierrec
   427  func sigtrampgo(sig uint32, info *siginfo, ctx unsafe.Pointer) {
   428  	if sigfwdgo(sig, info, ctx) {
   429  		return
   430  	}
   431  	c := &sigctxt{info, ctx}
   432  	g := sigFetchG(c)
   433  	setg(g)
   434  	if g == nil {
   435  		if sig == _SIGPROF {
   436  			sigprofNonGoPC(c.sigpc())
   437  			return
   438  		}
   439  		if sig == sigPreempt && preemptMSupported && debug.asyncpreemptoff == 0 {
   440  			// This is probably a signal from preemptM sent
   441  			// while executing Go code but received while
   442  			// executing non-Go code.
   443  			// We got past sigfwdgo, so we know that there is
   444  			// no non-Go signal handler for sigPreempt.
   445  			// The default behavior for sigPreempt is to ignore
   446  			// the signal, so badsignal will be a no-op anyway.
   447  			if GOOS == "darwin" {
   448  				atomic.Xadd(&pendingPreemptSignals, -1)
   449  			}
   450  			return
   451  		}
   452  		c.fixsigcode(sig)
   453  		badsignal(uintptr(sig), c)
   454  		return
   455  	}
   456  
   457  	setg(g.m.gsignal)
   458  
   459  	// If some non-Go code called sigaltstack, adjust.
   460  	var gsignalStack gsignalStack
   461  	setStack := adjustSignalStack(sig, g.m, &gsignalStack)
   462  	if setStack {
   463  		g.m.gsignal.stktopsp = getcallersp()
   464  	}
   465  
   466  	if g.stackguard0 == stackFork {
   467  		signalDuringFork(sig)
   468  	}
   469  
   470  	c.fixsigcode(sig)
   471  	sighandler(sig, info, ctx, g)
   472  	setg(g)
   473  	if setStack {
   474  		restoreGsignalStack(&gsignalStack)
   475  	}
   476  }
   477  
   478  // adjustSignalStack adjusts the current stack guard based on the
   479  // stack pointer that is actually in use while handling a signal.
   480  // We do this in case some non-Go code called sigaltstack.
   481  // This reports whether the stack was adjusted, and if so stores the old
   482  // signal stack in *gsigstack.
   483  //go:nosplit
   484  func adjustSignalStack(sig uint32, mp *m, gsigStack *gsignalStack) bool {
   485  	sp := uintptr(unsafe.Pointer(&sig))
   486  	if sp >= mp.gsignal.stack.lo && sp < mp.gsignal.stack.hi {
   487  		return false
   488  	}
   489  
   490  	if sp >= mp.g0.stack.lo && sp < mp.g0.stack.hi {
   491  		// The signal was delivered on the g0 stack.
   492  		// This can happen when linked with C code
   493  		// using the thread sanitizer, which collects
   494  		// signals then delivers them itself by calling
   495  		// the signal handler directly when C code,
   496  		// including C code called via cgo, calls a
   497  		// TSAN-intercepted function such as malloc.
   498  		st := stackt{ss_size: mp.g0.stack.hi - mp.g0.stack.lo}
   499  		setSignalstackSP(&st, mp.g0.stack.lo)
   500  		setGsignalStack(&st, gsigStack)
   501  		return true
   502  	}
   503  
   504  	var st stackt
   505  	sigaltstack(nil, &st)
   506  	if st.ss_flags&_SS_DISABLE != 0 {
   507  		setg(nil)
   508  		needm(0)
   509  		noSignalStack(sig)
   510  		dropm()
   511  	}
   512  	stsp := uintptr(unsafe.Pointer(st.ss_sp))
   513  	if sp < stsp || sp >= stsp+st.ss_size {
   514  		setg(nil)
   515  		needm(0)
   516  		sigNotOnStack(sig)
   517  		dropm()
   518  	}
   519  	setGsignalStack(&st, gsigStack)
   520  	return true
   521  }
   522  
   523  // crashing is the number of m's we have waited for when implementing
   524  // GOTRACEBACK=crash when a signal is received.
   525  var crashing int32
   526  
   527  // testSigtrap and testSigusr1 are used by the runtime tests. If
   528  // non-nil, it is called on SIGTRAP/SIGUSR1. If it returns true, the
   529  // normal behavior on this signal is suppressed.
   530  var testSigtrap func(info *siginfo, ctxt *sigctxt, gp *g) bool
   531  var testSigusr1 func(gp *g) bool
   532  
   533  // sighandler is invoked when a signal occurs. The global g will be
   534  // set to a gsignal goroutine and we will be running on the alternate
   535  // signal stack. The parameter g will be the value of the global g
   536  // when the signal occurred. The sig, info, and ctxt parameters are
   537  // from the system signal handler: they are the parameters passed when
   538  // the SA is passed to the sigaction system call.
   539  //
   540  // The garbage collector may have stopped the world, so write barriers
   541  // are not allowed.
   542  //
   543  //go:nowritebarrierrec
   544  func sighandler(sig uint32, info *siginfo, ctxt unsafe.Pointer, gp *g) {
   545  	_g_ := getg()
   546  	c := &sigctxt{info, ctxt}
   547  
   548  	if sig == _SIGPROF {
   549  		sigprof(c.sigpc(), c.sigsp(), c.siglr(), gp, _g_.m)
   550  		return
   551  	}
   552  
   553  	if sig == _SIGTRAP && testSigtrap != nil && testSigtrap(info, (*sigctxt)(noescape(unsafe.Pointer(c))), gp) {
   554  		return
   555  	}
   556  
   557  	if sig == _SIGUSR1 && testSigusr1 != nil && testSigusr1(gp) {
   558  		return
   559  	}
   560  
   561  	if sig == sigPreempt && debug.asyncpreemptoff == 0 {
   562  		// Might be a preemption signal.
   563  		doSigPreempt(gp, c)
   564  		// Even if this was definitely a preemption signal, it
   565  		// may have been coalesced with another signal, so we
   566  		// still let it through to the application.
   567  	}
   568  
   569  	flags := int32(_SigThrow)
   570  	if sig < uint32(len(sigtable)) {
   571  		flags = sigtable[sig].flags
   572  	}
   573  	if c.sigcode() != _SI_USER && flags&_SigPanic != 0 && gp.throwsplit {
   574  		// We can't safely sigpanic because it may grow the
   575  		// stack. Abort in the signal handler instead.
   576  		flags = _SigThrow
   577  	}
   578  	if isAbortPC(c.sigpc()) {
   579  		// On many architectures, the abort function just
   580  		// causes a memory fault. Don't turn that into a panic.
   581  		flags = _SigThrow
   582  	}
   583  	if c.sigcode() != _SI_USER && flags&_SigPanic != 0 {
   584  		// The signal is going to cause a panic.
   585  		// Arrange the stack so that it looks like the point
   586  		// where the signal occurred made a call to the
   587  		// function sigpanic. Then set the PC to sigpanic.
   588  
   589  		// Have to pass arguments out of band since
   590  		// augmenting the stack frame would break
   591  		// the unwinding code.
   592  		gp.sig = sig
   593  		gp.sigcode0 = uintptr(c.sigcode())
   594  		gp.sigcode1 = uintptr(c.fault())
   595  		gp.sigpc = c.sigpc()
   596  
   597  		c.preparePanic(sig, gp)
   598  		return
   599  	}
   600  
   601  	if c.sigcode() == _SI_USER || flags&_SigNotify != 0 {
   602  		if sigsend(sig) {
   603  			return
   604  		}
   605  	}
   606  
   607  	if c.sigcode() == _SI_USER && signal_ignored(sig) {
   608  		return
   609  	}
   610  
   611  	if flags&_SigKill != 0 {
   612  		dieFromSignal(sig)
   613  	}
   614  
   615  	// _SigThrow means that we should exit now.
   616  	// If we get here with _SigPanic, it means that the signal
   617  	// was sent to us by a program (c.sigcode() == _SI_USER);
   618  	// in that case, if we didn't handle it in sigsend, we exit now.
   619  	if flags&(_SigThrow|_SigPanic) == 0 {
   620  		return
   621  	}
   622  
   623  	_g_.m.throwing = 1
   624  	_g_.m.caughtsig.set(gp)
   625  
   626  	if crashing == 0 {
   627  		startpanic_m()
   628  	}
   629  
   630  	if sig < uint32(len(sigtable)) {
   631  		print(sigtable[sig].name, "\n")
   632  	} else {
   633  		print("Signal ", sig, "\n")
   634  	}
   635  
   636  	print("PC=", hex(c.sigpc()), " m=", _g_.m.id, " sigcode=", c.sigcode(), "\n")
   637  	if _g_.m.lockedg != 0 && _g_.m.ncgo > 0 && gp == _g_.m.g0 {
   638  		print("signal arrived during cgo execution\n")
   639  		gp = _g_.m.lockedg.ptr()
   640  	}
   641  	if sig == _SIGILL {
   642  		// It would be nice to know how long the instruction is.
   643  		// Unfortunately, that's complicated to do in general (mostly for x86
   644  		// and s930x, but other archs have non-standard instruction lengths also).
   645  		// Opt to print 16 bytes, which covers most instructions.
   646  		const maxN = 16
   647  		n := uintptr(maxN)
   648  		// We have to be careful, though. If we're near the end of
   649  		// a page and the following page isn't mapped, we could
   650  		// segfault. So make sure we don't straddle a page (even though
   651  		// that could lead to printing an incomplete instruction).
   652  		// We're assuming here we can read at least the page containing the PC.
   653  		// I suppose it is possible that the page is mapped executable but not readable?
   654  		pc := c.sigpc()
   655  		if n > physPageSize-pc%physPageSize {
   656  			n = physPageSize - pc%physPageSize
   657  		}
   658  		print("instruction bytes:")
   659  		b := (*[maxN]byte)(unsafe.Pointer(pc))
   660  		for i := uintptr(0); i < n; i++ {
   661  			print(" ", hex(b[i]))
   662  		}
   663  		println()
   664  	}
   665  	print("\n")
   666  
   667  	level, _, docrash := gotraceback()
   668  	if level > 0 {
   669  		goroutineheader(gp)
   670  		tracebacktrap(c.sigpc(), c.sigsp(), c.siglr(), gp)
   671  		if crashing > 0 && gp != _g_.m.curg && _g_.m.curg != nil && readgstatus(_g_.m.curg)&^_Gscan == _Grunning {
   672  			// tracebackothers on original m skipped this one; trace it now.
   673  			goroutineheader(_g_.m.curg)
   674  			traceback(^uintptr(0), ^uintptr(0), 0, _g_.m.curg)
   675  		} else if crashing == 0 {
   676  			tracebackothers(gp)
   677  			print("\n")
   678  		}
   679  		dumpregs(c)
   680  	}
   681  
   682  	if docrash {
   683  		crashing++
   684  		if crashing < mcount()-int32(extraMCount) {
   685  			// There are other m's that need to dump their stacks.
   686  			// Relay SIGQUIT to the next m by sending it to the current process.
   687  			// All m's that have already received SIGQUIT have signal masks blocking
   688  			// receipt of any signals, so the SIGQUIT will go to an m that hasn't seen it yet.
   689  			// When the last m receives the SIGQUIT, it will fall through to the call to
   690  			// crash below. Just in case the relaying gets botched, each m involved in
   691  			// the relay sleeps for 5 seconds and then does the crash/exit itself.
   692  			// In expected operation, the last m has received the SIGQUIT and run
   693  			// crash/exit and the process is gone, all long before any of the
   694  			// 5-second sleeps have finished.
   695  			print("\n-----\n\n")
   696  			raiseproc(_SIGQUIT)
   697  			usleep(5 * 1000 * 1000)
   698  		}
   699  		crash()
   700  	}
   701  
   702  	printDebugLog()
   703  
   704  	exit(2)
   705  }
   706  
   707  // sigpanic turns a synchronous signal into a run-time panic.
   708  // If the signal handler sees a synchronous panic, it arranges the
   709  // stack to look like the function where the signal occurred called
   710  // sigpanic, sets the signal's PC value to sigpanic, and returns from
   711  // the signal handler. The effect is that the program will act as
   712  // though the function that got the signal simply called sigpanic
   713  // instead.
   714  //
   715  // This must NOT be nosplit because the linker doesn't know where
   716  // sigpanic calls can be injected.
   717  //
   718  // The signal handler must not inject a call to sigpanic if
   719  // getg().throwsplit, since sigpanic may need to grow the stack.
   720  //
   721  // This is exported via linkname to assembly in runtime/cgo.
   722  //go:linkname sigpanic
   723  func sigpanic() {
   724  	g := getg()
   725  	if !canpanic(g) {
   726  		throw("unexpected signal during runtime execution")
   727  	}
   728  
   729  	switch g.sig {
   730  	case _SIGBUS:
   731  		if g.sigcode0 == _BUS_ADRERR && g.sigcode1 < 0x1000 {
   732  			panicmem()
   733  		}
   734  		// Support runtime/debug.SetPanicOnFault.
   735  		if g.paniconfault {
   736  			panicmem()
   737  		}
   738  		print("unexpected fault address ", hex(g.sigcode1), "\n")
   739  		throw("fault")
   740  	case _SIGSEGV:
   741  		if (g.sigcode0 == 0 || g.sigcode0 == _SEGV_MAPERR || g.sigcode0 == _SEGV_ACCERR) && g.sigcode1 < 0x1000 {
   742  			panicmem()
   743  		}
   744  		// Support runtime/debug.SetPanicOnFault.
   745  		if g.paniconfault {
   746  			panicmem()
   747  		}
   748  		print("unexpected fault address ", hex(g.sigcode1), "\n")
   749  		throw("fault")
   750  	case _SIGFPE:
   751  		switch g.sigcode0 {
   752  		case _FPE_INTDIV:
   753  			panicdivide()
   754  		case _FPE_INTOVF:
   755  			panicoverflow()
   756  		}
   757  		panicfloat()
   758  	}
   759  
   760  	if g.sig >= uint32(len(sigtable)) {
   761  		// can't happen: we looked up g.sig in sigtable to decide to call sigpanic
   762  		throw("unexpected signal value")
   763  	}
   764  	panic(errorString(sigtable[g.sig].name))
   765  }
   766  
   767  // dieFromSignal kills the program with a signal.
   768  // This provides the expected exit status for the shell.
   769  // This is only called with fatal signals expected to kill the process.
   770  //go:nosplit
   771  //go:nowritebarrierrec
   772  func dieFromSignal(sig uint32) {
   773  	unblocksig(sig)
   774  	// Mark the signal as unhandled to ensure it is forwarded.
   775  	atomic.Store(&handlingSig[sig], 0)
   776  	raise(sig)
   777  
   778  	// That should have killed us. On some systems, though, raise
   779  	// sends the signal to the whole process rather than to just
   780  	// the current thread, which means that the signal may not yet
   781  	// have been delivered. Give other threads a chance to run and
   782  	// pick up the signal.
   783  	osyield()
   784  	osyield()
   785  	osyield()
   786  
   787  	// If that didn't work, try _SIG_DFL.
   788  	setsig(sig, _SIG_DFL)
   789  	raise(sig)
   790  
   791  	osyield()
   792  	osyield()
   793  	osyield()
   794  
   795  	// If we are still somehow running, just exit with the wrong status.
   796  	exit(2)
   797  }
   798  
   799  // raisebadsignal is called when a signal is received on a non-Go
   800  // thread, and the Go program does not want to handle it (that is, the
   801  // program has not called os/signal.Notify for the signal).
   802  func raisebadsignal(sig uint32, c *sigctxt) {
   803  	if sig == _SIGPROF {
   804  		// Ignore profiling signals that arrive on non-Go threads.
   805  		return
   806  	}
   807  
   808  	var handler uintptr
   809  	if sig >= _NSIG {
   810  		handler = _SIG_DFL
   811  	} else {
   812  		handler = atomic.Loaduintptr(&fwdSig[sig])
   813  	}
   814  
   815  	// Reset the signal handler and raise the signal.
   816  	// We are currently running inside a signal handler, so the
   817  	// signal is blocked. We need to unblock it before raising the
   818  	// signal, or the signal we raise will be ignored until we return
   819  	// from the signal handler. We know that the signal was unblocked
   820  	// before entering the handler, or else we would not have received
   821  	// it. That means that we don't have to worry about blocking it
   822  	// again.
   823  	unblocksig(sig)
   824  	setsig(sig, handler)
   825  
   826  	// If we're linked into a non-Go program we want to try to
   827  	// avoid modifying the original context in which the signal
   828  	// was raised. If the handler is the default, we know it
   829  	// is non-recoverable, so we don't have to worry about
   830  	// re-installing sighandler. At this point we can just
   831  	// return and the signal will be re-raised and caught by
   832  	// the default handler with the correct context.
   833  	//
   834  	// On FreeBSD, the libthr sigaction code prevents
   835  	// this from working so we fall through to raise.
   836  	if GOOS != "freebsd" && (isarchive || islibrary) && handler == _SIG_DFL && c.sigcode() != _SI_USER {
   837  		return
   838  	}
   839  
   840  	raise(sig)
   841  
   842  	// Give the signal a chance to be delivered.
   843  	// In almost all real cases the program is about to crash,
   844  	// so sleeping here is not a waste of time.
   845  	usleep(1000)
   846  
   847  	// If the signal didn't cause the program to exit, restore the
   848  	// Go signal handler and carry on.
   849  	//
   850  	// We may receive another instance of the signal before we
   851  	// restore the Go handler, but that is not so bad: we know
   852  	// that the Go program has been ignoring the signal.
   853  	setsig(sig, funcPC(sighandler))
   854  }
   855  
   856  //go:nosplit
   857  func crash() {
   858  	// OS X core dumps are linear dumps of the mapped memory,
   859  	// from the first virtual byte to the last, with zeros in the gaps.
   860  	// Because of the way we arrange the address space on 64-bit systems,
   861  	// this means the OS X core file will be >128 GB and even on a zippy
   862  	// workstation can take OS X well over an hour to write (uninterruptible).
   863  	// Save users from making that mistake.
   864  	if GOOS == "darwin" && GOARCH == "amd64" {
   865  		return
   866  	}
   867  
   868  	dieFromSignal(_SIGABRT)
   869  }
   870  
   871  // ensureSigM starts one global, sleeping thread to make sure at least one thread
   872  // is available to catch signals enabled for os/signal.
   873  func ensureSigM() {
   874  	if maskUpdatedChan != nil {
   875  		return
   876  	}
   877  	maskUpdatedChan = make(chan struct{})
   878  	disableSigChan = make(chan uint32)
   879  	enableSigChan = make(chan uint32)
   880  	go func() {
   881  		// Signal masks are per-thread, so make sure this goroutine stays on one
   882  		// thread.
   883  		LockOSThread()
   884  		defer UnlockOSThread()
   885  		// The sigBlocked mask contains the signals not active for os/signal,
   886  		// initially all signals except the essential. When signal.Notify()/Stop is called,
   887  		// sigenable/sigdisable in turn notify this thread to update its signal
   888  		// mask accordingly.
   889  		sigBlocked := sigset_all
   890  		for i := range sigtable {
   891  			if !blockableSig(uint32(i)) {
   892  				sigdelset(&sigBlocked, i)
   893  			}
   894  		}
   895  		sigprocmask(_SIG_SETMASK, &sigBlocked, nil)
   896  		for {
   897  			select {
   898  			case sig := <-enableSigChan:
   899  				if sig > 0 {
   900  					sigdelset(&sigBlocked, int(sig))
   901  				}
   902  			case sig := <-disableSigChan:
   903  				if sig > 0 && blockableSig(sig) {
   904  					sigaddset(&sigBlocked, int(sig))
   905  				}
   906  			}
   907  			sigprocmask(_SIG_SETMASK, &sigBlocked, nil)
   908  			maskUpdatedChan <- struct{}{}
   909  		}
   910  	}()
   911  }
   912  
   913  // This is called when we receive a signal when there is no signal stack.
   914  // This can only happen if non-Go code calls sigaltstack to disable the
   915  // signal stack.
   916  func noSignalStack(sig uint32) {
   917  	println("signal", sig, "received on thread with no signal stack")
   918  	throw("non-Go code disabled sigaltstack")
   919  }
   920  
   921  // This is called if we receive a signal when there is a signal stack
   922  // but we are not on it. This can only happen if non-Go code called
   923  // sigaction without setting the SS_ONSTACK flag.
   924  func sigNotOnStack(sig uint32) {
   925  	println("signal", sig, "received but handler not on signal stack")
   926  	throw("non-Go code set up signal handler without SA_ONSTACK flag")
   927  }
   928  
   929  // signalDuringFork is called if we receive a signal while doing a fork.
   930  // We do not want signals at that time, as a signal sent to the process
   931  // group may be delivered to the child process, causing confusion.
   932  // This should never be called, because we block signals across the fork;
   933  // this function is just a safety check. See issue 18600 for background.
   934  func signalDuringFork(sig uint32) {
   935  	println("signal", sig, "received during fork")
   936  	throw("signal received during fork")
   937  }
   938  
   939  var badginsignalMsg = "fatal: bad g in signal handler\n"
   940  
   941  // This runs on a foreign stack, without an m or a g. No stack split.
   942  //go:nosplit
   943  //go:norace
   944  //go:nowritebarrierrec
   945  func badsignal(sig uintptr, c *sigctxt) {
   946  	if !iscgo && !cgoHasExtraM {
   947  		// There is no extra M. needm will not be able to grab
   948  		// an M. Instead of hanging, just crash.
   949  		// Cannot call split-stack function as there is no G.
   950  		s := stringStructOf(&badginsignalMsg)
   951  		write(2, s.str, int32(s.len))
   952  		exit(2)
   953  		*(*uintptr)(unsafe.Pointer(uintptr(123))) = 2
   954  	}
   955  	needm(0)
   956  	if !sigsend(uint32(sig)) {
   957  		// A foreign thread received the signal sig, and the
   958  		// Go code does not want to handle it.
   959  		raisebadsignal(uint32(sig), c)
   960  	}
   961  	dropm()
   962  }
   963  
   964  //go:noescape
   965  func sigfwd(fn uintptr, sig uint32, info *siginfo, ctx unsafe.Pointer)
   966  
   967  // Determines if the signal should be handled by Go and if not, forwards the
   968  // signal to the handler that was installed before Go's. Returns whether the
   969  // signal was forwarded.
   970  // This is called by the signal handler, and the world may be stopped.
   971  //go:nosplit
   972  //go:nowritebarrierrec
   973  func sigfwdgo(sig uint32, info *siginfo, ctx unsafe.Pointer) bool {
   974  	if sig >= uint32(len(sigtable)) {
   975  		return false
   976  	}
   977  	fwdFn := atomic.Loaduintptr(&fwdSig[sig])
   978  	flags := sigtable[sig].flags
   979  
   980  	// If we aren't handling the signal, forward it.
   981  	if atomic.Load(&handlingSig[sig]) == 0 || !signalsOK {
   982  		// If the signal is ignored, doing nothing is the same as forwarding.
   983  		if fwdFn == _SIG_IGN || (fwdFn == _SIG_DFL && flags&_SigIgn != 0) {
   984  			return true
   985  		}
   986  		// We are not handling the signal and there is no other handler to forward to.
   987  		// Crash with the default behavior.
   988  		if fwdFn == _SIG_DFL {
   989  			setsig(sig, _SIG_DFL)
   990  			dieFromSignal(sig)
   991  			return false
   992  		}
   993  
   994  		sigfwd(fwdFn, sig, info, ctx)
   995  		return true
   996  	}
   997  
   998  	// This function and its caller sigtrampgo assumes SIGPIPE is delivered on the
   999  	// originating thread. This property does not hold on macOS (golang.org/issue/33384),
  1000  	// so we have no choice but to ignore SIGPIPE.
  1001  	if GOOS == "darwin" && sig == _SIGPIPE {
  1002  		return true
  1003  	}
  1004  
  1005  	// If there is no handler to forward to, no need to forward.
  1006  	if fwdFn == _SIG_DFL {
  1007  		return false
  1008  	}
  1009  
  1010  	c := &sigctxt{info, ctx}
  1011  	// Only forward synchronous signals and SIGPIPE.
  1012  	// Unfortunately, user generated SIGPIPEs will also be forwarded, because si_code
  1013  	// is set to _SI_USER even for a SIGPIPE raised from a write to a closed socket
  1014  	// or pipe.
  1015  	if (c.sigcode() == _SI_USER || flags&_SigPanic == 0) && sig != _SIGPIPE {
  1016  		return false
  1017  	}
  1018  	// Determine if the signal occurred inside Go code. We test that:
  1019  	//   (1) we weren't in VDSO page,
  1020  	//   (2) we were in a goroutine (i.e., m.curg != nil), and
  1021  	//   (3) we weren't in CGO.
  1022  	g := sigFetchG(c)
  1023  	if g != nil && g.m != nil && g.m.curg != nil && !g.m.incgo {
  1024  		return false
  1025  	}
  1026  
  1027  	// Signal not handled by Go, forward it.
  1028  	if fwdFn != _SIG_IGN {
  1029  		sigfwd(fwdFn, sig, info, ctx)
  1030  	}
  1031  
  1032  	return true
  1033  }
  1034  
  1035  // msigsave saves the current thread's signal mask into mp.sigmask.
  1036  // This is used to preserve the non-Go signal mask when a non-Go
  1037  // thread calls a Go function.
  1038  // This is nosplit and nowritebarrierrec because it is called by needm
  1039  // which may be called on a non-Go thread with no g available.
  1040  //go:nosplit
  1041  //go:nowritebarrierrec
  1042  func msigsave(mp *m) {
  1043  	sigprocmask(_SIG_SETMASK, nil, &mp.sigmask)
  1044  }
  1045  
  1046  // msigrestore sets the current thread's signal mask to sigmask.
  1047  // This is used to restore the non-Go signal mask when a non-Go thread
  1048  // calls a Go function.
  1049  // This is nosplit and nowritebarrierrec because it is called by dropm
  1050  // after g has been cleared.
  1051  //go:nosplit
  1052  //go:nowritebarrierrec
  1053  func msigrestore(sigmask sigset) {
  1054  	sigprocmask(_SIG_SETMASK, &sigmask, nil)
  1055  }
  1056  
  1057  // sigblock blocks all signals in the current thread's signal mask.
  1058  // This is used to block signals while setting up and tearing down g
  1059  // when a non-Go thread calls a Go function.
  1060  // The OS-specific code is expected to define sigset_all.
  1061  // This is nosplit and nowritebarrierrec because it is called by needm
  1062  // which may be called on a non-Go thread with no g available.
  1063  //go:nosplit
  1064  //go:nowritebarrierrec
  1065  func sigblock() {
  1066  	sigprocmask(_SIG_SETMASK, &sigset_all, nil)
  1067  }
  1068  
  1069  // unblocksig removes sig from the current thread's signal mask.
  1070  // This is nosplit and nowritebarrierrec because it is called from
  1071  // dieFromSignal, which can be called by sigfwdgo while running in the
  1072  // signal handler, on the signal stack, with no g available.
  1073  //go:nosplit
  1074  //go:nowritebarrierrec
  1075  func unblocksig(sig uint32) {
  1076  	var set sigset
  1077  	sigaddset(&set, int(sig))
  1078  	sigprocmask(_SIG_UNBLOCK, &set, nil)
  1079  }
  1080  
  1081  // minitSignals is called when initializing a new m to set the
  1082  // thread's alternate signal stack and signal mask.
  1083  func minitSignals() {
  1084  	minitSignalStack()
  1085  	minitSignalMask()
  1086  }
  1087  
  1088  // minitSignalStack is called when initializing a new m to set the
  1089  // alternate signal stack. If the alternate signal stack is not set
  1090  // for the thread (the normal case) then set the alternate signal
  1091  // stack to the gsignal stack. If the alternate signal stack is set
  1092  // for the thread (the case when a non-Go thread sets the alternate
  1093  // signal stack and then calls a Go function) then set the gsignal
  1094  // stack to the alternate signal stack. We also set the alternate
  1095  // signal stack to the gsignal stack if cgo is not used (regardless
  1096  // of whether it is already set). Record which choice was made in
  1097  // newSigstack, so that it can be undone in unminit.
  1098  func minitSignalStack() {
  1099  	_g_ := getg()
  1100  	var st stackt
  1101  	sigaltstack(nil, &st)
  1102  	if st.ss_flags&_SS_DISABLE != 0 || !iscgo {
  1103  		signalstack(&_g_.m.gsignal.stack)
  1104  		_g_.m.newSigstack = true
  1105  	} else {
  1106  		setGsignalStack(&st, &_g_.m.goSigStack)
  1107  		_g_.m.newSigstack = false
  1108  	}
  1109  }
  1110  
  1111  // minitSignalMask is called when initializing a new m to set the
  1112  // thread's signal mask. When this is called all signals have been
  1113  // blocked for the thread.  This starts with m.sigmask, which was set
  1114  // either from initSigmask for a newly created thread or by calling
  1115  // msigsave if this is a non-Go thread calling a Go function. It
  1116  // removes all essential signals from the mask, thus causing those
  1117  // signals to not be blocked. Then it sets the thread's signal mask.
  1118  // After this is called the thread can receive signals.
  1119  func minitSignalMask() {
  1120  	nmask := getg().m.sigmask
  1121  	for i := range sigtable {
  1122  		if !blockableSig(uint32(i)) {
  1123  			sigdelset(&nmask, i)
  1124  		}
  1125  	}
  1126  	sigprocmask(_SIG_SETMASK, &nmask, nil)
  1127  }
  1128  
  1129  // unminitSignals is called from dropm, via unminit, to undo the
  1130  // effect of calling minit on a non-Go thread.
  1131  //go:nosplit
  1132  func unminitSignals() {
  1133  	if getg().m.newSigstack {
  1134  		st := stackt{ss_flags: _SS_DISABLE}
  1135  		sigaltstack(&st, nil)
  1136  	} else {
  1137  		// We got the signal stack from someone else. Restore
  1138  		// the Go-allocated stack in case this M gets reused
  1139  		// for another thread (e.g., it's an extram). Also, on
  1140  		// Android, libc allocates a signal stack for all
  1141  		// threads, so it's important to restore the Go stack
  1142  		// even on Go-created threads so we can free it.
  1143  		restoreGsignalStack(&getg().m.goSigStack)
  1144  	}
  1145  }
  1146  
  1147  // blockableSig reports whether sig may be blocked by the signal mask.
  1148  // We never want to block the signals marked _SigUnblock;
  1149  // these are the synchronous signals that turn into a Go panic.
  1150  // In a Go program--not a c-archive/c-shared--we never want to block
  1151  // the signals marked _SigKill or _SigThrow, as otherwise it's possible
  1152  // for all running threads to block them and delay their delivery until
  1153  // we start a new thread. When linked into a C program we let the C code
  1154  // decide on the disposition of those signals.
  1155  func blockableSig(sig uint32) bool {
  1156  	flags := sigtable[sig].flags
  1157  	if flags&_SigUnblock != 0 {
  1158  		return false
  1159  	}
  1160  	if isarchive || islibrary {
  1161  		return true
  1162  	}
  1163  	return flags&(_SigKill|_SigThrow) == 0
  1164  }
  1165  
  1166  // gsignalStack saves the fields of the gsignal stack changed by
  1167  // setGsignalStack.
  1168  type gsignalStack struct {
  1169  	stack       stack
  1170  	stackguard0 uintptr
  1171  	stackguard1 uintptr
  1172  	stktopsp    uintptr
  1173  }
  1174  
  1175  // setGsignalStack sets the gsignal stack of the current m to an
  1176  // alternate signal stack returned from the sigaltstack system call.
  1177  // It saves the old values in *old for use by restoreGsignalStack.
  1178  // This is used when handling a signal if non-Go code has set the
  1179  // alternate signal stack.
  1180  //go:nosplit
  1181  //go:nowritebarrierrec
  1182  func setGsignalStack(st *stackt, old *gsignalStack) {
  1183  	g := getg()
  1184  	if old != nil {
  1185  		old.stack = g.m.gsignal.stack
  1186  		old.stackguard0 = g.m.gsignal.stackguard0
  1187  		old.stackguard1 = g.m.gsignal.stackguard1
  1188  		old.stktopsp = g.m.gsignal.stktopsp
  1189  	}
  1190  	stsp := uintptr(unsafe.Pointer(st.ss_sp))
  1191  	g.m.gsignal.stack.lo = stsp
  1192  	g.m.gsignal.stack.hi = stsp + st.ss_size
  1193  	g.m.gsignal.stackguard0 = stsp + _StackGuard
  1194  	g.m.gsignal.stackguard1 = stsp + _StackGuard
  1195  }
  1196  
  1197  // restoreGsignalStack restores the gsignal stack to the value it had
  1198  // before entering the signal handler.
  1199  //go:nosplit
  1200  //go:nowritebarrierrec
  1201  func restoreGsignalStack(st *gsignalStack) {
  1202  	gp := getg().m.gsignal
  1203  	gp.stack = st.stack
  1204  	gp.stackguard0 = st.stackguard0
  1205  	gp.stackguard1 = st.stackguard1
  1206  	gp.stktopsp = st.stktopsp
  1207  }
  1208  
  1209  // signalstack sets the current thread's alternate signal stack to s.
  1210  //go:nosplit
  1211  func signalstack(s *stack) {
  1212  	st := stackt{ss_size: s.hi - s.lo}
  1213  	setSignalstackSP(&st, s.lo)
  1214  	sigaltstack(&st, nil)
  1215  }
  1216  
  1217  // setsigsegv is used on darwin/arm64 to fake a segmentation fault.
  1218  //
  1219  // This is exported via linkname to assembly in runtime/cgo.
  1220  //
  1221  //go:nosplit
  1222  //go:linkname setsigsegv
  1223  func setsigsegv(pc uintptr) {
  1224  	g := getg()
  1225  	g.sig = _SIGSEGV
  1226  	g.sigpc = pc
  1227  	g.sigcode0 = _SEGV_MAPERR
  1228  	g.sigcode1 = 0 // TODO: emulate si_addr
  1229  }
  1230  

View as plain text