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