blob: 88b4b296df56648e74dcf97b2ee6f56f8d8a0572 [file] [log] [blame]
Russ Cox9406f682015-04-17 00:34:18 -04001// errorcheck -0 -l -d=wb
2
Emmanuel Odeke53fd5222016-04-10 14:32:26 -07003// Copyright 2015 The Go Authors. All rights reserved.
Russ Cox9406f682015-04-17 00:34:18 -04004// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7// Test where write barriers are and are not emitted.
8
9package p
10
11import "unsafe"
12
13func f(x **byte, y *byte) {
14 *x = y // ERROR "write barrier"
15
16 z := y // no barrier
17 *x = z // ERROR "write barrier"
18}
19
20func f1(x *[]byte, y []byte) {
21 *x = y // ERROR "write barrier"
22
23 z := y // no barrier
24 *x = z // ERROR "write barrier"
25}
26
27func f1a(x *[]byte, y *[]byte) {
28 *x = *y // ERROR "write barrier"
29
30 z := *y // no barrier
Russ Cox85520472015-05-06 12:34:30 -040031 *x = z // ERROR "write barrier"
Russ Cox9406f682015-04-17 00:34:18 -040032}
33
34func f2(x *interface{}, y interface{}) {
35 *x = y // ERROR "write barrier"
36
37 z := y // no barrier
38 *x = z // ERROR "write barrier"
39}
40
41func f2a(x *interface{}, y *interface{}) {
42 *x = *y // ERROR "write barrier"
43
44 z := y // no barrier
45 *x = z // ERROR "write barrier"
46}
47
48func f3(x *string, y string) {
49 *x = y // ERROR "write barrier"
50
51 z := y // no barrier
52 *x = z // ERROR "write barrier"
53}
54
55func f3a(x *string, y *string) {
56 *x = *y // ERROR "write barrier"
57
58 z := *y // no barrier
Russ Cox85520472015-05-06 12:34:30 -040059 *x = z // ERROR "write barrier"
Russ Cox9406f682015-04-17 00:34:18 -040060}
61
62func f4(x *[2]string, y [2]string) {
63 *x = y // ERROR "write barrier"
64
65 z := y // no barrier
66 *x = z // ERROR "write barrier"
67}
68
69func f4a(x *[2]string, y *[2]string) {
70 *x = *y // ERROR "write barrier"
71
72 z := *y // no barrier
Russ Cox85520472015-05-06 12:34:30 -040073 *x = z // ERROR "write barrier"
Russ Cox9406f682015-04-17 00:34:18 -040074}
75
76type T struct {
77 X *int
78 Y int
79 M map[int]int
80}
81
82func f5(t, u *T) {
83 t.X = &u.Y // ERROR "write barrier"
84}
85
86func f6(t *T) {
87 t.M = map[int]int{1: 2} // ERROR "write barrier"
88}
89
90func f7(x, y *int) []*int {
91 var z [3]*int
92 i := 0
93 z[i] = x // ERROR "write barrier"
94 i++
95 z[i] = y // ERROR "write barrier"
96 i++
97 return z[:i]
98}
99
100func f9(x *interface{}, v *byte) {
101 *x = v // ERROR "write barrier"
102}
103
104func f10(x *byte, f func(interface{})) {
105 f(x)
106}
107
108func f11(x *unsafe.Pointer, y unsafe.Pointer) {
109 *x = unsafe.Pointer(uintptr(y) + 1) // ERROR "write barrier"
110}
Russ Cox85520472015-05-06 12:34:30 -0400111
112func f12(x []*int, y *int) []*int {
113 // write barrier for storing y in x's underlying array
114 x = append(x, y) // ERROR "write barrier"
115 return x
116}
117
118func f12a(x []int, y int) []int {
119 // y not a pointer, so no write barriers in this function
120 x = append(x, y)
121 return x
122}
123
124func f13(x []int, y *[]int) {
125 *y = append(x, 1) // ERROR "write barrier"
126}
127
128func f14(y *[]int) {
129 *y = append(*y, 1) // ERROR "write barrier"
130}
Russ Cox366ba522015-05-18 16:54:59 -0400131
132type T1 struct {
133 X *int
134}
135
136func f15(x []T1, y T1) []T1 {
137 return append(x, y) // ERROR "write barrier"
138}
139
140type T8 struct {
141 X [8]*int
142}
143
144func f16(x []T8, y T8) []T8 {
145 return append(x, y) // ERROR "write barrier"
146}
Keith Randalle3033fc2016-02-12 10:07:36 -0800147
148func t1(i interface{}) **int {
149 // From issue 14306, make sure we have write barriers in a type switch
150 // where the assigned variable escapes.
151 switch x := i.(type) { // ERROR "write barrier"
152 case *int:
153 return &x
154 }
155 switch y := i.(type) { // no write barrier here
156 case **int:
157 return y
158 }
159 return nil
160}
Austin Clements3e54ca92016-03-16 18:22:58 -0400161
162type T17 struct {
163 f func(*T17)
164}
165
166func f17(x *T17) {
167 // See golang.org/issue/13901
168 x.f = f17 // no barrier
169 x.f = func(y *T17) { *y = *x } // ERROR "write barrier"
170}
Keith Randall15ed37d2016-03-16 21:51:17 -0700171
172type T18 struct {
173 a []int
174 s string
175}
176
177func f18(p *T18, x *[]int) {
178 p.a = p.a[:5] // no barrier
Keith Randalld4663e12016-03-21 10:22:03 -0700179 *x = (*x)[0:5] // no barrier
180 p.a = p.a[3:5] // ERROR "write barrier"
181 p.a = p.a[1:2:3] // ERROR "write barrier"
182 p.s = p.s[8:9] // ERROR "write barrier"
183 *x = (*x)[3:5] // ERROR "write barrier"
Keith Randall15ed37d2016-03-16 21:51:17 -0700184}
Keith Randall934c3592016-04-23 22:59:01 -0700185
186func f19(x, y *int, i int) int {
187 // Constructing a temporary slice on the stack should not
188 // require any write barriers. See issue 14263.
189 a := []*int{x, y} // no barrier
190 return *a[i]
191}
192
193func f20(x, y *int, i int) []*int {
194 // ... but if that temporary slice escapes, then the
195 // write barriers are necessary.
196 a := []*int{x, y} // ERROR "write barrier"
197 return a
198}
Austin Clementsb49b71a2016-03-18 11:27:59 -0400199
200var x21 *int
201var y21 struct {
202 x *int
203}
204var z21 int
205
206func f21(x *int) {
207 // Global -> heap pointer updates must have write barriers.
208 x21 = x // ERROR "write barrier"
209 y21.x = x // ERROR "write barrier"
210 x21 = &z21 // no barrier
211 y21.x = &z21 // no barrier
212 y21 = struct{ x *int }{x} // ERROR "write barrier"
213}