...
Run Format

Source file test/escape_because.go

Documentation: test

     1  // errorcheck -0 -m -m -l
     2  
     3  // Copyright 2015 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  // Note the doubled -m; this tests the "because" explanations for escapes,
     8  // and is likely to be annoyingly fragile under compiler change.
     9  // As long as the explanations look reasonably sane, meaning eyeball verify output of
    10  //    go build -gcflags '-l -m -m' escape_because.go
    11  // and investigate changes, feel free to update with
    12  //    go run run.go -update_errors -- escape_because.go
    13  
    14  package main
    15  
    16  func main() {
    17  }
    18  
    19  var sink interface{}
    20  
    21  type pair struct {
    22  	x, y *int
    23  }
    24  
    25  type Pairy interface {
    26  	EqualParts() bool
    27  }
    28  
    29  func (p *pair) EqualParts() bool { // ERROR "\(\*pair\).EqualParts p does not escape$"
    30  	return p != nil && (p.x == p.y || *p.x == *p.y)
    31  }
    32  
    33  func f1(p *int) { // ERROR "from \[3\]\*int literal \(array literal element\) at escape_because.go:34$" "from a \(assigned\) at escape_because.go:34$" "from a \(interface-converted\) at escape_because.go:35$" "from sink \(assigned to top level variable\) at escape_because.go:35$" "leaking param: p$"
    34  	a := [3]*int{p, nil, nil}
    35  	sink = a // ERROR "a escapes to heap$" "from sink \(assigned to top level variable\) at escape_because.go:35$"
    36  
    37  }
    38  
    39  func f2(q *int) { // ERROR "from &u \(address-of\) at escape_because.go:43$" "from &u \(interface-converted\) at escape_because.go:43$" "from pair literal \(struct literal element\) at escape_because.go:41$" "from s \(assigned\) at escape_because.go:40$" "from sink \(assigned to top level variable\) at escape_because.go:43$" "from t \(assigned\) at escape_because.go:41$" "from u \(assigned\) at escape_because.go:42$" "leaking param: q$"
    40  	s := q
    41  	t := pair{s, nil}
    42  	u := t    // ERROR "moved to heap: u$"
    43  	sink = &u // ERROR "&u escapes to heap$" "from &u \(interface-converted\) at escape_because.go:43$" "from sink \(assigned to top level variable\) at escape_because.go:43$"
    44  }
    45  
    46  func f3(r *int) interface{} { // ERROR "from \[\]\*int literal \(slice-literal-element\) at escape_because.go:47$" "from c \(assigned\) at escape_because.go:47$" "from c \(interface-converted\) at escape_because.go:48$" "from ~r1 \(return\) at escape_because.go:48$" "leaking param: r to result ~r1 level=-1$"
    47  	c := []*int{r} // ERROR "\[\]\*int literal escapes to heap$" "from c \(assigned\) at escape_because.go:47$" "from c \(interface-converted\) at escape_because.go:48$" "from ~r1 \(return\) at escape_because.go:48$"
    48  	return c       // "return" // ERROR "c escapes to heap$" "from ~r1 \(return\) at escape_because.go:48$"
    49  }
    50  
    51  func f4(a *int, s []*int) int { // ERROR "from \*s \(indirection\) at escape_because.go:51$" "from append\(s, a\) \(appended to slice\) at escape_because.go:52$" "from append\(s, a\) \(appendee slice\) at escape_because.go:52$" "leaking param content: s$" "leaking param: a$"
    52  	s = append(s, a)
    53  	return *(s[0])
    54  }
    55  
    56  func f5(s1, s2 []*int) int { // ERROR "from \*s1 \(indirection\) at escape_because.go:56$" "from \*s2 \(indirection\) at escape_because.go:56$" "from append\(s1, s2...\) \(appended slice...\) at escape_because.go:57$" "from append\(s1, s2...\) \(appendee slice\) at escape_because.go:57$" "leaking param content: s1$" "leaking param content: s2$"
    57  	s1 = append(s1, s2...)
    58  	return *(s1[0])
    59  }
    60  
    61  func f6(x, y *int) bool { // ERROR "f6 x does not escape$" "f6 y does not escape$"
    62  	p := pair{x, y}
    63  	var P Pairy = &p // ERROR "f6 &p does not escape$"
    64  	pp := P.(*pair)
    65  	return pp.EqualParts()
    66  }
    67  
    68  func f7(x map[int]*int, y int) *int { // ERROR "f7 x does not escape$"
    69  	z, ok := x[y]
    70  	if !ok {
    71  		return nil
    72  	}
    73  	return z
    74  }
    75  
    76  func f8(x int, y *int) *int { // ERROR "from ~r2 \(return\) at escape_because.go:78$" "from ~r2 \(returned from recursive function\) at escape_because.go:76$" "leaking param: y$" "moved to heap: x$"
    77  	if x <= 0 {
    78  		return y
    79  	}
    80  	x--
    81  	return f8(*y, &x) // ERROR "&x escapes to heap$" "from y \(arg to recursive call\) at escape_because.go:81$" "from ~r2 \(return\) at escape_because.go:78$" "from ~r2 \(returned from recursive function\) at escape_because.go:76$"
    82  }
    83  
    84  func f9(x int, y ...*int) *int { // ERROR "from y\[0\] \(dot of pointer\) at escape_because.go:86$" "from ~r2 \(return\) at escape_because.go:86$" "from ~r2 \(returned from recursive function\) at escape_because.go:84$" "leaking param content: y$" "leaking param: y to result ~r2 level=1$" "moved to heap: x$"
    85  	if x <= 0 {
    86  		return y[0]
    87  	}
    88  	x--
    89  	return f9(*y[0], &x) // ERROR "&x escapes to heap$" "f9 ... argument does not escape$" "from ... argument \(... arg to recursive call\) at escape_because.go:89$"
    90  }
    91  
    92  func f10(x map[*int]*int, y, z *int) *int { // ERROR "f10 x does not escape$" "from x\[y\] \(key of map put\) at escape_because.go:93$" "from x\[y\] \(value of map put\) at escape_because.go:93$" "leaking param: y$" "leaking param: z$"
    93  	x[y] = z
    94  	return z
    95  }
    96  
    97  func f11(x map[*int]*int, y, z *int) map[*int]*int { // ERROR "f11 x does not escape$" "from map\[\*int\]\*int literal \(map literal key\) at escape_because.go:98$" "from map\[\*int\]\*int literal \(map literal value\) at escape_because.go:98$" "leaking param: y$" "leaking param: z$"
    98  	return map[*int]*int{y: z} // ERROR "from ~r3 \(return\) at escape_because.go:98$" "map\[\*int\]\*int literal escapes to heap$"
    99  }
   100  
   101  func f12() {
   102  	b := []byte("test") // ERROR "\(\[\]byte\)\(.test.\) escapes to heap$" "from b \(assigned\) at escape_because.go:102$" "from b \(passed to call\[argument escapes\]\) at escape_because.go:103$"
   103  	escape(b)
   104  }
   105  
   106  func escape(b []byte) { // ERROR "from panic\(b\) \(panic\) at escape_because.go:107$" "leaking param: b$"
   107  	panic(b)
   108  }
   109  
   110  func f13() {
   111  	b := []byte("test") // ERROR "\(\[\]byte\)\(.test.\) escapes to heap$" "from .out0 \(passed-to-and-returned-from-call\) at escape_because.go:112$" "from b \(assigned\) at escape_because.go:111$" "from c \(assigned\) at escape_because.go:112$" "from c \(passed to call\[argument escapes\]\) at escape_because.go:113$"
   112  	c := transmit(b)
   113  	escape(c)
   114  }
   115  
   116  //go:noinline
   117  func transmit(b []byte) []byte { // ERROR "from ~r1 \(return\) at escape_because.go:118$" "leaking param: b to result ~r1 level=0$"
   118  	return b
   119  }
   120  
   121  func f14() {
   122  	n := 32
   123  	s1 := make([]int, n)    // ERROR "make\(\[\]int, n\) escapes to heap" "from make\(\[\]int, n\) \(non-constant size\)"
   124  	s2 := make([]int, 0, n) // ERROR "make\(\[\]int, 0, n\) escapes to heap" "from make\(\[\]int, 0, n\) \(non-constant size\)"
   125  	_, _ = s1, s2
   126  }
   127  
   128  // The list below is all of the why-escapes messages seen building the escape analysis tests.
   129  /*
   130     for i in escape*go ; do echo compile $i; go build -gcflags '-l -m -m' $i >& `basename $i .go`.log ; done
   131     grep 'from .* at ' escape*.log | sed -e 's/^.*(\([^()]*\))[^()]*$/\1/' | sort -u
   132  */
   133  // sed RE above assumes that (reason) is the last parenthesized phrase in the line,
   134  // and that none of the reasons contains any parentheses
   135  
   136  /*
   137  ... arg to recursive call
   138  address-of
   139  appended slice...
   140  appended to slice
   141  appendee slice
   142  arg to ...
   143  arg to recursive call
   144  array-element-equals
   145  array literal element
   146  assigned
   147  assigned to top level variable
   148  assign-pair-dot-type
   149  assign-pair-func-call
   150  captured by a closure
   151  captured by called closure
   152  dot
   153  dot-equals
   154  dot of pointer
   155  fixed-array-index-of
   156  go func arg
   157  indirection
   158  interface-converted
   159  key of map put
   160  map literal key
   161  map literal value
   162  non-constant size
   163  panic
   164  parameter to indirect call
   165  passed-to-and-returned-from-call
   166  passed to call[argument content escapes]
   167  passed to call[argument escapes]
   168  pointer literal
   169  range-deref
   170  receiver in indirect call
   171  return
   172  returned from recursive function
   173  slice-element-equals
   174  slice-literal-element
   175  star-dot-equals
   176  star-equals
   177  struct literal element
   178  too large for stack
   179  value of map put
   180  */
   181  
   182  // Expected, but not yet seen (they may be unreachable):
   183  
   184  /*
   185  append-first-arg
   186  assign-pair-mapr
   187  assign-pair-receive
   188  call receiver
   189  map index
   190  pointer literal [assign]
   191  slice literal element
   192  */
   193  

View as plain text