Source file src/runtime/cgo_mmap.go

     1  // Copyright 2015 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/mmap.go.
     6  
     7  //go:build (linux && (amd64 || arm64 || loong64)) || (freebsd && amd64)
     8  
     9  package runtime
    10  
    11  import "unsafe"
    12  
    13  // _cgo_mmap is filled in by runtime/cgo when it is linked into the
    14  // program, so it is only non-nil when using cgo.
    15  //
    16  //go:linkname _cgo_mmap _cgo_mmap
    17  var _cgo_mmap unsafe.Pointer
    18  
    19  // _cgo_munmap is filled in by runtime/cgo when it is linked into the
    20  // program, so it is only non-nil when using cgo.
    21  //
    22  //go:linkname _cgo_munmap _cgo_munmap
    23  var _cgo_munmap unsafe.Pointer
    24  
    25  // mmap is used to route the mmap system call through C code when using cgo, to
    26  // support sanitizer interceptors. Don't allow stack splits, since this function
    27  // (used by sysAlloc) is called in a lot of low-level parts of the runtime and
    28  // callers often assume it won't acquire any locks.
    29  //
    30  //go:nosplit
    31  func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (unsafe.Pointer, int) {
    32  	if _cgo_mmap != nil {
    33  		// Make ret a uintptr so that writing to it in the
    34  		// function literal does not trigger a write barrier.
    35  		// A write barrier here could break because of the way
    36  		// that mmap uses the same value both as a pointer and
    37  		// an errno value.
    38  		var ret uintptr
    39  		systemstack(func() {
    40  			ret = callCgoMmap(addr, n, prot, flags, fd, off)
    41  		})
    42  		if ret < 4096 {
    43  			return nil, int(ret)
    44  		}
    45  		return unsafe.Pointer(ret), 0
    46  	}
    47  	return sysMmap(addr, n, prot, flags, fd, off)
    48  }
    49  
    50  func munmap(addr unsafe.Pointer, n uintptr) {
    51  	if _cgo_munmap != nil {
    52  		systemstack(func() { callCgoMunmap(addr, n) })
    53  		return
    54  	}
    55  	sysMunmap(addr, n)
    56  }
    57  
    58  // sysMmap calls the mmap system call. It is implemented in assembly.
    59  func sysMmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (p unsafe.Pointer, err int)
    60  
    61  // callCgoMmap calls the mmap function in the runtime/cgo package
    62  // using the GCC calling convention. It is implemented in assembly.
    63  func callCgoMmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) uintptr
    64  
    65  // sysMunmap calls the munmap system call. It is implemented in assembly.
    66  func sysMunmap(addr unsafe.Pointer, n uintptr)
    67  
    68  // callCgoMunmap calls the munmap function in the runtime/cgo package
    69  // using the GCC calling convention. It is implemented in assembly.
    70  func callCgoMunmap(addr unsafe.Pointer, n uintptr)
    71  

View as plain text