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