Source file test/nowritebarrier.go

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

View as plain text