-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis_callgraph.go
87 lines (71 loc) · 2.12 KB
/
analysis_callgraph.go
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
package main
import (
"fmt"
"strings"
"golang.org/x/tools/go/callgraph"
"golang.org/x/tools/go/callgraph/cha"
"golang.org/x/tools/go/callgraph/rta"
"golang.org/x/tools/go/callgraph/static"
"golang.org/x/tools/go/callgraph/vta"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
)
func doCallgraph(algo string, tests bool, args []string) (*callgraph.Graph, *ssa.Program, error) {
cfg := &packages.Config{
Mode: packages.LoadAllSyntax, // nolint:staticcheck
Tests: tests,
BuildFlags: strings.Split(buildFlag, " "),
}
initial, err := packages.Load(cfg, args...)
if err != nil {
return nil, nil, err
}
if packages.PrintErrors(initial) > 0 {
return nil, nil, fmt.Errorf("packages contain errors")
}
// Create and build SSA-form program representation.
mode := ssa.InstantiateGenerics // instantiate generics by default for soundness
prog, pkgs := ssautil.AllPackages(initial, mode)
prog.Build()
// -- call graph construction ------------------------------------------
var cg *callgraph.Graph
switch algo {
case "static":
cg = static.CallGraph(prog)
case "cha":
cg = cha.CallGraph(prog)
case "rta":
mains, err := mainPackages(pkgs)
if err != nil {
return nil, nil, err
}
var roots []*ssa.Function
for _, main := range mains {
roots = append(roots, main.Func("init"), main.Func("main"))
}
rtares := rta.Analyze(roots, true)
cg = rtares.CallGraph
// NB: RTA gives us Reachable and RuntimeTypes too.
case "vta":
cg = vta.CallGraph(ssautil.AllFunctions(prog), cha.CallGraph(prog))
default:
return nil, nil, fmt.Errorf("unknown algorithm: %s", algo)
}
cg.DeleteSyntheticNodes()
return cg, prog, nil
}
// mainPackages returns the main packages to analyze.
// Each resulting package is named "main" and has a main function.
func mainPackages(pkgs []*ssa.Package) ([]*ssa.Package, error) {
var mains []*ssa.Package
for _, p := range pkgs {
if p != nil && p.Pkg.Name() == "main" && p.Func("main") != nil {
mains = append(mains, p)
}
}
if len(mains) == 0 {
return nil, fmt.Errorf("no main packages")
}
return mains, nil
}