-
Notifications
You must be signed in to change notification settings - Fork 0
/
callgraph.go
146 lines (120 loc) · 3.52 KB
/
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
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
package pointer
import (
"go/types"
"golang.org/x/tools/go/callgraph"
"golang.org/x/tools/go/ssa"
)
// CallGraph returns a call graph for the analysed program. Dynamically
// dispatched calls are resolved using the results of the pointer analysis.
func (r *Result) CallGraph() *callgraph.Graph {
cg, ctx := r.callGraph, r.ctx
if !r.initializedCallGraph {
r.initializedCallGraph = true
var prog *ssa.Program
if len(r.Reachable) == 0 {
goto DONE
}
for fun := range r.Reachable {
prog = fun.Prog
n := cg.CreateNode(fun)
for _, block := range fun.Blocks {
for _, insn := range block.Instrs {
call, ok := insn.(ssa.CallInstruction)
if !ok {
continue
}
common := call.Common()
v := common.Value
var callees []*ssa.Function
if common.IsInvoke() {
if pt, ok := find(ctx.eval(v)).x.(tPointsTo); ok {
find(pt.x).x.(tInterface).iterateCallees(
prog,
common.Method,
func(fun *ssa.Function, _ *term) {
callees = append(callees, fun)
})
}
} else if sc := common.StaticCallee(); sc != nil {
var funs map[*ssa.Function][]*term
switch {
case sc == ctx.godebug_setUpdate,
sc == ctx.sync_runtime_registerPoolCleanup:
funs = find(ctx.eval(common.Args[0])).x.(tClosure).funs
case sc == ctx.time_startTimer:
argT := sc.Signature.Params().At(0).Type().(*types.Pointer)
runtimeTimerT := argT.Elem().Underlying().(*types.Struct)
fI := FieldIndex(runtimeTimerT, "f")
arg := find(ctx.eval(common.Args[0]))
strukt := find(arg.x.(tPointsTo).x).x.(tStruct)
closure := find(strukt.fields[fI]).x.(tClosure)
funs = closure.funs
}
for fun := range funs {
callgraph.AddEdge(cg.CreateNode(sc), nil, cg.CreateNode(fun))
}
callees = []*ssa.Function{sc}
} else if _, isBuiltin := v.(*ssa.Builtin); !isBuiltin {
closure, _ := find(ctx.eval(v)).x.(tClosure)
for fun := range closure.funs {
callees = append(callees, fun)
}
}
for _, callee := range callees {
callgraph.AddEdge(n, call, cg.CreateNode(callee))
}
}
}
}
// Add call edges for runtime.SetFinalizer calls
if runtime := prog.ImportedPackage("runtime"); runtime != nil {
fun := runtime.Func("SetFinalizer")
var callers []ssa.CallInstruction
for _, ed := range cg.CreateNode(fun).In {
callers = append(callers, ed.Site)
}
if len(callers) == 0 {
goto DONE
}
obj := find(find(ctx.eval(fun.Params[0])).x.(tPointsTo).x).x.(tInterface)
if obj.contents.Len() == 0 {
goto DONE
}
funs := find(find(ctx.eval(fun.Params[1])).x.(tPointsTo).x).x.(tInterface)
if funs.contents.Len() == 0 {
goto DONE
}
funs.contents.Iterate(func(fType types.Type, v *term) {
fSig, ok := fType.Underlying().(*types.Signature)
if !ok || fSig.Recv() != nil || fSig.Params().Len() != 1 {
return
}
clos, ok := find(v).x.(tClosure)
if !ok {
return
}
pType := fSig.Params().At(0).Type()
anyWorks := false
obj.contents.Iterate(func(oType types.Type, _ *term) {
anyWorks = anyWorks || types.AssignableTo(oType, pType)
})
if anyWorks {
for cfun := range clos.funs {
for _, caller := range callers {
callgraph.AddEdge(
cg.CreateNode(caller.Parent()),
caller,
cg.CreateNode(cfun))
callgraph.AddEdge(
cg.CreateNode(fun),
caller,
cg.CreateNode(cfun))
}
}
}
})
}
DONE:
}
return cg
}