slicing.go (1109B)
1 // Copyright 2021 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 vulncheck 6 7 import ( 8 "golang.org/x/tools/go/callgraph" 9 "golang.org/x/tools/go/ssa" 10 ) 11 12 // forwardSlice computes the transitive closure of functions forward reachable 13 // via calls in cg or referred to in an instruction starting from `sources`. 14 func forwardSlice(sources map[*ssa.Function]bool, cg *callgraph.Graph) map[*ssa.Function]bool { 15 seen := make(map[*ssa.Function]bool) 16 var visit func(f *ssa.Function) 17 visit = func(f *ssa.Function) { 18 if seen[f] { 19 return 20 } 21 seen[f] = true 22 23 if n := cg.Nodes[f]; n != nil { 24 for _, e := range n.Out { 25 if e.Site != nil { 26 visit(e.Callee.Func) 27 } 28 } 29 } 30 31 var buf [10]*ssa.Value // avoid alloc in common case 32 for _, b := range f.Blocks { 33 for _, instr := range b.Instrs { 34 for _, op := range instr.Operands(buf[:0]) { 35 if fn, ok := (*op).(*ssa.Function); ok { 36 visit(fn) 37 } 38 } 39 } 40 } 41 } 42 for source := range sources { 43 visit(source) 44 } 45 return seen 46 }