github.com/c9s/go@v0.0.0-20180120015821-984e81f64e0c/src/runtime/signal_unix.go (about) 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 func sigpanic() { 364 g := getg() 365 if !canpanic(g) { 366 throw("unexpected signal during runtime execution") 367 } 368 369 switch g.sig { 370 case _SIGBUS: 371 if g.sigcode0 == _BUS_ADRERR && g.sigcode1 < 0x1000 { 372 panicmem() 373 } 374 // Support runtime/debug.SetPanicOnFault. 375 if g.paniconfault { 376 panicmem() 377 } 378 print("unexpected fault address ", hex(g.sigcode1), "\n") 379 throw("fault") 380 case _SIGSEGV: 381 if (g.sigcode0 == 0 || g.sigcode0 == _SEGV_MAPERR || g.sigcode0 == _SEGV_ACCERR) && g.sigcode1 < 0x1000 { 382 panicmem() 383 } 384 // Support runtime/debug.SetPanicOnFault. 385 if g.paniconfault { 386 panicmem() 387 } 388 print("unexpected fault address ", hex(g.sigcode1), "\n") 389 throw("fault") 390 case _SIGFPE: 391 switch g.sigcode0 { 392 case _FPE_INTDIV: 393 panicdivide() 394 case _FPE_INTOVF: 395 panicoverflow() 396 } 397 panicfloat() 398 } 399 400 if g.sig >= uint32(len(sigtable)) { 401 // can't happen: we looked up g.sig in sigtable to decide to call sigpanic 402 throw("unexpected signal value") 403 } 404 panic(errorString(sigtable[g.sig].name)) 405 } 406 407 // dieFromSignal kills the program with a signal. 408 // This provides the expected exit status for the shell. 409 // This is only called with fatal signals expected to kill the process. 410 //go:nosplit 411 //go:nowritebarrierrec 412 func dieFromSignal(sig uint32) { 413 unblocksig(sig) 414 // Mark the signal as unhandled to ensure it is forwarded. 415 atomic.Store(&handlingSig[sig], 0) 416 raise(sig) 417 418 // That should have killed us. On some systems, though, raise 419 // sends the signal to the whole process rather than to just 420 // the current thread, which means that the signal may not yet 421 // have been delivered. Give other threads a chance to run and 422 // pick up the signal. 423 osyield() 424 osyield() 425 osyield() 426 427 // If that didn't work, try _SIG_DFL. 428 setsig(sig, _SIG_DFL) 429 raise(sig) 430 431 osyield() 432 osyield() 433 osyield() 434 435 // On Darwin we may still fail to die, because raise sends the 436 // signal to the whole process rather than just the current thread, 437 // and osyield just sleeps briefly rather than letting all other 438 // threads run. See issue 20315. Sleep longer. 439 if GOOS == "darwin" { 440 usleep(100) 441 } 442 443 // If we are still somehow running, just exit with the wrong status. 444 exit(2) 445 } 446 447 // raisebadsignal is called when a signal is received on a non-Go 448 // thread, and the Go program does not want to handle it (that is, the 449 // program has not called os/signal.Notify for the signal). 450 func raisebadsignal(sig uint32, c *sigctxt) { 451 if sig == _SIGPROF { 452 // Ignore profiling signals that arrive on non-Go threads. 453 return 454 } 455 456 var handler uintptr 457 if sig >= _NSIG { 458 handler = _SIG_DFL 459 } else { 460 handler = atomic.Loaduintptr(&fwdSig[sig]) 461 } 462 463 // Reset the signal handler and raise the signal. 464 // We are currently running inside a signal handler, so the 465 // signal is blocked. We need to unblock it before raising the 466 // signal, or the signal we raise will be ignored until we return 467 // from the signal handler. We know that the signal was unblocked 468 // before entering the handler, or else we would not have received 469 // it. That means that we don't have to worry about blocking it 470 // again. 471 unblocksig(sig) 472 setsig(sig, handler) 473 474 // If we're linked into a non-Go program we want to try to 475 // avoid modifying the original context in which the signal 476 // was raised. If the handler is the default, we know it 477 // is non-recoverable, so we don't have to worry about 478 // re-installing sighandler. At this point we can just 479 // return and the signal will be re-raised and caught by 480 // the default handler with the correct context. 481 if (isarchive || islibrary) && handler == _SIG_DFL && c.sigcode() != _SI_USER { 482 return 483 } 484 485 raise(sig) 486 487 // Give the signal a chance to be delivered. 488 // In almost all real cases the program is about to crash, 489 // so sleeping here is not a waste of time. 490 usleep(1000) 491 492 // If the signal didn't cause the program to exit, restore the 493 // Go signal handler and carry on. 494 // 495 // We may receive another instance of the signal before we 496 // restore the Go handler, but that is not so bad: we know 497 // that the Go program has been ignoring the signal. 498 setsig(sig, funcPC(sighandler)) 499 } 500 501 func crash() { 502 if GOOS == "darwin" { 503 // OS X core dumps are linear dumps of the mapped memory, 504 // from the first virtual byte to the last, with zeros in the gaps. 505 // Because of the way we arrange the address space on 64-bit systems, 506 // this means the OS X core file will be >128 GB and even on a zippy 507 // workstation can take OS X well over an hour to write (uninterruptible). 508 // Save users from making that mistake. 509 if GOARCH == "amd64" { 510 return 511 } 512 } 513 514 dieFromSignal(_SIGABRT) 515 } 516 517 // ensureSigM starts one global, sleeping thread to make sure at least one thread 518 // is available to catch signals enabled for os/signal. 519 func ensureSigM() { 520 if maskUpdatedChan != nil { 521 return 522 } 523 maskUpdatedChan = make(chan struct{}) 524 disableSigChan = make(chan uint32) 525 enableSigChan = make(chan uint32) 526 go func() { 527 // Signal masks are per-thread, so make sure this goroutine stays on one 528 // thread. 529 LockOSThread() 530 defer UnlockOSThread() 531 // The sigBlocked mask contains the signals not active for os/signal, 532 // initially all signals except the essential. When signal.Notify()/Stop is called, 533 // sigenable/sigdisable in turn notify this thread to update its signal 534 // mask accordingly. 535 sigBlocked := sigset_all 536 for i := range sigtable { 537 if !blockableSig(uint32(i)) { 538 sigdelset(&sigBlocked, i) 539 } 540 } 541 sigprocmask(_SIG_SETMASK, &sigBlocked, nil) 542 for { 543 select { 544 case sig := <-enableSigChan: 545 if sig > 0 { 546 sigdelset(&sigBlocked, int(sig)) 547 } 548 case sig := <-disableSigChan: 549 if sig > 0 && blockableSig(sig) { 550 sigaddset(&sigBlocked, int(sig)) 551 } 552 } 553 sigprocmask(_SIG_SETMASK, &sigBlocked, nil) 554 maskUpdatedChan <- struct{}{} 555 } 556 }() 557 } 558 559 // This is called when we receive a signal when there is no signal stack. 560 // This can only happen if non-Go code calls sigaltstack to disable the 561 // signal stack. 562 func noSignalStack(sig uint32) { 563 println("signal", sig, "received on thread with no signal stack") 564 throw("non-Go code disabled sigaltstack") 565 } 566 567 // This is called if we receive a signal when there is a signal stack 568 // but we are not on it. This can only happen if non-Go code called 569 // sigaction without setting the SS_ONSTACK flag. 570 func sigNotOnStack(sig uint32) { 571 println("signal", sig, "received but handler not on signal stack") 572 throw("non-Go code set up signal handler without SA_ONSTACK flag") 573 } 574 575 // signalDuringFork is called if we receive a signal while doing a fork. 576 // We do not want signals at that time, as a signal sent to the process 577 // group may be delivered to the child process, causing confusion. 578 // This should never be called, because we block signals across the fork; 579 // this function is just a safety check. See issue 18600 for background. 580 func signalDuringFork(sig uint32) { 581 println("signal", sig, "received during fork") 582 throw("signal received during fork") 583 } 584 585 // This runs on a foreign stack, without an m or a g. No stack split. 586 //go:nosplit 587 //go:norace 588 //go:nowritebarrierrec 589 func badsignal(sig uintptr, c *sigctxt) { 590 needm(0) 591 if !sigsend(uint32(sig)) { 592 // A foreign thread received the signal sig, and the 593 // Go code does not want to handle it. 594 raisebadsignal(uint32(sig), c) 595 } 596 dropm() 597 } 598 599 //go:noescape 600 func sigfwd(fn uintptr, sig uint32, info *siginfo, ctx unsafe.Pointer) 601 602 // Determines if the signal should be handled by Go and if not, forwards the 603 // signal to the handler that was installed before Go's. Returns whether the 604 // signal was forwarded. 605 // This is called by the signal handler, and the world may be stopped. 606 //go:nosplit 607 //go:nowritebarrierrec 608 func sigfwdgo(sig uint32, info *siginfo, ctx unsafe.Pointer) bool { 609 if sig >= uint32(len(sigtable)) { 610 return false 611 } 612 fwdFn := atomic.Loaduintptr(&fwdSig[sig]) 613 flags := sigtable[sig].flags 614 615 // If we aren't handling the signal, forward it. 616 if atomic.Load(&handlingSig[sig]) == 0 || !signalsOK { 617 // If the signal is ignored, doing nothing is the same as forwarding. 618 if fwdFn == _SIG_IGN || (fwdFn == _SIG_DFL && flags&_SigIgn != 0) { 619 return true 620 } 621 // We are not handling the signal and there is no other handler to forward to. 622 // Crash with the default behavior. 623 if fwdFn == _SIG_DFL { 624 setsig(sig, _SIG_DFL) 625 dieFromSignal(sig) 626 return false 627 } 628 629 sigfwd(fwdFn, sig, info, ctx) 630 return true 631 } 632 633 // If there is no handler to forward to, no need to forward. 634 if fwdFn == _SIG_DFL { 635 return false 636 } 637 638 c := &sigctxt{info, ctx} 639 // Only forward synchronous signals and SIGPIPE. 640 // Unfortunately, user generated SIGPIPEs will also be forwarded, because si_code 641 // is set to _SI_USER even for a SIGPIPE raised from a write to a closed socket 642 // or pipe. 643 if (c.sigcode() == _SI_USER || flags&_SigPanic == 0) && sig != _SIGPIPE { 644 return false 645 } 646 // Determine if the signal occurred inside Go code. We test that: 647 // (1) we were in a goroutine (i.e., m.curg != nil), and 648 // (2) we weren't in CGO. 649 g := getg() 650 if g != nil && g.m != nil && g.m.curg != nil && !g.m.incgo { 651 return false 652 } 653 654 // Signal not handled by Go, forward it. 655 if fwdFn != _SIG_IGN { 656 sigfwd(fwdFn, sig, info, ctx) 657 } 658 659 return true 660 } 661 662 // msigsave saves the current thread's signal mask into mp.sigmask. 663 // This is used to preserve the non-Go signal mask when a non-Go 664 // thread calls a Go function. 665 // This is nosplit and nowritebarrierrec because it is called by needm 666 // which may be called on a non-Go thread with no g available. 667 //go:nosplit 668 //go:nowritebarrierrec 669 func msigsave(mp *m) { 670 sigprocmask(_SIG_SETMASK, nil, &mp.sigmask) 671 } 672 673 // msigrestore sets the current thread's signal mask to sigmask. 674 // This is used to restore the non-Go signal mask when a non-Go thread 675 // calls a Go function. 676 // This is nosplit and nowritebarrierrec because it is called by dropm 677 // after g has been cleared. 678 //go:nosplit 679 //go:nowritebarrierrec 680 func msigrestore(sigmask sigset) { 681 sigprocmask(_SIG_SETMASK, &sigmask, nil) 682 } 683 684 // sigblock blocks all signals in the current thread's signal mask. 685 // This is used to block signals while setting up and tearing down g 686 // when a non-Go thread calls a Go function. 687 // The OS-specific code is expected to define sigset_all. 688 // This is nosplit and nowritebarrierrec because it is called by needm 689 // which may be called on a non-Go thread with no g available. 690 //go:nosplit 691 //go:nowritebarrierrec 692 func sigblock() { 693 sigprocmask(_SIG_SETMASK, &sigset_all, nil) 694 } 695 696 // unblocksig removes sig from the current thread's signal mask. 697 // This is nosplit and nowritebarrierrec because it is called from 698 // dieFromSignal, which can be called by sigfwdgo while running in the 699 // signal handler, on the signal stack, with no g available. 700 //go:nosplit 701 //go:nowritebarrierrec 702 func unblocksig(sig uint32) { 703 var set sigset 704 sigaddset(&set, int(sig)) 705 sigprocmask(_SIG_UNBLOCK, &set, nil) 706 } 707 708 // minitSignals is called when initializing a new m to set the 709 // thread's alternate signal stack and signal mask. 710 func minitSignals() { 711 minitSignalStack() 712 minitSignalMask() 713 } 714 715 // minitSignalStack is called when initializing a new m to set the 716 // alternate signal stack. If the alternate signal stack is not set 717 // for the thread (the normal case) then set the alternate signal 718 // stack to the gsignal stack. If the alternate signal stack is set 719 // for the thread (the case when a non-Go thread sets the alternate 720 // signal stack and then calls a Go function) then set the gsignal 721 // stack to the alternate signal stack. Record which choice was made 722 // in newSigstack, so that it can be undone in unminit. 723 func minitSignalStack() { 724 _g_ := getg() 725 var st stackt 726 sigaltstack(nil, &st) 727 if st.ss_flags&_SS_DISABLE != 0 { 728 signalstack(&_g_.m.gsignal.stack) 729 _g_.m.newSigstack = true 730 } else { 731 setGsignalStack(&st, &_g_.m.goSigStack) 732 _g_.m.newSigstack = false 733 } 734 } 735 736 // minitSignalMask is called when initializing a new m to set the 737 // thread's signal mask. When this is called all signals have been 738 // blocked for the thread. This starts with m.sigmask, which was set 739 // either from initSigmask for a newly created thread or by calling 740 // msigsave if this is a non-Go thread calling a Go function. It 741 // removes all essential signals from the mask, thus causing those 742 // signals to not be blocked. Then it sets the thread's signal mask. 743 // After this is called the thread can receive signals. 744 func minitSignalMask() { 745 nmask := getg().m.sigmask 746 for i := range sigtable { 747 if !blockableSig(uint32(i)) { 748 sigdelset(&nmask, i) 749 } 750 } 751 sigprocmask(_SIG_SETMASK, &nmask, nil) 752 } 753 754 // unminitSignals is called from dropm, via unminit, to undo the 755 // effect of calling minit on a non-Go thread. 756 //go:nosplit 757 func unminitSignals() { 758 if getg().m.newSigstack { 759 st := stackt{ss_flags: _SS_DISABLE} 760 sigaltstack(&st, nil) 761 } else { 762 // We got the signal stack from someone else. Restore 763 // the Go-allocated stack in case this M gets reused 764 // for another thread (e.g., it's an extram). Also, on 765 // Android, libc allocates a signal stack for all 766 // threads, so it's important to restore the Go stack 767 // even on Go-created threads so we can free it. 768 restoreGsignalStack(&getg().m.goSigStack) 769 } 770 } 771 772 // blockableSig returns whether sig may be blocked by the signal mask. 773 // We never want to block the signals marked _SigUnblock; 774 // these are the synchronous signals that turn into a Go panic. 775 // In a Go program--not a c-archive/c-shared--we never want to block 776 // the signals marked _SigKill or _SigThrow, as otherwise it's possible 777 // for all running threads to block them and delay their delivery until 778 // we start a new thread. When linked into a C program we let the C code 779 // decide on the disposition of those signals. 780 func blockableSig(sig uint32) bool { 781 flags := sigtable[sig].flags 782 if flags&_SigUnblock != 0 { 783 return false 784 } 785 if isarchive || islibrary { 786 return true 787 } 788 return flags&(_SigKill|_SigThrow) == 0 789 } 790 791 // gsignalStack saves the fields of the gsignal stack changed by 792 // setGsignalStack. 793 type gsignalStack struct { 794 stack stack 795 stackguard0 uintptr 796 stackguard1 uintptr 797 stktopsp uintptr 798 } 799 800 // setGsignalStack sets the gsignal stack of the current m to an 801 // alternate signal stack returned from the sigaltstack system call. 802 // It saves the old values in *old for use by restoreGsignalStack. 803 // This is used when handling a signal if non-Go code has set the 804 // alternate signal stack. 805 //go:nosplit 806 //go:nowritebarrierrec 807 func setGsignalStack(st *stackt, old *gsignalStack) { 808 g := getg() 809 if old != nil { 810 old.stack = g.m.gsignal.stack 811 old.stackguard0 = g.m.gsignal.stackguard0 812 old.stackguard1 = g.m.gsignal.stackguard1 813 old.stktopsp = g.m.gsignal.stktopsp 814 } 815 stsp := uintptr(unsafe.Pointer(st.ss_sp)) 816 g.m.gsignal.stack.lo = stsp 817 g.m.gsignal.stack.hi = stsp + st.ss_size 818 g.m.gsignal.stackguard0 = stsp + _StackGuard 819 g.m.gsignal.stackguard1 = stsp + _StackGuard 820 } 821 822 // restoreGsignalStack restores the gsignal stack to the value it had 823 // before entering the signal handler. 824 //go:nosplit 825 //go:nowritebarrierrec 826 func restoreGsignalStack(st *gsignalStack) { 827 gp := getg().m.gsignal 828 gp.stack = st.stack 829 gp.stackguard0 = st.stackguard0 830 gp.stackguard1 = st.stackguard1 831 gp.stktopsp = st.stktopsp 832 } 833 834 // signalstack sets the current thread's alternate signal stack to s. 835 //go:nosplit 836 func signalstack(s *stack) { 837 st := stackt{ss_size: s.hi - s.lo} 838 setSignalstackSP(&st, s.lo) 839 sigaltstack(&st, nil) 840 } 841 842 // setsigsegv is used on darwin/arm{,64} to fake a segmentation fault. 843 //go:nosplit 844 func setsigsegv(pc uintptr) { 845 g := getg() 846 g.sig = _SIGSEGV 847 g.sigpc = pc 848 g.sigcode0 = _SEGV_MAPERR 849 g.sigcode1 = 0 // TODO: emulate si_addr 850 }