forked from system-pclub/GCatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.go
250 lines (205 loc) · 5.19 KB
/
lock.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
244
245
246
247
248
249
250
package doublelock
import (
"fmt"
"github.com/system-pclub/GCatch/GCatch/config"
"github.com/system-pclub/GCatch/GCatch/instinfo"
"github.com/system-pclub/GCatch/GCatch/tools/go/ssa"
"github.com/system-pclub/GCatch/GCatch/util"
"strings"
)
const Unknown = "Unknown"
const Edited = "Edited"
const M_Lock = "M_Lock"
const M_Unlock = "M_Unlock"
const RW_Lock = "RW_Lock"
const RW_RLock = "RW_RLock"
const RW_Unlock = "RW_Unlock"
const RW_RUnlock = "RW_RUnlock"
type StLockingOp struct {
StrName string
I ssa.Instruction
IsRWLock bool
IsDefer bool
Parent *StMutex
StrFileName string
NumLine int
}
type StDoubleLock struct {
PLock1 *StLockingOp
PLock2 *StLockingOp
}
type StUnlockingOp struct {
StrName string
I ssa.Instruction
IsRWLock bool
IsDefer bool
Parent *StMutex
}
type StMutex struct {
StrName string
StrType string
MapLockingOps map[ssa.Instruction]*StLockingOp
MapUnlockingOps map[ssa.Instruction]*StUnlockingOp
Pkg string // Don't use *ssa.Package here! It's not reliable
StrStatus string
}
func printLockingOps(m map[*StLockingOp]bool) {
for l, _ := range m {
fmt.Print(l.StrName)
fmt.Print(" ")
}
fmt.Println()
}
func getLockingOpInfo(inputInst ssa.Instruction) (strName string, strMutexType string, strOpType string, isDefer bool, isLockingOp bool) {
strName = Unknown
strMutexType = Unknown
strOpType = Unknown
isDefer = false
isLockingOp = true
var pCall *ssa.CallCommon
switch pType := inputInst.(type) {
case *ssa.Call:
pCall = pType.Common()
case *ssa.Defer:
pCall = pType.Common()
isDefer = true
default:
isLockingOp = false
return
}
switch instinfo.GetCallName(pCall) {
case "(*sync.Mutex).Lock":
strMutexType = "Mutex"
strOpType = "Lock"
case "(*sync.Mutex).Unlock":
strMutexType = "Mutex"
strOpType = "Unlock"
case "(*sync.RWMutex).Lock":
strMutexType = "RWMutex"
strOpType = "Lock"
case "(*sync.RWMutex).Unlock":
strMutexType = "RWMutex"
strOpType = "Unlock"
case "(*sync.RWMutex).RLock":
strMutexType = "RWMutex"
strOpType = "RLock"
case "(*sync.RWMutex).RUnlock":
strMutexType = "RWMutex"
strOpType = "RUnlock"
default:
var strCallName string = ""
if pCall.IsInvoke() {
strCallName = pCall.Method.Name()
} else {
if callee, ok := pCall.Value.(*ssa.Function); ok {
strCallName = callee.Name()
}
}
if strCallName != "" {
strCallName = strings.ToLower(strCallName)
switch {
case strCallName == "lock":
strOpType = "Lock"
case strCallName == "unlock":
strOpType = "Unlock"
case strCallName == "rLock":
strOpType = "RLock"
case strCallName == "runlock":
strOpType = "RUnlock"
default:
isLockingOp = false
return
}
} else {
isLockingOp = false
return
}
}
strName = instinfo.GetMutexName(inputInst)
if strName == "" {
strName = Unknown
}
return
}
func handleInst(inputInst ssa.Instruction) (isLockingOp bool) {
isLockingOp = false
if inputInst.Parent().Pkg == nil {
return
}
strName, strMutexType, strOpType, isDefer, isLockingOp := getLockingOpInfo(inputInst)
if !isLockingOp {
return
}
if strName == Unknown || strOpType == Unknown {
return
}
var pMutex *StMutex
strBaseType := ""
if pcall, ok := inputInst.(*ssa.Call); ok {
if len(pcall.Common().Args) == 0 {
strBaseType = pcall.Common().Signature().Recv().Type().String() //util.GetBaseType(pcall.Common().Signature().Recv())
} else {
strBaseType = util.GetBaseType(pcall.Common().Args[0]).String()
}
}
if pm, ok := MapMutex[inputInst.Parent().Pkg.Pkg.Path()+": "+strName+" ("+strBaseType+")"]; ok {
pMutex = pm
} else {
pMutex = &StMutex{
StrName: strName,
StrType: strMutexType,
MapLockingOps: map[ssa.Instruction]*StLockingOp{},
MapUnlockingOps: map[ssa.Instruction]*StUnlockingOp{},
Pkg: inputInst.Parent().Pkg.Pkg.Path(),
StrStatus: Edited,
}
MapMutex[inputInst.Parent().Pkg.Pkg.Path()+": "+strName+" ("+strBaseType+")"] = pMutex
}
isRWLocking := false
if strOpType == "RLock" || strOpType == "RUnlock" {
isRWLocking = true
}
if strOpType == "Lock" || strOpType == "RLock" {
loc := config.Prog.Fset.Position(inputInst.Pos())
//output.PrintIISrc(inputInst)
//if pcall, ok := inputInst.(*ssa.Call); ok {
// fmt.Println(pcall.Common().Args[0])
// fmt.Println(util.GetBaseType(pcall.Common().Args[0]))
//}
//fmt.Println()
//fmt.Println()
//fmt.Println(util.GetBaseType())
newLocking := &StLockingOp{
StrName: strName,
I: inputInst,
IsRWLock: isRWLocking,
IsDefer: isDefer,
Parent: pMutex,
StrFileName: loc.Filename,
NumLine: loc.Line,
}
pMutex.MapLockingOps[inputInst] = newLocking
} else if strOpType == "Unlock" || strOpType == "RUnlock" {
newUnlocking := &StUnlockingOp{
StrName: strName,
I: inputInst,
IsRWLock: isRWLocking,
IsDefer: isDefer,
Parent: pMutex,
}
pMutex.MapUnlockingOps[inputInst] = newUnlocking
}
return
}
func handleFN(fn *ssa.Function) bool {
hasLockingOp := false
for _, bb := range fn.Blocks {
for _, ii := range bb.Instrs {
isLockingOp := handleInst(ii)
if isLockingOp {
hasLockingOp = true
}
}
}
return hasLockingOp
}