| |
| |
| |
|
|
| package escape |
|
|
| import ( |
| "cmd/compile/internal/base" |
| "cmd/compile/internal/ir" |
| "cmd/compile/internal/logopt" |
| "cmd/internal/src" |
| "fmt" |
| "math/bits" |
| "strings" |
| ) |
|
|
| |
| |
| func (b *batch) walkAll() { |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| todo := newQueue(len(b.allLocs) + 3) |
|
|
| enqueue := func(loc *location) { |
| if !loc.queuedWalkAll { |
| loc.queuedWalkAll = true |
| if loc.hasAttr(attrEscapes) { |
| |
| |
| |
| todo.pushFront(loc) |
| } else { |
| todo.pushBack(loc) |
| } |
| } |
| } |
|
|
| for _, loc := range b.allLocs { |
| todo.pushFront(loc) |
| |
| loc.queuedWalkAll = true |
| } |
| todo.pushFront(&b.mutatorLoc) |
| todo.pushFront(&b.calleeLoc) |
| todo.pushFront(&b.heapLoc) |
|
|
| b.mutatorLoc.queuedWalkAll = true |
| b.calleeLoc.queuedWalkAll = true |
| b.heapLoc.queuedWalkAll = true |
|
|
| var walkgen uint32 |
| for todo.len() > 0 { |
| root := todo.popFront() |
| root.queuedWalkAll = false |
| walkgen++ |
| b.walkOne(root, walkgen, enqueue) |
| } |
| } |
|
|
| |
| |
| func (b *batch) walkOne(root *location, walkgen uint32, enqueue func(*location)) { |
| |
| |
| |
| |
|
|
| root.walkgen = walkgen |
| root.derefs = 0 |
| root.dst = nil |
|
|
| if root.hasAttr(attrCalls) { |
| if clo, ok := root.n.(*ir.ClosureExpr); ok { |
| if fn := clo.Func; b.inMutualBatch(fn.Nname) && !fn.ClosureResultsLost() { |
| fn.SetClosureResultsLost(true) |
|
|
| |
| |
| for _, result := range fn.Type().Results() { |
| enqueue(b.oldLoc(result.Nname.(*ir.Name))) |
| } |
| } |
| } |
| } |
|
|
| todo := newQueue(1) |
| todo.pushFront(root) |
|
|
| for todo.len() > 0 { |
| l := todo.popFront() |
| l.queuedWalkOne = 0 |
|
|
| derefs := l.derefs |
| var newAttrs locAttr |
|
|
| |
| addressOf := derefs < 0 |
| if addressOf { |
| |
| |
| |
| |
| derefs = 0 |
|
|
| |
| |
| |
| if b.outlives(root, l) { |
| if !l.hasAttr(attrEscapes) && (logopt.Enabled() || base.Flag.LowerM >= 2) { |
| if base.Flag.LowerM >= 2 { |
| fmt.Printf("%s: %v escapes to heap in %v:\n", base.FmtPos(l.n.Pos()), l.n, ir.FuncName(l.curfn)) |
| } |
| explanation := b.explainPath(root, l) |
| if logopt.Enabled() { |
| var e_curfn *ir.Func |
| logopt.LogOpt(l.n.Pos(), "escape", "escape", ir.FuncName(e_curfn), fmt.Sprintf("%v escapes to heap", l.n), explanation) |
| } |
| } |
| newAttrs |= attrEscapes | attrPersists | attrMutates | attrCalls |
| } else |
| |
| |
| if root.hasAttr(attrPersists) { |
| newAttrs |= attrPersists |
| } |
| } |
|
|
| if derefs == 0 { |
| newAttrs |= root.attrs & (attrMutates | attrCalls) |
| } |
|
|
| |
| |
| |
| |
| |
| if l.param { |
| if b.outlives(root, l) { |
| if !l.hasAttr(attrEscapes) && (logopt.Enabled() || base.Flag.LowerM >= 2) { |
| if base.Flag.LowerM >= 2 { |
| fmt.Printf("%s: parameter %v leaks to %s for %v with derefs=%d:\n", base.FmtPos(l.n.Pos()), l.n, b.explainLoc(root), ir.FuncName(l.curfn), derefs) |
| } |
| explanation := b.explainPath(root, l) |
| if logopt.Enabled() { |
| var e_curfn *ir.Func |
| logopt.LogOpt(l.n.Pos(), "leak", "escape", ir.FuncName(e_curfn), |
| fmt.Sprintf("parameter %v leaks to %s with derefs=%d", l.n, b.explainLoc(root), derefs), explanation) |
| } |
| } |
| l.leakTo(root, derefs) |
| } |
| if root.hasAttr(attrMutates) { |
| l.paramEsc.AddMutator(derefs) |
| } |
| if root.hasAttr(attrCalls) { |
| l.paramEsc.AddCallee(derefs) |
| } |
| } |
|
|
| if newAttrs&^l.attrs != 0 { |
| l.attrs |= newAttrs |
| enqueue(l) |
| if l.attrs&attrEscapes != 0 { |
| continue |
| } |
| } |
|
|
| for i, edge := range l.edges { |
| if edge.src.hasAttr(attrEscapes) { |
| continue |
| } |
| d := derefs + edge.derefs |
| if edge.src.walkgen != walkgen || edge.src.derefs > d { |
| edge.src.walkgen = walkgen |
| edge.src.derefs = d |
| edge.src.dst = l |
| edge.src.dstEdgeIdx = i |
| |
| if edge.src.queuedWalkOne != walkgen { |
| edge.src.queuedWalkOne = walkgen |
|
|
| |
| |
| todo.pushBack(edge.src) |
| } |
| } |
| } |
| } |
| } |
|
|
| |
| func (b *batch) explainPath(root, src *location) []*logopt.LoggedOpt { |
| visited := make(map[*location]bool) |
| pos := base.FmtPos(src.n.Pos()) |
| var explanation []*logopt.LoggedOpt |
| for { |
| |
| if visited[src] { |
| if base.Flag.LowerM >= 2 { |
| fmt.Printf("%s: warning: truncated explanation due to assignment cycle; see golang.org/issue/35518\n", pos) |
| } |
| break |
| } |
| visited[src] = true |
| dst := src.dst |
| edge := &dst.edges[src.dstEdgeIdx] |
| if edge.src != src { |
| base.Fatalf("path inconsistency: %v != %v", edge.src, src) |
| } |
|
|
| explanation = b.explainFlow(pos, dst, src, edge.derefs, edge.notes, explanation) |
|
|
| if dst == root { |
| break |
| } |
| src = dst |
| } |
|
|
| return explanation |
| } |
|
|
| func (b *batch) explainFlow(pos string, dst, srcloc *location, derefs int, notes *note, explanation []*logopt.LoggedOpt) []*logopt.LoggedOpt { |
| ops := "&" |
| if derefs >= 0 { |
| ops = strings.Repeat("*", derefs) |
| } |
| print := base.Flag.LowerM >= 2 |
|
|
| flow := fmt.Sprintf(" flow: %s ← %s%v:", b.explainLoc(dst), ops, b.explainLoc(srcloc)) |
| if print { |
| fmt.Printf("%s:%s\n", pos, flow) |
| } |
| if logopt.Enabled() { |
| var epos src.XPos |
| if notes != nil { |
| epos = notes.where.Pos() |
| } else if srcloc != nil && srcloc.n != nil { |
| epos = srcloc.n.Pos() |
| } |
| var e_curfn *ir.Func |
| explanation = append(explanation, logopt.NewLoggedOpt(epos, epos, "escflow", "escape", ir.FuncName(e_curfn), flow)) |
| } |
|
|
| for note := notes; note != nil; note = note.next { |
| if print { |
| fmt.Printf("%s: from %v (%v) at %s\n", pos, note.where, note.why, base.FmtPos(note.where.Pos())) |
| } |
| if logopt.Enabled() { |
| var e_curfn *ir.Func |
| notePos := note.where.Pos() |
| explanation = append(explanation, logopt.NewLoggedOpt(notePos, notePos, "escflow", "escape", ir.FuncName(e_curfn), |
| fmt.Sprintf(" from %v (%v)", note.where, note.why))) |
| } |
| } |
| return explanation |
| } |
|
|
| func (b *batch) explainLoc(l *location) string { |
| if l == &b.heapLoc { |
| return "{heap}" |
| } |
| if l.n == nil { |
| |
| return "{temp}" |
| } |
| if l.n.Op() == ir.ONAME { |
| return fmt.Sprintf("%v", l.n) |
| } |
| return fmt.Sprintf("{storage for %v}", l.n) |
| } |
|
|
| |
| |
| func (b *batch) outlives(l, other *location) bool { |
| |
| if l.hasAttr(attrEscapes) { |
| return true |
| } |
|
|
| |
| if l == &b.mutatorLoc || l == &b.calleeLoc { |
| return false |
| } |
|
|
| |
| |
| |
| if l.paramOut { |
| |
| |
| |
| |
| |
| |
| |
| if ir.ContainsClosure(other.curfn, l.curfn) && !l.curfn.ClosureResultsLost() { |
| return false |
| } |
|
|
| return true |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| if l.curfn == other.curfn && l.loopDepth < other.loopDepth { |
| return true |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| if ir.ContainsClosure(l.curfn, other.curfn) { |
| return true |
| } |
|
|
| return false |
| } |
|
|
| |
| |
| |
| type queue struct { |
| locs []*location |
| head int |
| tail int |
| elems int |
| } |
|
|
| func newQueue(capacity int) *queue { |
| capacity = max(capacity, 2) |
| capacity = 1 << bits.Len64(uint64(capacity-1)) |
| return &queue{locs: make([]*location, capacity)} |
| } |
|
|
| |
| func (q *queue) pushFront(loc *location) { |
| if q.elems == len(q.locs) { |
| q.grow() |
| } |
| q.head = q.wrap(q.head - 1) |
| q.locs[q.head] = loc |
| q.elems++ |
| } |
|
|
| |
| func (q *queue) pushBack(loc *location) { |
| if q.elems == len(q.locs) { |
| q.grow() |
| } |
| q.locs[q.tail] = loc |
| q.tail = q.wrap(q.tail + 1) |
| q.elems++ |
| } |
|
|
| |
| func (q *queue) popFront() *location { |
| if q.elems == 0 { |
| return nil |
| } |
| loc := q.locs[q.head] |
| q.head = q.wrap(q.head + 1) |
| q.elems-- |
| return loc |
| } |
|
|
| |
| func (q *queue) grow() { |
| newLocs := make([]*location, len(q.locs)*2) |
| for i := range q.elems { |
| |
| newLocs[i] = q.locs[q.wrap(q.head+i)] |
| } |
| q.locs = newLocs |
| q.head = 0 |
| q.tail = q.elems |
| } |
|
|
| func (q *queue) len() int { return q.elems } |
| func (q *queue) wrap(i int) int { return i & (len(q.locs) - 1) } |
|
|