Home | History | Annotate | Download | only in 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 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 // Stores the signal handlers registered before Go installed its own.
     42 // These signal handlers will be invoked in cases where Go doesn't want to
     43 // handle a particular signal (e.g., signal occurred on a non-Go thread).
     44 // See sigfwdgo for more information on when the signals are forwarded.
     45 //
     46 // This is read by the signal handler; accesses should use
     47 // atomic.Loaduintptr and atomic.Storeuintptr.
     48 var fwdSig [_NSIG]uintptr
     49 
     50 // handlingSig is indexed by signal number and is non-zero if we are
     51 // currently handling the signal. Or, to put it another way, whether
     52 // the signal handler is currently set to the Go signal handler or not.
     53 // This is uint32 rather than bool so that we can use atomic instructions.
     54 var handlingSig [_NSIG]uint32
     55 
     56 // channels for synchronizing signal mask updates with the signal mask
     57 // thread
     58 var (
     59 	disableSigChan  chan uint32
     60 	enableSigChan   chan uint32
     61 	maskUpdatedChan chan struct{}
     62 )
     63 
     64 func init() {
     65 	// _NSIG is the number of signals on this operating system.
     66 	// sigtable should describe what to do for all the possible signals.
     67 	if len(sigtable) != _NSIG {
     68 		print("runtime: len(sigtable)=", len(sigtable), " _NSIG=", _NSIG, "\n")
     69 		throw("bad sigtable len")
     70 	}
     71 }
     72 
     73 var signalsOK bool
     74 
     75 // Initialize signals.
     76 // Called by libpreinit so runtime may not be initialized.
     77 //go:nosplit
     78 //go:nowritebarrierrec
     79 func initsig(preinit bool) {
     80 	if !preinit {
     81 		// It's now OK for signal handlers to run.
     82 		signalsOK = true
     83 	}
     84 
     85 	// For c-archive/c-shared this is called by libpreinit with
     86 	// preinit == true.
     87 	if (isarchive || islibrary) && !preinit {
     88 		return
     89 	}
     90 
     91 	for i := uint32(0); i < _NSIG; i++ {
     92 		t := &sigtable[i]
     93 		if t.flags == 0 || t.flags&_SigDefault != 0 {
     94 			continue
     95 		}
     96 
     97 		// We don't need to use atomic operations here because
     98 		// there shouldn't be any other goroutines running yet.
     99 		fwdSig[i] = getsig(i)
    100 
    101 		if !sigInstallGoHandler(i) {
    102 			// Even if we are not installing a signal handler,
    103 			// set SA_ONSTACK if necessary.
    104 			if fwdSig[i] != _SIG_DFL && fwdSig[i] != _SIG_IGN {
    105 				setsigstack(i)
    106 			}
    107 			continue
    108 		}
    109 
    110 		handlingSig[i] = 1
    111 		setsig(i, funcPC(sighandler))
    112 	}
    113 }
    114 
    115 //go:nosplit
    116 //go:nowritebarrierrec
    117 func sigInstallGoHandler(sig uint32) bool {
    118 	// For some signals, we respect an inherited SIG_IGN handler
    119 	// rather than insist on installing our own default handler.
    120 	// Even these signals can be fetched using the os/signal package.
    121 	switch sig {
    122 	case _SIGHUP, _SIGINT:
    123 		if atomic.Loaduintptr(&fwdSig[sig]) == _SIG_IGN {
    124 			return false
    125 		}
    126 	}
    127 
    128 	t := &sigtable[sig]
    129 	if t.flags&_SigSetStack != 0 {
    130 		return false
    131 	}
    132 
    133 	// When built using c-archive or c-shared, only install signal
    134 	// handlers for synchronous signals and SIGPIPE.
    135 	if (isarchive || islibrary) && t.flags&_SigPanic == 0 && sig != _SIGPIPE {
    136 		return false
    137 	}
    138 
    139 	return true
    140 }
    141 
    142 // sigenable enables the Go signal handler to catch the signal sig.
    143 // It is only called while holding the os/signal.handlers lock,
    144 // via os/signal.enableSignal and signal_enable.
    145 func sigenable(sig uint32) {
    146 	if sig >= uint32(len(sigtable)) {
    147 		return
    148 	}
    149 
    150 	// SIGPROF is handled specially for profiling.
    151 	if sig == _SIGPROF {
    152 		return
    153 	}
    154 
    155 	t := &sigtable[sig]
    156 	if t.flags&_SigNotify != 0 {
    157 		ensureSigM()
    158 		enableSigChan <- sig
    159 		<-maskUpdatedChan
    160 		if atomic.Cas(&handlingSig[sig], 0, 1) {
    161 			atomic.Storeuintptr(&fwdSig[sig], getsig(sig))
    162 			setsig(sig, funcPC(sighandler))
    163 		}
    164 	}
    165 }
    166 
    167 // sigdisable disables the Go signal handler for the signal sig.
    168 // It is only called while holding the os/signal.handlers lock,
    169 // via os/signal.disableSignal and signal_disable.
    170 func sigdisable(sig uint32) {
    171 	if sig >= uint32(len(sigtable)) {
    172 		return
    173 	}
    174 
    175 	// SIGPROF is handled specially for profiling.
    176 	if sig == _SIGPROF {
    177 		return
    178 	}
    179 
    180 	t := &sigtable[sig]
    181 	if t.flags&_SigNotify != 0 {
    182 		ensureSigM()
    183 		disableSigChan <- sig
    184 		<-maskUpdatedChan
    185 
    186 		// If initsig does not install a signal handler for a
    187 		// signal, then to go back to the state before Notify
    188 		// we should remove the one we installed.
    189 		if !sigInstallGoHandler(sig) {
    190 			atomic.Store(&handlingSig[sig], 0)
    191 			setsig(sig, atomic.Loaduintptr(&fwdSig[sig]))
    192 		}
    193 	}
    194 }
    195 
    196 // sigignore ignores the signal sig.
    197 // It is only called while holding the os/signal.handlers lock,
    198 // via os/signal.ignoreSignal and signal_ignore.
    199 func sigignore(sig uint32) {
    200 	if sig >= uint32(len(sigtable)) {
    201 		return
    202 	}
    203 
    204 	// SIGPROF is handled specially for profiling.
    205 	if sig == _SIGPROF {
    206 		return
    207 	}
    208 
    209 	t := &sigtable[sig]
    210 	if t.flags&_SigNotify != 0 {
    211 		atomic.Store(&handlingSig[sig], 0)
    212 		setsig(sig, _SIG_IGN)
    213 	}
    214 }
    215 
    216 // clearSignalHandlers clears all signal handlers that are not ignored
    217 // back to the default. This is called by the child after a fork, so that
    218 // we can enable the signal mask for the exec without worrying about
    219 // running a signal handler in the child.
    220 //go:nosplit
    221 //go:nowritebarrierrec
    222 func clearSignalHandlers() {
    223 	for i := uint32(0); i < _NSIG; i++ {
    224 		if atomic.Load(&handlingSig[i]) != 0 {
    225 			setsig(i, _SIG_DFL)
    226 		}
    227 	}
    228 }
    229 
    230 // setProcessCPUProfiler is called when the profiling timer changes.
    231 // It is called with prof.lock held. hz is the new timer, and is 0 if
    232 // profiling is being disabled. Enable or disable the signal as
    233 // required for -buildmode=c-archive.
    234 func setProcessCPUProfiler(hz int32) {
    235 	if hz != 0 {
    236 		// Enable the Go signal handler if not enabled.
    237 		if atomic.Cas(&handlingSig[_SIGPROF], 0, 1) {
    238 			atomic.Storeuintptr(&fwdSig[_SIGPROF], getsig(_SIGPROF))
    239 			setsig(_SIGPROF, funcPC(sighandler))
    240 		}
    241 	} else {
    242 		// If the Go signal handler should be disabled by default,
    243 		// disable it if it is enabled.
    244 		if !sigInstallGoHandler(_SIGPROF) {
    245 			if atomic.Cas(&handlingSig[_SIGPROF], 1, 0) {
    246 				setsig(_SIGPROF, atomic.Loaduintptr(&fwdSig[_SIGPROF]))
    247 			}
    248 		}
    249 	}
    250 }
    251 
    252 // setThreadCPUProfiler makes any thread-specific changes required to
    253 // implement profiling at a rate of hz.
    254 func setThreadCPUProfiler(hz int32) {
    255 	var it itimerval
    256 	if hz == 0 {
    257 		setitimer(_ITIMER_PROF, &it, nil)
    258 	} else {
    259 		it.it_interval.tv_sec = 0
    260 		it.it_interval.set_usec(1000000 / hz)
    261 		it.it_value = it.it_interval
    262 		setitimer(_ITIMER_PROF, &it, nil)
    263 	}
    264 	_g_ := getg()
    265 	_g_.m.profilehz = hz
    266 }
    267 
    268 func sigpipe() {
    269 	if sigsend(_SIGPIPE) {
    270 		return
    271 	}
    272 	dieFromSignal(_SIGPIPE)
    273 }
    274 
    275 // sigtrampgo is called from the signal handler function, sigtramp,
    276 // written in assembly code.
    277 // This is called by the signal handler, and the world may be stopped.
    278 //
    279 // It must be nosplit because getg() is still the G that was running
    280 // (if any) when the signal was delivered, but it's (usually) called
    281 // on the gsignal stack. Until this switches the G to gsignal, the
    282 // stack bounds check won't work.
    283 //
    284 //go:nosplit
    285 //go:nowritebarrierrec
    286 func sigtrampgo(sig uint32, info *siginfo, ctx unsafe.Pointer) {
    287 	if sigfwdgo(sig, info, ctx) {
    288 		return
    289 	}
    290 	g := getg()
    291 	if g == nil {
    292 		c := &sigctxt{info, ctx}
    293 		if sig == _SIGPROF {
    294 			sigprofNonGoPC(c.sigpc())
    295 			return
    296 		}
    297 		badsignal(uintptr(sig), c)
    298 		return
    299 	}
    300 
    301 	// If some non-Go code called sigaltstack, adjust.
    302 	setStack := false
    303 	var gsignalStack gsignalStack
    304 	sp := uintptr(unsafe.Pointer(&sig))
    305 	if sp < g.m.gsignal.stack.lo || sp >= g.m.gsignal.stack.hi {
    306 		if sp >= g.m.g0.stack.lo && sp < g.m.g0.stack.hi {
    307 			// The signal was delivered on the g0 stack.
    308 			// This can happen when linked with C code
    309 			// using the thread sanitizer, which collects
    310 			// signals then delivers them itself by calling
    311 			// the signal handler directly when C code,
    312 			// including C code called via cgo, calls a
    313 			// TSAN-intercepted function such as malloc.
    314 			st := stackt{ss_size: g.m.g0.stack.hi - g.m.g0.stack.lo}
    315 			setSignalstackSP(&st, g.m.g0.stack.lo)
    316 			setGsignalStack(&st, &gsignalStack)
    317 			g.m.gsignal.stktopsp = getcallersp(unsafe.Pointer(&sig))
    318 			setStack = true
    319 		} else {
    320 			var st stackt
    321 			sigaltstack(nil, &st)
    322 			if st.ss_flags&_SS_DISABLE != 0 {
    323 				setg(nil)
    324 				needm(0)
    325 				noSignalStack(sig)
    326 				dropm()
    327 			}
    328 			stsp := uintptr(unsafe.Pointer(st.ss_sp))
    329 			if sp < stsp || sp >= stsp+st.ss_size {
    330 				setg(nil)
    331 				needm(0)
    332 				sigNotOnStack(sig)
    333 				dropm()
    334 			}
    335 			setGsignalStack(&st, &gsignalStack)
    336 			g.m.gsignal.stktopsp = getcallersp(unsafe.Pointer(&sig))
    337 			setStack = true
    338 		}
    339 	}
    340 
    341 	setg(g.m.gsignal)
    342 
    343 	if g.stackguard0 == stackFork {
    344 		signalDuringFork(sig)
    345 	}
    346 
    347 	c := &sigctxt{info, ctx}
    348 	c.fixsigcode(sig)
    349 	sighandler(sig, info, ctx, g)
    350 	setg(g)
    351 	if setStack {
    352 		restoreGsignalStack(&gsignalStack)
    353 	}
    354 }
    355 
    356 // sigpanic turns a synchronous signal into a run-time panic.
    357 // If the signal handler sees a synchronous panic, it arranges the
    358 // stack to look like the function where the signal occurred called
    359 // sigpanic, sets the signal's PC value to sigpanic, and returns from
    360 // the signal handler. The effect is that the program will act as
    361 // though the function that got the signal simply called sigpanic
    362 // instead.
    363 //
    364 // This must NOT be nosplit because the linker doesn't know where
    365 // sigpanic calls can be injected.
    366 //
    367 // The signal handler must not inject a call to sigpanic if
    368 // getg().throwsplit, since sigpanic may need to grow the stack.
    369 func sigpanic() {
    370 	g := getg()
    371 	if !canpanic(g) {
    372 		throw("unexpected signal during runtime execution")
    373 	}
    374 
    375 	switch g.sig {
    376 	case _SIGBUS:
    377 		if g.sigcode0 == _BUS_ADRERR && g.sigcode1 < 0x1000 {
    378 			panicmem()
    379 		}
    380 		// Support runtime/debug.SetPanicOnFault.
    381 		if g.paniconfault {
    382 			panicmem()
    383 		}
    384 		print("unexpected fault address ", hex(g.sigcode1), "\n")
    385 		throw("fault")
    386 	case _SIGSEGV:
    387 		if (g.sigcode0 == 0 || g.sigcode0 == _SEGV_MAPERR || g.sigcode0 == _SEGV_ACCERR) && g.sigcode1 < 0x1000 {
    388 			panicmem()
    389 		}
    390 		// Support runtime/debug.SetPanicOnFault.
    391 		if g.paniconfault {
    392 			panicmem()
    393 		}
    394 		print("unexpected fault address ", hex(g.sigcode1), "\n")
    395 		throw("fault")
    396 	case _SIGFPE:
    397 		switch g.sigcode0 {
    398 		case _FPE_INTDIV:
    399 			panicdivide()
    400 		case _FPE_INTOVF:
    401 			panicoverflow()
    402 		}
    403 		panicfloat()
    404 	}
    405 
    406 	if g.sig >= uint32(len(sigtable)) {
    407 		// can't happen: we looked up g.sig in sigtable to decide to call sigpanic
    408 		throw("unexpected signal value")
    409 	}
    410 	panic(errorString(sigtable[g.sig].name))
    411 }
    412 
    413 // dieFromSignal kills the program with a signal.
    414 // This provides the expected exit status for the shell.
    415 // This is only called with fatal signals expected to kill the process.
    416 //go:nosplit
    417 //go:nowritebarrierrec
    418 func dieFromSignal(sig uint32) {
    419 	unblocksig(sig)
    420 	// Mark the signal as unhandled to ensure it is forwarded.
    421 	atomic.Store(&handlingSig[sig], 0)
    422 	raise(sig)
    423 
    424 	// That should have killed us. On some systems, though, raise
    425 	// sends the signal to the whole process rather than to just
    426 	// the current thread, which means that the signal may not yet
    427 	// have been delivered. Give other threads a chance to run and
    428 	// pick up the signal.
    429 	osyield()
    430 	osyield()
    431 	osyield()
    432 
    433 	// If that didn't work, try _SIG_DFL.
    434 	setsig(sig, _SIG_DFL)
    435 	raise(sig)
    436 
    437 	osyield()
    438 	osyield()
    439 	osyield()
    440 
    441 	// On Darwin we may still fail to die, because raise sends the
    442 	// signal to the whole process rather than just the current thread,
    443 	// and osyield just sleeps briefly rather than letting all other
    444 	// threads run. See issue 20315. Sleep longer.
    445 	if GOOS == "darwin" {
    446 		usleep(100)
    447 	}
    448 
    449 	// If we are still somehow running, just exit with the wrong status.
    450 	exit(2)
    451 }
    452 
    453 // raisebadsignal is called when a signal is received on a non-Go
    454 // thread, and the Go program does not want to handle it (that is, the
    455 // program has not called os/signal.Notify for the signal).
    456 func raisebadsignal(sig uint32, c *sigctxt) {
    457 	if sig == _SIGPROF {
    458 		// Ignore profiling signals that arrive on non-Go threads.
    459 		return
    460 	}
    461 
    462 	var handler uintptr
    463 	if sig >= _NSIG {
    464 		handler = _SIG_DFL
    465 	} else {
    466 		handler = atomic.Loaduintptr(&fwdSig[sig])
    467 	}
    468 
    469 	// Reset the signal handler and raise the signal.
    470 	// We are currently running inside a signal handler, so the
    471 	// signal is blocked. We need to unblock it before raising the
    472 	// signal, or the signal we raise will be ignored until we return
    473 	// from the signal handler. We know that the signal was unblocked
    474 	// before entering the handler, or else we would not have received
    475 	// it. That means that we don't have to worry about blocking it
    476 	// again.
    477 	unblocksig(sig)
    478 	setsig(sig, handler)
    479 
    480 	// If we're linked into a non-Go program we want to try to
    481 	// avoid modifying the original context in which the signal
    482 	// was raised. If the handler is the default, we know it
    483 	// is non-recoverable, so we don't have to worry about
    484 	// re-installing sighandler. At this point we can just
    485 	// return and the signal will be re-raised and caught by
    486 	// the default handler with the correct context.
    487 	if (isarchive || islibrary) && handler == _SIG_DFL && c.sigcode() != _SI_USER {
    488 		return
    489 	}
    490 
    491 	raise(sig)
    492 
    493 	// Give the signal a chance to be delivered.
    494 	// In almost all real cases the program is about to crash,
    495 	// so sleeping here is not a waste of time.
    496 	usleep(1000)
    497 
    498 	// If the signal didn't cause the program to exit, restore the
    499 	// Go signal handler and carry on.
    500 	//
    501 	// We may receive another instance of the signal before we
    502 	// restore the Go handler, but that is not so bad: we know
    503 	// that the Go program has been ignoring the signal.
    504 	setsig(sig, funcPC(sighandler))
    505 }
    506 
    507 func crash() {
    508 	if GOOS == "darwin" {
    509 		// OS X core dumps are linear dumps of the mapped memory,
    510 		// from the first virtual byte to the last, with zeros in the gaps.
    511 		// Because of the way we arrange the address space on 64-bit systems,
    512 		// this means the OS X core file will be >128 GB and even on a zippy
    513 		// workstation can take OS X well over an hour to write (uninterruptible).
    514 		// Save users from making that mistake.
    515 		if GOARCH == "amd64" {
    516 			return
    517 		}
    518 	}
    519 
    520 	dieFromSignal(_SIGABRT)
    521 }
    522 
    523 // ensureSigM starts one global, sleeping thread to make sure at least one thread
    524 // is available to catch signals enabled for os/signal.
    525 func ensureSigM() {
    526 	if maskUpdatedChan != nil {
    527 		return
    528 	}
    529 	maskUpdatedChan = make(chan struct{})
    530 	disableSigChan = make(chan uint32)
    531 	enableSigChan = make(chan uint32)
    532 	go func() {
    533 		// Signal masks are per-thread, so make sure this goroutine stays on one
    534 		// thread.
    535 		LockOSThread()
    536 		defer UnlockOSThread()
    537 		// The sigBlocked mask contains the signals not active for os/signal,
    538 		// initially all signals except the essential. When signal.Notify()/Stop is called,
    539 		// sigenable/sigdisable in turn notify this thread to update its signal
    540 		// mask accordingly.
    541 		sigBlocked := sigset_all
    542 		for i := range sigtable {
    543 			if !blockableSig(uint32(i)) {
    544 				sigdelset(&sigBlocked, i)
    545 			}
    546 		}
    547 		sigprocmask(_SIG_SETMASK, &sigBlocked, nil)
    548 		for {
    549 			select {
    550 			case sig := <-enableSigChan:
    551 				if sig > 0 {
    552 					sigdelset(&sigBlocked, int(sig))
    553 				}
    554 			case sig := <-disableSigChan:
    555 				if sig > 0 && blockableSig(sig) {
    556 					sigaddset(&sigBlocked, int(sig))
    557 				}
    558 			}
    559 			sigprocmask(_SIG_SETMASK, &sigBlocked, nil)
    560 			maskUpdatedChan <- struct{}{}
    561 		}
    562 	}()
    563 }
    564 
    565 // This is called when we receive a signal when there is no signal stack.
    566 // This can only happen if non-Go code calls sigaltstack to disable the
    567 // signal stack.
    568 func noSignalStack(sig uint32) {
    569 	println("signal", sig, "received on thread with no signal stack")
    570 	throw("non-Go code disabled sigaltstack")
    571 }
    572 
    573 // This is called if we receive a signal when there is a signal stack
    574 // but we are not on it. This can only happen if non-Go code called
    575 // sigaction without setting the SS_ONSTACK flag.
    576 func sigNotOnStack(sig uint32) {
    577 	println("signal", sig, "received but handler not on signal stack")
    578 	throw("non-Go code set up signal handler without SA_ONSTACK flag")
    579 }
    580 
    581 // signalDuringFork is called if we receive a signal while doing a fork.
    582 // We do not want signals at that time, as a signal sent to the process
    583 // group may be delivered to the child process, causing confusion.
    584 // This should never be called, because we block signals across the fork;
    585 // this function is just a safety check. See issue 18600 for background.
    586 func signalDuringFork(sig uint32) {
    587 	println("signal", sig, "received during fork")
    588 	throw("signal received during fork")
    589 }
    590 
    591 // This runs on a foreign stack, without an m or a g. No stack split.
    592 //go:nosplit
    593 //go:norace
    594 //go:nowritebarrierrec
    595 func badsignal(sig uintptr, c *sigctxt) {
    596 	needm(0)
    597 	if !sigsend(uint32(sig)) {
    598 		// A foreign thread received the signal sig, and the
    599 		// Go code does not want to handle it.
    600 		raisebadsignal(uint32(sig), c)
    601 	}
    602 	dropm()
    603 }
    604 
    605 //go:noescape
    606 func sigfwd(fn uintptr, sig uint32, info *siginfo, ctx unsafe.Pointer)
    607 
    608 // Determines if the signal should be handled by Go and if not, forwards the
    609 // signal to the handler that was installed before Go's. Returns whether the
    610 // signal was forwarded.
    611 // This is called by the signal handler, and the world may be stopped.
    612 //go:nosplit
    613 //go:nowritebarrierrec
    614 func sigfwdgo(sig uint32, info *siginfo, ctx unsafe.Pointer) bool {
    615 	if sig >= uint32(len(sigtable)) {
    616 		return false
    617 	}
    618 	fwdFn := atomic.Loaduintptr(&fwdSig[sig])
    619 	flags := sigtable[sig].flags
    620 
    621 	// If we aren't handling the signal, forward it.
    622 	if atomic.Load(&handlingSig[sig]) == 0 || !signalsOK {
    623 		// If the signal is ignored, doing nothing is the same as forwarding.
    624 		if fwdFn == _SIG_IGN || (fwdFn == _SIG_DFL && flags&_SigIgn != 0) {
    625 			return true
    626 		}
    627 		// We are not handling the signal and there is no other handler to forward to.
    628 		// Crash with the default behavior.
    629 		if fwdFn == _SIG_DFL {
    630 			setsig(sig, _SIG_DFL)
    631 			dieFromSignal(sig)
    632 			return false
    633 		}
    634 
    635 		sigfwd(fwdFn, sig, info, ctx)
    636 		return true
    637 	}
    638 
    639 	// If there is no handler to forward to, no need to forward.
    640 	if fwdFn == _SIG_DFL {
    641 		return false
    642 	}
    643 
    644 	c := &sigctxt{info, ctx}
    645 	// Only forward synchronous signals and SIGPIPE.
    646 	// Unfortunately, user generated SIGPIPEs will also be forwarded, because si_code
    647 	// is set to _SI_USER even for a SIGPIPE raised from a write to a closed socket
    648 	// or pipe.
    649 	if (c.sigcode() == _SI_USER || flags&_SigPanic == 0) && sig != _SIGPIPE {
    650 		return false
    651 	}
    652 	// Determine if the signal occurred inside Go code. We test that:
    653 	//   (1) we were in a goroutine (i.e., m.curg != nil), and
    654 	//   (2) we weren't in CGO.
    655 	g := getg()
    656 	if g != nil && g.m != nil && g.m.curg != nil && !g.m.incgo {
    657 		return false
    658 	}
    659 
    660 	// Signal not handled by Go, forward it.
    661 	if fwdFn != _SIG_IGN {
    662 		sigfwd(fwdFn, sig, info, ctx)
    663 	}
    664 
    665 	return true
    666 }
    667 
    668 // msigsave saves the current thread's signal mask into mp.sigmask.
    669 // This is used to preserve the non-Go signal mask when a non-Go
    670 // thread calls a Go function.
    671 // This is nosplit and nowritebarrierrec because it is called by needm
    672 // which may be called on a non-Go thread with no g available.
    673 //go:nosplit
    674 //go:nowritebarrierrec
    675 func msigsave(mp *m) {
    676 	sigprocmask(_SIG_SETMASK, nil, &mp.sigmask)
    677 }
    678 
    679 // msigrestore sets the current thread's signal mask to sigmask.
    680 // This is used to restore the non-Go signal mask when a non-Go thread
    681 // calls a Go function.
    682 // This is nosplit and nowritebarrierrec because it is called by dropm
    683 // after g has been cleared.
    684 //go:nosplit
    685 //go:nowritebarrierrec
    686 func msigrestore(sigmask sigset) {
    687 	sigprocmask(_SIG_SETMASK, &sigmask, nil)
    688 }
    689 
    690 // sigblock blocks all signals in the current thread's signal mask.
    691 // This is used to block signals while setting up and tearing down g
    692 // when a non-Go thread calls a Go function.
    693 // The OS-specific code is expected to define sigset_all.
    694 // This is nosplit and nowritebarrierrec because it is called by needm
    695 // which may be called on a non-Go thread with no g available.
    696 //go:nosplit
    697 //go:nowritebarrierrec
    698 func sigblock() {
    699 	sigprocmask(_SIG_SETMASK, &sigset_all, nil)
    700 }
    701 
    702 // unblocksig removes sig from the current thread's signal mask.
    703 // This is nosplit and nowritebarrierrec because it is called from
    704 // dieFromSignal, which can be called by sigfwdgo while running in the
    705 // signal handler, on the signal stack, with no g available.
    706 //go:nosplit
    707 //go:nowritebarrierrec
    708 func unblocksig(sig uint32) {
    709 	var set sigset
    710 	sigaddset(&set, int(sig))
    711 	sigprocmask(_SIG_UNBLOCK, &set, nil)
    712 }
    713 
    714 // minitSignals is called when initializing a new m to set the
    715 // thread's alternate signal stack and signal mask.
    716 func minitSignals() {
    717 	minitSignalStack()
    718 	minitSignalMask()
    719 }
    720 
    721 // minitSignalStack is called when initializing a new m to set the
    722 // alternate signal stack. If the alternate signal stack is not set
    723 // for the thread (the normal case) then set the alternate signal
    724 // stack to the gsignal stack. If the alternate signal stack is set
    725 // for the thread (the case when a non-Go thread sets the alternate
    726 // signal stack and then calls a Go function) then set the gsignal
    727 // stack to the alternate signal stack. Record which choice was made
    728 // in newSigstack, so that it can be undone in unminit.
    729 func minitSignalStack() {
    730 	_g_ := getg()
    731 	var st stackt
    732 	sigaltstack(nil, &st)
    733 	if st.ss_flags&_SS_DISABLE != 0 {
    734 		signalstack(&_g_.m.gsignal.stack)
    735 		_g_.m.newSigstack = true
    736 	} else {
    737 		setGsignalStack(&st, &_g_.m.goSigStack)
    738 		_g_.m.newSigstack = false
    739 	}
    740 }
    741 
    742 // minitSignalMask is called when initializing a new m to set the
    743 // thread's signal mask. When this is called all signals have been
    744 // blocked for the thread.  This starts with m.sigmask, which was set
    745 // either from initSigmask for a newly created thread or by calling
    746 // msigsave if this is a non-Go thread calling a Go function. It
    747 // removes all essential signals from the mask, thus causing those
    748 // signals to not be blocked. Then it sets the thread's signal mask.
    749 // After this is called the thread can receive signals.
    750 func minitSignalMask() {
    751 	nmask := getg().m.sigmask
    752 	for i := range sigtable {
    753 		if !blockableSig(uint32(i)) {
    754 			sigdelset(&nmask, i)
    755 		}
    756 	}
    757 	sigprocmask(_SIG_SETMASK, &nmask, nil)
    758 }
    759 
    760 // unminitSignals is called from dropm, via unminit, to undo the
    761 // effect of calling minit on a non-Go thread.
    762 //go:nosplit
    763 func unminitSignals() {
    764 	if getg().m.newSigstack {
    765 		st := stackt{ss_flags: _SS_DISABLE}
    766 		sigaltstack(&st, nil)
    767 	} else {
    768 		// We got the signal stack from someone else. Restore
    769 		// the Go-allocated stack in case this M gets reused
    770 		// for another thread (e.g., it's an extram). Also, on
    771 		// Android, libc allocates a signal stack for all
    772 		// threads, so it's important to restore the Go stack
    773 		// even on Go-created threads so we can free it.
    774 		restoreGsignalStack(&getg().m.goSigStack)
    775 	}
    776 }
    777 
    778 // blockableSig returns whether sig may be blocked by the signal mask.
    779 // We never want to block the signals marked _SigUnblock;
    780 // these are the synchronous signals that turn into a Go panic.
    781 // In a Go program--not a c-archive/c-shared--we never want to block
    782 // the signals marked _SigKill or _SigThrow, as otherwise it's possible
    783 // for all running threads to block them and delay their delivery until
    784 // we start a new thread. When linked into a C program we let the C code
    785 // decide on the disposition of those signals.
    786 func blockableSig(sig uint32) bool {
    787 	flags := sigtable[sig].flags
    788 	if flags&_SigUnblock != 0 {
    789 		return false
    790 	}
    791 	if isarchive || islibrary {
    792 		return true
    793 	}
    794 	return flags&(_SigKill|_SigThrow) == 0
    795 }
    796 
    797 // gsignalStack saves the fields of the gsignal stack changed by
    798 // setGsignalStack.
    799 type gsignalStack struct {
    800 	stack       stack
    801 	stackguard0 uintptr
    802 	stackguard1 uintptr
    803 	stktopsp    uintptr
    804 }
    805 
    806 // setGsignalStack sets the gsignal stack of the current m to an
    807 // alternate signal stack returned from the sigaltstack system call.
    808 // It saves the old values in *old for use by restoreGsignalStack.
    809 // This is used when handling a signal if non-Go code has set the
    810 // alternate signal stack.
    811 //go:nosplit
    812 //go:nowritebarrierrec
    813 func setGsignalStack(st *stackt, old *gsignalStack) {
    814 	g := getg()
    815 	if old != nil {
    816 		old.stack = g.m.gsignal.stack
    817 		old.stackguard0 = g.m.gsignal.stackguard0
    818 		old.stackguard1 = g.m.gsignal.stackguard1
    819 		old.stktopsp = g.m.gsignal.stktopsp
    820 	}
    821 	stsp := uintptr(unsafe.Pointer(st.ss_sp))
    822 	g.m.gsignal.stack.lo = stsp
    823 	g.m.gsignal.stack.hi = stsp + st.ss_size
    824 	g.m.gsignal.stackguard0 = stsp + _StackGuard
    825 	g.m.gsignal.stackguard1 = stsp + _StackGuard
    826 }
    827 
    828 // restoreGsignalStack restores the gsignal stack to the value it had
    829 // before entering the signal handler.
    830 //go:nosplit
    831 //go:nowritebarrierrec
    832 func restoreGsignalStack(st *gsignalStack) {
    833 	gp := getg().m.gsignal
    834 	gp.stack = st.stack
    835 	gp.stackguard0 = st.stackguard0
    836 	gp.stackguard1 = st.stackguard1
    837 	gp.stktopsp = st.stktopsp
    838 }
    839 
    840 // signalstack sets the current thread's alternate signal stack to s.
    841 //go:nosplit
    842 func signalstack(s *stack) {
    843 	st := stackt{ss_size: s.hi - s.lo}
    844 	setSignalstackSP(&st, s.lo)
    845 	sigaltstack(&st, nil)
    846 }
    847 
    848 // setsigsegv is used on darwin/arm{,64} to fake a segmentation fault.
    849 //go:nosplit
    850 func setsigsegv(pc uintptr) {
    851 	g := getg()
    852 	g.sig = _SIGSEGV
    853 	g.sigpc = pc
    854 	g.sigcode0 = _SEGV_MAPERR
    855 	g.sigcode1 = 0 // TODO: emulate si_addr
    856 }
    857