modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/nowritebarrier.go (about)

     1  // errorcheck -+
     2  
     3  // Copyright 2016 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.
     6  
     7  // Test go:nowritebarrier and related directives.
     8  
     9  package p
    10  
    11  type t struct {
    12  	f *t
    13  }
    14  
    15  var x t
    16  var y *t
    17  
    18  //go:nowritebarrier
    19  func a1() {
    20  	x.f = y // ERROR "write barrier prohibited"
    21  	a2()    // no error
    22  }
    23  
    24  //go:noinline
    25  func a2() {
    26  	x.f = y
    27  }
    28  
    29  //go:nowritebarrierrec
    30  func b1() {
    31  	b2()
    32  }
    33  
    34  //go:noinline
    35  func b2() {
    36  	x.f = y // ERROR "write barrier prohibited by caller"
    37  }
    38  
    39  // Test recursive cycles through nowritebarrierrec and yeswritebarrierrec.
    40  
    41  //go:nowritebarrierrec
    42  func c1() {
    43  	c2()
    44  }
    45  
    46  //go:yeswritebarrierrec
    47  func c2() {
    48  	c3()
    49  }
    50  
    51  func c3() {
    52  	x.f = y
    53  	c4()
    54  }
    55  
    56  //go:nowritebarrierrec
    57  func c4() {
    58  	c2()
    59  }
    60  
    61  //go:nowritebarrierrec
    62  func d1() {
    63  	d2()
    64  }
    65  
    66  func d2() {
    67  	d3()
    68  }
    69  
    70  func d3() {
    71  	x.f = y // ERROR "write barrier prohibited by caller"
    72  	d4()
    73  }
    74  
    75  //go:yeswritebarrierrec
    76  func d4() {
    77  	d2()
    78  }
    79  
    80  //go:noinline
    81  func systemstack(func()) {}
    82  
    83  //go:nowritebarrierrec
    84  func e1() {
    85  	systemstack(e2)
    86  	systemstack(func() {
    87  		x.f = y // ERROR "write barrier prohibited by caller"
    88  	})
    89  }
    90  
    91  func e2() {
    92  	x.f = y // ERROR "write barrier prohibited by caller"
    93  }