]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gc: fix &result escaping into result
authorRuss Cox <rsc@golang.org>
Wed, 11 Jun 2014 18:21:06 +0000 (14:21 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 11 Jun 2014 18:21:06 +0000 (14:21 -0400)
There is a hierarchy of location defined by loop depth:

        -1 = the heap
        0 = function results
        1 = local variables (and parameters)
        2 = local variable declared inside a loop
        3 = local variable declared inside a loop inside a loop
        etc

In general if an address from loopdepth n is assigned to
something in loop depth m < n, that indicates an extended
lifetime of some form that requires a heap allocation.

Function results can be local variables too, though, and so
they don't actually fit into the hierarchy very well.
Treat the address of a function result as level 1 so that
if it is written back into a result, the address is treated
as escaping.

Fixes #8185.

LGTM=iant
R=iant
CC=golang-codereviews
https://golang.org/cl/108870044

src/cmd/gc/esc.c
test/escape2.go

index 9d7dc1149c718bcdb63a21b9a3d23e2e4bfc47aa..78624d7cbf192132b3f611c947de27b344641491 100644 (file)
@@ -673,12 +673,21 @@ esc(EscState *e, Node *n, Node *up)
                // for &x, use loop depth of x if known.
                // it should always be known, but if not, be conservative
                // and keep the current loop depth.
-               if(n->left->op == ONAME && (n->left->escloopdepth != 0 || n->left->class == PPARAMOUT)) {
+               if(n->left->op == ONAME) {
                        switch(n->left->class) {
                        case PAUTO:
+                               if(n->left->escloopdepth != 0)
+                                       n->escloopdepth = n->left->escloopdepth;
+                               break;
                        case PPARAM:
                        case PPARAMOUT:
-                               n->escloopdepth = n->left->escloopdepth;
+                               // PPARAM is loop depth 1 always.
+                               // PPARAMOUT is loop depth 0 for writes
+                               // but considered loop depth 1 for address-of,
+                               // so that writing the address of one result
+                               // to another (or the same) result makes the
+                               // first result move to the heap.
+                               n->escloopdepth = 1;
                                break;
                        }
                }
index f00741dc2f622157e50815ef85d83f5671461c6c..28251aa98bcfc67d3fbe00897b4fec6141bb6517 100644 (file)
@@ -1478,3 +1478,15 @@ func foo153(v interface{}) *int { // ERROR "leaking param: v"
        }
        panic(0)
 }
+
+// issue 8185 - &result escaping into result
+
+func f() (x int, y *int) { // ERROR "moved to heap: x"
+       y = &x // ERROR "&x escapes to heap"
+       return
+}
+
+func g() (x interface{}) { // ERROR "moved to heap: x"
+       x = &x // ERROR "&x escapes to heap"
+       return
+}