mirror of
https://github.com/golang/go
synced 2024-11-18 17:24:40 -07:00
0c1f0549b8
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>
35 lines
444 B
Go
35 lines
444 B
Go
package main
|
|
|
|
/*
|
|
#cgo CFLAGS: -fsanitize=memory
|
|
#cgo LDFLAGS: -fsanitize=memory
|
|
|
|
#include <stdint.h>
|
|
|
|
void f(int32_t *p, int n) {
|
|
int i;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
p[i] = (int32_t)i;
|
|
}
|
|
}
|
|
*/
|
|
import "C"
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"unsafe"
|
|
)
|
|
|
|
func main() {
|
|
a := make([]int32, 10)
|
|
C.f((*C.int32_t)(unsafe.Pointer(&a[0])), C.int(len(a)))
|
|
for i, v := range a {
|
|
if i != int(v) {
|
|
fmt.Println("bad %d: %v\n", i, a)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
}
|