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