diff options
Diffstat (limited to 'tools/checklocks/test')
-rw-r--r-- | tools/checklocks/test/BUILD | 2 | ||||
-rw-r--r-- | tools/checklocks/test/aliases.go | 26 | ||||
-rw-r--r-- | tools/checklocks/test/branches.go | 16 | ||||
-rw-r--r-- | tools/checklocks/test/closures.go | 18 | ||||
-rw-r--r-- | tools/checklocks/test/incompat.go | 9 | ||||
-rw-r--r-- | tools/checklocks/test/locker.go | 33 |
6 files changed, 95 insertions, 9 deletions
diff --git a/tools/checklocks/test/BUILD b/tools/checklocks/test/BUILD index f2ea6c7c6..4b90731f5 100644 --- a/tools/checklocks/test/BUILD +++ b/tools/checklocks/test/BUILD @@ -5,6 +5,7 @@ package(licenses = ["notice"]) go_library( name = "test", srcs = [ + "aliases.go", "alignment.go", "anon.go", "atomics.go", @@ -13,6 +14,7 @@ go_library( "closures.go", "defer.go", "incompat.go", + "locker.go", "methods.go", "parameters.go", "return.go", diff --git a/tools/checklocks/test/aliases.go b/tools/checklocks/test/aliases.go new file mode 100644 index 000000000..e28027fe5 --- /dev/null +++ b/tools/checklocks/test/aliases.go @@ -0,0 +1,26 @@ +// Copyright 2021 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 + +// +checklocks:tc.mu +// +checklocksalias:tc2.mu=tc.mu +func testAliasValid(tc *oneGuardStruct, tc2 *oneGuardStruct) { + tc2.guardedField = 1 +} + +// +checklocks:tc.mu +func testAliasInvalid(tc *oneGuardStruct, tc2 *oneGuardStruct) { + tc2.guardedField = 1 // +checklocksfail +} diff --git a/tools/checklocks/test/branches.go b/tools/checklocks/test/branches.go index 81fec29e5..247885a49 100644 --- a/tools/checklocks/test/branches.go +++ b/tools/checklocks/test/branches.go @@ -54,3 +54,19 @@ func testInconsistentBranching(tc *oneGuardStruct) { // +checklocksfail:2 tc.mu.Unlock() // +checklocksforce } } + +func testUnboundedLocks(tc []*oneGuardStruct) { + for _, l := range tc { + l.mu.Lock() + } + // This test should have the above *not fail*, though the exact + // lock state cannot be tracked through the below. Therefore, we + // expect the next loop to actually fail, and we force the unlock + // loop to succeed in exactly the same way. + for _, l := range tc { + l.guardedField = 1 // +checklocksfail + } + for _, l := range tc { + l.mu.Unlock() // +checklocksforce + } +} diff --git a/tools/checklocks/test/closures.go b/tools/checklocks/test/closures.go index 7da87540a..316d12ce1 100644 --- a/tools/checklocks/test/closures.go +++ b/tools/checklocks/test/closures.go @@ -53,6 +53,15 @@ func testClosureInline(tc *oneGuardStruct) { tc.mu.Unlock() } +// +checklocksignore +func testClosureIgnore(tc *oneGuardStruct) { + // Inherit the checklocksignore. + x := func() { + tc.guardedField = 1 + } + x() +} + func testAnonymousInvalid(tc *oneGuardStruct) { // Invalid, as per testClosureInvalid above. callAnonymous(func(tc *oneGuardStruct) { @@ -89,6 +98,15 @@ func testAnonymousInline(tc *oneGuardStruct) { tc.mu.Unlock() } +// +checklocksignore +func testAnonymousIgnore(tc *oneGuardStruct) { + // Inherit the checklocksignore. + x := func(tc *oneGuardStruct) { + tc.guardedField = 1 + } + x(tc) +} + //go:noinline func callClosure(fn func()) { fn() diff --git a/tools/checklocks/test/incompat.go b/tools/checklocks/test/incompat.go index b39bc66c1..f55fa532d 100644 --- a/tools/checklocks/test/incompat.go +++ b/tools/checklocks/test/incompat.go @@ -18,15 +18,6 @@ import ( "sync" ) -// unsupportedLockerStruct verifies that trying to annotate a field that is not a -// sync.Mutex or sync.RWMutex results in a failure. -type unsupportedLockerStruct struct { - mu sync.Locker - - // +checklocks:mu - x int // +checklocksfail -} - // badFieldsStruct verifies that refering invalid fields fails. type badFieldsStruct struct { // +checklocks:mu diff --git a/tools/checklocks/test/locker.go b/tools/checklocks/test/locker.go new file mode 100644 index 000000000..b0e7d1143 --- /dev/null +++ b/tools/checklocks/test/locker.go @@ -0,0 +1,33 @@ +// Copyright 2021 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 + +import "sync" + +type lockerStruct struct { + mu sync.Locker + // +checklocks:mu + guardedField int +} + +func testLockerValid(tc *lockerStruct) { + tc.mu.Lock() + tc.guardedField = 1 + tc.mu.Unlock() +} + +func testLockerInvalid(tc *lockerStruct) { + tc.guardedField = 1 // +checklocksfail +} |