Source file src/runtime/cgo/cgo_linux.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
     6  
     7  package cgo
     8  
     9  import (
    10  	"internal/goarch"
    11  	"unsafe"
    12  )
    13  
    14  const (
    15  	// SIG_SETMASK is 3 on MIPS Linux and 2 on all other Linux architectures.
    16  	_SIG_SETMASK             = 2 + (goarch.IsMips | goarch.IsMipsle | goarch.IsMips64 | goarch.IsMips64le)
    17  	_EAGAIN                  = 11
    18  	_PTHREAD_CREATE_DETACHED = 1
    19  )
    20  
    21  //go:linkname _cgo_libc_errno_location _cgo_libc_errno_location
    22  var _cgo_libc_errno_location byte
    23  var libc_errno_location = unsafe.Pointer(&_cgo_libc_errno_location)
    24  
    25  //go:linkname _cgo_libc_clearenv _cgo_libc_clearenv
    26  var _cgo_libc_clearenv byte
    27  var libc_clearenv = unsafe.Pointer(&_cgo_libc_clearenv)
    28  
    29  //go:linkname _cgo_libc_setegid _cgo_libc_setegid
    30  //go:linkname cgo_libc_setegid syscall.cgo_libc_setegid
    31  var _cgo_libc_setegid byte
    32  var cgo_libc_setegid = unsafe.Pointer(&_cgo_libc_setegid)
    33  
    34  //go:linkname _cgo_libc_seteuid _cgo_libc_seteuid
    35  //go:linkname cgo_libc_seteuid syscall.cgo_libc_seteuid
    36  var _cgo_libc_seteuid byte
    37  var cgo_libc_seteuid = unsafe.Pointer(&_cgo_libc_seteuid)
    38  
    39  //go:linkname _cgo_libc_setgid _cgo_libc_setgid
    40  //go:linkname cgo_libc_setgid syscall.cgo_libc_setgid
    41  var _cgo_libc_setgid byte
    42  var cgo_libc_setgid = unsafe.Pointer(&_cgo_libc_setgid)
    43  
    44  //go:linkname _cgo_libc_setgroups _cgo_libc_setgroups
    45  //go:linkname cgo_libc_setgroups syscall.cgo_libc_setgroups
    46  var _cgo_libc_setgroups byte
    47  var cgo_libc_setgroups = unsafe.Pointer(&_cgo_libc_setgroups)
    48  
    49  //go:linkname _cgo_libc_setregid _cgo_libc_setregid
    50  //go:linkname cgo_libc_setregid syscall.cgo_libc_setregid
    51  var _cgo_libc_setregid byte
    52  var cgo_libc_setregid = unsafe.Pointer(&_cgo_libc_setregid)
    53  
    54  //go:linkname _cgo_libc_setresgid _cgo_libc_setresgid
    55  //go:linkname cgo_libc_setresgid syscall.cgo_libc_setresgid
    56  var _cgo_libc_setresgid byte
    57  var cgo_libc_setresgid = unsafe.Pointer(&_cgo_libc_setresgid)
    58  
    59  //go:linkname _cgo_libc_setresuid _cgo_libc_setresuid
    60  //go:linkname cgo_libc_setresuid syscall.cgo_libc_setresuid
    61  var _cgo_libc_setresuid byte
    62  var cgo_libc_setresuid = unsafe.Pointer(&_cgo_libc_setresuid)
    63  
    64  //go:linkname _cgo_libc_setreuid _cgo_libc_setreuid
    65  //go:linkname cgo_libc_setreuid syscall.cgo_libc_setreuid
    66  var _cgo_libc_setreuid byte
    67  var cgo_libc_setreuid = unsafe.Pointer(&_cgo_libc_setreuid)
    68  
    69  //go:linkname _cgo_libc_setuid _cgo_libc_setuid
    70  //go:linkname cgo_libc_setuid syscall.cgo_libc_setuid
    71  var _cgo_libc_setuid byte
    72  var cgo_libc_setuid = unsafe.Pointer(&_cgo_libc_setuid)
    73  
    74  //go:linknamestd _cgoClearenv runtime.cgoClearenv
    75  var _cgoClearenv = cgoClearenv
    76  
    77  //go:norace
    78  func cgoClearenv() {
    79  	tsanAcquire()
    80  	asmcgocall(uintptr(libc_clearenv), 0, 0, 0, 0, 0, 0)
    81  	tsanRelease()
    82  }
    83  
    84  //go:linknamestd _cgoSyscall runtime.cgoSyscall
    85  var _cgoSyscall = cgoSyscall
    86  
    87  //go:nosplit
    88  //go:norace
    89  //go:nocheckptr
    90  func cgoSyscall(fn unsafe.Pointer, a0, a1, a2 uintptr) uintptr {
    91  	if fn != cgo_libc_setgroups {
    92  		// The other functions take only uid_t or gid_t arguments.
    93  		a0, a1, a2 = cUint32Arg(a0), cUint32Arg(a1), cUint32Arg(a2)
    94  	}
    95  	r := int32(cgocall(uintptr(fn), a0, a1, a2, 0, 0, 0))
    96  	if r == -1 {
    97  		errnoPtr := asmcgocall(uintptr(libc_errno_location), 0, 0, 0, 0, 0, 0)
    98  		return uintptr(*(*int32)(unsafe.Pointer(errnoPtr)))
    99  	}
   100  	return uintptr(int(r))
   101  }
   102  
   103  // cUint32Arg converts x, holding a 32-bit unsigned C argument such as a
   104  // uid_t, to the register representation required by the C ABI. Callers
   105  // pass values like uintptr(-1), which must not leak into the upper bits:
   106  // 64-bit RISC-V, LoongArch, and MIPS require 32-bit values to be
   107  // sign-extended, while the other 64-bit ABIs zero-extend them (or ignore
   108  // the upper bits).
   109  //
   110  //go:nosplit
   111  func cUint32Arg(x uintptr) uintptr {
   112  	if goarch.IsRiscv64|goarch.IsLoong64|goarch.IsMips64|goarch.IsMips64le != 0 {
   113  		return uintptr(int32(x))
   114  	}
   115  	return uintptr(uint32(x))
   116  }
   117  

View as plain text