msan2_cmsan.go 710 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. package main
  5. /*
  6. #cgo LDFLAGS: -fsanitize=memory
  7. #cgo CPPFLAGS: -fsanitize=memory
  8. #include <string.h>
  9. #include <stdint.h>
  10. #include <stdlib.h>
  11. void f(int32_t *p, int n) {
  12. int32_t * volatile q = (int32_t *)malloc(sizeof(int32_t) * n);
  13. memcpy(p, q, n * sizeof(*p));
  14. free(q);
  15. }
  16. void g(int32_t *p, int n) {
  17. if (p[4] != 1) {
  18. abort();
  19. }
  20. }
  21. */
  22. import "C"
  23. import (
  24. "unsafe"
  25. )
  26. func main() {
  27. a := make([]int32, 10)
  28. C.f((*C.int32_t)(unsafe.Pointer(&a[0])), C.int(len(a)))
  29. a[4] = 1
  30. C.g((*C.int32_t)(unsafe.Pointer(&a[0])), C.int(len(a)))
  31. }