Source file src/runtime/cgo/cgo_linux_sigaction.go

     1  // Copyright 2026 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  //go:build goexperiment.runtimecgopurego && !msan && !tsan && linux && (386 || amd64 || arm64 || loong64 || ppc64 || ppc64le || riscv64)
     6  
     7  package cgo
     8  
     9  import "unsafe"
    10  
    11  //go:linkname _cgo_libc_sigaction _cgo_libc_sigaction
    12  var _cgo_libc_sigaction byte
    13  var libc_sigaction = unsafe.Pointer(&_cgo_libc_sigaction)
    14  
    15  //go:linknamestd _cgoSigaction runtime.cgoSigaction
    16  var _cgoSigaction = cgoSigaction
    17  
    18  // sigactionCall calls the C function fn with up to 3 arguments.
    19  // It calls the runtime directly rather than through asmcgocall or
    20  // asmcgocallNoG so that the nosplit chain from cgoSigaction stays
    21  // within the nosplit stack limit even when compiled with -N -l.
    22  //
    23  //go:nosplit
    24  //go:norace
    25  //go:nocheckptr
    26  func sigactionCall(noG bool, fn, a0, a1, a2 uintptr) uintptr {
    27  	var f cgoCallFrame
    28  	f.fn, f.a0, f.a1, f.a2 = fn, a0, a1, a2
    29  	if noG {
    30  		runtime_asmcgocall_no_g(cgoCallTrampolinePC(), unsafe.Pointer(&f))
    31  	} else {
    32  		runtime_asmcgocall(cgoCallTrampolinePC(), unsafe.Pointer(&f))
    33  	}
    34  	return f.r1
    35  }
    36  
    37  // restrictMask restricts act.mask, which must have been filled by
    38  // sigfillset, to the signals in gomask. Starting from sigfillset rather
    39  // than sigemptyset keeps the signals reserved by libc (such as glibc's
    40  // SIGCANCEL and SIGSETXID), which sigfillset leaves out and sigaddset in
    41  // gcc_sigaction.c refuses to add, out of the mask.
    42  //
    43  //go:nosplit
    44  //go:norace
    45  func (act *cSigaction) restrictMask(gomask uint64) {
    46  	act.mask[0] &= uintptr(gomask)
    47  	n := 1
    48  	if unsafe.Sizeof(uintptr(0)) == 4 && len(act.mask) > 1 {
    49  		act.mask[:][1] &= uintptr(gomask >> 32)
    50  		n = 2
    51  	}
    52  	for i := n; i < len(act.mask); i++ {
    53  		act.mask[i] = 0
    54  	}
    55  }
    56  
    57  //go:nosplit
    58  //go:norace
    59  //go:nocheckptr
    60  func cgoSigaction(signum uintptr, goact, oldgoact *goSigaction, noG bool) int32 {
    61  	// SA_RESTORER is part of the kernel interface.
    62  	// This is Linux i386/amd64 specific.
    63  	// See the comment on fixSigactionForCgo.
    64  	const _SA_RESTORER = 0x4000000
    65  
    66  	tsanAcquireMode(noG)
    67  
    68  	var oldact cSigaction
    69  	var actPtr, oldactPtr uintptr
    70  
    71  	if goact != nil {
    72  		var act cSigaction
    73  		act.handler = goact.handler
    74  		sigactionCall(noG, uintptr(libc_sigfillset), uintptr(unsafe.Pointer(&act.mask)), 0, 0)
    75  		act.restrictMask(goact.mask)
    76  		act.flags = int32(goact.flags &^ _SA_RESTORER)
    77  		msanUnpoisonMode(noG, unsafe.Pointer(&act), unsafe.Sizeof(act))
    78  		actPtr = uintptr(unsafe.Pointer(&act))
    79  	}
    80  	if oldgoact != nil {
    81  		oldactPtr = uintptr(unsafe.Pointer(&oldact))
    82  	}
    83  
    84  	ret := int32(sigactionCall(noG, uintptr(libc_sigaction), uintptr(int32(signum)), actPtr, oldactPtr))
    85  	if ret == -1 {
    86  		// runtime.rt_sigaction expects _cgo_sigaction to return errno on error.
    87  		errnoPtr := sigactionCall(noG, uintptr(libc_errno_location), 0, 0, 0)
    88  		errno := *(*int32)(unsafe.Pointer(errnoPtr))
    89  		tsanReleaseMode(noG)
    90  		return errno
    91  	}
    92  
    93  	if oldgoact != nil {
    94  		oldgoact.handler = oldact.handler
    95  		oldgoact.mask = uint64(oldact.mask[0])
    96  		if unsafe.Sizeof(uintptr(0)) == 4 && len(oldact.mask) > 1 {
    97  			oldgoact.mask |= uint64(oldact.mask[:][1]) << 32
    98  		}
    99  		oldgoact.flags = uintptr(uint32(oldact.flags))
   100  		msanUnpoisonMode(noG, unsafe.Pointer(oldgoact), unsafe.Sizeof(*oldgoact))
   101  	}
   102  
   103  	tsanReleaseMode(noG)
   104  	return ret
   105  }
   106  

View as plain text