Use pthreads for thread-local lsan allocator cache on darwin
authorFrancis Ricci <francisjricci@gmail.com>
Mon, 27 Mar 2017 14:07:50 +0000 (14:07 +0000)
committerFrancis Ricci <francisjricci@gmail.com>
Mon, 27 Mar 2017 14:07:50 +0000 (14:07 +0000)
Summary:
This patch allows us to move away from using __thread on darwin,
which is requiring for building lsan for darwin on ios version 7
and on iossim i386.

Reviewers: kubamracek, kcc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D31291

llvm-svn: 298848

compiler-rt/lib/lsan/lsan_allocator.cc
compiler-rt/lib/lsan/lsan_allocator.h
compiler-rt/lib/lsan/lsan_common_linux.cc
compiler-rt/lib/lsan/lsan_common_mac.cc

index a08dcf286f408a66710e1c94d94246b016e2c7e9..640497d401ea71ca6ad1266dc05c1ac2d9172e20 100644 (file)
 extern "C" void *memset(void *ptr, int value, uptr num);
 
 namespace __lsan {
-
-struct ChunkMetadata {
-  u8 allocated : 8;  // Must be first.
-  ChunkTag tag : 2;
-#if SANITIZER_WORDSIZE == 64
-  uptr requested_size : 54;
-#else
-  uptr requested_size : 32;
-  uptr padding : 22;
-#endif
-  u32 stack_trace_id;
-};
-
-#if defined(__mips64) || defined(__aarch64__) || defined(__i386__)
 #if defined(__i386__)
 static const uptr kMaxAllowedMallocSize = 1UL << 30;
-#else
+#elif defined(__mips64) || defined(__aarch64__)
 static const uptr kMaxAllowedMallocSize = 4UL << 30;
-#endif
-static const uptr kRegionSizeLog = 20;
-static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
-typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
-typedef CompactSizeClassMap SizeClassMap;
-typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE,
-    sizeof(ChunkMetadata), SizeClassMap, kRegionSizeLog, ByteMap>
-    PrimaryAllocator;
 #else
 static const uptr kMaxAllowedMallocSize = 8UL << 30;
-
-struct AP64 {  // Allocator64 parameters. Deliberately using a short name.
-  static const uptr kSpaceBeg = 0x600000000000ULL;
-  static const uptr kSpaceSize =  0x40000000000ULL; // 4T.
-  static const uptr kMetadataSize = sizeof(ChunkMetadata);
-  typedef DefaultSizeClassMap SizeClassMap;
-  typedef NoOpMapUnmapCallback MapUnmapCallback;
-  static const uptr kFlags = 0;
-};
-
-typedef SizeClassAllocator64<AP64> PrimaryAllocator;
 #endif
-typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
 typedef LargeMmapAllocator<> SecondaryAllocator;
 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
           SecondaryAllocator> Allocator;
 
 static Allocator allocator;
-static THREADLOCAL AllocatorCache allocator_cache;
-AllocatorCache *GetAllocatorCache() { return &allocator_cache; }
 
 void InitializeAllocator() {
   allocator.InitLinkerInitialized(
index f564601193bd3c1ee0bde71e44c06dcef49a17aa..9c3dce97a091dcb960b4eca9dcbedc095ef2aee1 100644 (file)
 #ifndef LSAN_ALLOCATOR_H
 #define LSAN_ALLOCATOR_H
 
+#include "sanitizer_common/sanitizer_allocator.h"
 #include "sanitizer_common/sanitizer_common.h"
 #include "sanitizer_common/sanitizer_internal_defs.h"
+#include "lsan_common.h"
 
 namespace __lsan {
 
@@ -34,6 +36,41 @@ void GetAllocatorCacheRange(uptr *begin, uptr *end);
 void AllocatorThreadFinish();
 void InitializeAllocator();
 
+struct ChunkMetadata {
+  u8 allocated : 8;  // Must be first.
+  ChunkTag tag : 2;
+#if SANITIZER_WORDSIZE == 64
+  uptr requested_size : 54;
+#else
+  uptr requested_size : 32;
+  uptr padding : 22;
+#endif
+  u32 stack_trace_id;
+};
+
+#if defined(__mips64) || defined(__aarch64__) || defined(__i386__)
+static const uptr kRegionSizeLog = 20;
+static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
+typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
+typedef CompactSizeClassMap SizeClassMap;
+typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE,
+    sizeof(ChunkMetadata), SizeClassMap, kRegionSizeLog, ByteMap>
+    PrimaryAllocator;
+#elif defined(__x86_64__)
+struct AP64 {  // Allocator64 parameters. Deliberately using a short name.
+  static const uptr kSpaceBeg = 0x600000000000ULL;
+  static const uptr kSpaceSize =  0x40000000000ULL; // 4T.
+  static const uptr kMetadataSize = sizeof(ChunkMetadata);
+  typedef DefaultSizeClassMap SizeClassMap;
+  typedef NoOpMapUnmapCallback MapUnmapCallback;
+  static const uptr kFlags = 0;
+};
+
+typedef SizeClassAllocator64<AP64> PrimaryAllocator;
+#endif
+typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
+
+AllocatorCache *GetAllocatorCache();
 }  // namespace __lsan
 
 #endif  // LSAN_ALLOCATOR_H
index 0e10d41914eda5c13b5ec526a04605059d85a984..e73768e5824d0d891d7896e21ef7a9a72deb18e6 100644 (file)
@@ -22,6 +22,7 @@
 #include "sanitizer_common/sanitizer_flags.h"
 #include "sanitizer_common/sanitizer_linux.h"
 #include "sanitizer_common/sanitizer_stackdepot.h"
+#include "lsan_allocator.h"
 
 namespace __lsan {
 
@@ -38,6 +39,9 @@ static THREADLOCAL u32 current_thread_tid = kInvalidTid;
 u32 GetCurrentThread() { return current_thread_tid; }
 void SetCurrentThread(u32 tid) { current_thread_tid = tid; }
 
+static THREADLOCAL AllocatorCache allocator_cache;
+AllocatorCache *GetAllocatorCache() { return &allocator_cache; }
+
 __attribute__((tls_model("initial-exec")))
 THREADLOCAL int disable_counter;
 bool DisabledInThisThread() { return disable_counter > 0; }
index 7f5e0550dc830803157497352529c9593e42434e..f70ebdd0e4cd6e44eff9a56af7ac34e9452838cb 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
-#include "sanitizer_common/sanitizer_allocator_internal.h"
 #include "sanitizer_common/sanitizer_platform.h"
 #include "lsan_common.h"
 
 #if CAN_SANITIZE_LEAKS && SANITIZER_MAC
 
+#include "sanitizer_common/sanitizer_allocator_internal.h"
+#include "lsan_allocator.h"
+
 #include <pthread.h>
 
 namespace __lsan {
@@ -25,6 +27,7 @@ namespace __lsan {
 typedef struct {
   int disable_counter;
   u32 current_thread_id;
+  AllocatorCache cache;
 } thread_local_data_t;
 
 static pthread_key_t key;
@@ -40,6 +43,7 @@ static thread_local_data_t *get_tls_val() {
     ptr = (thread_local_data_t *)InternalAlloc(sizeof(*ptr));
     ptr->disable_counter = 0;
     ptr->current_thread_id = kInvalidTid;
+    ptr->cache = AllocatorCache();
     pthread_setspecific(key, ptr);
   }
 
@@ -62,6 +66,8 @@ u32 GetCurrentThread() { return get_tls_val()->current_thread_id; }
 
 void SetCurrentThread(u32 tid) { get_tls_val()->current_thread_id = tid; }
 
+AllocatorCache *GetAllocatorCache() { return &get_tls_val()->cache; }
+
 void InitializePlatformSpecificModules() {
   CHECK(0 && "unimplemented");
 }