1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package lib // import "go.opentelemetry.io/contrib/instrgen/lib"
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"strings"
"golang.org/x/tools/go/packages"
)
// FuncDescriptor stores an information about
// id, type and if function requires custom instrumentation.
type FuncDescriptor struct {
Id string
DeclType string
CustomInjection bool
}
// Function TypeHash. Each function is itentified by its
// id and type.
func (fd FuncDescriptor) TypeHash() string {
return fd.Id + fd.DeclType
}
// LoadMode. Tells about needed information during analysis.
const LoadMode packages.LoadMode = packages.NeedName |
packages.NeedTypes |
packages.NeedSyntax |
packages.NeedTypesInfo |
packages.NeedFiles
func getPkgs(projectPath string, packagePattern string, fset *token.FileSet) ([]*packages.Package, error) {
cfg := &packages.Config{Fset: fset, Mode: LoadMode, Dir: projectPath}
pkgs, err := packages.Load(cfg, packagePattern)
var packageSet []*packages.Package
if err != nil {
return nil, err
}
for _, pkg := range pkgs {
fmt.Println("\t", pkg)
packageSet = append(packageSet, pkg)
}
return packageSet, nil
}
// FindRootFunctions looks for all root functions eg. entry points.
// Currently an entry point is a function that contains call of function
// passed as functionLabel paramaterer.
func FindRootFunctions(projectPath string, packagePattern string, functionLabel string) []FuncDescriptor {
fset := token.NewFileSet()
pkgs, _ := getPkgs(projectPath, packagePattern, fset)
var currentFun FuncDescriptor
var rootFunctions []FuncDescriptor
for _, pkg := range pkgs {
for _, node := range pkg.Syntax {
ast.Inspect(node, func(n ast.Node) bool {
switch xNode := n.(type) {
case *ast.CallExpr:
selector, ok := xNode.Fun.(*ast.SelectorExpr)
if ok {
if selector.Sel.Name == functionLabel {
rootFunctions = append(rootFunctions, currentFun)
}
}
case *ast.FuncDecl:
if pkg.TypesInfo.Defs[xNode.Name] != nil {
funId := pkg.TypesInfo.Defs[xNode.Name].Pkg().Path() + "." + pkg.TypesInfo.Defs[xNode.Name].Name()
currentFun = FuncDescriptor{funId, pkg.TypesInfo.Defs[xNode.Name].Type().String(), false}
fmt.Println("\t\t\tFuncDecl:", funId, pkg.TypesInfo.Defs[xNode.Name].Type().String())
}
}
return true
})
}
}
return rootFunctions
}
// GetMostInnerAstIdent takes most inner identifier used for
// function call. For a.b.foo(), `b` will be the most inner identifier.
func GetMostInnerAstIdent(inSel *ast.SelectorExpr) *ast.Ident {
var l []*ast.Ident
var e ast.Expr
e = inSel
for e != nil {
if _, ok := e.(*ast.Ident); ok {
l = append(l, e.(*ast.Ident))
break
} else if _, ok := e.(*ast.SelectorExpr); ok {
l = append(l, e.(*ast.SelectorExpr).Sel)
e = e.(*ast.SelectorExpr).X
} else if _, ok := e.(*ast.CallExpr); ok {
e = e.(*ast.CallExpr).Fun
} else if _, ok := e.(*ast.IndexExpr); ok {
e = e.(*ast.IndexExpr).X
} else if _, ok := e.(*ast.UnaryExpr); ok {
e = e.(*ast.UnaryExpr).X
} else if _, ok := e.(*ast.ParenExpr); ok {
e = e.(*ast.ParenExpr).X
} else if _, ok := e.(*ast.SliceExpr); ok {
e = e.(*ast.SliceExpr).X
} else if _, ok := e.(*ast.IndexListExpr); ok {
e = e.(*ast.IndexListExpr).X
} else if _, ok := e.(*ast.StarExpr); ok {
e = e.(*ast.StarExpr).X
} else if _, ok := e.(*ast.TypeAssertExpr); ok {
e = e.(*ast.TypeAssertExpr).X
} else if _, ok := e.(*ast.CompositeLit); ok {
// TODO dummy implementation
if len(e.(*ast.CompositeLit).Elts) == 0 {
e = e.(*ast.CompositeLit).Type
} else {
e = e.(*ast.CompositeLit).Elts[0]
}
} else if _, ok := e.(*ast.KeyValueExpr); ok {
e = e.(*ast.KeyValueExpr).Value
} else {
// TODO this is uncaught expression
panic("uncaught expression")
}
}
if len(l) < 2 {
panic("selector list should have at least 2 elems")
}
// caller or receiver is always
// at position 1, function is at 0
return l[1]
}
// GetPkgPathFromRecvInterface builds package path taking
// receiver interface into account.
func GetPkgPathFromRecvInterface(pkg *packages.Package,
pkgs []*packages.Package, funDeclNode *ast.FuncDecl, interfaces map[string]bool,
) string {
var pkgPath string
for _, v := range funDeclNode.Recv.List {
for _, dependentpkg := range pkgs {
for _, defs := range dependentpkg.TypesInfo.Defs {
if defs == nil {
continue
}
if _, ok := defs.Type().Underlying().(*types.Interface); !ok {
continue
}
if len(v.Names) == 0 || pkg.TypesInfo.Defs[v.Names[0]] == nil {
continue
}
funType := pkg.TypesInfo.Defs[v.Names[0]].Type()
if types.Implements(funType, defs.Type().Underlying().(*types.Interface)) {
interfaceExists := interfaces[defs.Type().String()]
if interfaceExists {
pkgPath = defs.Type().String()
}
break
}
}
}
}
return pkgPath
}
// GetPkgPathFromFunctionRecv build package path taking function receiver parameters.
func GetPkgPathFromFunctionRecv(pkg *packages.Package,
pkgs []*packages.Package, funDeclNode *ast.FuncDecl, interfaces map[string]bool,
) string {
pkgPath := GetPkgPathFromRecvInterface(pkg, pkgs, funDeclNode, interfaces)
if len(pkgPath) != 0 {
return pkgPath
}
for _, v := range funDeclNode.Recv.List {
if len(v.Names) == 0 {
continue
}
funType := pkg.TypesInfo.Defs[v.Names[0]].Type()
pkgPath = funType.String()
// We don't care if that's pointer, remove it from
// type id
if _, ok := funType.(*types.Pointer); ok {
pkgPath = strings.TrimPrefix(pkgPath, "*")
}
// We don't care if called via index, remove it from
// type id
if _, ok := funType.(*types.Slice); ok {
pkgPath = strings.TrimPrefix(pkgPath, "[]")
}
}
return pkgPath
}
// GetSelectorPkgPath builds packages path according to selector expr.
func GetSelectorPkgPath(sel *ast.SelectorExpr, pkg *packages.Package, pkgPath string) string {
caller := GetMostInnerAstIdent(sel)
if caller != nil && pkg.TypesInfo.Uses[caller] != nil {
if !strings.Contains(pkg.TypesInfo.Uses[caller].Type().String(), "invalid") {
pkgPath = pkg.TypesInfo.Uses[caller].Type().String()
// We don't care if that's pointer, remove it from
// type id
if _, ok := pkg.TypesInfo.Uses[caller].Type().(*types.Pointer); ok {
pkgPath = strings.TrimPrefix(pkgPath, "*")
}
// We don't care if called via index, remove it from
// type id
if _, ok := pkg.TypesInfo.Uses[caller].Type().(*types.Slice); ok {
pkgPath = strings.TrimPrefix(pkgPath, "[]")
}
}
}
return pkgPath
}
// GetPkgNameFromUsesTable gets package name from uses table.
func GetPkgNameFromUsesTable(pkg *packages.Package, ident *ast.Ident) string {
var pkgPath string
if pkg.TypesInfo.Uses[ident].Pkg() != nil {
pkgPath = pkg.TypesInfo.Uses[ident].Pkg().Path()
}
return pkgPath
}
// GetPkgNameFromDefsTable gets package name from uses table.
func GetPkgNameFromDefsTable(pkg *packages.Package, ident *ast.Ident) string {
var pkgPath string
if pkg.TypesInfo.Defs[ident] == nil {
return pkgPath
}
if pkg.TypesInfo.Defs[ident].Pkg() != nil {
pkgPath = pkg.TypesInfo.Defs[ident].Pkg().Path()
}
return pkgPath
}
// GetPkgPathForFunction builds package path, delegates work to
// other helper functions defined above.
func GetPkgPathForFunction(pkg *packages.Package,
pkgs []*packages.Package, funDecl *ast.FuncDecl, interfaces map[string]bool,
) string {
if funDecl.Recv != nil {
return GetPkgPathFromFunctionRecv(pkg, pkgs, funDecl, interfaces)
}
return GetPkgNameFromDefsTable(pkg, funDecl.Name)
}
// BuildCallGraph builds an information about flow graph
// in the following form child->parent.
func BuildCallGraph(
projectPath string,
packagePattern string,
funcDecls map[FuncDescriptor]bool,
interfaces map[string]bool,
) map[FuncDescriptor][]FuncDescriptor {
fset := token.NewFileSet()
pkgs, _ := getPkgs(projectPath, packagePattern, fset)
fmt.Println("BuildCallGraph")
currentFun := FuncDescriptor{"nil", "", false}
backwardCallGraph := make(map[FuncDescriptor][]FuncDescriptor)
for _, pkg := range pkgs {
fmt.Println("\t", pkg)
for _, node := range pkg.Syntax {
fmt.Println("\t\t", fset.File(node.Pos()).Name())
ast.Inspect(node, func(n ast.Node) bool {
switch xNode := n.(type) {
case *ast.CallExpr:
if id, ok := xNode.Fun.(*ast.Ident); ok {
pkgPath := GetPkgNameFromUsesTable(pkg, id)
funId := pkgPath + "." + pkg.TypesInfo.Uses[id].Name()
fmt.Println("\t\t\tFuncCall:", funId, pkg.TypesInfo.Uses[id].Type().String(),
" @called : ",
fset.File(node.Pos()).Name())
fun := FuncDescriptor{funId, pkg.TypesInfo.Uses[id].Type().String(), false}
if !Contains(backwardCallGraph[fun], currentFun) {
if funcDecls[fun] {
backwardCallGraph[fun] = append(backwardCallGraph[fun], currentFun)
}
}
}
if sel, ok := xNode.Fun.(*ast.SelectorExpr); ok {
if pkg.TypesInfo.Uses[sel.Sel] != nil {
pkgPath := GetPkgNameFromUsesTable(pkg, sel.Sel)
if sel.X != nil {
pkgPath = GetSelectorPkgPath(sel, pkg, pkgPath)
}
funId := pkgPath + "." + pkg.TypesInfo.Uses[sel.Sel].Name()
fmt.Println("\t\t\tFuncCall via selector:", funId, pkg.TypesInfo.Uses[sel.Sel].Type().String(),
" @called : ",
fset.File(node.Pos()).Name())
fun := FuncDescriptor{funId, pkg.TypesInfo.Uses[sel.Sel].Type().String(), false}
if !Contains(backwardCallGraph[fun], currentFun) {
if funcDecls[fun] {
backwardCallGraph[fun] = append(backwardCallGraph[fun], currentFun)
}
}
}
}
case *ast.FuncDecl:
if pkg.TypesInfo.Defs[xNode.Name] != nil {
pkgPath := GetPkgPathForFunction(pkg, pkgs, xNode, interfaces)
funId := pkgPath + "." + pkg.TypesInfo.Defs[xNode.Name].Name()
funcDecls[FuncDescriptor{funId, pkg.TypesInfo.Defs[xNode.Name].Type().String(), false}] = true
currentFun = FuncDescriptor{funId, pkg.TypesInfo.Defs[xNode.Name].Type().String(), false}
fmt.Println("\t\t\tFuncDecl:", funId, pkg.TypesInfo.Defs[xNode.Name].Type().String())
}
}
return true
})
}
}
return backwardCallGraph
}
// FindFuncDecls looks for all function declarations.
func FindFuncDecls(projectPath string, packagePattern string, interfaces map[string]bool) map[FuncDescriptor]bool {
fset := token.NewFileSet()
pkgs, _ := getPkgs(projectPath, packagePattern, fset)
fmt.Println("FindFuncDecls")
funcDecls := make(map[FuncDescriptor]bool)
for _, pkg := range pkgs {
fmt.Println("\t", pkg)
for _, node := range pkg.Syntax {
fmt.Println("\t\t", fset.File(node.Pos()).Name())
ast.Inspect(node, func(n ast.Node) bool {
if funDeclNode, ok := n.(*ast.FuncDecl); ok {
pkgPath := GetPkgPathForFunction(pkg, pkgs, funDeclNode, interfaces)
if pkg.TypesInfo.Defs[funDeclNode.Name] != nil {
funId := pkgPath + "." + pkg.TypesInfo.Defs[funDeclNode.Name].Name()
fmt.Println("\t\t\tFuncDecl:", funId, pkg.TypesInfo.Defs[funDeclNode.Name].Type().String())
funcDecls[FuncDescriptor{funId, pkg.TypesInfo.Defs[funDeclNode.Name].Type().String(), false}] = true
}
}
return true
})
}
}
return funcDecls
}
// FindInterfaces looks for all interfaces.
func FindInterfaces(projectPath string, packagePattern string) map[string]bool {
fset := token.NewFileSet()
pkgs, _ := getPkgs(projectPath, packagePattern, fset)
fmt.Println("FindInterfaces")
interaceTable := make(map[string]bool)
for _, pkg := range pkgs {
fmt.Println("\t", pkg)
for _, node := range pkg.Syntax {
fmt.Println("\t\t", fset.File(node.Pos()).Name())
ast.Inspect(node, func(n ast.Node) bool {
if typeSpecNode, ok := n.(*ast.TypeSpec); ok {
if _, ok := typeSpecNode.Type.(*ast.InterfaceType); ok {
fmt.Println("\t\t\tInterface:", pkg.TypesInfo.Defs[typeSpecNode.Name].Type().String())
interaceTable[pkg.TypesInfo.Defs[typeSpecNode.Name].Type().String()] = true
}
}
return true
})
}
}
return interaceTable
}
// InferRootFunctionsFromGraph tries to infer entry points from passed call graph.
func InferRootFunctionsFromGraph(callgraph map[FuncDescriptor][]FuncDescriptor) []FuncDescriptor {
var allFunctions map[FuncDescriptor]bool
var rootFunctions []FuncDescriptor
allFunctions = make(map[FuncDescriptor]bool)
for k, v := range callgraph {
allFunctions[k] = true
for _, childFun := range v {
allFunctions[childFun] = true
}
}
for k := range allFunctions {
_, exists := callgraph[k]
if !exists {
rootFunctions = append(rootFunctions, k)
}
}
return rootFunctions
}
|