Source file src/runtime/cgo_sigaction.go
Documentation: runtime
1 // Copyright 2016 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 // Support for sanitizers. See runtime/cgo/sigaction.go. 6 7 //go:build (linux && amd64) || (freebsd && amd64) || (linux && arm64) || (linux && ppc64le) 8 // +build linux,amd64 freebsd,amd64 linux,arm64 linux,ppc64le 9 10 package runtime 11 12 import "unsafe" 13 14 // _cgo_sigaction is filled in by runtime/cgo when it is linked into the 15 // program, so it is only non-nil when using cgo. 16 //go:linkname _cgo_sigaction _cgo_sigaction 17 var _cgo_sigaction unsafe.Pointer 18 19 //go:nosplit 20 //go:nowritebarrierrec 21 func sigaction(sig uint32, new, old *sigactiont) { 22 // racewalk.go avoids adding sanitizing instrumentation to package runtime, 23 // but we might be calling into instrumented C functions here, 24 // so we need the pointer parameters to be properly marked. 25 // 26 // Mark the input as having been written before the call 27 // and the output as read after. 28 if msanenabled && new != nil { 29 msanwrite(unsafe.Pointer(new), unsafe.Sizeof(*new)) 30 } 31 32 if _cgo_sigaction == nil || inForkedChild { 33 sysSigaction(sig, new, old) 34 } else { 35 // We need to call _cgo_sigaction, which means we need a big enough stack 36 // for C. To complicate matters, we may be in libpreinit (before the 37 // runtime has been initialized) or in an asynchronous signal handler (with 38 // the current thread in transition between goroutines, or with the g0 39 // system stack already in use). 40 41 var ret int32 42 43 var g *g 44 if mainStarted { 45 g = getg() 46 } 47 sp := uintptr(unsafe.Pointer(&sig)) 48 switch { 49 case g == nil: 50 // No g: we're on a C stack or a signal stack. 51 ret = callCgoSigaction(uintptr(sig), new, old) 52 case sp < g.stack.lo || sp >= g.stack.hi: 53 // We're no longer on g's stack, so we must be handling a signal. It's 54 // possible that we interrupted the thread during a transition between g 55 // and g0, so we should stay on the current stack to avoid corrupting g0. 56 ret = callCgoSigaction(uintptr(sig), new, old) 57 default: 58 // We're running on g's stack, so either we're not in a signal handler or 59 // the signal handler has set the correct g. If we're on gsignal or g0, 60 // systemstack will make the call directly; otherwise, it will switch to 61 // g0 to ensure we have enough room to call a libc function. 62 // 63 // The function literal that we pass to systemstack is not nosplit, but 64 // that's ok: we'll be running on a fresh, clean system stack so the stack 65 // check will always succeed anyway. 66 systemstack(func() { 67 ret = callCgoSigaction(uintptr(sig), new, old) 68 }) 69 } 70 71 const EINVAL = 22 72 if ret == EINVAL { 73 // libc reserves certain signals — normally 32-33 — for pthreads, and 74 // returns EINVAL for sigaction calls on those signals. If we get EINVAL, 75 // fall back to making the syscall directly. 76 sysSigaction(sig, new, old) 77 } 78 } 79 80 if msanenabled && old != nil { 81 msanread(unsafe.Pointer(old), unsafe.Sizeof(*old)) 82 } 83 } 84 85 // callCgoSigaction calls the sigaction function in the runtime/cgo package 86 // using the GCC calling convention. It is implemented in assembly. 87 //go:noescape 88 func callCgoSigaction(sig uintptr, new, old *sigactiont) int32 89