-
Notifications
You must be signed in to change notification settings - Fork 39
/
genkill.go
227 lines (172 loc) · 4.51 KB
/
genkill.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
package doublelock
import (
"fmt"
"github.com/system-pclub/GCatch/GCatch/tools/go/ssa"
"github.com/system-pclub/GCatch/GCatch/util"
)
var mapGen map[ssa.Instruction]*StLockingOp
var mapKill map[ssa.Instruction]*StUnlockingOp
var mapBefore map[ssa.Instruction]map[*StLockingOp]bool
var mapAfter map[ssa.Instruction]map[*StLockingOp]bool
func printMapBefore() {
fmt.Println()
fmt.Println("Print Before Map:")
for ii, m := range mapBefore {
if len(m) == 0 {
continue
}
fmt.Print(ii, " (before): ")
for l, _ := range m {
fmt.Print(l.StrName)
fmt.Print(" ")
}
fmt.Println()
}
}
func printMapGen() {
fmt.Println()
fmt.Println("Print Gen Map:")
for ii, l := range mapGen {
fmt.Print(ii, " (Gen): ", l.StrName)
fmt.Println()
}
}
func CompareTwoMaps(map1 map[*StLockingOp]bool, map2 map[*StLockingOp]bool) bool {
if len(map1) != len(map2) {
return false
}
for s, _ := range map1 {
if _, ok := map2[s]; !ok {
return false
}
}
return true
}
func GetLiveMutex(inputInst ssa.Instruction) map[*StLockingOp]bool {
mapReturn := make(map[*StLockingOp]bool)
for op, _ := range mapBefore[inputInst] {
mapReturn[op] = true
}
return mapReturn
}
func InitGenKillMap(inputFn *ssa.Function) {
mapGen = make(map[ssa.Instruction]*StLockingOp)
mapKill = make(map[ssa.Instruction]*StUnlockingOp)
for _, bb := range inputFn.Blocks {
for _, ii := range bb.Instrs {
if _, ok := mapIIStLockingOp[ii]; ok {
mapGen[ii] = mapIIStLockingOp[ii]
}
if _, ok := mapIIStUnlockingOp[ii]; ok {
mapKill[ii] = mapIIStUnlockingOp[ii]
}
}
}
}
func InitBeforeAfterMap(inputFn *ssa.Function, contextLock map[*StLockingOp]bool) {
for _, bb := range inputFn.Blocks {
for _, ii := range bb.Instrs {
m1 := make(map[*StLockingOp]bool)
mapBefore[ii] = m1
m2 := make(map[*StLockingOp]bool)
mapAfter[ii] = m2
}
}
vecEntryInsts := util.GetEntryInsts(inputFn)
//fmt.Println("InitBeforeAfterMap:", inputFn.Name(), len(contextLock), len(vecEntryInsts))
for _, ii := range vecEntryInsts {
for l, _ := range contextLock {
mapBefore[ii][l] = true
}
}
}
func UnionGenSet(newbefore map[*StLockingOp]bool, pLocking *StLockingOp) *StDoubleLock {
//fmt.Println("inside union gen")
for l, _ := range newbefore {
if l.Parent == pLocking.Parent { // try alias analysis here
bug := &StDoubleLock{
PLock1: l,
PLock2: pLocking,
}
//should we return here?
return bug
}
}
newbefore[pLocking] = true
return nil
}
func KillKillSet(newbefore map[*StLockingOp]bool, pUnlocking *StUnlockingOp) {
for l, _ := range newbefore {
if l.Parent == pUnlocking.Parent {
delete(newbefore, l)
//return
}
}
}
func GenKillAnalysis(inputFn *ssa.Function, contextLock map[*StLockingOp]bool) []*StDoubleLock {
//fmt.Println(inputFn.Name(), len(contextLock))
mapGen = make(map[ssa.Instruction]*StLockingOp)
mapKill = make(map[ssa.Instruction]*StUnlockingOp)
mapBefore = make(map[ssa.Instruction]map[*StLockingOp]bool)
mapAfter = make(map[ssa.Instruction]map[*StLockingOp]bool)
bugs := make([]*StDoubleLock, 0)
InitGenKillMap(inputFn)
if len(mapGen) == 0 && len(mapKill) == 0 && len(contextLock) == 0 {
return bugs
}
InitBeforeAfterMap(inputFn, contextLock)
//printMapBefore()
vecWorkList := make([]ssa.Instruction, 0)
for _, bb := range inputFn.Blocks {
for _, ii := range bb.Instrs {
vecWorkList = append(vecWorkList, ii)
}
}
for len(vecWorkList) > 0 {
ii := vecWorkList[len(vecWorkList)-1]
vecWorkList = vecWorkList[:len(vecWorkList)-1]
prevIIs := util.GetPrevInsts(ii)
newBefore := make(map[*StLockingOp]bool)
if len(prevIIs) > 0 {
for _, prevII := range prevIIs {
for op, _ := range mapAfter[prevII] {
newBefore[op] = true
}
}
mapBefore[ii] = make(map[*StLockingOp]bool)
for op, _ := range newBefore {
mapBefore[ii][op] = true
}
} else {
for op, _ := range mapBefore[ii] {
newBefore[op] = true
}
}
//if inputFn.Name() == "ProtectedInc" {
// fmt.Println(ii, len(newBefore))
//}
if op, ok := mapGen[ii]; ok {
bug := UnionGenSet(newBefore, op)
if bug != nil {
bugs = append(bugs, bug)
}
}
if op, ok := mapKill[ii]; ok {
KillKillSet(newBefore, op)
}
//if op, ok := mapGen[ii]; ok {
// newBefore[op] = true
//}
//if strMutexName, ok := mapKill[ii]; ok {
// delete(newBefore, strMutexName)
//
//}
if !CompareTwoMaps(newBefore, mapAfter[ii]) {
mapAfter[ii] = newBefore
for _, pI := range util.GetSuccInsts(ii) {
vecWorkList = append(vecWorkList, pI)
}
}
}
return bugs
}