msan5.go 904 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2016 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. // Using reflect to set a value was not seen by msan.
  6. /*
  7. #include <stdlib.h>
  8. extern void Go1(int*);
  9. extern void Go2(char*);
  10. // Use weak as a hack to permit defining a function even though we use export.
  11. void C1() __attribute__ ((weak));
  12. void C2() __attribute__ ((weak));
  13. void C1() {
  14. int i;
  15. Go1(&i);
  16. if (i != 42) {
  17. abort();
  18. }
  19. }
  20. void C2() {
  21. char a[2];
  22. a[1] = 42;
  23. Go2(a);
  24. if (a[0] != 42) {
  25. abort();
  26. }
  27. }
  28. */
  29. import "C"
  30. import (
  31. "reflect"
  32. "unsafe"
  33. )
  34. //export Go1
  35. func Go1(p *C.int) {
  36. reflect.ValueOf(p).Elem().Set(reflect.ValueOf(C.int(42)))
  37. }
  38. //export Go2
  39. func Go2(p *C.char) {
  40. a := (*[2]byte)(unsafe.Pointer(p))
  41. reflect.Copy(reflect.ValueOf(a[:1]), reflect.ValueOf(a[1:]))
  42. }
  43. func main() {
  44. C.C1()
  45. C.C2()
  46. }