/
analyzer.go
510 lines (433 loc) · 12.7 KB
/
analyzer.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
package protectedby
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"strings"
"unicode"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/astutil"
)
const (
protectedBy = "protected by "
testDirective = "// want `"
)
var testRun bool
var syncLocker = types.NewInterfaceType(
[]*types.Func{
types.NewFunc(token.NoPos, nil, "Lock",
types.NewSignatureType(nil, nil, nil, types.NewTuple(), types.NewTuple(), false)),
types.NewFunc(token.NoPos, nil, "Unlock",
types.NewSignatureType(nil, nil, nil, types.NewTuple(), types.NewTuple(), false)),
},
nil,
).Complete()
type analysisError struct {
msg string
pos token.Pos
}
func (e analysisError) Error() string {
return e.msg
}
type protectedData struct {
field *ast.Field
lock *ast.Field
enclosingStruct *ast.TypeSpec
usages []*usage
}
type usage struct {
file *ast.File
selectorXID *ast.Ident
enclosingFunc *ast.FuncDecl
deferStmt *ast.DeferStmt
}
var Analyzer = &analysis.Analyzer{
Name: "protectedby",
Doc: "Checks that access to shared resources is protected.",
Run: run,
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
func run(pass *analysis.Pass) (interface{}, error) {
protectedMap, errors := parseComments(pass)
if errors != nil {
for _, e := range errors {
pass.Reportf(e.pos, e.Error())
}
if !testRun {
return nil, nil
}
}
errors = addUsages(pass, protectedMap)
if errors != nil {
for _, e := range errors {
pass.Reportf(e.pos, e.Error())
}
if !testRun {
return nil, nil
}
}
errors = checkLocksUsed(pass, protectedMap)
if errors != nil {
for _, e := range errors {
pass.Reportf(e.pos, e.Error())
}
if !testRun {
return nil, nil
}
}
return nil, nil
}
func parseComments(pass *analysis.Pass) (map[string]*protectedData, []*analysisError) {
res := make(map[string]*protectedData)
var errors []*analysisError
for _, f := range pass.Files {
commentMap := ast.NewCommentMap(pass.Fset, f, f.Comments)
for node, commentMapGroups := range commentMap {
// Filter out nodes that are not fields. The linter only works for struct fields protected
// by another field.
field, ok := node.(*ast.Field)
if !ok {
continue
}
// Skip embedded fields and blank identifiers.
fieldName := getFieldName(field)
if fieldName == "" || fieldName == "_" {
continue
}
commentGroup:
for _, cg := range commentMapGroups {
for _, comment := range cg.List {
if !strings.Contains(strings.ToLower(comment.Text), protectedBy) {
continue
}
spec := getEnclosingStruct(f, comment.Pos(), comment.End())
if spec == nil {
continue commentGroup
}
if token.IsExported(fieldName) {
errors = append(errors, &analysisError{
msg: fmt.Sprintf("exported protected field %s.%s", spec.Name, fieldName),
pos: field.Pos(),
})
continue commentGroup
}
lock, err := getLock(pass, spec, comment)
if err != nil {
errors = append(errors, err)
continue commentGroup
}
p := &protectedData{
field: field,
lock: lock,
enclosingStruct: spec,
}
pName := protectedName(spec.Name.Name, fieldName)
res[pName] = p
break
}
}
}
}
return res, errors
}
func implementsLocker(pass *analysis.Pass, f *ast.Field) bool {
realType := pass.TypesInfo.TypeOf(f.Type)
ptrType := types.NewPointer(realType)
return types.Implements(realType, syncLocker) || types.Implements(ptrType, syncLocker)
}
func addUsages(pass *analysis.Pass, protectedMap map[string]*protectedData) []*analysisError {
var errors []*analysisError
for _, file := range pass.Files {
ast.Inspect(file, func(n ast.Node) bool {
switch se := n.(type) {
case *ast.SelectorExpr:
id, ok := se.X.(*ast.Ident)
if !ok {
return false
}
xtyp := deref(pass.TypesInfo.TypeOf(se.X))
// The type of the accessor must be named.
named, ok := xtyp.(*types.Named)
if !ok {
return false
}
// Use typed name to lookup a corresponding protected field.
pName := protectedName(named.Obj().Name(), se.Sel.Name)
p, ok := protectedMap[pName]
if !ok {
return false
}
fn, deferStmt, err := findEnclosingFunction(se.X.Pos(), se.X.End(), file)
if err != nil {
errors = append(errors, err)
return false
}
p.usages = append(p.usages, &usage{
file: file,
selectorXID: id,
enclosingFunc: fn,
deferStmt: deferStmt,
})
return false
}
return true
})
}
return errors
}
func findEnclosingFunction(start, end token.Pos, file *ast.File) (*ast.FuncDecl, *ast.DeferStmt, *analysisError) {
path, _ := astutil.PathEnclosingInterval(file, start, end)
// Find the function declaration that encloses the positions.
var outer *ast.FuncDecl
var deferStmt *ast.DeferStmt
for _, p := range path {
// Deferred access to a protected field is validated differently.
if d, ok := p.(*ast.DeferStmt); ok {
deferStmt = d
}
if p, ok := p.(*ast.FuncDecl); ok {
outer = p
break
}
}
if outer == nil {
return nil, nil, &analysisError{
msg: "no enclosing function",
pos: start,
}
}
return outer, deferStmt, nil
}
func checkLocksUsed(pass *analysis.Pass, m map[string]*protectedData) []*analysisError {
var errors []*analysisError
for _, p := range m {
for _, u := range p.usages {
found := false
var lockExpr *ast.SelectorExpr
ast.Inspect(u.enclosingFunc, func(curr ast.Node) bool {
if curr == nil {
return false
}
// Access to a protected field can be deferred. Skip node if this is a defer statement
// that is not the same where the field is accessed.
if deferStmt, ok := curr.(*ast.DeferStmt); ok {
if deferStmt == u.deferStmt {
return true
} else {
return false
}
}
cexpr, ok := curr.(*ast.CallExpr)
if !ok {
return true
}
fnSelector, ok := cexpr.Fun.(*ast.SelectorExpr)
if !ok {
return true
}
// Return if the function is not "Lock". At this point we already checked that the lock implements
// sync.Locker interface, namely Lock() function, hence it cannot have another function with
// a name Lock and arguments -- overloading is forbidden in go.
if fnSelector.Sel.Name != "Lock" {
return true
}
// Return if the function call is outside the function or after the protected field access.
if curr.Pos() <= u.enclosingFunc.Body.Pos() || curr.Pos() >= u.selectorXID.Pos() {
return false
}
/*
Function expression must be a SelectorExpr because the following is not valid:
s := s1{} // a struct with a protected field and a mutex mu.
copyMu := s.mu // this copies mutex, i.e. copyMu.Lock() will not protect the field. This is reported
// by go vet: "assignment copies lock value to mu: sync.Mutex".
copyMu.Lock()
*/
selExpr, ok := fnSelector.X.(*ast.SelectorExpr)
if !ok {
return true
}
sxid, ok := selExpr.X.(*ast.Ident)
if !ok {
return true
}
if pass.TypesInfo.ObjectOf(sxid) == pass.TypesInfo.ObjectOf(u.selectorXID) {
found = true
lockExpr = fnSelector
return false
}
return true
})
if !found || isUnlockCalled(pass, u, lockExpr) {
errors = append(errors, &analysisError{
msg: fmt.Sprintf("not protected access to shared field %s, use %s.%s.Lock()",
getFieldName(p.field),
u.selectorXID.Name,
getFieldName(p.lock),
),
pos: u.selectorXID.Pos(),
})
}
}
}
return errors
}
func isUnlockCalled(pass *analysis.Pass, u *usage, lockExpr *ast.SelectorExpr) bool {
unlocked := false
ast.Inspect(u.enclosingFunc, func(curr ast.Node) bool {
if curr == nil {
return false
}
cexpr, ok := curr.(*ast.CallExpr)
if !ok {
return true
}
fnSelector, ok := cexpr.Fun.(*ast.SelectorExpr)
if !ok {
return true
}
if fnSelector.Sel.Name != "Unlock" {
return true
}
withinRange := curr.Pos() > lockExpr.Pos() && curr.Pos() < u.selectorXID.Pos()
// Return if the Unlock() is called before Lock() or after access to the protected field.
if !withinRange {
return false
}
/*
function expression must be a SelectorExpr because the following is not valid:
s := s1{} // a struct with a protected field and a mutex mu.
copyMu := s.mu // this copies mutex, i.e. copyMu.Lock() will not protect the field. This is reported
// by go vet: "assignment copies lock value to mu: sync.Mutex".
copyMu.Lock()
*/
selExpr, ok := fnSelector.X.(*ast.SelectorExpr)
if !ok {
return true
}
sxid, ok := selExpr.X.(*ast.Ident)
if !ok {
return true
}
if pass.TypesInfo.ObjectOf(sxid) == pass.TypesInfo.ObjectOf(u.selectorXID) {
// If Unlock() is called from within deferred function
_, deferStmt, _ := findEnclosingFunction(curr.Pos(), curr.End(), u.file)
// it must be the same deferred statement as the deferred statement where current usage happened.
unlocked = deferStmt == u.deferStmt
return false
}
return true
})
return unlocked
}
func getEnclosingStruct(f *ast.File, posStart, posEnd token.Pos) *ast.TypeSpec {
// Need TypeSpec here to get the struct name.
var spec *ast.TypeSpec
path, _ := astutil.PathEnclosingInterval(f, posStart, posEnd)
for _, p := range path {
if typeSpec, ok := p.(*ast.TypeSpec); ok {
if typeSpec.Name == nil {
continue
}
if _, ok := typeSpec.Type.(*ast.StructType); ok {
spec = typeSpec
break
}
}
}
return spec
}
func getLock(pass *analysis.Pass, spec *ast.TypeSpec, c *ast.Comment) (*ast.Field, *analysisError) {
lockName, err := getLockName(c, testRun)
if err != nil {
return nil, err
}
lock := getStructFieldByName(lockName, spec.Type.(*ast.StructType))
if lock == nil {
return nil, &analysisError{
msg: fmt.Sprintf("struct %q does not have lock field %q", spec.Name.Name, lockName),
pos: c.Pos(),
}
}
// Check if the lock field is exported after verifying that it exists. Otherwise may report
// "exported mutex" for not existing field.
if token.IsExported(lockName) {
return nil, &analysisError{
msg: fmt.Sprintf("exported mutex %s.%s", spec.Name.Name, lockName),
pos: lock.Pos(),
}
}
if !implementsLocker(pass, lock) {
return nil, &analysisError{
msg: fmt.Sprintf("lock %s doesn't implement sync.Locker interface", lockName),
pos: lock.Pos(),
}
}
return lock, nil
}
func getStructFieldByName(name string, st *ast.StructType) *ast.Field {
var lockField *ast.Field
for _, field := range st.Fields.List {
for _, n := range field.Names {
if name == n.Name {
lockField = field
break
}
}
}
return lockField
}
// getLockName returns the first word in the comment after "protected by" statement or error if the statement is not
// found or found more than once.
func getLockName(comment *ast.Comment, testRun bool) (string, *analysisError) {
text := comment.Text
// analysistest uses comments of the form "// want ..." as an expected error message. A comment in a test file looks
// like "is protected by not existing mutex.// want `struct "s1" does not have lock field "not"`" i.e. contains
// multiple "protected by"'s. Since the analyser reacts on each "protected by" the code below excludes test
// directives from "// want `" till the end of the comment line.
if testRun {
if idx := strings.Index(comment.Text, testDirective); idx != -1 {
text = text[:idx]
}
}
// Compare "protected by " directive with lowercase comment because the directive can be a separate sentence i.e.
// starts with capital letter.
lowerCaseComment := strings.ToLower(text)
cnt := strings.Count(lowerCaseComment, protectedBy)
if cnt != 1 {
return "", &analysisError{
msg: fmt.Sprintf("found %d %q in comment %q, expected exact one", cnt, protectedBy, text),
pos: comment.Pos(),
}
}
// The index of "protected by " directive is guaranteed to be greater than -1 by checking count above.
idx := strings.Index(lowerCaseComment, protectedBy)
c := text[idx+len(protectedBy):]
fields := strings.FieldsFunc(c, isLetterOrNumber)
if len(fields) == 0 {
return "", &analysisError{
msg: fmt.Sprintf("failed to parse lock name from comment %q", text),
pos: comment.Pos(),
}
}
return fields[0], nil
}
func isLetterOrNumber(c rune) bool {
return !unicode.IsLetter(c) && !unicode.IsNumber(c)
}
func deref(T types.Type) types.Type {
if p, ok := T.Underlying().(*types.Pointer); ok {
return deref(p.Elem())
}
return T
}
func protectedName(structName, fieldName string) string {
return fmt.Sprintf("%s.%s", structName, fieldName)
}
func getFieldName(f *ast.Field) string {
if len(f.Names) != 1 {
return ""
}
return f.Names[0].Name
}