gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sync/locking/lockdep_test.go (about) 1 // Copyright 2022 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 //go:build lockdep 16 // +build lockdep 17 18 package locking_test 19 20 import ( 21 "testing" 22 ) 23 24 func TestReverse(t *testing.T) { 25 m := testMutex{} 26 m2 := test2RWMutex{} 27 m.Lock() 28 m2.Lock() 29 m2.Unlock() 30 m.Unlock() 31 32 defer func() { 33 if r := recover(); r != nil { 34 t.Logf("Got expected panic: %s", r) 35 } 36 }() 37 38 m2.Lock() 39 m.Lock() 40 m.Unlock() 41 m2.Unlock() 42 t.Error("The reverse lock order hasn't been detected") 43 } 44 45 func TestIndirect(t *testing.T) { 46 m1 := testMutex{} 47 m2 := test2RWMutex{} 48 m3 := test3Mutex{} 49 50 m1.Lock() 51 m2.Lock() 52 m2.Unlock() 53 m1.Unlock() 54 m2.Lock() 55 m3.Lock() 56 m3.Unlock() 57 m2.Unlock() 58 defer func() { 59 if r := recover(); r != nil { 60 t.Logf("Got expected panic: %s", r) 61 } 62 }() 63 64 m3.Lock() 65 m1.Lock() 66 m1.Unlock() 67 m3.Unlock() 68 t.Error("The reverse lock order hasn't been detected") 69 } 70 71 func TestSame(t *testing.T) { 72 defer func() { 73 if r := recover(); r != nil { 74 t.Logf("Got expected panic: %s", r) 75 } 76 }() 77 78 m := testMutex{} 79 m.Lock() 80 m.Lock() 81 m.Unlock() 82 m.Unlock() 83 t.Error("The same lock has been locked twice, and was not detected.") 84 } 85 86 func TestReverseNested(t *testing.T) { 87 m1 := testMutex{} 88 m2 := testMutex{} 89 m1.Lock() 90 m2.NestedLock(testLockM2) 91 m1.Unlock() 92 m2.NestedUnlock(testLockM2) 93 94 defer func() { 95 if r := recover(); r != nil { 96 t.Logf("Got expected panic: %s", r) 97 } 98 }() 99 100 m2.NestedLock(testLockM2) 101 m1.Lock() 102 m1.NestedUnlock(testLockM2) 103 m2.Unlock() 104 105 t.Error("The reverse lock order hasn't been detected") 106 } 107 108 func TestReverseNestedDeeper(t *testing.T) { 109 m1 := testMutex{} 110 m2 := testMutex{} 111 m3 := testMutex{} 112 m1.Lock() 113 m2.NestedLock(testLockM2) 114 m3.NestedLock(testLockM3) 115 m1.Unlock() 116 m3.NestedUnlock(testLockM3) 117 m2.NestedUnlock(testLockM2) 118 119 m1.Lock() 120 m2.NestedLock(testLockM2) 121 m3.NestedLock(testLockM3) 122 m1.Unlock() 123 m2.NestedUnlock(testLockM2) 124 m3.NestedUnlock(testLockM3) 125 126 defer func() { 127 if r := recover(); r != nil { 128 t.Logf("Got expected panic: %s", r) 129 } 130 }() 131 132 m2.NestedLock(testLockM2) 133 m3.NestedLock(testLockM3) 134 m1.Lock() 135 m1.Unlock() 136 m3.NestedUnlock(testLockM3) 137 m2.NestedUnlock(testLockM2) 138 139 t.Error("The reverse lock order hasn't been detected") 140 } 141 142 func TestUnknownLock(t *testing.T) { 143 m1 := testMutex{} 144 m2 := testMutex{} 145 146 m1.Lock() 147 m2.NestedLock(testLockM2) 148 m2.NestedUnlock(testLockM2) 149 m1.Unlock() 150 151 defer func() { 152 if r := recover(); r != nil { 153 t.Logf("Got expected panic: %s", r) 154 } 155 }() 156 m1.Lock() 157 m2.NestedUnlock(testLockM2) 158 t.Error("An unknown lock has not been detected.") 159 }