[llvm-commits] [compiler-rt] r147878 - in /compiler-rt/trunk/lib/asan: asan_interceptors.cc asan_linux.cc asan_lock.h asan_mac.cc asan_thread_registry.cc

Kostya Serebryany kcc at google.com
Tue Jan 10 13:24:40 PST 2012


Author: kcc
Date: Tue Jan 10 15:24:40 2012
New Revision: 147878

URL: http://llvm.org/viewvc/llvm-project?rev=147878&view=rev
Log:
[asan] move OS-dependent code away from asan_lock.h

Modified:
    compiler-rt/trunk/lib/asan/asan_interceptors.cc
    compiler-rt/trunk/lib/asan/asan_linux.cc
    compiler-rt/trunk/lib/asan/asan_lock.h
    compiler-rt/trunk/lib/asan/asan_mac.cc
    compiler-rt/trunk/lib/asan/asan_thread_registry.cc

Modified: compiler-rt/trunk/lib/asan/asan_interceptors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_interceptors.cc?rev=147878&r1=147877&r2=147878&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_interceptors.cc Tue Jan 10 15:24:40 2012
@@ -35,7 +35,7 @@
 typedef longjmp_f _longjmp_f;
 typedef longjmp_f siglongjmp_f;
 typedef void (*__cxa_throw_f)(void *, void *, void *);
-typedef int (*pthread_create_f)(pthread_t *thread, const pthread_attr_t *attr,
+typedef int (*pthread_create_f)(void *thread, const void *attr,
                                 void *(*start_routine) (void *), void *arg);
 #ifdef __APPLE__
 dispatch_async_f_f real_dispatch_async_f;
@@ -223,10 +223,13 @@
 }
 
 extern "C"
+int pthread_create(void *thread, const void *attr,
+                   void *(*start_routine) (void *), void *arg);
+extern "C"
 #ifndef __APPLE__
 __attribute__((visibility("default")))
 #endif
-int WRAP(pthread_create)(pthread_t *thread, const pthread_attr_t *attr,
+int WRAP(pthread_create)(void *thread, const void *attr,
                          void *(*start_routine) (void *), void *arg) {
   GET_STACK_TRACE_HERE(kStackTraceMax, /*fast_unwind*/false);
   AsanThread *curr_thread = asanThreadRegistry().GetCurrent();

Modified: compiler-rt/trunk/lib/asan/asan_linux.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_linux.cc?rev=147878&r1=147877&r2=147878&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_linux.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_linux.cc Tue Jan 10 15:24:40 2012
@@ -15,6 +15,7 @@
 
 #include "asan_interceptors.h"
 #include "asan_internal.h"
+#include "asan_lock.h"
 #include "asan_procmaps.h"
 #include "asan_thread.h"
 
@@ -276,6 +277,26 @@
   CHECK(AddrIsInStack((uintptr_t)&attr));
 }
 
+AsanLock::AsanLock(LinkerInitialized) {
+  // We assume that pthread_mutex_t initialized to all zeroes is a valid
+  // unlocked mutex. We can not use PTHREAD_MUTEX_INITIALIZER as it triggers
+  // a gcc warning:
+  // extended initializer lists only available with -std=c++0x or -std=gnu++0x
+}
+
+void AsanLock::Lock() {
+  CHECK(sizeof(pthread_mutex_t) <= sizeof(opaque_storage_));
+  pthread_mutex_lock((pthread_mutex_t*)&opaque_storage_);
+  CHECK(!owner_);
+  owner_ = (uintptr_t)pthread_self();
+}
+
+void AsanLock::Unlock() {
+  CHECK(owner_ == (uintptr_t)pthread_self());
+  owner_ = 0;
+  pthread_mutex_unlock((pthread_mutex_t*)&opaque_storage_);
+}
+
 }  // namespace __asan
 
 #endif  // __linux__

Modified: compiler-rt/trunk/lib/asan/asan_lock.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_lock.h?rev=147878&r1=147877&r2=147878&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_lock.h (original)
+++ compiler-rt/trunk/lib/asan/asan_lock.h Tue Jan 10 15:24:40 2012
@@ -19,70 +19,21 @@
 // The locks in ASan are global objects and they are never destroyed to avoid
 // at-exit races (that is, a lock is being used by other threads while the main
 // thread is doing atexit destructors).
+// We define the class using opaque storage to avoid including system headers.
 
-#ifdef __APPLE__
-#include <pthread.h>
-
-#include <libkern/OSAtomic.h>
 namespace __asan {
-class AsanLock {
- public:
-  explicit AsanLock(LinkerInitialized) :
-    mu_(OS_SPINLOCK_INIT),
-    owner_(0),
-    is_locked_(false) {}
 
-  void Lock() {
-    CHECK(owner_ != pthread_self());
-    OSSpinLockLock(&mu_);
-    is_locked_ = true;
-    owner_ = pthread_self();
-  }
-  void Unlock() {
-    owner_ = 0;
-    is_locked_ = false;
-    OSSpinLockUnlock(&mu_);
-  }
-
-  bool IsLocked() {
-    // This is not atomic, e.g. one thread may get different values if another
-    // one is about to release the lock.
-    return is_locked_;
-  }
- private:
-  OSSpinLock mu_;
-  volatile pthread_t owner_;  // for debugging purposes
-  bool is_locked_;  // for silly malloc_introspection_t interface
-};
-}  // namespace __asan
-
-#else  // assume linux
-#include <pthread.h>
-namespace __asan {
 class AsanLock {
  public:
-  explicit AsanLock(LinkerInitialized) {
-    // We assume that pthread_mutex_t initialized to all zeroes is a valid
-    // unlocked mutex. We can not use PTHREAD_MUTEX_INITIALIZER as it triggers
-    // a gcc warning:
-    // extended initializer lists only available with -std=c++0x or -std=gnu++0x
-  }
-  void Lock() {
-    pthread_mutex_lock(&mu_);
-    // pthread_spin_lock(&mu_);
-  }
-  void Unlock() {
-    pthread_mutex_unlock(&mu_);
-    // pthread_spin_unlock(&mu_);
-  }
+  explicit AsanLock(LinkerInitialized);
+  void Lock();
+  void Unlock();
+  bool IsLocked() { return owner_ != 0; }
  private:
-  pthread_mutex_t mu_;
-  // pthread_spinlock_t mu_;
+  uintptr_t opaque_storage_[10];
+  uintptr_t owner_;  // for debugging and for malloc_introspection_t interface
 };
-}  // namespace __asan
-#endif
 
-namespace __asan {
 class ScopedLock {
  public:
   explicit ScopedLock(AsanLock *mu) : mu_(mu) {

Modified: compiler-rt/trunk/lib/asan/asan_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_mac.cc?rev=147878&r1=147877&r2=147878&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_mac.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_mac.cc Tue Jan 10 15:24:40 2012
@@ -27,6 +27,7 @@
 #include <pthread.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <libkern/OSAtomic.h>
 
 namespace __asan {
 
@@ -130,6 +131,27 @@
   CHECK(AddrIsInStack((uintptr_t)&local));
 }
 
+
+AsanLock::AsanLock(LinkerInitialized) {
+  // We assume that OS_SPINLOCK_INIT is zero
+}
+
+void AsanLock::Lock() {
+  CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_));
+  CHECK(OS_SPINLOCK_INIT == 0);
+  CHECK(owner_ != (uintptr_t)pthread_self());
+  OSSpinLockLock((OSSpinLock*)&opaque_storage_);
+  CHECK(!owner_);
+  owner_ = (uintptr_t)pthread_self();
+}
+
+void AsanLock::Unlock() {
+  CHECK(owner_ == (uintptr_t)pthread_self());
+  owner_ = 0;
+  OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
+}
+
+
 // Support for the following functions from libdispatch on Mac OS:
 //   dispatch_async_f()
 //   dispatch_async()

Modified: compiler-rt/trunk/lib/asan/asan_thread_registry.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread_registry.cc?rev=147878&r1=147877&r2=147878&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread_registry.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_thread_registry.cc Tue Jan 10 15:24:40 2012
@@ -18,6 +18,7 @@
 #include "asan_thread_registry.h"
 
 #include <limits.h>
+#include <pthread.h>
 
 namespace __asan {
 





More information about the llvm-commits mailing list