Skip to content

Commit 1edfd64

Browse files
committed
[release-branch.go1.8] cmd/compile: do not use "oaslit" for global
The compiler did not emit write barrier for assigning global with struct literal, like global = T{} where T contains pointer. The relevant code path is: walkexpr OAS var_ OSTRUCTLIT oaslit anylit OSTRUCTLIT walkexpr OAS var_ nil return without adding write barrier return true break (without adding write barrier) This CL makes oaslit not apply to globals. See also CL https://go-review.googlesource.com/c/36355/ for an alternative fix. The downside of this is that it generates static data for zeroing struct now. Also this only covers global. If there is any lurking bug with implicit zeroing other than globals, this doesn't fix. Fixes #18956. Change-Id: Ibcd27e4fae3aa38390ffa94a32a9dd7a802e4b37 Reviewed-on: https://go-review.googlesource.com/36410 Reviewed-by: Russ Cox <[email protected]> Run-TryBot: Cherry Zhang <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> (cherry picked from commit 160914e) Reviewed-on: https://go-review.googlesource.com/36531
1 parent 6eb0f54 commit 1edfd64

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/cmd/compile/internal/gc/sinit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ func isliteral(n *Node) bool {
585585
}
586586

587587
func (n *Node) isSimpleName() bool {
588-
return n.Op == ONAME && n.Addable && n.Class != PAUTOHEAP
588+
return n.Op == ONAME && n.Addable && n.Class != PAUTOHEAP && n.Class != PEXTERN
589589
}
590590

591591
func litas(l *Node, r *Node, init *Nodes) {

test/writebarrier.go

+16
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,19 @@ func f22(x *int) (y *int) {
220220
*p = x // no barrier
221221
return
222222
}
223+
224+
type T23 struct {
225+
p *int
226+
a int
227+
}
228+
229+
var t23 T23
230+
var i23 int
231+
232+
func f23() {
233+
// zeroing global needs write barrier for the hybrid barrier.
234+
t23 = T23{} // ERROR "write barrier"
235+
// also test partial assignments
236+
t23 = T23{a: 1} // ERROR "write barrier"
237+
t23 = T23{p: &i23} // ERROR "write barrier"
238+
}

0 commit comments

Comments
 (0)