| 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 vta computes the call graph of a Go program using the Variable |
| 6 | // Type Analysis (VTA) algorithm originally described in "Practical Virtual |
| 7 | // Method Call Resolution for Java," Vijay Sundaresan, Laurie Hendren, |
| 8 | // Chrislain Razafimahefa, Raja Vallée-Rai, Patrick Lam, Etienne Gagnon, and |
| 9 | // Charles Godin. |
| 10 | // |
| 11 | // Note: this package is in experimental phase and its interface is |
| 12 | // subject to change. |
| 13 | // TODO(zpavlinovic): reiterate on documentation. |
| 14 | // |
| 15 | // The VTA algorithm overapproximates the set of types (and function literals) |
| 16 | // a variable can take during runtime by building a global type propagation |
| 17 | // graph and propagating types (and function literals) through the graph. |
| 18 | // |
| 19 | // A type propagation is a directed, labeled graph. A node can represent |
| 20 | // one of the following: |
| 21 | // - A field of a struct type. |
| 22 | // - A local (SSA) variable of a method/function. |
| 23 | // - All pointers to a non-interface type. |
| 24 | // - The return value of a method. |
| 25 | // - All elements in an array. |
| 26 | // - All elements in a slice. |
| 27 | // - All elements in a map. |
| 28 | // - All elements in a channel. |
| 29 | // - A global variable. |
| 30 | // |
| 31 | // In addition, the implementation used in this package introduces |
| 32 | // a few Go specific kinds of nodes: |
| 33 | // - (De)references of nested pointers to interfaces are modeled |
| 34 | // as a unique nestedPtrInterface node in the type propagation graph. |
| 35 | // - Each function literal is represented as a function node whose |
| 36 | // internal value is the (SSA) representation of the function. This |
| 37 | // is done to precisely infer flow of higher-order functions. |
| 38 | // |
| 39 | // Edges in the graph represent flow of types (and function literals) through |
| 40 | // the program. That is, the model 1) typing constraints that are induced by |
| 41 | // assignment statements or function and method calls and 2) higher-order flow |
| 42 | // of functions in the program. |
| 43 | // |
| 44 | // The labeling function maps each node to a set of types and functions that |
| 45 | // can intuitively reach the program construct the node represents. Initially, |
| 46 | // every node is assigned a type corresponding to the program construct it |
| 47 | // represents. Function nodes are also assigned the function they represent. |
| 48 | // The labeling function then propagates types and function through the graph. |
| 49 | // |
| 50 | // The result of VTA is a type propagation graph in which each node is labeled |
| 51 | // with a conservative overapproximation of the set of types (and functions) |
| 52 | // it may have. This information is then used to construct the call graph. |
| 53 | // For each unresolved call site, vta uses the set of types and functions |
| 54 | // reaching the node representing the call site to create a set of callees. |
| 55 | package vtascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/ |
| 56 | |
| 57 | // TODO(zpavlinovic): update VTA for how it handles generic function bodies and instantiation wrappers. |
| 58 | |
| 59 | import ( |
| 60 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 61 | |
| 62 | "golang.org/x/tools/go/callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/" |
| 63 | "golang.org/x/tools/go/ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/" |
| 64 | ) |
| 65 | |
| 66 | // CallGraph uses the VTA algorithm to compute call graph for all functions |
| 67 | // f:true in funcs. VTA refines the results of initial call graph and uses it |
| 68 | // to establish interprocedural type flow. If initial is nil, VTA uses a more |
| 69 | // efficient approach to construct a CHA call graph. |
| 70 | // |
| 71 | // The resulting graph does not have a root node. |
| 72 | // |
| 73 | // CallGraph does not make any assumptions on initial types global variables |
| 74 | // and function/method inputs can have. CallGraph is then sound, modulo use of |
| 75 | // reflection and unsafe, if the initial call graph is sound. |
| 76 | func CallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/CallGraph().(funcs map[*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#]bool, initial *callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/.Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#) *callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/.Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph# { |
| 77 | callees := makeCalleesFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/makeCalleesFunc().(funcs, initial) |
| 78 | vtaG, canon := typePropGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/typePropGraph().(funcs, callees) |
| 79 | types := propagatescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/propagate().(vtaG, canon) |
| 80 | |
| 81 | c := &constructorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#{typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#types.: types, calleesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#callees.: callees, cachescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#cache.: make(methodCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/methodCache#)} |
| 82 | return c.constructscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#construct().(funcs) |
| 83 | } |
| 84 | |
| 85 | // constructor type linearly traverses the input program |
| 86 | // and constructs a callgraph based on the results of the |
| 87 | // VTA type propagation phase. |
| 88 | type constructorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor# struct { |
| 89 | typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#types. propTypeMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/propTypeMap# |
| 90 | cachescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#cache. methodCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/methodCache# |
| 91 | calleesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#callees. calleesFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/calleesFunc# |
| 92 | } |
| 93 | |
| 94 | func (c *constructorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#) constructscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#construct().(funcs map[*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#]bool) *callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/.Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph# { |
| 95 | cg := &callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/.Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#{Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#Nodes.: make(map[*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#]*callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#)} |
| 96 | for f, in := range funcs { |
| 97 | if in { |
| 98 | c.constrctscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#constrct().(cg, f) |
| 99 | } |
| 100 | } |
| 101 | return cg |
| 102 | } |
| 103 | |
| 104 | func (c *constructorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#) constrctscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#constrct().(g *callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/.Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#, f *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#) { |
| 105 | caller := g.CreateNodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#CreateNode().(f) |
| 106 | for _, call := range callsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/calls().(f) { |
| 107 | for _, c := range c.resolvesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#resolves().(call) { |
| 108 | callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/.AddEdgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/AddEdge().(caller, call, g.CreateNodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#CreateNode().(c)) |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // resolves computes the set of functions to which VTA resolves `c`. The resolved |
| 114 | // functions are intersected with functions to which `c.initial` resolves `c`. |
| 115 | func (c *constructorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#) resolvesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#resolves().(call ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.CallInstructionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#) []*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function# { |
| 116 | cc := call.Commonscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#Common.() |
| 117 | if cc.StaticCalleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#StaticCallee().() != nil { |
| 118 | return []*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#{cc.StaticCalleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#StaticCallee().()} |
| 119 | } |
| 120 | |
| 121 | // Skip builtins as they are not *ssa.Function. |
| 122 | if _, ok := cc.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Value..(*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Builtinscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Builtin#); ok { |
| 123 | return nil |
| 124 | } |
| 125 | |
| 126 | // Cover the case of dynamic higher-order and interface calls. |
| 127 | var res []*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function# |
| 128 | resolved := resolvescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/resolve().(call, c.typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#types., c.cachescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#cache.) |
| 129 | for f := range siteCalleesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/siteCallees().(call, c.calleesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constructor#callees.) { |
| 130 | if _, ok := resolved[f]; ok { |
| 131 | res = append(res, f) |
| 132 | } |
| 133 | } |
| 134 | return res |
| 135 | } |
| 136 | |
| 137 | // resolve returns a set of functions `c` resolves to based on the |
| 138 | // type propagation results in `types`. |
| 139 | func resolvescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/resolve().(c ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.CallInstructionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#, types propTypeMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/propTypeMap#, cache methodCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/methodCache#) map[*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#]emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/empty# { |
| 140 | fns := make(map[*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#]emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/empty#) |
| 141 | n := localscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/local#{valscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/local#val.: c.Commonscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#Common.().Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Value.} |
| 142 | for p := range types.propTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/propTypeMap#propTypes().(n) { |
| 143 | for _, f := range propFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/propFunc().(p, c, cache) { |
| 144 | fns[f] = emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/empty#{} |
| 145 | } |
| 146 | } |
| 147 | return fns |
| 148 | } |
| 149 | |
| 150 | // propFunc returns the functions modeled with the propagation type `p` |
| 151 | // assigned to call site `c`. If no such function exists, nil is returned. |
| 152 | func propFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/propFunc().(p propTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/propType#, c ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.CallInstructionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#, cache methodCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/methodCache#) []*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function# { |
| 153 | if p.fscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/propType#f. != nil { |
| 154 | return []*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#{p.fscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/propType#f.} |
| 155 | } |
| 156 | |
| 157 | if c.Commonscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#Common.().Methodscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Method. == nil { |
| 158 | return nil |
| 159 | } |
| 160 | |
| 161 | return cache.methodsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/methodCache#methods().(p.typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/propType#typ., c.Commonscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#Common.().Methodscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Method..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Name().(), c.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Instruction#Parent.().Progscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Prog.) |
| 162 | } |
| 163 | |
| 164 | // methodCache serves as a type -> method name -> methods |
| 165 | // cache when computing methods of a type using the |
| 166 | // ssa.Program.MethodSets and ssa.Program.MethodValue |
| 167 | // APIs. The cache is used to speed up querying of |
| 168 | // methods of a type as the mentioned APIs are expensive. |
| 169 | type methodCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/methodCache# map[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#]map[string][]*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function# |
| 170 | |
| 171 | // methods returns methods of a type `t` named `name`. First consults |
| 172 | // `mc` and otherwise queries `prog` for the method. If no such method |
| 173 | // exists, nil is returned. |
| 174 | func (mc methodCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/methodCache#) methodsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/methodCache#methods().(t typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#, name string, prog *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Programscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#) []*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function# { |
| 175 | if ms, ok := mc[t]; ok { |
| 176 | return ms[name] |
| 177 | } |
| 178 | |
| 179 | ms := make(map[string][]*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#) |
| 180 | mset := prog.MethodSetsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#MethodSets..MethodSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#MethodSet().(t) |
| 181 | for i, n := 0, mset.Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet#Len().(); i < n; i++ { |
| 182 | // f can be nil when t is an interface or some |
| 183 | // other type without any runtime methods. |
| 184 | if f := prog.MethodValuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#MethodValue().(mset.Atscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet#At().(i)); f != nil { |
| 185 | ms[f.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Name().()] = append(ms[f.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Name().()], f) |
| 186 | } |
| 187 | } |
| 188 | mc[t] = ms |
| 189 | return ms[name] |
| 190 | } |