Commit 38dee12d authored by Keith Randall's avatar Keith Randall

cmd/compile: zero ambiguously live variables at VARKILLs

At VARKILLs, zero a variable if it is ambiguously live.
After the VARKILL anything this variable references
might be collected. If it were to become live again later,
the GC will see references to already-collected objects.

We don't know a variable is ambiguously live until very
late in compilation (after lowering, register allocation, ...),
so it is hard to generate the code in an arch-independent way.
We also have to be careful not to clobber any registers.
Fortunately, this almost never happens so performance is ~irrelevant.

There are only 2 instances where this triggers in the stdlib.

Fixes #20029

Change-Id: Ia9585a91d7b823fad4a9d141d954464cc7af31f4
Reviewed-on: https://go-review.googlesource.com/41076
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarDavid Chase <drchase@google.com>
parent 7bd1c210
...@@ -22,6 +22,7 @@ func Init(arch *gc.Arch) { ...@@ -22,6 +22,7 @@ func Init(arch *gc.Arch) {
arch.MAXWIDTH = 1 << 50 arch.MAXWIDTH = 1 << 50
arch.ZeroRange = zerorange arch.ZeroRange = zerorange
arch.ZeroAuto = zeroAuto
arch.Ginsnop = ginsnop arch.Ginsnop = ginsnop
arch.SSAMarkMoves = ssaMarkMoves arch.SSAMarkMoves = ssaMarkMoves
......
...@@ -121,6 +121,26 @@ func zerorange(pp *gc.Progs, p *obj.Prog, off, cnt int64, state *uint32) *obj.Pr ...@@ -121,6 +121,26 @@ func zerorange(pp *gc.Progs, p *obj.Prog, off, cnt int64, state *uint32) *obj.Pr
return p return p
} }
func zeroAuto(pp *gc.Progs, n *gc.Node) {
// Note: this code must not clobber any registers.
op := x86.AMOVQ
if gc.Widthptr == 4 {
op = x86.AMOVL
}
sym := gc.Linksym(n.Sym)
size := n.Type.Size()
for i := int64(0); i < size; i += int64(gc.Widthptr) {
p := pp.Prog(op)
p.From.Type = obj.TYPE_CONST
p.From.Offset = 0
p.To.Type = obj.TYPE_MEM
p.To.Name = obj.NAME_AUTO
p.To.Reg = x86.REG_SP
p.To.Offset = n.Xoffset + i
p.To.Sym = sym
}
}
func ginsnop(pp *gc.Progs) { func ginsnop(pp *gc.Progs) {
// This is actually not the x86 NOP anymore, // This is actually not the x86 NOP anymore,
// but at the point where it gets used, AX is dead // but at the point where it gets used, AX is dead
......
...@@ -16,6 +16,7 @@ func Init(arch *gc.Arch) { ...@@ -16,6 +16,7 @@ func Init(arch *gc.Arch) {
arch.MAXWIDTH = (1 << 32) - 1 arch.MAXWIDTH = (1 << 32) - 1
arch.ZeroRange = zerorange arch.ZeroRange = zerorange
arch.ZeroAuto = zeroAuto
arch.Ginsnop = ginsnop arch.Ginsnop = ginsnop
arch.SSAMarkMoves = func(s *gc.SSAGenState, b *ssa.Block) {} arch.SSAMarkMoves = func(s *gc.SSAGenState, b *ssa.Block) {}
......
...@@ -47,6 +47,27 @@ func zerorange(pp *gc.Progs, p *obj.Prog, off, cnt int64, r0 *uint32) *obj.Prog ...@@ -47,6 +47,27 @@ func zerorange(pp *gc.Progs, p *obj.Prog, off, cnt int64, r0 *uint32) *obj.Prog
return p return p
} }
func zeroAuto(pp *gc.Progs, n *gc.Node) {
// Note: this code must not clobber any registers.
sym := gc.Linksym(n.Sym)
size := n.Type.Size()
p := pp.Prog(arm.AMOVW)
p.From.Type = obj.TYPE_CONST
p.From.Offset = 0
p.To.Type = obj.TYPE_REG
p.To.Reg = arm.REGTMP
for i := int64(0); i < size; i += 4 {
p := pp.Prog(arm.AMOVW)
p.From.Type = obj.TYPE_REG
p.From.Reg = arm.REGTMP
p.To.Type = obj.TYPE_MEM
p.To.Name = obj.NAME_AUTO
p.To.Reg = arm.REGSP
p.To.Offset = n.Xoffset + i
p.To.Sym = sym
}
}
func ginsnop(pp *gc.Progs) { func ginsnop(pp *gc.Progs) {
p := pp.Prog(arm.AAND) p := pp.Prog(arm.AAND)
p.From.Type = obj.TYPE_REG p.From.Type = obj.TYPE_REG
......
...@@ -17,6 +17,7 @@ func Init(arch *gc.Arch) { ...@@ -17,6 +17,7 @@ func Init(arch *gc.Arch) {
arch.PadFrame = padframe arch.PadFrame = padframe
arch.ZeroRange = zerorange arch.ZeroRange = zerorange
arch.ZeroAuto = zeroAuto
arch.Ginsnop = ginsnop arch.Ginsnop = ginsnop
arch.SSAMarkMoves = func(s *gc.SSAGenState, b *ssa.Block) {} arch.SSAMarkMoves = func(s *gc.SSAGenState, b *ssa.Block) {}
......
...@@ -58,6 +58,22 @@ func zerorange(pp *gc.Progs, p *obj.Prog, off, cnt int64, _ *uint32) *obj.Prog { ...@@ -58,6 +58,22 @@ func zerorange(pp *gc.Progs, p *obj.Prog, off, cnt int64, _ *uint32) *obj.Prog {
return p return p
} }
func zeroAuto(pp *gc.Progs, n *gc.Node) {
// Note: this code must not clobber any registers.
sym := gc.Linksym(n.Sym)
size := n.Type.Size()
for i := int64(0); i < size; i += 8 {
p := pp.Prog(arm64.AMOVD)
p.From.Type = obj.TYPE_REG
p.From.Reg = arm64.REGZERO
p.To.Type = obj.TYPE_MEM
p.To.Name = obj.NAME_AUTO
p.To.Reg = arm64.REGSP
p.To.Offset = n.Xoffset + i
p.To.Sym = sym
}
}
func ginsnop(pp *gc.Progs) { func ginsnop(pp *gc.Progs) {
p := pp.Prog(arm64.AHINT) p := pp.Prog(arm64.AHINT)
p.From.Type = obj.TYPE_CONST p.From.Type = obj.TYPE_CONST
......
...@@ -243,6 +243,11 @@ type Arch struct { ...@@ -243,6 +243,11 @@ type Arch struct {
// SSAGenBlock emits end-of-block Progs. SSAGenValue should be called // SSAGenBlock emits end-of-block Progs. SSAGenValue should be called
// for all values in the block before SSAGenBlock. // for all values in the block before SSAGenBlock.
SSAGenBlock func(s *SSAGenState, b, next *ssa.Block) SSAGenBlock func(s *SSAGenState, b, next *ssa.Block)
// ZeroAuto emits code to zero the given auto stack variable.
// ZeroAuto must not use any non-temporary registers.
// ZeroAuto will only be called for variables which contain a pointer.
ZeroAuto func(*Progs, *Node)
} }
var thearch Arch var thearch Arch
......
...@@ -711,7 +711,7 @@ func livenessepilogue(lv *Liveness) { ...@@ -711,7 +711,7 @@ func livenessepilogue(lv *Liveness) {
} }
// Annotate ambiguously live variables so that they can // Annotate ambiguously live variables so that they can
// be zeroed at function entry. // be zeroed at function entry and at VARKILL points.
// liveout is dead here and used as a temporary. // liveout is dead here and used as a temporary.
liveout.AndNot(any, all) liveout.AndNot(any, all)
if !liveout.IsEmpty() { if !liveout.IsEmpty() {
......
...@@ -4375,8 +4375,24 @@ func genssa(f *ssa.Func, pp *Progs) { ...@@ -4375,8 +4375,24 @@ func genssa(f *ssa.Func, pp *Progs) {
case ssa.OpGetG: case ssa.OpGetG:
// nothing to do when there's a g register, // nothing to do when there's a g register,
// and checkLower complains if there's not // and checkLower complains if there's not
case ssa.OpVarDef, ssa.OpVarKill, ssa.OpVarLive, ssa.OpKeepAlive: case ssa.OpVarDef, ssa.OpVarLive, ssa.OpKeepAlive:
// nothing to do; already used by liveness // nothing to do; already used by liveness
case ssa.OpVarKill:
// Zero variable if it is ambiguously live.
// After the VARKILL anything this variable references
// might be collected. If it were to become live again later,
// the GC will see references to already-collected objects.
// See issue 20029.
n := v.Aux.(*Node)
if n.Name.Needzero() {
if n.Class != PAUTO {
v.Fatalf("zero of variable which isn't PAUTO %v", n)
}
if n.Type.Size()%int64(Widthptr) != 0 {
v.Fatalf("zero of variable not a multiple of ptr size %v", n)
}
thearch.ZeroAuto(s.pp, n)
}
case ssa.OpPhi: case ssa.OpPhi:
CheckLoweredPhi(v) CheckLoweredPhi(v)
......
...@@ -19,6 +19,7 @@ func Init(arch *gc.Arch) { ...@@ -19,6 +19,7 @@ func Init(arch *gc.Arch) {
arch.REGSP = mips.REGSP arch.REGSP = mips.REGSP
arch.MAXWIDTH = (1 << 31) - 1 arch.MAXWIDTH = (1 << 31) - 1
arch.ZeroRange = zerorange arch.ZeroRange = zerorange
arch.ZeroAuto = zeroAuto
arch.Ginsnop = ginsnop arch.Ginsnop = ginsnop
arch.SSAMarkMoves = func(s *gc.SSAGenState, b *ssa.Block) {} arch.SSAMarkMoves = func(s *gc.SSAGenState, b *ssa.Block) {}
arch.SSAGenValue = ssaGenValue arch.SSAGenValue = ssaGenValue
......
...@@ -43,6 +43,22 @@ func zerorange(pp *gc.Progs, p *obj.Prog, off, cnt int64, _ *uint32) *obj.Prog { ...@@ -43,6 +43,22 @@ func zerorange(pp *gc.Progs, p *obj.Prog, off, cnt int64, _ *uint32) *obj.Prog {
return p return p
} }
func zeroAuto(pp *gc.Progs, n *gc.Node) {
// Note: this code must not clobber any registers.
sym := gc.Linksym(n.Sym)
size := n.Type.Size()
for i := int64(0); i < size; i += 4 {
p := pp.Prog(mips.AMOVW)
p.From.Type = obj.TYPE_REG
p.From.Reg = mips.REGZERO
p.To.Type = obj.TYPE_MEM
p.To.Name = obj.NAME_AUTO
p.To.Reg = mips.REGSP
p.To.Offset = n.Xoffset + i
p.To.Sym = sym
}
}
func ginsnop(pp *gc.Progs) { func ginsnop(pp *gc.Progs) {
p := pp.Prog(mips.ANOR) p := pp.Prog(mips.ANOR)
p.From.Type = obj.TYPE_REG p.From.Type = obj.TYPE_REG
......
...@@ -20,6 +20,7 @@ func Init(arch *gc.Arch) { ...@@ -20,6 +20,7 @@ func Init(arch *gc.Arch) {
arch.MAXWIDTH = 1 << 50 arch.MAXWIDTH = 1 << 50
arch.ZeroRange = zerorange arch.ZeroRange = zerorange
arch.ZeroAuto = zeroAuto
arch.Ginsnop = ginsnop arch.Ginsnop = ginsnop
arch.SSAMarkMoves = func(s *gc.SSAGenState, b *ssa.Block) {} arch.SSAMarkMoves = func(s *gc.SSAGenState, b *ssa.Block) {}
......
...@@ -47,6 +47,22 @@ func zerorange(pp *gc.Progs, p *obj.Prog, off, cnt int64, _ *uint32) *obj.Prog { ...@@ -47,6 +47,22 @@ func zerorange(pp *gc.Progs, p *obj.Prog, off, cnt int64, _ *uint32) *obj.Prog {
return p return p
} }
func zeroAuto(pp *gc.Progs, n *gc.Node) {
// Note: this code must not clobber any registers.
sym := gc.Linksym(n.Sym)
size := n.Type.Size()
for i := int64(0); i < size; i += 8 {
p := pp.Prog(mips.AMOVV)
p.From.Type = obj.TYPE_REG
p.From.Reg = mips.REGZERO
p.To.Type = obj.TYPE_MEM
p.To.Name = obj.NAME_AUTO
p.To.Reg = mips.REGSP
p.To.Offset = n.Xoffset + i
p.To.Sym = sym
}
}
func ginsnop(pp *gc.Progs) { func ginsnop(pp *gc.Progs) {
p := pp.Prog(mips.ANOR) p := pp.Prog(mips.ANOR)
p.From.Type = obj.TYPE_REG p.From.Type = obj.TYPE_REG
......
...@@ -19,6 +19,7 @@ func Init(arch *gc.Arch) { ...@@ -19,6 +19,7 @@ func Init(arch *gc.Arch) {
arch.MAXWIDTH = 1 << 50 arch.MAXWIDTH = 1 << 50
arch.ZeroRange = zerorange arch.ZeroRange = zerorange
arch.ZeroAuto = zeroAuto
arch.Ginsnop = ginsnop2 arch.Ginsnop = ginsnop2
arch.SSAMarkMoves = ssaMarkMoves arch.SSAMarkMoves = ssaMarkMoves
......
...@@ -42,6 +42,22 @@ func zerorange(pp *gc.Progs, p *obj.Prog, off, cnt int64, _ *uint32) *obj.Prog { ...@@ -42,6 +42,22 @@ func zerorange(pp *gc.Progs, p *obj.Prog, off, cnt int64, _ *uint32) *obj.Prog {
return p return p
} }
func zeroAuto(pp *gc.Progs, n *gc.Node) {
// Note: this code must not clobber any registers.
sym := gc.Linksym(n.Sym)
size := n.Type.Size()
for i := int64(0); i < size; i += 8 {
p := pp.Prog(ppc64.AMOVD)
p.From.Type = obj.TYPE_REG
p.From.Reg = ppc64.REGZERO
p.To.Type = obj.TYPE_MEM
p.To.Name = obj.NAME_AUTO
p.To.Reg = ppc64.REGSP
p.To.Offset = n.Xoffset + i
p.To.Sym = sym
}
}
func ginsnop(pp *gc.Progs) { func ginsnop(pp *gc.Progs) {
p := pp.Prog(ppc64.AOR) p := pp.Prog(ppc64.AOR)
p.From.Type = obj.TYPE_REG p.From.Type = obj.TYPE_REG
......
...@@ -15,6 +15,7 @@ func Init(arch *gc.Arch) { ...@@ -15,6 +15,7 @@ func Init(arch *gc.Arch) {
arch.MAXWIDTH = 1 << 50 arch.MAXWIDTH = 1 << 50
arch.ZeroRange = zerorange arch.ZeroRange = zerorange
arch.ZeroAuto = zeroAuto
arch.Ginsnop = ginsnop arch.Ginsnop = ginsnop
arch.SSAMarkMoves = ssaMarkMoves arch.SSAMarkMoves = ssaMarkMoves
......
...@@ -93,6 +93,18 @@ func zerorange(pp *gc.Progs, p *obj.Prog, off, cnt int64, _ *uint32) *obj.Prog { ...@@ -93,6 +93,18 @@ func zerorange(pp *gc.Progs, p *obj.Prog, off, cnt int64, _ *uint32) *obj.Prog {
return p return p
} }
func zeroAuto(pp *gc.Progs, n *gc.Node) {
// Note: this code must not clobber any registers.
p := pp.Prog(s390x.ACLEAR)
p.From.Type = obj.TYPE_CONST
p.From.Offset = n.Type.Size()
p.To.Type = obj.TYPE_MEM
p.To.Name = obj.NAME_AUTO
p.To.Reg = s390x.REGSP
p.To.Offset = n.Xoffset
p.To.Sym = gc.Linksym(n.Sym)
}
func ginsnop(pp *gc.Progs) { func ginsnop(pp *gc.Progs) {
p := pp.Prog(s390x.AOR) p := pp.Prog(s390x.AOR)
p.From.Type = obj.TYPE_REG p.From.Type = obj.TYPE_REG
......
...@@ -30,6 +30,7 @@ func Init(arch *gc.Arch) { ...@@ -30,6 +30,7 @@ func Init(arch *gc.Arch) {
arch.MAXWIDTH = (1 << 32) - 1 arch.MAXWIDTH = (1 << 32) - 1
arch.ZeroRange = zerorange arch.ZeroRange = zerorange
arch.ZeroAuto = zeroAuto
arch.Ginsnop = ginsnop arch.Ginsnop = ginsnop
arch.SSAMarkMoves = ssaMarkMoves arch.SSAMarkMoves = ssaMarkMoves
......
...@@ -37,6 +37,22 @@ func zerorange(pp *gc.Progs, p *obj.Prog, off, cnt int64, ax *uint32) *obj.Prog ...@@ -37,6 +37,22 @@ func zerorange(pp *gc.Progs, p *obj.Prog, off, cnt int64, ax *uint32) *obj.Prog
return p return p
} }
func zeroAuto(pp *gc.Progs, n *gc.Node) {
// Note: this code must not clobber any registers.
sym := gc.Linksym(n.Sym)
size := n.Type.Size()
for i := int64(0); i < size; i += 4 {
p := pp.Prog(x86.AMOVL)
p.From.Type = obj.TYPE_CONST
p.From.Offset = 0
p.To.Type = obj.TYPE_MEM
p.To.Name = obj.NAME_AUTO
p.To.Reg = x86.REG_SP
p.To.Offset = n.Xoffset + i
p.To.Sym = sym
}
}
func ginsnop(pp *gc.Progs) { func ginsnop(pp *gc.Progs) {
p := pp.Prog(x86.AXCHGL) p := pp.Prog(x86.AXCHGL)
p.From.Type = obj.TYPE_REG p.From.Type = obj.TYPE_REG
......
// run
// 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.
// Issue 20029: make sure we zero at VARKILLs of
// ambiguously live variables.
// The ambiguously live variable here is the hiter
// for the inner range loop.
package main
import "runtime"
func f(m map[int]int) {
outer:
for i := 0; i < 10; i++ {
for k := range m {
if k == 5 {
continue outer
}
}
runtime.GC()
break
}
runtime.GC()
}
func main() {
m := map[int]int{1: 2, 2: 3, 3: 4}
f(m)
}
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