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.
5 // Support for memory sanitizer. See runtime/cgo/mmap.go.
7 //go:build (linux && (amd64 || arm64 || loong64)) || (freebsd && amd64)
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.
16 //go:linkname _cgo_mmap _cgo_mmap
17 var _cgo_mmap unsafe.Pointer
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.
22 //go:linkname _cgo_munmap _cgo_munmap
23 var _cgo_munmap unsafe.Pointer
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.
31 func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (unsafe.Pointer, int) {
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
40 ret = callCgoMmap(addr, n, prot, flags, fd, off)
45 return unsafe.Pointer(ret), 0
47 return sysMmap(addr, n, prot, flags, fd, off)
50 func munmap(addr unsafe.Pointer, n uintptr) {
51 if _cgo_munmap != nil {
52 systemstack(func() { callCgoMunmap(addr, n) })
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)
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
65 // sysMunmap calls the munmap system call. It is implemented in assembly.
66 func sysMunmap(addr unsafe.Pointer, n uintptr)
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)