1
2
3
4
5 package main
6
7 import (
8 "runtime"
9 "testing"
10 "unsafe"
11 )
12
13
14 var a *[8]uint
15
16
17 var always = true
18
19
20
21
22
23
24
25 func f_ssa() *[8]uint {
26
27 var x uintptr
28 if always {
29 x = uintptr(unsafe.Pointer(a))
30 } else {
31 x = 0
32 }
33
34
35 a = nil
36
37
38
39 p := unsafe.Pointer(x)
40
41
42 runtime.GC()
43
44
45 y := uintptr(p)
46
47
48
49
50 var z uintptr
51 if always {
52 z = y
53 } else {
54 z = 0
55 }
56 return (*[8]uint)(unsafe.Pointer(z))
57 }
58
59
60
61 func g_ssa() *[7]uint {
62
63 var x uintptr
64 if always {
65 x = uintptr(unsafe.Pointer(a))
66 } else {
67 x = 0
68 }
69
70
71 a = nil
72
73
74 x += unsafe.Sizeof(int(0))
75
76
77
78 p := unsafe.Pointer(x)
79
80
81 runtime.GC()
82
83
84 y := uintptr(p)
85
86
87
88
89 var z uintptr
90 if always {
91 z = y
92 } else {
93 z = 0
94 }
95 return (*[7]uint)(unsafe.Pointer(z))
96 }
97
98 func testf(t *testing.T) {
99 a = new([8]uint)
100 for i := 0; i < 8; i++ {
101 a[i] = 0xabcd
102 }
103 c := f_ssa()
104 for i := 0; i < 8; i++ {
105 if c[i] != 0xabcd {
106 t.Fatalf("%d:%x\n", i, c[i])
107 }
108 }
109 }
110
111 func testg(t *testing.T) {
112 a = new([8]uint)
113 for i := 0; i < 8; i++ {
114 a[i] = 0xabcd
115 }
116 c := g_ssa()
117 for i := 0; i < 7; i++ {
118 if c[i] != 0xabcd {
119 t.Fatalf("%d:%x\n", i, c[i])
120 }
121 }
122 }
123
124 func alias_ssa(ui64 *uint64, ui32 *uint32) uint32 {
125 *ui32 = 0xffffffff
126 *ui64 = 0
127 ret := *ui32
128 *ui64 = 0xffffffffffffffff
129 return ret
130 }
131 func testdse(t *testing.T) {
132 x := int64(-1)
133
134 ui64 := (*uint64)(unsafe.Pointer(&x))
135 ui32 := (*uint32)(unsafe.Pointer(&x))
136 if want, got := uint32(0), alias_ssa(ui64, ui32); got != want {
137 t.Fatalf("alias_ssa: wanted %d, got %d\n", want, got)
138 }
139 }
140
141 func TestUnsafe(t *testing.T) {
142 testf(t)
143 testg(t)
144 testdse(t)
145 }
146
View as plain text