Skip to content

Commit a51e4cc

Browse files
khrrandall77
khr
authored andcommitted
cmd/compile: zero return parameters earlier
Move the zeroing of results earlier. In particular, they need to come before any move-to-heap operations, as those require allocation. Those allocations are points at which the GC can see the uninitialized result slots. For the function: func f() (x, y, z *int) { defer(){}() escape(&y) return } We used to generate code like this: x = nil y = nil &y = new(int) z = nil Now we will generate: x = nil y = nil z = nil &y = new(int) Since the fix for #18860, the return slots are always live if there is a defer, so the former ordering allowed the GC to see junk in the z slot. Fixes #19078 Change-Id: I71554ae437549725bb79e13b2c100b2911d47ed4 Reviewed-on: https://go-review.googlesource.com/38133 Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 27492a2 commit a51e4cc

File tree

3 files changed

+73
-14
lines changed

3 files changed

+73
-14
lines changed

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -1106,11 +1106,15 @@ func livenessepilogue(lv *Liveness) {
11061106
for i, n := range lv.vars {
11071107
if n.Class == PPARAMOUT {
11081108
if n.IsOutputParamHeapAddr() {
1109-
// Just to be paranoid.
1109+
// Just to be paranoid. Heap addresses are PAUTOs.
11101110
Fatalf("variable %v both output param and heap output param", n)
11111111
}
1112-
// Needzero not necessary, as the compiler
1113-
// explicitly zeroes output vars at start of fn.
1112+
if n.Name.Param.Heapaddr != nil {
1113+
// If this variable moved to the heap, then
1114+
// its stack copy is not live.
1115+
continue
1116+
}
1117+
// Note: zeroing is handled by zeroResults in walk.go.
11141118
livedefer.Set(int32(i))
11151119
}
11161120
if n.IsOutputParamHeapAddr() {

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

+24-11
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func walk(fn *Node) {
6868
dumplist(s, Curfn.Nbody)
6969
}
7070

71+
zeroResults()
7172
heapmoves()
7273
if Debug['W'] != 0 && Curfn.Func.Enter.Len() > 0 {
7374
s := fmt.Sprintf("enter %v", Curfn.Func.Nname.Sym)
@@ -2466,20 +2467,9 @@ func vmatch1(l *Node, r *Node) bool {
24662467

24672468
// paramstoheap returns code to allocate memory for heap-escaped parameters
24682469
// and to copy non-result prameters' values from the stack.
2469-
// If out is true, then code is also produced to zero-initialize their
2470-
// stack memory addresses.
24712470
func paramstoheap(params *Type) []*Node {
24722471
var nn []*Node
24732472
for _, t := range params.Fields().Slice() {
2474-
// For precise stacks, the garbage collector assumes results
2475-
// are always live, so zero them always.
2476-
if params.StructType().Funarg == FunargResults {
2477-
// Defer might stop a panic and show the
2478-
// return values as they exist at the time of panic.
2479-
// Make sure to zero them on entry to the function.
2480-
nn = append(nn, nod(OAS, nodarg(t, 1), nil))
2481-
}
2482-
24832473
v := t.Nname
24842474
if v != nil && v.Sym != nil && strings.HasPrefix(v.Sym.Name, "~r") { // unnamed result
24852475
v = nil
@@ -2499,6 +2489,29 @@ func paramstoheap(params *Type) []*Node {
24992489
return nn
25002490
}
25012491

2492+
// zeroResults zeros the return values at the start of the function.
2493+
// We need to do this very early in the function. Defer might stop a
2494+
// panic and show the return values as they exist at the time of
2495+
// panic. For precise stacks, the garbage collector assumes results
2496+
// are always live, so we need to zero them before any allocations,
2497+
// even allocations to move params/results to the heap.
2498+
// The generated code is added to Curfn's Enter list.
2499+
func zeroResults() {
2500+
lno := lineno
2501+
lineno = Curfn.Pos
2502+
for _, f := range Curfn.Type.Results().Fields().Slice() {
2503+
if v := f.Nname; v != nil && v.Name.Param.Heapaddr != nil {
2504+
// The local which points to the return value is the
2505+
// thing that needs zeroing. This is already handled
2506+
// by a Needzero annotation in plive.go:livenessepilogue.
2507+
continue
2508+
}
2509+
// Zero the stack location containing f.
2510+
Curfn.Func.Enter.Append(nod(OAS, nodarg(f, 1), nil))
2511+
}
2512+
lineno = lno
2513+
}
2514+
25022515
// returnsfromheap returns code to copy values for heap-escaped parameters
25032516
// back to the stack.
25042517
func returnsfromheap(params *Type) []*Node {

test/fixedbugs/issue19078.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// run
2+
3+
// Copyright 2017 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+
// Issue 19078: liveness & zero-initialization of results
8+
// when there is a defer.
9+
package main
10+
11+
import "unsafe"
12+
13+
func main() {
14+
// Construct an invalid pointer. We do this by
15+
// making a pointer which points to the unused space
16+
// between the last 48-byte object in a span and the
17+
// end of the span (there are 32 unused bytes there).
18+
p := new([48]byte) // make a 48-byte object
19+
sink = &p // escape it, so it allocates for real
20+
u := uintptr(unsafe.Pointer(p)) // get its address
21+
u = u >> 13 << 13 // round down to page size
22+
u += 1<<13 - 1 // add almost a page
23+
24+
for i := 0; i < 1000000; i++ {
25+
_ = identity(u) // installs u at return slot
26+
_ = liveReturnSlot(nil) // incorrectly marks return slot as live
27+
}
28+
}
29+
30+
//go:noinline
31+
func liveReturnSlot(x *int) *int {
32+
defer func() {}() // causes return slot to be marked live
33+
sink = &x // causes x to be moved to the heap, triggering allocation
34+
return x
35+
}
36+
37+
//go:noinline
38+
func identity(x uintptr) uintptr {
39+
return x
40+
}
41+
42+
var sink interface{}

0 commit comments

Comments
 (0)