-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer_globalvar.go
243 lines (230 loc) · 6.52 KB
/
analyzer_globalvar.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
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
package main
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/callgraph"
"golang.org/x/tools/go/ssa"
)
func (analyzer *VarAnalyzer) FindVar(pass *analysis.Pass) {
for _, file := range pass.Files {
for _, decl := range file.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || genDecl.Tok != token.VAR {
continue
}
var mutexValueSpecs []*ast.ValueSpec
for _, spec := range genDecl.Specs {
if valueSpec, ok := spec.(*ast.ValueSpec); ok {
if !isMutexType2(valueSpec) {
continue
}
ident := valueSpec.Names[0]
obj := pass.TypesInfo.Defs[ident]
if obj == nil {
continue
}
v, _ := obj.(*types.Var)
isGlobal := !v.IsField() && !v.Embedded() && v.Parent() == pass.Pkg.Scope() // 全局变量
if isGlobal {
mutexValueSpecs = append(mutexValueSpecs, valueSpec)
}
}
}
if len(mutexValueSpecs) == 0 {
continue
}
for _, mutexValueSpec := range mutexValueSpecs {
pos := pass.Fset.Position(mutexValueSpec.Pos())
mutexVar := analyzer.getGlobalVarByPos(analyzer.prog, pos)
comment := ""
if mutexValueSpec.Comment != nil {
comment = strings.ReplaceAll(mutexValueSpec.Comment.Text(), " ", "")
comment = strings.ReplaceAll(comment, "\n", "")
}
if comment == "" {
fmt.Printf("[mutex check] %v:%v mutex 变量没有注释,指明它要锁的变量\n", pos.Filename, pos.Line)
continue
}
if nolint(comment) {
continue
}
varNames := strings.Split(comment, ",")
for _, name := range varNames {
valueSpec := analyzer.getGlobalVarByName(pass, file, name)
if valueSpec == nil {
pos := pass.Fset.Position(mutexValueSpec.Pos())
fmt.Printf("[mutex check] %v:%v mutex 变量注释中的变量 %v ,未声明\n", pos.Filename, pos.Line, name)
break
} else {
pos := pass.Fset.Position(valueSpec.Pos())
v := analyzer.getGlobalVarByPos(analyzer.prog, pos)
analyzer.vars[v] = mutexVar
}
}
}
}
}
}
func (analyzer *VarAnalyzer) FindCaller(edge *callgraph.Edge, seen map[*callgraph.Node]bool) error {
caller := edge.Caller
if seen[caller] {
return nil
}
if caller.Func == nil {
return nil
}
seen[caller] = true
if caller.Func.Name() == "init" {
return nil
}
usesVar := func(instr ssa.Instruction, v *types.Var) bool {
for _, op := range instr.Operands(nil) {
if varRef, ok := (*op).(*ssa.Global); ok && varRef.Object() == v {
return true
}
}
return false
}
for _, block := range caller.Func.Blocks {
for _, instr := range block.Instrs {
for k := range analyzer.vars {
if usesVar(instr, k) {
pos := analyzer.prog.Fset.Position(instr.Pos())
comment := getComment(pos)
if nolint(comment) {
continue
}
if _, ok := analyzer.callers[k]; !ok {
analyzer.callers[k] = make(map[*callgraph.Node][]token.Position)
}
analyzer.callers[k][caller] = []token.Position{}
}
}
}
}
return nil
}
func (analyzer *VarAnalyzer) CheckVarLock(prog *ssa.Program, caller *callgraph.Node, mymutex, myvar *types.Var) (poss []token.Position) {
var mInstrs []ssa.Instruction
var vInstrs []ssa.Instruction
for _, block := range caller.Func.Blocks {
mInstrs = append(mInstrs, analyzer.findInstrByGlobalVar(block, mymutex)...)
vInstrs = append(vInstrs, analyzer.findInstrByGlobalVar(block, myvar)...)
}
for _, vInstr := range vInstrs {
vPos := prog.Fset.Position(vInstr.Pos())
comment := getComment(vPos)
if nolint(comment) {
continue
}
if !checkMutexLock(prog, mInstrs, vPos) {
poss = append(poss, caller.Func.Prog.Fset.Position(vInstr.Pos()))
}
}
return
}
func (analyzer *VarAnalyzer) HaveVar(prog *ssa.Program, caller *callgraph.Node, m *types.Var) bool {
var find bool
for _, block := range caller.Func.Blocks {
mInstr := analyzer.findInstrByGlobalVar(block, m)
if len(mInstr) > 0 {
find = true
break
}
}
return find
}
func (analyzer *VarAnalyzer) CheckCallLock(prog *ssa.Program, caller *callgraph.Node, mymutex *types.Var, callee *callgraph.Node) bool {
var mInstrs []ssa.Instruction
for _, block := range caller.Func.Blocks {
mInstrs = append(mInstrs, analyzer.findInstrByGlobalVar(block, mymutex)...)
}
for _, vPos := range getCalleePostion(prog, caller, callee) {
comment := getComment(vPos)
if nolint(comment) {
continue
}
if !checkMutexLock(prog, mInstrs, vPos) {
return false
}
}
return true
}
func (analyzer *VarAnalyzer) CheckVarReturn(prog *ssa.Program, caller *callgraph.Node, myvar *types.Var) (poss []token.Position) {
for _, block := range caller.Func.Blocks {
for _, instr := range block.Instrs {
if retInstr, ok := instr.(*ssa.Return); ok {
for _, r := range retInstr.Results {
if hasVar(r, myvar) {
vPos := analyzer.prog.Fset.Position(instr.Pos())
comment := getComment(vPos)
if nolint(comment) {
continue
}
poss = append(poss, vPos)
}
}
}
}
}
return
}
func (analyzer *VarAnalyzer) getGlobalVarByPos(prog *ssa.Program, pos token.Position) *types.Var {
for _, pkg := range prog.AllPackages() {
for _, member := range pkg.Members {
if global, ok := member.(*ssa.Global); ok {
p := prog.Fset.Position(global.Pos())
if p == pos {
return global.Object().(*types.Var)
}
}
}
}
return nil
}
func (analyzer *VarAnalyzer) findInstrByGlobalVar(block *ssa.BasicBlock, v *types.Var) (instrs []ssa.Instruction) {
for _, instr := range block.Instrs {
for _, op := range instr.Operands(nil) {
if varRef, ok := (*op).(*ssa.Global); ok && varRef.Object() == v {
instrs = append(instrs, instr)
}
}
}
return
}
func (analyzer *VarAnalyzer) getGlobalVarByName(pass *analysis.Pass, file *ast.File, name string) *ast.ValueSpec {
for _, decl := range file.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || genDecl.Tok != token.VAR {
continue
}
for _, spec := range genDecl.Specs {
if valueSpec, ok := spec.(*ast.ValueSpec); ok {
ident := valueSpec.Names[0]
obj := pass.TypesInfo.Defs[ident]
if obj == nil {
continue
}
v, _ := obj.(*types.Var)
isGlobal := !v.IsField() && !v.Embedded() && v.Parent() == pass.Pkg.Scope() // 全局变量
if isGlobal && ident.Name == name {
return valueSpec
}
}
}
}
return nil
}
type VarAnalyzer struct {
*BaseAnalyzer
}
func NewVarAnalyzer(path string, cg *callgraph.Graph, prog *ssa.Program) *VarAnalyzer {
analyzer := &VarAnalyzer{}
analyzer.BaseAnalyzer = NewBaseAnalyzer(path, cg, prog)
analyzer.Derive = analyzer
return analyzer
}