Debian Codegraph @

src/cmd/compile/internal/escape/escape.go

raw
1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package escape
6
7 import (
8 "fmt"
9 "go/constant"
10 "go/token"
11
12 "cmd/compile/internal/base"
13 "cmd/compile/internal/ir"
14 "cmd/compile/internal/logopt"
15 "cmd/compile/internal/typecheck"
16 "cmd/compile/internal/types"
17 "cmd/internal/src"
18 )
19
20 // Escape analysis.
21 //
22 // Here we analyze functions to determine which Go variables
23 // (including implicit allocations such as calls to "new" or "make",
24 // composite literals, etc.) can be allocated on the stack. The two
25 // key invariants we have to ensure are: (1) pointers to stack objects
26 // cannot be stored in the heap, and (2) pointers to a stack object
27 // cannot outlive that object (e.g., because the declaring function
28 // returned and destroyed the object's stack frame, or its space is
29 // reused across loop iterations for logically distinct variables).
30 //
31 // We implement this with a static data-flow analysis of the AST.
32 // First, we construct a directed weighted graph where vertices
33 // (termed "locations") represent variables allocated by statements
34 // and expressions, and edges represent assignments between variables
35 // (with weights representing addressing/dereference counts).
36 //
37 // Next we walk the graph looking for assignment paths that might
38 // violate the invariants stated above. If a variable v's address is
39 // stored in the heap or elsewhere that may outlive it, then v is
40 // marked as requiring heap allocation.
41 //
42 // To support interprocedural analysis, we also record data-flow from
43 // each function's parameters to the heap and to its result
44 // parameters. This information is summarized as "parameter tags",
45 // which are used at static call sites to improve escape analysis of
46 // function arguments.
47
48 // Constructing the location graph.
49 //
50 // Every allocating statement (e.g., variable declaration) or
51 // expression (e.g., "new" or "make") is first mapped to a unique
52 // "location."
53 //
54 // We also model every Go assignment as a directed edges between
55 // locations. The number of dereference operations minus the number of
56 // addressing operations is recorded as the edge's weight (termed
57 // "derefs"). For example:
58 //
59 // p = &q // -1
60 // p = q // 0
61 // p = *q // 1
62 // p = **q // 2
63 //
64 // p = **&**&q // 2
65 //
66 // Note that the & operator can only be applied to addressable
67 // expressions, and the expression &x itself is not addressable, so
68 // derefs cannot go below -1.
69 //
70 // Every Go language construct is lowered into this representation,
71 // generally without sensitivity to flow, path, or context; and
72 // without distinguishing elements within a compound variable. For
73 // example:
74 //
75 // var x struct { f, g *int }
76 // var u []*int
77 //
78 // x.f = u[0]
79 //
80 // is modeled simply as
81 //
82 // x = *u
83 //
84 // That is, we don't distinguish x.f from x.g, or u[0] from u[1],
85 // u[2], etc. However, we do record the implicit dereference involved
86 // in indexing a slice.
87
88 // A batch holds escape analysis state that's shared across an entire
89 // batch of functions being analyzed at once.
90 type batch struct {
91 allLocs []*location
92 closures []closure
93 reassignOracles map[*ir.Func]*ir.ReassignOracle
94
95 heapLoc location
96 mutatorLoc location
97 calleeLoc location
98 blankLoc location
99 }
100
101 // A closure holds a closure expression and its spill hole (i.e.,
102 // where the hole representing storing into its closure record).
103 type closure struct {
104 k hole
105 clo *ir.ClosureExpr
106 }
107
108 // An escape holds state specific to a single function being analyzed
109 // within a batch.
110 type escape struct {
111 *batch
112
113 curfn *ir.Func // function being analyzed
114
115 labels map[*types.Sym]labelState // known labels
116
117 // loopDepth counts the current loop nesting depth within
118 // curfn. It increments within each "for" loop and at each
119 // label with a corresponding backwards "goto" (i.e.,
120 // unstructured loop).
121 loopDepth int
122 }
123
124 func Funcs(all []*ir.Func) {
125 // Make a cache of ir.ReassignOracles. The cache is lazily populated.
126 // TODO(thepudds): consider adding a field on ir.Func instead. We might also be able
127 // to use that field elsewhere, like in walk. See discussion in https://go.dev/cl/688075.
128 reassignOracles := make(map[*ir.Func]*ir.ReassignOracle)
129
130 ir.VisitFuncsBottomUp(all, func(list []*ir.Func, recursive bool) {
131 Batch(list, reassignOracles)
132 })
133 }
134
135 // Batch performs escape analysis on a minimal batch of
136 // functions.
137 func Batch(fns []*ir.Func, reassignOracles map[*ir.Func]*ir.ReassignOracle) {
138 var b batch
139 b.heapLoc.attrs = attrEscapes | attrPersists | attrMutates | attrCalls
140 b.mutatorLoc.attrs = attrMutates
141 b.calleeLoc.attrs = attrCalls
142 b.reassignOracles = reassignOracles
143
144 // Construct data-flow graph from syntax trees.
145 for _, fn := range fns {
146 if base.Flag.W > 1 {
147 s := fmt.Sprintf("\nbefore escape %v", fn)
148 ir.Dump(s, fn)
149 }
150 b.initFunc(fn)
151 }
152 for _, fn := range fns {
153 if !fn.IsClosure() {
154 b.walkFunc(fn)
155 }
156 }
157
158 // We've walked the function bodies, so we've seen everywhere a
159 // variable might be reassigned or have its address taken. Now we
160 // can decide whether closures should capture their free variables
161 // by value or reference.
162 for _, closure := range b.closures {
163 b.flowClosure(closure.k, closure.clo)
164 }
165 b.closures = nil
166
167 for _, loc := range b.allLocs {
168 // Try to replace some non-constant expressions with literals.
169 b.rewriteWithLiterals(loc.n, loc.curfn)
170
171 // Check if the node must be heap allocated for certain reasons
172 // such as OMAKESLICE for a large slice.
173 if why := HeapAllocReason(loc.n); why != "" {
174 b.flow(b.heapHole().addr(loc.n, why), loc)
175 }
176 }
177
178 b.walkAll()
179 b.finish(fns)
180 }
181
182 func (b *batch) with(fn *ir.Func) *escape {
183 return &escape{
184 batch: b,
185 curfn: fn,
186 loopDepth: 1,
187 }
188 }
189
190 func (b *batch) initFunc(fn *ir.Func) {
191 e := b.with(fn)
192 if fn.Esc() != escFuncUnknown {
193 base.Fatalf("unexpected node: %v", fn)
194 }
195 fn.SetEsc(escFuncPlanned)
196 if base.Flag.LowerM > 3 {
197 ir.Dump("escAnalyze", fn)
198 }
199
200 // Allocate locations for local variables.
201 for _, n := range fn.Dcl {
202 e.newLoc(n, true)
203 }
204
205 // Also for hidden parameters (e.g., the ".this" parameter to a
206 // method value wrapper).
207 if fn.OClosure == nil {
208 for _, n := range fn.ClosureVars {
209 e.newLoc(n.Canonical(), true)
210 }
211 }
212
213 // Initialize resultIndex for result parameters.
214 for i, f := range fn.Type().Results() {
215 e.oldLoc(f.Nname.(*ir.Name)).resultIndex = 1 + i
216 }
217 }
218
219 func (b *batch) walkFunc(fn *ir.Func) {
220 e := b.with(fn)
221 fn.SetEsc(escFuncStarted)
222
223 // Identify labels that mark the head of an unstructured loop.
224 ir.Visit(fn, func(n ir.Node) {
225 switch n.Op() {
226 case ir.OLABEL:
227 n := n.(*ir.LabelStmt)
228 if n.Label.IsBlank() {
229 break
230 }
231 if e.labels == nil {
232 e.labels = make(map[*types.Sym]labelState)
233 }
234 e.labels[n.Label] = nonlooping
235
236 case ir.OGOTO:
237 // If we visited the label before the goto,
238 // then this is a looping label.
239 n := n.(*ir.BranchStmt)
240 if e.labels[n.Label] == nonlooping {
241 e.labels[n.Label] = looping
242 }
243 }
244 })
245
246 e.block(fn.Body)
247
248 if len(e.labels) != 0 {
249 base.FatalfAt(fn.Pos(), "leftover labels after walkFunc")
250 }
251 }
252
253 func (b *batch) flowClosure(k hole, clo *ir.ClosureExpr) {
254 for _, cv := range clo.Func.ClosureVars {
255 n := cv.Canonical()
256 loc := b.oldLoc(cv)
257 if !loc.captured {
258 base.FatalfAt(cv.Pos(), "closure variable never captured: %v", cv)
259 }
260
261 // Capture by value for variables <= 128 bytes that are never reassigned.
262 n.SetByval(!loc.addrtaken && !loc.reassigned && n.Type().Size() <= 128)
263 if !n.Byval() {
264 n.SetAddrtaken(true)
265 if n.Sym().Name == typecheck.LocalDictName {
266 base.FatalfAt(n.Pos(), "dictionary variable not captured by value")
267 }
268 }
269
270 if base.Flag.LowerM > 1 {
271 how := "ref"
272 if n.Byval() {
273 how = "value"
274 }
275 base.WarnfAt(n.Pos(), "%v capturing by %s: %v (addr=%v assign=%v width=%d)", n.Curfn, how, n, loc.addrtaken, loc.reassigned, n.Type().Size())
276 }
277
278 // Flow captured variables to closure.
279 k := k
280 if !cv.Byval() {
281 k = k.addr(cv, "reference")
282 }
283 b.flow(k.note(cv, "captured by a closure"), loc)
284 }
285 }
286
287 func (b *batch) finish(fns []*ir.Func) {
288 // Record parameter tags for package export data.
289 for _, fn := range fns {
290 fn.SetEsc(escFuncTagged)
291
292 for i, param := range fn.Type().RecvParams() {
293 param.Note = b.paramTag(fn, 1+i, param)
294 }
295 }
296
297 for _, loc := range b.allLocs {
298 n := loc.n
299 if n == nil {
300 continue
301 }
302
303 if n.Op() == ir.ONAME {
304 n := n.(*ir.Name)
305 n.Opt = nil
306 }
307
308 // Update n.Esc based on escape analysis results.
309
310 // Omit escape diagnostics for go/defer wrappers, at least for now.
311 // Historically, we haven't printed them, and test cases don't expect them.
312 // TODO(mdempsky): Update tests to expect this.
313 goDeferWrapper := n.Op() == ir.OCLOSURE && n.(*ir.ClosureExpr).Func.Wrapper()
314
315 if loc.hasAttr(attrEscapes) {
316 if n.Op() == ir.ONAME {
317 if base.Flag.CompilingRuntime {
318 base.ErrorfAt(n.Pos(), 0, "%v escapes to heap, not allowed in runtime", n)
319 }
320 if base.Flag.LowerM != 0 {
321 base.WarnfAt(n.Pos(), "moved to heap: %v", n)
322 }
323 } else {
324 if base.Flag.LowerM != 0 && !goDeferWrapper {
325 if n.Op() == ir.OAPPEND {
326 base.WarnfAt(n.Pos(), "append escapes to heap")
327 } else {
328 base.WarnfAt(n.Pos(), "%v escapes to heap", n)
329 }
330 }
331 if logopt.Enabled() {
332 var e_curfn *ir.Func // TODO(mdempsky): Fix.
333 logopt.LogOpt(n.Pos(), "escape", "escape", ir.FuncName(e_curfn))
334 }
335 }
336 n.SetEsc(ir.EscHeap)
337 } else {
338 if base.Flag.LowerM != 0 && n.Op() != ir.ONAME && !goDeferWrapper {
339 if n.Op() == ir.OAPPEND {
340 base.WarnfAt(n.Pos(), "append does not escape")
341 } else {
342 base.WarnfAt(n.Pos(), "%v does not escape", n)
343 }
344 }
345 n.SetEsc(ir.EscNone)
346 if !loc.hasAttr(attrPersists) {
347 switch n.Op() {
348 case ir.OCLOSURE:
349 n := n.(*ir.ClosureExpr)
350 n.SetTransient(true)
351 case ir.OMETHVALUE:
352 n := n.(*ir.SelectorExpr)
353 n.SetTransient(true)
354 case ir.OSLICELIT:
355 n := n.(*ir.CompLitExpr)
356 n.SetTransient(true)
357 }
358 }
359 }
360
361 // If the result of a string->[]byte conversion is never mutated,
362 // then it can simply reuse the string's memory directly.
363 if base.Debug.ZeroCopy != 0 {
364 if n, ok := n.(*ir.ConvExpr); ok && n.Op() == ir.OSTR2BYTES && !loc.hasAttr(attrMutates) {
365 if base.Flag.LowerM >= 1 {
366 base.WarnfAt(n.Pos(), "zero-copy string->[]byte conversion")
367 }
368 n.SetOp(ir.OSTR2BYTESTMP)
369 }
370 }
371 }
372 }
373
374 // inMutualBatch reports whether function fn is in the batch of
375 // mutually recursive functions being analyzed. When this is true,
376 // fn has not yet been analyzed, so its parameters and results
377 // should be incorporated directly into the flow graph instead of
378 // relying on its escape analysis tagging.
379 func (b *batch) inMutualBatch(fn *ir.Name) bool {
380 if fn.Defn != nil && fn.Defn.Esc() < escFuncTagged {
381 if fn.Defn.Esc() == escFuncUnknown {
382 base.FatalfAt(fn.Pos(), "graph inconsistency: %v", fn)
383 }
384 return true
385 }
386 return false
387 }
388
389 const (
390 escFuncUnknown = 0 + iota
391 escFuncPlanned
392 escFuncStarted
393 escFuncTagged
394 )
395
396 // Mark labels that have no backjumps to them as not increasing e.loopdepth.
397 type labelState int
398
399 const (
400 looping labelState = 1 + iota
401 nonlooping
402 )
403
404 func (b *batch) paramTag(fn *ir.Func, narg int, f *types.Field) string {
405 name := func() string {
406 if f.Nname != nil {
407 return f.Nname.Sym().Name
408 }
409 return fmt.Sprintf("arg#%d", narg)
410 }
411
412 // Only report diagnostics for user code;
413 // not for wrappers generated around them.
414 // TODO(mdempsky): Generalize this.
415 diagnose := base.Flag.LowerM != 0 && !(fn.Wrapper() || fn.Dupok())
416
417 if len(fn.Body) == 0 {
418 // Assume that uintptr arguments must be held live across the call.
419 // This is most important for syscall.Syscall.
420 // See golang.org/issue/13372.
421 // This really doesn't have much to do with escape analysis per se,
422 // but we are reusing the ability to annotate an individual function
423 // argument and pass those annotations along to importing code.
424 fn.Pragma |= ir.UintptrKeepAlive
425
426 if f.Type.IsUintptr() {
427 if diagnose {
428 base.WarnfAt(f.Pos, "assuming %v is unsafe uintptr", name())
429 }
430 return ""
431 }
432
433 if !f.Type.HasPointers() { // don't bother tagging for scalars
434 return ""
435 }
436
437 var esc leaks
438
439 // External functions are assumed unsafe, unless
440 // //go:noescape is given before the declaration.
441 if fn.Pragma&ir.Noescape != 0 {
442 if diagnose && f.Sym != nil {
443 base.WarnfAt(f.Pos, "%v does not escape", name())
444 }
445 esc.AddMutator(0)
446 esc.AddCallee(0)
447 } else {
448 if diagnose && f.Sym != nil {
449 base.WarnfAt(f.Pos, "leaking param: %v", name())
450 }
451 esc.AddHeap(0)
452 }
453
454 return esc.Encode()
455 }
456
457 if fn.Pragma&ir.UintptrEscapes != 0 {
458 if f.Type.IsUintptr() {
459 if diagnose {
460 base.WarnfAt(f.Pos, "marking %v as escaping uintptr", name())
461 }
462 return ""
463 }
464 if f.IsDDD() && f.Type.Elem().IsUintptr() {
465 // final argument is ...uintptr.
466 if diagnose {
467 base.WarnfAt(f.Pos, "marking %v as escaping ...uintptr", name())
468 }
469 return ""
470 }
471 }
472
473 if !f.Type.HasPointers() { // don't bother tagging for scalars
474 return ""
475 }
476
477 // Unnamed parameters are unused and therefore do not escape.
478 if f.Sym == nil || f.Sym.IsBlank() {
479 var esc leaks
480 return esc.Encode()
481 }
482
483 n := f.Nname.(*ir.Name)
484 loc := b.oldLoc(n)
485 esc := loc.paramEsc
486 esc.Optimize()
487
488 if diagnose && !loc.hasAttr(attrEscapes) {
489 b.reportLeaks(f.Pos, name(), esc, fn.Type())
490 }
491
492 return esc.Encode()
493 }
494
495 func (b *batch) reportLeaks(pos src.XPos, name string, esc leaks, sig *types.Type) {
496 warned := false
497 if x := esc.Heap(); x >= 0 {
498 if x == 0 {
499 base.WarnfAt(pos, "leaking param: %v", name)
500 } else {
501 // TODO(mdempsky): Mention level=x like below?
502 base.WarnfAt(pos, "leaking param content: %v", name)
503 }
504 warned = true
505 }
506 for i := 0; i < numEscResults; i++ {
507 if x := esc.Result(i); x >= 0 {
508 res := sig.Result(i).Nname.Sym().Name
509 base.WarnfAt(pos, "leaking param: %v to result %v level=%d", name, res, x)
510 warned = true
511 }
512 }
513
514 if base.Debug.EscapeMutationsCalls <= 0 {
515 if !warned {
516 base.WarnfAt(pos, "%v does not escape", name)
517 }
518 return
519 }
520
521 if x := esc.Mutator(); x >= 0 {
522 base.WarnfAt(pos, "mutates param: %v derefs=%v", name, x)
523 warned = true
524 }
525 if x := esc.Callee(); x >= 0 {
526 base.WarnfAt(pos, "calls param: %v derefs=%v", name, x)
527 warned = true
528 }
529
530 if !warned {
531 base.WarnfAt(pos, "%v does not escape, mutate, or call", name)
532 }
533 }
534
535 // rewriteWithLiterals attempts to replace certain non-constant expressions
536 // within n with a literal if possible.
537 func (b *batch) rewriteWithLiterals(n ir.Node, fn *ir.Func) {
538 if n == nil || fn == nil {
539 return
540 }
541
542 assignTemp := func(pos src.XPos, n ir.Node, init *ir.Nodes) {
543 // Preserve any side effects of n by assigning it to an otherwise unused temp.
544 tmp := typecheck.TempAt(pos, fn, n.Type())
545 init.Append(typecheck.Stmt(ir.NewDecl(pos, ir.ODCL, tmp)))
546 init.Append(typecheck.Stmt(ir.NewAssignStmt(pos, tmp, n)))
547 }
548
549 switch n.Op() {
550 case ir.OMAKESLICE:
551 // Check if we can replace a non-constant argument to make with
552 // a literal to allow for this slice to be stack allocated if otherwise allowed.
553 n := n.(*ir.MakeExpr)
554
555 r := &n.Cap
556 if n.Cap == nil {
557 r = &n.Len
558 }
559
560 if (*r).Op() != ir.OLITERAL {
561 // Look up a cached ReassignOracle for the function, lazily computing one if needed.
562 ro := b.reassignOracle(fn)
563 if ro == nil {
564 base.Fatalf("no ReassignOracle for function %v with closure parent %v", fn, fn.ClosureParent)
565 }
566 if s := ro.StaticValue(*r); s.Op() == ir.OLITERAL {
567 lit, ok := s.(*ir.BasicLit)
568 if !ok || lit.Val().Kind() != constant.Int {
569 base.Fatalf("unexpected BasicLit Kind")
570 }
571 if constant.Compare(lit.Val(), token.GEQ, constant.MakeInt64(0)) {
572 if !base.LiteralAllocHash.MatchPos(n.Pos(), nil) {
573 // De-selected by literal alloc optimizations debug hash.
574 return
575 }
576 // Preserve any side effects of the original expression, then replace it.
577 assignTemp(n.Pos(), *r, n.PtrInit())
578 *r = ir.NewBasicLit(n.Pos(), (*r).Type(), lit.Val())
579 }
580 }
581 }
582 case ir.OCONVIFACE:
583 // Check if we can replace a non-constant expression in an interface conversion with
584 // a literal to avoid heap allocating the underlying interface value.
585 conv := n.(*ir.ConvExpr)
586 if conv.X.Op() != ir.OLITERAL && !conv.X.Type().IsInterface() {
587 // TODO(thepudds): likely could avoid some work by tightening the check of conv.X's type.
588 // Look up a cached ReassignOracle for the function, lazily computing one if needed.
589 ro := b.reassignOracle(fn)
590 if ro == nil {
591 base.Fatalf("no ReassignOracle for function %v with closure parent %v", fn, fn.ClosureParent)
592 }
593 v := ro.StaticValue(conv.X)
594 if v != nil && v.Op() == ir.OLITERAL && ir.ValidTypeForConst(conv.X.Type(), v.Val()) {
595 if !base.LiteralAllocHash.MatchPos(n.Pos(), nil) {
596 // De-selected by literal alloc optimizations debug hash.
597 return
598 }
599 if base.Debug.EscapeDebug >= 3 {
600 base.WarnfAt(n.Pos(), "rewriting OCONVIFACE value from %v (%v) to %v (%v)", conv.X, conv.X.Type(), v, v.Type())
601 }
602 // Preserve any side effects of the original expression, then replace it.
603 assignTemp(conv.Pos(), conv.X, conv.PtrInit())
604 v := v.(*ir.BasicLit)
605 conv.X = ir.NewBasicLit(conv.Pos(), conv.X.Type(), v.Val())
606 typecheck.Expr(conv)
607 }
608 }
609 }
610 }
611
612 // reassignOracle returns an initialized *ir.ReassignOracle for fn.
613 // If fn is a closure, it returns the ReassignOracle for the ultimate parent.
614 //
615 // A new ReassignOracle is initialized lazily if needed, and the result
616 // is cached to reduce duplicative work of preparing a ReassignOracle.
617 func (b *batch) reassignOracle(fn *ir.Func) *ir.ReassignOracle {
618 if ro, ok := b.reassignOracles[fn]; ok {
619 return ro // Hit.
620 }
621
622 // For closures, we want the ultimate parent's ReassignOracle,
623 // so walk up the parent chain, if any.
624 f := fn
625 for f.ClosureParent != nil && !f.ClosureParent.IsPackageInit() {
626 f = f.ClosureParent
627 }
628
629 if f != fn {
630 // We found a parent.
631 ro := b.reassignOracles[f]
632 if ro != nil {
633 // Hit, via a parent. Before returning, store this ro for the original fn as well.
634 b.reassignOracles[fn] = ro
635 return ro
636 }
637 }
638
639 // Miss. We did not find a ReassignOracle for fn or a parent, so lazily create one.
640 ro := &ir.ReassignOracle{}
641 ro.Init(f)
642
643 // Cache the answer for the original fn.
644 b.reassignOracles[fn] = ro
645 if f != fn {
646 // Cache for the parent as well.
647 b.reassignOracles[f] = ro
648 }
649 return ro
650 }