...
Run Format

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 memory sanitizer. See runtime/cgo/sigaction.go.
     6	
     7	// +build linux,amd64
     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 rt_sigaction(sig uintptr, new, old *sigactiont, size uintptr) int32 {
    21		// The runtime package is explicitly blacklisted from sanitizer
    22		// instrumentation in racewalk.go, but we might be calling into instrumented C
    23		// functions here — so we need the pointer parameters to be properly marked.
    24		//
    25		// Mark the input as having been written before the call and the output as
    26		// read after.
    27		if msanenabled && new != nil {
    28			msanwrite(unsafe.Pointer(new), unsafe.Sizeof(*new))
    29		}
    30	
    31		var ret int32
    32	
    33		if _cgo_sigaction == nil {
    34			ret = sysSigaction(sig, new, old, size)
    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			g := getg()
    43			sp := uintptr(unsafe.Pointer(&sig))
    44			switch {
    45			case g == nil:
    46				// No g: we're on a C stack or a signal stack.
    47				ret = callCgoSigaction(sig, new, old)
    48			case sp < g.stack.lo || sp >= g.stack.hi:
    49				// We're no longer on g's stack, so we must be handling a signal.  It's
    50				// possible that we interrupted the thread during a transition between g
    51				// and g0, so we should stay on the current stack to avoid corrupting g0.
    52				ret = callCgoSigaction(sig, new, old)
    53			default:
    54				// We're running on g's stack, so either we're not in a signal handler or
    55				// the signal handler has set the correct g.  If we're on gsignal or g0,
    56				// systemstack will make the call directly; otherwise, it will switch to
    57				// g0 to ensure we have enough room to call a libc function.
    58				//
    59				// The function literal that we pass to systemstack is not nosplit, but
    60				// that's ok: we'll be running on a fresh, clean system stack so the stack
    61				// check will always succeed anyway.
    62				systemstack(func() {
    63					ret = callCgoSigaction(sig, new, old)
    64				})
    65			}
    66	
    67			const EINVAL = 22
    68			if ret == EINVAL {
    69				// libc reserves certain signals — normally 32-33 — for pthreads, and
    70				// returns EINVAL for sigaction calls on those signals.  If we get EINVAL,
    71				// fall back to making the syscall directly.
    72				ret = sysSigaction(sig, new, old, size)
    73			}
    74		}
    75	
    76		if msanenabled && old != nil && ret == 0 {
    77			msanread(unsafe.Pointer(old), unsafe.Sizeof(*old))
    78		}
    79		return ret
    80	}
    81	
    82	// sysSigaction calls the rt_sigaction system call. It is implemented in assembly.
    83	//go:noescape
    84	func sysSigaction(sig uintptr, new, old *sigactiont, size uintptr) int32
    85	
    86	// callCgoSigaction calls the sigaction function in the runtime/cgo package
    87	// using the GCC calling convention. It is implemented in assembly.
    88	//go:noescape
    89	func callCgoSigaction(sig uintptr, new, old *sigactiont) int32
    90	

View as plain text