/
callgraph.go
177 lines (155 loc) · 5.12 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
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
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//This file may have been modified by Bytedance Ltd. and/or its affiliates (“Bytedance's Modifications”).
// All Bytedance's Modifications are Copyright (2022) Bytedance Ltd. and/or its affiliates.
package graph
import (
"fmt"
"go/types"
"os"
"path/filepath"
"strings"
"github.com/bytedance/nxt_unit/manager/duplicatepackagemanager"
"github.com/bytedance/nxt_unit/atgconstant"
"github.com/bytedance/nxt_unit/codebuilder/setup/parsermodel"
"golang.org/x/tools/go/callgraph"
"golang.org/x/tools/go/ssa"
)
func BuildCallGraph(function *parsermodel.ProjectFunction, functionName string) *callgraph.Graph {
cg := &callgraph.Graph{Nodes: make(map[*ssa.Function]*callgraph.Node)}
cg.Root = cg.CreateNode(function.Function)
createGraph(cg.Root, cg, function)
// buildNode(cg.Root, cg, function)
return cg
}
func createGraph(parentNode *callgraph.Node, cg *callgraph.Graph, projectFunction *parsermodel.ProjectFunction) *callgraph.Node {
fNode := parentNode
ScanFuncBlocks(parentNode.Func.Blocks, projectFunction, fNode, cg)
return fNode
}
func getFunction(call *ssa.CallCommon, function *parsermodel.ProjectFunction) *ssa.Function {
tiface := call.Value.Type().Underlying().(*types.Interface)
methods := function.Program.LookupMethods(tiface, call.Method)
var f *ssa.Function
for _, method := range methods {
if method.Name() == call.Method.Name() {
f = method
}
}
return f
}
func funcInStd(f *ssa.Function) bool {
if f == nil || f.Pkg == nil || f.Pkg.Pkg == nil || f.Pkg.Pkg.Path() == "" {
return true
}
funcPath := f.Pkg.Pkg.Path()
if strings.HasPrefix(funcPath, "golang.org/x") {
return true
}
if duplicatepackagemanager.IsPathShouldBeRemoved(funcPath) {
return true
}
for name := range atgconstant.IgnoredPath {
if strings.HasPrefix(funcPath, name) {
return true
}
}
// TODO: No idea why we need this
path := joinPath(atgconstant.GOROOT, "src", funcPath)
fi, err := os.Stat(path)
return err == nil && fi.IsDir()
}
// hasCycleAndAdded checks whether the function is in the map
func hasCycleAndAdded(function *ssa.Function, projectFunction *parsermodel.ProjectFunction) bool {
if function == nil || function.Pkg == nil {
return true
}
funcFullName := fmt.Sprintf("%v/%v", function.Pkg.Pkg.Path(), function.Name())
if _, ok := projectFunction.CalleeFunctionsForTargetFunction[funcFullName]; ok {
return true
}
projectFunction.CalleeFunctionsForTargetFunction[funcFullName] = function
return false
}
func joinPath(elem ...string) string {
return filepath.Join(elem...)
}
func ScanFuncBlocks(bacicBlocks []*ssa.BasicBlock, projectFunction *parsermodel.ProjectFunction, parentNode *callgraph.Node, cg *callgraph.Graph) {
for _, b := range bacicBlocks {
for _, instr := range b.Instrs {
var gNode *callgraph.Node
site, ok := instr.(ssa.CallInstruction)
if !ok {
continue
}
callCommon := site.Common()
if callCommon == nil {
continue
}
var downStreamFunc *ssa.Function
if callCommon.IsInvoke() {
invokeF := getFunction(callCommon, projectFunction)
if invokeF != nil {
downStreamFunc = invokeF
}
} else {
calleeF := callCommon.StaticCallee()
if calleeF != nil {
downStreamFunc = calleeF
}
}
if downStreamFunc == nil {
continue
}
_, isDefer := site.(*ssa.Defer)
_, isGoRoutine := site.(*ssa.Go)
if strings.Contains(downStreamFunc.Name(), "$") && (isDefer || isGoRoutine) {
// make callgraph: add downstream method to callGraph eg (*ssa.Go)(*ssa.Defer)
// eg: go routine(*ssa.Defer) eg:/atg/template/atg.go,funcName:ComplexDeferFunction
// eg: go routine(*ssa.Go) eg:/atg/template/atg.go,funcName:ComplexGoRoutineFunction
// case use to get defer function downstream method
// maybe missing corner cases ,waiting to supplement
switch commonValue := callCommon.Value.(type) {
case *ssa.Function:
if commonValue != nil && commonValue.Blocks != nil && len(commonValue.Blocks) > 0 {
ScanFuncBlocks(commonValue.Blocks, projectFunction, parentNode, cg)
}
case *ssa.MakeClosure:
closureFunc, ok := commonValue.Fn.(*ssa.Function)
if ok {
if closureFunc != nil && closureFunc.Blocks != nil && len(closureFunc.Blocks) > 0 {
ScanFuncBlocks(closureFunc.Blocks, projectFunction, parentNode, cg)
}
}
}
} else {
gNode = createCalleeNode(downStreamFunc, cg, projectFunction)
}
if gNode == nil {
continue
}
if atgconstant.GraphLevel == 1 {
callgraph.AddEdge(parentNode, site, gNode)
continue
} else {
callgraph.AddEdge(gNode, site, createGraph(gNode, cg, projectFunction))
}
}
}
}
func createCalleeNode(downStreamFunc *ssa.Function, cg *callgraph.Graph, projectFunction *parsermodel.ProjectFunction) *callgraph.Node {
if downStreamFunc == nil {
return nil
}
if funcInStd(downStreamFunc) {
return nil
}
if !strings.Contains(downStreamFunc.Name(), "$") {
if hasCycleAndAdded(downStreamFunc, projectFunction) {
return nil
}
return cg.CreateNode(downStreamFunc)
}
return nil
}