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

View as plain text