[LSan] Add dynamic loading support for LSan library 50/192250/6
authorVyacheslav Cherkashin <v.cherkashin@samsung.com>
Wed, 31 Oct 2018 14:52:22 +0000 (17:52 +0300)
committerVyacheslav Cherkashin <v.cherkashin@samsung.com>
Fri, 11 Jan 2019 14:04:38 +0000 (17:04 +0300)
The LSan library has very big static TLS section

Problem:
  - LSan library could not be loaded dynamically because it had a very
    big static TLS section.
        AllocatorCache: ~15KB for 32bit
                        ~53KB for 64bit
Solution:
  - Store in the TLS section only a pointer to the AllocatorCache and
    dynamically allocate memory for it.

Change-Id: I6e90e6682e277c90825938f5732867b6564d62b4
Signed-off-by: Vyacheslav Cherkashin <v.cherkashin@samsung.com>
libsanitizer/lsan/lsan_allocator.cc

index e711766..00b6a15 100644 (file)
@@ -17,6 +17,7 @@
 #include "sanitizer_common/sanitizer_internal_defs.h"
 #include "sanitizer_common/sanitizer_stackdepot.h"
 #include "sanitizer_common/sanitizer_stacktrace.h"
+#include "sanitizer_common/sanitizer_allocator_internal.h"
 #include "lsan_common.h"
 
 extern "C" void *memset(void *ptr, int value, uptr num);
@@ -76,8 +77,15 @@ typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
           SecondaryAllocator> Allocator;
 
 static Allocator allocator;
-static THREADLOCAL AllocatorCache allocator_cache;
-AllocatorCache *GetAllocatorCache() { return &allocator_cache; }
+static THREADLOCAL AllocatorCache *allocator_cache;
+AllocatorCache *GetAllocatorCache() {
+  if (UNLIKELY(allocator_cache == nullptr)) {
+    // We will not release the memory because the library is not unloaded.
+    allocator_cache = (AllocatorCache *)InternalAlloc(sizeof(*allocator_cache));
+    *allocator_cache = AllocatorCache();
+  }
+  return allocator_cache;
+}
 
 void InitializeAllocator() {
   SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null);