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