/
annotations.go
381 lines (309 loc) · 7.69 KB
/
annotations.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package testutil
import (
"strings"
"github.com/cs-au-dk/goat/analysis/cfg"
"github.com/cs-au-dk/goat/analysis/defs"
L "github.com/cs-au-dk/goat/analysis/lattice"
"golang.org/x/tools/go/expect"
"golang.org/x/tools/go/ssa"
)
type Annotation interface {
// Returns related annotations (created from notes on the same line).
Related() annList
String() string
Panicked() bool
Note() *expect.Note
Nodes() map[cfg.Node]struct{}
Manager() NotesManager
}
type AnnotationWithFocus interface {
Annotation
Focused() annList
}
type AnnPanicked struct {
basicAnnotation
}
func (a AnnPanicked) String() string {
fset := a.mgr.loadRes.Prog.Fset
npos := fset.Position(a.note.Pos)
return At(id_PANICKED + " at " + npos.String())
}
type AnnProgress interface {
Annotation
Focused() AnnGoro
HasFocus() bool
}
type AnnBlocks struct {
basicAnnotation
goro string
goroann *AnnGoro
}
func (a AnnBlocks) String() string {
fset := a.mgr.loadRes.Prog.Fset
npos := fset.Position(a.note.Pos)
var gostr string
if a.goro != "" {
gostr = a.Focused().String()
}
return At(gostr + " " + id_BLOCKS + " at " + npos.String())
}
func (a AnnBlocks) HasFocus() bool {
return a.goro != ""
}
func (a AnnBlocks) Focused() AnnGoro {
if a.goroann == nil {
a.goroann = new(AnnGoro)
*a.goroann = a.mgr.FindGoro(a.goro)
}
return *a.goroann
}
func (a AnnBlocks) Nodes() map[cfg.Node]struct{} {
return commNodes(a)
}
type AnnReleases struct {
basicAnnotation
goro string
goroann *AnnGoro
}
func (a AnnReleases) String() string {
fset := a.mgr.loadRes.Prog.Fset
npos := fset.Position(a.note.Pos)
return At(id_MAY_RELEASE + " at " + npos.String())
}
func (a AnnReleases) HasFocus() bool {
return a.goro != ""
}
func (a AnnReleases) Focused() AnnGoro {
if a.goroann == nil {
a.goroann = new(AnnGoro)
*a.goroann = a.mgr.FindGoro(a.goro)
}
return *a.goroann
}
func (a AnnReleases) Nodes() map[cfg.Node]struct{} {
return commNodes(a.basicAnnotation)
}
type AnnChan struct {
basicAnnotation
ch ssa.Value
}
func (a AnnChan) String() string {
fset := a.mgr.loadRes.Prog.Fset
npos := fset.Position(a.note.Pos)
return At("Channel at " + npos.String() +
"\n name: " + idToStr(a.note.Args[0]) +
"\n SSA value: " + a.ch.String())
}
func (a AnnChan) Name() string {
return idToStr(a.note.Args[0])
}
func commTransitiveNodes(a Annotation) map[cfg.Node]struct{} {
nodes := a.Manager().NodesForNote(a.Note())
commsuccs := make(map[cfg.Node]struct{})
for node := range nodes {
for cs := range node.CommTransitive() {
commsuccs[cs] = struct{}{}
}
}
return commsuccs
}
func commNodes(a Annotation) map[cfg.Node]struct{} {
nodes := a.Manager().NodesForNote(a.Note())
commsuccs := make(map[cfg.Node]struct{})
for node := range nodes {
if node.IsCommunicationNode() {
commsuccs[node] = struct{}{}
}
}
return commsuccs
}
func (a AnnChan) Nodes() map[cfg.Node]struct{} {
return commTransitiveNodes(a.basicAnnotation)
}
func (a AnnChan) Chan() ssa.Value {
return a.ch
}
type AnnChanQuery struct {
basicAnnotation
prop string
val L.Element
chnote Annotation
gownerNote Annotation
focusedNote Annotation
gowner string
focused string
ch ssa.Value
}
func (a AnnChanQuery) Value() L.Element {
return a.val
}
func (a AnnChanQuery) Chan() ssa.Value {
return a.ch
}
func (a AnnChanQuery) Gowner() string {
return a.gowner
}
func (a AnnChanQuery) Focused() string {
return a.focused
}
func (a AnnChanQuery) Prop() string {
return a.prop
}
// Returns related channel queries.
func (a1 AnnChanQuery) ChanRelated() []Annotation {
fas := []Annotation{}
for _, a2 := range a1.basicAnnotation.Related() {
switch a2 := a2.(type) {
case AnnChanQuery:
if a1.ch == a2.ch {
fas = append(fas, a2)
}
}
}
return fas
}
// Retrieves the channel annotation with the same channel name.
func (a *AnnChanQuery) ChanNote() AnnChan {
if a.chnote != nil {
return a.chnote.(AnnChan)
}
for _, a2 := range a.mgr.anns {
a2, ok := a2.(AnnChan)
if !ok {
continue
}
if a.ch == a2.ch {
a.chnote = a2
return a2
}
}
// Only reachable if no channel annotation was found.
// Annotation creation should prevent such scenarios from happening.
panic("Unreachable.")
}
// Retrieves the goroutine annotation for the goroutine
// that owns the channel. It should always succeed.
func (a AnnChanQuery) GownerNote() AnnGoro {
return a.mgr.FindGoro(a.gowner)
}
func (a AnnChanQuery) HasOwner() bool {
return a.gowner != ""
}
// Retrieves the goroutine annotation for the focused goroutine
// of that channel query. It should always succeed.
func (a AnnChanQuery) FocusedNote() AnnGoro {
return a.mgr.FindGoro(a.focused)
}
func (a AnnChanQuery) IsFocused() bool {
return a.focused != ""
}
func (a AnnChanQuery) String() string {
fset := a.mgr.loadRes.Prog.Fset
npos := fset.Position(a.note.Pos)
var suffix string
if a.HasOwner() {
suffix += "\n Channel owner: " + a.mgr.FindGoro(a.gowner).String()
}
if a.IsFocused() {
suffix += "\n Focused goroutine: " + a.mgr.FindGoro(a.focused).String()
}
return At("Channel " + a.prop + " query at " + npos.String() +
"\n name: " + idToStr(a.note.Args[0]) +
"\n SSA value: " + a.ch.String() + " at " + fset.Position(a.ch.Pos()).String() +
"\n Abstract value: " + a.val.String() +
suffix)
}
func (a AnnChanQuery) Nodes() map[cfg.Node]struct{} {
nodes := a.basicAnnotation.mgr.NodesForNote(a.note)
commsuccs := make(map[cfg.Node]struct{})
for node := range nodes {
for cs := range node.CommTransitive() {
commsuccs[cs] = struct{}{}
}
}
return commsuccs
}
// Annotation for naming spawning points
type AnnGo struct {
basicAnnotation
g cfg.Node
}
// Goroutine CFG node associated with spawning point.
func (a AnnGo) Go() cfg.Node {
return a.g
}
// Annotation for creating specific goroutines.
// Goroutines consist of a chain of spawning points.
// The order is given left to right from the root goroutine.
type AnnGoro struct {
basicAnnotation
gos []cfg.Node
hardmatch bool
}
func (a AnnGoro) Goro() []cfg.Node {
return a.gos
}
func (a AnnGoro) Name() string {
return idToStr(a.note.Args[0])
}
func (a AnnGoro) String() string {
str := a.Name()
nodestrs := make([]string, 0, len(a.gos))
for _, node := range a.gos {
nodestrs = append(nodestrs, node.String())
}
return str + ": " + strings.Join(nodestrs, " ↝ ")
}
// Checks whether a goroutine structure is represented by
// a goroutine annotation (only takes control locations into account).
// If the goroutine annotation is set to hardmatch, then the full spawning
// point chain must match. Otherwise, only the inner-most chain must.
func (a AnnGoro) Matches(g defs.Goro) bool {
i := len(a.gos) - 1
for ; g != nil && i >= 0; g = g.Parent() {
if g.CtrLoc().Node() != a.gos[i] {
return false
}
i--
}
if a.hardmatch {
return i < 0 && g == nil
}
return i < 0
}
type AnnFocused struct {
basicAnnotation
goro string
goroann *AnnGoro
}
func (a AnnFocused) Matches(g defs.Goro) bool {
return a.Goro().Matches(g)
}
func (a AnnFocused) Goro() AnnGoro {
if a.goroann == nil {
a.goroann = new(AnnGoro)
*a.goroann = a.mgr.FindGoro(a.goro)
}
return *a.goroann
}
func (a AnnFocused) String() string {
fset := a.mgr.loadRes.Prog.Fset
npos := fset.Position(a.note.Pos)
return At("Focus on goroutine " + a.goro + " at " + npos.String())
}
type AnnFalseNegative struct {
basicAnnotation
}
func (a AnnFalseNegative) String() string {
fset := a.mgr.loadRes.Prog.Fset
npos := fset.Position(a.note.Pos)
return At("False negative at " + npos.String())
}
type AnnFalsePositive struct {
basicAnnotation
}
func (a AnnFalsePositive) String() string {
fset := a.mgr.loadRes.Prog.Fset
npos := fset.Position(a.note.Pos)
return At("False positive at " + npos.String())
}