gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/tools/checklocks/test/branches.go (about) 1 // Copyright 2020 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package test 16 17 import ( 18 "math/rand" 19 ) 20 21 func testInconsistentReturn(tc *oneGuardStruct) { // +checklocksfail 22 if x := rand.Intn(10); x%2 == 1 { 23 tc.mu.Lock() 24 } 25 } 26 27 func testConsistentBranching(tc *oneGuardStruct) { 28 x := rand.Intn(10) 29 if x%2 == 1 { 30 tc.mu.Lock() 31 } else { 32 tc.mu.Lock() 33 } 34 tc.guardedField = 1 35 if x%2 == 1 { 36 tc.mu.Unlock() 37 } else { 38 tc.mu.Unlock() 39 } 40 } 41 42 func testInconsistentBranching(tc *oneGuardStruct) { // +checklocksfail:2 43 // We traverse the control flow graph in all consistent ways. We cannot 44 // determine however, that the first if block and second if block will 45 // evaluate to the same condition. Therefore, there are two consistent 46 // paths through this code, and two inconsistent paths. Either way, the 47 // guardedField should be also marked as an invalid access. 48 x := rand.Intn(10) 49 if x%2 == 1 { 50 tc.mu.Lock() 51 } 52 tc.guardedField = 1 // +checklocksfail 53 if x%2 == 1 { 54 tc.mu.Unlock() // +checklocksforce 55 } 56 } 57 58 func testUnboundedLocks(tc []*oneGuardStruct) { 59 for _, l := range tc { 60 l.mu.Lock() 61 } 62 // This test should have the above *not fail*, though the exact 63 // lock state cannot be tracked through the below. Therefore, we 64 // expect the next loop to actually fail, and we force the unlock 65 // loop to succeed in exactly the same way. 66 for _, l := range tc { 67 l.guardedField = 1 // +checklocksfail 68 } 69 for _, l := range tc { 70 l.mu.Unlock() // +checklocksforce 71 } 72 }