msan_fail.go 701 B

123456789101112131415161718192021222324252627282930313233343536
  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. #include <string.h>
  7. #include <stdint.h>
  8. #include <stdlib.h>
  9. void f(int32_t *p, int n) {
  10. int32_t * volatile q = (int32_t *)malloc(sizeof(int32_t) * n);
  11. memcpy(p, q, n * sizeof(*p));
  12. free(q);
  13. }
  14. void g(int32_t *p, int n) {
  15. if (p[4] != 1) {
  16. // We shouldn't get here; msan should stop us first.
  17. exit(0);
  18. }
  19. }
  20. */
  21. import "C"
  22. import (
  23. "unsafe"
  24. )
  25. func main() {
  26. a := make([]int32, 10)
  27. C.f((*C.int32_t)(unsafe.Pointer(&a[0])), C.int(len(a)))
  28. a[3] = 1
  29. C.g((*C.int32_t)(unsafe.Pointer(&a[0])), C.int(len(a)))
  30. }