| 1 |
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-beta -Wno-thread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=0 %s |
| 2 |
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-beta -Wno-thread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=1 %s |
| 3 |
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 -Wthread-safety -Wthread-safety-beta -Wno-thread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=0 %s |
| 4 |
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 -Wthread-safety -Wthread-safety-beta -Wno-thread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=1 %s |
| 5 |
|
| 6 |
// FIXME: should also run %clang_cc1 -fsyntax-only -verify -Wthread-safety -std=c++11 -Wc++98-compat %s |
| 7 |
// FIXME: should also run %clang_cc1 -fsyntax-only -verify -Wthread-safety %s |
| 8 |
|
| 9 |
#include "thread-safety-annotations.h" |
| 10 |
|
| 11 |
class LOCKABLE Mutex { |
| 12 |
public: |
| 13 |
void Lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 14 |
void ReaderLock() SHARED_LOCK_FUNCTION(); |
| 15 |
void Unlock() UNLOCK_FUNCTION(); |
| 16 |
void ExclusiveUnlock() EXCLUSIVE_UNLOCK_FUNCTION(); |
| 17 |
void ReaderUnlock() SHARED_UNLOCK_FUNCTION(); |
| 18 |
bool TryLock() EXCLUSIVE_TRYLOCK_FUNCTION(true); |
| 19 |
bool ReaderTryLock() SHARED_TRYLOCK_FUNCTION(true); |
| 20 |
void LockWhen(const int &cond) EXCLUSIVE_LOCK_FUNCTION(); |
| 21 |
|
| 22 |
void PromoteShared() SHARED_UNLOCK_FUNCTION() EXCLUSIVE_LOCK_FUNCTION(); |
| 23 |
void DemoteExclusive() EXCLUSIVE_UNLOCK_FUNCTION() SHARED_LOCK_FUNCTION(); |
| 24 |
|
| 25 |
// for negative capabilities |
| 26 |
const Mutex& operator!() const { return *this; } |
| 27 |
|
| 28 |
void AssertHeld() ASSERT_EXCLUSIVE_LOCK(); |
| 29 |
void AssertReaderHeld() ASSERT_SHARED_LOCK(); |
| 30 |
}; |
| 31 |
|
| 32 |
class SCOPED_LOCKABLE MutexLock { |
| 33 |
public: |
| 34 |
MutexLock(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu); |
| 35 |
MutexLock(Mutex *mu, bool adopt) EXCLUSIVE_LOCKS_REQUIRED(mu); |
| 36 |
~MutexLock() UNLOCK_FUNCTION(); |
| 37 |
}; |
| 38 |
|
| 39 |
class SCOPED_LOCKABLE ReaderMutexLock { |
| 40 |
public: |
| 41 |
ReaderMutexLock(Mutex *mu) SHARED_LOCK_FUNCTION(mu); |
| 42 |
ReaderMutexLock(Mutex *mu, bool adopt) SHARED_LOCKS_REQUIRED(mu); |
| 43 |
~ReaderMutexLock() UNLOCK_FUNCTION(); |
| 44 |
}; |
| 45 |
|
| 46 |
class SCOPED_LOCKABLE ReleasableMutexLock { |
| 47 |
public: |
| 48 |
ReleasableMutexLock(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu); |
| 49 |
~ReleasableMutexLock() UNLOCK_FUNCTION(); |
| 50 |
|
| 51 |
void Release() UNLOCK_FUNCTION(); |
| 52 |
}; |
| 53 |
|
| 54 |
class SCOPED_LOCKABLE DoubleMutexLock { |
| 55 |
public: |
| 56 |
DoubleMutexLock(Mutex *mu1, Mutex *mu2) EXCLUSIVE_LOCK_FUNCTION(mu1, mu2); |
| 57 |
~DoubleMutexLock() UNLOCK_FUNCTION(); |
| 58 |
}; |
| 59 |
|
| 60 |
// The universal lock, written "*", allows checking to be selectively turned |
| 61 |
// off for a particular piece of code. |
| 62 |
void beginNoWarnOnReads() SHARED_LOCK_FUNCTION("*"); |
| 63 |
void endNoWarnOnReads() UNLOCK_FUNCTION("*"); |
| 64 |
void beginNoWarnOnWrites() EXCLUSIVE_LOCK_FUNCTION("*"); |
| 65 |
void endNoWarnOnWrites() UNLOCK_FUNCTION("*"); |
| 66 |
|
| 67 |
|
| 68 |
// For testing handling of smart pointers. |
| 69 |
template<class T> |
| 70 |
class SmartPtr { |
| 71 |
public: |
| 72 |
SmartPtr(T* p) : ptr_(p) { } |
| 73 |
SmartPtr(const SmartPtr<T>& p) : ptr_(p.ptr_) { } |
| 74 |
~SmartPtr(); |
| 75 |
|
| 76 |
T* get() const { return ptr_; } |
| 77 |
T* operator->() const { return ptr_; } |
| 78 |
T& operator*() const { return *ptr_; } |
| 79 |
T& operator[](int i) const { return ptr_[i]; } |
| 80 |
|
| 81 |
private: |
| 82 |
T* ptr_; |
| 83 |
}; |
| 84 |
|
| 85 |
template<typename T, typename U> |
| 86 |
U& operator->*(const SmartPtr<T>& ptr, U T::*p) { return ptr->*p; } |
| 87 |
|
| 88 |
|
| 89 |
// For testing destructor calls and cleanup. |
| 90 |
class MyString { |
| 91 |
public: |
| 92 |
MyString(const char* s); |
| 93 |
~MyString(); |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
// For testing operator overloading |
| 98 |
template <class K, class T> |
| 99 |
class MyMap { |
| 100 |
public: |
| 101 |
T& operator[](const K& k); |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
// For testing handling of containers. |
| 106 |
template <class T> |
| 107 |
class MyContainer { |
| 108 |
public: |
| 109 |
MyContainer(); |
| 110 |
|
| 111 |
typedef T* iterator; |
| 112 |
typedef const T* const_iterator; |
| 113 |
|
| 114 |
T* begin(); |
| 115 |
T* end(); |
| 116 |
|
| 117 |
const T* cbegin(); |
| 118 |
const T* cend(); |
| 119 |
|
| 120 |
T& operator[](int i); |
| 121 |
const T& operator[](int i) const; |
| 122 |
|
| 123 |
private: |
| 124 |
T* ptr_; |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
Mutex sls_mu; |
| 130 |
|
| 131 |
Mutex sls_mu2 __attribute__((acquired_after(sls_mu))); |
| 132 |
int sls_guard_var __attribute__((guarded_var)) = 0; |
| 133 |
int sls_guardby_var __attribute__((guarded_by(sls_mu))) = 0; |
| 134 |
|
| 135 |
bool getBool(); |
| 136 |
|
| 137 |
class MutexWrapper { |
| 138 |
public: |
| 139 |
Mutex mu; |
| 140 |
int x __attribute__((guarded_by(mu))); |
| 141 |
void MyLock() EXCLUSIVE_LOCK_FUNCTION(mu); |
| 142 |
}; |
| 143 |
|
| 144 |
struct TestingMoreComplexAttributes { |
| 145 |
Mutex lock; |
| 146 |
struct { Mutex lock; } strct; |
| 147 |
union { |
| 148 |
bool a __attribute__((guarded_by(lock))); |
| 149 |
bool b __attribute__((guarded_by(strct.lock))); |
| 150 |
bool *ptr_a __attribute__((pt_guarded_by(lock))); |
| 151 |
bool *ptr_b __attribute__((pt_guarded_by(strct.lock))); |
| 152 |
Mutex lock1 __attribute__((acquired_before(lock))) __attribute__((acquired_before(strct.lock))); |
| 153 |
Mutex lock2 __attribute__((acquired_after(lock))) __attribute__((acquired_after(strct.lock))); |
| 154 |
}; |
| 155 |
} more_complex_atttributes; |
| 156 |
|
| 157 |
void more_complex_attributes() { |
| 158 |
more_complex_atttributes.a = true; // expected-warning{{writing variable 'a' requires holding mutex 'lock' exclusively}} |
| 159 |
more_complex_atttributes.b = true; // expected-warning{{writing variable 'b' requires holding mutex 'strct.lock' exclusively}} |
| 160 |
*more_complex_atttributes.ptr_a = true; // expected-warning{{writing the value pointed to by 'ptr_a' requires holding mutex 'lock' exclusively}} |
| 161 |
*more_complex_atttributes.ptr_b = true; // expected-warning{{writing the value pointed to by 'ptr_b' requires holding mutex 'strct.lock' exclusively}} |
| 162 |
|
| 163 |
more_complex_atttributes.lock.Lock(); |
| 164 |
more_complex_atttributes.lock1.Lock(); // expected-warning{{mutex 'lock1' must be acquired before 'lock'}} |
| 165 |
more_complex_atttributes.lock1.Unlock(); |
| 166 |
more_complex_atttributes.lock.Unlock(); |
| 167 |
|
| 168 |
more_complex_atttributes.lock2.Lock(); |
| 169 |
more_complex_atttributes.lock.Lock(); // expected-warning{{mutex 'lock' must be acquired before 'lock2'}} |
| 170 |
more_complex_atttributes.lock.Unlock(); |
| 171 |
more_complex_atttributes.lock2.Unlock(); |
| 172 |
} |
| 173 |
|
| 174 |
MutexWrapper sls_mw; |
| 175 |
|
| 176 |
void sls_fun_0() { |
| 177 |
sls_mw.mu.Lock(); |
| 178 |
sls_mw.x = 5; |
| 179 |
sls_mw.mu.Unlock(); |
| 180 |
} |
| 181 |
|
| 182 |
void sls_fun_2() { |
| 183 |
sls_mu.Lock(); |
| 184 |
int x = sls_guard_var; |
| 185 |
sls_mu.Unlock(); |
| 186 |
} |
| 187 |
|
| 188 |
void sls_fun_3() { |
| 189 |
sls_mu.Lock(); |
| 190 |
sls_guard_var = 2; |
| 191 |
sls_mu.Unlock(); |
| 192 |
} |
| 193 |
|
| 194 |
void sls_fun_4() { |
| 195 |
sls_mu2.Lock(); |
| 196 |
sls_guard_var = 2; |
| 197 |
sls_mu2.Unlock(); |
| 198 |
} |
| 199 |
|
| 200 |
void sls_fun_5() { |
| 201 |
sls_mu.Lock(); |
| 202 |
int x = sls_guardby_var; |
| 203 |
sls_mu.Unlock(); |
| 204 |
} |
| 205 |
|
| 206 |
void sls_fun_6() { |
| 207 |
sls_mu.Lock(); |
| 208 |
sls_guardby_var = 2; |
| 209 |
sls_mu.Unlock(); |
| 210 |
} |
| 211 |
|
| 212 |
void sls_fun_7() { |
| 213 |
sls_mu.Lock(); |
| 214 |
sls_mu2.Lock(); |
| 215 |
sls_mu2.Unlock(); |
| 216 |
sls_mu.Unlock(); |
| 217 |
} |
| 218 |
|
| 219 |
void sls_fun_8() { |
| 220 |
sls_mu.Lock(); |
| 221 |
if (getBool()) |
| 222 |
sls_mu.Unlock(); |
| 223 |
else |
| 224 |
sls_mu.Unlock(); |
| 225 |
} |
| 226 |
|
| 227 |
void sls_fun_9() { |
| 228 |
if (getBool()) |
| 229 |
sls_mu.Lock(); |
| 230 |
else |
| 231 |
sls_mu.Lock(); |
| 232 |
sls_mu.Unlock(); |
| 233 |
} |
| 234 |
|
| 235 |
void sls_fun_good_6() { |
| 236 |
if (getBool()) { |
| 237 |
sls_mu.Lock(); |
| 238 |
} else { |
| 239 |
if (getBool()) { |
| 240 |
getBool(); // EMPTY |
| 241 |
} else { |
| 242 |
getBool(); // EMPTY |
| 243 |
} |
| 244 |
sls_mu.Lock(); |
| 245 |
} |
| 246 |
sls_mu.Unlock(); |
| 247 |
} |
| 248 |
|
| 249 |
void sls_fun_good_7() { |
| 250 |
sls_mu.Lock(); |
| 251 |
while (getBool()) { |
| 252 |
sls_mu.Unlock(); |
| 253 |
if (getBool()) { |
| 254 |
if (getBool()) { |
| 255 |
sls_mu.Lock(); |
| 256 |
continue; |
| 257 |
} |
| 258 |
} |
| 259 |
sls_mu.Lock(); |
| 260 |
} |
| 261 |
sls_mu.Unlock(); |
| 262 |
} |
| 263 |
|
| 264 |
void sls_fun_good_8() { |
| 265 |
sls_mw.MyLock(); |
| 266 |
sls_mw.mu.Unlock(); |
| 267 |
} |
| 268 |
|
| 269 |
void sls_fun_bad_1() { |
| 270 |
sls_mu.Unlock(); // \ |
| 271 |
// expected-warning{{releasing mutex 'sls_mu' that was not held}} |
| 272 |
} |
| 273 |
|
| 274 |
void sls_fun_bad_2() { |
| 275 |
sls_mu.Lock(); // expected-note{{mutex acquired here}} |
| 276 |
sls_mu.Lock(); // \ |
| 277 |
// expected-warning{{acquiring mutex 'sls_mu' that is already held}} |
| 278 |
sls_mu.Unlock(); |
| 279 |
} |
| 280 |
|
| 281 |
void sls_fun_bad_3() { |
| 282 |
sls_mu.Lock(); // expected-note {{mutex acquired here}} |
| 283 |
} // expected-warning{{mutex 'sls_mu' is still held at the end of function}} |
| 284 |
|
| 285 |
void sls_fun_bad_4() { |
| 286 |
if (getBool()) |
| 287 |
sls_mu.Lock(); // expected-note{{mutex acquired here}} |
| 288 |
else |
| 289 |
sls_mu2.Lock(); // expected-note{{mutex acquired here}} |
| 290 |
} // expected-warning{{mutex 'sls_mu' is not held on every path through here}} \ |
| 291 |
// expected-warning{{mutex 'sls_mu2' is not held on every path through here}} |
| 292 |
|
| 293 |
void sls_fun_bad_5() { |
| 294 |
sls_mu.Lock(); // expected-note {{mutex acquired here}} |
| 295 |
if (getBool()) |
| 296 |
sls_mu.Unlock(); |
| 297 |
} // expected-warning{{mutex 'sls_mu' is not held on every path through here}} |
| 298 |
|
| 299 |
void sls_fun_bad_6() { |
| 300 |
if (getBool()) { |
| 301 |
sls_mu.Lock(); // expected-note {{mutex acquired here}} |
| 302 |
} else { |
| 303 |
if (getBool()) { |
| 304 |
getBool(); // EMPTY |
| 305 |
} else { |
| 306 |
getBool(); // EMPTY |
| 307 |
} |
| 308 |
} |
| 309 |
sls_mu.Unlock(); // \ |
| 310 |
expected-warning{{mutex 'sls_mu' is not held on every path through here}}\ |
| 311 |
expected-warning{{releasing mutex 'sls_mu' that was not held}} |
| 312 |
} |
| 313 |
|
| 314 |
void sls_fun_bad_7() { |
| 315 |
sls_mu.Lock(); |
| 316 |
while (getBool()) { // \ |
| 317 |
expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} |
| 318 |
sls_mu.Unlock(); |
| 319 |
if (getBool()) { |
| 320 |
if (getBool()) { |
| 321 |
continue; |
| 322 |
} |
| 323 |
} |
| 324 |
sls_mu.Lock(); // expected-note {{mutex acquired here}} |
| 325 |
} |
| 326 |
sls_mu.Unlock(); |
| 327 |
} |
| 328 |
|
| 329 |
void sls_fun_bad_8() { |
| 330 |
sls_mu.Lock(); // expected-note{{mutex acquired here}} |
| 331 |
|
| 332 |
do { |
| 333 |
sls_mu.Unlock(); // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} |
| 334 |
} while (getBool()); |
| 335 |
} |
| 336 |
|
| 337 |
void sls_fun_bad_9() { |
| 338 |
do { |
| 339 |
sls_mu.Lock(); // \ |
| 340 |
// expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} \ |
| 341 |
// expected-note{{mutex acquired here}} |
| 342 |
} while (getBool()); |
| 343 |
sls_mu.Unlock(); |
| 344 |
} |
| 345 |
|
| 346 |
void sls_fun_bad_10() { |
| 347 |
sls_mu.Lock(); // expected-note 2{{mutex acquired here}} |
| 348 |
while(getBool()) { // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} |
| 349 |
sls_mu.Unlock(); |
| 350 |
} |
| 351 |
} // expected-warning{{mutex 'sls_mu' is still held at the end of function}} |
| 352 |
|
| 353 |
void sls_fun_bad_11() { |
| 354 |
while (getBool()) { // \ |
| 355 |
expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} |
| 356 |
sls_mu.Lock(); // expected-note {{mutex acquired here}} |
| 357 |
} |
| 358 |
sls_mu.Unlock(); // \ |
| 359 |
// expected-warning{{releasing mutex 'sls_mu' that was not held}} |
| 360 |
} |
| 361 |
|
| 362 |
void sls_fun_bad_12() { |
| 363 |
sls_mu.Lock(); // expected-note {{mutex acquired here}} |
| 364 |
while (getBool()) { |
| 365 |
sls_mu.Unlock(); |
| 366 |
if (getBool()) { |
| 367 |
if (getBool()) { |
| 368 |
break; |
| 369 |
} |
| 370 |
} |
| 371 |
sls_mu.Lock(); |
| 372 |
} |
| 373 |
sls_mu.Unlock(); // \ |
| 374 |
expected-warning{{mutex 'sls_mu' is not held on every path through here}} \ |
| 375 |
expected-warning{{releasing mutex 'sls_mu' that was not held}} |
| 376 |
} |
| 377 |
|
| 378 |
//-----------------------------------------// |
| 379 |
// Handling lock expressions in attribute args |
| 380 |
// -------------------------------------------// |
| 381 |
|
| 382 |
Mutex aa_mu; |
| 383 |
|
| 384 |
class GlobalLocker { |
| 385 |
public: |
| 386 |
void globalLock() EXCLUSIVE_LOCK_FUNCTION(aa_mu); |
| 387 |
void globalUnlock() UNLOCK_FUNCTION(aa_mu); |
| 388 |
}; |
| 389 |
|
| 390 |
GlobalLocker glock; |
| 391 |
|
| 392 |
void aa_fun_1() { |
| 393 |
glock.globalLock(); |
| 394 |
glock.globalUnlock(); |
| 395 |
} |
| 396 |
|
| 397 |
void aa_fun_bad_1() { |
| 398 |
glock.globalUnlock(); // \ |
| 399 |
// expected-warning{{releasing mutex 'aa_mu' that was not held}} |
| 400 |
} |
| 401 |
|
| 402 |
void aa_fun_bad_2() { |
| 403 |
glock.globalLock(); // expected-note{{mutex acquired here}} |
| 404 |
glock.globalLock(); // \ |
| 405 |
// expected-warning{{acquiring mutex 'aa_mu' that is already held}} |
| 406 |
glock.globalUnlock(); |
| 407 |
} |
| 408 |
|
| 409 |
void aa_fun_bad_3() { |
| 410 |
glock.globalLock(); // expected-note{{mutex acquired here}} |
| 411 |
} // expected-warning{{mutex 'aa_mu' is still held at the end of function}} |
| 412 |
|
| 413 |
//--------------------------------------------------// |
| 414 |
// Regression tests for unusual method names |
| 415 |
//--------------------------------------------------// |
| 416 |
|
| 417 |
Mutex wmu; |
| 418 |
|
| 419 |
// Test diagnostics for other method names. |
| 420 |
class WeirdMethods { |
| 421 |
// FIXME: can't currently check inside constructors and destructors. |
| 422 |
WeirdMethods() { |
| 423 |
wmu.Lock(); // EXPECTED-NOTE {{mutex acquired here}} |
| 424 |
} // EXPECTED-WARNING {{mutex 'wmu' is still held at the end of function}} |
| 425 |
~WeirdMethods() { |
| 426 |
wmu.Lock(); // EXPECTED-NOTE {{mutex acquired here}} |
| 427 |
} // EXPECTED-WARNING {{mutex 'wmu' is still held at the end of function}} |
| 428 |
void operator++() { |
| 429 |
wmu.Lock(); // expected-note {{mutex acquired here}} |
| 430 |
} // expected-warning {{mutex 'wmu' is still held at the end of function}} |
| 431 |
operator int*() { |
| 432 |
wmu.Lock(); // expected-note {{mutex acquired here}} |
| 433 |
return 0; |
| 434 |
} // expected-warning {{mutex 'wmu' is still held at the end of function}} |
| 435 |
}; |
| 436 |
|
| 437 |
//-----------------------------------------------// |
| 438 |
// Errors for guarded by or guarded var variables |
| 439 |
// ----------------------------------------------// |
| 440 |
|
| 441 |
int *pgb_gvar __attribute__((pt_guarded_var)); |
| 442 |
int *pgb_var __attribute__((pt_guarded_by(sls_mu))); |
| 443 |
|
| 444 |
class PGBFoo { |
| 445 |
public: |
| 446 |
int x; |
| 447 |
int *pgb_field __attribute__((guarded_by(sls_mu2))) |
| 448 |
__attribute__((pt_guarded_by(sls_mu))); |
| 449 |
void testFoo() { |
| 450 |
pgb_field = &x; // \ |
| 451 |
// expected-warning {{writing variable 'pgb_field' requires holding mutex 'sls_mu2' exclusively}} |
| 452 |
*pgb_field = x; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \ |
| 453 |
// expected-warning {{writing the value pointed to by 'pgb_field' requires holding mutex 'sls_mu' exclusively}} |
| 454 |
x = *pgb_field; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \ |
| 455 |
// expected-warning {{reading the value pointed to by 'pgb_field' requires holding mutex 'sls_mu'}} |
| 456 |
(*pgb_field)++; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \ |
| 457 |
// expected-warning {{writing the value pointed to by 'pgb_field' requires holding mutex 'sls_mu' exclusively}} |
| 458 |
} |
| 459 |
}; |
| 460 |
|
| 461 |
class GBFoo { |
| 462 |
public: |
| 463 |
int gb_field __attribute__((guarded_by(sls_mu))); |
| 464 |
|
| 465 |
void testFoo() { |
| 466 |
gb_field = 0; // \ |
| 467 |
// expected-warning {{writing variable 'gb_field' requires holding mutex 'sls_mu' exclusively}} |
| 468 |
} |
| 469 |
|
| 470 |
void testNoAnal() NO_THREAD_SAFETY_ANALYSIS { |
| 471 |
gb_field = 0; |
| 472 |
} |
| 473 |
}; |
| 474 |
|
| 475 |
GBFoo GlobalGBFoo __attribute__((guarded_by(sls_mu))); |
| 476 |
|
| 477 |
void gb_fun_0() { |
| 478 |
sls_mu.Lock(); |
| 479 |
int x = *pgb_var; |
| 480 |
sls_mu.Unlock(); |
| 481 |
} |
| 482 |
|
| 483 |
void gb_fun_1() { |
| 484 |
sls_mu.Lock(); |
| 485 |
*pgb_var = 2; |
| 486 |
sls_mu.Unlock(); |
| 487 |
} |
| 488 |
|
| 489 |
void gb_fun_2() { |
| 490 |
int x; |
| 491 |
pgb_var = &x; |
| 492 |
} |
| 493 |
|
| 494 |
void gb_fun_3() { |
| 495 |
int *x = pgb_var; |
| 496 |
} |
| 497 |
|
| 498 |
void gb_bad_0() { |
| 499 |
sls_guard_var = 1; // \ |
| 500 |
// expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}} |
| 501 |
} |
| 502 |
|
| 503 |
void gb_bad_1() { |
| 504 |
int x = sls_guard_var; // \ |
| 505 |
// expected-warning{{reading variable 'sls_guard_var' requires holding any mutex}} |
| 506 |
} |
| 507 |
|
| 508 |
void gb_bad_2() { |
| 509 |
sls_guardby_var = 1; // \ |
| 510 |
// expected-warning {{writing variable 'sls_guardby_var' requires holding mutex 'sls_mu' exclusively}} |
| 511 |
} |
| 512 |
|
| 513 |
void gb_bad_3() { |
| 514 |
int x = sls_guardby_var; // \ |
| 515 |
// expected-warning {{reading variable 'sls_guardby_var' requires holding mutex 'sls_mu'}} |
| 516 |
} |
| 517 |
|
| 518 |
void gb_bad_4() { |
| 519 |
*pgb_gvar = 1; // \ |
| 520 |
// expected-warning {{writing the value pointed to by 'pgb_gvar' requires holding any mutex exclusively}} |
| 521 |
} |
| 522 |
|
| 523 |
void gb_bad_5() { |
| 524 |
int x = *pgb_gvar; // \ |
| 525 |
// expected-warning {{reading the value pointed to by 'pgb_gvar' requires holding any mutex}} |
| 526 |
} |
| 527 |
|
| 528 |
void gb_bad_6() { |
| 529 |
*pgb_var = 1; // \ |
| 530 |
// expected-warning {{writing the value pointed to by 'pgb_var' requires holding mutex 'sls_mu' exclusively}} |
| 531 |
} |
| 532 |
|
| 533 |
void gb_bad_7() { |
| 534 |
int x = *pgb_var; // \ |
| 535 |
// expected-warning {{reading the value pointed to by 'pgb_var' requires holding mutex 'sls_mu'}} |
| 536 |
} |
| 537 |
|
| 538 |
void gb_bad_8() { |
| 539 |
GBFoo G; |
| 540 |
G.gb_field = 0; // \ |
| 541 |
// expected-warning {{writing variable 'gb_field' requires holding mutex 'sls_mu'}} |
| 542 |
} |
| 543 |
|
| 544 |
void gb_bad_9() { |
| 545 |
sls_guard_var++; // \ |
| 546 |
// expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}} |
| 547 |
sls_guard_var--; // \ |
| 548 |
// expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}} |
| 549 |
++sls_guard_var; // \ |
| 550 |
// expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}} |
| 551 |
--sls_guard_var;// \ |
| 552 |
// expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}} |
| 553 |
} |
| 554 |
|
| 555 |
//-----------------------------------------------// |
| 556 |
// Warnings on variables with late parsed attributes |
| 557 |
// ----------------------------------------------// |
| 558 |
|
| 559 |
class LateFoo { |
| 560 |
public: |
| 561 |
int a __attribute__((guarded_by(mu))); |
| 562 |
int b; |
| 563 |
|
| 564 |
void foo() EXCLUSIVE_LOCKS_REQUIRED(mu) { } |
| 565 |
|
| 566 |
void test() { |
| 567 |
a = 0; // \ |
| 568 |
// expected-warning{{writing variable 'a' requires holding mutex 'mu' exclusively}} |
| 569 |
b = a; // \ |
| 570 |
// expected-warning {{reading variable 'a' requires holding mutex 'mu'}} |
| 571 |
c = 0; // \ |
| 572 |
// expected-warning {{writing variable 'c' requires holding mutex 'mu' exclusively}} |
| 573 |
} |
| 574 |
|
| 575 |
int c __attribute__((guarded_by(mu))); |
| 576 |
|
| 577 |
Mutex mu; |
| 578 |
}; |
| 579 |
|
| 580 |
class LateBar { |
| 581 |
public: |
| 582 |
int a_ __attribute__((guarded_by(mu1_))); |
| 583 |
int b_; |
| 584 |
int *q __attribute__((pt_guarded_by(mu))); |
| 585 |
Mutex mu1_; |
| 586 |
Mutex mu; |
| 587 |
LateFoo Foo; |
| 588 |
LateFoo Foo2; |
| 589 |
LateFoo *FooPointer; |
| 590 |
}; |
| 591 |
|
| 592 |
LateBar b1, *b3; |
| 593 |
|
| 594 |
void late_0() { |
| 595 |
LateFoo FooA; |
| 596 |
LateFoo FooB; |
| 597 |
FooA.mu.Lock(); |
| 598 |
FooA.a = 5; |
| 599 |
FooA.mu.Unlock(); |
| 600 |
} |
| 601 |
|
| 602 |
void late_1() { |
| 603 |
LateBar BarA; |
| 604 |
BarA.FooPointer->mu.Lock(); |
| 605 |
BarA.FooPointer->a = 2; |
| 606 |
BarA.FooPointer->mu.Unlock(); |
| 607 |
} |
| 608 |
|
| 609 |
void late_bad_0() { |
| 610 |
LateFoo fooA; |
| 611 |
LateFoo fooB; |
| 612 |
fooA.mu.Lock(); |
| 613 |
fooB.a = 5; // \ |
| 614 |
// expected-warning{{writing variable 'a' requires holding mutex 'fooB.mu' exclusively}} \ |
| 615 |
// expected-note{{found near match 'fooA.mu'}} |
| 616 |
fooA.mu.Unlock(); |
| 617 |
} |
| 618 |
|
| 619 |
void late_bad_1() { |
| 620 |
Mutex mu; |
| 621 |
mu.Lock(); |
| 622 |
b1.mu1_.Lock(); |
| 623 |
int res = b1.a_ + b3->b_; |
| 624 |
b3->b_ = *b1.q; // \ |
| 625 |
// expected-warning{{reading the value pointed to by 'q' requires holding mutex 'b1.mu'}} |
| 626 |
b1.mu1_.Unlock(); |
| 627 |
b1.b_ = res; |
| 628 |
mu.Unlock(); |
| 629 |
} |
| 630 |
|
| 631 |
void late_bad_2() { |
| 632 |
LateBar BarA; |
| 633 |
BarA.FooPointer->mu.Lock(); |
| 634 |
BarA.Foo.a = 2; // \ |
| 635 |
// expected-warning{{writing variable 'a' requires holding mutex 'BarA.Foo.mu' exclusively}} \ |
| 636 |
// expected-note{{found near match 'BarA.FooPointer->mu'}} |
| 637 |
BarA.FooPointer->mu.Unlock(); |
| 638 |
} |
| 639 |
|
| 640 |
void late_bad_3() { |
| 641 |
LateBar BarA; |
| 642 |
BarA.Foo.mu.Lock(); |
| 643 |
BarA.FooPointer->a = 2; // \ |
| 644 |
// expected-warning{{writing variable 'a' requires holding mutex 'BarA.FooPointer->mu' exclusively}} \ |
| 645 |
// expected-note{{found near match 'BarA.Foo.mu'}} |
| 646 |
BarA.Foo.mu.Unlock(); |
| 647 |
} |
| 648 |
|
| 649 |
void late_bad_4() { |
| 650 |
LateBar BarA; |
| 651 |
BarA.Foo.mu.Lock(); |
| 652 |
BarA.Foo2.a = 2; // \ |
| 653 |
// expected-warning{{writing variable 'a' requires holding mutex 'BarA.Foo2.mu' exclusively}} \ |
| 654 |
// expected-note{{found near match 'BarA.Foo.mu'}} |
| 655 |
BarA.Foo.mu.Unlock(); |
| 656 |
} |
| 657 |
|
| 658 |
//-----------------------------------------------// |
| 659 |
// Extra warnings for shared vs. exclusive locks |
| 660 |
// ----------------------------------------------// |
| 661 |
|
| 662 |
void shared_fun_0() { |
| 663 |
sls_mu.Lock(); |
| 664 |
do { |
| 665 |
sls_mu.Unlock(); |
| 666 |
sls_mu.Lock(); |
| 667 |
} while (getBool()); |
| 668 |
sls_mu.Unlock(); |
| 669 |
} |
| 670 |
|
| 671 |
void shared_fun_1() { |
| 672 |
sls_mu.ReaderLock(); // \ |
| 673 |
// expected-note {{the other acquisition of mutex 'sls_mu' is here}} |
| 674 |
do { |
| 675 |
sls_mu.Unlock(); |
| 676 |
sls_mu.Lock(); // \ |
| 677 |
// expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}} |
| 678 |
} while (getBool()); |
| 679 |
sls_mu.Unlock(); |
| 680 |
} |
| 681 |
|
| 682 |
void shared_fun_3() { |
| 683 |
if (getBool()) |
| 684 |
sls_mu.Lock(); |
| 685 |
else |
| 686 |
sls_mu.Lock(); |
| 687 |
*pgb_var = 1; |
| 688 |
sls_mu.Unlock(); |
| 689 |
} |
| 690 |
|
| 691 |
void shared_fun_4() { |
| 692 |
if (getBool()) |
| 693 |
sls_mu.ReaderLock(); |
| 694 |
else |
| 695 |
sls_mu.ReaderLock(); |
| 696 |
int x = sls_guardby_var; |
| 697 |
sls_mu.Unlock(); |
| 698 |
} |
| 699 |
|
| 700 |
void shared_fun_8() { |
| 701 |
if (getBool()) |
| 702 |
sls_mu.Lock(); // \ |
| 703 |
// expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}} |
| 704 |
else |
| 705 |
sls_mu.ReaderLock(); // \ |
| 706 |
// expected-note {{the other acquisition of mutex 'sls_mu' is here}} |
| 707 |
sls_mu.Unlock(); |
| 708 |
} |
| 709 |
|
| 710 |
void shared_fun_9() { |
| 711 |
sls_mu.Lock(); |
| 712 |
sls_mu.ExclusiveUnlock(); |
| 713 |
|
| 714 |
sls_mu.ReaderLock(); |
| 715 |
sls_mu.ReaderUnlock(); |
| 716 |
} |
| 717 |
|
| 718 |
void shared_fun_10() { |
| 719 |
sls_mu.Lock(); |
| 720 |
sls_mu.DemoteExclusive(); |
| 721 |
sls_mu.ReaderUnlock(); |
| 722 |
} |
| 723 |
|
| 724 |
void shared_fun_11() { |
| 725 |
sls_mu.ReaderLock(); |
| 726 |
sls_mu.PromoteShared(); |
| 727 |
sls_mu.Unlock(); |
| 728 |
} |
| 729 |
|
| 730 |
void shared_bad_0() { |
| 731 |
sls_mu.Lock(); // \ |
| 732 |
// expected-note {{the other acquisition of mutex 'sls_mu' is here}} |
| 733 |
do { |
| 734 |
sls_mu.Unlock(); |
| 735 |
sls_mu.ReaderLock(); // \ |
| 736 |
// expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}} |
| 737 |
} while (getBool()); |
| 738 |
sls_mu.Unlock(); |
| 739 |
} |
| 740 |
|
| 741 |
void shared_bad_1() { |
| 742 |
if (getBool()) |
| 743 |
sls_mu.Lock(); // \ |
| 744 |
// expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}} |
| 745 |
else |
| 746 |
sls_mu.ReaderLock(); // \ |
| 747 |
// expected-note {{the other acquisition of mutex 'sls_mu' is here}} |
| 748 |
*pgb_var = 1; |
| 749 |
sls_mu.Unlock(); |
| 750 |
} |
| 751 |
|
| 752 |
void shared_bad_2() { |
| 753 |
if (getBool()) |
| 754 |
sls_mu.ReaderLock(); // \ |
| 755 |
// expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}} |
| 756 |
else |
| 757 |
sls_mu.Lock(); // \ |
| 758 |
// expected-note {{the other acquisition of mutex 'sls_mu' is here}} |
| 759 |
*pgb_var = 1; |
| 760 |
sls_mu.Unlock(); |
| 761 |
} |
| 762 |
|
| 763 |
void shared_bad_3() { |
| 764 |
sls_mu.Lock(); // expected-note {{mutex acquired here}} |
| 765 |
sls_mu.ReaderUnlock(); // \ |
| 766 |
// expected-warning {{releasing mutex 'sls_mu' using shared access, expected exclusive access}} |
| 767 |
} |
| 768 |
|
| 769 |
void shared_bad_4() { |
| 770 |
sls_mu.ReaderLock(); // expected-note {{mutex acquired here}} |
| 771 |
sls_mu.ExclusiveUnlock(); // \ |
| 772 |
// expected-warning {{releasing mutex 'sls_mu' using exclusive access, expected shared access}} |
| 773 |
} |
| 774 |
|
| 775 |
void shared_bad_5() { |
| 776 |
sls_mu.Lock(); // expected-note {{mutex acquired here}} |
| 777 |
sls_mu.PromoteShared(); // \ |
| 778 |
// expected-warning {{releasing mutex 'sls_mu' using shared access, expected exclusive access}} |
| 779 |
sls_mu.ExclusiveUnlock(); |
| 780 |
} |
| 781 |
|
| 782 |
void shared_bad_6() { |
| 783 |
sls_mu.ReaderLock(); // expected-note {{mutex acquired here}} |
| 784 |
sls_mu.DemoteExclusive(); // \ |
| 785 |
// expected-warning {{releasing mutex 'sls_mu' using exclusive access, expected shared access}} |
| 786 |
sls_mu.ReaderUnlock(); |
| 787 |
} |
| 788 |
|
| 789 |
// FIXME: Add support for functions (not only methods) |
| 790 |
class LRBar { |
| 791 |
public: |
| 792 |
void aa_elr_fun() EXCLUSIVE_LOCKS_REQUIRED(aa_mu); |
| 793 |
void aa_elr_fun_s() SHARED_LOCKS_REQUIRED(aa_mu); |
| 794 |
void le_fun() __attribute__((locks_excluded(sls_mu))); |
| 795 |
}; |
| 796 |
|
| 797 |
class LRFoo { |
| 798 |
public: |
| 799 |
void test() EXCLUSIVE_LOCKS_REQUIRED(sls_mu); |
| 800 |
void testShared() SHARED_LOCKS_REQUIRED(sls_mu2); |
| 801 |
}; |
| 802 |
|
| 803 |
void elr_fun() EXCLUSIVE_LOCKS_REQUIRED(sls_mu); |
| 804 |
void elr_fun() {} |
| 805 |
|
| 806 |
LRFoo MyLRFoo; |
| 807 |
LRBar Bar; |
| 808 |
|
| 809 |
void es_fun_0() { |
| 810 |
aa_mu.Lock(); |
| 811 |
Bar.aa_elr_fun(); |
| 812 |
aa_mu.Unlock(); |
| 813 |
} |
| 814 |
|
| 815 |
void es_fun_1() { |
| 816 |
aa_mu.Lock(); |
| 817 |
Bar.aa_elr_fun_s(); |
| 818 |
aa_mu.Unlock(); |
| 819 |
} |
| 820 |
|
| 821 |
void es_fun_2() { |
| 822 |
aa_mu.ReaderLock(); |
| 823 |
Bar.aa_elr_fun_s(); |
| 824 |
aa_mu.Unlock(); |
| 825 |
} |
| 826 |
|
| 827 |
void es_fun_3() { |
| 828 |
sls_mu.Lock(); |
| 829 |
MyLRFoo.test(); |
| 830 |
sls_mu.Unlock(); |
| 831 |
} |
| 832 |
|
| 833 |
void es_fun_4() { |
| 834 |
sls_mu2.Lock(); |
| 835 |
MyLRFoo.testShared(); |
| 836 |
sls_mu2.Unlock(); |
| 837 |
} |
| 838 |
|
| 839 |
void es_fun_5() { |
| 840 |
sls_mu2.ReaderLock(); |
| 841 |
MyLRFoo.testShared(); |
| 842 |
sls_mu2.Unlock(); |
| 843 |
} |
| 844 |
|
| 845 |
void es_fun_6() { |
| 846 |
Bar.le_fun(); |
| 847 |
} |
| 848 |
|
| 849 |
void es_fun_7() { |
| 850 |
sls_mu.Lock(); |
| 851 |
elr_fun(); |
| 852 |
sls_mu.Unlock(); |
| 853 |
} |
| 854 |
|
| 855 |
void es_fun_8() NO_THREAD_SAFETY_ANALYSIS; |
| 856 |
|
| 857 |
void es_fun_8() { |
| 858 |
Bar.aa_elr_fun_s(); |
| 859 |
} |
| 860 |
|
| 861 |
void es_fun_9() SHARED_LOCKS_REQUIRED(aa_mu); |
| 862 |
void es_fun_9() { |
| 863 |
Bar.aa_elr_fun_s(); |
| 864 |
} |
| 865 |
|
| 866 |
void es_fun_10() EXCLUSIVE_LOCKS_REQUIRED(aa_mu); |
| 867 |
void es_fun_10() { |
| 868 |
Bar.aa_elr_fun_s(); |
| 869 |
} |
| 870 |
|
| 871 |
void es_bad_0() { |
| 872 |
Bar.aa_elr_fun(); // \ |
| 873 |
// expected-warning {{calling function 'aa_elr_fun' requires holding mutex 'aa_mu' exclusively}} |
| 874 |
} |
| 875 |
|
| 876 |
void es_bad_1() { |
| 877 |
aa_mu.ReaderLock(); |
| 878 |
Bar.aa_elr_fun(); // \ |
| 879 |
// expected-warning {{calling function 'aa_elr_fun' requires holding mutex 'aa_mu' exclusively}} |
| 880 |
aa_mu.Unlock(); |
| 881 |
} |
| 882 |
|
| 883 |
void es_bad_2() { |
| 884 |
Bar.aa_elr_fun_s(); // \ |
| 885 |
// expected-warning {{calling function 'aa_elr_fun_s' requires holding mutex 'aa_mu'}} |
| 886 |
} |
| 887 |
|
| 888 |
void es_bad_3() { |
| 889 |
MyLRFoo.test(); // \ |
| 890 |
// expected-warning {{calling function 'test' requires holding mutex 'sls_mu' exclusively}} |
| 891 |
} |
| 892 |
|
| 893 |
void es_bad_4() { |
| 894 |
MyLRFoo.testShared(); // \ |
| 895 |
// expected-warning {{calling function 'testShared' requires holding mutex 'sls_mu2'}} |
| 896 |
} |
| 897 |
|
| 898 |
void es_bad_5() { |
| 899 |
sls_mu.ReaderLock(); |
| 900 |
MyLRFoo.test(); // \ |
| 901 |
// expected-warning {{calling function 'test' requires holding mutex 'sls_mu' exclusively}} |
| 902 |
sls_mu.Unlock(); |
| 903 |
} |
| 904 |
|
| 905 |
void es_bad_6() { |
| 906 |
sls_mu.Lock(); |
| 907 |
Bar.le_fun(); // \ |
| 908 |
// expected-warning {{cannot call function 'le_fun' while mutex 'sls_mu' is held}} |
| 909 |
sls_mu.Unlock(); |
| 910 |
} |
| 911 |
|
| 912 |
void es_bad_7() { |
| 913 |
sls_mu.ReaderLock(); |
| 914 |
Bar.le_fun(); // \ |
| 915 |
// expected-warning {{cannot call function 'le_fun' while mutex 'sls_mu' is held}} |
| 916 |
sls_mu.Unlock(); |
| 917 |
} |
| 918 |
|
| 919 |
|
| 920 |
//-----------------------------------------------// |
| 921 |
// Unparseable lock expressions |
| 922 |
// ----------------------------------------------// |
| 923 |
|
| 924 |
// FIXME -- derive new tests for unhandled expressions |
| 925 |
|
| 926 |
|
| 927 |
//----------------------------------------------------------------------------// |
| 928 |
// The following test cases are ported from the gcc thread safety implementation |
| 929 |
// They are each wrapped inside a namespace with the test number of the gcc test |
| 930 |
// |
| 931 |
// FIXME: add all the gcc tests, once this analysis passes them. |
| 932 |
//----------------------------------------------------------------------------// |
| 933 |
|
| 934 |
//-----------------------------------------// |
| 935 |
// Good testcases (no errors) |
| 936 |
//-----------------------------------------// |
| 937 |
|
| 938 |
namespace thread_annot_lock_20 { |
| 939 |
class Bar { |
| 940 |
public: |
| 941 |
static int func1() EXCLUSIVE_LOCKS_REQUIRED(mu1_); |
| 942 |
static int b_ GUARDED_BY(mu1_); |
| 943 |
static Mutex mu1_; |
| 944 |
static int a_ GUARDED_BY(mu1_); |
| 945 |
}; |
| 946 |
|
| 947 |
Bar b1; |
| 948 |
|
| 949 |
int Bar::func1() |
| 950 |
{ |
| 951 |
int res = 5; |
| 952 |
|
| 953 |
if (a_ == 4) |
| 954 |
res = b_; |
| 955 |
return res; |
| 956 |
} |
| 957 |
} // end namespace thread_annot_lock_20 |
| 958 |
|
| 959 |
namespace thread_annot_lock_22 { |
| 960 |
// Test various usage of GUARDED_BY and PT_GUARDED_BY annotations, especially |
| 961 |
// uses in class definitions. |
| 962 |
Mutex mu; |
| 963 |
|
| 964 |
class Bar { |
| 965 |
public: |
| 966 |
int a_ GUARDED_BY(mu1_); |
| 967 |
int b_; |
| 968 |
int *q PT_GUARDED_BY(mu); |
| 969 |
Mutex mu1_ ACQUIRED_AFTER(mu); |
| 970 |
}; |
| 971 |
|
| 972 |
Bar b1, *b3; |
| 973 |
int *p GUARDED_BY(mu) PT_GUARDED_BY(mu); |
| 974 |
int res GUARDED_BY(mu) = 5; |
| 975 |
|
| 976 |
int func(int i) |
| 977 |
{ |
| 978 |
int x; |
| 979 |
mu.Lock(); |
| 980 |
b1.mu1_.Lock(); |
| 981 |
res = b1.a_ + b3->b_; |
| 982 |
*p = i; |
| 983 |
b1.a_ = res + b3->b_; |
| 984 |
b3->b_ = *b1.q; |
| 985 |
b1.mu1_.Unlock(); |
| 986 |
b1.b_ = res; |
| 987 |
x = res; |
| 988 |
mu.Unlock(); |
| 989 |
return x; |
| 990 |
} |
| 991 |
} // end namespace thread_annot_lock_22 |
| 992 |
|
| 993 |
namespace thread_annot_lock_27_modified { |
| 994 |
// test lock annotations applied to function definitions |
| 995 |
// Modified: applied annotations only to function declarations |
| 996 |
Mutex mu1; |
| 997 |
Mutex mu2 ACQUIRED_AFTER(mu1); |
| 998 |
|
| 999 |
class Foo { |
| 1000 |
public: |
| 1001 |
int method1(int i) SHARED_LOCKS_REQUIRED(mu2) EXCLUSIVE_LOCKS_REQUIRED(mu1); |
| 1002 |
}; |
| 1003 |
|
| 1004 |
int Foo::method1(int i) { |
| 1005 |
return i; |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
int foo(int i) EXCLUSIVE_LOCKS_REQUIRED(mu2) SHARED_LOCKS_REQUIRED(mu1); |
| 1010 |
int foo(int i) { |
| 1011 |
return i; |
| 1012 |
} |
| 1013 |
|
| 1014 |
static int bar(int i) EXCLUSIVE_LOCKS_REQUIRED(mu1); |
| 1015 |
static int bar(int i) { |
| 1016 |
return i; |
| 1017 |
} |
| 1018 |
|
| 1019 |
void main() { |
| 1020 |
Foo a; |
| 1021 |
|
| 1022 |
mu1.Lock(); |
| 1023 |
mu2.Lock(); |
| 1024 |
a.method1(1); |
| 1025 |
foo(2); |
| 1026 |
mu2.Unlock(); |
| 1027 |
bar(3); |
| 1028 |
mu1.Unlock(); |
| 1029 |
} |
| 1030 |
} // end namespace thread_annot_lock_27_modified |
| 1031 |
|
| 1032 |
|
| 1033 |
namespace thread_annot_lock_38 { |
| 1034 |
// Test the case where a template member function is annotated with lock |
| 1035 |
// attributes in a non-template class. |
| 1036 |
class Foo { |
| 1037 |
public: |
| 1038 |
void func1(int y) LOCKS_EXCLUDED(mu_); |
| 1039 |
template <typename T> void func2(T x) LOCKS_EXCLUDED(mu_); |
| 1040 |
private: |
| 1041 |
Mutex mu_; |
| 1042 |
}; |
| 1043 |
|
| 1044 |
Foo *foo; |
| 1045 |
|
| 1046 |
void main() |
| 1047 |
{ |
| 1048 |
foo->func1(5); |
| 1049 |
foo->func2(5); |
| 1050 |
} |
| 1051 |
} // end namespace thread_annot_lock_38 |
| 1052 |
|
| 1053 |
namespace thread_annot_lock_43 { |
| 1054 |
// Tests lock canonicalization |
| 1055 |
class Foo { |
| 1056 |
public: |
| 1057 |
Mutex *mu_; |
| 1058 |
}; |
| 1059 |
|
| 1060 |
class FooBar { |
| 1061 |
public: |
| 1062 |
Foo *foo_; |
| 1063 |
int GetA() EXCLUSIVE_LOCKS_REQUIRED(foo_->mu_) { return a_; } |
| 1064 |
int a_ GUARDED_BY(foo_->mu_); |
| 1065 |
}; |
| 1066 |
|
| 1067 |
FooBar *fb; |
| 1068 |
|
| 1069 |
void main() |
| 1070 |
{ |
| 1071 |
int x; |
| 1072 |
fb->foo_->mu_->Lock(); |
| 1073 |
x = fb->GetA(); |
| 1074 |
fb->foo_->mu_->Unlock(); |
| 1075 |
} |
| 1076 |
} // end namespace thread_annot_lock_43 |
| 1077 |
|
| 1078 |
namespace thread_annot_lock_49 { |
| 1079 |
// Test the support for use of lock expression in the annotations |
| 1080 |
class Foo { |
| 1081 |
public: |
| 1082 |
Mutex foo_mu_; |
| 1083 |
}; |
| 1084 |
|
| 1085 |
class Bar { |
| 1086 |
private: |
| 1087 |
Foo *foo; |
| 1088 |
Mutex bar_mu_ ACQUIRED_AFTER(foo->foo_mu_); |
| 1089 |
|
| 1090 |
public: |
| 1091 |
void Test1() { |
| 1092 |
foo->foo_mu_.Lock(); |
| 1093 |
bar_mu_.Lock(); |
| 1094 |
bar_mu_.Unlock(); |
| 1095 |
foo->foo_mu_.Unlock(); |
| 1096 |
} |
| 1097 |
}; |
| 1098 |
|
| 1099 |
void main() { |
| 1100 |
Bar bar; |
| 1101 |
bar.Test1(); |
| 1102 |
} |
| 1103 |
} // end namespace thread_annot_lock_49 |
| 1104 |
|
| 1105 |
namespace thread_annot_lock_61_modified { |
| 1106 |
// Modified to fix the compiler errors |
| 1107 |
// Test the fix for a bug introduced by the support of pass-by-reference |
| 1108 |
// parameters. |
| 1109 |
struct Foo { Foo &operator<< (bool) {return *this;} }; |
| 1110 |
Foo &getFoo(); |
| 1111 |
struct Bar { Foo &func () {return getFoo();} }; |
| 1112 |
struct Bas { void operator& (Foo &) {} }; |
| 1113 |
void mumble() |
| 1114 |
{ |
| 1115 |
Bas() & Bar().func() << "" << ""; |
| 1116 |
Bas() & Bar().func() << ""; |
| 1117 |
} |
| 1118 |
} // end namespace thread_annot_lock_61_modified |
| 1119 |
|
| 1120 |
|
| 1121 |
namespace thread_annot_lock_65 { |
| 1122 |
// Test the fix for a bug in the support of allowing reader locks for |
| 1123 |
// non-const, non-modifying overload functions. (We didn't handle the builtin |
| 1124 |
// properly.) |
| 1125 |
enum MyFlags { |
| 1126 |
Zero, |
| 1127 |
One, |
| 1128 |
Two, |
| 1129 |
Three, |
| 1130 |
Four, |
| 1131 |
Five, |
| 1132 |
Six, |
| 1133 |
Seven, |
| 1134 |
Eight, |
| 1135 |
Nine |
| 1136 |
}; |
| 1137 |
|
| 1138 |
inline MyFlags |
| 1139 |
operator|(MyFlags a, MyFlags b) |
| 1140 |
{ |
| 1141 |
return MyFlags(static_cast<int>(a) | static_cast<int>(b)); |
| 1142 |
} |
| 1143 |
|
| 1144 |
inline MyFlags& |
| 1145 |
operator|=(MyFlags& a, MyFlags b) |
| 1146 |
{ |
| 1147 |
return a = a | b; |
| 1148 |
} |
| 1149 |
} // end namespace thread_annot_lock_65 |
| 1150 |
|
| 1151 |
namespace thread_annot_lock_66_modified { |
| 1152 |
// Modified: Moved annotation to function defn |
| 1153 |
// Test annotations on out-of-line definitions of member functions where the |
| 1154 |
// annotations refer to locks that are also data members in the class. |
| 1155 |
Mutex mu; |
| 1156 |
|
| 1157 |
class Foo { |
| 1158 |
public: |
| 1159 |
int method1(int i) SHARED_LOCKS_REQUIRED(mu1, mu, mu2); |
| 1160 |
int data GUARDED_BY(mu1); |
| 1161 |
Mutex *mu1; |
| 1162 |
Mutex *mu2; |
| 1163 |
}; |
| 1164 |
|
| 1165 |
int Foo::method1(int i) |
| 1166 |
{ |
| 1167 |
return data + i; |
| 1168 |
} |
| 1169 |
|
| 1170 |
void main() |
| 1171 |
{ |
| 1172 |
Foo a; |
| 1173 |
|
| 1174 |
a.mu2->Lock(); |
| 1175 |
a.mu1->Lock(); |
| 1176 |
mu.Lock(); |
| 1177 |
a.method1(1); |
| 1178 |
mu.Unlock(); |
| 1179 |
a.mu1->Unlock(); |
| 1180 |
a.mu2->Unlock(); |
| 1181 |
} |
| 1182 |
} // end namespace thread_annot_lock_66_modified |
| 1183 |
|
| 1184 |
namespace thread_annot_lock_68_modified { |
| 1185 |
// Test a fix to a bug in the delayed name binding with nested template |
| 1186 |
// instantiation. We use a stack to make sure a name is not resolved to an |
| 1187 |
// inner context. |
| 1188 |
template <typename T> |
| 1189 |
class Bar { |
| 1190 |
Mutex mu_; |
| 1191 |
}; |
| 1192 |
|
| 1193 |
template <typename T> |
| 1194 |
class Foo { |
| 1195 |
public: |
| 1196 |
void func(T x) { |
| 1197 |
mu_.Lock(); |
| 1198 |
count_ = x; |
| 1199 |
mu_.Unlock(); |
| 1200 |
} |
| 1201 |
|
| 1202 |
private: |
| 1203 |
T count_ GUARDED_BY(mu_); |
| 1204 |
Bar<T> bar_; |
| 1205 |
Mutex mu_; |
| 1206 |
}; |
| 1207 |
|
| 1208 |
void main() |
| 1209 |
{ |
| 1210 |
Foo<int> *foo; |
| 1211 |
foo->func(5); |
| 1212 |
} |
| 1213 |
} // end namespace thread_annot_lock_68_modified |
| 1214 |
|
| 1215 |
namespace thread_annot_lock_30_modified { |
| 1216 |
// Test delay parsing of lock attribute arguments with nested classes. |
| 1217 |
// Modified: trylocks replaced with exclusive_lock_fun |
| 1218 |
int a = 0; |
| 1219 |
|
| 1220 |
class Bar { |
| 1221 |
struct Foo; |
| 1222 |
|
| 1223 |
public: |
| 1224 |
void MyLock() EXCLUSIVE_LOCK_FUNCTION(mu); |
| 1225 |
|
| 1226 |
int func() { |
| 1227 |
MyLock(); |
| 1228 |
// if (foo == 0) { |
| 1229 |
// return 0; |
| 1230 |
// } |
| 1231 |
a = 5; |
| 1232 |
mu.Unlock(); |
| 1233 |
return 1; |
| 1234 |
} |
| 1235 |
|
| 1236 |
class FooBar { |
| 1237 |
int x; |
| 1238 |
int y; |
| 1239 |
}; |
| 1240 |
|
| 1241 |
private: |
| 1242 |
Mutex mu; |
| 1243 |
}; |
| 1244 |
|
| 1245 |
Bar *bar; |
| 1246 |
|
| 1247 |
void main() |
| 1248 |
{ |
| 1249 |
bar->func(); |
| 1250 |
} |
| 1251 |
} // end namespace thread_annot_lock_30_modified |
| 1252 |
|
| 1253 |
namespace thread_annot_lock_47 { |
| 1254 |
// Test the support for annotations on virtual functions. |
| 1255 |
// This is a good test case. (i.e. There should be no warning emitted by the |
| 1256 |
// compiler.) |
| 1257 |
class Base { |
| 1258 |
public: |
| 1259 |
virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 1260 |
virtual void func2() LOCKS_EXCLUDED(mu_); |
| 1261 |
Mutex mu_; |
| 1262 |
}; |
| 1263 |
|
| 1264 |
class Child : public Base { |
| 1265 |
public: |
| 1266 |
virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 1267 |
virtual void func2() LOCKS_EXCLUDED(mu_); |
| 1268 |
}; |
| 1269 |
|
| 1270 |
void main() { |
| 1271 |
Child *c; |
| 1272 |
Base *b = c; |
| 1273 |
|
| 1274 |
b->mu_.Lock(); |
| 1275 |
b->func1(); |
| 1276 |
b->mu_.Unlock(); |
| 1277 |
b->func2(); |
| 1278 |
|
| 1279 |
c->mu_.Lock(); |
| 1280 |
c->func1(); |
| 1281 |
c->mu_.Unlock(); |
| 1282 |
c->func2(); |
| 1283 |
} |
| 1284 |
} // end namespace thread_annot_lock_47 |
| 1285 |
|
| 1286 |
//-----------------------------------------// |
| 1287 |
// Tests which produce errors |
| 1288 |
//-----------------------------------------// |
| 1289 |
|
| 1290 |
namespace thread_annot_lock_13 { |
| 1291 |
Mutex mu1; |
| 1292 |
Mutex mu2; |
| 1293 |
|
| 1294 |
int g GUARDED_BY(mu1); |
| 1295 |
int w GUARDED_BY(mu2); |
| 1296 |
|
| 1297 |
class Foo { |
| 1298 |
public: |
| 1299 |
void bar() LOCKS_EXCLUDED(mu_, mu1); |
| 1300 |
int foo() SHARED_LOCKS_REQUIRED(mu_) EXCLUSIVE_LOCKS_REQUIRED(mu2); |
| 1301 |
|
| 1302 |
private: |
| 1303 |
int a_ GUARDED_BY(mu_); |
| 1304 |
public: |
| 1305 |
Mutex mu_ ACQUIRED_AFTER(mu1); |
| 1306 |
}; |
| 1307 |
|
| 1308 |
int Foo::foo() |
| 1309 |
{ |
| 1310 |
int res; |
| 1311 |
w = 5; |
| 1312 |
res = a_ + 5; |
| 1313 |
return res; |
| 1314 |
} |
| 1315 |
|
| 1316 |
void Foo::bar() |
| 1317 |
{ |
| 1318 |
int x; |
| 1319 |
mu_.Lock(); |
| 1320 |
x = foo(); // expected-warning {{calling function 'foo' requires holding mutex 'mu2' exclusively}} |
| 1321 |
a_ = x + 1; |
| 1322 |
mu_.Unlock(); |
| 1323 |
if (x > 5) { |
| 1324 |
mu1.Lock(); |
| 1325 |
g = 2; |
| 1326 |
mu1.Unlock(); |
| 1327 |
} |
| 1328 |
} |
| 1329 |
|
| 1330 |
void main() |
| 1331 |
{ |
| 1332 |
Foo f1, *f2; |
| 1333 |
f1.mu_.Lock(); |
| 1334 |
f1.bar(); // expected-warning {{cannot call function 'bar' while mutex 'f1.mu_' is held}} |
| 1335 |
mu2.Lock(); |
| 1336 |
f1.foo(); |
| 1337 |
mu2.Unlock(); |
| 1338 |
f1.mu_.Unlock(); |
| 1339 |
f2->mu_.Lock(); |
| 1340 |
f2->bar(); // expected-warning {{cannot call function 'bar' while mutex 'f2->mu_' is held}} |
| 1341 |
f2->mu_.Unlock(); |
| 1342 |
mu2.Lock(); |
| 1343 |
w = 2; |
| 1344 |
mu2.Unlock(); |
| 1345 |
} |
| 1346 |
} // end namespace thread_annot_lock_13 |
| 1347 |
|
| 1348 |
namespace thread_annot_lock_18_modified { |
| 1349 |
// Modified: Trylocks removed |
| 1350 |
// Test the ability to distnguish between the same lock field of |
| 1351 |
// different objects of a class. |
| 1352 |
class Bar { |
| 1353 |
public: |
| 1354 |
bool MyLock() EXCLUSIVE_LOCK_FUNCTION(mu1_); |
| 1355 |
void MyUnlock() UNLOCK_FUNCTION(mu1_); |
| 1356 |
int a_ GUARDED_BY(mu1_); |
| 1357 |
|
| 1358 |
private: |
| 1359 |
Mutex mu1_; |
| 1360 |
}; |
| 1361 |
|
| 1362 |
Bar *b1, *b2; |
| 1363 |
|
| 1364 |
void func() |
| 1365 |
{ |
| 1366 |
b1->MyLock(); |
| 1367 |
b1->a_ = 5; |
| 1368 |
b2->a_ = 3; // \ |
| 1369 |
// expected-warning {{writing variable 'a_' requires holding mutex 'b2->mu1_' exclusively}} \ |
| 1370 |
// expected-note {{found near match 'b1->mu1_'}} |
| 1371 |
b2->MyLock(); |
| 1372 |
b2->MyUnlock(); |
| 1373 |
b1->MyUnlock(); |
| 1374 |
} |
| 1375 |
} // end namespace thread_annot_lock_18_modified |
| 1376 |
|
| 1377 |
namespace thread_annot_lock_21 { |
| 1378 |
// Test various usage of GUARDED_BY and PT_GUARDED_BY annotations, especially |
| 1379 |
// uses in class definitions. |
| 1380 |
Mutex mu; |
| 1381 |
|
| 1382 |
class Bar { |
| 1383 |
public: |
| 1384 |
int a_ GUARDED_BY(mu1_); |
| 1385 |
int b_; |
| 1386 |
int *q PT_GUARDED_BY(mu); |
| 1387 |
Mutex mu1_ ACQUIRED_AFTER(mu); |
| 1388 |
}; |
| 1389 |
|
| 1390 |
Bar b1, *b3; |
| 1391 |
int *p GUARDED_BY(mu) PT_GUARDED_BY(mu); |
| 1392 |
|
| 1393 |
int res GUARDED_BY(mu) = 5; |
| 1394 |
|
| 1395 |
int func(int i) |
| 1396 |
{ |
| 1397 |
int x; |
| 1398 |
b3->mu1_.Lock(); |
| 1399 |
res = b1.a_ + b3->b_; // expected-warning {{reading variable 'a_' requires holding mutex 'b1.mu1_'}} \ |
| 1400 |
// expected-warning {{writing variable 'res' requires holding mutex 'mu' exclusively}} \ |
| 1401 |
// expected-note {{found near match 'b3->mu1_'}} |
| 1402 |
*p = i; // expected-warning {{reading variable 'p' requires holding mutex 'mu'}} \ |
| 1403 |
// expected-warning {{writing the value pointed to by 'p' requires holding mutex 'mu' exclusively}} |
| 1404 |
b1.a_ = res + b3->b_; // expected-warning {{reading variable 'res' requires holding mutex 'mu'}} \ |
| 1405 |
// expected-warning {{writing variable 'a_' requires holding mutex 'b1.mu1_' exclusively}} \ |
| 1406 |
// expected-note {{found near match 'b3->mu1_'}} |
| 1407 |
b3->b_ = *b1.q; // expected-warning {{reading the value pointed to by 'q' requires holding mutex 'mu'}} |
| 1408 |
b3->mu1_.Unlock(); |
| 1409 |
b1.b_ = res; // expected-warning {{reading variable 'res' requires holding mutex 'mu'}} |
| 1410 |
x = res; // expected-warning {{reading variable 'res' requires holding mutex 'mu'}} |
| 1411 |
return x; |
| 1412 |
} |
| 1413 |
} // end namespace thread_annot_lock_21 |
| 1414 |
|
| 1415 |
namespace thread_annot_lock_35_modified { |
| 1416 |
// Test the analyzer's ability to distinguish the lock field of different |
| 1417 |
// objects. |
| 1418 |
class Foo { |
| 1419 |
private: |
| 1420 |
Mutex lock_; |
| 1421 |
int a_ GUARDED_BY(lock_); |
| 1422 |
|
| 1423 |
public: |
| 1424 |
void Func(Foo* child) LOCKS_EXCLUDED(lock_) { |
| 1425 |
Foo *new_foo = new Foo; |
| 1426 |
|
| 1427 |
lock_.Lock(); |
| 1428 |
|
| 1429 |
child->Func(new_foo); // There shouldn't be any warning here as the |
| 1430 |
// acquired lock is not in child. |
| 1431 |
child->bar(7); // \ |
| 1432 |
// expected-warning {{calling function 'bar' requires holding mutex 'child->lock_' exclusively}} \ |
| 1433 |
// expected-note {{found near match 'lock_'}} |
| 1434 |
child->a_ = 5; // \ |
| 1435 |
// expected-warning {{writing variable 'a_' requires holding mutex 'child->lock_' exclusively}} \ |
| 1436 |
// expected-note {{found near match 'lock_'}} |
| 1437 |
lock_.Unlock(); |
| 1438 |
} |
| 1439 |
|
| 1440 |
void bar(int y) EXCLUSIVE_LOCKS_REQUIRED(lock_) { |
| 1441 |
a_ = y; |
| 1442 |
} |
| 1443 |
}; |
| 1444 |
|
| 1445 |
Foo *x; |
| 1446 |
|
| 1447 |
void main() { |
| 1448 |
Foo *child = new Foo; |
| 1449 |
x->Func(child); |
| 1450 |
} |
| 1451 |
} // end namespace thread_annot_lock_35_modified |
| 1452 |
|
| 1453 |
namespace thread_annot_lock_36_modified { |
| 1454 |
// Modified to move the annotations to function defns. |
| 1455 |
// Test the analyzer's ability to distinguish the lock field of different |
| 1456 |
// objects |
| 1457 |
class Foo { |
| 1458 |
private: |
| 1459 |
Mutex lock_; |
| 1460 |
int a_ GUARDED_BY(lock_); |
| 1461 |
|
| 1462 |
public: |
| 1463 |
void Func(Foo* child) LOCKS_EXCLUDED(lock_); |
| 1464 |
void bar(int y) EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 1465 |
}; |
| 1466 |
|
| 1467 |
void Foo::Func(Foo* child) { |
| 1468 |
Foo *new_foo = new Foo; |
| 1469 |
|
| 1470 |
lock_.Lock(); |
| 1471 |
|
| 1472 |
child->lock_.Lock(); |
| 1473 |
child->Func(new_foo); // expected-warning {{cannot call function 'Func' while mutex 'child->lock_' is held}} |
| 1474 |
child->bar(7); |
| 1475 |
child->a_ = 5; |
| 1476 |
child->lock_.Unlock(); |
| 1477 |
|
| 1478 |
lock_.Unlock(); |
| 1479 |
} |
| 1480 |
|
| 1481 |
void Foo::bar(int y) { |
| 1482 |
a_ = y; |
| 1483 |
} |
| 1484 |
|
| 1485 |
|
| 1486 |
Foo *x; |
| 1487 |
|
| 1488 |
void main() { |
| 1489 |
Foo *child = new Foo; |
| 1490 |
x->Func(child); |
| 1491 |
} |
| 1492 |
} // end namespace thread_annot_lock_36_modified |
| 1493 |
|
| 1494 |
|
| 1495 |
namespace thread_annot_lock_42 { |
| 1496 |
// Test support of multiple lock attributes of the same kind on a decl. |
| 1497 |
class Foo { |
| 1498 |
private: |
| 1499 |
Mutex mu1, mu2, mu3; |
| 1500 |
int x GUARDED_BY(mu1) GUARDED_BY(mu2); |
| 1501 |
int y GUARDED_BY(mu2); |
| 1502 |
|
| 1503 |
void f2() LOCKS_EXCLUDED(mu1) LOCKS_EXCLUDED(mu2) LOCKS_EXCLUDED(mu3) { |
| 1504 |
mu2.Lock(); |
| 1505 |
y = 2; |
| 1506 |
mu2.Unlock(); |
| 1507 |
} |
| 1508 |
|
| 1509 |
public: |
| 1510 |
void f1() EXCLUSIVE_LOCKS_REQUIRED(mu2) EXCLUSIVE_LOCKS_REQUIRED(mu1) { |
| 1511 |
x = 5; |
| 1512 |
f2(); // expected-warning {{cannot call function 'f2' while mutex 'mu1' is held}} \ |
| 1513 |
// expected-warning {{cannot call function 'f2' while mutex 'mu2' is held}} |
| 1514 |
} |
| 1515 |
}; |
| 1516 |
|
| 1517 |
Foo *foo; |
| 1518 |
|
| 1519 |
void func() |
| 1520 |
{ |
| 1521 |
foo->f1(); // expected-warning {{calling function 'f1' requires holding mutex 'foo->mu2' exclusively}} \ |
| 1522 |
// expected-warning {{calling function 'f1' requires holding mutex 'foo->mu1' exclusively}} |
| 1523 |
} |
| 1524 |
} // end namespace thread_annot_lock_42 |
| 1525 |
|
| 1526 |
namespace thread_annot_lock_46 { |
| 1527 |
// Test the support for annotations on virtual functions. |
| 1528 |
class Base { |
| 1529 |
public: |
| 1530 |
virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 1531 |
virtual void func2() LOCKS_EXCLUDED(mu_); |
| 1532 |
Mutex mu_; |
| 1533 |
}; |
| 1534 |
|
| 1535 |
class Child : public Base { |
| 1536 |
public: |
| 1537 |
virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 1538 |
virtual void func2() LOCKS_EXCLUDED(mu_); |
| 1539 |
}; |
| 1540 |
|
| 1541 |
void main() { |
| 1542 |
Child *c; |
| 1543 |
Base *b = c; |
| 1544 |
|
| 1545 |
b->func1(); // expected-warning {{calling function 'func1' requires holding mutex 'b->mu_' exclusively}} |
| 1546 |
b->mu_.Lock(); |
| 1547 |
b->func2(); // expected-warning {{cannot call function 'func2' while mutex 'b->mu_' is held}} |
| 1548 |
b->mu_.Unlock(); |
| 1549 |
|
| 1550 |
c->func1(); // expected-warning {{calling function 'func1' requires holding mutex 'c->mu_' exclusively}} |
| 1551 |
c->mu_.Lock(); |
| 1552 |
c->func2(); // expected-warning {{cannot call function 'func2' while mutex 'c->mu_' is held}} |
| 1553 |
c->mu_.Unlock(); |
| 1554 |
} |
| 1555 |
} // end namespace thread_annot_lock_46 |
| 1556 |
|
| 1557 |
namespace thread_annot_lock_67_modified { |
| 1558 |
// Modified: attributes on definitions moved to declarations |
| 1559 |
// Test annotations on out-of-line definitions of member functions where the |
| 1560 |
// annotations refer to locks that are also data members in the class. |
| 1561 |
Mutex mu; |
| 1562 |
Mutex mu3; |
| 1563 |
|
| 1564 |
class Foo { |
| 1565 |
public: |
| 1566 |
int method1(int i) SHARED_LOCKS_REQUIRED(mu1, mu, mu2, mu3); |
| 1567 |
int data GUARDED_BY(mu1); |
| 1568 |
Mutex *mu1; |
| 1569 |
Mutex *mu2; |
| 1570 |
}; |
| 1571 |
|
| 1572 |
int Foo::method1(int i) { |
| 1573 |
return data + i; |
| 1574 |
} |
| 1575 |
|
| 1576 |
void main() |
| 1577 |
{ |
| 1578 |
Foo a; |
| 1579 |
a.method1(1); // expected-warning {{calling function 'method1' requires holding mutex 'a.mu1'}} \ |
| 1580 |
// expected-warning {{calling function 'method1' requires holding mutex 'mu'}} \ |
| 1581 |
// expected-warning {{calling function 'method1' requires holding mutex 'a.mu2'}} \ |
| 1582 |
// expected-warning {{calling function 'method1' requires holding mutex 'mu3'}} |
| 1583 |
} |
| 1584 |
} // end namespace thread_annot_lock_67_modified |
| 1585 |
|
| 1586 |
|
| 1587 |
namespace substitution_test { |
| 1588 |
class MyData { |
| 1589 |
public: |
| 1590 |
Mutex mu; |
| 1591 |
|
| 1592 |
void lockData() EXCLUSIVE_LOCK_FUNCTION(mu); |
| 1593 |
void unlockData() UNLOCK_FUNCTION(mu); |
| 1594 |
|
| 1595 |
void doSomething() EXCLUSIVE_LOCKS_REQUIRED(mu) { } |
| 1596 |
}; |
| 1597 |
|
| 1598 |
|
| 1599 |
class DataLocker { |
| 1600 |
public: |
| 1601 |
void lockData (MyData *d) EXCLUSIVE_LOCK_FUNCTION(d->mu); |
| 1602 |
void unlockData(MyData *d) UNLOCK_FUNCTION(d->mu); |
| 1603 |
}; |
| 1604 |
|
| 1605 |
|
| 1606 |
class Foo { |
| 1607 |
public: |
| 1608 |
void foo(MyData* d) EXCLUSIVE_LOCKS_REQUIRED(d->mu) { } |
| 1609 |
|
| 1610 |
void bar1(MyData* d) { |
| 1611 |
d->lockData(); |
| 1612 |
foo(d); |
| 1613 |
d->unlockData(); |
| 1614 |
} |
| 1615 |
|
| 1616 |
void bar2(MyData* d) { |
| 1617 |
DataLocker dlr; |
| 1618 |
dlr.lockData(d); |
| 1619 |
foo(d); |
| 1620 |
dlr.unlockData(d); |
| 1621 |
} |
| 1622 |
|
| 1623 |
void bar3(MyData* d1, MyData* d2) { |
| 1624 |
DataLocker dlr; |
| 1625 |
dlr.lockData(d1); // expected-note {{mutex acquired here}} |
| 1626 |
dlr.unlockData(d2); // \ |
| 1627 |
// expected-warning {{releasing mutex 'd2->mu' that was not held}} |
| 1628 |
} // expected-warning {{mutex 'd1->mu' is still held at the end of function}} |
| 1629 |
|
| 1630 |
void bar4(MyData* d1, MyData* d2) { |
| 1631 |
DataLocker dlr; |
| 1632 |
dlr.lockData(d1); |
| 1633 |
foo(d2); // \ |
| 1634 |
// expected-warning {{calling function 'foo' requires holding mutex 'd2->mu' exclusively}} \ |
| 1635 |
// expected-note {{found near match 'd1->mu'}} |
| 1636 |
dlr.unlockData(d1); |
| 1637 |
} |
| 1638 |
}; |
| 1639 |
|
| 1640 |
// Automatic object destructor calls don't appear as expressions in the CFG, |
| 1641 |
// so we have to handle them separately whenever substitutions are required. |
| 1642 |
struct DestructorRequires { |
| 1643 |
Mutex mu; |
| 1644 |
~DestructorRequires() EXCLUSIVE_LOCKS_REQUIRED(mu); |
| 1645 |
}; |
| 1646 |
|
| 1647 |
void destructorRequires() { |
| 1648 |
DestructorRequires rd; |
| 1649 |
rd.mu.AssertHeld(); |
| 1650 |
} |
| 1651 |
|
| 1652 |
struct DestructorExcludes { |
| 1653 |
Mutex mu; |
| 1654 |
~DestructorExcludes() LOCKS_EXCLUDED(mu); |
| 1655 |
}; |
| 1656 |
|
| 1657 |
void destructorExcludes() { |
| 1658 |
DestructorExcludes ed; |
| 1659 |
ed.mu.Lock(); // expected-note {{mutex acquired here}} |
| 1660 |
} // expected-warning {{cannot call function '~DestructorExcludes' while mutex 'ed.mu' is held}} |
| 1661 |
// expected-warning@-1 {{mutex 'ed.mu' is still held at the end of function}} |
| 1662 |
|
| 1663 |
} // end namespace substituation_test |
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
namespace constructor_destructor_tests { |
| 1668 |
Mutex fooMu; |
| 1669 |
int myVar GUARDED_BY(fooMu); |
| 1670 |
|
| 1671 |
class Foo { |
| 1672 |
public: |
| 1673 |
Foo() EXCLUSIVE_LOCK_FUNCTION(fooMu) { } |
| 1674 |
~Foo() UNLOCK_FUNCTION(fooMu) { } |
| 1675 |
}; |
| 1676 |
|
| 1677 |
void fooTest() { |
| 1678 |
Foo foo; |
| 1679 |
myVar = 0; |
| 1680 |
} |
| 1681 |
} |
| 1682 |
|
| 1683 |
|
| 1684 |
namespace template_member_test { |
| 1685 |
|
| 1686 |
struct S { int n; }; |
| 1687 |
struct T { |
| 1688 |
Mutex m; |
| 1689 |
S *s GUARDED_BY(this->m); |
| 1690 |
}; |
| 1691 |
Mutex m; |
| 1692 |
struct U { |
| 1693 |
union { |
| 1694 |
int n; |
| 1695 |
}; |
| 1696 |
} *u GUARDED_BY(m); |
| 1697 |
|
| 1698 |
template<typename U> |
| 1699 |
struct IndirectLock { |
| 1700 |
int DoNaughtyThings(T *t) { |
| 1701 |
u->n = 0; // expected-warning {{reading variable 'u' requires holding mutex 'm'}} |
| 1702 |
return t->s->n; // expected-warning {{reading variable 's' requires holding mutex 't->m'}} |
| 1703 |
} |
| 1704 |
}; |
| 1705 |
|
| 1706 |
template struct IndirectLock<int>; // expected-note {{here}} |
| 1707 |
|
| 1708 |
struct V { |
| 1709 |
void f(int); |
| 1710 |
void f(double); |
| 1711 |
|
| 1712 |
Mutex m; |
| 1713 |
V *p GUARDED_BY(this->m); |
| 1714 |
}; |
| 1715 |
template<typename U> struct W { |
| 1716 |
V v; |
| 1717 |
void f(U u) { |
| 1718 |
v.p->f(u); // expected-warning {{reading variable 'p' requires holding mutex 'v.m'}} |
| 1719 |
} |
| 1720 |
}; |
| 1721 |
template struct W<int>; // expected-note {{here}} |
| 1722 |
|
| 1723 |
} |
| 1724 |
|
| 1725 |
namespace test_scoped_lockable { |
| 1726 |
|
| 1727 |
struct TestScopedLockable { |
| 1728 |
Mutex mu1; |
| 1729 |
Mutex mu2; |
| 1730 |
int a __attribute__((guarded_by(mu1))); |
| 1731 |
int b __attribute__((guarded_by(mu2))); |
| 1732 |
|
| 1733 |
bool getBool(); |
| 1734 |
|
| 1735 |
bool lock2Bool(MutexLock); |
| 1736 |
|
| 1737 |
void foo1() { |
| 1738 |
MutexLock mulock(&mu1); |
| 1739 |
a = 5; |
| 1740 |
} |
| 1741 |
|
| 1742 |
#ifdef __cpp_guaranteed_copy_elision |
| 1743 |
void const_lock() { |
| 1744 |
const MutexLock mulock = MutexLock(&mu1); |
| 1745 |
a = 5; |
| 1746 |
} |
| 1747 |
#endif |
| 1748 |
|
| 1749 |
void temporary() { |
| 1750 |
MutexLock{&mu1}, a = 5; |
| 1751 |
} |
| 1752 |
|
| 1753 |
void temporary_cfg(int x) { |
| 1754 |
// test the case where a pair of temporary Ctor and Dtor is in different CFG blocks |
| 1755 |
lock2Bool(MutexLock{&mu1}) || x; |
| 1756 |
MutexLock{&mu1}; // no-warn |
| 1757 |
} |
| 1758 |
|
| 1759 |
void lifetime_extension() { |
| 1760 |
const MutexLock &mulock = MutexLock(&mu1); |
| 1761 |
a = 5; |
| 1762 |
} |
| 1763 |
|
| 1764 |
void foo2() { |
| 1765 |
ReaderMutexLock mulock1(&mu1); |
| 1766 |
if (getBool()) { |
| 1767 |
MutexLock mulock2a(&mu2); |
| 1768 |
b = a + 1; |
| 1769 |
} |
| 1770 |
else { |
| 1771 |
MutexLock mulock2b(&mu2); |
| 1772 |
b = a + 2; |
| 1773 |
} |
| 1774 |
} |
| 1775 |
|
| 1776 |
void foo3() { |
| 1777 |
MutexLock mulock_a(&mu1); // expected-note{{mutex acquired here}} |
| 1778 |
MutexLock mulock_b(&mu1); // \ |
| 1779 |
// expected-warning {{acquiring mutex 'mu1' that is already held}} |
| 1780 |
} |
| 1781 |
|
| 1782 |
void temporary_double_lock() { |
| 1783 |
MutexLock mulock_a(&mu1); // expected-note{{mutex acquired here}} |
| 1784 |
MutexLock{&mu1}; // \ |
| 1785 |
// expected-warning {{acquiring mutex 'mu1' that is already held}} |
| 1786 |
} |
| 1787 |
|
| 1788 |
void foo4() { |
| 1789 |
MutexLock mulock1(&mu1), mulock2(&mu2); |
| 1790 |
a = b+1; |
| 1791 |
b = a+1; |
| 1792 |
} |
| 1793 |
|
| 1794 |
void foo5() { |
| 1795 |
DoubleMutexLock mulock(&mu1, &mu2); |
| 1796 |
a = b + 1; |
| 1797 |
b = a + 1; |
| 1798 |
} |
| 1799 |
}; |
| 1800 |
|
| 1801 |
} // end namespace test_scoped_lockable |
| 1802 |
|
| 1803 |
|
| 1804 |
namespace FunctionAttrTest { |
| 1805 |
|
| 1806 |
class Foo { |
| 1807 |
public: |
| 1808 |
Mutex mu_; |
| 1809 |
int a GUARDED_BY(mu_); |
| 1810 |
}; |
| 1811 |
|
| 1812 |
Foo fooObj; |
| 1813 |
|
| 1814 |
void foo() EXCLUSIVE_LOCKS_REQUIRED(fooObj.mu_); |
| 1815 |
|
| 1816 |
void bar() { |
| 1817 |
foo(); // expected-warning {{calling function 'foo' requires holding mutex 'fooObj.mu_' exclusively}} |
| 1818 |
fooObj.mu_.Lock(); |
| 1819 |
foo(); |
| 1820 |
fooObj.mu_.Unlock(); |
| 1821 |
} |
| 1822 |
|
| 1823 |
}; // end namespace FunctionAttrTest |
| 1824 |
|
| 1825 |
|
| 1826 |
namespace TryLockTest { |
| 1827 |
|
| 1828 |
struct TestTryLock { |
| 1829 |
Mutex mu; |
| 1830 |
int a GUARDED_BY(mu); |
| 1831 |
bool cond; |
| 1832 |
|
| 1833 |
void foo1() { |
| 1834 |
if (mu.TryLock()) { |
| 1835 |
a = 1; |
| 1836 |
mu.Unlock(); |
| 1837 |
} |
| 1838 |
} |
| 1839 |
|
| 1840 |
void foo2() { |
| 1841 |
if (!mu.TryLock()) return; |
| 1842 |
a = 2; |
| 1843 |
mu.Unlock(); |
| 1844 |
} |
| 1845 |
|
| 1846 |
void foo2_builtin_expect() { |
| 1847 |
if (__builtin_expect(!mu.TryLock(), false)) |
| 1848 |
return; |
| 1849 |
a = 2; |
| 1850 |
mu.Unlock(); |
| 1851 |
} |
| 1852 |
|
| 1853 |
void foo3() { |
| 1854 |
bool b = mu.TryLock(); |
| 1855 |
if (b) { |
| 1856 |
a = 3; |
| 1857 |
mu.Unlock(); |
| 1858 |
} |
| 1859 |
} |
| 1860 |
|
| 1861 |
void foo3_builtin_expect() { |
| 1862 |
bool b = mu.TryLock(); |
| 1863 |
if (__builtin_expect(b, true)) { |
| 1864 |
a = 3; |
| 1865 |
mu.Unlock(); |
| 1866 |
} |
| 1867 |
} |
| 1868 |
|
| 1869 |
void foo4() { |
| 1870 |
bool b = mu.TryLock(); |
| 1871 |
if (!b) return; |
| 1872 |
a = 4; |
| 1873 |
mu.Unlock(); |
| 1874 |
} |
| 1875 |
|
| 1876 |
void foo5() { |
| 1877 |
while (mu.TryLock()) { |
| 1878 |
a = a + 1; |
| 1879 |
mu.Unlock(); |
| 1880 |
} |
| 1881 |
} |
| 1882 |
|
| 1883 |
void foo6() { |
| 1884 |
bool b = mu.TryLock(); |
| 1885 |
b = !b; |
| 1886 |
if (b) return; |
| 1887 |
a = 6; |
| 1888 |
mu.Unlock(); |
| 1889 |
} |
| 1890 |
|
| 1891 |
void foo7() { |
| 1892 |
bool b1 = mu.TryLock(); |
| 1893 |
bool b2 = !b1; |
| 1894 |
bool b3 = !b2; |
| 1895 |
if (b3) { |
| 1896 |
a = 7; |
| 1897 |
mu.Unlock(); |
| 1898 |
} |
| 1899 |
} |
| 1900 |
|
| 1901 |
// Test use-def chains: join points |
| 1902 |
void foo8() { |
| 1903 |
bool b = mu.TryLock(); |
| 1904 |
bool b2 = b; |
| 1905 |
if (cond) |
| 1906 |
b = true; |
| 1907 |
if (b) { // b should be unknown at this point, because of the join point |
| 1908 |
a = 8; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} |
| 1909 |
} |
| 1910 |
if (b2) { // b2 should be known at this point. |
| 1911 |
a = 8; |
| 1912 |
mu.Unlock(); |
| 1913 |
} |
| 1914 |
} |
| 1915 |
|
| 1916 |
// Test use-def-chains: back edges |
| 1917 |
void foo9() { |
| 1918 |
bool b = mu.TryLock(); |
| 1919 |
|
| 1920 |
for (int i = 0; i < 10; ++i); |
| 1921 |
|
| 1922 |
if (b) { // b is still known, because the loop doesn't alter it |
| 1923 |
a = 9; |
| 1924 |
mu.Unlock(); |
| 1925 |
} |
| 1926 |
} |
| 1927 |
|
| 1928 |
// Test use-def chains: back edges |
| 1929 |
void foo10() { |
| 1930 |
bool b = mu.TryLock(); |
| 1931 |
|
| 1932 |
while (cond) { |
| 1933 |
if (b) { // b should be unknown at this point b/c of the loop |
| 1934 |
a = 10; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} |
| 1935 |
} |
| 1936 |
b = !b; |
| 1937 |
} |
| 1938 |
} |
| 1939 |
|
| 1940 |
// Test merge of exclusive trylock |
| 1941 |
void foo11() { |
| 1942 |
if (cond) { |
| 1943 |
if (!mu.TryLock()) |
| 1944 |
return; |
| 1945 |
} |
| 1946 |
else { |
| 1947 |
mu.Lock(); |
| 1948 |
} |
| 1949 |
a = 10; |
| 1950 |
mu.Unlock(); |
| 1951 |
} |
| 1952 |
|
| 1953 |
// Test merge of shared trylock |
| 1954 |
void foo12() { |
| 1955 |
if (cond) { |
| 1956 |
if (!mu.ReaderTryLock()) |
| 1957 |
return; |
| 1958 |
} |
| 1959 |
else { |
| 1960 |
mu.ReaderLock(); |
| 1961 |
} |
| 1962 |
int i = a; |
| 1963 |
mu.Unlock(); |
| 1964 |
} |
| 1965 |
|
| 1966 |
// Test with conditional operator |
| 1967 |
void foo13() { |
| 1968 |
if (mu.TryLock() ? 1 : 0) |
| 1969 |
mu.Unlock(); |
| 1970 |
} |
| 1971 |
|
| 1972 |
void foo14() { |
| 1973 |
if (mu.TryLock() ? 0 : 1) |
| 1974 |
return; |
| 1975 |
mu.Unlock(); |
| 1976 |
} |
| 1977 |
|
| 1978 |
void foo15() { |
| 1979 |
if (mu.TryLock() ? 0 : 1) // expected-note{{mutex acquired here}} |
| 1980 |
mu.Unlock(); // expected-warning{{releasing mutex 'mu' that was not held}} |
| 1981 |
} // expected-warning{{mutex 'mu' is not held on every path through here}} |
| 1982 |
}; // end TestTrylock |
| 1983 |
|
| 1984 |
} // end namespace TrylockTest |
| 1985 |
|
| 1986 |
|
| 1987 |
namespace TestTemplateAttributeInstantiation { |
| 1988 |
|
| 1989 |
class Foo1 { |
| 1990 |
public: |
| 1991 |
Mutex mu_; |
| 1992 |
int a GUARDED_BY(mu_); |
| 1993 |
}; |
| 1994 |
|
| 1995 |
class Foo2 { |
| 1996 |
public: |
| 1997 |
int a GUARDED_BY(mu_); |
| 1998 |
Mutex mu_; |
| 1999 |
}; |
| 2000 |
|
| 2001 |
|
| 2002 |
class Bar { |
| 2003 |
public: |
| 2004 |
// Test non-dependent expressions in attributes on template functions |
| 2005 |
template <class T> |
| 2006 |
void barND(Foo1 *foo, T *fooT) EXCLUSIVE_LOCKS_REQUIRED(foo->mu_) { |
| 2007 |
foo->a = 0; |
| 2008 |
} |
| 2009 |
|
| 2010 |
// Test dependent expressions in attributes on template functions |
| 2011 |
template <class T> |
| 2012 |
void barD(Foo1 *foo, T *fooT) EXCLUSIVE_LOCKS_REQUIRED(fooT->mu_) { |
| 2013 |
fooT->a = 0; |
| 2014 |
} |
| 2015 |
}; |
| 2016 |
|
| 2017 |
|
| 2018 |
template <class T> |
| 2019 |
class BarT { |
| 2020 |
public: |
| 2021 |
Foo1 fooBase; |
| 2022 |
T fooBaseT; |
| 2023 |
|
| 2024 |
// Test non-dependent expression in ordinary method on template class |
| 2025 |
void barND() EXCLUSIVE_LOCKS_REQUIRED(fooBase.mu_) { |
| 2026 |
fooBase.a = 0; |
| 2027 |
} |
| 2028 |
|
| 2029 |
// Test dependent expressions in ordinary methods on template class |
| 2030 |
void barD() EXCLUSIVE_LOCKS_REQUIRED(fooBaseT.mu_) { |
| 2031 |
fooBaseT.a = 0; |
| 2032 |
} |
| 2033 |
|
| 2034 |
// Test dependent expressions in template method in template class |
| 2035 |
template <class T2> |
| 2036 |
void barTD(T2 *fooT) EXCLUSIVE_LOCKS_REQUIRED(fooBaseT.mu_, fooT->mu_) { |
| 2037 |
fooBaseT.a = 0; |
| 2038 |
fooT->a = 0; |
| 2039 |
} |
| 2040 |
}; |
| 2041 |
|
| 2042 |
template <class T> |
| 2043 |
class Cell { |
| 2044 |
public: |
| 2045 |
Mutex mu_; |
| 2046 |
// Test dependent guarded_by |
| 2047 |
T data GUARDED_BY(mu_); |
| 2048 |
|
| 2049 |
void fooEx() EXCLUSIVE_LOCKS_REQUIRED(mu_) { |
| 2050 |
data = 0; |
| 2051 |
} |
| 2052 |
|
| 2053 |
void foo() { |
| 2054 |
mu_.Lock(); |
| 2055 |
data = 0; |
| 2056 |
mu_.Unlock(); |
| 2057 |
} |
| 2058 |
}; |
| 2059 |
|
| 2060 |
void test() { |
| 2061 |
Bar b; |
| 2062 |
BarT<Foo2> bt; |
| 2063 |
Foo1 f1; |
| 2064 |
Foo2 f2; |
| 2065 |
|
| 2066 |
f1.mu_.Lock(); |
| 2067 |
f2.mu_.Lock(); |
| 2068 |
bt.fooBase.mu_.Lock(); |
| 2069 |
bt.fooBaseT.mu_.Lock(); |
| 2070 |
|
| 2071 |
b.barND(&f1, &f2); |
| 2072 |
b.barD(&f1, &f2); |
| 2073 |
bt.barND(); |
| 2074 |
bt.barD(); |
| 2075 |
bt.barTD(&f2); |
| 2076 |
|
| 2077 |
f1.mu_.Unlock(); |
| 2078 |
bt.barTD(&f1); // \ |
| 2079 |
// expected-warning {{calling function 'barTD<TestTemplateAttributeInstantiation::Foo1>' requires holding mutex 'f1.mu_' exclusively}} \ |
| 2080 |
// expected-note {{found near match 'bt.fooBase.mu_'}} |
| 2081 |
|
| 2082 |
bt.fooBase.mu_.Unlock(); |
| 2083 |
bt.fooBaseT.mu_.Unlock(); |
| 2084 |
f2.mu_.Unlock(); |
| 2085 |
|
| 2086 |
Cell<int> cell; |
| 2087 |
cell.data = 0; // \ |
| 2088 |
// expected-warning {{writing variable 'data' requires holding mutex 'cell.mu_' exclusively}} |
| 2089 |
cell.foo(); |
| 2090 |
cell.mu_.Lock(); |
| 2091 |
cell.fooEx(); |
| 2092 |
cell.mu_.Unlock(); |
| 2093 |
} |
| 2094 |
|
| 2095 |
|
| 2096 |
template <class T> |
| 2097 |
class CellDelayed { |
| 2098 |
public: |
| 2099 |
// Test dependent guarded_by |
| 2100 |
T data GUARDED_BY(mu_); |
| 2101 |
static T static_data GUARDED_BY(static_mu_); |
| 2102 |
|
| 2103 |
void fooEx(CellDelayed<T> *other) EXCLUSIVE_LOCKS_REQUIRED(mu_, other->mu_) { |
| 2104 |
this->data = other->data; |
| 2105 |
} |
| 2106 |
|
| 2107 |
template <class T2> |
| 2108 |
void fooExT(CellDelayed<T2> *otherT) EXCLUSIVE_LOCKS_REQUIRED(mu_, otherT->mu_) { |
| 2109 |
this->data = otherT->data; |
| 2110 |
} |
| 2111 |
|
| 2112 |
void foo() { |
| 2113 |
mu_.Lock(); |
| 2114 |
data = 0; |
| 2115 |
mu_.Unlock(); |
| 2116 |
} |
| 2117 |
|
| 2118 |
Mutex mu_; |
| 2119 |
static Mutex static_mu_; |
| 2120 |
}; |
| 2121 |
|
| 2122 |
void testDelayed() { |
| 2123 |
CellDelayed<int> celld; |
| 2124 |
CellDelayed<int> celld2; |
| 2125 |
celld.foo(); |
| 2126 |
celld.mu_.Lock(); |
| 2127 |
celld2.mu_.Lock(); |
| 2128 |
|
| 2129 |
celld.fooEx(&celld2); |
| 2130 |
celld.fooExT(&celld2); |
| 2131 |
|
| 2132 |
celld2.mu_.Unlock(); |
| 2133 |
celld.mu_.Unlock(); |
| 2134 |
} |
| 2135 |
|
| 2136 |
}; // end namespace TestTemplateAttributeInstantiation |
| 2137 |
|
| 2138 |
|
| 2139 |
namespace FunctionDeclDefTest { |
| 2140 |
|
| 2141 |
class Foo { |
| 2142 |
public: |
| 2143 |
Mutex mu_; |
| 2144 |
int a GUARDED_BY(mu_); |
| 2145 |
|
| 2146 |
virtual void foo1(Foo *f_declared) EXCLUSIVE_LOCKS_REQUIRED(f_declared->mu_); |
| 2147 |
}; |
| 2148 |
|
| 2149 |
// EXCLUSIVE_LOCKS_REQUIRED should be applied, and rewritten to f_defined->mu_ |
| 2150 |
void Foo::foo1(Foo *f_defined) { |
| 2151 |
f_defined->a = 0; |
| 2152 |
}; |
| 2153 |
|
| 2154 |
void test() { |
| 2155 |
Foo myfoo; |
| 2156 |
myfoo.foo1(&myfoo); // \ |
| 2157 |
// expected-warning {{calling function 'foo1' requires holding mutex 'myfoo.mu_' exclusively}} |
| 2158 |
myfoo.mu_.Lock(); |
| 2159 |
myfoo.foo1(&myfoo); |
| 2160 |
myfoo.mu_.Unlock(); |
| 2161 |
} |
| 2162 |
|
| 2163 |
}; |
| 2164 |
|
| 2165 |
namespace GoingNative { |
| 2166 |
|
| 2167 |
struct LOCKABLE mutex { |
| 2168 |
void lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 2169 |
void unlock() UNLOCK_FUNCTION(); |
| 2170 |
// ... |
| 2171 |
}; |
| 2172 |
bool foo(); |
| 2173 |
bool bar(); |
| 2174 |
mutex m; |
| 2175 |
void test() { |
| 2176 |
m.lock(); |
| 2177 |
while (foo()) { // expected-warning {{expecting mutex 'm' to be held at start of each loop}} |
| 2178 |
m.unlock(); |
| 2179 |
// ... |
| 2180 |
if (bar()) { |
| 2181 |
// ... |
| 2182 |
if (foo()) |
| 2183 |
continue; |
| 2184 |
//... |
| 2185 |
} |
| 2186 |
// ... |
| 2187 |
m.lock(); // expected-note {{mutex acquired here}} |
| 2188 |
} |
| 2189 |
m.unlock(); |
| 2190 |
} |
| 2191 |
|
| 2192 |
} |
| 2193 |
|
| 2194 |
|
| 2195 |
|
| 2196 |
namespace FunctionDefinitionTest { |
| 2197 |
|
| 2198 |
class Foo { |
| 2199 |
public: |
| 2200 |
void foo1(); |
| 2201 |
void foo2(); |
| 2202 |
void foo3(Foo *other); |
| 2203 |
|
| 2204 |
template<class T> |
| 2205 |
void fooT1(const T& dummy1); |
| 2206 |
|
| 2207 |
template<class T> |
| 2208 |
void fooT2(const T& dummy2) EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 2209 |
|
| 2210 |
Mutex mu_; |
| 2211 |
int a GUARDED_BY(mu_); |
| 2212 |
}; |
| 2213 |
|
| 2214 |
template<class T> |
| 2215 |
class FooT { |
| 2216 |
public: |
| 2217 |
void foo(); |
| 2218 |
|
| 2219 |
Mutex mu_; |
| 2220 |
T a GUARDED_BY(mu_); |
| 2221 |
}; |
| 2222 |
|
| 2223 |
|
| 2224 |
void Foo::foo1() NO_THREAD_SAFETY_ANALYSIS { |
| 2225 |
a = 1; |
| 2226 |
} |
| 2227 |
|
| 2228 |
void Foo::foo2() EXCLUSIVE_LOCKS_REQUIRED(mu_) { |
| 2229 |
a = 2; |
| 2230 |
} |
| 2231 |
|
| 2232 |
void Foo::foo3(Foo *other) EXCLUSIVE_LOCKS_REQUIRED(other->mu_) { |
| 2233 |
other->a = 3; |
| 2234 |
} |
| 2235 |
|
| 2236 |
template<class T> |
| 2237 |
void Foo::fooT1(const T& dummy1) EXCLUSIVE_LOCKS_REQUIRED(mu_) { |
| 2238 |
a = dummy1; |
| 2239 |
} |
| 2240 |
|
| 2241 |
/* TODO -- uncomment with template instantiation of attributes. |
| 2242 |
template<class T> |
| 2243 |
void Foo::fooT2(const T& dummy2) { |
| 2244 |
a = dummy2; |
| 2245 |
} |
| 2246 |
*/ |
| 2247 |
|
| 2248 |
void fooF1(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) { |
| 2249 |
f->a = 1; |
| 2250 |
} |
| 2251 |
|
| 2252 |
void fooF2(Foo *f); |
| 2253 |
void fooF2(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) { |
| 2254 |
f->a = 2; |
| 2255 |
} |
| 2256 |
|
| 2257 |
void fooF3(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_); |
| 2258 |
void fooF3(Foo *f) { |
| 2259 |
f->a = 3; |
| 2260 |
} |
| 2261 |
|
| 2262 |
template<class T> |
| 2263 |
void FooT<T>::foo() EXCLUSIVE_LOCKS_REQUIRED(mu_) { |
| 2264 |
a = 0; |
| 2265 |
} |
| 2266 |
|
| 2267 |
void test() { |
| 2268 |
int dummy = 0; |
| 2269 |
Foo myFoo; |
| 2270 |
|
| 2271 |
myFoo.foo2(); // \ |
| 2272 |
// expected-warning {{calling function 'foo2' requires holding mutex 'myFoo.mu_' exclusively}} |
| 2273 |
myFoo.foo3(&myFoo); // \ |
| 2274 |
// expected-warning {{calling function 'foo3' requires holding mutex 'myFoo.mu_' exclusively}} |
| 2275 |
myFoo.fooT1(dummy); // \ |
| 2276 |
// expected-warning {{calling function 'fooT1<int>' requires holding mutex 'myFoo.mu_' exclusively}} |
| 2277 |
|
| 2278 |
myFoo.fooT2(dummy); // \ |
| 2279 |
// expected-warning {{calling function 'fooT2<int>' requires holding mutex 'myFoo.mu_' exclusively}} |
| 2280 |
|
| 2281 |
fooF1(&myFoo); // \ |
| 2282 |
// expected-warning {{calling function 'fooF1' requires holding mutex 'myFoo.mu_' exclusively}} |
| 2283 |
fooF2(&myFoo); // \ |
| 2284 |
// expected-warning {{calling function 'fooF2' requires holding mutex 'myFoo.mu_' exclusively}} |
| 2285 |
fooF3(&myFoo); // \ |
| 2286 |
// expected-warning {{calling function 'fooF3' requires holding mutex 'myFoo.mu_' exclusively}} |
| 2287 |
|
| 2288 |
myFoo.mu_.Lock(); |
| 2289 |
myFoo.foo2(); |
| 2290 |
myFoo.foo3(&myFoo); |
| 2291 |
myFoo.fooT1(dummy); |
| 2292 |
|
| 2293 |
myFoo.fooT2(dummy); |
| 2294 |
|
| 2295 |
fooF1(&myFoo); |
| 2296 |
fooF2(&myFoo); |
| 2297 |
fooF3(&myFoo); |
| 2298 |
myFoo.mu_.Unlock(); |
| 2299 |
|
| 2300 |
FooT<int> myFooT; |
| 2301 |
myFooT.foo(); // \ |
| 2302 |
// expected-warning {{calling function 'foo' requires holding mutex 'myFooT.mu_' exclusively}} |
| 2303 |
} |
| 2304 |
|
| 2305 |
} // end namespace FunctionDefinitionTest |
| 2306 |
|
| 2307 |
|
| 2308 |
namespace SelfLockingTest { |
| 2309 |
|
| 2310 |
class LOCKABLE MyLock { |
| 2311 |
public: |
| 2312 |
int foo GUARDED_BY(this); |
| 2313 |
|
| 2314 |
void lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 2315 |
void unlock() UNLOCK_FUNCTION(); |
| 2316 |
|
| 2317 |
void doSomething() { |
| 2318 |
this->lock(); // allow 'this' as a lock expression |
| 2319 |
foo = 0; |
| 2320 |
doSomethingElse(); |
| 2321 |
this->unlock(); |
| 2322 |
} |
| 2323 |
|
| 2324 |
void doSomethingElse() EXCLUSIVE_LOCKS_REQUIRED(this) { |
| 2325 |
foo = 1; |
| 2326 |
}; |
| 2327 |
|
| 2328 |
void test() { |
| 2329 |
foo = 2; // \ |
| 2330 |
// expected-warning {{writing variable 'foo' requires holding mutex 'this' exclusively}} |
| 2331 |
} |
| 2332 |
}; |
| 2333 |
|
| 2334 |
|
| 2335 |
class LOCKABLE MyLock2 { |
| 2336 |
public: |
| 2337 |
Mutex mu_; |
| 2338 |
int foo GUARDED_BY(this); |
| 2339 |
|
| 2340 |
// don't check inside lock and unlock functions |
| 2341 |
void lock() EXCLUSIVE_LOCK_FUNCTION() { mu_.Lock(); } |
| 2342 |
void unlock() UNLOCK_FUNCTION() { mu_.Unlock(); } |
| 2343 |
|
| 2344 |
// don't check inside constructors and destructors |
| 2345 |
MyLock2() { foo = 1; } |
| 2346 |
~MyLock2() { foo = 0; } |
| 2347 |
}; |
| 2348 |
|
| 2349 |
|
| 2350 |
} // end namespace SelfLockingTest |
| 2351 |
|
| 2352 |
|
| 2353 |
namespace InvalidNonstatic { |
| 2354 |
|
| 2355 |
// Forward decl here causes bogus "invalid use of non-static data member" |
| 2356 |
// on reference to mutex_ in guarded_by attribute. |
| 2357 |
class Foo; |
| 2358 |
|
| 2359 |
class Foo { |
| 2360 |
Mutex* mutex_; |
| 2361 |
|
| 2362 |
int foo __attribute__((guarded_by(mutex_))); |
| 2363 |
}; |
| 2364 |
|
| 2365 |
} // end namespace InvalidNonStatic |
| 2366 |
|
| 2367 |
|
| 2368 |
namespace NoReturnTest { |
| 2369 |
|
| 2370 |
bool condition(); |
| 2371 |
void fatal() __attribute__((noreturn)); |
| 2372 |
|
| 2373 |
Mutex mu_; |
| 2374 |
|
| 2375 |
void test1() { |
| 2376 |
MutexLock lock(&mu_); |
| 2377 |
if (condition()) { |
| 2378 |
fatal(); |
| 2379 |
return; |
| 2380 |
} |
| 2381 |
} |
| 2382 |
|
| 2383 |
} // end namespace NoReturnTest |
| 2384 |
|
| 2385 |
|
| 2386 |
namespace TestMultiDecl { |
| 2387 |
|
| 2388 |
class Foo { |
| 2389 |
public: |
| 2390 |
int GUARDED_BY(mu_) a; |
| 2391 |
int GUARDED_BY(mu_) b, c; |
| 2392 |
|
| 2393 |
void foo() { |
| 2394 |
a = 0; // \ |
| 2395 |
// expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} |
| 2396 |
b = 0; // \ |
| 2397 |
// expected-warning {{writing variable 'b' requires holding mutex 'mu_' exclusively}} |
| 2398 |
c = 0; // \ |
| 2399 |
// expected-warning {{writing variable 'c' requires holding mutex 'mu_' exclusively}} |
| 2400 |
} |
| 2401 |
|
| 2402 |
private: |
| 2403 |
Mutex mu_; |
| 2404 |
}; |
| 2405 |
|
| 2406 |
} // end namespace TestMultiDecl |
| 2407 |
|
| 2408 |
|
| 2409 |
namespace WarnNoDecl { |
| 2410 |
|
| 2411 |
class Foo { |
| 2412 |
void foo(int a); __attribute__(( // \ |
| 2413 |
// expected-warning {{declaration does not declare anything}} |
| 2414 |
exclusive_locks_required(a))); // \ |
| 2415 |
// expected-warning {{attribute exclusive_locks_required ignored}} |
| 2416 |
}; |
| 2417 |
|
| 2418 |
} // end namespace WarnNoDecl |
| 2419 |
|
| 2420 |
|
| 2421 |
|
| 2422 |
namespace MoreLockExpressions { |
| 2423 |
|
| 2424 |
class Foo { |
| 2425 |
public: |
| 2426 |
Mutex mu_; |
| 2427 |
int a GUARDED_BY(mu_); |
| 2428 |
}; |
| 2429 |
|
| 2430 |
class Bar { |
| 2431 |
public: |
| 2432 |
int b; |
| 2433 |
Foo* f; |
| 2434 |
|
| 2435 |
Foo& getFoo() { return *f; } |
| 2436 |
Foo& getFoo2(int c) { return *f; } |
| 2437 |
Foo& getFoo3(int c, int d) { return *f; } |
| 2438 |
|
| 2439 |
Foo& getFooey() { return *f; } |
| 2440 |
}; |
| 2441 |
|
| 2442 |
Foo& getBarFoo(Bar &bar, int c) { return bar.getFoo2(c); } |
| 2443 |
|
| 2444 |
void test() { |
| 2445 |
Foo foo; |
| 2446 |
Foo *fooArray; |
| 2447 |
Foo &(*fooFuncPtr)(); |
| 2448 |
Bar bar; |
| 2449 |
int a; |
| 2450 |
int b; |
| 2451 |
int c; |
| 2452 |
|
| 2453 |
bar.getFoo().mu_.Lock(); |
| 2454 |
bar.getFoo().a = 0; |
| 2455 |
bar.getFoo().mu_.Unlock(); |
| 2456 |
|
| 2457 |
(bar.getFoo().mu_).Lock(); // test parenthesis |
| 2458 |
bar.getFoo().a = 0; |
| 2459 |
(bar.getFoo().mu_).Unlock(); |
| 2460 |
|
| 2461 |
bar.getFoo2(a).mu_.Lock(); |
| 2462 |
bar.getFoo2(a).a = 0; |
| 2463 |
bar.getFoo2(a).mu_.Unlock(); |
| 2464 |
|
| 2465 |
bar.getFoo3(a, b).mu_.Lock(); |
| 2466 |
bar.getFoo3(a, b).a = 0; |
| 2467 |
bar.getFoo3(a, b).mu_.Unlock(); |
| 2468 |
|
| 2469 |
getBarFoo(bar, a).mu_.Lock(); |
| 2470 |
getBarFoo(bar, a).a = 0; |
| 2471 |
getBarFoo(bar, a).mu_.Unlock(); |
| 2472 |
|
| 2473 |
bar.getFoo2(10).mu_.Lock(); |
| 2474 |
bar.getFoo2(10).a = 0; |
| 2475 |
bar.getFoo2(10).mu_.Unlock(); |
| 2476 |
|
| 2477 |
bar.getFoo2(a + 1).mu_.Lock(); |
| 2478 |
bar.getFoo2(a + 1).a = 0; |
| 2479 |
bar.getFoo2(a + 1).mu_.Unlock(); |
| 2480 |
|
| 2481 |
(a > 0 ? fooArray[1] : fooArray[b]).mu_.Lock(); |
| 2482 |
(a > 0 ? fooArray[1] : fooArray[b]).a = 0; |
| 2483 |
(a > 0 ? fooArray[1] : fooArray[b]).mu_.Unlock(); |
| 2484 |
|
| 2485 |
fooFuncPtr().mu_.Lock(); |
| 2486 |
fooFuncPtr().a = 0; |
| 2487 |
fooFuncPtr().mu_.Unlock(); |
| 2488 |
} |
| 2489 |
|
| 2490 |
|
| 2491 |
void test2() { |
| 2492 |
Foo *fooArray; |
| 2493 |
Bar bar; |
| 2494 |
int a; |
| 2495 |
int b; |
| 2496 |
int c; |
| 2497 |
|
| 2498 |
bar.getFoo().mu_.Lock(); |
| 2499 |
bar.getFooey().a = 0; // \ |
| 2500 |
// expected-warning {{writing variable 'a' requires holding mutex 'bar.getFooey().mu_' exclusively}} \ |
| 2501 |
// expected-note {{found near match 'bar.getFoo().mu_'}} |
| 2502 |
bar.getFoo().mu_.Unlock(); |
| 2503 |
|
| 2504 |
bar.getFoo2(a).mu_.Lock(); |
| 2505 |
bar.getFoo2(b).a = 0; // \ |
| 2506 |
// expected-warning {{writing variable 'a' requires holding mutex 'bar.getFoo2(b).mu_' exclusively}} \ |
| 2507 |
// expected-note {{found near match 'bar.getFoo2(a).mu_'}} |
| 2508 |
bar.getFoo2(a).mu_.Unlock(); |
| 2509 |
|
| 2510 |
bar.getFoo3(a, b).mu_.Lock(); |
| 2511 |
bar.getFoo3(a, c).a = 0; // \ |
| 2512 |
// expected-warning {{writing variable 'a' requires holding mutex 'bar.getFoo3(a, c).mu_' exclusively}} \ |
| 2513 |
// expected-note {{found near match 'bar.getFoo3(a, b).mu_'}} |
| 2514 |
bar.getFoo3(a, b).mu_.Unlock(); |
| 2515 |
|
| 2516 |
getBarFoo(bar, a).mu_.Lock(); |
| 2517 |
getBarFoo(bar, b).a = 0; // \ |
| 2518 |
// expected-warning {{writing variable 'a' requires holding mutex 'getBarFoo(bar, b).mu_' exclusively}} \ |
| 2519 |
// expected-note {{found near match 'getBarFoo(bar, a).mu_'}} |
| 2520 |
getBarFoo(bar, a).mu_.Unlock(); |
| 2521 |
|
| 2522 |
(a > 0 ? fooArray[1] : fooArray[b]).mu_.Lock(); |
| 2523 |
(a > 0 ? fooArray[b] : fooArray[c]).a = 0; // \ |
| 2524 |
// expected-warning {{writing variable 'a' requires holding mutex '((0 < a) ? fooArray[b] : fooArray[c]).mu_' exclusively}} \ |
| 2525 |
// expected-note {{found near match '((0 < a) ? fooArray[1] : fooArray[b]).mu_'}} |
| 2526 |
(a > 0 ? fooArray[1] : fooArray[b]).mu_.Unlock(); |
| 2527 |
} |
| 2528 |
|
| 2529 |
|
| 2530 |
} // end namespace MoreLockExpressions |
| 2531 |
|
| 2532 |
|
| 2533 |
namespace TrylockJoinPoint { |
| 2534 |
|
| 2535 |
class Foo { |
| 2536 |
Mutex mu; |
| 2537 |
bool c; |
| 2538 |
|
| 2539 |
void foo() { |
| 2540 |
if (c) { |
| 2541 |
if (!mu.TryLock()) |
| 2542 |
return; |
| 2543 |
} else { |
| 2544 |
mu.Lock(); |
| 2545 |
} |
| 2546 |
mu.Unlock(); |
| 2547 |
} |
| 2548 |
}; |
| 2549 |
|
| 2550 |
} // end namespace TrylockJoinPoint |
| 2551 |
|
| 2552 |
|
| 2553 |
namespace LockReturned { |
| 2554 |
|
| 2555 |
class Foo { |
| 2556 |
public: |
| 2557 |
int a GUARDED_BY(mu_); |
| 2558 |
void foo() EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 2559 |
void foo2(Foo* f) EXCLUSIVE_LOCKS_REQUIRED(mu_, f->mu_); |
| 2560 |
|
| 2561 |
static void sfoo(Foo* f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_); |
| 2562 |
|
| 2563 |
Mutex* getMu() LOCK_RETURNED(mu_); |
| 2564 |
|
| 2565 |
Mutex mu_; |
| 2566 |
|
| 2567 |
static Mutex* getMu(Foo* f) LOCK_RETURNED(f->mu_); |
| 2568 |
}; |
| 2569 |
|
| 2570 |
|
| 2571 |
// Calls getMu() directly to lock and unlock |
| 2572 |
void test1(Foo* f1, Foo* f2) { |
| 2573 |
f1->a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'f1->mu_' exclusively}} |
| 2574 |
f1->foo(); // expected-warning {{calling function 'foo' requires holding mutex 'f1->mu_' exclusively}} |
| 2575 |
|
| 2576 |
f1->foo2(f2); // expected-warning {{calling function 'foo2' requires holding mutex 'f1->mu_' exclusively}} \ |
| 2577 |
// expected-warning {{calling function 'foo2' requires holding mutex 'f2->mu_' exclusively}} |
| 2578 |
Foo::sfoo(f1); // expected-warning {{calling function 'sfoo' requires holding mutex 'f1->mu_' exclusively}} |
| 2579 |
|
| 2580 |
f1->getMu()->Lock(); |
| 2581 |
|
| 2582 |
f1->a = 0; |
| 2583 |
f1->foo(); |
| 2584 |
f1->foo2(f2); // \ |
| 2585 |
// expected-warning {{calling function 'foo2' requires holding mutex 'f2->mu_' exclusively}} \ |
| 2586 |
// expected-note {{found near match 'f1->mu_'}} |
| 2587 |
|
| 2588 |
Foo::getMu(f2)->Lock(); |
| 2589 |
f1->foo2(f2); |
| 2590 |
Foo::getMu(f2)->Unlock(); |
| 2591 |
|
| 2592 |
Foo::sfoo(f1); |
| 2593 |
|
| 2594 |
f1->getMu()->Unlock(); |
| 2595 |
} |
| 2596 |
|
| 2597 |
|
| 2598 |
Mutex* getFooMu(Foo* f) LOCK_RETURNED(Foo::getMu(f)); |
| 2599 |
|
| 2600 |
class Bar : public Foo { |
| 2601 |
public: |
| 2602 |
int b GUARDED_BY(getMu()); |
| 2603 |
void bar() EXCLUSIVE_LOCKS_REQUIRED(getMu()); |
| 2604 |
void bar2(Bar* g) EXCLUSIVE_LOCKS_REQUIRED(getMu(this), g->getMu()); |
| 2605 |
|
| 2606 |
static void sbar(Bar* g) EXCLUSIVE_LOCKS_REQUIRED(g->getMu()); |
| 2607 |
static void sbar2(Bar* g) EXCLUSIVE_LOCKS_REQUIRED(getFooMu(g)); |
| 2608 |
}; |
| 2609 |
|
| 2610 |
|
| 2611 |
|
| 2612 |
// Use getMu() within other attributes. |
| 2613 |
// This requires at lest levels of substitution, more in the case of |
| 2614 |
void test2(Bar* b1, Bar* b2) { |
| 2615 |
b1->b = 0; // expected-warning {{writing variable 'b' requires holding mutex 'b1->mu_' exclusively}} |
| 2616 |
b1->bar(); // expected-warning {{calling function 'bar' requires holding mutex 'b1->mu_' exclusively}} |
| 2617 |
b1->bar2(b2); // expected-warning {{calling function 'bar2' requires holding mutex 'b1->mu_' exclusively}} \ |
| 2618 |
// expected-warning {{calling function 'bar2' requires holding mutex 'b2->mu_' exclusively}} |
| 2619 |
Bar::sbar(b1); // expected-warning {{calling function 'sbar' requires holding mutex 'b1->mu_' exclusively}} |
| 2620 |
Bar::sbar2(b1); // expected-warning {{calling function 'sbar2' requires holding mutex 'b1->mu_' exclusively}} |
| 2621 |
|
| 2622 |
b1->getMu()->Lock(); |
| 2623 |
|
| 2624 |
b1->b = 0; |
| 2625 |
b1->bar(); |
| 2626 |
b1->bar2(b2); // \ |
| 2627 |
// expected-warning {{calling function 'bar2' requires holding mutex 'b2->mu_' exclusively}} \ |
| 2628 |
// // expected-note {{found near match 'b1->mu_'}} |
| 2629 |
|
| 2630 |
b2->getMu()->Lock(); |
| 2631 |
b1->bar2(b2); |
| 2632 |
|
| 2633 |
b2->getMu()->Unlock(); |
| 2634 |
|
| 2635 |
Bar::sbar(b1); |
| 2636 |
Bar::sbar2(b1); |
| 2637 |
|
| 2638 |
b1->getMu()->Unlock(); |
| 2639 |
} |
| 2640 |
|
| 2641 |
|
| 2642 |
// Lock the mutex directly, but use attributes that call getMu() |
| 2643 |
// Also lock the mutex using getFooMu, which calls a lock_returned function. |
| 2644 |
void test3(Bar* b1, Bar* b2) { |
| 2645 |
b1->mu_.Lock(); |
| 2646 |
b1->b = 0; |
| 2647 |
b1->bar(); |
| 2648 |
|
| 2649 |
getFooMu(b2)->Lock(); |
| 2650 |
b1->bar2(b2); |
| 2651 |
getFooMu(b2)->Unlock(); |
| 2652 |
|
| 2653 |
Bar::sbar(b1); |
| 2654 |
Bar::sbar2(b1); |
| 2655 |
|
| 2656 |
b1->mu_.Unlock(); |
| 2657 |
} |
| 2658 |
|
| 2659 |
} // end namespace LockReturned |
| 2660 |
|
| 2661 |
|
| 2662 |
namespace ReleasableScopedLock { |
| 2663 |
|
| 2664 |
class Foo { |
| 2665 |
Mutex mu_; |
| 2666 |
bool c; |
| 2667 |
int a GUARDED_BY(mu_); |
| 2668 |
|
| 2669 |
void test1(); |
| 2670 |
void test2(); |
| 2671 |
void test3(); |
| 2672 |
void test4(); |
| 2673 |
void test5(); |
| 2674 |
void test6(); |
| 2675 |
}; |
| 2676 |
|
| 2677 |
|
| 2678 |
void Foo::test1() { |
| 2679 |
ReleasableMutexLock rlock(&mu_); |
| 2680 |
rlock.Release(); |
| 2681 |
} |
| 2682 |
|
| 2683 |
void Foo::test2() { |
| 2684 |
ReleasableMutexLock rlock(&mu_); |
| 2685 |
if (c) { // test join point -- held/not held during release |
| 2686 |
rlock.Release(); |
| 2687 |
} |
| 2688 |
// No warning on join point because the lock will be released by the scope object anyway. |
| 2689 |
} |
| 2690 |
|
| 2691 |
void Foo::test3() { |
| 2692 |
ReleasableMutexLock rlock(&mu_); |
| 2693 |
a = 0; |
| 2694 |
rlock.Release(); |
| 2695 |
a = 1; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} |
| 2696 |
} |
| 2697 |
|
| 2698 |
void Foo::test4() { |
| 2699 |
ReleasableMutexLock rlock(&mu_); |
| 2700 |
rlock.Release(); // expected-note{{mutex released here}} |
| 2701 |
rlock.Release(); // expected-warning {{releasing mutex 'mu_' that was not held}} |
| 2702 |
} |
| 2703 |
|
| 2704 |
void Foo::test5() { |
| 2705 |
ReleasableMutexLock rlock(&mu_); |
| 2706 |
if (c) { |
| 2707 |
rlock.Release(); |
| 2708 |
} |
| 2709 |
// No warning on join point because the lock will be released by the scope object anyway. |
| 2710 |
rlock.Release(); // expected-warning {{releasing mutex 'mu_' that was not held}} |
| 2711 |
} |
| 2712 |
|
| 2713 |
void Foo::test6() { |
| 2714 |
ReleasableMutexLock rlock(&mu_); |
| 2715 |
do { |
| 2716 |
if (c) { |
| 2717 |
rlock.Release(); |
| 2718 |
break; |
| 2719 |
} |
| 2720 |
} while (c); |
| 2721 |
// No warning on join point because the lock will be released by the scope object anyway |
| 2722 |
a = 1; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} |
| 2723 |
} |
| 2724 |
|
| 2725 |
|
| 2726 |
} // end namespace ReleasableScopedLock |
| 2727 |
|
| 2728 |
|
| 2729 |
namespace RelockableScopedLock { |
| 2730 |
|
| 2731 |
class DeferTraits {}; |
| 2732 |
|
| 2733 |
class SCOPED_LOCKABLE RelockableExclusiveMutexLock { |
| 2734 |
public: |
| 2735 |
RelockableExclusiveMutexLock(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu); |
| 2736 |
RelockableExclusiveMutexLock(Mutex *mu, DeferTraits) LOCKS_EXCLUDED(mu); |
| 2737 |
~RelockableExclusiveMutexLock() EXCLUSIVE_UNLOCK_FUNCTION(); |
| 2738 |
|
| 2739 |
void Lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 2740 |
void Unlock() UNLOCK_FUNCTION(); |
| 2741 |
}; |
| 2742 |
|
| 2743 |
struct SharedTraits {}; |
| 2744 |
struct ExclusiveTraits {}; |
| 2745 |
|
| 2746 |
class SCOPED_LOCKABLE RelockableMutexLock { |
| 2747 |
public: |
| 2748 |
RelockableMutexLock(Mutex *mu, DeferTraits) LOCKS_EXCLUDED(mu); |
| 2749 |
RelockableMutexLock(Mutex *mu, SharedTraits) SHARED_LOCK_FUNCTION(mu); |
| 2750 |
RelockableMutexLock(Mutex *mu, ExclusiveTraits) EXCLUSIVE_LOCK_FUNCTION(mu); |
| 2751 |
~RelockableMutexLock() UNLOCK_FUNCTION(); |
| 2752 |
|
| 2753 |
void Lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 2754 |
void Unlock() UNLOCK_FUNCTION(); |
| 2755 |
|
| 2756 |
void ReaderLock() SHARED_LOCK_FUNCTION(); |
| 2757 |
void ReaderUnlock() UNLOCK_FUNCTION(); |
| 2758 |
|
| 2759 |
void PromoteShared() UNLOCK_FUNCTION() EXCLUSIVE_LOCK_FUNCTION(); |
| 2760 |
void DemoteExclusive() UNLOCK_FUNCTION() SHARED_LOCK_FUNCTION(); |
| 2761 |
}; |
| 2762 |
|
| 2763 |
Mutex mu; |
| 2764 |
int x GUARDED_BY(mu); |
| 2765 |
bool b; |
| 2766 |
|
| 2767 |
void print(int); |
| 2768 |
|
| 2769 |
void relock() { |
| 2770 |
RelockableExclusiveMutexLock scope(&mu); |
| 2771 |
x = 2; |
| 2772 |
scope.Unlock(); |
| 2773 |
|
| 2774 |
x = 3; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 2775 |
|
| 2776 |
scope.Lock(); |
| 2777 |
x = 4; |
| 2778 |
} |
| 2779 |
|
| 2780 |
void deferLock() { |
| 2781 |
RelockableExclusiveMutexLock scope(&mu, DeferTraits{}); |
| 2782 |
x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 2783 |
scope.Lock(); |
| 2784 |
x = 3; |
| 2785 |
} |
| 2786 |
|
| 2787 |
void relockExclusive() { |
| 2788 |
RelockableMutexLock scope(&mu, SharedTraits{}); |
| 2789 |
print(x); |
| 2790 |
x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 2791 |
scope.ReaderUnlock(); |
| 2792 |
|
| 2793 |
print(x); // expected-warning {{reading variable 'x' requires holding mutex 'mu'}} |
| 2794 |
|
| 2795 |
scope.Lock(); |
| 2796 |
print(x); |
| 2797 |
x = 4; |
| 2798 |
|
| 2799 |
scope.DemoteExclusive(); |
| 2800 |
print(x); |
| 2801 |
x = 5; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 2802 |
} |
| 2803 |
|
| 2804 |
void relockShared() { |
| 2805 |
RelockableMutexLock scope(&mu, ExclusiveTraits{}); |
| 2806 |
print(x); |
| 2807 |
x = 2; |
| 2808 |
scope.Unlock(); |
| 2809 |
|
| 2810 |
print(x); // expected-warning {{reading variable 'x' requires holding mutex 'mu'}} |
| 2811 |
|
| 2812 |
scope.ReaderLock(); |
| 2813 |
print(x); |
| 2814 |
x = 4; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 2815 |
|
| 2816 |
scope.PromoteShared(); |
| 2817 |
print(x); |
| 2818 |
x = 5; |
| 2819 |
} |
| 2820 |
|
| 2821 |
void deferLockShared() { |
| 2822 |
RelockableMutexLock scope(&mu, DeferTraits{}); |
| 2823 |
print(x); // expected-warning {{reading variable 'x' requires holding mutex 'mu'}} |
| 2824 |
scope.ReaderLock(); |
| 2825 |
print(x); |
| 2826 |
x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 2827 |
} |
| 2828 |
|
| 2829 |
void doubleUnlock() { |
| 2830 |
RelockableExclusiveMutexLock scope(&mu); |
| 2831 |
scope.Unlock(); // expected-note{{mutex released here}} |
| 2832 |
scope.Unlock(); // expected-warning {{releasing mutex 'mu' that was not held}} |
| 2833 |
} |
| 2834 |
|
| 2835 |
void doubleLock1() { |
| 2836 |
RelockableExclusiveMutexLock scope(&mu); // expected-note{{mutex acquired here}} |
| 2837 |
scope.Lock(); // expected-warning {{acquiring mutex 'mu' that is already held}} |
| 2838 |
} |
| 2839 |
|
| 2840 |
void doubleLock2() { |
| 2841 |
RelockableExclusiveMutexLock scope(&mu); |
| 2842 |
scope.Unlock(); |
| 2843 |
scope.Lock(); // expected-note{{mutex acquired here}} |
| 2844 |
scope.Lock(); // expected-warning {{acquiring mutex 'mu' that is already held}} |
| 2845 |
} |
| 2846 |
|
| 2847 |
void lockJoin() { |
| 2848 |
RelockableMutexLock scope(&mu, DeferTraits{}); |
| 2849 |
if (b) |
| 2850 |
scope.Lock(); |
| 2851 |
// No warning on join point because the lock will be released by the scope object anyway. |
| 2852 |
x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 2853 |
} |
| 2854 |
|
| 2855 |
void unlockJoin() { |
| 2856 |
RelockableMutexLock scope(&mu, DeferTraits{}); |
| 2857 |
scope.Lock(); |
| 2858 |
if (b) |
| 2859 |
scope.Unlock(); |
| 2860 |
// No warning on join point because the lock will be released by the scope object anyway. |
| 2861 |
x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 2862 |
} |
| 2863 |
|
| 2864 |
void loopAcquire() { |
| 2865 |
RelockableMutexLock scope(&mu, DeferTraits{}); |
| 2866 |
for (unsigned i = 1; i < 10; ++i) |
| 2867 |
scope.Lock(); // We could catch this double lock with negative capabilities. |
| 2868 |
} |
| 2869 |
|
| 2870 |
void loopRelease() { |
| 2871 |
RelockableMutexLock scope(&mu, ExclusiveTraits{}); // expected-note {{mutex acquired here}} |
| 2872 |
// We have to warn on this join point despite the lock being managed ... |
| 2873 |
for (unsigned i = 1; i < 10; ++i) { // expected-warning {{expecting mutex 'mu' to be held at start of each loop}} |
| 2874 |
x = 1; // ... because we might miss that this doesn't always happen under lock. |
| 2875 |
if (i == 5) |
| 2876 |
scope.Unlock(); |
| 2877 |
} |
| 2878 |
} |
| 2879 |
|
| 2880 |
void loopPromote() { |
| 2881 |
RelockableMutexLock scope(&mu, SharedTraits{}); |
| 2882 |
for (unsigned i = 1; i < 10; ++i) { |
| 2883 |
x = 1; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 2884 |
if (i == 5) |
| 2885 |
scope.PromoteShared(); |
| 2886 |
} |
| 2887 |
} |
| 2888 |
|
| 2889 |
void loopDemote() { |
| 2890 |
RelockableMutexLock scope(&mu, ExclusiveTraits{}); // expected-note {{the other acquisition of mutex 'mu' is here}} |
| 2891 |
// We have to warn on this join point despite the lock being managed ... |
| 2892 |
for (unsigned i = 1; i < 10; ++i) { |
| 2893 |
x = 1; // ... because we might miss that this doesn't always happen under exclusive lock. |
| 2894 |
if (i == 5) |
| 2895 |
scope.DemoteExclusive(); // expected-warning {{mutex 'mu' is acquired exclusively and shared in the same scope}} |
| 2896 |
} |
| 2897 |
} |
| 2898 |
|
| 2899 |
void loopAcquireContinue() { |
| 2900 |
RelockableMutexLock scope(&mu, DeferTraits{}); |
| 2901 |
for (unsigned i = 1; i < 10; ++i) { |
| 2902 |
x = 1; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 2903 |
if (i == 5) { |
| 2904 |
scope.Lock(); |
| 2905 |
continue; |
| 2906 |
} |
| 2907 |
} |
| 2908 |
} |
| 2909 |
|
| 2910 |
void loopReleaseContinue() { |
| 2911 |
RelockableMutexLock scope(&mu, ExclusiveTraits{}); // expected-note {{mutex acquired here}} |
| 2912 |
// We have to warn on this join point despite the lock being managed ... |
| 2913 |
for (unsigned i = 1; i < 10; ++i) { // expected-warning {{expecting mutex 'mu' to be held at start of each loop}} |
| 2914 |
x = 1; // ... because we might miss that this doesn't always happen under lock. |
| 2915 |
if (i == 5) { |
| 2916 |
scope.Unlock(); |
| 2917 |
continue; |
| 2918 |
} |
| 2919 |
} |
| 2920 |
} |
| 2921 |
|
| 2922 |
void loopPromoteContinue() { |
| 2923 |
RelockableMutexLock scope(&mu, SharedTraits{}); |
| 2924 |
for (unsigned i = 1; i < 10; ++i) { |
| 2925 |
x = 1; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 2926 |
if (i == 5) { |
| 2927 |
scope.PromoteShared(); |
| 2928 |
continue; |
| 2929 |
} |
| 2930 |
} |
| 2931 |
} |
| 2932 |
|
| 2933 |
void loopDemoteContinue() { |
| 2934 |
RelockableMutexLock scope(&mu, ExclusiveTraits{}); // expected-note {{the other acquisition of mutex 'mu' is here}} |
| 2935 |
// We have to warn on this join point despite the lock being managed ... |
| 2936 |
for (unsigned i = 1; i < 10; ++i) { |
| 2937 |
x = 1; // ... because we might miss that this doesn't always happen under exclusive lock. |
| 2938 |
if (i == 5) { |
| 2939 |
scope.DemoteExclusive(); // expected-warning {{mutex 'mu' is acquired exclusively and shared in the same scope}} |
| 2940 |
continue; |
| 2941 |
} |
| 2942 |
} |
| 2943 |
} |
| 2944 |
|
| 2945 |
void exclusiveSharedJoin() { |
| 2946 |
RelockableMutexLock scope(&mu, DeferTraits{}); |
| 2947 |
if (b) |
| 2948 |
scope.Lock(); |
| 2949 |
else |
| 2950 |
scope.ReaderLock(); |
| 2951 |
// No warning on join point because the lock will be released by the scope object anyway. |
| 2952 |
print(x); |
| 2953 |
x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 2954 |
} |
| 2955 |
|
| 2956 |
void sharedExclusiveJoin() { |
| 2957 |
RelockableMutexLock scope(&mu, DeferTraits{}); |
| 2958 |
if (b) |
| 2959 |
scope.ReaderLock(); |
| 2960 |
else |
| 2961 |
scope.Lock(); |
| 2962 |
// No warning on join point because the lock will be released by the scope object anyway. |
| 2963 |
print(x); |
| 2964 |
x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 2965 |
} |
| 2966 |
|
| 2967 |
void assertJoin() { |
| 2968 |
RelockableMutexLock scope(&mu, DeferTraits{}); |
| 2969 |
if (b) |
| 2970 |
scope.Lock(); |
| 2971 |
else |
| 2972 |
mu.AssertHeld(); |
| 2973 |
x = 2; |
| 2974 |
} |
| 2975 |
|
| 2976 |
void assertSharedJoin() { |
| 2977 |
RelockableMutexLock scope(&mu, DeferTraits{}); |
| 2978 |
if (b) |
| 2979 |
scope.ReaderLock(); |
| 2980 |
else |
| 2981 |
mu.AssertReaderHeld(); |
| 2982 |
print(x); |
| 2983 |
x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 2984 |
} |
| 2985 |
|
| 2986 |
void assertStrongerJoin() { |
| 2987 |
RelockableMutexLock scope(&mu, DeferTraits{}); |
| 2988 |
if (b) |
| 2989 |
scope.ReaderLock(); |
| 2990 |
else |
| 2991 |
mu.AssertHeld(); |
| 2992 |
print(x); |
| 2993 |
x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 2994 |
} |
| 2995 |
|
| 2996 |
void assertWeakerJoin() { |
| 2997 |
RelockableMutexLock scope(&mu, DeferTraits{}); |
| 2998 |
if (b) |
| 2999 |
scope.Lock(); |
| 3000 |
else |
| 3001 |
mu.AssertReaderHeld(); |
| 3002 |
print(x); |
| 3003 |
x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 3004 |
} |
| 3005 |
|
| 3006 |
void directUnlock() { |
| 3007 |
RelockableExclusiveMutexLock scope(&mu); |
| 3008 |
mu.Unlock(); |
| 3009 |
// Debatable that there is no warning. Currently we don't track in the scoped |
| 3010 |
// object whether it is active, but just check if the contained locks can be |
| 3011 |
// reacquired. Here they can, because mu has been unlocked manually. |
| 3012 |
scope.Lock(); |
| 3013 |
} |
| 3014 |
|
| 3015 |
void directRelock() { |
| 3016 |
RelockableExclusiveMutexLock scope(&mu); |
| 3017 |
scope.Unlock(); |
| 3018 |
mu.Lock(); |
| 3019 |
// Similarly debatable that there is no warning. |
| 3020 |
scope.Unlock(); |
| 3021 |
} |
| 3022 |
|
| 3023 |
// Doesn't make a lot of sense, just making sure there is no crash. |
| 3024 |
void destructLock() { |
| 3025 |
RelockableExclusiveMutexLock scope(&mu); |
| 3026 |
scope.~RelockableExclusiveMutexLock(); |
| 3027 |
scope.Lock(); // Should be UB, so we don't really care. |
| 3028 |
} |
| 3029 |
|
| 3030 |
class SCOPED_LOCKABLE MemberLock { |
| 3031 |
public: |
| 3032 |
MemberLock() EXCLUSIVE_LOCK_FUNCTION(mutex); |
| 3033 |
~MemberLock() UNLOCK_FUNCTION(mutex); |
| 3034 |
void Lock() EXCLUSIVE_LOCK_FUNCTION(mutex); |
| 3035 |
Mutex mutex; |
| 3036 |
}; |
| 3037 |
|
| 3038 |
void relockShared2() { |
| 3039 |
MemberLock lock; // expected-note{{mutex acquired here}} |
| 3040 |
lock.Lock(); // expected-warning {{acquiring mutex 'lock.mutex' that is already held}} |
| 3041 |
} |
| 3042 |
|
| 3043 |
class SCOPED_LOCKABLE WeirdScope { |
| 3044 |
private: |
| 3045 |
Mutex *other; |
| 3046 |
|
| 3047 |
public: |
| 3048 |
WeirdScope(Mutex *mutex) EXCLUSIVE_LOCK_FUNCTION(mutex); |
| 3049 |
void unlock() EXCLUSIVE_UNLOCK_FUNCTION() EXCLUSIVE_UNLOCK_FUNCTION(other); |
| 3050 |
void lock() EXCLUSIVE_LOCK_FUNCTION() EXCLUSIVE_LOCK_FUNCTION(other); |
| 3051 |
~WeirdScope() EXCLUSIVE_UNLOCK_FUNCTION(); |
| 3052 |
|
| 3053 |
void requireOther() EXCLUSIVE_LOCKS_REQUIRED(other); |
| 3054 |
}; |
| 3055 |
|
| 3056 |
void relockWeird() { |
| 3057 |
WeirdScope scope(&mu); |
| 3058 |
x = 1; |
| 3059 |
scope.unlock(); // expected-warning {{releasing mutex 'scope.other' that was not held}} |
| 3060 |
x = 2; // \ |
| 3061 |
// expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 3062 |
scope.requireOther(); // \ |
| 3063 |
// expected-warning {{calling function 'requireOther' requires holding mutex 'scope.other' exclusively}} |
| 3064 |
scope.lock(); // expected-note {{mutex acquired here}} |
| 3065 |
x = 3; |
| 3066 |
scope.requireOther(); |
| 3067 |
} // expected-warning {{mutex 'scope.other' is still held at the end of function}} |
| 3068 |
|
| 3069 |
} // end namespace RelockableScopedLock |
| 3070 |
|
| 3071 |
|
| 3072 |
namespace ScopedUnlock { |
| 3073 |
|
| 3074 |
class SCOPED_LOCKABLE MutexUnlock { |
| 3075 |
public: |
| 3076 |
MutexUnlock(Mutex *mu) EXCLUSIVE_UNLOCK_FUNCTION(mu); |
| 3077 |
~MutexUnlock() EXCLUSIVE_UNLOCK_FUNCTION(); |
| 3078 |
|
| 3079 |
void Lock() EXCLUSIVE_UNLOCK_FUNCTION(); |
| 3080 |
void Unlock() EXCLUSIVE_LOCK_FUNCTION(); |
| 3081 |
}; |
| 3082 |
|
| 3083 |
class SCOPED_LOCKABLE ReaderMutexUnlock { |
| 3084 |
public: |
| 3085 |
ReaderMutexUnlock(Mutex *mu) SHARED_UNLOCK_FUNCTION(mu); |
| 3086 |
~ReaderMutexUnlock() EXCLUSIVE_UNLOCK_FUNCTION(); |
| 3087 |
|
| 3088 |
void Lock() EXCLUSIVE_UNLOCK_FUNCTION(); |
| 3089 |
void Unlock() EXCLUSIVE_LOCK_FUNCTION(); |
| 3090 |
}; |
| 3091 |
|
| 3092 |
Mutex mu; |
| 3093 |
int x GUARDED_BY(mu); |
| 3094 |
bool c; |
| 3095 |
void print(int); |
| 3096 |
|
| 3097 |
void simple() EXCLUSIVE_LOCKS_REQUIRED(mu) { |
| 3098 |
x = 1; |
| 3099 |
MutexUnlock scope(&mu); |
| 3100 |
x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 3101 |
} |
| 3102 |
|
| 3103 |
void simpleShared() SHARED_LOCKS_REQUIRED(mu) { |
| 3104 |
print(x); |
| 3105 |
ReaderMutexUnlock scope(&mu); |
| 3106 |
print(x); // expected-warning {{reading variable 'x' requires holding mutex 'mu'}} |
| 3107 |
} |
| 3108 |
|
| 3109 |
void innerUnlock() { |
| 3110 |
MutexLock outer(&mu); |
| 3111 |
if (x == 0) { |
| 3112 |
MutexUnlock inner(&mu); |
| 3113 |
x = 1; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 3114 |
} |
| 3115 |
x = 2; |
| 3116 |
} |
| 3117 |
|
| 3118 |
void innerUnlockShared() { |
| 3119 |
ReaderMutexLock outer(&mu); |
| 3120 |
if (x == 0) { |
| 3121 |
ReaderMutexUnlock inner(&mu); |
| 3122 |
print(x); // expected-warning {{reading variable 'x' requires holding mutex 'mu'}} |
| 3123 |
} |
| 3124 |
print(x); |
| 3125 |
} |
| 3126 |
|
| 3127 |
void manual() EXCLUSIVE_LOCKS_REQUIRED(mu) { |
| 3128 |
MutexUnlock scope(&mu); |
| 3129 |
scope.Lock(); |
| 3130 |
x = 2; |
| 3131 |
scope.Unlock(); |
| 3132 |
x = 3; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 3133 |
} |
| 3134 |
|
| 3135 |
void join() EXCLUSIVE_LOCKS_REQUIRED(mu) { |
| 3136 |
MutexUnlock scope(&mu); |
| 3137 |
if (c) |
| 3138 |
scope.Lock(); |
| 3139 |
// No warning on join point because the lock will be released by the scope object anyway. |
| 3140 |
scope.Lock(); |
| 3141 |
} |
| 3142 |
|
| 3143 |
void doubleLock() EXCLUSIVE_LOCKS_REQUIRED(mu) { |
| 3144 |
MutexUnlock scope(&mu); |
| 3145 |
scope.Lock(); // expected-note{{mutex acquired here}} |
| 3146 |
scope.Lock(); // expected-warning {{acquiring mutex 'mu' that is already held}} |
| 3147 |
} |
| 3148 |
|
| 3149 |
void doubleUnlock() EXCLUSIVE_LOCKS_REQUIRED(mu) { |
| 3150 |
MutexUnlock scope(&mu); // expected-note{{mutex released here}} |
| 3151 |
scope.Unlock(); // expected-warning {{releasing mutex 'mu' that was not held}} |
| 3152 |
} |
| 3153 |
|
| 3154 |
class SCOPED_LOCKABLE MutexLockUnlock { |
| 3155 |
public: |
| 3156 |
MutexLockUnlock(Mutex *mu1, Mutex *mu2) EXCLUSIVE_UNLOCK_FUNCTION(mu1) EXCLUSIVE_LOCK_FUNCTION(mu2); |
| 3157 |
~MutexLockUnlock() EXCLUSIVE_UNLOCK_FUNCTION(); |
| 3158 |
|
| 3159 |
void Release() EXCLUSIVE_UNLOCK_FUNCTION(); |
| 3160 |
void Acquire() EXCLUSIVE_LOCK_FUNCTION(); |
| 3161 |
}; |
| 3162 |
|
| 3163 |
Mutex other; |
| 3164 |
void fn() EXCLUSIVE_LOCKS_REQUIRED(other); |
| 3165 |
|
| 3166 |
void lockUnlock() EXCLUSIVE_LOCKS_REQUIRED(mu) { |
| 3167 |
MutexLockUnlock scope(&mu, &other); |
| 3168 |
fn(); |
| 3169 |
x = 1; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} |
| 3170 |
} |
| 3171 |
|
| 3172 |
} // end namespace ScopedUnlock |
| 3173 |
|
| 3174 |
|
| 3175 |
namespace TrylockFunctionTest { |
| 3176 |
|
| 3177 |
class Foo { |
| 3178 |
public: |
| 3179 |
Mutex mu1_; |
| 3180 |
Mutex mu2_; |
| 3181 |
bool c; |
| 3182 |
|
| 3183 |
bool lockBoth() EXCLUSIVE_TRYLOCK_FUNCTION(true, mu1_, mu2_); |
| 3184 |
}; |
| 3185 |
|
| 3186 |
bool Foo::lockBoth() { |
| 3187 |
if (!mu1_.TryLock()) |
| 3188 |
return false; |
| 3189 |
|
| 3190 |
mu2_.Lock(); |
| 3191 |
if (!c) { |
| 3192 |
mu1_.Unlock(); |
| 3193 |
mu2_.Unlock(); |
| 3194 |
return false; |
| 3195 |
} |
| 3196 |
|
| 3197 |
return true; |
| 3198 |
} |
| 3199 |
|
| 3200 |
|
| 3201 |
} // end namespace TrylockFunctionTest |
| 3202 |
|
| 3203 |
|
| 3204 |
|
| 3205 |
namespace DoubleLockBug { |
| 3206 |
|
| 3207 |
class Foo { |
| 3208 |
public: |
| 3209 |
Mutex mu_; |
| 3210 |
int a GUARDED_BY(mu_); |
| 3211 |
|
| 3212 |
void foo1() EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 3213 |
int foo2() SHARED_LOCKS_REQUIRED(mu_); |
| 3214 |
}; |
| 3215 |
|
| 3216 |
|
| 3217 |
void Foo::foo1() EXCLUSIVE_LOCKS_REQUIRED(mu_) { |
| 3218 |
a = 0; |
| 3219 |
} |
| 3220 |
|
| 3221 |
int Foo::foo2() SHARED_LOCKS_REQUIRED(mu_) { |
| 3222 |
return a; |
| 3223 |
} |
| 3224 |
|
| 3225 |
} |
| 3226 |
|
| 3227 |
|
| 3228 |
|
| 3229 |
namespace UnlockBug { |
| 3230 |
|
| 3231 |
class Foo { |
| 3232 |
public: |
| 3233 |
Mutex mutex_; |
| 3234 |
|
| 3235 |
void foo1() EXCLUSIVE_LOCKS_REQUIRED(mutex_) { // expected-note {{mutex acquired here}} |
| 3236 |
mutex_.Unlock(); |
| 3237 |
} // expected-warning {{expecting mutex 'mutex_' to be held at the end of function}} |
| 3238 |
|
| 3239 |
|
| 3240 |
void foo2() SHARED_LOCKS_REQUIRED(mutex_) { // expected-note {{mutex acquired here}} |
| 3241 |
mutex_.Unlock(); |
| 3242 |
} // expected-warning {{expecting mutex 'mutex_' to be held at the end of function}} |
| 3243 |
}; |
| 3244 |
|
| 3245 |
} // end namespace UnlockBug |
| 3246 |
|
| 3247 |
|
| 3248 |
|
| 3249 |
namespace FoolishScopedLockableBug { |
| 3250 |
|
| 3251 |
class SCOPED_LOCKABLE WTF_ScopedLockable { |
| 3252 |
public: |
| 3253 |
WTF_ScopedLockable(Mutex* mu) EXCLUSIVE_LOCK_FUNCTION(mu); |
| 3254 |
|
| 3255 |
// have to call release() manually; |
| 3256 |
~WTF_ScopedLockable(); |
| 3257 |
|
| 3258 |
void release() UNLOCK_FUNCTION(); |
| 3259 |
}; |
| 3260 |
|
| 3261 |
|
| 3262 |
class Foo { |
| 3263 |
Mutex mu_; |
| 3264 |
int a GUARDED_BY(mu_); |
| 3265 |
bool c; |
| 3266 |
|
| 3267 |
void doSomething(); |
| 3268 |
|
| 3269 |
void test1() { |
| 3270 |
WTF_ScopedLockable wtf(&mu_); |
| 3271 |
wtf.release(); |
| 3272 |
} |
| 3273 |
|
| 3274 |
void test2() { |
| 3275 |
WTF_ScopedLockable wtf(&mu_); // expected-note {{mutex acquired here}} |
| 3276 |
} // expected-warning {{mutex 'mu_' is still held at the end of function}} |
| 3277 |
|
| 3278 |
void test3() { |
| 3279 |
if (c) { |
| 3280 |
WTF_ScopedLockable wtf(&mu_); |
| 3281 |
wtf.release(); |
| 3282 |
} |
| 3283 |
} |
| 3284 |
|
| 3285 |
void test4() { |
| 3286 |
if (c) { |
| 3287 |
doSomething(); |
| 3288 |
} |
| 3289 |
else { |
| 3290 |
WTF_ScopedLockable wtf(&mu_); |
| 3291 |
wtf.release(); |
| 3292 |
} |
| 3293 |
} |
| 3294 |
|
| 3295 |
void test5() { |
| 3296 |
if (c) { |
| 3297 |
WTF_ScopedLockable wtf(&mu_); // expected-note {{mutex acquired here}} |
| 3298 |
} |
| 3299 |
} // expected-warning {{mutex 'mu_' is not held on every path through here}} |
| 3300 |
|
| 3301 |
void test6() { |
| 3302 |
if (c) { |
| 3303 |
doSomething(); |
| 3304 |
} |
| 3305 |
else { |
| 3306 |
WTF_ScopedLockable wtf(&mu_); // expected-note {{mutex acquired here}} |
| 3307 |
} |
| 3308 |
} // expected-warning {{mutex 'mu_' is not held on every path through here}} |
| 3309 |
}; |
| 3310 |
|
| 3311 |
|
| 3312 |
} // end namespace FoolishScopedLockableBug |
| 3313 |
|
| 3314 |
|
| 3315 |
|
| 3316 |
namespace TemporaryCleanupExpr { |
| 3317 |
|
| 3318 |
class Foo { |
| 3319 |
int a GUARDED_BY(getMutexPtr().get()); |
| 3320 |
|
| 3321 |
SmartPtr<Mutex> getMutexPtr(); |
| 3322 |
|
| 3323 |
void test(); |
| 3324 |
}; |
| 3325 |
|
| 3326 |
|
| 3327 |
void Foo::test() { |
| 3328 |
{ |
| 3329 |
ReaderMutexLock lock(getMutexPtr().get()); |
| 3330 |
int b = a; |
| 3331 |
} |
| 3332 |
int b = a; // expected-warning {{reading variable 'a' requires holding mutex 'getMutexPtr()'}} |
| 3333 |
} |
| 3334 |
|
| 3335 |
#ifdef __cpp_guaranteed_copy_elision |
| 3336 |
|
| 3337 |
void guaranteed_copy_elision() { |
| 3338 |
MutexLock lock = MutexLock{&sls_mu}; |
| 3339 |
sls_guard_var = 0; |
| 3340 |
} |
| 3341 |
|
| 3342 |
void guaranteed_copy_elision_const() { |
| 3343 |
const MutexLock lock = MutexLock{&sls_mu}; |
| 3344 |
sls_guard_var = 0; |
| 3345 |
} |
| 3346 |
|
| 3347 |
#endif |
| 3348 |
|
| 3349 |
} // end namespace TemporaryCleanupExpr |
| 3350 |
|
| 3351 |
|
| 3352 |
|
| 3353 |
namespace SmartPointerTests { |
| 3354 |
|
| 3355 |
class Foo { |
| 3356 |
public: |
| 3357 |
SmartPtr<Mutex> mu_; |
| 3358 |
int a GUARDED_BY(mu_); |
| 3359 |
int b GUARDED_BY(mu_.get()); |
| 3360 |
int c GUARDED_BY(*mu_); |
| 3361 |
|
| 3362 |
void Lock() EXCLUSIVE_LOCK_FUNCTION(mu_); |
| 3363 |
void Unlock() UNLOCK_FUNCTION(mu_); |
| 3364 |
|
| 3365 |
void test0(); |
| 3366 |
void test1(); |
| 3367 |
void test2(); |
| 3368 |
void test3(); |
| 3369 |
void test4(); |
| 3370 |
void test5(); |
| 3371 |
void test6(); |
| 3372 |
void test7(); |
| 3373 |
void test8(); |
| 3374 |
}; |
| 3375 |
|
| 3376 |
void Foo::test0() { |
| 3377 |
a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} |
| 3378 |
b = 0; // expected-warning {{writing variable 'b' requires holding mutex 'mu_' exclusively}} |
| 3379 |
c = 0; // expected-warning {{writing variable 'c' requires holding mutex 'mu_' exclusively}} |
| 3380 |
} |
| 3381 |
|
| 3382 |
void Foo::test1() { |
| 3383 |
mu_->Lock(); |
| 3384 |
a = 0; |
| 3385 |
b = 0; |
| 3386 |
c = 0; |
| 3387 |
mu_->Unlock(); |
| 3388 |
} |
| 3389 |
|
| 3390 |
void Foo::test2() { |
| 3391 |
(*mu_).Lock(); |
| 3392 |
a = 0; |
| 3393 |
b = 0; |
| 3394 |
c = 0; |
| 3395 |
(*mu_).Unlock(); |
| 3396 |
} |
| 3397 |
|
| 3398 |
|
| 3399 |
void Foo::test3() { |
| 3400 |
mu_.get()->Lock(); |
| 3401 |
a = 0; |
| 3402 |
b = 0; |
| 3403 |
c = 0; |
| 3404 |
mu_.get()->Unlock(); |
| 3405 |
} |
| 3406 |
|
| 3407 |
|
| 3408 |
void Foo::test4() { |
| 3409 |
MutexLock lock(mu_.get()); |
| 3410 |
a = 0; |
| 3411 |
b = 0; |
| 3412 |
c = 0; |
| 3413 |
} |
| 3414 |
|
| 3415 |
|
| 3416 |
void Foo::test5() { |
| 3417 |
MutexLock lock(&(*mu_)); |
| 3418 |
a = 0; |
| 3419 |
b = 0; |
| 3420 |
c = 0; |
| 3421 |
} |
| 3422 |
|
| 3423 |
|
| 3424 |
void Foo::test6() { |
| 3425 |
Lock(); |
| 3426 |
a = 0; |
| 3427 |
b = 0; |
| 3428 |
c = 0; |
| 3429 |
Unlock(); |
| 3430 |
} |
| 3431 |
|
| 3432 |
|
| 3433 |
void Foo::test7() { |
| 3434 |
{ |
| 3435 |
Lock(); |
| 3436 |
mu_->Unlock(); |
| 3437 |
} |
| 3438 |
{ |
| 3439 |
mu_->Lock(); |
| 3440 |
Unlock(); |
| 3441 |
} |
| 3442 |
{ |
| 3443 |
mu_.get()->Lock(); |
| 3444 |
mu_->Unlock(); |
| 3445 |
} |
| 3446 |
{ |
| 3447 |
mu_->Lock(); |
| 3448 |
mu_.get()->Unlock(); |
| 3449 |
} |
| 3450 |
{ |
| 3451 |
mu_.get()->Lock(); |
| 3452 |
(*mu_).Unlock(); |
| 3453 |
} |
| 3454 |
{ |
| 3455 |
(*mu_).Lock(); |
| 3456 |
mu_->Unlock(); |
| 3457 |
} |
| 3458 |
} |
| 3459 |
|
| 3460 |
|
| 3461 |
void Foo::test8() { |
| 3462 |
mu_->Lock(); // expected-note 2 {{mutex acquired here}} |
| 3463 |
mu_.get()->Lock(); // expected-warning {{acquiring mutex 'mu_' that is already held}} |
| 3464 |
(*mu_).Lock(); // expected-warning {{acquiring mutex 'mu_' that is already held}} |
| 3465 |
mu_.get()->Unlock(); // expected-note {{mutex released here}} |
| 3466 |
Unlock(); // expected-warning {{releasing mutex 'mu_' that was not held}} |
| 3467 |
} |
| 3468 |
|
| 3469 |
|
| 3470 |
class Bar { |
| 3471 |
SmartPtr<Foo> foo; |
| 3472 |
|
| 3473 |
void test0(); |
| 3474 |
void test1(); |
| 3475 |
void test2(); |
| 3476 |
void test3(); |
| 3477 |
}; |
| 3478 |
|
| 3479 |
|
| 3480 |
void Bar::test0() { |
| 3481 |
foo->a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'foo->mu_' exclusively}} |
| 3482 |
(*foo).b = 0; // expected-warning {{writing variable 'b' requires holding mutex 'foo->mu_' exclusively}} |
| 3483 |
foo.get()->c = 0; // expected-warning {{writing variable 'c' requires holding mutex 'foo->mu_' exclusively}} |
| 3484 |
} |
| 3485 |
|
| 3486 |
|
| 3487 |
void Bar::test1() { |
| 3488 |
foo->mu_->Lock(); |
| 3489 |
foo->a = 0; |
| 3490 |
(*foo).b = 0; |
| 3491 |
foo.get()->c = 0; |
| 3492 |
foo->mu_->Unlock(); |
| 3493 |
} |
| 3494 |
|
| 3495 |
|
| 3496 |
void Bar::test2() { |
| 3497 |
(*foo).mu_->Lock(); |
| 3498 |
foo->a = 0; |
| 3499 |
(*foo).b = 0; |
| 3500 |
foo.get()->c = 0; |
| 3501 |
foo.get()->mu_->Unlock(); |
| 3502 |
} |
| 3503 |
|
| 3504 |
|
| 3505 |
void Bar::test3() { |
| 3506 |
MutexLock lock(foo->mu_.get()); |
| 3507 |
foo->a = 0; |
| 3508 |
(*foo).b = 0; |
| 3509 |
foo.get()->c = 0; |
| 3510 |
} |
| 3511 |
|
| 3512 |
} // end namespace SmartPointerTests |
| 3513 |
|
| 3514 |
|
| 3515 |
|
| 3516 |
namespace DuplicateAttributeTest { |
| 3517 |
|
| 3518 |
class LOCKABLE Foo { |
| 3519 |
public: |
| 3520 |
Mutex mu1_; |
| 3521 |
Mutex mu2_; |
| 3522 |
Mutex mu3_; |
| 3523 |
int a GUARDED_BY(mu1_); |
| 3524 |
int b GUARDED_BY(mu2_); |
| 3525 |
int c GUARDED_BY(mu3_); |
| 3526 |
|
| 3527 |
void lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 3528 |
void unlock() UNLOCK_FUNCTION(); |
| 3529 |
|
| 3530 |
void lock1() EXCLUSIVE_LOCK_FUNCTION(mu1_); |
| 3531 |
void slock1() SHARED_LOCK_FUNCTION(mu1_); |
| 3532 |
void lock3() EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_); |
| 3533 |
void locklots() |
| 3534 |
EXCLUSIVE_LOCK_FUNCTION(mu1_) |
| 3535 |
EXCLUSIVE_LOCK_FUNCTION(mu2_) |
| 3536 |
EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_); |
| 3537 |
|
| 3538 |
void unlock1() UNLOCK_FUNCTION(mu1_); |
| 3539 |
void unlock3() UNLOCK_FUNCTION(mu1_, mu2_, mu3_); |
| 3540 |
void unlocklots() |
| 3541 |
UNLOCK_FUNCTION(mu1_) |
| 3542 |
UNLOCK_FUNCTION(mu2_) |
| 3543 |
UNLOCK_FUNCTION(mu1_, mu2_, mu3_); |
| 3544 |
}; |
| 3545 |
|
| 3546 |
|
| 3547 |
void Foo::lock() EXCLUSIVE_LOCK_FUNCTION() { } |
| 3548 |
void Foo::unlock() UNLOCK_FUNCTION() { } |
| 3549 |
|
| 3550 |
void Foo::lock1() EXCLUSIVE_LOCK_FUNCTION(mu1_) { |
| 3551 |
mu1_.Lock(); |
| 3552 |
} |
| 3553 |
|
| 3554 |
void Foo::slock1() SHARED_LOCK_FUNCTION(mu1_) { |
| 3555 |
mu1_.ReaderLock(); |
| 3556 |
} |
| 3557 |
|
| 3558 |
void Foo::lock3() EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_) { |
| 3559 |
mu1_.Lock(); |
| 3560 |
mu2_.Lock(); |
| 3561 |
mu3_.Lock(); |
| 3562 |
} |
| 3563 |
|
| 3564 |
void Foo::locklots() |
| 3565 |
EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_) |
| 3566 |
EXCLUSIVE_LOCK_FUNCTION(mu2_, mu3_) { |
| 3567 |
mu1_.Lock(); |
| 3568 |
mu2_.Lock(); |
| 3569 |
mu3_.Lock(); |
| 3570 |
} |
| 3571 |
|
| 3572 |
void Foo::unlock1() UNLOCK_FUNCTION(mu1_) { |
| 3573 |
mu1_.Unlock(); |
| 3574 |
} |
| 3575 |
|
| 3576 |
void Foo::unlock3() UNLOCK_FUNCTION(mu1_, mu2_, mu3_) { |
| 3577 |
mu1_.Unlock(); |
| 3578 |
mu2_.Unlock(); |
| 3579 |
mu3_.Unlock(); |
| 3580 |
} |
| 3581 |
|
| 3582 |
void Foo::unlocklots() |
| 3583 |
UNLOCK_FUNCTION(mu1_, mu2_) |
| 3584 |
UNLOCK_FUNCTION(mu2_, mu3_) { |
| 3585 |
mu1_.Unlock(); |
| 3586 |
mu2_.Unlock(); |
| 3587 |
mu3_.Unlock(); |
| 3588 |
} |
| 3589 |
|
| 3590 |
|
| 3591 |
void test0() { |
| 3592 |
Foo foo; |
| 3593 |
foo.lock(); |
| 3594 |
foo.unlock(); |
| 3595 |
|
| 3596 |
foo.lock(); // expected-note{{mutex acquired here}} |
| 3597 |
foo.lock(); // expected-warning {{acquiring mutex 'foo' that is already held}} |
| 3598 |
foo.unlock(); // expected-note{{mutex released here}} |
| 3599 |
foo.unlock(); // expected-warning {{releasing mutex 'foo' that was not held}} |
| 3600 |
} |
| 3601 |
|
| 3602 |
|
| 3603 |
void test1() { |
| 3604 |
Foo foo; |
| 3605 |
foo.lock1(); |
| 3606 |
foo.a = 0; |
| 3607 |
foo.unlock1(); |
| 3608 |
|
| 3609 |
foo.lock1(); // expected-note{{mutex acquired here}} |
| 3610 |
foo.lock1(); // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}} |
| 3611 |
foo.a = 0; |
| 3612 |
foo.unlock1(); // expected-note{{mutex released here}} |
| 3613 |
foo.unlock1(); // expected-warning {{releasing mutex 'foo.mu1_' that was not held}} |
| 3614 |
} |
| 3615 |
|
| 3616 |
|
| 3617 |
int test2() { |
| 3618 |
Foo foo; |
| 3619 |
foo.slock1(); |
| 3620 |
int d1 = foo.a; |
| 3621 |
foo.unlock1(); |
| 3622 |
|
| 3623 |
foo.slock1(); // expected-note{{mutex acquired here}} |
| 3624 |
foo.slock1(); // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}} |
| 3625 |
int d2 = foo.a; |
| 3626 |
foo.unlock1(); // expected-note{{mutex released here}} |
| 3627 |
foo.unlock1(); // expected-warning {{releasing mutex 'foo.mu1_' that was not held}} |
| 3628 |
return d1 + d2; |
| 3629 |
} |
| 3630 |
|
| 3631 |
|
| 3632 |
void test3() { |
| 3633 |
Foo foo; |
| 3634 |
foo.lock3(); |
| 3635 |
foo.a = 0; |
| 3636 |
foo.b = 0; |
| 3637 |
foo.c = 0; |
| 3638 |
foo.unlock3(); |
| 3639 |
|
| 3640 |
foo.lock3(); // expected-note 3 {{mutex acquired here}} |
| 3641 |
foo.lock3(); // \ |
| 3642 |
// expected-warning {{acquiring mutex 'foo.mu1_' that is already held}} \ |
| 3643 |
// expected-warning {{acquiring mutex 'foo.mu2_' that is already held}} \ |
| 3644 |
// expected-warning {{acquiring mutex 'foo.mu3_' that is already held}} |
| 3645 |
foo.a = 0; |
| 3646 |
foo.b = 0; |
| 3647 |
foo.c = 0; |
| 3648 |
foo.unlock3(); // expected-note 3 {{mutex released here}} |
| 3649 |
foo.unlock3(); // \ |
| 3650 |
// expected-warning {{releasing mutex 'foo.mu1_' that was not held}} \ |
| 3651 |
// expected-warning {{releasing mutex 'foo.mu2_' that was not held}} \ |
| 3652 |
// expected-warning {{releasing mutex 'foo.mu3_' that was not held}} |
| 3653 |
} |
| 3654 |
|
| 3655 |
|
| 3656 |
void testlots() { |
| 3657 |
Foo foo; |
| 3658 |
foo.locklots(); |
| 3659 |
foo.a = 0; |
| 3660 |
foo.b = 0; |
| 3661 |
foo.c = 0; |
| 3662 |
foo.unlocklots(); |
| 3663 |
|
| 3664 |
foo.locklots(); // expected-note 3 {{mutex acquired here}} |
| 3665 |
foo.locklots(); // \ |
| 3666 |
// expected-warning {{acquiring mutex 'foo.mu1_' that is already held}} \ |
| 3667 |
// expected-warning {{acquiring mutex 'foo.mu2_' that is already held}} \ |
| 3668 |
// expected-warning {{acquiring mutex 'foo.mu3_' that is already held}} |
| 3669 |
foo.a = 0; |
| 3670 |
foo.b = 0; |
| 3671 |
foo.c = 0; |
| 3672 |
foo.unlocklots(); // expected-note 3 {{mutex released here}} |
| 3673 |
foo.unlocklots(); // \ |
| 3674 |
// expected-warning {{releasing mutex 'foo.mu1_' that was not held}} \ |
| 3675 |
// expected-warning {{releasing mutex 'foo.mu2_' that was not held}} \ |
| 3676 |
// expected-warning {{releasing mutex 'foo.mu3_' that was not held}} |
| 3677 |
} |
| 3678 |
|
| 3679 |
} // end namespace DuplicateAttributeTest |
| 3680 |
|
| 3681 |
|
| 3682 |
|
| 3683 |
namespace TryLockEqTest { |
| 3684 |
|
| 3685 |
class Foo { |
| 3686 |
Mutex mu_; |
| 3687 |
int a GUARDED_BY(mu_); |
| 3688 |
bool c; |
| 3689 |
|
| 3690 |
int tryLockMutexI() EXCLUSIVE_TRYLOCK_FUNCTION(1, mu_); |
| 3691 |
Mutex* tryLockMutexP() EXCLUSIVE_TRYLOCK_FUNCTION(1, mu_); |
| 3692 |
void unlock() UNLOCK_FUNCTION(mu_); |
| 3693 |
|
| 3694 |
void test1(); |
| 3695 |
void test2(); |
| 3696 |
}; |
| 3697 |
|
| 3698 |
|
| 3699 |
void Foo::test1() { |
| 3700 |
if (tryLockMutexP() == 0) { |
| 3701 |
a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} |
| 3702 |
return; |
| 3703 |
} |
| 3704 |
a = 0; |
| 3705 |
unlock(); |
| 3706 |
|
| 3707 |
if (tryLockMutexP() != 0) { |
| 3708 |
a = 0; |
| 3709 |
unlock(); |
| 3710 |
} |
| 3711 |
|
| 3712 |
if (0 != tryLockMutexP()) { |
| 3713 |
a = 0; |
| 3714 |
unlock(); |
| 3715 |
} |
| 3716 |
|
| 3717 |
if (!(tryLockMutexP() == 0)) { |
| 3718 |
a = 0; |
| 3719 |
unlock(); |
| 3720 |
} |
| 3721 |
|
| 3722 |
if (tryLockMutexI() == 0) { |
| 3723 |
a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} |
| 3724 |
return; |
| 3725 |
} |
| 3726 |
a = 0; |
| 3727 |
unlock(); |
| 3728 |
|
| 3729 |
if (0 == tryLockMutexI()) { |
| 3730 |
a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} |
| 3731 |
return; |
| 3732 |
} |
| 3733 |
a = 0; |
| 3734 |
unlock(); |
| 3735 |
|
| 3736 |
if (tryLockMutexI() == 1) { |
| 3737 |
a = 0; |
| 3738 |
unlock(); |
| 3739 |
} |
| 3740 |
|
| 3741 |
if (mu_.TryLock() == false) { |
| 3742 |
a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} |
| 3743 |
return; |
| 3744 |
} |
| 3745 |
a = 0; |
| 3746 |
unlock(); |
| 3747 |
|
| 3748 |
if (mu_.TryLock() == true) { |
| 3749 |
a = 0; |
| 3750 |
unlock(); |
| 3751 |
} |
| 3752 |
else { |
| 3753 |
a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} |
| 3754 |
} |
| 3755 |
|
| 3756 |
#if __has_feature(cxx_nullptr) |
| 3757 |
if (tryLockMutexP() == nullptr) { |
| 3758 |
a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} |
| 3759 |
return; |
| 3760 |
} |
| 3761 |
a = 0; |
| 3762 |
unlock(); |
| 3763 |
#endif |
| 3764 |
} |
| 3765 |
|
| 3766 |
} // end namespace TryLockEqTest |
| 3767 |
|
| 3768 |
|
| 3769 |
namespace ExistentialPatternMatching { |
| 3770 |
|
| 3771 |
class Graph { |
| 3772 |
public: |
| 3773 |
Mutex mu_; |
| 3774 |
}; |
| 3775 |
|
| 3776 |
void LockAllGraphs() EXCLUSIVE_LOCK_FUNCTION(&Graph::mu_); |
| 3777 |
void UnlockAllGraphs() UNLOCK_FUNCTION(&Graph::mu_); |
| 3778 |
|
| 3779 |
class Node { |
| 3780 |
public: |
| 3781 |
int a GUARDED_BY(&Graph::mu_); |
| 3782 |
|
| 3783 |
void foo() EXCLUSIVE_LOCKS_REQUIRED(&Graph::mu_) { |
| 3784 |
a = 0; |
| 3785 |
} |
| 3786 |
void foo2() LOCKS_EXCLUDED(&Graph::mu_); |
| 3787 |
}; |
| 3788 |
|
| 3789 |
void test() { |
| 3790 |
Graph g1; |
| 3791 |
Graph g2; |
| 3792 |
Node n1; |
| 3793 |
|
| 3794 |
n1.a = 0; // expected-warning {{writing variable 'a' requires holding mutex '&ExistentialPatternMatching::Graph::mu_' exclusively}} |
| 3795 |
n1.foo(); // expected-warning {{calling function 'foo' requires holding mutex '&ExistentialPatternMatching::Graph::mu_' exclusively}} |
| 3796 |
n1.foo2(); |
| 3797 |
|
| 3798 |
g1.mu_.Lock(); |
| 3799 |
n1.a = 0; |
| 3800 |
n1.foo(); |
| 3801 |
n1.foo2(); // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is held}} |
| 3802 |
g1.mu_.Unlock(); |
| 3803 |
|
| 3804 |
g2.mu_.Lock(); |
| 3805 |
n1.a = 0; |
| 3806 |
n1.foo(); |
| 3807 |
n1.foo2(); // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is held}} |
| 3808 |
g2.mu_.Unlock(); |
| 3809 |
|
| 3810 |
LockAllGraphs(); |
| 3811 |
n1.a = 0; |
| 3812 |
n1.foo(); |
| 3813 |
n1.foo2(); // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is held}} |
| 3814 |
UnlockAllGraphs(); |
| 3815 |
|
| 3816 |
LockAllGraphs(); |
| 3817 |
g1.mu_.Unlock(); |
| 3818 |
|
| 3819 |
LockAllGraphs(); |
| 3820 |
g2.mu_.Unlock(); |
| 3821 |
|
| 3822 |
LockAllGraphs(); // expected-note{{mutex acquired here}} |
| 3823 |
g1.mu_.Lock(); // expected-warning {{acquiring mutex 'g1.mu_' that is already held}} |
| 3824 |
g1.mu_.Unlock(); |
| 3825 |
} |
| 3826 |
|
| 3827 |
} // end namespace ExistentialPatternMatching |
| 3828 |
|
| 3829 |
|
| 3830 |
namespace StringIgnoreTest { |
| 3831 |
|
| 3832 |
class Foo { |
| 3833 |
public: |
| 3834 |
Mutex mu_; |
| 3835 |
void lock() EXCLUSIVE_LOCK_FUNCTION(""); |
| 3836 |
void unlock() UNLOCK_FUNCTION(""); |
| 3837 |
void goober() EXCLUSIVE_LOCKS_REQUIRED(""); |
| 3838 |
void roober() SHARED_LOCKS_REQUIRED(""); |
| 3839 |
}; |
| 3840 |
|
| 3841 |
|
| 3842 |
class Bar : public Foo { |
| 3843 |
public: |
| 3844 |
void bar(Foo* f) { |
| 3845 |
f->unlock(); |
| 3846 |
f->goober(); |
| 3847 |
f->roober(); |
| 3848 |
f->lock(); |
| 3849 |
}; |
| 3850 |
}; |
| 3851 |
|
| 3852 |
} // end namespace StringIgnoreTest |
| 3853 |
|
| 3854 |
|
| 3855 |
namespace LockReturnedScopeFix { |
| 3856 |
|
| 3857 |
class Base { |
| 3858 |
protected: |
| 3859 |
struct Inner; |
| 3860 |
bool c; |
| 3861 |
|
| 3862 |
const Mutex& getLock(const Inner* i); |
| 3863 |
|
| 3864 |
void lockInner (Inner* i) EXCLUSIVE_LOCK_FUNCTION(getLock(i)); |
| 3865 |
void unlockInner(Inner* i) UNLOCK_FUNCTION(getLock(i)); |
| 3866 |
void foo(Inner* i) EXCLUSIVE_LOCKS_REQUIRED(getLock(i)); |
| 3867 |
|
| 3868 |
void bar(Inner* i); |
| 3869 |
}; |
| 3870 |
|
| 3871 |
|
| 3872 |
struct Base::Inner { |
| 3873 |
Mutex lock_; |
| 3874 |
void doSomething() EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 3875 |
}; |
| 3876 |
|
| 3877 |
|
| 3878 |
const Mutex& Base::getLock(const Inner* i) LOCK_RETURNED(i->lock_) { |
| 3879 |
return i->lock_; |
| 3880 |
} |
| 3881 |
|
| 3882 |
|
| 3883 |
void Base::foo(Inner* i) { |
| 3884 |
i->doSomething(); |
| 3885 |
} |
| 3886 |
|
| 3887 |
void Base::bar(Inner* i) { |
| 3888 |
if (c) { |
| 3889 |
i->lock_.Lock(); |
| 3890 |
unlockInner(i); |
| 3891 |
} |
| 3892 |
else { |
| 3893 |
lockInner(i); |
| 3894 |
i->lock_.Unlock(); |
| 3895 |
} |
| 3896 |
} |
| 3897 |
|
| 3898 |
} // end namespace LockReturnedScopeFix |
| 3899 |
|
| 3900 |
|
| 3901 |
namespace TrylockWithCleanups { |
| 3902 |
|
| 3903 |
struct Foo { |
| 3904 |
Mutex mu_; |
| 3905 |
int a GUARDED_BY(mu_); |
| 3906 |
}; |
| 3907 |
|
| 3908 |
Foo* GetAndLockFoo(const MyString& s) |
| 3909 |
EXCLUSIVE_TRYLOCK_FUNCTION(true, &Foo::mu_); |
| 3910 |
|
| 3911 |
static void test() { |
| 3912 |
Foo* lt = GetAndLockFoo("foo"); |
| 3913 |
if (!lt) return; |
| 3914 |
int a = lt->a; |
| 3915 |
lt->mu_.Unlock(); |
| 3916 |
} |
| 3917 |
|
| 3918 |
} // end namespace TrylockWithCleanups |
| 3919 |
|
| 3920 |
|
| 3921 |
namespace UniversalLock { |
| 3922 |
|
| 3923 |
class Foo { |
| 3924 |
Mutex mu_; |
| 3925 |
bool c; |
| 3926 |
|
| 3927 |
int a GUARDED_BY(mu_); |
| 3928 |
void r_foo() SHARED_LOCKS_REQUIRED(mu_); |
| 3929 |
void w_foo() EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 3930 |
|
| 3931 |
void test1() { |
| 3932 |
int b; |
| 3933 |
|
| 3934 |
beginNoWarnOnReads(); |
| 3935 |
b = a; |
| 3936 |
r_foo(); |
| 3937 |
endNoWarnOnReads(); |
| 3938 |
|
| 3939 |
beginNoWarnOnWrites(); |
| 3940 |
a = 0; |
| 3941 |
w_foo(); |
| 3942 |
endNoWarnOnWrites(); |
| 3943 |
} |
| 3944 |
|
| 3945 |
// don't warn on joins with universal lock |
| 3946 |
void test2() { |
| 3947 |
if (c) { |
| 3948 |
beginNoWarnOnWrites(); |
| 3949 |
} |
| 3950 |
a = 0; // \ |
| 3951 |
// expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} |
| 3952 |
endNoWarnOnWrites(); // \ |
| 3953 |
// expected-warning {{releasing wildcard '*' that was not held}} |
| 3954 |
} |
| 3955 |
|
| 3956 |
|
| 3957 |
// make sure the universal lock joins properly |
| 3958 |
void test3() { |
| 3959 |
if (c) { |
| 3960 |
mu_.Lock(); |
| 3961 |
beginNoWarnOnWrites(); |
| 3962 |
} |
| 3963 |
else { |
| 3964 |
beginNoWarnOnWrites(); |
| 3965 |
mu_.Lock(); |
| 3966 |
} |
| 3967 |
a = 0; |
| 3968 |
endNoWarnOnWrites(); |
| 3969 |
mu_.Unlock(); |
| 3970 |
} |
| 3971 |
|
| 3972 |
|
| 3973 |
// combine universal lock with other locks |
| 3974 |
void test4() { |
| 3975 |
beginNoWarnOnWrites(); |
| 3976 |
mu_.Lock(); |
| 3977 |
mu_.Unlock(); |
| 3978 |
endNoWarnOnWrites(); |
| 3979 |
|
| 3980 |
mu_.Lock(); |
| 3981 |
beginNoWarnOnWrites(); |
| 3982 |
endNoWarnOnWrites(); |
| 3983 |
mu_.Unlock(); |
| 3984 |
|
| 3985 |
mu_.Lock(); |
| 3986 |
beginNoWarnOnWrites(); |
| 3987 |
mu_.Unlock(); |
| 3988 |
endNoWarnOnWrites(); |
| 3989 |
} |
| 3990 |
}; |
| 3991 |
|
| 3992 |
} // end namespace UniversalLock |
| 3993 |
|
| 3994 |
|
| 3995 |
namespace TemplateLockReturned { |
| 3996 |
|
| 3997 |
template<class T> |
| 3998 |
class BaseT { |
| 3999 |
public: |
| 4000 |
virtual void baseMethod() = 0; |
| 4001 |
Mutex* get_mutex() LOCK_RETURNED(mutex_) { return &mutex_; } |
| 4002 |
|
| 4003 |
Mutex mutex_; |
| 4004 |
int a GUARDED_BY(mutex_); |
| 4005 |
}; |
| 4006 |
|
| 4007 |
|
| 4008 |
class Derived : public BaseT<int> { |
| 4009 |
public: |
| 4010 |
void baseMethod() EXCLUSIVE_LOCKS_REQUIRED(get_mutex()) { |
| 4011 |
a = 0; |
| 4012 |
} |
| 4013 |
}; |
| 4014 |
|
| 4015 |
} // end namespace TemplateLockReturned |
| 4016 |
|
| 4017 |
|
| 4018 |
namespace ExprMatchingBugFix { |
| 4019 |
|
| 4020 |
class Foo { |
| 4021 |
public: |
| 4022 |
Mutex mu_; |
| 4023 |
}; |
| 4024 |
|
| 4025 |
|
| 4026 |
class Bar { |
| 4027 |
public: |
| 4028 |
bool c; |
| 4029 |
Foo* foo; |
| 4030 |
Bar(Foo* f) : foo(f) { } |
| 4031 |
|
| 4032 |
struct Nested { |
| 4033 |
Foo* foo; |
| 4034 |
Nested(Foo* f) : foo(f) { } |
| 4035 |
|
| 4036 |
void unlockFoo() UNLOCK_FUNCTION(&Foo::mu_); |
| 4037 |
}; |
| 4038 |
|
| 4039 |
void test(); |
| 4040 |
}; |
| 4041 |
|
| 4042 |
|
| 4043 |
void Bar::test() { |
| 4044 |
foo->mu_.Lock(); |
| 4045 |
if (c) { |
| 4046 |
Nested *n = new Nested(foo); |
| 4047 |
n->unlockFoo(); |
| 4048 |
} |
| 4049 |
else { |
| 4050 |
foo->mu_.Unlock(); |
| 4051 |
} |
| 4052 |
} |
| 4053 |
|
| 4054 |
}; // end namespace ExprMatchingBugfix |
| 4055 |
|
| 4056 |
|
| 4057 |
namespace ComplexNameTest { |
| 4058 |
|
| 4059 |
class Foo { |
| 4060 |
public: |
| 4061 |
static Mutex mu_; |
| 4062 |
|
| 4063 |
Foo() EXCLUSIVE_LOCKS_REQUIRED(mu_) { } |
| 4064 |
~Foo() EXCLUSIVE_LOCKS_REQUIRED(mu_) { } |
| 4065 |
|
| 4066 |
int operator[](int i) EXCLUSIVE_LOCKS_REQUIRED(mu_) { return 0; } |
| 4067 |
}; |
| 4068 |
|
| 4069 |
class Bar { |
| 4070 |
public: |
| 4071 |
static Mutex mu_; |
| 4072 |
|
| 4073 |
Bar() LOCKS_EXCLUDED(mu_) { } |
| 4074 |
~Bar() LOCKS_EXCLUDED(mu_) { } |
| 4075 |
|
| 4076 |
int operator[](int i) LOCKS_EXCLUDED(mu_) { return 0; } |
| 4077 |
}; |
| 4078 |
|
| 4079 |
|
| 4080 |
void test1() { |
| 4081 |
Foo f; // expected-warning {{calling function 'Foo' requires holding mutex 'mu_' exclusively}} |
| 4082 |
int a = f[0]; // expected-warning {{calling function 'operator[]' requires holding mutex 'mu_' exclusively}} |
| 4083 |
} // expected-warning {{calling function '~Foo' requires holding mutex 'mu_' exclusively}} |
| 4084 |
|
| 4085 |
|
| 4086 |
void test2() { |
| 4087 |
Bar::mu_.Lock(); |
| 4088 |
{ |
| 4089 |
Bar b; // expected-warning {{cannot call function 'Bar' while mutex 'mu_' is held}} |
| 4090 |
int a = b[0]; // expected-warning {{cannot call function 'operator[]' while mutex 'mu_' is held}} |
| 4091 |
} // expected-warning {{cannot call function '~Bar' while mutex 'mu_' is held}} |
| 4092 |
Bar::mu_.Unlock(); |
| 4093 |
} |
| 4094 |
|
| 4095 |
}; // end namespace ComplexNameTest |
| 4096 |
|
| 4097 |
|
| 4098 |
namespace UnreachableExitTest { |
| 4099 |
|
| 4100 |
class FemmeFatale { |
| 4101 |
public: |
| 4102 |
FemmeFatale(); |
| 4103 |
~FemmeFatale() __attribute__((noreturn)); |
| 4104 |
}; |
| 4105 |
|
| 4106 |
void exitNow() __attribute__((noreturn)); |
| 4107 |
void exitDestruct(const MyString& ms) __attribute__((noreturn)); |
| 4108 |
|
| 4109 |
Mutex fatalmu_; |
| 4110 |
|
| 4111 |
void test1() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) { |
| 4112 |
exitNow(); |
| 4113 |
} |
| 4114 |
|
| 4115 |
void test2() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) { |
| 4116 |
FemmeFatale femme; |
| 4117 |
} |
| 4118 |
|
| 4119 |
bool c; |
| 4120 |
|
| 4121 |
void test3() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) { |
| 4122 |
if (c) { |
| 4123 |
exitNow(); |
| 4124 |
} |
| 4125 |
else { |
| 4126 |
FemmeFatale femme; |
| 4127 |
} |
| 4128 |
} |
| 4129 |
|
| 4130 |
void test4() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) { |
| 4131 |
exitDestruct("foo"); |
| 4132 |
} |
| 4133 |
|
| 4134 |
} // end namespace UnreachableExitTest |
| 4135 |
|
| 4136 |
|
| 4137 |
namespace VirtualMethodCanonicalizationTest { |
| 4138 |
|
| 4139 |
class Base { |
| 4140 |
public: |
| 4141 |
virtual Mutex* getMutex() = 0; |
| 4142 |
}; |
| 4143 |
|
| 4144 |
class Base2 : public Base { |
| 4145 |
public: |
| 4146 |
Mutex* getMutex(); |
| 4147 |
}; |
| 4148 |
|
| 4149 |
class Base3 : public Base2 { |
| 4150 |
public: |
| 4151 |
Mutex* getMutex(); |
| 4152 |
}; |
| 4153 |
|
| 4154 |
class Derived : public Base3 { |
| 4155 |
public: |
| 4156 |
Mutex* getMutex(); // overrides Base::getMutex() |
| 4157 |
}; |
| 4158 |
|
| 4159 |
void baseFun(Base *b) EXCLUSIVE_LOCKS_REQUIRED(b->getMutex()) { } |
| 4160 |
|
| 4161 |
void derivedFun(Derived *d) EXCLUSIVE_LOCKS_REQUIRED(d->getMutex()) { |
| 4162 |
baseFun(d); |
| 4163 |
} |
| 4164 |
|
| 4165 |
} // end namespace VirtualMethodCanonicalizationTest |
| 4166 |
|
| 4167 |
|
| 4168 |
namespace TemplateFunctionParamRemapTest { |
| 4169 |
|
| 4170 |
template <class T> |
| 4171 |
struct Cell { |
| 4172 |
T dummy_; |
| 4173 |
Mutex* mu_; |
| 4174 |
}; |
| 4175 |
|
| 4176 |
class Foo { |
| 4177 |
public: |
| 4178 |
template <class T> |
| 4179 |
void elr(Cell<T>* c) EXCLUSIVE_LOCKS_REQUIRED(c->mu_); |
| 4180 |
|
| 4181 |
void test(); |
| 4182 |
}; |
| 4183 |
|
| 4184 |
template<class T> |
| 4185 |
void Foo::elr(Cell<T>* c1) { } |
| 4186 |
|
| 4187 |
void Foo::test() { |
| 4188 |
Cell<int> cell; |
| 4189 |
elr(&cell); // \ |
| 4190 |
// expected-warning {{calling function 'elr<int>' requires holding mutex 'cell.mu_' exclusively}} |
| 4191 |
} |
| 4192 |
|
| 4193 |
|
| 4194 |
template<class T> |
| 4195 |
void globalELR(Cell<T>* c) EXCLUSIVE_LOCKS_REQUIRED(c->mu_); |
| 4196 |
|
| 4197 |
template<class T> |
| 4198 |
void globalELR(Cell<T>* c1) { } |
| 4199 |
|
| 4200 |
void globalTest() { |
| 4201 |
Cell<int> cell; |
| 4202 |
globalELR(&cell); // \ |
| 4203 |
// expected-warning {{calling function 'globalELR<int>' requires holding mutex 'cell.mu_' exclusively}} |
| 4204 |
} |
| 4205 |
|
| 4206 |
|
| 4207 |
template<class T> |
| 4208 |
void globalELR2(Cell<T>* c) EXCLUSIVE_LOCKS_REQUIRED(c->mu_); |
| 4209 |
|
| 4210 |
// second declaration |
| 4211 |
template<class T> |
| 4212 |
void globalELR2(Cell<T>* c2); |
| 4213 |
|
| 4214 |
template<class T> |
| 4215 |
void globalELR2(Cell<T>* c3) { } |
| 4216 |
|
| 4217 |
// re-declaration after definition |
| 4218 |
template<class T> |
| 4219 |
void globalELR2(Cell<T>* c4); |
| 4220 |
|
| 4221 |
void globalTest2() { |
| 4222 |
Cell<int> cell; |
| 4223 |
globalELR2(&cell); // \ |
| 4224 |
// expected-warning {{calling function 'globalELR2<int>' requires holding mutex 'cell.mu_' exclusively}} |
| 4225 |
} |
| 4226 |
|
| 4227 |
|
| 4228 |
template<class T> |
| 4229 |
class FooT { |
| 4230 |
public: |
| 4231 |
void elr(Cell<T>* c) EXCLUSIVE_LOCKS_REQUIRED(c->mu_); |
| 4232 |
}; |
| 4233 |
|
| 4234 |
template<class T> |
| 4235 |
void FooT<T>::elr(Cell<T>* c1) { } |
| 4236 |
|
| 4237 |
void testFooT() { |
| 4238 |
Cell<int> cell; |
| 4239 |
FooT<int> foo; |
| 4240 |
foo.elr(&cell); // \ |
| 4241 |
// expected-warning {{calling function 'elr' requires holding mutex 'cell.mu_' exclusively}} |
| 4242 |
} |
| 4243 |
|
| 4244 |
} // end namespace TemplateFunctionParamRemapTest |
| 4245 |
|
| 4246 |
|
| 4247 |
namespace SelfConstructorTest { |
| 4248 |
|
| 4249 |
class SelfLock { |
| 4250 |
public: |
| 4251 |
SelfLock() EXCLUSIVE_LOCK_FUNCTION(mu_); |
| 4252 |
~SelfLock() UNLOCK_FUNCTION(mu_); |
| 4253 |
|
| 4254 |
void foo() EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 4255 |
|
| 4256 |
Mutex mu_; |
| 4257 |
}; |
| 4258 |
|
| 4259 |
class LOCKABLE SelfLock2 { |
| 4260 |
public: |
| 4261 |
SelfLock2() EXCLUSIVE_LOCK_FUNCTION(); |
| 4262 |
~SelfLock2() UNLOCK_FUNCTION(); |
| 4263 |
|
| 4264 |
void foo() EXCLUSIVE_LOCKS_REQUIRED(this); |
| 4265 |
}; |
| 4266 |
|
| 4267 |
class SelfLockDeferred { |
| 4268 |
public: |
| 4269 |
SelfLockDeferred() LOCKS_EXCLUDED(mu_); |
| 4270 |
~SelfLockDeferred() UNLOCK_FUNCTION(mu_); |
| 4271 |
|
| 4272 |
Mutex mu_; |
| 4273 |
}; |
| 4274 |
|
| 4275 |
class LOCKABLE SelfLockDeferred2 { |
| 4276 |
public: |
| 4277 |
SelfLockDeferred2() LOCKS_EXCLUDED(this); |
| 4278 |
~SelfLockDeferred2() UNLOCK_FUNCTION(); |
| 4279 |
}; |
| 4280 |
|
| 4281 |
|
| 4282 |
void test() { |
| 4283 |
SelfLock s; |
| 4284 |
s.foo(); |
| 4285 |
} |
| 4286 |
|
| 4287 |
void test2() { |
| 4288 |
SelfLock2 s2; |
| 4289 |
s2.foo(); |
| 4290 |
} |
| 4291 |
|
| 4292 |
void testDeferredTemporary() { |
| 4293 |
SelfLockDeferred(); // expected-warning {{releasing mutex '<temporary>.mu_' that was not held}} |
| 4294 |
} |
| 4295 |
|
| 4296 |
void testDeferredTemporary2() { |
| 4297 |
SelfLockDeferred2(); // expected-warning {{releasing mutex '<temporary>' that was not held}} |
| 4298 |
} |
| 4299 |
|
| 4300 |
} // end namespace SelfConstructorTest |
| 4301 |
|
| 4302 |
|
| 4303 |
namespace MultipleAttributeTest { |
| 4304 |
|
| 4305 |
class Foo { |
| 4306 |
Mutex mu1_; |
| 4307 |
Mutex mu2_; |
| 4308 |
int a GUARDED_BY(mu1_); |
| 4309 |
int b GUARDED_BY(mu2_); |
| 4310 |
int c GUARDED_BY(mu1_) GUARDED_BY(mu2_); |
| 4311 |
int* d PT_GUARDED_BY(mu1_) PT_GUARDED_BY(mu2_); |
| 4312 |
|
| 4313 |
void foo1() EXCLUSIVE_LOCKS_REQUIRED(mu1_) |
| 4314 |
EXCLUSIVE_LOCKS_REQUIRED(mu2_); |
| 4315 |
void foo2() SHARED_LOCKS_REQUIRED(mu1_) |
| 4316 |
SHARED_LOCKS_REQUIRED(mu2_); |
| 4317 |
void foo3() LOCKS_EXCLUDED(mu1_) |
| 4318 |
LOCKS_EXCLUDED(mu2_); |
| 4319 |
void lock() EXCLUSIVE_LOCK_FUNCTION(mu1_) |
| 4320 |
EXCLUSIVE_LOCK_FUNCTION(mu2_); |
| 4321 |
void readerlock() SHARED_LOCK_FUNCTION(mu1_) |
| 4322 |
SHARED_LOCK_FUNCTION(mu2_); |
| 4323 |
void unlock() UNLOCK_FUNCTION(mu1_) |
| 4324 |
UNLOCK_FUNCTION(mu2_); |
| 4325 |
bool trylock() EXCLUSIVE_TRYLOCK_FUNCTION(true, mu1_) |
| 4326 |
EXCLUSIVE_TRYLOCK_FUNCTION(true, mu2_); |
| 4327 |
bool readertrylock() SHARED_TRYLOCK_FUNCTION(true, mu1_) |
| 4328 |
SHARED_TRYLOCK_FUNCTION(true, mu2_); |
| 4329 |
void assertBoth() ASSERT_EXCLUSIVE_LOCK(mu1_) |
| 4330 |
ASSERT_EXCLUSIVE_LOCK(mu2_); |
| 4331 |
|
| 4332 |
void alsoAssertBoth() ASSERT_EXCLUSIVE_LOCK(mu1_, mu2_); |
| 4333 |
|
| 4334 |
void assertShared() ASSERT_SHARED_LOCK(mu1_) |
| 4335 |
ASSERT_SHARED_LOCK(mu2_); |
| 4336 |
|
| 4337 |
void alsoAssertShared() ASSERT_SHARED_LOCK(mu1_, mu2_); |
| 4338 |
|
| 4339 |
void test(); |
| 4340 |
void testAssert(); |
| 4341 |
void testAssertShared(); |
| 4342 |
}; |
| 4343 |
|
| 4344 |
|
| 4345 |
void Foo::foo1() { |
| 4346 |
a = 1; |
| 4347 |
b = 2; |
| 4348 |
} |
| 4349 |
|
| 4350 |
void Foo::foo2() { |
| 4351 |
int result = a + b; |
| 4352 |
} |
| 4353 |
|
| 4354 |
void Foo::foo3() { } |
| 4355 |
void Foo::lock() { mu1_.Lock(); mu2_.Lock(); } |
| 4356 |
void Foo::readerlock() { mu1_.ReaderLock(); mu2_.ReaderLock(); } |
| 4357 |
void Foo::unlock() { mu1_.Unlock(); mu2_.Unlock(); } |
| 4358 |
bool Foo::trylock() { return true; } |
| 4359 |
bool Foo::readertrylock() { return true; } |
| 4360 |
|
| 4361 |
|
| 4362 |
void Foo::test() { |
| 4363 |
mu1_.Lock(); |
| 4364 |
foo1(); // expected-warning {{}} |
| 4365 |
c = 0; // expected-warning {{}} |
| 4366 |
*d = 0; // expected-warning {{}} |
| 4367 |
mu1_.Unlock(); |
| 4368 |
|
| 4369 |
mu1_.ReaderLock(); |
| 4370 |
foo2(); // expected-warning {{}} |
| 4371 |
int x = c; // expected-warning {{}} |
| 4372 |
int y = *d; // expected-warning {{}} |
| 4373 |
mu1_.Unlock(); |
| 4374 |
|
| 4375 |
mu2_.Lock(); |
| 4376 |
foo3(); // expected-warning {{}} |
| 4377 |
mu2_.Unlock(); |
| 4378 |
|
| 4379 |
lock(); |
| 4380 |
a = 0; |
| 4381 |
b = 0; |
| 4382 |
unlock(); |
| 4383 |
|
| 4384 |
readerlock(); |
| 4385 |
int z = a + b; |
| 4386 |
unlock(); |
| 4387 |
|
| 4388 |
if (trylock()) { |
| 4389 |
a = 0; |
| 4390 |
b = 0; |
| 4391 |
unlock(); |
| 4392 |
} |
| 4393 |
|
| 4394 |
if (readertrylock()) { |
| 4395 |
int zz = a + b; |
| 4396 |
unlock(); |
| 4397 |
} |
| 4398 |
} |
| 4399 |
|
| 4400 |
// Force duplication of attributes |
| 4401 |
void Foo::assertBoth() { } |
| 4402 |
void Foo::alsoAssertBoth() { } |
| 4403 |
void Foo::assertShared() { } |
| 4404 |
void Foo::alsoAssertShared() { } |
| 4405 |
|
| 4406 |
void Foo::testAssert() { |
| 4407 |
{ |
| 4408 |
assertBoth(); |
| 4409 |
a = 0; |
| 4410 |
b = 0; |
| 4411 |
} |
| 4412 |
{ |
| 4413 |
alsoAssertBoth(); |
| 4414 |
a = 0; |
| 4415 |
b = 0; |
| 4416 |
} |
| 4417 |
} |
| 4418 |
|
| 4419 |
void Foo::testAssertShared() { |
| 4420 |
{ |
| 4421 |
assertShared(); |
| 4422 |
int zz = a + b; |
| 4423 |
} |
| 4424 |
|
| 4425 |
{ |
| 4426 |
alsoAssertShared(); |
| 4427 |
int zz = a + b; |
| 4428 |
} |
| 4429 |
} |
| 4430 |
|
| 4431 |
|
| 4432 |
} // end namespace MultipleAttributeTest |
| 4433 |
|
| 4434 |
|
| 4435 |
namespace GuardedNonPrimitiveTypeTest { |
| 4436 |
|
| 4437 |
|
| 4438 |
class Data { |
| 4439 |
public: |
| 4440 |
Data(int i) : dat(i) { } |
| 4441 |
|
| 4442 |
int getValue() const { return dat; } |
| 4443 |
void setValue(int i) { dat = i; } |
| 4444 |
|
| 4445 |
int operator[](int i) const { return dat; } |
| 4446 |
int& operator[](int i) { return dat; } |
| 4447 |
|
| 4448 |
void operator()() { } |
| 4449 |
|
| 4450 |
Data& operator+=(int); |
| 4451 |
Data& operator-=(int); |
| 4452 |
Data& operator*=(int); |
| 4453 |
Data& operator/=(int); |
| 4454 |
Data& operator%=(int); |
| 4455 |
Data& operator^=(int); |
| 4456 |
Data& operator&=(int); |
| 4457 |
Data& operator|=(int); |
| 4458 |
Data& operator<<=(int); |
| 4459 |
Data& operator>>=(int); |
| 4460 |
Data& operator++(); |
| 4461 |
Data& operator++(int); |
| 4462 |
Data& operator--(); |
| 4463 |
Data& operator--(int); |
| 4464 |
|
| 4465 |
private: |
| 4466 |
int dat; |
| 4467 |
}; |
| 4468 |
|
| 4469 |
|
| 4470 |
class DataCell { |
| 4471 |
public: |
| 4472 |
DataCell(const Data& d) : dat(d) { } |
| 4473 |
|
| 4474 |
private: |
| 4475 |
Data dat; |
| 4476 |
}; |
| 4477 |
|
| 4478 |
|
| 4479 |
void showDataCell(const DataCell& dc); |
| 4480 |
|
| 4481 |
|
| 4482 |
class Foo { |
| 4483 |
public: |
| 4484 |
// method call tests |
| 4485 |
void test() { |
| 4486 |
data_.setValue(0); // FIXME -- should be writing \ |
| 4487 |
// expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} |
| 4488 |
int a = data_.getValue(); // \ |
| 4489 |
// expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} |
| 4490 |
|
| 4491 |
datap1_->setValue(0); // FIXME -- should be writing \ |
| 4492 |
// expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}} |
| 4493 |
a = datap1_->getValue(); // \ |
| 4494 |
// expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}} |
| 4495 |
|
| 4496 |
datap2_->setValue(0); // FIXME -- should be writing \ |
| 4497 |
// expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} |
| 4498 |
a = datap2_->getValue(); // \ |
| 4499 |
// expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} |
| 4500 |
|
| 4501 |
(*datap2_).setValue(0); // FIXME -- should be writing \ |
| 4502 |
// expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} |
| 4503 |
a = (*datap2_).getValue(); // \ |
| 4504 |
// expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} |
| 4505 |
|
| 4506 |
mu_.Lock(); |
| 4507 |
data_.setValue(1); |
| 4508 |
datap1_->setValue(1); |
| 4509 |
datap2_->setValue(1); |
| 4510 |
mu_.Unlock(); |
| 4511 |
|
| 4512 |
mu_.ReaderLock(); |
| 4513 |
a = data_.getValue(); |
| 4514 |
datap1_->setValue(0); // reads datap1_, writes *datap1_ |
| 4515 |
a = datap1_->getValue(); |
| 4516 |
a = datap2_->getValue(); |
| 4517 |
mu_.Unlock(); |
| 4518 |
} |
| 4519 |
|
| 4520 |
// operator tests |
| 4521 |
void test2() { |
| 4522 |
data_ = Data(1); // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} |
| 4523 |
*datap1_ = data_; // expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}} \ |
| 4524 |
// expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} |
| 4525 |
*datap2_ = data_; // expected-warning {{writing the value pointed to by 'datap2_' requires holding mutex 'mu_' exclusively}} \ |
| 4526 |
// expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} |
| 4527 |
data_ = *datap1_; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} \ |
| 4528 |
// expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}} |
| 4529 |
data_ = *datap2_; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} \ |
| 4530 |
// expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} |
| 4531 |
data_ += 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} |
| 4532 |
data_ -= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} |
| 4533 |
data_ *= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} |
| 4534 |
data_ /= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} |
| 4535 |
data_ %= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} |
| 4536 |
data_ ^= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} |
| 4537 |
data_ &= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} |
| 4538 |
data_ |= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} |
| 4539 |
data_ <<= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} |
| 4540 |
data_ >>= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} |
| 4541 |
++data_; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} |
| 4542 |
data_++; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} |
| 4543 |
--data_; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} |
| 4544 |
data_--; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} |
| 4545 |
|
| 4546 |
data_[0] = 0; // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} |
| 4547 |
(*datap2_)[0] = 0; // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} |
| 4548 |
|
| 4549 |
data_(); // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} |
| 4550 |
} |
| 4551 |
|
| 4552 |
// const operator tests |
| 4553 |
void test3() const { |
| 4554 |
Data mydat(data_); // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} |
| 4555 |
|
| 4556 |
//FIXME |
| 4557 |
//showDataCell(data_); // xpected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} |
| 4558 |
//showDataCell(*datap2_); // xpected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} |
| 4559 |
|
| 4560 |
int a = data_[0]; // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} |
| 4561 |
} |
| 4562 |
|
| 4563 |
private: |
| 4564 |
Mutex mu_; |
| 4565 |
Data data_ GUARDED_BY(mu_); |
| 4566 |
Data* datap1_ GUARDED_BY(mu_); |
| 4567 |
Data* datap2_ PT_GUARDED_BY(mu_); |
| 4568 |
}; |
| 4569 |
|
| 4570 |
} // end namespace GuardedNonPrimitiveTypeTest |
| 4571 |
|
| 4572 |
|
| 4573 |
namespace GuardedNonPrimitive_MemberAccess { |
| 4574 |
|
| 4575 |
class Cell { |
| 4576 |
public: |
| 4577 |
Cell(int i); |
| 4578 |
|
| 4579 |
void cellMethod(); |
| 4580 |
|
| 4581 |
int a; |
| 4582 |
}; |
| 4583 |
|
| 4584 |
|
| 4585 |
class Foo { |
| 4586 |
public: |
| 4587 |
int a; |
| 4588 |
Cell c GUARDED_BY(cell_mu_); |
| 4589 |
Cell* cp PT_GUARDED_BY(cell_mu_); |
| 4590 |
|
| 4591 |
void myMethod(); |
| 4592 |
|
| 4593 |
Mutex cell_mu_; |
| 4594 |
}; |
| 4595 |
|
| 4596 |
|
| 4597 |
class Bar { |
| 4598 |
private: |
| 4599 |
Mutex mu_; |
| 4600 |
Foo foo GUARDED_BY(mu_); |
| 4601 |
Foo* foop PT_GUARDED_BY(mu_); |
| 4602 |
|
| 4603 |
void test() { |
| 4604 |
foo.myMethod(); // expected-warning {{reading variable 'foo' requires holding mutex 'mu_'}} |
| 4605 |
|
| 4606 |
int fa = foo.a; // expected-warning {{reading variable 'foo' requires holding mutex 'mu_'}} |
| 4607 |
foo.a = fa; // expected-warning {{writing variable 'foo' requires holding mutex 'mu_' exclusively}} |
| 4608 |
|
| 4609 |
fa = foop->a; // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}} |
| 4610 |
foop->a = fa; // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_' exclusively}} |
| 4611 |
|
| 4612 |
fa = (*foop).a; // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}} |
| 4613 |
(*foop).a = fa; // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_' exclusively}} |
| 4614 |
|
| 4615 |
foo.c = Cell(0); // expected-warning {{writing variable 'foo' requires holding mutex 'mu_'}} \ |
| 4616 |
// expected-warning {{writing variable 'c' requires holding mutex 'foo.cell_mu_' exclusively}} |
| 4617 |
foo.c.cellMethod(); // expected-warning {{reading variable 'foo' requires holding mutex 'mu_'}} \ |
| 4618 |
// expected-warning {{reading variable 'c' requires holding mutex 'foo.cell_mu_'}} |
| 4619 |
|
| 4620 |
foop->c = Cell(0); // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_'}} \ |
| 4621 |
// expected-warning {{writing variable 'c' requires holding mutex 'foop->cell_mu_' exclusively}} |
| 4622 |
foop->c.cellMethod(); // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}} \ |
| 4623 |
// expected-warning {{reading variable 'c' requires holding mutex 'foop->cell_mu_'}} |
| 4624 |
|
| 4625 |
(*foop).c = Cell(0); // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_'}} \ |
| 4626 |
// expected-warning {{writing variable 'c' requires holding mutex 'foop->cell_mu_' exclusively}} |
| 4627 |
(*foop).c.cellMethod(); // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}} \ |
| 4628 |
// expected-warning {{reading variable 'c' requires holding mutex 'foop->cell_mu_'}} |
| 4629 |
}; |
| 4630 |
}; |
| 4631 |
|
| 4632 |
} // namespace GuardedNonPrimitive_MemberAccess |
| 4633 |
|
| 4634 |
|
| 4635 |
namespace TestThrowExpr { |
| 4636 |
|
| 4637 |
class Foo { |
| 4638 |
Mutex mu_; |
| 4639 |
|
| 4640 |
bool hasError(); |
| 4641 |
|
| 4642 |
void test() { |
| 4643 |
mu_.Lock(); |
| 4644 |
if (hasError()) { |
| 4645 |
throw "ugly"; |
| 4646 |
} |
| 4647 |
mu_.Unlock(); |
| 4648 |
} |
| 4649 |
}; |
| 4650 |
|
| 4651 |
} // end namespace TestThrowExpr |
| 4652 |
|
| 4653 |
|
| 4654 |
namespace UnevaluatedContextTest { |
| 4655 |
|
| 4656 |
// parse attribute expressions in an unevaluated context. |
| 4657 |
|
| 4658 |
static inline Mutex* getMutex1(); |
| 4659 |
static inline Mutex* getMutex2(); |
| 4660 |
|
| 4661 |
void bar() EXCLUSIVE_LOCKS_REQUIRED(getMutex1()); |
| 4662 |
|
| 4663 |
void bar2() EXCLUSIVE_LOCKS_REQUIRED(getMutex1(), getMutex2()); |
| 4664 |
|
| 4665 |
} // end namespace UnevaluatedContextTest |
| 4666 |
|
| 4667 |
|
| 4668 |
namespace LockUnlockFunctionTest { |
| 4669 |
|
| 4670 |
// Check built-in lock functions |
| 4671 |
class LOCKABLE MyLockable { |
| 4672 |
public: |
| 4673 |
void lock() EXCLUSIVE_LOCK_FUNCTION() { mu_.Lock(); } |
| 4674 |
void readerLock() SHARED_LOCK_FUNCTION() { mu_.ReaderLock(); } |
| 4675 |
void unlock() UNLOCK_FUNCTION() { mu_.Unlock(); } |
| 4676 |
|
| 4677 |
private: |
| 4678 |
Mutex mu_; |
| 4679 |
}; |
| 4680 |
|
| 4681 |
|
| 4682 |
class Foo { |
| 4683 |
public: |
| 4684 |
// Correct lock/unlock functions |
| 4685 |
void lock() EXCLUSIVE_LOCK_FUNCTION(mu_) { |
| 4686 |
mu_.Lock(); |
| 4687 |
} |
| 4688 |
|
| 4689 |
void readerLock() SHARED_LOCK_FUNCTION(mu_) { |
| 4690 |
mu_.ReaderLock(); |
| 4691 |
} |
| 4692 |
|
| 4693 |
void unlock() UNLOCK_FUNCTION(mu_) { |
| 4694 |
mu_.Unlock(); |
| 4695 |
} |
| 4696 |
|
| 4697 |
void unlockExclusive() EXCLUSIVE_UNLOCK_FUNCTION(mu_) { |
| 4698 |
mu_.Unlock(); |
| 4699 |
} |
| 4700 |
|
| 4701 |
void unlockShared() SHARED_UNLOCK_FUNCTION(mu_) { |
| 4702 |
mu_.ReaderUnlock(); |
| 4703 |
} |
| 4704 |
|
| 4705 |
// Check failure to lock. |
| 4706 |
void lockBad() EXCLUSIVE_LOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} |
| 4707 |
mu2_.Lock(); |
| 4708 |
mu2_.Unlock(); |
| 4709 |
} // expected-warning {{expecting mutex 'mu_' to be held at the end of function}} |
| 4710 |
|
| 4711 |
void readerLockBad() SHARED_LOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} |
| 4712 |
mu2_.Lock(); |
| 4713 |
mu2_.Unlock(); |
| 4714 |
} // expected-warning {{expecting mutex 'mu_' to be held at the end of function}} |
| 4715 |
|
| 4716 |
void unlockBad() UNLOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} |
| 4717 |
mu2_.Lock(); |
| 4718 |
mu2_.Unlock(); |
| 4719 |
} // expected-warning {{mutex 'mu_' is still held at the end of function}} |
| 4720 |
|
| 4721 |
// Check locking the wrong thing. |
| 4722 |
void lockBad2() EXCLUSIVE_LOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} |
| 4723 |
mu2_.Lock(); // expected-note {{mutex acquired here}} |
| 4724 |
} // expected-warning {{expecting mutex 'mu_' to be held at the end of function}} \ |
| 4725 |
// expected-warning {{mutex 'mu2_' is still held at the end of function}} |
| 4726 |
|
| 4727 |
|
| 4728 |
void readerLockBad2() SHARED_LOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} |
| 4729 |
mu2_.ReaderLock(); // expected-note {{mutex acquired here}} |
| 4730 |
} // expected-warning {{expecting mutex 'mu_' to be held at the end of function}} \ |
| 4731 |
// expected-warning {{mutex 'mu2_' is still held at the end of function}} |
| 4732 |
|
| 4733 |
|
| 4734 |
void unlockBad2() UNLOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} |
| 4735 |
mu2_.Unlock(); // expected-warning {{releasing mutex 'mu2_' that was not held}} |
| 4736 |
} // expected-warning {{mutex 'mu_' is still held at the end of function}} |
| 4737 |
|
| 4738 |
private: |
| 4739 |
Mutex mu_; |
| 4740 |
Mutex mu2_; |
| 4741 |
}; |
| 4742 |
|
| 4743 |
} // end namespace LockUnlockFunctionTest |
| 4744 |
|
| 4745 |
|
| 4746 |
namespace AssertHeldTest { |
| 4747 |
|
| 4748 |
class Foo { |
| 4749 |
public: |
| 4750 |
int c; |
| 4751 |
int a GUARDED_BY(mu_); |
| 4752 |
Mutex mu_; |
| 4753 |
|
| 4754 |
void test1() { |
| 4755 |
mu_.AssertHeld(); |
| 4756 |
int b = a; |
| 4757 |
a = 0; |
| 4758 |
} |
| 4759 |
|
| 4760 |
void test2() { |
| 4761 |
mu_.AssertReaderHeld(); |
| 4762 |
int b = a; |
| 4763 |
a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} |
| 4764 |
} |
| 4765 |
|
| 4766 |
void test3() { |
| 4767 |
if (c) { |
| 4768 |
mu_.AssertHeld(); |
| 4769 |
} |
| 4770 |
else { |
| 4771 |
mu_.AssertHeld(); |
| 4772 |
} |
| 4773 |
int b = a; |
| 4774 |
a = 0; |
| 4775 |
} |
| 4776 |
|
| 4777 |
void test4() EXCLUSIVE_LOCKS_REQUIRED(mu_) { |
| 4778 |
mu_.AssertHeld(); |
| 4779 |
int b = a; |
| 4780 |
a = 0; |
| 4781 |
} |
| 4782 |
|
| 4783 |
void test5() UNLOCK_FUNCTION(mu_) { |
| 4784 |
mu_.AssertHeld(); |
| 4785 |
mu_.Unlock(); |
| 4786 |
} |
| 4787 |
|
| 4788 |
void test6() { |
| 4789 |
mu_.AssertHeld(); |
| 4790 |
mu_.Unlock(); // should this be a warning? |
| 4791 |
} |
| 4792 |
|
| 4793 |
void test7() { |
| 4794 |
if (c) { |
| 4795 |
mu_.AssertHeld(); |
| 4796 |
} |
| 4797 |
else { |
| 4798 |
mu_.Lock(); |
| 4799 |
} |
| 4800 |
int b = a; |
| 4801 |
a = 0; |
| 4802 |
mu_.Unlock(); |
| 4803 |
} |
| 4804 |
|
| 4805 |
void test8() { |
| 4806 |
if (c) { |
| 4807 |
mu_.Lock(); |
| 4808 |
} |
| 4809 |
else { |
| 4810 |
mu_.AssertHeld(); |
| 4811 |
} |
| 4812 |
// FIXME: should warn, because it's unclear whether we need to release or not. |
| 4813 |
int b = a; |
| 4814 |
a = 0; |
| 4815 |
mu_.Unlock(); // should this be a warning? |
| 4816 |
} |
| 4817 |
|
| 4818 |
void test9() { |
| 4819 |
if (c) { |
| 4820 |
mu_.AssertHeld(); |
| 4821 |
} |
| 4822 |
else { |
| 4823 |
mu_.Lock(); // expected-note {{mutex acquired here}} |
| 4824 |
} |
| 4825 |
} // expected-warning {{mutex 'mu_' is still held at the end of function}} |
| 4826 |
|
| 4827 |
void test10() { |
| 4828 |
if (c) { |
| 4829 |
mu_.Lock(); // expected-note {{mutex acquired here}} |
| 4830 |
} |
| 4831 |
else { |
| 4832 |
mu_.AssertHeld(); |
| 4833 |
} |
| 4834 |
} // expected-warning {{mutex 'mu_' is still held at the end of function}} |
| 4835 |
|
| 4836 |
void assertMu() ASSERT_EXCLUSIVE_LOCK(mu_); |
| 4837 |
|
| 4838 |
void test11() { |
| 4839 |
assertMu(); |
| 4840 |
int b = a; |
| 4841 |
a = 0; |
| 4842 |
} |
| 4843 |
|
| 4844 |
void test12() { |
| 4845 |
if (c) |
| 4846 |
mu_.ReaderLock(); // expected-warning {{mutex 'mu_' is acquired exclusively and shared in the same scope}} |
| 4847 |
else |
| 4848 |
mu_.AssertHeld(); // expected-note {{the other acquisition of mutex 'mu_' is here}} |
| 4849 |
// FIXME: should instead warn because it's unclear whether we need to release or not. |
| 4850 |
int b = a; |
| 4851 |
a = 0; |
| 4852 |
mu_.Unlock(); |
| 4853 |
} |
| 4854 |
|
| 4855 |
void test13() { |
| 4856 |
if (c) |
| 4857 |
mu_.Lock(); // expected-warning {{mutex 'mu_' is acquired exclusively and shared in the same scope}} |
| 4858 |
else |
| 4859 |
mu_.AssertReaderHeld(); // expected-note {{the other acquisition of mutex 'mu_' is here}} |
| 4860 |
// FIXME: should instead warn because it's unclear whether we need to release or not. |
| 4861 |
int b = a; |
| 4862 |
a = 0; |
| 4863 |
mu_.Unlock(); |
| 4864 |
} |
| 4865 |
}; |
| 4866 |
|
| 4867 |
} // end namespace AssertHeldTest |
| 4868 |
|
| 4869 |
|
| 4870 |
namespace LogicalConditionalTryLock { |
| 4871 |
|
| 4872 |
class Foo { |
| 4873 |
public: |
| 4874 |
Mutex mu; |
| 4875 |
int a GUARDED_BY(mu); |
| 4876 |
bool c; |
| 4877 |
|
| 4878 |
bool newc(); |
| 4879 |
|
| 4880 |
void test1() { |
| 4881 |
if (c && mu.TryLock()) { |
| 4882 |
a = 0; |
| 4883 |
mu.Unlock(); |
| 4884 |
} |
| 4885 |
} |
| 4886 |
|
| 4887 |
void test2() { |
| 4888 |
bool b = mu.TryLock(); |
| 4889 |
if (c && b) { |
| 4890 |
a = 0; |
| 4891 |
mu.Unlock(); |
| 4892 |
} |
| 4893 |
} |
| 4894 |
|
| 4895 |
void test3() { |
| 4896 |
if (c || !mu.TryLock()) |
| 4897 |
return; |
| 4898 |
a = 0; |
| 4899 |
mu.Unlock(); |
| 4900 |
} |
| 4901 |
|
| 4902 |
void test4() { |
| 4903 |
while (c && mu.TryLock()) { |
| 4904 |
a = 0; |
| 4905 |
c = newc(); |
| 4906 |
mu.Unlock(); |
| 4907 |
} |
| 4908 |
} |
| 4909 |
|
| 4910 |
void test5() { |
| 4911 |
while (c) { |
| 4912 |
if (newc() || !mu.TryLock()) |
| 4913 |
break; |
| 4914 |
a = 0; |
| 4915 |
mu.Unlock(); |
| 4916 |
} |
| 4917 |
} |
| 4918 |
|
| 4919 |
void test6() { |
| 4920 |
mu.Lock(); |
| 4921 |
do { |
| 4922 |
a = 0; |
| 4923 |
mu.Unlock(); |
| 4924 |
} while (newc() && mu.TryLock()); |
| 4925 |
} |
| 4926 |
|
| 4927 |
void test7() { |
| 4928 |
for (bool b = mu.TryLock(); c && b;) { |
| 4929 |
a = 0; |
| 4930 |
mu.Unlock(); |
| 4931 |
} |
| 4932 |
} |
| 4933 |
|
| 4934 |
void test8() { |
| 4935 |
if (c && newc() && mu.TryLock()) { |
| 4936 |
a = 0; |
| 4937 |
mu.Unlock(); |
| 4938 |
} |
| 4939 |
} |
| 4940 |
|
| 4941 |
void test9() { |
| 4942 |
if (!(c && newc() && mu.TryLock())) |
| 4943 |
return; |
| 4944 |
a = 0; |
| 4945 |
mu.Unlock(); |
| 4946 |
} |
| 4947 |
|
| 4948 |
void test10() { |
| 4949 |
if (!(c || !mu.TryLock())) { |
| 4950 |
a = 0; |
| 4951 |
mu.Unlock(); |
| 4952 |
} |
| 4953 |
} |
| 4954 |
}; |
| 4955 |
|
| 4956 |
} // end namespace LogicalConditionalTryLock |
| 4957 |
|
| 4958 |
|
| 4959 |
|
| 4960 |
namespace PtGuardedByTest { |
| 4961 |
|
| 4962 |
void doSomething(); |
| 4963 |
|
| 4964 |
class Cell { |
| 4965 |
public: |
| 4966 |
int a; |
| 4967 |
}; |
| 4968 |
|
| 4969 |
|
| 4970 |
// This mainly duplicates earlier tests, but just to make sure... |
| 4971 |
class PtGuardedByCorrectnessTest { |
| 4972 |
Mutex mu1; |
| 4973 |
Mutex mu2; |
| 4974 |
int* a GUARDED_BY(mu1) PT_GUARDED_BY(mu2); |
| 4975 |
Cell* c GUARDED_BY(mu1) PT_GUARDED_BY(mu2); |
| 4976 |
int sa[10] GUARDED_BY(mu1); |
| 4977 |
Cell sc[10] GUARDED_BY(mu1); |
| 4978 |
|
| 4979 |
static constexpr int Cell::*pa = &Cell::a; |
| 4980 |
|
| 4981 |
void test1() { |
| 4982 |
mu1.Lock(); |
| 4983 |
if (a == 0) doSomething(); // OK, we don't dereference. |
| 4984 |
a = 0; |
| 4985 |
c = 0; |
| 4986 |
if (sa[0] == 42) doSomething(); |
| 4987 |
sa[0] = 57; |
| 4988 |
if (sc[0].a == 42) doSomething(); |
| 4989 |
sc[0].a = 57; |
| 4990 |
mu1.Unlock(); |
| 4991 |
} |
| 4992 |
|
| 4993 |
void test2() { |
| 4994 |
mu1.ReaderLock(); |
| 4995 |
if (*a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'a' requires holding mutex 'mu2'}} |
| 4996 |
*a = 0; // expected-warning {{writing the value pointed to by 'a' requires holding mutex 'mu2' exclusively}} |
| 4997 |
|
| 4998 |
if (c->a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'c' requires holding mutex 'mu2'}} |
| 4999 |
c->a = 0; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}} |
| 5000 |
c->*pa = 0; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}} |
| 5001 |
|
| 5002 |
if ((*c).a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'c' requires holding mutex 'mu2'}} |
| 5003 |
(*c).a = 0; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}} |
| 5004 |
(*c).*pa = 0; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}} |
| 5005 |
|
| 5006 |
if (a[0] == 42) doSomething(); // expected-warning {{reading the value pointed to by 'a' requires holding mutex 'mu2'}} |
| 5007 |
a[0] = 57; // expected-warning {{writing the value pointed to by 'a' requires holding mutex 'mu2' exclusively}} |
| 5008 |
if (c[0].a == 42) doSomething(); // expected-warning {{reading the value pointed to by 'c' requires holding mutex 'mu2'}} |
| 5009 |
c[0].a = 57; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}} |
| 5010 |
mu1.Unlock(); |
| 5011 |
} |
| 5012 |
|
| 5013 |
void test3() { |
| 5014 |
mu2.Lock(); |
| 5015 |
if (*a == 0) doSomething(); // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}} |
| 5016 |
*a = 0; // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}} |
| 5017 |
|
| 5018 |
if (c->a == 0) doSomething(); // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} |
| 5019 |
c->a = 0; // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} |
| 5020 |
|
| 5021 |
if ((*c).a == 0) doSomething(); // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} |
| 5022 |
(*c).a = 0; // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} |
| 5023 |
|
| 5024 |
if (a[0] == 42) doSomething(); // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}} |
| 5025 |
a[0] = 57; // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}} |
| 5026 |
if (c[0].a == 42) doSomething(); // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} |
| 5027 |
c[0].a = 57; // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} |
| 5028 |
mu2.Unlock(); |
| 5029 |
} |
| 5030 |
|
| 5031 |
void test4() { // Literal arrays |
| 5032 |
if (sa[0] == 42) doSomething(); // expected-warning {{reading variable 'sa' requires holding mutex 'mu1'}} |
| 5033 |
sa[0] = 57; // expected-warning {{writing variable 'sa' requires holding mutex 'mu1' exclusively}} |
| 5034 |
if (sc[0].a == 42) doSomething(); // expected-warning {{reading variable 'sc' requires holding mutex 'mu1'}} |
| 5035 |
sc[0].a = 57; // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}} |
| 5036 |
sc[0].*pa = 57; // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}} |
| 5037 |
|
| 5038 |
if (*sa == 42) doSomething(); // expected-warning {{reading variable 'sa' requires holding mutex 'mu1'}} |
| 5039 |
*sa = 57; // expected-warning {{writing variable 'sa' requires holding mutex 'mu1' exclusively}} |
| 5040 |
if ((*sc).a == 42) doSomething(); // expected-warning {{reading variable 'sc' requires holding mutex 'mu1'}} |
| 5041 |
(*sc).a = 57; // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}} |
| 5042 |
if (sc->a == 42) doSomething(); // expected-warning {{reading variable 'sc' requires holding mutex 'mu1'}} |
| 5043 |
sc->a = 57; // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}} |
| 5044 |
} |
| 5045 |
|
| 5046 |
void test5() { |
| 5047 |
mu1.ReaderLock(); // OK -- correct use. |
| 5048 |
mu2.Lock(); |
| 5049 |
if (*a == 0) doSomething(); |
| 5050 |
*a = 0; |
| 5051 |
|
| 5052 |
if (c->a == 0) doSomething(); |
| 5053 |
c->a = 0; |
| 5054 |
|
| 5055 |
if ((*c).a == 0) doSomething(); |
| 5056 |
(*c).a = 0; |
| 5057 |
mu2.Unlock(); |
| 5058 |
mu1.Unlock(); |
| 5059 |
} |
| 5060 |
}; |
| 5061 |
|
| 5062 |
|
| 5063 |
class SmartPtr_PtGuardedBy_Test { |
| 5064 |
Mutex mu1; |
| 5065 |
Mutex mu2; |
| 5066 |
SmartPtr<int> sp GUARDED_BY(mu1) PT_GUARDED_BY(mu2); |
| 5067 |
SmartPtr<Cell> sq GUARDED_BY(mu1) PT_GUARDED_BY(mu2); |
| 5068 |
|
| 5069 |
static constexpr int Cell::*pa = &Cell::a; |
| 5070 |
|
| 5071 |
void test1() { |
| 5072 |
mu1.ReaderLock(); |
| 5073 |
mu2.Lock(); |
| 5074 |
|
| 5075 |
sp.get(); |
| 5076 |
if (*sp == 0) doSomething(); |
| 5077 |
*sp = 0; |
| 5078 |
sq->a = 0; |
| 5079 |
sq->*pa = 0; |
| 5080 |
|
| 5081 |
if (sp[0] == 0) doSomething(); |
| 5082 |
sp[0] = 0; |
| 5083 |
|
| 5084 |
mu2.Unlock(); |
| 5085 |
mu1.Unlock(); |
| 5086 |
} |
| 5087 |
|
| 5088 |
void test2() { |
| 5089 |
mu2.Lock(); |
| 5090 |
|
| 5091 |
sp.get(); // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}} |
| 5092 |
if (*sp == 0) doSomething(); // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}} |
| 5093 |
*sp = 0; // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}} |
| 5094 |
sq->a = 0; // expected-warning {{reading variable 'sq' requires holding mutex 'mu1'}} |
| 5095 |
sq->*pa = 0; // expected-warning {{reading variable 'sq' requires holding mutex 'mu1'}} |
| 5096 |
|
| 5097 |
if (sp[0] == 0) doSomething(); // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}} |
| 5098 |
sp[0] = 0; // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}} |
| 5099 |
if (sq[0].a == 0) doSomething(); // expected-warning {{reading variable 'sq' requires holding mutex 'mu1'}} |
| 5100 |
sq[0].a = 0; // expected-warning {{reading variable 'sq' requires holding mutex 'mu1'}} |
| 5101 |
|
| 5102 |
mu2.Unlock(); |
| 5103 |
} |
| 5104 |
|
| 5105 |
void test3() { |
| 5106 |
mu1.Lock(); |
| 5107 |
|
| 5108 |
sp.get(); |
| 5109 |
if (*sp == 0) doSomething(); // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}} |
| 5110 |
*sp = 0; // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}} |
| 5111 |
sq->a = 0; // expected-warning {{reading the value pointed to by 'sq' requires holding mutex 'mu2'}} |
| 5112 |
sq->*pa = 0; // expected-warning {{reading the value pointed to by 'sq' requires holding mutex 'mu2'}} |
| 5113 |
|
| 5114 |
if (sp[0] == 0) doSomething(); // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}} |
| 5115 |
sp[0] = 0; // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}} |
| 5116 |
if (sq[0].a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'sq' requires holding mutex 'mu2'}} |
| 5117 |
sq[0].a = 0; // expected-warning {{reading the value pointed to by 'sq' requires holding mutex 'mu2'}} |
| 5118 |
|
| 5119 |
mu1.Unlock(); |
| 5120 |
} |
| 5121 |
}; |
| 5122 |
|
| 5123 |
} // end namespace PtGuardedByTest |
| 5124 |
|
| 5125 |
|
| 5126 |
namespace NonMemberCalleeICETest { |
| 5127 |
|
| 5128 |
class A { |
| 5129 |
void Run() { |
| 5130 |
(RunHelper)(); // expected-warning {{calling function 'RunHelper' requires holding mutex 'M' exclusively}} |
| 5131 |
} |
| 5132 |
|
| 5133 |
void RunHelper() EXCLUSIVE_LOCKS_REQUIRED(M); |
| 5134 |
Mutex M; |
| 5135 |
}; |
| 5136 |
|
| 5137 |
} // end namespace NonMemberCalleeICETest |
| 5138 |
|
| 5139 |
|
| 5140 |
namespace pt_guard_attribute_type { |
| 5141 |
int i PT_GUARDED_BY(sls_mu); // expected-warning {{'pt_guarded_by' only applies to pointer types; type here is 'int'}} |
| 5142 |
int j PT_GUARDED_VAR; // expected-warning {{'pt_guarded_var' only applies to pointer types; type here is 'int'}} |
| 5143 |
|
| 5144 |
void test() { |
| 5145 |
int i PT_GUARDED_BY(sls_mu); // expected-warning {{'pt_guarded_by' attribute only applies to non-static data members and global variables}} |
| 5146 |
int j PT_GUARDED_VAR; // expected-warning {{'pt_guarded_var' attribute only applies to non-static data members and global variables}} |
| 5147 |
|
| 5148 |
typedef int PT_GUARDED_BY(sls_mu) bad1; // expected-warning {{'pt_guarded_by' attribute only applies to}} |
| 5149 |
typedef int PT_GUARDED_VAR bad2; // expected-warning {{'pt_guarded_var' attribute only applies to}} |
| 5150 |
} |
| 5151 |
} // end namespace pt_guard_attribute_type |
| 5152 |
|
| 5153 |
|
| 5154 |
namespace ThreadAttributesOnLambdas { |
| 5155 |
|
| 5156 |
class Foo { |
| 5157 |
Mutex mu_; |
| 5158 |
|
| 5159 |
void LockedFunction() EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 5160 |
|
| 5161 |
void test() { |
| 5162 |
auto func1 = [this]() EXCLUSIVE_LOCKS_REQUIRED(mu_) { |
| 5163 |
LockedFunction(); |
| 5164 |
}; |
| 5165 |
|
| 5166 |
auto func2 = [this]() NO_THREAD_SAFETY_ANALYSIS { |
| 5167 |
LockedFunction(); |
| 5168 |
}; |
| 5169 |
|
| 5170 |
auto func3 = [this]() EXCLUSIVE_LOCK_FUNCTION(mu_) { |
| 5171 |
mu_.Lock(); |
| 5172 |
}; |
| 5173 |
|
| 5174 |
func1(); // expected-warning {{calling function 'operator()' requires holding mutex 'mu_' exclusively}} |
| 5175 |
func2(); |
| 5176 |
func3(); |
| 5177 |
mu_.Unlock(); |
| 5178 |
} |
| 5179 |
}; |
| 5180 |
|
| 5181 |
} // end namespace ThreadAttributesOnLambdas |
| 5182 |
|
| 5183 |
|
| 5184 |
|
| 5185 |
namespace AttributeExpressionCornerCases { |
| 5186 |
|
| 5187 |
class Foo { |
| 5188 |
int a GUARDED_BY(getMu()); |
| 5189 |
|
| 5190 |
Mutex* getMu() LOCK_RETURNED(""); |
| 5191 |
Mutex* getUniv() LOCK_RETURNED("*"); |
| 5192 |
|
| 5193 |
void test1() { |
| 5194 |
a = 0; |
| 5195 |
} |
| 5196 |
|
| 5197 |
void test2() EXCLUSIVE_LOCKS_REQUIRED(getUniv()) { |
| 5198 |
a = 0; |
| 5199 |
} |
| 5200 |
|
| 5201 |
void foo(Mutex* mu) EXCLUSIVE_LOCKS_REQUIRED(mu); |
| 5202 |
|
| 5203 |
void test3() { |
| 5204 |
foo(nullptr); |
| 5205 |
} |
| 5206 |
}; |
| 5207 |
|
| 5208 |
|
| 5209 |
class MapTest { |
| 5210 |
struct MuCell { Mutex* mu; }; |
| 5211 |
|
| 5212 |
MyMap<MyString, Mutex*> map; |
| 5213 |
MyMap<MyString, MuCell> mapCell; |
| 5214 |
|
| 5215 |
int a GUARDED_BY(map["foo"]); |
| 5216 |
int b GUARDED_BY(mapCell["foo"].mu); |
| 5217 |
|
| 5218 |
void test() { |
| 5219 |
map["foo"]->Lock(); |
| 5220 |
a = 0; |
| 5221 |
map["foo"]->Unlock(); |
| 5222 |
} |
| 5223 |
|
| 5224 |
void test2() { |
| 5225 |
mapCell["foo"].mu->Lock(); |
| 5226 |
b = 0; |
| 5227 |
mapCell["foo"].mu->Unlock(); |
| 5228 |
} |
| 5229 |
}; |
| 5230 |
|
| 5231 |
|
| 5232 |
class PreciseSmartPtr { |
| 5233 |
SmartPtr<Mutex> mu; |
| 5234 |
int val GUARDED_BY(mu); |
| 5235 |
|
| 5236 |
static bool compare(PreciseSmartPtr& a, PreciseSmartPtr &b) { |
| 5237 |
a.mu->Lock(); |
| 5238 |
bool result = (a.val == b.val); // expected-warning {{reading variable 'val' requires holding mutex 'b.mu'}} \ |
| 5239 |
// expected-note {{found near match 'a.mu'}} |
| 5240 |
a.mu->Unlock(); |
| 5241 |
return result; |
| 5242 |
} |
| 5243 |
}; |
| 5244 |
|
| 5245 |
|
| 5246 |
class SmartRedeclare { |
| 5247 |
SmartPtr<Mutex> mu; |
| 5248 |
int val GUARDED_BY(mu); |
| 5249 |
|
| 5250 |
void test() EXCLUSIVE_LOCKS_REQUIRED(mu); |
| 5251 |
void test2() EXCLUSIVE_LOCKS_REQUIRED(mu.get()); |
| 5252 |
void test3() EXCLUSIVE_LOCKS_REQUIRED(mu.get()); |
| 5253 |
}; |
| 5254 |
|
| 5255 |
|
| 5256 |
void SmartRedeclare::test() EXCLUSIVE_LOCKS_REQUIRED(mu.get()) { |
| 5257 |
val = 0; |
| 5258 |
} |
| 5259 |
|
| 5260 |
void SmartRedeclare::test2() EXCLUSIVE_LOCKS_REQUIRED(mu) { |
| 5261 |
val = 0; |
| 5262 |
} |
| 5263 |
|
| 5264 |
void SmartRedeclare::test3() { |
| 5265 |
val = 0; |
| 5266 |
} |
| 5267 |
|
| 5268 |
|
| 5269 |
namespace CustomMutex { |
| 5270 |
|
| 5271 |
|
| 5272 |
class LOCKABLE BaseMutex { }; |
| 5273 |
class DerivedMutex : public BaseMutex { }; |
| 5274 |
|
| 5275 |
void customLock(const BaseMutex *m) EXCLUSIVE_LOCK_FUNCTION(m); |
| 5276 |
void customUnlock(const BaseMutex *m) UNLOCK_FUNCTION(m); |
| 5277 |
|
| 5278 |
static struct DerivedMutex custMu; |
| 5279 |
|
| 5280 |
static void doSomethingRequiringLock() EXCLUSIVE_LOCKS_REQUIRED(custMu) { } |
| 5281 |
|
| 5282 |
void customTest() { |
| 5283 |
customLock(reinterpret_cast<BaseMutex*>(&custMu)); // ignore casts |
| 5284 |
doSomethingRequiringLock(); |
| 5285 |
customUnlock(reinterpret_cast<BaseMutex*>(&custMu)); |
| 5286 |
} |
| 5287 |
|
| 5288 |
} // end namespace CustomMutex |
| 5289 |
|
| 5290 |
} // end AttributeExpressionCornerCases |
| 5291 |
|
| 5292 |
|
| 5293 |
namespace ScopedLockReturnedInvalid { |
| 5294 |
|
| 5295 |
class Opaque; |
| 5296 |
|
| 5297 |
Mutex* getMutex(Opaque* o) LOCK_RETURNED(""); |
| 5298 |
|
| 5299 |
void test(Opaque* o) { |
| 5300 |
MutexLock lock(getMutex(o)); |
| 5301 |
} |
| 5302 |
|
| 5303 |
} // end namespace ScopedLockReturnedInvalid |
| 5304 |
|
| 5305 |
|
| 5306 |
namespace NegativeRequirements { |
| 5307 |
|
| 5308 |
class Bar { |
| 5309 |
Mutex mu; |
| 5310 |
int a GUARDED_BY(mu); |
| 5311 |
|
| 5312 |
public: |
| 5313 |
void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) { |
| 5314 |
mu.Lock(); |
| 5315 |
a = 0; |
| 5316 |
mu.Unlock(); |
| 5317 |
} |
| 5318 |
}; |
| 5319 |
|
| 5320 |
|
| 5321 |
class Foo { |
| 5322 |
Mutex mu; |
| 5323 |
int a GUARDED_BY(mu); |
| 5324 |
|
| 5325 |
public: |
| 5326 |
void foo() { |
| 5327 |
mu.Lock(); // warning? needs !mu? |
| 5328 |
baz(); // expected-warning {{cannot call function 'baz' while mutex 'mu' is held}} |
| 5329 |
bar(); |
| 5330 |
mu.Unlock(); |
| 5331 |
} |
| 5332 |
|
| 5333 |
void bar() { |
| 5334 |
bar2(); // expected-warning {{calling function 'bar2' requires negative capability '!mu'}} |
| 5335 |
} |
| 5336 |
|
| 5337 |
void bar2() EXCLUSIVE_LOCKS_REQUIRED(!mu) { |
| 5338 |
baz(); |
| 5339 |
} |
| 5340 |
|
| 5341 |
void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) { |
| 5342 |
mu.Lock(); |
| 5343 |
a = 0; |
| 5344 |
mu.Unlock(); |
| 5345 |
} |
| 5346 |
|
| 5347 |
void test() { |
| 5348 |
Bar b; |
| 5349 |
b.baz(); // no warning -- in different class. |
| 5350 |
} |
| 5351 |
}; |
| 5352 |
|
| 5353 |
} // end namespace NegativeRequirements |
| 5354 |
|
| 5355 |
|
| 5356 |
namespace NegativeThreadRoles { |
| 5357 |
|
| 5358 |
typedef int __attribute__((capability("role"))) ThreadRole; |
| 5359 |
|
| 5360 |
void acquire(ThreadRole R) EXCLUSIVE_LOCK_FUNCTION(R) NO_THREAD_SAFETY_ANALYSIS {} |
| 5361 |
void release(ThreadRole R) UNLOCK_FUNCTION(R) NO_THREAD_SAFETY_ANALYSIS {} |
| 5362 |
|
| 5363 |
ThreadRole FlightControl, Logger; |
| 5364 |
|
| 5365 |
extern void enque_log_msg(const char *msg); |
| 5366 |
void log_msg(const char *msg) { |
| 5367 |
enque_log_msg(msg); |
| 5368 |
} |
| 5369 |
|
| 5370 |
void dispatch_log(const char *msg) __attribute__((requires_capability(!FlightControl))) {} |
| 5371 |
void dispatch_log2(const char *msg) __attribute__((requires_capability(Logger))) {} |
| 5372 |
|
| 5373 |
void flight_control_entry(void) __attribute__((requires_capability(FlightControl))) { |
| 5374 |
dispatch_log("wrong"); /* expected-warning {{cannot call function 'dispatch_log' while role 'FlightControl' is held}} */ |
| 5375 |
dispatch_log2("also wrong"); /* expected-warning {{calling function 'dispatch_log2' requires holding role 'Logger' exclusively}} */ |
| 5376 |
} |
| 5377 |
|
| 5378 |
void spawn_fake_flight_control_thread(void) { |
| 5379 |
acquire(FlightControl); |
| 5380 |
flight_control_entry(); |
| 5381 |
release(FlightControl); |
| 5382 |
} |
| 5383 |
|
| 5384 |
extern const char *deque_log_msg(void) __attribute__((requires_capability(Logger))); |
| 5385 |
void logger_entry(void) __attribute__((requires_capability(Logger))) |
| 5386 |
__attribute__((requires_capability(!FlightControl))) { |
| 5387 |
const char *msg; |
| 5388 |
|
| 5389 |
while ((msg = deque_log_msg())) { |
| 5390 |
dispatch_log(msg); |
| 5391 |
} |
| 5392 |
} |
| 5393 |
|
| 5394 |
void spawn_fake_logger_thread(void) __attribute__((requires_capability(!FlightControl))) { |
| 5395 |
acquire(Logger); |
| 5396 |
logger_entry(); |
| 5397 |
release(Logger); |
| 5398 |
} |
| 5399 |
|
| 5400 |
int main(void) __attribute__((requires_capability(!FlightControl))) { |
| 5401 |
spawn_fake_flight_control_thread(); |
| 5402 |
spawn_fake_logger_thread(); |
| 5403 |
|
| 5404 |
for (;;) |
| 5405 |
; /* Pretend to dispatch things. */ |
| 5406 |
|
| 5407 |
return 0; |
| 5408 |
} |
| 5409 |
|
| 5410 |
} // end namespace NegativeThreadRoles |
| 5411 |
|
| 5412 |
|
| 5413 |
namespace AssertSharedExclusive { |
| 5414 |
|
| 5415 |
void doSomething(); |
| 5416 |
|
| 5417 |
class Foo { |
| 5418 |
Mutex mu; |
| 5419 |
int a GUARDED_BY(mu); |
| 5420 |
|
| 5421 |
void test() SHARED_LOCKS_REQUIRED(mu) { |
| 5422 |
mu.AssertHeld(); |
| 5423 |
if (a > 0) |
| 5424 |
doSomething(); |
| 5425 |
} |
| 5426 |
}; |
| 5427 |
|
| 5428 |
} // end namespace AssertSharedExclusive |
| 5429 |
|
| 5430 |
|
| 5431 |
namespace RangeBasedForAndReferences { |
| 5432 |
|
| 5433 |
class Foo { |
| 5434 |
struct MyStruct { |
| 5435 |
int a; |
| 5436 |
}; |
| 5437 |
|
| 5438 |
Mutex mu; |
| 5439 |
int a GUARDED_BY(mu); |
| 5440 |
MyContainer<int> cntr GUARDED_BY(mu); |
| 5441 |
MyStruct s GUARDED_BY(mu); |
| 5442 |
int arr[10] GUARDED_BY(mu); |
| 5443 |
|
| 5444 |
void nonref_test() { |
| 5445 |
int b = a; // expected-warning {{reading variable 'a' requires holding mutex 'mu'}} |
| 5446 |
b = 0; // no warning |
| 5447 |
} |
| 5448 |
|
| 5449 |
void auto_test() { |
| 5450 |
auto b = a; // expected-warning {{reading variable 'a' requires holding mutex 'mu'}} |
| 5451 |
b = 0; // no warning |
| 5452 |
auto &c = a; // no warning |
| 5453 |
c = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} |
| 5454 |
} |
| 5455 |
|
| 5456 |
void ref_test() { |
| 5457 |
int &b = a; |
| 5458 |
int &c = b; |
| 5459 |
int &d = c; |
| 5460 |
b = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} |
| 5461 |
c = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} |
| 5462 |
d = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} |
| 5463 |
|
| 5464 |
MyStruct &rs = s; |
| 5465 |
rs.a = 0; // expected-warning {{writing variable 's' requires holding mutex 'mu' exclusively}} |
| 5466 |
|
| 5467 |
int (&rarr)[10] = arr; |
| 5468 |
rarr[2] = 0; // expected-warning {{writing variable 'arr' requires holding mutex 'mu' exclusively}} |
| 5469 |
} |
| 5470 |
|
| 5471 |
void ptr_test() { |
| 5472 |
int *b = &a; |
| 5473 |
*b = 0; // no expected warning yet |
| 5474 |
} |
| 5475 |
|
| 5476 |
void for_test() { |
| 5477 |
int total = 0; |
| 5478 |
for (int i : cntr) { // expected-warning2 {{reading variable 'cntr' requires holding mutex 'mu'}} |
| 5479 |
total += i; |
| 5480 |
} |
| 5481 |
} |
| 5482 |
}; |
| 5483 |
|
| 5484 |
|
| 5485 |
} // end namespace RangeBasedForAndReferences |
| 5486 |
|
| 5487 |
|
| 5488 |
|
| 5489 |
namespace PassByRefTest { |
| 5490 |
|
| 5491 |
class Foo { |
| 5492 |
public: |
| 5493 |
Foo() : a(0), b(0) { } |
| 5494 |
|
| 5495 |
int a; |
| 5496 |
int b; |
| 5497 |
|
| 5498 |
void operator+(const Foo& f); |
| 5499 |
|
| 5500 |
void operator[](const Foo& g); |
| 5501 |
|
| 5502 |
void operator()(); |
| 5503 |
}; |
| 5504 |
|
| 5505 |
template<class T> |
| 5506 |
T&& mymove(T& f); |
| 5507 |
|
| 5508 |
|
| 5509 |
// test top-level functions |
| 5510 |
void copy(Foo f); |
| 5511 |
void write1(Foo& f); |
| 5512 |
void write2(int a, Foo& f); |
| 5513 |
void read1(const Foo& f); |
| 5514 |
void read2(int a, const Foo& f); |
| 5515 |
void destroy(Foo&& f); |
| 5516 |
|
| 5517 |
void operator/(const Foo& f, const Foo& g); |
| 5518 |
void operator*(const Foo& f, const Foo& g); |
| 5519 |
|
| 5520 |
// Test constructors. |
| 5521 |
struct FooRead { |
| 5522 |
FooRead(const Foo &); |
| 5523 |
}; |
| 5524 |
struct FooWrite { |
| 5525 |
FooWrite(Foo &); |
| 5526 |
}; |
| 5527 |
|
| 5528 |
// Test variadic functions |
| 5529 |
template<typename... T> |
| 5530 |
void copyVariadic(T...) {} |
| 5531 |
template<typename... T> |
| 5532 |
void writeVariadic(T&...) {} |
| 5533 |
template<typename... T> |
| 5534 |
void readVariadic(const T&...) {} |
| 5535 |
|
| 5536 |
void copyVariadicC(int, ...); |
| 5537 |
|
| 5538 |
class Bar { |
| 5539 |
public: |
| 5540 |
Mutex mu; |
| 5541 |
Foo foo GUARDED_BY(mu); |
| 5542 |
Foo foo2 GUARDED_BY(mu); |
| 5543 |
Foo* foop PT_GUARDED_BY(mu); |
| 5544 |
SmartPtr<Foo> foosp PT_GUARDED_BY(mu); |
| 5545 |
|
| 5546 |
// test methods. |
| 5547 |
void mwrite1(Foo& f); |
| 5548 |
void mwrite2(int a, Foo& f); |
| 5549 |
void mread1(const Foo& f); |
| 5550 |
void mread2(int a, const Foo& f); |
| 5551 |
|
| 5552 |
// static methods |
| 5553 |
static void smwrite1(Foo& f); |
| 5554 |
static void smwrite2(int a, Foo& f); |
| 5555 |
static void smread1(const Foo& f); |
| 5556 |
static void smread2(int a, const Foo& f); |
| 5557 |
|
| 5558 |
void operator<<(const Foo& f); |
| 5559 |
|
| 5560 |
void test1() { |
| 5561 |
copy(foo); // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} |
| 5562 |
write1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5563 |
write2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5564 |
read1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5565 |
read2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5566 |
destroy(mymove(foo)); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5567 |
|
| 5568 |
copyVariadic(foo); // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} |
| 5569 |
readVariadic(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5570 |
writeVariadic(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5571 |
copyVariadicC(1, foo); // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} |
| 5572 |
|
| 5573 |
FooRead reader(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5574 |
FooWrite writer(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5575 |
|
| 5576 |
mwrite1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5577 |
mwrite2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5578 |
mread1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5579 |
mread2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5580 |
|
| 5581 |
smwrite1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5582 |
smwrite2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5583 |
smread1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5584 |
smread2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5585 |
|
| 5586 |
foo + foo2; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \ |
| 5587 |
// expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}} |
| 5588 |
foo / foo2; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \ |
| 5589 |
// expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}} |
| 5590 |
foo * foo2; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \ |
| 5591 |
// expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}} |
| 5592 |
foo[foo2]; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \ |
| 5593 |
// expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}} |
| 5594 |
foo(); // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} |
| 5595 |
(*this) << foo; // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} |
| 5596 |
|
| 5597 |
copy(*foop); // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu'}} |
| 5598 |
write1(*foop); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}} |
| 5599 |
write2(10, *foop); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}} |
| 5600 |
read1(*foop); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}} |
| 5601 |
read2(10, *foop); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}} |
| 5602 |
destroy(mymove(*foop)); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}} |
| 5603 |
|
| 5604 |
copy(*foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} |
| 5605 |
write1(*foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} |
| 5606 |
write2(10, *foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} |
| 5607 |
read1(*foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} |
| 5608 |
read2(10, *foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} |
| 5609 |
destroy(mymove(*foosp)); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} |
| 5610 |
|
| 5611 |
// TODO -- these require better smart pointer handling. |
| 5612 |
copy(*foosp.get()); |
| 5613 |
write1(*foosp.get()); |
| 5614 |
write2(10, *foosp.get()); |
| 5615 |
read1(*foosp.get()); |
| 5616 |
read2(10, *foosp.get()); |
| 5617 |
destroy(mymove(*foosp.get())); |
| 5618 |
} |
| 5619 |
}; |
| 5620 |
|
| 5621 |
class Return { |
| 5622 |
Mutex mu; |
| 5623 |
Foo foo GUARDED_BY(mu); |
| 5624 |
Foo* foo_ptr PT_GUARDED_BY(mu); |
| 5625 |
|
| 5626 |
Foo returns_value_locked() { |
| 5627 |
MutexLock lock(&mu); |
| 5628 |
return foo; |
| 5629 |
} |
| 5630 |
|
| 5631 |
Foo returns_value_locks_required() EXCLUSIVE_LOCKS_REQUIRED(mu) { |
| 5632 |
return foo; |
| 5633 |
} |
| 5634 |
|
| 5635 |
Foo returns_value_releases_lock_after_return() UNLOCK_FUNCTION(mu) { |
| 5636 |
MutexLock lock(&mu, true); |
| 5637 |
return foo; |
| 5638 |
} |
| 5639 |
|
| 5640 |
Foo returns_value_aquires_lock() EXCLUSIVE_LOCK_FUNCTION(mu) { |
| 5641 |
mu.Lock(); |
| 5642 |
return foo; |
| 5643 |
} |
| 5644 |
|
| 5645 |
Foo returns_value_not_locked() { |
| 5646 |
return foo; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} |
| 5647 |
} |
| 5648 |
|
| 5649 |
Foo returns_value_releases_lock_before_return() UNLOCK_FUNCTION(mu) { |
| 5650 |
mu.Unlock(); |
| 5651 |
return foo; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} |
| 5652 |
} |
| 5653 |
|
| 5654 |
Foo &returns_ref_not_locked() { |
| 5655 |
return foo; // expected-warning {{returning variable 'foo' by reference requires holding mutex 'mu'}} |
| 5656 |
} |
| 5657 |
|
| 5658 |
Foo &returns_ref_locked() { |
| 5659 |
MutexLock lock(&mu); |
| 5660 |
return foo; // expected-warning {{returning variable 'foo' by reference requires holding mutex 'mu'}} |
| 5661 |
} |
| 5662 |
|
| 5663 |
Foo &returns_ref_shared_locks_required() SHARED_LOCKS_REQUIRED(mu) { |
| 5664 |
return foo; // expected-warning {{returning variable 'foo' by reference requires holding mutex 'mu' exclusively}} |
| 5665 |
} |
| 5666 |
|
| 5667 |
Foo &returns_ref_exclusive_locks_required() EXCLUSIVE_LOCKS_REQUIRED(mu) { |
| 5668 |
return foo; |
| 5669 |
} |
| 5670 |
|
| 5671 |
Foo &returns_ref_releases_lock_after_return() UNLOCK_FUNCTION(mu) { |
| 5672 |
MutexLock lock(&mu, true); |
| 5673 |
return foo; // expected-warning {{returning variable 'foo' by reference requires holding mutex 'mu' exclusively}} |
| 5674 |
} |
| 5675 |
|
| 5676 |
Foo& returns_ref_releases_lock_before_return() UNLOCK_FUNCTION(mu) { |
| 5677 |
mu.Unlock(); |
| 5678 |
return foo; // // expected-warning {{returning variable 'foo' by reference requires holding mutex 'mu' exclusively}} |
| 5679 |
} |
| 5680 |
|
| 5681 |
Foo &returns_ref_aquires_lock() EXCLUSIVE_LOCK_FUNCTION(mu) { |
| 5682 |
mu.Lock(); |
| 5683 |
return foo; |
| 5684 |
} |
| 5685 |
|
| 5686 |
const Foo &returns_constref_shared_locks_required() SHARED_LOCKS_REQUIRED(mu) { |
| 5687 |
return foo; |
| 5688 |
} |
| 5689 |
|
| 5690 |
Foo *returns_ptr() { |
| 5691 |
return &foo; // FIXME -- Do we want to warn on this ? |
| 5692 |
} |
| 5693 |
|
| 5694 |
Foo &returns_ref2() { |
| 5695 |
return *foo_ptr; // expected-warning {{returning the value that 'foo_ptr' points to by reference requires holding mutex 'mu' exclusively}} |
| 5696 |
} |
| 5697 |
|
| 5698 |
}; |
| 5699 |
|
| 5700 |
|
| 5701 |
} // end namespace PassByRefTest |
| 5702 |
|
| 5703 |
|
| 5704 |
namespace AcquiredBeforeAfterText { |
| 5705 |
|
| 5706 |
class Foo { |
| 5707 |
Mutex mu1 ACQUIRED_BEFORE(mu2, mu3); |
| 5708 |
Mutex mu2; |
| 5709 |
Mutex mu3; |
| 5710 |
|
| 5711 |
void test1() { |
| 5712 |
mu1.Lock(); |
| 5713 |
mu2.Lock(); |
| 5714 |
mu3.Lock(); |
| 5715 |
|
| 5716 |
mu3.Unlock(); |
| 5717 |
mu2.Unlock(); |
| 5718 |
mu1.Unlock(); |
| 5719 |
} |
| 5720 |
|
| 5721 |
void test2() { |
| 5722 |
mu2.Lock(); |
| 5723 |
mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}} |
| 5724 |
mu1.Unlock(); |
| 5725 |
mu2.Unlock(); |
| 5726 |
} |
| 5727 |
|
| 5728 |
void test3() { |
| 5729 |
mu3.Lock(); |
| 5730 |
mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu3'}} |
| 5731 |
mu1.Unlock(); |
| 5732 |
mu3.Unlock(); |
| 5733 |
} |
| 5734 |
|
| 5735 |
void test4() EXCLUSIVE_LOCKS_REQUIRED(mu1) { |
| 5736 |
mu2.Lock(); |
| 5737 |
mu2.Unlock(); |
| 5738 |
} |
| 5739 |
|
| 5740 |
void test5() EXCLUSIVE_LOCKS_REQUIRED(mu2) { |
| 5741 |
mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}} |
| 5742 |
mu1.Unlock(); |
| 5743 |
} |
| 5744 |
|
| 5745 |
void test6() EXCLUSIVE_LOCKS_REQUIRED(mu2) { |
| 5746 |
mu1.AssertHeld(); |
| 5747 |
} |
| 5748 |
|
| 5749 |
void test7() EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2, mu3) { } |
| 5750 |
|
| 5751 |
void test8() EXCLUSIVE_LOCKS_REQUIRED(mu3, mu2, mu1) { } |
| 5752 |
}; |
| 5753 |
|
| 5754 |
|
| 5755 |
class Foo2 { |
| 5756 |
Mutex mu1; |
| 5757 |
Mutex mu2 ACQUIRED_AFTER(mu1); |
| 5758 |
Mutex mu3 ACQUIRED_AFTER(mu1); |
| 5759 |
|
| 5760 |
void test1() { |
| 5761 |
mu1.Lock(); |
| 5762 |
mu2.Lock(); |
| 5763 |
mu3.Lock(); |
| 5764 |
|
| 5765 |
mu3.Unlock(); |
| 5766 |
mu2.Unlock(); |
| 5767 |
mu1.Unlock(); |
| 5768 |
} |
| 5769 |
|
| 5770 |
void test2() { |
| 5771 |
mu2.Lock(); |
| 5772 |
mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}} |
| 5773 |
mu1.Unlock(); |
| 5774 |
mu2.Unlock(); |
| 5775 |
} |
| 5776 |
|
| 5777 |
void test3() { |
| 5778 |
mu3.Lock(); |
| 5779 |
mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu3'}} |
| 5780 |
mu1.Unlock(); |
| 5781 |
mu3.Unlock(); |
| 5782 |
} |
| 5783 |
}; |
| 5784 |
|
| 5785 |
|
| 5786 |
class Foo3 { |
| 5787 |
Mutex mu1 ACQUIRED_BEFORE(mu2); |
| 5788 |
Mutex mu2; |
| 5789 |
Mutex mu3 ACQUIRED_AFTER(mu2) ACQUIRED_BEFORE(mu4); |
| 5790 |
Mutex mu4; |
| 5791 |
|
| 5792 |
void test1() { |
| 5793 |
mu1.Lock(); |
| 5794 |
mu2.Lock(); |
| 5795 |
mu3.Lock(); |
| 5796 |
mu4.Lock(); |
| 5797 |
|
| 5798 |
mu4.Unlock(); |
| 5799 |
mu3.Unlock(); |
| 5800 |
mu2.Unlock(); |
| 5801 |
mu1.Unlock(); |
| 5802 |
} |
| 5803 |
|
| 5804 |
void test2() { |
| 5805 |
mu4.Lock(); |
| 5806 |
mu2.Lock(); // expected-warning {{mutex 'mu2' must be acquired before 'mu4'}} |
| 5807 |
|
| 5808 |
mu2.Unlock(); |
| 5809 |
mu4.Unlock(); |
| 5810 |
} |
| 5811 |
|
| 5812 |
void test3() { |
| 5813 |
mu4.Lock(); |
| 5814 |
mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu4'}} |
| 5815 |
|
| 5816 |
mu1.Unlock(); |
| 5817 |
mu4.Unlock(); |
| 5818 |
} |
| 5819 |
|
| 5820 |
void test4() { |
| 5821 |
mu3.Lock(); |
| 5822 |
mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu3'}} |
| 5823 |
|
| 5824 |
mu1.Unlock(); |
| 5825 |
mu3.Unlock(); |
| 5826 |
} |
| 5827 |
}; |
| 5828 |
|
| 5829 |
|
| 5830 |
// Test transitive DAG traversal with AFTER |
| 5831 |
class Foo4 { |
| 5832 |
Mutex mu1; |
| 5833 |
Mutex mu2 ACQUIRED_AFTER(mu1); |
| 5834 |
Mutex mu3 ACQUIRED_AFTER(mu1); |
| 5835 |
Mutex mu4 ACQUIRED_AFTER(mu2, mu3); |
| 5836 |
Mutex mu5 ACQUIRED_AFTER(mu4); |
| 5837 |
Mutex mu6 ACQUIRED_AFTER(mu4); |
| 5838 |
Mutex mu7 ACQUIRED_AFTER(mu5, mu6); |
| 5839 |
Mutex mu8 ACQUIRED_AFTER(mu7); |
| 5840 |
|
| 5841 |
void test() { |
| 5842 |
mu8.Lock(); |
| 5843 |
mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu8'}} |
| 5844 |
mu1.Unlock(); |
| 5845 |
mu8.Unlock(); |
| 5846 |
} |
| 5847 |
}; |
| 5848 |
|
| 5849 |
|
| 5850 |
// Test transitive DAG traversal with BEFORE |
| 5851 |
class Foo5 { |
| 5852 |
Mutex mu1 ACQUIRED_BEFORE(mu2, mu3); |
| 5853 |
Mutex mu2 ACQUIRED_BEFORE(mu4); |
| 5854 |
Mutex mu3 ACQUIRED_BEFORE(mu4); |
| 5855 |
Mutex mu4 ACQUIRED_BEFORE(mu5, mu6); |
| 5856 |
Mutex mu5 ACQUIRED_BEFORE(mu7); |
| 5857 |
Mutex mu6 ACQUIRED_BEFORE(mu7); |
| 5858 |
Mutex mu7 ACQUIRED_BEFORE(mu8); |
| 5859 |
Mutex mu8; |
| 5860 |
|
| 5861 |
void test() { |
| 5862 |
mu8.Lock(); |
| 5863 |
mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu8'}} |
| 5864 |
mu1.Unlock(); |
| 5865 |
mu8.Unlock(); |
| 5866 |
} |
| 5867 |
}; |
| 5868 |
|
| 5869 |
|
| 5870 |
class Foo6 { |
| 5871 |
Mutex mu1 ACQUIRED_AFTER(mu3); // expected-warning {{cycle in acquired_before/after dependencies, starting with 'mu1'}} |
| 5872 |
Mutex mu2 ACQUIRED_AFTER(mu1); // expected-warning {{cycle in acquired_before/after dependencies, starting with 'mu2'}} |
| 5873 |
Mutex mu3 ACQUIRED_AFTER(mu2); // expected-warning {{cycle in acquired_before/after dependencies, starting with 'mu3'}} |
| 5874 |
|
| 5875 |
Mutex mu_b ACQUIRED_BEFORE(mu_b); // expected-warning {{cycle in acquired_before/after dependencies, starting with 'mu_b'}} |
| 5876 |
Mutex mu_a ACQUIRED_AFTER(mu_a); // expected-warning {{cycle in acquired_before/after dependencies, starting with 'mu_a'}} |
| 5877 |
|
| 5878 |
void test0() { |
| 5879 |
mu_a.Lock(); |
| 5880 |
mu_b.Lock(); |
| 5881 |
mu_b.Unlock(); |
| 5882 |
mu_a.Unlock(); |
| 5883 |
} |
| 5884 |
|
| 5885 |
void test1a() { |
| 5886 |
mu1.Lock(); |
| 5887 |
mu1.Unlock(); |
| 5888 |
} |
| 5889 |
|
| 5890 |
void test1b() { |
| 5891 |
mu1.Lock(); |
| 5892 |
mu_a.Lock(); |
| 5893 |
mu_b.Lock(); |
| 5894 |
mu_b.Unlock(); |
| 5895 |
mu_a.Unlock(); |
| 5896 |
mu1.Unlock(); |
| 5897 |
} |
| 5898 |
|
| 5899 |
void test() { |
| 5900 |
mu2.Lock(); |
| 5901 |
mu2.Unlock(); |
| 5902 |
} |
| 5903 |
|
| 5904 |
void test3() { |
| 5905 |
mu3.Lock(); |
| 5906 |
mu3.Unlock(); |
| 5907 |
} |
| 5908 |
}; |
| 5909 |
|
| 5910 |
} // end namespace AcquiredBeforeAfterTest |
| 5911 |
|
| 5912 |
|
| 5913 |
namespace ScopedAdoptTest { |
| 5914 |
|
| 5915 |
class Foo { |
| 5916 |
Mutex mu; |
| 5917 |
int a GUARDED_BY(mu); |
| 5918 |
int b; |
| 5919 |
|
| 5920 |
void test1() EXCLUSIVE_UNLOCK_FUNCTION(mu) { |
| 5921 |
MutexLock slock(&mu, true); |
| 5922 |
a = 0; |
| 5923 |
} |
| 5924 |
|
| 5925 |
void test2() SHARED_UNLOCK_FUNCTION(mu) { |
| 5926 |
ReaderMutexLock slock(&mu, true); |
| 5927 |
b = a; |
| 5928 |
} |
| 5929 |
|
| 5930 |
void test3() EXCLUSIVE_LOCKS_REQUIRED(mu) { // expected-note {{mutex acquired here}} |
| 5931 |
MutexLock slock(&mu, true); |
| 5932 |
a = 0; |
| 5933 |
} // expected-warning {{expecting mutex 'mu' to be held at the end of function}} |
| 5934 |
|
| 5935 |
void test4() SHARED_LOCKS_REQUIRED(mu) { // expected-note {{mutex acquired here}} |
| 5936 |
ReaderMutexLock slock(&mu, true); |
| 5937 |
b = a; |
| 5938 |
} // expected-warning {{expecting mutex 'mu' to be held at the end of function}} |
| 5939 |
|
| 5940 |
}; |
| 5941 |
|
| 5942 |
} // end namespace ScopedAdoptTest |
| 5943 |
|
| 5944 |
|
| 5945 |
namespace TestReferenceNoThreadSafetyAnalysis { |
| 5946 |
|
| 5947 |
#define TS_UNCHECKED_READ(x) ts_unchecked_read(x) |
| 5948 |
|
| 5949 |
// Takes a reference to a guarded data member, and returns an unguarded |
| 5950 |
// reference. |
| 5951 |
template <class T> |
| 5952 |
inline const T& ts_unchecked_read(const T& v) NO_THREAD_SAFETY_ANALYSIS { |
| 5953 |
return v; |
| 5954 |
} |
| 5955 |
|
| 5956 |
template <class T> |
| 5957 |
inline T& ts_unchecked_read(T& v) NO_THREAD_SAFETY_ANALYSIS { |
| 5958 |
return v; |
| 5959 |
} |
| 5960 |
|
| 5961 |
|
| 5962 |
class Foo { |
| 5963 |
public: |
| 5964 |
Foo(): a(0) { } |
| 5965 |
|
| 5966 |
int a; |
| 5967 |
}; |
| 5968 |
|
| 5969 |
|
| 5970 |
class Bar { |
| 5971 |
public: |
| 5972 |
Bar() : a(0) { } |
| 5973 |
|
| 5974 |
Mutex mu; |
| 5975 |
int a GUARDED_BY(mu); |
| 5976 |
Foo foo GUARDED_BY(mu); |
| 5977 |
}; |
| 5978 |
|
| 5979 |
|
| 5980 |
void test() { |
| 5981 |
Bar bar; |
| 5982 |
const Bar cbar; |
| 5983 |
|
| 5984 |
int a = TS_UNCHECKED_READ(bar.a); // nowarn |
| 5985 |
TS_UNCHECKED_READ(bar.a) = 1; // nowarn |
| 5986 |
|
| 5987 |
int b = TS_UNCHECKED_READ(bar.foo).a; // nowarn |
| 5988 |
TS_UNCHECKED_READ(bar.foo).a = 1; // nowarn |
| 5989 |
|
| 5990 |
int c = TS_UNCHECKED_READ(cbar.a); // nowarn |
| 5991 |
} |
| 5992 |
|
| 5993 |
#undef TS_UNCHECKED_READ |
| 5994 |
|
| 5995 |
} // end namespace TestReferenceNoThreadSafetyAnalysis |
| 5996 |
|
| 5997 |
|
| 5998 |
namespace GlobalAcquiredBeforeAfterTest { |
| 5999 |
|
| 6000 |
Mutex mu1; |
| 6001 |
Mutex mu2 ACQUIRED_AFTER(mu1); |
| 6002 |
|
| 6003 |
void test3() { |
| 6004 |
mu2.Lock(); |
| 6005 |
mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}} |
| 6006 |
mu1.Unlock(); |
| 6007 |
mu2.Unlock(); |
| 6008 |
} |
| 6009 |
|
| 6010 |
} // end namespace GlobalAcquiredBeforeAfterTest |
| 6011 |
|
| 6012 |
|
| 6013 |
namespace LifetimeExtensionText { |
| 6014 |
|
| 6015 |
struct Holder { |
| 6016 |
virtual ~Holder() throw() {} |
| 6017 |
int i = 0; |
| 6018 |
}; |
| 6019 |
|
| 6020 |
void test() { |
| 6021 |
// Should not crash. |
| 6022 |
const auto &value = Holder().i; |
| 6023 |
} |
| 6024 |
|
| 6025 |
} // end namespace LifetimeExtensionTest |
| 6026 |
|
| 6027 |
|
| 6028 |
namespace LockableUnions { |
| 6029 |
|
| 6030 |
union LOCKABLE MutexUnion { |
| 6031 |
int a; |
| 6032 |
char* b; |
| 6033 |
|
| 6034 |
void Lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 6035 |
void Unlock() UNLOCK_FUNCTION(); |
| 6036 |
}; |
| 6037 |
|
| 6038 |
MutexUnion muun2; |
| 6039 |
MutexUnion muun1 ACQUIRED_BEFORE(muun2); |
| 6040 |
|
| 6041 |
void test() { |
| 6042 |
muun2.Lock(); |
| 6043 |
muun1.Lock(); // expected-warning {{mutex 'muun1' must be acquired before 'muun2'}} |
| 6044 |
muun1.Unlock(); |
| 6045 |
muun2.Unlock(); |
| 6046 |
} |
| 6047 |
|
| 6048 |
} // end namespace LockableUnions |
| 6049 |
|
| 6050 |
// This used to crash. |
| 6051 |
class acquired_before_empty_str { |
| 6052 |
void WaitUntilSpaceAvailable() { |
| 6053 |
lock_.ReaderLock(); // expected-note {{acquired here}} |
| 6054 |
} // expected-warning {{mutex 'lock_' is still held at the end of function}} |
| 6055 |
Mutex lock_ ACQUIRED_BEFORE(""); |
| 6056 |
}; |
| 6057 |
|
| 6058 |
namespace PR34800 { |
| 6059 |
struct A { |
| 6060 |
operator int() const; |
| 6061 |
}; |
| 6062 |
struct B { |
| 6063 |
bool g() __attribute__((locks_excluded(h))); // expected-warning {{'locks_excluded' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}} |
| 6064 |
int h; |
| 6065 |
}; |
| 6066 |
struct C { |
| 6067 |
B *operator[](int); |
| 6068 |
}; |
| 6069 |
C c; |
| 6070 |
void f() { c[A()]->g(); } |
| 6071 |
} // namespace PR34800 |
| 6072 |
|
| 6073 |
#ifdef __cpp_guaranteed_copy_elision |
| 6074 |
|
| 6075 |
namespace ReturnScopedLockable { |
| 6076 |
|
| 6077 |
class Object { |
| 6078 |
public: |
| 6079 |
MutexLock lock() EXCLUSIVE_LOCK_FUNCTION(mutex) { |
| 6080 |
// TODO: False positive because scoped lock isn't destructed. |
| 6081 |
return MutexLock(&mutex); // expected-note {{mutex acquired here}} |
| 6082 |
} // expected-warning {{mutex 'mutex' is still held at the end of function}} |
| 6083 |
|
| 6084 |
ReaderMutexLock lockShared() SHARED_LOCK_FUNCTION(mutex) { |
| 6085 |
// TODO: False positive because scoped lock isn't destructed. |
| 6086 |
return ReaderMutexLock(&mutex); // expected-note {{mutex acquired here}} |
| 6087 |
} // expected-warning {{mutex 'mutex' is still held at the end of function}} |
| 6088 |
|
| 6089 |
MutexLock adopt() EXCLUSIVE_LOCKS_REQUIRED(mutex) { |
| 6090 |
// TODO: False positive because scoped lock isn't destructed. |
| 6091 |
return MutexLock(&mutex, true); // expected-note {{mutex acquired here}} |
| 6092 |
} // expected-warning {{mutex 'mutex' is still held at the end of function}} |
| 6093 |
|
| 6094 |
ReaderMutexLock adoptShared() SHARED_LOCKS_REQUIRED(mutex) { |
| 6095 |
// TODO: False positive because scoped lock isn't destructed. |
| 6096 |
return ReaderMutexLock(&mutex, true); // expected-note {{mutex acquired here}} |
| 6097 |
} // expected-warning {{mutex 'mutex' is still held at the end of function}} |
| 6098 |
|
| 6099 |
int x GUARDED_BY(mutex); |
| 6100 |
void needsLock() EXCLUSIVE_LOCKS_REQUIRED(mutex); |
| 6101 |
|
| 6102 |
void testInside() { |
| 6103 |
MutexLock scope = lock(); |
| 6104 |
x = 1; |
| 6105 |
needsLock(); |
| 6106 |
} |
| 6107 |
|
| 6108 |
Mutex mutex; |
| 6109 |
}; |
| 6110 |
|
| 6111 |
Object obj; |
| 6112 |
|
| 6113 |
void testLock() { |
| 6114 |
MutexLock scope = obj.lock(); |
| 6115 |
obj.x = 1; |
| 6116 |
obj.needsLock(); |
| 6117 |
} |
| 6118 |
|
| 6119 |
int testSharedLock() { |
| 6120 |
ReaderMutexLock scope = obj.lockShared(); |
| 6121 |
obj.x = 1; // expected-warning {{writing variable 'x' requires holding mutex 'obj.mutex' exclusively}} |
| 6122 |
return obj.x; |
| 6123 |
} |
| 6124 |
|
| 6125 |
void testAdopt() { |
| 6126 |
obj.mutex.Lock(); |
| 6127 |
MutexLock scope = obj.adopt(); |
| 6128 |
obj.x = 1; |
| 6129 |
} |
| 6130 |
|
| 6131 |
int testAdoptShared() { |
| 6132 |
obj.mutex.Lock(); |
| 6133 |
ReaderMutexLock scope = obj.adoptShared(); |
| 6134 |
obj.x = 1; |
| 6135 |
return obj.x; |
| 6136 |
} |
| 6137 |
|
| 6138 |
} // namespace ReturnScopedLockable |
| 6139 |
|
| 6140 |
#endif |
| 6141 |
|
| 6142 |
namespace PR38640 { |
| 6143 |
void f() { |
| 6144 |
// Self-referencing assignment previously caused an infinite loop when thread |
| 6145 |
// safety analysis was enabled. |
| 6146 |
int &i = i; // expected-warning {{reference 'i' is not yet bound to a value when used within its own initialization}} |
| 6147 |
} |
| 6148 |
} |
| 6149 |
|
| 6150 |
namespace Derived_Smart_Pointer { |
| 6151 |
template <class T> |
| 6152 |
class SmartPtr_Derived : public SmartPtr<T> {}; |
| 6153 |
|
| 6154 |
class Foo { |
| 6155 |
public: |
| 6156 |
SmartPtr_Derived<Mutex> mu_; |
| 6157 |
int a GUARDED_BY(mu_); |
| 6158 |
int b GUARDED_BY(mu_.get()); |
| 6159 |
int c GUARDED_BY(*mu_); |
| 6160 |
|
| 6161 |
void Lock() EXCLUSIVE_LOCK_FUNCTION(mu_); |
| 6162 |
void Unlock() UNLOCK_FUNCTION(mu_); |
| 6163 |
|
| 6164 |
void test0() { |
| 6165 |
a = 1; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} |
| 6166 |
b = 1; // expected-warning {{writing variable 'b' requires holding mutex 'mu_' exclusively}} |
| 6167 |
c = 1; // expected-warning {{writing variable 'c' requires holding mutex 'mu_' exclusively}} |
| 6168 |
} |
| 6169 |
|
| 6170 |
void test1() { |
| 6171 |
Lock(); |
| 6172 |
a = 1; |
| 6173 |
b = 1; |
| 6174 |
c = 1; |
| 6175 |
Unlock(); |
| 6176 |
} |
| 6177 |
}; |
| 6178 |
|
| 6179 |
class Bar { |
| 6180 |
SmartPtr_Derived<Foo> foo; |
| 6181 |
|
| 6182 |
void test0() { |
| 6183 |
foo->a = 1; // expected-warning {{writing variable 'a' requires holding mutex 'foo->mu_' exclusively}} |
| 6184 |
(*foo).b = 1; // expected-warning {{writing variable 'b' requires holding mutex 'foo->mu_' exclusively}} |
| 6185 |
foo.get()->c = 1; // expected-warning {{writing variable 'c' requires holding mutex 'foo->mu_' exclusively}} |
| 6186 |
} |
| 6187 |
|
| 6188 |
void test1() { |
| 6189 |
foo->Lock(); |
| 6190 |
foo->a = 1; |
| 6191 |
foo->Unlock(); |
| 6192 |
|
| 6193 |
foo->mu_->Lock(); |
| 6194 |
foo->b = 1; |
| 6195 |
foo->mu_->Unlock(); |
| 6196 |
|
| 6197 |
MutexLock lock(foo->mu_.get()); |
| 6198 |
foo->c = 1; |
| 6199 |
} |
| 6200 |
}; |
| 6201 |
|
| 6202 |
class PointerGuard { |
| 6203 |
Mutex mu1; |
| 6204 |
Mutex mu2; |
| 6205 |
SmartPtr_Derived<int> i GUARDED_BY(mu1) PT_GUARDED_BY(mu2); |
| 6206 |
|
| 6207 |
void test0() { |
| 6208 |
i.get(); // expected-warning {{reading variable 'i' requires holding mutex 'mu1'}} |
| 6209 |
*i = 2; // expected-warning {{reading variable 'i' requires holding mutex 'mu1'}} \ |
| 6210 |
// expected-warning {{reading the value pointed to by 'i' requires holding mutex 'mu2'}} |
| 6211 |
|
| 6212 |
} |
| 6213 |
|
| 6214 |
void test1() { |
| 6215 |
mu1.Lock(); |
| 6216 |
|
| 6217 |
i.get(); |
| 6218 |
*i = 2; // expected-warning {{reading the value pointed to by 'i' requires holding mutex 'mu2'}} |
| 6219 |
|
| 6220 |
mu1.Unlock(); |
| 6221 |
} |
| 6222 |
|
| 6223 |
void test2() { |
| 6224 |
mu2.Lock(); |
| 6225 |
|
| 6226 |
i.get(); // expected-warning {{reading variable 'i' requires holding mutex 'mu1'}} |
| 6227 |
*i = 2; // expected-warning {{reading variable 'i' requires holding mutex 'mu1'}} |
| 6228 |
|
| 6229 |
mu2.Unlock(); |
| 6230 |
} |
| 6231 |
|
| 6232 |
void test3() { |
| 6233 |
mu1.Lock(); |
| 6234 |
mu2.Lock(); |
| 6235 |
|
| 6236 |
i.get(); |
| 6237 |
*i = 2; |
| 6238 |
|
| 6239 |
mu2.Unlock(); |
| 6240 |
mu1.Unlock(); |
| 6241 |
} |
| 6242 |
}; |
| 6243 |
} |