Source file src/runtime/sys_darwin.go

     1  // Copyright 2018 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  package runtime
     6  
     7  import (
     8  	"internal/abi"
     9  	"internal/runtime/atomic"
    10  	"unsafe"
    11  )
    12  
    13  //go:nosplit
    14  func libcError() uintptr {
    15  	errPtr, _ := syscall(abi.FuncPCABI0(libc_error_trampoline), 0, 0, 0)
    16  	return errPtr
    17  }
    18  func libc_error_trampoline()
    19  
    20  // The X versions of syscall expect the libc call to return a 64-bit result.
    21  // Otherwise (the non-X version) expects a 32-bit result.
    22  // This distinction is required because an error is indicated by returning -1,
    23  // and we need to know whether to check 32 or 64 bits of the result.
    24  // (Some libc functions that return 32 bits put junk in the upper 32 bits of AX.)
    25  
    26  //go:nosplit
    27  func syscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr) {
    28  	args := struct{ fn, a1, a2, a3, r1, r2 uintptr }{fn, a1, a2, a3, r1, r2}
    29  	libcCall(unsafe.Pointer(abi.FuncPCABI0(syscall_trampoline)), unsafe.Pointer(&args))
    30  	return args.r1, args.r2
    31  }
    32  func syscall_trampoline()
    33  
    34  // golang.org/x/sys linknames syscall_syscall
    35  // (in addition to standard package syscall).
    36  // Do not remove or change the type signature.
    37  //
    38  //go:linkname syscall_syscall syscall.syscall
    39  //go:nosplit
    40  func syscall_syscall(fn, a1, a2, a3 uintptr) (r1, r2, err uintptr) {
    41  	entersyscall()
    42  	r1, r2, err = syscall_rawSyscall(fn, a1, a2, a3)
    43  	exitsyscall()
    44  	return r1, r2, err
    45  }
    46  
    47  //go:linkname syscall_syscallX syscall.syscallX
    48  //go:nosplit
    49  func syscall_syscallX(fn, a1, a2, a3 uintptr) (r1, r2, err uintptr) {
    50  	entersyscall()
    51  	r1, r2, err = syscall_rawSyscallX(fn, a1, a2, a3)
    52  	exitsyscall()
    53  	return r1, r2, err
    54  }
    55  
    56  //go:nosplit
    57  func syscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr) {
    58  	args := struct{ fn, a1, a2, a3, a4, a5, a6, r1, r2 uintptr }{fn, a1, a2, a3, a4, a5, a6, r1, r2}
    59  	libcCall(unsafe.Pointer(abi.FuncPCABI0(syscall6_trampoline)), unsafe.Pointer(&args))
    60  	return args.r1, args.r2
    61  }
    62  func syscall6_trampoline()
    63  
    64  // golang.org/x/sys linknames syscall.syscall6
    65  // (in addition to standard package syscall).
    66  // Do not remove or change the type signature.
    67  //
    68  // syscall.syscall6 is meant for package syscall (and x/sys),
    69  // but widely used packages access it using linkname.
    70  // Notable members of the hall of shame include:
    71  //   - github.com/tetratelabs/wazero
    72  //
    73  // See go.dev/issue/67401.
    74  //
    75  //go:linkname syscall_syscall6 syscall.syscall6
    76  //go:nosplit
    77  func syscall_syscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr) {
    78  	entersyscall()
    79  	r1, r2, err = syscall_rawSyscall6(fn, a1, a2, a3, a4, a5, a6)
    80  	exitsyscall()
    81  	return r1, r2, err
    82  }
    83  
    84  //go:linkname syscall_syscall6X syscall.syscall6X
    85  //go:nosplit
    86  func syscall_syscall6X(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr) {
    87  	entersyscall()
    88  	r1, r2, err = syscall_rawSyscall6X(fn, a1, a2, a3, a4, a5, a6)
    89  	exitsyscall()
    90  	return r1, r2, err
    91  }
    92  
    93  //go:nosplit
    94  func syscall9(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr) {
    95  	args := struct{ fn, a1, a2, a3, a4, a5, a6, a7, a8, a9, r1, r2 uintptr }{fn, a1, a2, a3, a4, a5, a6, a7, a8, a9, r1, r2}
    96  	libcCall(unsafe.Pointer(abi.FuncPCABI0(syscall9_trampoline)), unsafe.Pointer(&args))
    97  	return args.r1, args.r2
    98  }
    99  func syscall9_trampoline()
   100  
   101  // golang.org/x/sys linknames syscall.syscall9
   102  // (in addition to standard package syscall).
   103  // Do not remove or change the type signature.
   104  //
   105  //go:linkname syscall_syscall9 syscall.syscall9
   106  //go:nosplit
   107  func syscall_syscall9(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2, err uintptr) {
   108  	entersyscall()
   109  	r1, r2, err = syscall_rawSyscall9(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9)
   110  	exitsyscall()
   111  	return r1, r2, err
   112  }
   113  
   114  // golang.org/x/sys linknames syscall.syscallPtr
   115  // (in addition to standard package syscall).
   116  // Do not remove or change the type signature.
   117  //
   118  //go:linkname syscall_syscallPtr syscall.syscallPtr
   119  //go:nosplit
   120  func syscall_syscallPtr(fn, a1, a2, a3 uintptr) (r1, r2, err uintptr) {
   121  	entersyscall()
   122  	r1, r2, err = syscall_rawSyscallPtr(fn, a1, a2, a3)
   123  	exitsyscall()
   124  	return r1, r2, err
   125  }
   126  
   127  // golang.org/x/sys linknames syscall_rawSyscall
   128  // (in addition to standard package syscall).
   129  // Do not remove or change the type signature.
   130  //
   131  //go:linkname syscall_rawSyscall syscall.rawSyscall
   132  //go:nosplit
   133  func syscall_rawSyscall(fn, a1, a2, a3 uintptr) (r1, r2, err uintptr) {
   134  	r1, r2 = syscall(fn, a1, a2, a3)
   135  	// Check if r1 low 32 bits is -1, indicating an error.
   136  	if int32(r1) == -1 {
   137  		err = libcError()
   138  	}
   139  	return r1, r2, err
   140  }
   141  
   142  //go:nosplit
   143  func syscall_rawSyscallX(fn, a1, a2, a3 uintptr) (r1, r2, err uintptr) {
   144  	r1, r2 = syscall(fn, a1, a2, a3)
   145  	if r1 == ^uintptr(0) {
   146  		err = libcError()
   147  	}
   148  	return r1, r2, err
   149  }
   150  
   151  //go:nosplit
   152  func syscall_rawSyscallPtr(fn, a1, a2, a3 uintptr) (r1, r2, err uintptr) {
   153  	r1, r2 = syscall(fn, a1, a2, a3)
   154  	if r1 == 0 {
   155  		err = libcError()
   156  	}
   157  	return r1, r2, err
   158  }
   159  
   160  // golang.org/x/sys linknames syscall_rawSyscall6
   161  // (in addition to standard package syscall).
   162  // Do not remove or change the type signature.
   163  //
   164  //go:linkname syscall_rawSyscall6 syscall.rawSyscall6
   165  //go:nosplit
   166  func syscall_rawSyscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr) {
   167  	r1, r2 = syscall6(fn, a1, a2, a3, a4, a5, a6)
   168  	// Check if r1 low 32 bits is -1, indicating an error.
   169  	if int32(r1) == -1 {
   170  		err = libcError()
   171  	}
   172  	return r1, r2, err
   173  }
   174  
   175  //go:nosplit
   176  func syscall_rawSyscall6X(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr) {
   177  	r1, r2 = syscall6(fn, a1, a2, a3, a4, a5, a6)
   178  	if r1 == ^uintptr(0) {
   179  		err = libcError()
   180  	}
   181  	return r1, r2, err
   182  }
   183  
   184  //go:nosplit
   185  func syscall_rawSyscall9(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2, err uintptr) {
   186  	r1, r2 = syscall9(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9)
   187  	// Check if r1 low 32 bits is -1, indicating an error.
   188  	if int32(r1) == -1 {
   189  		err = libcError()
   190  	}
   191  	return r1, r2, err
   192  }
   193  
   194  // crypto_x509_syscall is used in crypto/x509/internal/macos to call into Security.framework and CF.
   195  
   196  //go:linkname crypto_x509_syscall crypto/x509/internal/macos.syscall
   197  //go:nosplit
   198  func crypto_x509_syscall(fn, a1, a2, a3, a4, a5 uintptr, f1 float64) (r1 uintptr) {
   199  	args := struct {
   200  		fn, a1, a2, a3, a4, a5 uintptr
   201  		f1                     float64
   202  		r1                     uintptr
   203  	}{fn, a1, a2, a3, a4, a5, f1, r1}
   204  	entersyscall()
   205  	libcCall(unsafe.Pointer(abi.FuncPCABI0(syscall_x509)), unsafe.Pointer(&args))
   206  	exitsyscall()
   207  	return args.r1
   208  }
   209  func syscall_x509()
   210  
   211  // The *_trampoline functions convert from the Go calling convention to the C calling convention
   212  // and then call the underlying libc function.  They are defined in sys_darwin_$ARCH.s.
   213  
   214  //go:nosplit
   215  //go:cgo_unsafe_args
   216  func pthread_attr_init(attr *pthreadattr) int32 {
   217  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_attr_init_trampoline)), unsafe.Pointer(&attr))
   218  	KeepAlive(attr)
   219  	return ret
   220  }
   221  func pthread_attr_init_trampoline()
   222  
   223  //go:nosplit
   224  //go:cgo_unsafe_args
   225  func pthread_attr_getstacksize(attr *pthreadattr, size *uintptr) int32 {
   226  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_attr_getstacksize_trampoline)), unsafe.Pointer(&attr))
   227  	KeepAlive(attr)
   228  	KeepAlive(size)
   229  	return ret
   230  }
   231  func pthread_attr_getstacksize_trampoline()
   232  
   233  //go:nosplit
   234  //go:cgo_unsafe_args
   235  func pthread_attr_setdetachstate(attr *pthreadattr, state int) int32 {
   236  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_attr_setdetachstate_trampoline)), unsafe.Pointer(&attr))
   237  	KeepAlive(attr)
   238  	return ret
   239  }
   240  func pthread_attr_setdetachstate_trampoline()
   241  
   242  //go:nosplit
   243  //go:cgo_unsafe_args
   244  func pthread_create(attr *pthreadattr, start uintptr, arg unsafe.Pointer) int32 {
   245  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_create_trampoline)), unsafe.Pointer(&attr))
   246  	KeepAlive(attr)
   247  	KeepAlive(arg) // Just for consistency. Arg of course needs to be kept alive for the start function.
   248  	return ret
   249  }
   250  func pthread_create_trampoline()
   251  
   252  //go:nosplit
   253  //go:cgo_unsafe_args
   254  func raise(sig uint32) {
   255  	libcCall(unsafe.Pointer(abi.FuncPCABI0(raise_trampoline)), unsafe.Pointer(&sig))
   256  }
   257  func raise_trampoline()
   258  
   259  //go:nosplit
   260  //go:cgo_unsafe_args
   261  func pthread_self() (t pthread) {
   262  	libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_self_trampoline)), unsafe.Pointer(&t))
   263  	return
   264  }
   265  func pthread_self_trampoline()
   266  
   267  //go:nosplit
   268  //go:cgo_unsafe_args
   269  func pthread_kill(t pthread, sig uint32) {
   270  	libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_kill_trampoline)), unsafe.Pointer(&t))
   271  	return
   272  }
   273  func pthread_kill_trampoline()
   274  
   275  // osinit_hack is a clumsy hack to work around Apple libc bugs
   276  // causing fork+exec to hang in the child process intermittently.
   277  // See go.dev/issue/33565 and go.dev/issue/56784 for a few reports.
   278  //
   279  // The stacks obtained from the hung child processes are in
   280  // libSystem_atfork_child, which is supposed to reinitialize various
   281  // parts of the C library in the new process.
   282  //
   283  // One common stack dies in _notify_fork_child calling _notify_globals
   284  // (inlined) calling _os_alloc_once, because _os_alloc_once detects that
   285  // the once lock is held by the parent process and then calls
   286  // _os_once_gate_corruption_abort. The allocation is setting up the
   287  // globals for the notification subsystem. See the source code at [1].
   288  // To work around this, we can allocate the globals earlier in the Go
   289  // program's lifetime, before any execs are involved, by calling any
   290  // notify routine that is exported, calls _notify_globals, and doesn't do
   291  // anything too expensive otherwise. notify_is_valid_token(0) fits the bill.
   292  //
   293  // The other common stack dies in xpc_atfork_child calling
   294  // _objc_msgSend_uncached which ends up in
   295  // WAITING_FOR_ANOTHER_THREAD_TO_FINISH_CALLING_+initialize. Of course,
   296  // whatever thread the child is waiting for is in the parent process and
   297  // is not going to finish anything in the child process. There is no
   298  // public source code for these routines, so it is unclear exactly what
   299  // the problem is. An Apple engineer suggests using xpc_date_create_from_current,
   300  // which empirically does fix the problem.
   301  //
   302  // So osinit_hack_trampoline (in sys_darwin_$GOARCH.s) calls
   303  // notify_is_valid_token(0) and xpc_date_create_from_current(), which makes the
   304  // fork+exec hangs stop happening. If Apple fixes the libc bug in
   305  // some future version of macOS, then we can remove this awful code.
   306  //
   307  //go:nosplit
   308  func osinit_hack() {
   309  	if GOOS == "darwin" { // not ios
   310  		libcCall(unsafe.Pointer(abi.FuncPCABI0(osinit_hack_trampoline)), nil)
   311  	}
   312  	return
   313  }
   314  func osinit_hack_trampoline()
   315  
   316  // mmap is used to do low-level memory allocation via mmap. Don't allow stack
   317  // splits, since this function (used by sysAlloc) is called in a lot of low-level
   318  // parts of the runtime and callers often assume it won't acquire any locks.
   319  //
   320  //go:nosplit
   321  func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (unsafe.Pointer, int) {
   322  	args := struct {
   323  		addr            unsafe.Pointer
   324  		n               uintptr
   325  		prot, flags, fd int32
   326  		off             uint32
   327  		ret1            unsafe.Pointer
   328  		ret2            int
   329  	}{addr, n, prot, flags, fd, off, nil, 0}
   330  	libcCall(unsafe.Pointer(abi.FuncPCABI0(mmap_trampoline)), unsafe.Pointer(&args))
   331  	return args.ret1, args.ret2
   332  }
   333  func mmap_trampoline()
   334  
   335  //go:nosplit
   336  //go:cgo_unsafe_args
   337  func munmap(addr unsafe.Pointer, n uintptr) {
   338  	libcCall(unsafe.Pointer(abi.FuncPCABI0(munmap_trampoline)), unsafe.Pointer(&addr))
   339  	KeepAlive(addr) // Just for consistency. Hopefully addr is not a Go address.
   340  }
   341  func munmap_trampoline()
   342  
   343  //go:nosplit
   344  //go:cgo_unsafe_args
   345  func madvise(addr unsafe.Pointer, n uintptr, flags int32) {
   346  	libcCall(unsafe.Pointer(abi.FuncPCABI0(madvise_trampoline)), unsafe.Pointer(&addr))
   347  	KeepAlive(addr) // Just for consistency. Hopefully addr is not a Go address.
   348  }
   349  func madvise_trampoline()
   350  
   351  //go:nosplit
   352  //go:cgo_unsafe_args
   353  func mlock(addr unsafe.Pointer, n uintptr) {
   354  	libcCall(unsafe.Pointer(abi.FuncPCABI0(mlock_trampoline)), unsafe.Pointer(&addr))
   355  	KeepAlive(addr) // Just for consistency. Hopefully addr is not a Go address.
   356  }
   357  func mlock_trampoline()
   358  
   359  //go:nosplit
   360  //go:cgo_unsafe_args
   361  func read(fd int32, p unsafe.Pointer, n int32) int32 {
   362  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(read_trampoline)), unsafe.Pointer(&fd))
   363  	KeepAlive(p)
   364  	return ret
   365  }
   366  func read_trampoline()
   367  
   368  func pipe() (r, w int32, errno int32) {
   369  	var p [2]int32
   370  	errno = libcCall(unsafe.Pointer(abi.FuncPCABI0(pipe_trampoline)), noescape(unsafe.Pointer(&p)))
   371  	return p[0], p[1], errno
   372  }
   373  func pipe_trampoline()
   374  
   375  //go:nosplit
   376  //go:cgo_unsafe_args
   377  func closefd(fd int32) int32 {
   378  	return libcCall(unsafe.Pointer(abi.FuncPCABI0(close_trampoline)), unsafe.Pointer(&fd))
   379  }
   380  func close_trampoline()
   381  
   382  // This is exported via linkname to assembly in runtime/cgo.
   383  //
   384  //go:nosplit
   385  //go:cgo_unsafe_args
   386  //go:linkname exit
   387  func exit(code int32) {
   388  	libcCall(unsafe.Pointer(abi.FuncPCABI0(exit_trampoline)), unsafe.Pointer(&code))
   389  }
   390  func exit_trampoline()
   391  
   392  //go:nosplit
   393  //go:cgo_unsafe_args
   394  func usleep(usec uint32) {
   395  	libcCall(unsafe.Pointer(abi.FuncPCABI0(usleep_trampoline)), unsafe.Pointer(&usec))
   396  }
   397  func usleep_trampoline()
   398  
   399  //go:nosplit
   400  //go:cgo_unsafe_args
   401  func usleep_no_g(usec uint32) {
   402  	asmcgocall_no_g(unsafe.Pointer(abi.FuncPCABI0(usleep_trampoline)), unsafe.Pointer(&usec))
   403  }
   404  
   405  //go:nosplit
   406  //go:cgo_unsafe_args
   407  func write1(fd uintptr, p unsafe.Pointer, n int32) int32 {
   408  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(write_trampoline)), unsafe.Pointer(&fd))
   409  	KeepAlive(p)
   410  	return ret
   411  }
   412  func write_trampoline()
   413  
   414  //go:nosplit
   415  //go:cgo_unsafe_args
   416  func open(name *byte, mode, perm int32) (ret int32) {
   417  	ret = libcCall(unsafe.Pointer(abi.FuncPCABI0(open_trampoline)), unsafe.Pointer(&name))
   418  	KeepAlive(name)
   419  	return
   420  }
   421  func open_trampoline()
   422  
   423  //go:nosplit
   424  //go:cgo_unsafe_args
   425  func nanotime1() int64 {
   426  	var r struct {
   427  		t            int64  // raw timer
   428  		numer, denom uint32 // conversion factors. nanoseconds = t * numer / denom.
   429  	}
   430  	libcCall(unsafe.Pointer(abi.FuncPCABI0(nanotime_trampoline)), unsafe.Pointer(&r))
   431  	// Note: Apple seems unconcerned about overflow here. See
   432  	// https://developer.apple.com/library/content/qa/qa1398/_index.html
   433  	// Note also, numer == denom == 1 is common.
   434  	t := r.t
   435  	if r.numer != 1 {
   436  		t *= int64(r.numer)
   437  	}
   438  	if r.denom != 1 {
   439  		t /= int64(r.denom)
   440  	}
   441  	return t
   442  }
   443  func nanotime_trampoline()
   444  
   445  // walltime should be an internal detail,
   446  // but widely used packages access it using linkname.
   447  // Notable members of the hall of shame include:
   448  //   - gitee.com/quant1x/gox
   449  //
   450  // Do not remove or change the type signature.
   451  // See go.dev/issue/67401.
   452  //
   453  //go:linkname walltime
   454  //go:nosplit
   455  //go:cgo_unsafe_args
   456  func walltime() (int64, int32) {
   457  	var t timespec
   458  	libcCall(unsafe.Pointer(abi.FuncPCABI0(walltime_trampoline)), unsafe.Pointer(&t))
   459  	return t.tv_sec, int32(t.tv_nsec)
   460  }
   461  func walltime_trampoline()
   462  
   463  //go:nosplit
   464  //go:cgo_unsafe_args
   465  func sigaction(sig uint32, new *usigactiont, old *usigactiont) {
   466  	libcCall(unsafe.Pointer(abi.FuncPCABI0(sigaction_trampoline)), unsafe.Pointer(&sig))
   467  	KeepAlive(new)
   468  	KeepAlive(old)
   469  }
   470  func sigaction_trampoline()
   471  
   472  //go:nosplit
   473  //go:cgo_unsafe_args
   474  func sigprocmask(how uint32, new *sigset, old *sigset) {
   475  	libcCall(unsafe.Pointer(abi.FuncPCABI0(sigprocmask_trampoline)), unsafe.Pointer(&how))
   476  	KeepAlive(new)
   477  	KeepAlive(old)
   478  }
   479  func sigprocmask_trampoline()
   480  
   481  //go:nosplit
   482  //go:cgo_unsafe_args
   483  func sigaltstack(new *stackt, old *stackt) {
   484  	if new != nil && new.ss_flags&_SS_DISABLE != 0 && new.ss_size == 0 {
   485  		// Despite the fact that Darwin's sigaltstack man page says it ignores the size
   486  		// when SS_DISABLE is set, it doesn't. sigaltstack returns ENOMEM
   487  		// if we don't give it a reasonable size.
   488  		// ref: http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20140421/214296.html
   489  		new.ss_size = 32768
   490  	}
   491  	libcCall(unsafe.Pointer(abi.FuncPCABI0(sigaltstack_trampoline)), unsafe.Pointer(&new))
   492  	KeepAlive(new)
   493  	KeepAlive(old)
   494  }
   495  func sigaltstack_trampoline()
   496  
   497  //go:nosplit
   498  //go:cgo_unsafe_args
   499  func raiseproc(sig uint32) {
   500  	libcCall(unsafe.Pointer(abi.FuncPCABI0(raiseproc_trampoline)), unsafe.Pointer(&sig))
   501  }
   502  func raiseproc_trampoline()
   503  
   504  //go:nosplit
   505  //go:cgo_unsafe_args
   506  func setitimer(mode int32, new, old *itimerval) {
   507  	libcCall(unsafe.Pointer(abi.FuncPCABI0(setitimer_trampoline)), unsafe.Pointer(&mode))
   508  	KeepAlive(new)
   509  	KeepAlive(old)
   510  }
   511  func setitimer_trampoline()
   512  
   513  //go:nosplit
   514  //go:cgo_unsafe_args
   515  func sysctl(mib *uint32, miblen uint32, oldp *byte, oldlenp *uintptr, newp *byte, newlen uintptr) int32 {
   516  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(sysctl_trampoline)), unsafe.Pointer(&mib))
   517  	KeepAlive(mib)
   518  	KeepAlive(oldp)
   519  	KeepAlive(oldlenp)
   520  	KeepAlive(newp)
   521  	return ret
   522  }
   523  func sysctl_trampoline()
   524  
   525  //go:nosplit
   526  //go:cgo_unsafe_args
   527  func sysctlbyname(name *byte, oldp *byte, oldlenp *uintptr, newp *byte, newlen uintptr) int32 {
   528  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(sysctlbyname_trampoline)), unsafe.Pointer(&name))
   529  	KeepAlive(name)
   530  	KeepAlive(oldp)
   531  	KeepAlive(oldlenp)
   532  	KeepAlive(newp)
   533  	return ret
   534  }
   535  func sysctlbyname_trampoline()
   536  
   537  //go:nosplit
   538  //go:cgo_unsafe_args
   539  func fcntl(fd, cmd, arg int32) (ret int32, errno int32) {
   540  	args := struct {
   541  		fd, cmd, arg int32
   542  		ret, errno   int32
   543  	}{fd, cmd, arg, 0, 0}
   544  	libcCall(unsafe.Pointer(abi.FuncPCABI0(fcntl_trampoline)), unsafe.Pointer(&args))
   545  	return args.ret, args.errno
   546  }
   547  func fcntl_trampoline()
   548  
   549  //go:nosplit
   550  //go:cgo_unsafe_args
   551  func kqueue() int32 {
   552  	v := libcCall(unsafe.Pointer(abi.FuncPCABI0(kqueue_trampoline)), nil)
   553  	return v
   554  }
   555  func kqueue_trampoline()
   556  
   557  //go:nosplit
   558  //go:cgo_unsafe_args
   559  func kevent(kq int32, ch *keventt, nch int32, ev *keventt, nev int32, ts *timespec) int32 {
   560  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(kevent_trampoline)), unsafe.Pointer(&kq))
   561  	KeepAlive(ch)
   562  	KeepAlive(ev)
   563  	KeepAlive(ts)
   564  	return ret
   565  }
   566  func kevent_trampoline()
   567  
   568  //go:nosplit
   569  //go:cgo_unsafe_args
   570  func pthread_mutex_init(m *pthreadmutex, attr *pthreadmutexattr) int32 {
   571  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_mutex_init_trampoline)), unsafe.Pointer(&m))
   572  	KeepAlive(m)
   573  	KeepAlive(attr)
   574  	return ret
   575  }
   576  func pthread_mutex_init_trampoline()
   577  
   578  //go:nosplit
   579  //go:cgo_unsafe_args
   580  func pthread_mutex_lock(m *pthreadmutex) int32 {
   581  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_mutex_lock_trampoline)), unsafe.Pointer(&m))
   582  	KeepAlive(m)
   583  	return ret
   584  }
   585  func pthread_mutex_lock_trampoline()
   586  
   587  //go:nosplit
   588  //go:cgo_unsafe_args
   589  func pthread_mutex_unlock(m *pthreadmutex) int32 {
   590  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_mutex_unlock_trampoline)), unsafe.Pointer(&m))
   591  	KeepAlive(m)
   592  	return ret
   593  }
   594  func pthread_mutex_unlock_trampoline()
   595  
   596  //go:nosplit
   597  //go:cgo_unsafe_args
   598  func pthread_cond_init(c *pthreadcond, attr *pthreadcondattr) int32 {
   599  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_cond_init_trampoline)), unsafe.Pointer(&c))
   600  	KeepAlive(c)
   601  	KeepAlive(attr)
   602  	return ret
   603  }
   604  func pthread_cond_init_trampoline()
   605  
   606  //go:nosplit
   607  //go:cgo_unsafe_args
   608  func pthread_cond_wait(c *pthreadcond, m *pthreadmutex) int32 {
   609  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_cond_wait_trampoline)), unsafe.Pointer(&c))
   610  	KeepAlive(c)
   611  	KeepAlive(m)
   612  	return ret
   613  }
   614  func pthread_cond_wait_trampoline()
   615  
   616  //go:nosplit
   617  //go:cgo_unsafe_args
   618  func pthread_cond_timedwait_relative_np(c *pthreadcond, m *pthreadmutex, t *timespec) int32 {
   619  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_cond_timedwait_relative_np_trampoline)), unsafe.Pointer(&c))
   620  	KeepAlive(c)
   621  	KeepAlive(m)
   622  	KeepAlive(t)
   623  	return ret
   624  }
   625  func pthread_cond_timedwait_relative_np_trampoline()
   626  
   627  //go:nosplit
   628  //go:cgo_unsafe_args
   629  func pthread_cond_signal(c *pthreadcond) int32 {
   630  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_cond_signal_trampoline)), unsafe.Pointer(&c))
   631  	KeepAlive(c)
   632  	return ret
   633  }
   634  func pthread_cond_signal_trampoline()
   635  
   636  //go:nosplit
   637  //go:cgo_unsafe_args
   638  func arc4random_buf(p unsafe.Pointer, n int32) {
   639  	// arc4random_buf() never fails, per its man page, so it's safe to ignore the return value.
   640  	libcCall(unsafe.Pointer(abi.FuncPCABI0(arc4random_buf_trampoline)), unsafe.Pointer(&p))
   641  	KeepAlive(p)
   642  }
   643  func arc4random_buf_trampoline()
   644  
   645  // Not used on Darwin, but must be defined.
   646  func exitThread(wait *atomic.Uint32) {
   647  	throw("exitThread")
   648  }
   649  
   650  //go:nosplit
   651  func setNonblock(fd int32) {
   652  	flags, _ := fcntl(fd, _F_GETFL, 0)
   653  	if flags != -1 {
   654  		fcntl(fd, _F_SETFL, flags|_O_NONBLOCK)
   655  	}
   656  }
   657  
   658  func issetugid() int32 {
   659  	return libcCall(unsafe.Pointer(abi.FuncPCABI0(issetugid_trampoline)), nil)
   660  }
   661  func issetugid_trampoline()
   662  
   663  // mach_vm_region is used to obtain virtual memory mappings for use by the
   664  // profiling system and is only exported to runtime/pprof. It is restricted
   665  // to obtaining mappings for the current process.
   666  //
   667  //go:linkname mach_vm_region runtime/pprof.mach_vm_region
   668  func mach_vm_region(address, region_size *uint64, info unsafe.Pointer) int32 {
   669  	// kern_return_t mach_vm_region(
   670  	// 	vm_map_read_t target_task,
   671  	// 	mach_vm_address_t *address,
   672  	// 	mach_vm_size_t *size,
   673  	// 	vm_region_flavor_t flavor,
   674  	// 	vm_region_info_t info,
   675  	// 	mach_msg_type_number_t *infoCnt,
   676  	// 	mach_port_t *object_name);
   677  	var count machMsgTypeNumber = _VM_REGION_BASIC_INFO_COUNT_64
   678  	var object_name machPort
   679  	args := struct {
   680  		address     *uint64
   681  		size        *uint64
   682  		flavor      machVMRegionFlavour
   683  		info        unsafe.Pointer
   684  		count       *machMsgTypeNumber
   685  		object_name *machPort
   686  	}{
   687  		address:     address,
   688  		size:        region_size,
   689  		flavor:      _VM_REGION_BASIC_INFO_64,
   690  		info:        info,
   691  		count:       &count,
   692  		object_name: &object_name,
   693  	}
   694  	return libcCall(unsafe.Pointer(abi.FuncPCABI0(mach_vm_region_trampoline)), unsafe.Pointer(&args))
   695  }
   696  func mach_vm_region_trampoline()
   697  
   698  //go:linkname proc_regionfilename runtime/pprof.proc_regionfilename
   699  func proc_regionfilename(pid int, address uint64, buf *byte, buflen int64) int32 {
   700  	args := struct {
   701  		pid     int
   702  		address uint64
   703  		buf     *byte
   704  		bufSize int64
   705  	}{
   706  		pid:     pid,
   707  		address: address,
   708  		buf:     buf,
   709  		bufSize: buflen,
   710  	}
   711  	return libcCall(unsafe.Pointer(abi.FuncPCABI0(proc_regionfilename_trampoline)), unsafe.Pointer(&args))
   712  }
   713  func proc_regionfilename_trampoline()
   714  
   715  // Tell the linker that the libc_* functions are to be found
   716  // in a system library, with the libc_ prefix missing.
   717  
   718  //go:cgo_import_dynamic libc_pthread_attr_init pthread_attr_init "/usr/lib/libSystem.B.dylib"
   719  //go:cgo_import_dynamic libc_pthread_attr_getstacksize pthread_attr_getstacksize "/usr/lib/libSystem.B.dylib"
   720  //go:cgo_import_dynamic libc_pthread_attr_setdetachstate pthread_attr_setdetachstate "/usr/lib/libSystem.B.dylib"
   721  //go:cgo_import_dynamic libc_pthread_create pthread_create "/usr/lib/libSystem.B.dylib"
   722  //go:cgo_import_dynamic libc_pthread_self pthread_self "/usr/lib/libSystem.B.dylib"
   723  //go:cgo_import_dynamic libc_pthread_kill pthread_kill "/usr/lib/libSystem.B.dylib"
   724  //go:cgo_import_dynamic libc_exit _exit "/usr/lib/libSystem.B.dylib"
   725  //go:cgo_import_dynamic libc_raise raise "/usr/lib/libSystem.B.dylib"
   726  
   727  //go:cgo_import_dynamic libc_open open "/usr/lib/libSystem.B.dylib"
   728  //go:cgo_import_dynamic libc_close close "/usr/lib/libSystem.B.dylib"
   729  //go:cgo_import_dynamic libc_read read "/usr/lib/libSystem.B.dylib"
   730  //go:cgo_import_dynamic libc_write write "/usr/lib/libSystem.B.dylib"
   731  //go:cgo_import_dynamic libc_pipe pipe "/usr/lib/libSystem.B.dylib"
   732  
   733  //go:cgo_import_dynamic libc_mmap mmap "/usr/lib/libSystem.B.dylib"
   734  //go:cgo_import_dynamic libc_munmap munmap "/usr/lib/libSystem.B.dylib"
   735  //go:cgo_import_dynamic libc_madvise madvise "/usr/lib/libSystem.B.dylib"
   736  //go:cgo_import_dynamic libc_mlock mlock "/usr/lib/libSystem.B.dylib"
   737  //go:cgo_import_dynamic libc_error __error "/usr/lib/libSystem.B.dylib"
   738  //go:cgo_import_dynamic libc_usleep usleep "/usr/lib/libSystem.B.dylib"
   739  
   740  //go:cgo_import_dynamic libc_proc_regionfilename proc_regionfilename "/usr/lib/libSystem.B.dylib"
   741  //go:cgo_import_dynamic libc_mach_task_self_ mach_task_self_ "/usr/lib/libSystem.B.dylib""
   742  //go:cgo_import_dynamic libc_mach_vm_region mach_vm_region "/usr/lib/libSystem.B.dylib""
   743  //go:cgo_import_dynamic libc_mach_timebase_info mach_timebase_info "/usr/lib/libSystem.B.dylib"
   744  //go:cgo_import_dynamic libc_mach_absolute_time mach_absolute_time "/usr/lib/libSystem.B.dylib"
   745  //go:cgo_import_dynamic libc_clock_gettime clock_gettime "/usr/lib/libSystem.B.dylib"
   746  //go:cgo_import_dynamic libc_sigaction sigaction "/usr/lib/libSystem.B.dylib"
   747  //go:cgo_import_dynamic libc_pthread_sigmask pthread_sigmask "/usr/lib/libSystem.B.dylib"
   748  //go:cgo_import_dynamic libc_sigaltstack sigaltstack "/usr/lib/libSystem.B.dylib"
   749  //go:cgo_import_dynamic libc_getpid getpid "/usr/lib/libSystem.B.dylib"
   750  //go:cgo_import_dynamic libc_kill kill "/usr/lib/libSystem.B.dylib"
   751  //go:cgo_import_dynamic libc_setitimer setitimer "/usr/lib/libSystem.B.dylib"
   752  //go:cgo_import_dynamic libc_sysctl sysctl "/usr/lib/libSystem.B.dylib"
   753  //go:cgo_import_dynamic libc_sysctlbyname sysctlbyname "/usr/lib/libSystem.B.dylib"
   754  //go:cgo_import_dynamic libc_fcntl fcntl "/usr/lib/libSystem.B.dylib"
   755  //go:cgo_import_dynamic libc_kqueue kqueue "/usr/lib/libSystem.B.dylib"
   756  //go:cgo_import_dynamic libc_kevent kevent "/usr/lib/libSystem.B.dylib"
   757  
   758  //go:cgo_import_dynamic libc_pthread_mutex_init pthread_mutex_init "/usr/lib/libSystem.B.dylib"
   759  //go:cgo_import_dynamic libc_pthread_mutex_lock pthread_mutex_lock "/usr/lib/libSystem.B.dylib"
   760  //go:cgo_import_dynamic libc_pthread_mutex_unlock pthread_mutex_unlock "/usr/lib/libSystem.B.dylib"
   761  //go:cgo_import_dynamic libc_pthread_cond_init pthread_cond_init "/usr/lib/libSystem.B.dylib"
   762  //go:cgo_import_dynamic libc_pthread_cond_wait pthread_cond_wait "/usr/lib/libSystem.B.dylib"
   763  //go:cgo_import_dynamic libc_pthread_cond_timedwait_relative_np pthread_cond_timedwait_relative_np "/usr/lib/libSystem.B.dylib"
   764  //go:cgo_import_dynamic libc_pthread_cond_signal pthread_cond_signal "/usr/lib/libSystem.B.dylib"
   765  //go:cgo_import_dynamic libc_arc4random_buf arc4random_buf "/usr/lib/libSystem.B.dylib"
   766  
   767  //go:cgo_import_dynamic libc_notify_is_valid_token notify_is_valid_token "/usr/lib/libSystem.B.dylib"
   768  //go:cgo_import_dynamic libc_xpc_date_create_from_current xpc_date_create_from_current "/usr/lib/libSystem.B.dylib"
   769  
   770  //go:cgo_import_dynamic libc_issetugid issetugid "/usr/lib/libSystem.B.dylib"
   771  

View as plain text