2017-02-14 05:03:15 +08:00
|
|
|
//=-- lsan_common_mac.cc --------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of LeakSanitizer.
|
|
|
|
// Implementation of common leak checking functionality. Darwin-specific code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "sanitizer_common/sanitizer_platform.h"
|
|
|
|
#include "lsan_common.h"
|
|
|
|
|
|
|
|
#if CAN_SANITIZE_LEAKS && SANITIZER_MAC
|
Use pthreads to manage thread-local storage on darwin for leak sanitizer
Summary:
__thread is supported on Darwin, but is implemented dynamically via
function calls to __tls_get_addr. This causes two issues when combined
with leak sanitizer, due to malloc() interception.
- The dynamic loader calls malloc during the process of loading
the sanitizer dylib, while swapping a placeholder tlv_boostrap
function for __tls_get_addr. This will cause tlv_bootstrap to
be called in DisabledInThisThread() via the asan allocator.
- The first time __tls_get_addr is called, it allocates memory
for the thread-local object, during which it calls malloc(). This
call will be intercepted, leading to an infinite loop in the asan
allocator, in which the allocator calls DisabledInThisThread,
which calls tls_get_addr, which calls into the allocator again.
Reviewers: kcc, glider, kubamracek
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29786
llvm-svn: 294994
2017-02-14 06:20:07 +08:00
|
|
|
|
2017-03-27 22:07:50 +08:00
|
|
|
#include "sanitizer_common/sanitizer_allocator_internal.h"
|
|
|
|
#include "lsan_allocator.h"
|
|
|
|
|
Use pthreads to manage thread-local storage on darwin for leak sanitizer
Summary:
__thread is supported on Darwin, but is implemented dynamically via
function calls to __tls_get_addr. This causes two issues when combined
with leak sanitizer, due to malloc() interception.
- The dynamic loader calls malloc during the process of loading
the sanitizer dylib, while swapping a placeholder tlv_boostrap
function for __tls_get_addr. This will cause tlv_bootstrap to
be called in DisabledInThisThread() via the asan allocator.
- The first time __tls_get_addr is called, it allocates memory
for the thread-local object, during which it calls malloc(). This
call will be intercepted, leading to an infinite loop in the asan
allocator, in which the allocator calls DisabledInThisThread,
which calls tls_get_addr, which calls into the allocator again.
Reviewers: kcc, glider, kubamracek
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29786
llvm-svn: 294994
2017-02-14 06:20:07 +08:00
|
|
|
#include <pthread.h>
|
|
|
|
|
2017-02-14 05:03:15 +08:00
|
|
|
namespace __lsan {
|
|
|
|
|
2017-02-17 11:23:07 +08:00
|
|
|
typedef struct {
|
|
|
|
int disable_counter;
|
|
|
|
u32 current_thread_id;
|
2017-03-27 22:07:50 +08:00
|
|
|
AllocatorCache cache;
|
2017-02-17 11:23:07 +08:00
|
|
|
} thread_local_data_t;
|
|
|
|
|
Use pthreads to manage thread-local storage on darwin for leak sanitizer
Summary:
__thread is supported on Darwin, but is implemented dynamically via
function calls to __tls_get_addr. This causes two issues when combined
with leak sanitizer, due to malloc() interception.
- The dynamic loader calls malloc during the process of loading
the sanitizer dylib, while swapping a placeholder tlv_boostrap
function for __tls_get_addr. This will cause tlv_bootstrap to
be called in DisabledInThisThread() via the asan allocator.
- The first time __tls_get_addr is called, it allocates memory
for the thread-local object, during which it calls malloc(). This
call will be intercepted, leading to an infinite loop in the asan
allocator, in which the allocator calls DisabledInThisThread,
which calls tls_get_addr, which calls into the allocator again.
Reviewers: kcc, glider, kubamracek
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29786
llvm-svn: 294994
2017-02-14 06:20:07 +08:00
|
|
|
static pthread_key_t key;
|
|
|
|
static pthread_once_t key_once = PTHREAD_ONCE_INIT;
|
|
|
|
|
2017-04-12 03:57:12 +08:00
|
|
|
// The main thread destructor requires the current thread id,
|
|
|
|
// so we can't destroy it until it's been used and reset to invalid tid
|
|
|
|
void restore_tid_data(void *ptr) {
|
|
|
|
thread_local_data_t *data = (thread_local_data_t *)ptr;
|
|
|
|
if (data->current_thread_id != kInvalidTid)
|
|
|
|
pthread_setspecific(key, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void make_tls_key() {
|
|
|
|
CHECK_EQ(pthread_key_create(&key, restore_tid_data), 0);
|
|
|
|
}
|
Use pthreads to manage thread-local storage on darwin for leak sanitizer
Summary:
__thread is supported on Darwin, but is implemented dynamically via
function calls to __tls_get_addr. This causes two issues when combined
with leak sanitizer, due to malloc() interception.
- The dynamic loader calls malloc during the process of loading
the sanitizer dylib, while swapping a placeholder tlv_boostrap
function for __tls_get_addr. This will cause tlv_bootstrap to
be called in DisabledInThisThread() via the asan allocator.
- The first time __tls_get_addr is called, it allocates memory
for the thread-local object, during which it calls malloc(). This
call will be intercepted, leading to an infinite loop in the asan
allocator, in which the allocator calls DisabledInThisThread,
which calls tls_get_addr, which calls into the allocator again.
Reviewers: kcc, glider, kubamracek
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29786
llvm-svn: 294994
2017-02-14 06:20:07 +08:00
|
|
|
|
2017-03-29 05:56:45 +08:00
|
|
|
static thread_local_data_t *get_tls_val(bool alloc) {
|
Use pthreads to manage thread-local storage on darwin for leak sanitizer
Summary:
__thread is supported on Darwin, but is implemented dynamically via
function calls to __tls_get_addr. This causes two issues when combined
with leak sanitizer, due to malloc() interception.
- The dynamic loader calls malloc during the process of loading
the sanitizer dylib, while swapping a placeholder tlv_boostrap
function for __tls_get_addr. This will cause tlv_bootstrap to
be called in DisabledInThisThread() via the asan allocator.
- The first time __tls_get_addr is called, it allocates memory
for the thread-local object, during which it calls malloc(). This
call will be intercepted, leading to an infinite loop in the asan
allocator, in which the allocator calls DisabledInThisThread,
which calls tls_get_addr, which calls into the allocator again.
Reviewers: kcc, glider, kubamracek
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29786
llvm-svn: 294994
2017-02-14 06:20:07 +08:00
|
|
|
pthread_once(&key_once, make_tls_key);
|
|
|
|
|
2017-02-17 11:23:07 +08:00
|
|
|
thread_local_data_t *ptr = (thread_local_data_t *)pthread_getspecific(key);
|
2017-03-29 05:56:45 +08:00
|
|
|
if (ptr == NULL && alloc) {
|
2017-02-17 11:23:07 +08:00
|
|
|
ptr = (thread_local_data_t *)InternalAlloc(sizeof(*ptr));
|
|
|
|
ptr->disable_counter = 0;
|
|
|
|
ptr->current_thread_id = kInvalidTid;
|
2017-03-27 22:07:50 +08:00
|
|
|
ptr->cache = AllocatorCache();
|
Use pthreads to manage thread-local storage on darwin for leak sanitizer
Summary:
__thread is supported on Darwin, but is implemented dynamically via
function calls to __tls_get_addr. This causes two issues when combined
with leak sanitizer, due to malloc() interception.
- The dynamic loader calls malloc during the process of loading
the sanitizer dylib, while swapping a placeholder tlv_boostrap
function for __tls_get_addr. This will cause tlv_bootstrap to
be called in DisabledInThisThread() via the asan allocator.
- The first time __tls_get_addr is called, it allocates memory
for the thread-local object, during which it calls malloc(). This
call will be intercepted, leading to an infinite loop in the asan
allocator, in which the allocator calls DisabledInThisThread,
which calls tls_get_addr, which calls into the allocator again.
Reviewers: kcc, glider, kubamracek
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29786
llvm-svn: 294994
2017-02-14 06:20:07 +08:00
|
|
|
pthread_setspecific(key, ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2017-03-29 05:56:45 +08:00
|
|
|
bool DisabledInThisThread() {
|
|
|
|
thread_local_data_t *data = get_tls_val(false);
|
|
|
|
return data ? data->disable_counter > 0 : false;
|
|
|
|
}
|
Use pthreads to manage thread-local storage on darwin for leak sanitizer
Summary:
__thread is supported on Darwin, but is implemented dynamically via
function calls to __tls_get_addr. This causes two issues when combined
with leak sanitizer, due to malloc() interception.
- The dynamic loader calls malloc during the process of loading
the sanitizer dylib, while swapping a placeholder tlv_boostrap
function for __tls_get_addr. This will cause tlv_bootstrap to
be called in DisabledInThisThread() via the asan allocator.
- The first time __tls_get_addr is called, it allocates memory
for the thread-local object, during which it calls malloc(). This
call will be intercepted, leading to an infinite loop in the asan
allocator, in which the allocator calls DisabledInThisThread,
which calls tls_get_addr, which calls into the allocator again.
Reviewers: kcc, glider, kubamracek
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29786
llvm-svn: 294994
2017-02-14 06:20:07 +08:00
|
|
|
|
2017-03-29 05:56:45 +08:00
|
|
|
void DisableInThisThread() { ++get_tls_val(true)->disable_counter; }
|
Use pthreads to manage thread-local storage on darwin for leak sanitizer
Summary:
__thread is supported on Darwin, but is implemented dynamically via
function calls to __tls_get_addr. This causes two issues when combined
with leak sanitizer, due to malloc() interception.
- The dynamic loader calls malloc during the process of loading
the sanitizer dylib, while swapping a placeholder tlv_boostrap
function for __tls_get_addr. This will cause tlv_bootstrap to
be called in DisabledInThisThread() via the asan allocator.
- The first time __tls_get_addr is called, it allocates memory
for the thread-local object, during which it calls malloc(). This
call will be intercepted, leading to an infinite loop in the asan
allocator, in which the allocator calls DisabledInThisThread,
which calls tls_get_addr, which calls into the allocator again.
Reviewers: kcc, glider, kubamracek
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29786
llvm-svn: 294994
2017-02-14 06:20:07 +08:00
|
|
|
|
|
|
|
void EnableInThisThread() {
|
2017-03-29 05:56:45 +08:00
|
|
|
int *disable_counter = &get_tls_val(true)->disable_counter;
|
Use pthreads to manage thread-local storage on darwin for leak sanitizer
Summary:
__thread is supported on Darwin, but is implemented dynamically via
function calls to __tls_get_addr. This causes two issues when combined
with leak sanitizer, due to malloc() interception.
- The dynamic loader calls malloc during the process of loading
the sanitizer dylib, while swapping a placeholder tlv_boostrap
function for __tls_get_addr. This will cause tlv_bootstrap to
be called in DisabledInThisThread() via the asan allocator.
- The first time __tls_get_addr is called, it allocates memory
for the thread-local object, during which it calls malloc(). This
call will be intercepted, leading to an infinite loop in the asan
allocator, in which the allocator calls DisabledInThisThread,
which calls tls_get_addr, which calls into the allocator again.
Reviewers: kcc, glider, kubamracek
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29786
llvm-svn: 294994
2017-02-14 06:20:07 +08:00
|
|
|
if (*disable_counter == 0) {
|
|
|
|
DisableCounterUnderflow();
|
|
|
|
}
|
|
|
|
--*disable_counter;
|
|
|
|
}
|
|
|
|
|
2017-04-12 03:57:12 +08:00
|
|
|
u32 GetCurrentThread() {
|
|
|
|
thread_local_data_t *data = get_tls_val(false);
|
|
|
|
CHECK(data);
|
|
|
|
return data->current_thread_id;
|
|
|
|
}
|
2017-02-17 11:23:07 +08:00
|
|
|
|
2017-03-29 05:56:45 +08:00
|
|
|
void SetCurrentThread(u32 tid) { get_tls_val(true)->current_thread_id = tid; }
|
2017-02-17 11:23:07 +08:00
|
|
|
|
2017-03-29 05:56:45 +08:00
|
|
|
AllocatorCache *GetAllocatorCache() { return &get_tls_val(true)->cache; }
|
2017-03-27 22:07:50 +08:00
|
|
|
|
2017-03-30 05:49:13 +08:00
|
|
|
// Required on Linux for initialization of TLS behavior, but should not be
|
|
|
|
// required on Darwin.
|
|
|
|
void InitializePlatformSpecificModules() {}
|
2017-02-14 05:03:15 +08:00
|
|
|
|
|
|
|
// Scans global variables for heap pointers.
|
|
|
|
void ProcessGlobalRegions(Frontier *frontier) {
|
|
|
|
CHECK(0 && "unimplemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProcessPlatformSpecificAllocations(Frontier *frontier) {
|
|
|
|
CHECK(0 && "unimplemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoStopTheWorld(StopTheWorldCallback callback, void *argument) {
|
|
|
|
CHECK(0 && "unimplemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __lsan
|
|
|
|
|
|
|
|
#endif // CAN_SANITIZE_LEAKS && SANITIZER_MAC
|