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