Commit 0ee9c46c authored by Matthew Dempsky's avatar Matthew Dempsky

cmd/compile: add missing WBs for reflect.{Slice,String}Header.Data

Fixes #19168.

Change-Id: I3f4fcc0b189c53819ac29ef8de86fdad76a17488
Reviewed-on: https://go-review.googlesource.com/37663Reviewed-by: 's avatarKeith Randall <khr@golang.org>
parent 3d77bc08
......@@ -3454,6 +3454,11 @@ func (s *state) insertWBstore(t *Type, left, right *ssa.Value, skip skipMask) {
if !s.WBPos.IsKnown() {
s.WBPos = left.Pos
}
if t == Types[TUINTPTR] {
// Stores to reflect.{Slice,String}Header.Data.
s.vars[&memVar] = s.newValue3I(ssa.OpStoreWB, ssa.TypeMem, s.config.PtrSize, left, right, s.mem())
return
}
s.storeTypeScalars(t, left, right, skip)
s.storeTypePtrsWB(t, left, right)
}
......
......@@ -2006,6 +2006,29 @@ func isstack(n *Node) bool {
return false
}
// isReflectHeaderDataField reports whether l is an expression p.Data
// where p has type reflect.SliceHeader or reflect.StringHeader.
func isReflectHeaderDataField(l *Node) bool {
if l.Type != Types[TUINTPTR] {
return false
}
var tsym *Sym
switch l.Op {
case ODOT:
tsym = l.Left.Type.Sym
case ODOTPTR:
tsym = l.Left.Type.Elem().Sym
default:
return false
}
if tsym == nil || l.Sym.Name != "Data" || tsym.Pkg.Path != "reflect" {
return false
}
return tsym.Name == "SliceHeader" || tsym.Name == "StringHeader"
}
// Do we need a write barrier for assigning to l?
func needwritebarrier(l *Node) bool {
if !use_writebarrier {
......@@ -2016,15 +2039,21 @@ func needwritebarrier(l *Node) bool {
return false
}
// No write barrier for write of non-pointers.
dowidth(l.Type)
if !haspointers(l.Type) {
// No write barrier for write to stack.
if isstack(l) {
return false
}
// No write barrier for write to stack.
if isstack(l) {
// Package unsafe's documentation says storing pointers into
// reflect.SliceHeader and reflect.StringHeader's Data fields
// is valid, even though they have type uintptr (#19168).
if isReflectHeaderDataField(l) {
return true
}
// No write barrier for write of non-pointers.
dowidth(l.Type)
if !haspointers(l.Type) {
return false
}
......
// errorcheck -0 -l -d=wb
// Copyright 2017 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.
package p
import (
"reflect"
"unsafe"
reflect2 "reflect"
)
func sink(e interface{})
func a(hdr *reflect.SliceHeader, p *byte) {
hdr.Data = uintptr(unsafe.Pointer(p)) // ERROR "write barrier"
}
func b(hdr *reflect.StringHeader, p *byte) {
hdr.Data = uintptr(unsafe.Pointer(p)) // ERROR "write barrier"
}
func c(hdrs *[1]reflect.SliceHeader, p *byte) {
hdrs[0].Data = uintptr(unsafe.Pointer(p)) // ERROR "write barrier"
}
func d(hdr *struct{ s reflect.StringHeader }, p *byte) {
hdr.s.Data = uintptr(unsafe.Pointer(p)) // ERROR "write barrier"
}
func e(p *byte) (resHeap, resStack string) {
sink(&resHeap)
hdr := (*reflect.StringHeader)(unsafe.Pointer(&resHeap))
hdr.Data = uintptr(unsafe.Pointer(p)) // ERROR "write barrier"
// No write barrier for non-escaping stack vars.
hdr = (*reflect.StringHeader)(unsafe.Pointer(&resStack))
hdr.Data = uintptr(unsafe.Pointer(p))
return
}
func f(hdr *reflect2.SliceHeader, p *byte) {
hdr.Data = uintptr(unsafe.Pointer(p)) // ERROR "write barrier"
}
type SliceHeader struct {
Data uintptr
}
func g(hdr *SliceHeader, p *byte) {
// No write barrier for lookalike SliceHeader.
hdr.Data = uintptr(unsafe.Pointer(p))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment