From db7d9656bb0a15e9f663c45052c3b429afdd07af Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Mon, 11 Mar 2013 15:45:20 +0000 Subject: [PATCH] [Sanitizer] Implement BlockingMutex::CheckLocked() llvm-svn: 176805 --- compiler-rt/lib/sanitizer_common/sanitizer_linux.cc | 5 +++++ compiler-rt/lib/sanitizer_common/sanitizer_mac.cc | 4 ++++ compiler-rt/lib/sanitizer_common/sanitizer_mutex.h | 1 + compiler-rt/lib/sanitizer_common/sanitizer_win.cc | 4 ++++ compiler-rt/lib/sanitizer_common/tests/sanitizer_mutex_test.cc | 7 +++++++ 5 files changed, 21 insertions(+) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc index 7d5b9a9..9b51d2c 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc @@ -545,6 +545,11 @@ void BlockingMutex::Unlock() { syscall(__NR_futex, m, FUTEX_WAKE, 1, 0, 0, 0); } +void BlockingMutex::CheckLocked() { + atomic_uint32_t *m = reinterpret_cast(&opaque_storage_); + CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed)); +} + // ----------------- sanitizer_linux.h // The actual size of this structure is specified by d_reclen. // Note that getdents64 uses a different structure format. We only provide the diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc index 17bc223..0f11220 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc @@ -317,6 +317,10 @@ void BlockingMutex::Unlock() { OSSpinLockUnlock((OSSpinLock*)&opaque_storage_); } +void BlockingMutex::CheckLocked() { + CHECK_EQ((uptr)pthread_self(), owner_); +} + } // namespace __sanitizer #endif // __APPLE__ diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_mutex.h b/compiler-rt/lib/sanitizer_common/sanitizer_mutex.h index 56438fc..be3d559 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_mutex.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_mutex.h @@ -72,6 +72,7 @@ class BlockingMutex { explicit BlockingMutex(LinkerInitialized); void Lock(); void Unlock(); + void CheckLocked(); private: uptr opaque_storage_[10]; uptr owner_; // for debugging diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_win.cc b/compiler-rt/lib/sanitizer_common/sanitizer_win.cc index 40af4e3..7b540a2 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_win.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_win.cc @@ -291,6 +291,10 @@ void BlockingMutex::Unlock() { LeaveCriticalSection((LPCRITICAL_SECTION)opaque_storage_); } +void BlockingMutex::CheckLocked() { + CHECK_EQ(owner_, GetThreadSelf()); +} + } // namespace __sanitizer #endif // _WIN32 diff --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_mutex_test.cc b/compiler-rt/lib/sanitizer_common/tests/sanitizer_mutex_test.cc index 6bb2ae2..1dc9bef 100644 --- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_mutex_test.cc +++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_mutex_test.cc @@ -92,6 +92,12 @@ static void *try_thread(void *param) { return 0; } +template +static void check_locked(MutexType *mtx) { + GenericScopedLock l(mtx); + mtx->CheckLocked(); +} + TEST(SanitizerCommon, SpinMutex) { SpinMutex mtx; mtx.Init(); @@ -123,6 +129,7 @@ TEST(SanitizerCommon, BlockingMutex) { pthread_create(&threads[i], 0, lock_thread, &data); for (int i = 0; i < kThreads; i++) pthread_join(threads[i], 0); + check_locked(mtx); } } // namespace __sanitizer -- 2.7.4