[LSan] Factor lsan allocator cache accesses into a function 49/192249/5
authorFrancis Ricci <francisjricci@gmail.com>
Thu, 25 Oct 2018 11:26:37 +0000 (14:26 +0300)
committerVyacheslav Cherkashin <v.cherkashin@samsung.com>
Fri, 11 Jan 2019 14:04:30 +0000 (17:04 +0300)
Summary:
This patch is the first step towards allows us to move away from using
__thread for the allocator cache on darwin,
which is requiring for building lsan for darwin on ios version 7
and on iossim i386.

This will be followed by patches to move the function into OS-specific files.

Reviewers: kubamracek, kcc

Subscribers: llvm-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@298537 91177308-0d34-0410-b5e6-96231b3b80d8

Backport:
  http://llvm.org/git/compiler-rt 65ede46c

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

index 2203506..e711766 100644 (file)
@@ -76,7 +76,8 @@ typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
           SecondaryAllocator> Allocator;
 
 static Allocator allocator;
-static THREADLOCAL AllocatorCache cache;
+static THREADLOCAL AllocatorCache allocator_cache;
+AllocatorCache *GetAllocatorCache() { return &allocator_cache; }
 
 void InitializeAllocator() {
   SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null);
@@ -85,7 +86,7 @@ void InitializeAllocator() {
 }
 
 void AllocatorThreadFinish() {
-  allocator.SwallowCache(&cache);
+  allocator.SwallowCache(GetAllocatorCache());
 }
 
 static ChunkMetadata *Metadata(const void *p) {
@@ -117,7 +118,7 @@ void *Allocate(const StackTrace &stack, uptr size, uptr alignment,
     Report("WARNING: LeakSanitizer failed to allocate %zu bytes\n", size);
     return nullptr;
   }
-  void *p = allocator.Allocate(&cache, size, alignment);
+  void *p = allocator.Allocate(GetAllocatorCache(), size, alignment);
   // Do not rely on the allocator to clear the memory (it's slow).
   if (cleared && allocator.FromPrimary(p))
     memset(p, 0, size);
@@ -131,7 +132,7 @@ void Deallocate(void *p) {
   if (&__sanitizer_free_hook) __sanitizer_free_hook(p);
   RunFreeHooks(p);
   RegisterDeallocation(p);
-  allocator.Deallocate(&cache, p);
+  allocator.Deallocate(GetAllocatorCache(), p);
 }
 
 void *Reallocate(const StackTrace &stack, void *p, uptr new_size,
@@ -139,17 +140,17 @@ void *Reallocate(const StackTrace &stack, void *p, uptr new_size,
   RegisterDeallocation(p);
   if (new_size > kMaxAllowedMallocSize) {
     Report("WARNING: LeakSanitizer failed to allocate %zu bytes\n", new_size);
-    allocator.Deallocate(&cache, p);
+    allocator.Deallocate(GetAllocatorCache(), p);
     return nullptr;
   }
-  p = allocator.Reallocate(&cache, p, new_size, alignment);
+  p = allocator.Reallocate(GetAllocatorCache(), p, new_size, alignment);
   RegisterAllocation(stack, p, new_size);
   return p;
 }
 
 void GetAllocatorCacheRange(uptr *begin, uptr *end) {
-  *begin = (uptr)&cache;
-  *end = *begin + sizeof(cache);
+  *begin = (uptr)GetAllocatorCache();
+  *end = *begin + sizeof(AllocatorCache);
 }
 
 uptr GetMallocUsableSize(const void *p) {