[PATCH] D31306: Postpone lsan tls allocation until required

Francis Ricci via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 23 14:11:48 PDT 2017


fjricci created this revision.

This prevents InternalAlloc from being called before the sanitizers
are fully initialized.


https://reviews.llvm.org/D31306

Files:
  lib/lsan/lsan_common_mac.cc


Index: lib/lsan/lsan_common_mac.cc
===================================================================
--- lib/lsan/lsan_common_mac.cc
+++ lib/lsan/lsan_common_mac.cc
@@ -35,11 +35,11 @@
 
 static void make_tls_key() { CHECK_EQ(pthread_key_create(&key, NULL), 0); }
 
-static thread_local_data_t *get_tls_val() {
+static thread_local_data_t *get_tls_val(bool alloc) {
   pthread_once(&key_once, make_tls_key);
 
   thread_local_data_t *ptr = (thread_local_data_t *)pthread_getspecific(key);
-  if (ptr == NULL) {
+  if (ptr == NULL && alloc) {
     ptr = (thread_local_data_t *)InternalAlloc(sizeof(*ptr));
     ptr->disable_counter = 0;
     ptr->current_thread_id = kInvalidTid;
@@ -50,23 +50,26 @@
   return ptr;
 }
 
-bool DisabledInThisThread() { return get_tls_val()->disable_counter > 0; }
+bool DisabledInThisThread() {
+  thread_local_data_t *data = get_tls_val(false);
+  return data ? data->disable_counter > 0 : false;
+}
 
-void DisableInThisThread() { ++get_tls_val()->disable_counter; }
+void DisableInThisThread() { ++get_tls_val(true)->disable_counter; }
 
 void EnableInThisThread() {
-  int *disable_counter = &get_tls_val()->disable_counter;
+  int *disable_counter = &get_tls_val(true)->disable_counter;
   if (*disable_counter == 0) {
     DisableCounterUnderflow();
   }
   --*disable_counter;
 }
 
-u32 GetCurrentThread() { return get_tls_val()->current_thread_id; }
+u32 GetCurrentThread() { return get_tls_val(true)->current_thread_id; }
 
-void SetCurrentThread(u32 tid) { get_tls_val()->current_thread_id = tid; }
+void SetCurrentThread(u32 tid) { get_tls_val(true)->current_thread_id = tid; }
 
-AllocatorCache *GetAllocatorCache() { return &get_tls_val()->cache; }
+AllocatorCache *GetAllocatorCache() { return &get_tls_val(true)->cache; }
 
 void InitializePlatformSpecificModules() {
   CHECK(0 && "unimplemented");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31306.92863.patch
Type: text/x-patch
Size: 1848 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170323/5179e31f/attachment.bin>


More information about the llvm-commits mailing list