diff options
author | Ian Lance Taylor <iant@golang.org> | 2015-09-29 21:24:13 -0700 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2015-09-30 22:17:55 +0000 |
commit | 0c1f0549b893d9ddaab42a7765ba82234fbcc10d (patch) | |
tree | 6f0e6089d8bdac64096243e33ad7e773f17443a0 /src/runtime/cgo_mmap.go | |
parent | b72a4a07c257f394f5a96dff29a4fc862601a41f (diff) | |
download | go-0c1f0549b893d9ddaab42a7765ba82234fbcc10d.tar.gz go-0c1f0549b893d9ddaab42a7765ba82234fbcc10d.zip |
runtime, runtime/cgo: support using msan on cgo code
The memory sanitizer (msan) is a nice compiler feature that can
dynamically check for memory errors in C code. It's not useful for Go
code, since Go is memory safe. But it is useful to be able to use the
memory sanitizer on C code that is linked into a Go program via cgo.
Without this change it does not work, as msan considers memory passed
from Go to C as uninitialized.
To make this work, change the runtime to call the C mmap function when
using cgo. When using msan the mmap call will be intercepted and marked
as returning initialized memory.
Work around what appears to be an msan bug by calling malloc before we
call mmap.
Change-Id: I8ab7286d7595ae84782f68a98bef6d3688b946f9
Reviewed-on: https://go-review.googlesource.com/15170
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
Diffstat (limited to 'src/runtime/cgo_mmap.go')
-rw-r--r-- | src/runtime/cgo_mmap.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/runtime/cgo_mmap.go b/src/runtime/cgo_mmap.go new file mode 100644 index 0000000000..ef5501ca5f --- /dev/null +++ b/src/runtime/cgo_mmap.go @@ -0,0 +1,34 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Support for memory sanitizer. See runtime/cgo/mmap.go. + +// +build linux,amd64 + +package runtime + +import "unsafe" + +// _cgo_mmap is filled in by runtime/cgo when it is linked into the +// program, so it is only non-nil when using cgo. +//go:linkname _cgo_mmap _cgo_mmap +var _cgo_mmap unsafe.Pointer + +func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (ret unsafe.Pointer) { + if _cgo_mmap != nil { + systemstack(func() { + ret = callCgoMmap(addr, n, prot, flags, fd, off) + }) + return + } + return sysMmap(addr, n, prot, flags, fd, off) +} + +// sysMmap calls the mmap system call. It is implemented in assembly. +func sysMmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) unsafe.Pointer + +// cgoMmap calls the mmap function in the runtime/cgo package on the +// callCgoMmap calls the mmap function in the runtime/cgo package +// using the GCC calling convention. It is implemented in assembly. +func callCgoMmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) unsafe.Pointer |