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