[asan] move OS-dependent code away from asan_lock.h
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@147878 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/asan_interceptors.cc b/lib/asan/asan_interceptors.cc
index 1346bb9..c445ca1 100644
--- a/lib/asan/asan_interceptors.cc
+++ b/lib/asan/asan_interceptors.cc
@@ -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();
diff --git a/lib/asan/asan_linux.cc b/lib/asan/asan_linux.cc
index a1be40e..17e2e37 100644
--- a/lib/asan/asan_linux.cc
+++ b/lib/asan/asan_linux.cc
@@ -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__
diff --git a/lib/asan/asan_lock.h b/lib/asan/asan_lock.h
index 030fae6..75da8ae 100644
--- a/lib/asan/asan_lock.h
+++ b/lib/asan/asan_lock.h
@@ -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_;
- }
+ explicit AsanLock(LinkerInitialized);
+ void Lock();
+ void Unlock();
+ bool IsLocked() { return owner_ != 0; }
private:
- OSSpinLock mu_;
- volatile pthread_t owner_; // for debugging purposes
- bool is_locked_; // for silly malloc_introspection_t interface
+ uintptr_t opaque_storage_[10];
+ uintptr_t owner_; // for debugging and for 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_);
- }
- private:
- pthread_mutex_t mu_;
- // pthread_spinlock_t mu_;
-};
-} // namespace __asan
-#endif
-
-namespace __asan {
class ScopedLock {
public:
explicit ScopedLock(AsanLock *mu) : mu_(mu) {
diff --git a/lib/asan/asan_mac.cc b/lib/asan/asan_mac.cc
index 8e02901..695d957 100644
--- a/lib/asan/asan_mac.cc
+++ b/lib/asan/asan_mac.cc
@@ -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()
diff --git a/lib/asan/asan_thread_registry.cc b/lib/asan/asan_thread_registry.cc
index e9a40b4..2d4efa6 100644
--- a/lib/asan/asan_thread_registry.cc
+++ b/lib/asan/asan_thread_registry.cc
@@ -18,6 +18,7 @@
#include "asan_thread_registry.h"
#include <limits.h>
+#include <pthread.h>
namespace __asan {