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