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