mirror of
https://github.com/golang/go
synced 2024-11-19 11:44:45 -07:00
e7ee268292
The -msan option compiles Go code to use the memory sanitizer. This is intended for use when linking with C/C++ code compiled with -fsanitize=memory. When memory blocks are passed back and forth between C/C++ and Go, code in both languages will agree as to whether the memory is correctly initialized or not, and will report errors for any use of uninitialized memory. Change-Id: I2dbdbd26951eacb7d84063cfc7297f88ffadd70c Reviewed-on: https://go-review.googlesource.com/16169 Reviewed-by: David Crawshaw <crawshaw@golang.org>
32 lines
484 B
Go
32 lines
484 B
Go
package main
|
|
|
|
/*
|
|
#include <string.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
|
|
void f(int32_t *p, int n) {
|
|
int32_t * volatile q = (int32_t *)malloc(sizeof(int32_t) * n);
|
|
memcpy(p, q, n * sizeof(*p));
|
|
free(q);
|
|
}
|
|
|
|
void g(int32_t *p, int n) {
|
|
if (p[4] != 1) {
|
|
abort();
|
|
}
|
|
}
|
|
*/
|
|
import "C"
|
|
|
|
import (
|
|
"unsafe"
|
|
)
|
|
|
|
func main() {
|
|
a := make([]int32, 10)
|
|
C.f((*C.int32_t)(unsafe.Pointer(&a[0])), C.int(len(a)))
|
|
a[4] = 1
|
|
C.g((*C.int32_t)(unsafe.Pointer(&a[0])), C.int(len(a)))
|
|
}
|