mirror of
https://github.com/golang/go
synced 2024-10-06 08:00:07 +00:00
cmd/internal/gc: add and test write barrier debug output
We can expand the test cases as we discover problems. This is some basic tests plus all the things I got wrong in some recent work. Change-Id: Id875fcfaf74eb087ae42b441fe47a34c5b8ccb39 Reviewed-on: https://go-review.googlesource.com/9158 Reviewed-by: Rick Hudson <rlh@golang.org> Reviewed-by: Austin Clements <austin@google.com>
This commit is contained in:
parent
80f575b78f
commit
9406f68e6a
|
@ -35,6 +35,8 @@ var goarch string
|
||||||
|
|
||||||
var goroot string
|
var goroot string
|
||||||
|
|
||||||
|
var Debug_wb int
|
||||||
|
|
||||||
// Debug arguments.
|
// Debug arguments.
|
||||||
// These can be specified with the -d flag, as in "-d nil"
|
// These can be specified with the -d flag, as in "-d nil"
|
||||||
// to set the debug_checknil variable. In general the list passed
|
// to set the debug_checknil variable. In general the list passed
|
||||||
|
@ -46,6 +48,7 @@ var debugtab = []struct {
|
||||||
{"nil", &Debug_checknil}, // print information about nil checks
|
{"nil", &Debug_checknil}, // print information about nil checks
|
||||||
{"typeassert", &Debug_typeassert}, // print information about type assertion inlining
|
{"typeassert", &Debug_typeassert}, // print information about type assertion inlining
|
||||||
{"disablenil", &Disable_checknil}, // disable nil checks
|
{"disablenil", &Disable_checknil}, // disable nil checks
|
||||||
|
{"wb", &Debug_wb}, // print information about write barriers
|
||||||
}
|
}
|
||||||
|
|
||||||
// Our own isdigit, isspace, isalpha, isalnum that take care
|
// Our own isdigit, isspace, isalpha, isalnum that take care
|
||||||
|
|
|
@ -2218,6 +2218,9 @@ func applywritebarrier(n *Node, init **NodeList) *Node {
|
||||||
if Curfn != nil && Curfn.Func.Nowritebarrier {
|
if Curfn != nil && Curfn.Func.Nowritebarrier {
|
||||||
Yyerror("write barrier prohibited")
|
Yyerror("write barrier prohibited")
|
||||||
}
|
}
|
||||||
|
if Debug_wb > 0 {
|
||||||
|
Warnl(int(n.Lineno), "write barrier")
|
||||||
|
}
|
||||||
t := n.Left.Type
|
t := n.Left.Type
|
||||||
l := Nod(OADDR, n.Left, nil)
|
l := Nod(OADDR, n.Left, nil)
|
||||||
l.Etype = 1 // addr does not escape
|
l.Etype = 1 // addr does not escape
|
||||||
|
|
110
test/writebarrier.go
Normal file
110
test/writebarrier.go
Normal file
|
@ -0,0 +1,110 @@
|
||||||
|
// errorcheck -0 -l -d=wb
|
||||||
|
|
||||||
|
// Copyright 2015 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Test where write barriers are and are not emitted.
|
||||||
|
|
||||||
|
package p
|
||||||
|
|
||||||
|
import "unsafe"
|
||||||
|
|
||||||
|
func f(x **byte, y *byte) {
|
||||||
|
*x = y // ERROR "write barrier"
|
||||||
|
|
||||||
|
z := y // no barrier
|
||||||
|
*x = z // ERROR "write barrier"
|
||||||
|
}
|
||||||
|
|
||||||
|
func f1(x *[]byte, y []byte) {
|
||||||
|
*x = y // ERROR "write barrier"
|
||||||
|
|
||||||
|
z := y // no barrier
|
||||||
|
*x = z // ERROR "write barrier"
|
||||||
|
}
|
||||||
|
|
||||||
|
func f1a(x *[]byte, y *[]byte) {
|
||||||
|
*x = *y // ERROR "write barrier"
|
||||||
|
|
||||||
|
z := *y // no barrier
|
||||||
|
*x = z // ERROR "write barrier"
|
||||||
|
}
|
||||||
|
|
||||||
|
func f2(x *interface{}, y interface{}) {
|
||||||
|
*x = y // ERROR "write barrier"
|
||||||
|
|
||||||
|
z := y // no barrier
|
||||||
|
*x = z // ERROR "write barrier"
|
||||||
|
}
|
||||||
|
|
||||||
|
func f2a(x *interface{}, y *interface{}) {
|
||||||
|
*x = *y // ERROR "write barrier"
|
||||||
|
|
||||||
|
z := y // no barrier
|
||||||
|
*x = z // ERROR "write barrier"
|
||||||
|
}
|
||||||
|
|
||||||
|
func f3(x *string, y string) {
|
||||||
|
*x = y // ERROR "write barrier"
|
||||||
|
|
||||||
|
z := y // no barrier
|
||||||
|
*x = z // ERROR "write barrier"
|
||||||
|
}
|
||||||
|
|
||||||
|
func f3a(x *string, y *string) {
|
||||||
|
*x = *y // ERROR "write barrier"
|
||||||
|
|
||||||
|
z := *y // no barrier
|
||||||
|
*x = z // ERROR "write barrier"
|
||||||
|
}
|
||||||
|
|
||||||
|
func f4(x *[2]string, y [2]string) {
|
||||||
|
*x = y // ERROR "write barrier"
|
||||||
|
|
||||||
|
z := y // no barrier
|
||||||
|
*x = z // ERROR "write barrier"
|
||||||
|
}
|
||||||
|
|
||||||
|
func f4a(x *[2]string, y *[2]string) {
|
||||||
|
*x = *y // ERROR "write barrier"
|
||||||
|
|
||||||
|
z := *y // no barrier
|
||||||
|
*x = z // ERROR "write barrier"
|
||||||
|
}
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
X *int
|
||||||
|
Y int
|
||||||
|
M map[int]int
|
||||||
|
}
|
||||||
|
|
||||||
|
func f5(t, u *T) {
|
||||||
|
t.X = &u.Y // ERROR "write barrier"
|
||||||
|
}
|
||||||
|
|
||||||
|
func f6(t *T) {
|
||||||
|
t.M = map[int]int{1: 2} // ERROR "write barrier"
|
||||||
|
}
|
||||||
|
|
||||||
|
func f7(x, y *int) []*int {
|
||||||
|
var z [3]*int
|
||||||
|
i := 0
|
||||||
|
z[i] = x // ERROR "write barrier"
|
||||||
|
i++
|
||||||
|
z[i] = y // ERROR "write barrier"
|
||||||
|
i++
|
||||||
|
return z[:i]
|
||||||
|
}
|
||||||
|
|
||||||
|
func f9(x *interface{}, v *byte) {
|
||||||
|
*x = v // ERROR "write barrier"
|
||||||
|
}
|
||||||
|
|
||||||
|
func f10(x *byte, f func(interface{})) {
|
||||||
|
f(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func f11(x *unsafe.Pointer, y unsafe.Pointer) {
|
||||||
|
*x = unsafe.Pointer(uintptr(y) + 1) // ERROR "write barrier"
|
||||||
|
}
|
Loading…
Reference in a new issue