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
|
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package test is a test package.
package test
import (
"math/rand"
"sync"
)
type oneGuarded struct {
mu sync.Mutex
// +checklocks:mu
guardedField int
unguardedField int
}
func testAccessOne() {
var tc oneGuarded
// Valid access
tc.mu.Lock()
tc.guardedField = 1
tc.unguardedField = 1
tc.mu.Unlock()
// Valid access as unguarded field is not protected by mu.
tc.unguardedField = 2
// Invalid access
tc.guardedField = 2 // +checklocksfail
// Invalid read of a guarded field.
x := tc.guardedField // +checklocksfail
_ = x
}
func testFunctionCallsNoParameters() {
// Couple of regular function calls with no parameters.
funcCallWithValidAccess()
funcCallWithInvalidAccess()
}
func funcCallWithValidAccess() {
var tc2 oneGuarded
// Valid tc2 access
tc2.mu.Lock()
tc2.guardedField = 1
tc2.mu.Unlock()
}
func funcCallWithInvalidAccess() {
var tc oneGuarded
var tc2 oneGuarded
// Invalid access, wrong mutex is held.
tc.mu.Lock()
tc2.guardedField = 2 // +checklocksfail
tc.mu.Unlock()
}
func testParameterPassing() {
var tc oneGuarded
// Valid call where a guardedField is passed to a function as a parameter.
tc.mu.Lock()
nestedWithGuardByAddr(&tc.guardedField, &tc.unguardedField)
tc.mu.Unlock()
// Invalid call where a guardedField is passed to a function as a parameter
// without holding locks.
nestedWithGuardByAddr(&tc.guardedField, &tc.unguardedField) // +checklocksfail
// Valid call where a guardedField is passed to a function as a parameter.
tc.mu.Lock()
nestedWithGuardByValue(tc.guardedField, tc.unguardedField)
tc.mu.Unlock()
// Invalid call where a guardedField is passed to a function as a parameter
// without holding locks.
nestedWithGuardByValue(tc.guardedField, tc.unguardedField) // +checklocksfail
}
func nestedWithGuardByAddr(guardedField, unguardedField *int) {
*guardedField = 4
*unguardedField = 5
}
func nestedWithGuardByValue(guardedField, unguardedField int) {
// read the fields to keep SA4009 static analyzer happy.
_ = guardedField
_ = unguardedField
guardedField = 4
unguardedField = 5
}
type twoGuarded struct {
mu sync.Mutex
// +checklocks:mu
guardedField1 int
// +checklocks:mu
guardedField2 int
}
type twoLocks struct {
mu sync.Mutex
secondMu sync.Mutex
// +checklocks:mu
guardedField1 int
// +checklocks:secondMu
guardedField2 int
}
type twoLocksDoubleGuard struct {
mu sync.Mutex
secondMu sync.Mutex
// +checklocks:mu
// +checklocks:secondMu
doubleGuardedField int
}
func testTwoLocksDoubleGuard() {
var tc twoLocksDoubleGuard
// Double guarded field
tc.mu.Lock()
tc.secondMu.Lock()
tc.doubleGuardedField = 1
tc.secondMu.Unlock()
// This should fail as we released the secondMu.
tc.doubleGuardedField = 2 // +checklocksfail
tc.mu.Unlock()
// This should fail as well as now we are not holding any locks.
//
// This line triggers two failures one for each mutex, hence the 2 after
// fail.
tc.doubleGuardedField = 3 // +checklocksfail:2
}
type rwGuarded struct {
rwMu sync.RWMutex
// +checklocks:rwMu
rwGuardedField int
}
func testRWGuarded() {
var tc rwGuarded
// Assignment w/ exclusive lock should pass.
tc.rwMu.Lock()
tc.rwGuardedField = 1
tc.rwMu.Unlock()
// Assignment w/ RWLock should pass as we don't differentiate between
// Lock/RLock.
tc.rwMu.RLock()
tc.rwGuardedField = 2
tc.rwMu.RUnlock()
// Assignment w/o hold Lock() should fail.
tc.rwGuardedField = 3 // +checklocksfail
// Reading w/o holding lock should fail.
x := tc.rwGuardedField + 3 // +checklocksfail
_ = x
}
type nestedFields struct {
mu sync.Mutex
// +checklocks:mu
nestedStruct struct {
nested1 int
nested2 int
}
}
func testNestedStructGuards() {
var tc nestedFields
// Valid access with mu held.
tc.mu.Lock()
tc.nestedStruct.nested1 = 1
tc.nestedStruct.nested2 = 2
tc.mu.Unlock()
// Invalid access to nested1 wihout holding mu.
tc.nestedStruct.nested1 = 1 // +checklocksfail
}
type testCaseMethods struct {
mu sync.Mutex
// +checklocks:mu
guardedField int
}
func (t *testCaseMethods) Method() {
// Valid access
t.mu.Lock()
t.guardedField = 1
t.mu.Unlock()
// invalid access
t.guardedField = 2 // +checklocksfail
}
// +checklocks:t.mu
func (t *testCaseMethods) MethodLocked(a, b, c int) {
t.guardedField = 3
}
// +checklocksignore
func (t *testCaseMethods) IgnoredMethod() {
// Invalid access but should not fail as the function is annotated
// with "// +checklocksignore"
t.guardedField = 2
}
func testMethodCalls() {
var tc2 testCaseMethods
// Valid use, tc2.Method acquires lock.
tc2.Method()
// Valid access tc2.mu is held before calling tc2.MethodLocked.
tc2.mu.Lock()
tc2.MethodLocked(1, 2, 3)
tc2.mu.Unlock()
// Invalid access no locks are being held.
tc2.MethodLocked(4, 5, 6) // +checklocksfail
}
type noMutex struct {
f int
g int
}
func (n noMutex) method() {
n.f = 1
n.f = n.g
}
func testNoMutex() {
var n noMutex
n.method()
}
func testMultiple() {
var tc1, tc2, tc3 testCaseMethods
tc1.mu.Lock()
// Valid access we are holding tc1's lock.
tc1.guardedField = 1
// Invalid access we are not holding tc2 or tc3's lock.
tc2.guardedField = 2 // +checklocksfail
tc3.guardedField = 3 // +checklocksfail
tc1.mu.Unlock()
}
func testConditionalBranchingLocks() {
var tc2 testCaseMethods
x := rand.Intn(10)
if x%2 == 1 {
tc2.mu.Lock()
}
// This is invalid access as tc2.mu is not held if we never entered
// the if block.
tc2.guardedField = 1 // +checklocksfail
var tc3 testCaseMethods
if x%2 == 1 {
tc3.mu.Lock()
} else {
tc3.mu.Lock()
}
// This is valid as tc3.mu is held in if and else blocks.
tc3.guardedField = 1
}
type testMethodWithParams struct {
mu sync.Mutex
// +checklocks:mu
guardedField int
}
type ptrToTestMethodWithParams *testMethodWithParams
// +checklocks:t.mu
// +checklocks:a.mu
func (t *testMethodWithParams) methodLockedWithParams(a *testMethodWithParams, b *testMethodWithParams) {
t.guardedField = a.guardedField
b.guardedField = a.guardedField // +checklocksfail
}
// +checklocks:t.mu
// +checklocks:a.mu
// +checklocks:b.mu
func (t *testMethodWithParams) methodLockedWithPtrType(a *testMethodWithParams, b ptrToTestMethodWithParams) {
t.guardedField = a.guardedField
b.guardedField = a.guardedField
}
// +checklocks:a.mu
func standaloneFunctionWithGuard(a *testMethodWithParams) {
a.guardedField = 1
a.mu.Unlock()
a.guardedField = 1 // +checklocksfail
}
type testMethodWithEmbedded struct {
mu sync.Mutex
// +checklocks:mu
guardedField int
p *testMethodWithParams
}
// +checklocks:t.mu
func (t *testMethodWithEmbedded) DoLocked() {
var a, b testMethodWithParams
t.guardedField = 1
a.mu.Lock()
b.mu.Lock()
t.p.methodLockedWithParams(&a, &b) // +checklocksfail
a.mu.Unlock()
b.mu.Unlock()
}
// UnsupportedLockerExample is a test that verifies that trying to annotate a
// field that is not a sync.Mutex/RWMutex results in a failure.
type UnsupportedLockerExample struct {
mu sync.Locker
// +checklocks:mu
x int // +checklocksfail
}
func abc() {
var mu sync.Mutex
a := UnsupportedLockerExample{mu: &mu}
a.x = 1
}
|