1 // errorcheck -0 -l -d=wb
3 // Copyright 2015 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Test where write barriers are and are not emitted.
13 func f(x **byte, y *byte) {
14 *x = y // no barrier (dead store)
17 *x = z // ERROR "write barrier"
20 func f1(x *[]byte, y []byte) {
21 *x = y // no barrier (dead store)
24 *x = z // ERROR "write barrier"
27 func f1a(x *[]byte, y *[]byte) {
28 *x = *y // ERROR "write barrier"
31 *x = z // ERROR "write barrier"
34 func f2(x *interface{}, y interface{}) {
35 *x = y // no barrier (dead store)
38 *x = z // ERROR "write barrier"
41 func f2a(x *interface{}, y *interface{}) {
42 *x = *y // no barrier (dead store)
45 *x = z // ERROR "write barrier"
48 func f3(x *string, y string) {
49 *x = y // no barrier (dead store)
52 *x = z // ERROR "write barrier"
55 func f3a(x *string, y *string) {
56 *x = *y // ERROR "write barrier"
59 *x = z // ERROR "write barrier"
62 func f4(x *[2]string, y [2]string) {
63 *x = y // ERROR "write barrier"
66 *x = z // ERROR "write barrier"
69 func f4a(x *[2]string, y *[2]string) {
70 *x = *y // ERROR "write barrier"
73 *x = z // ERROR "write barrier"
83 t.X = &u.Y // ERROR "write barrier"
87 t.M = map[int]int{1: 2} // ERROR "write barrier"
90 func f7(x, y *int) []*int {
93 z[i] = x // ERROR "write barrier"
95 z[i] = y // ERROR "write barrier"
100 func f9(x *interface{}, v *byte) {
101 *x = v // ERROR "write barrier"
104 func f10(x *byte, f func(interface{})) {
108 func f11(x *unsafe.Pointer, y unsafe.Pointer) {
109 *x = unsafe.Pointer(uintptr(y) + 1) // ERROR "write barrier"
112 func 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"
118 func f12a(x []int, y int) []int {
119 // y not a pointer, so no write barriers in this function
124 func f13(x []int, y *[]int) {
125 *y = append(x, 1) // ERROR "write barrier"
129 *y = append(*y, 1) // ERROR "write barrier"
136 func f15(x []T1, y T1) []T1 {
137 return append(x, y) // ERROR "write barrier"
144 func f16(x []T8, y T8) []T8 {
145 return append(x, y) // ERROR "write barrier"
148 func 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) {
152 case *int: // ERROR "write barrier"
155 switch y := i.(type) {
156 case **int: // no write barrier here
167 // Originally from golang.org/issue/13901, but the hybrid
168 // barrier requires both to have barriers.
169 x.f = f17 // ERROR "write barrier"
170 x.f = func(y *T17) { *y = *x } // ERROR "write barrier"
178 func f18(p *T18, x *[]int) {
179 p.a = p.a[:5] // no barrier
180 *x = (*x)[0:5] // no barrier
181 p.a = p.a[3:5] // ERROR "write barrier"
182 p.a = p.a[1:2:3] // ERROR "write barrier"
183 p.s = p.s[8:9] // ERROR "write barrier"
184 *x = (*x)[3:5] // ERROR "write barrier"
187 func f19(x, y *int, i int) int {
188 // Constructing a temporary slice on the stack should not
189 // require any write barriers. See issue 14263.
190 a := []*int{x, y} // no barrier
194 func f20(x, y *int, i int) []*int {
195 // ... but if that temporary slice escapes, then the
196 // write barriers are necessary.
197 a := []*int{x, y} // ERROR "write barrier"
207 // f21x: Global -> heap pointer updates must have write barriers.
209 x21 = x // ERROR "write barrier"
210 y21.x = x // ERROR "write barrier"
214 x21 = &z21 // ERROR "write barrier"
215 y21.x = &z21 // ERROR "write barrier"
219 y21 = struct{ x *int }{x} // ERROR "write barrier"
222 func f22(x *int) (y *int) {
223 // pointer write on stack should have no write barrier.
224 // this is a case that the frontend failed to eliminate.
238 // f23x: zeroing global needs write barrier for the hybrid barrier.
240 t23 = T23{} // ERROR "write barrier"
244 // also test partial assignments
245 t23 = T23{a: 1} // ERROR "write barrier"
249 t23 = T23{} // no barrier (dead store)
250 // also test partial assignments
251 t23 = T23{p: &i23} // ERROR "write barrier"
258 *p = &g // no write barrier here
261 func f25() []string {
262 return []string{"abc", "def", "ghi"} // no write barrier here
272 func f26(p *int) *T26 { // see issue 29573
277 d: &g26, // no write barrier: global ptr
278 e: nil, // no write barrier: nil ptr
279 f: p, // ERROR "write barrier"
283 func f27(p *int) []interface{} {
284 return []interface{}{
285 nil, // no write barrier: zeroed memory, nil ptr
286 (*T26)(nil), // no write barrier: zeroed memory, type ptr & nil ptr
287 &g26, // no write barrier: zeroed memory, type ptr & global ptr
288 7, // no write barrier: zeroed memory, type ptr & global ptr
289 p, // ERROR "write barrier"
295 func f28() []interface{} {
296 return []interface{}{
297 false, // no write barrier
298 true, // no write barrier
299 0, // no write barrier
300 1, // no write barrier
301 uint8(127), // no write barrier
302 int8(-4), // no write barrier
303 &g28[5], // no write barrier