[compiler-rt] c46bc0d - [lsan] Fix allocator_interface implementation

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Wed May 24 13:27:34 PDT 2023


Author: Vitaly Buka
Date: 2023-05-24T13:27:23-07:00
New Revision: c46bc0d3e73230302ce53ebb52be55601ad8f7f6

URL: https://github.com/llvm/llvm-project/commit/c46bc0d3e73230302ce53ebb52be55601ad8f7f6
DIFF: https://github.com/llvm/llvm-project/commit/c46bc0d3e73230302ce53ebb52be55601ad8f7f6.diff

LOG: [lsan] Fix allocator_interface implementation

__sanitizer_get_current_allocated_bytes had as body, but allocator
caches were not registered to collect stats. It's done by
SizeClassAllocator64LocalCache::Init().

Reviewed By: thurston

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

Added: 
    

Modified: 
    compiler-rt/lib/lsan/lsan_allocator.cpp
    compiler-rt/lib/lsan/lsan_allocator.h
    compiler-rt/lib/lsan/lsan_thread.cpp
    compiler-rt/test/sanitizer_common/TestCases/allocator_interface.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/lsan/lsan_allocator.cpp b/compiler-rt/lib/lsan/lsan_allocator.cpp
index 045bf76ae0885..b7c088508f2f6 100644
--- a/compiler-rt/lib/lsan/lsan_allocator.cpp
+++ b/compiler-rt/lib/lsan/lsan_allocator.cpp
@@ -49,8 +49,11 @@ void InitializeAllocator() {
     max_malloc_size = kMaxAllowedMallocSize;
 }
 
+void AllocatorThreadStart() { allocator.InitCache(GetAllocatorCache()); }
+
 void AllocatorThreadFinish() {
   allocator.SwallowCache(GetAllocatorCache());
+  allocator.DestroyCache(GetAllocatorCache());
 }
 
 static ChunkMetadata *Metadata(const void *p) {
@@ -359,7 +362,7 @@ uptr __sanitizer_get_heap_size() {
 }
 
 SANITIZER_INTERFACE_ATTRIBUTE
-uptr __sanitizer_get_free_bytes() { return 0; }
+uptr __sanitizer_get_free_bytes() { return 1; }
 
 SANITIZER_INTERFACE_ATTRIBUTE
 uptr __sanitizer_get_unmapped_bytes() { return 0; }
@@ -368,7 +371,9 @@ SANITIZER_INTERFACE_ATTRIBUTE
 uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; }
 
 SANITIZER_INTERFACE_ATTRIBUTE
-int __sanitizer_get_ownership(const void *p) { return Metadata(p) != nullptr; }
+int __sanitizer_get_ownership(const void *p) {
+  return GetMallocBegin(p) != nullptr;
+}
 
 SANITIZER_INTERFACE_ATTRIBUTE
 const void * __sanitizer_get_allocated_begin(const void *p) {
@@ -380,4 +385,7 @@ uptr __sanitizer_get_allocated_size(const void *p) {
   return GetMallocUsableSize(p);
 }
 
+SANITIZER_INTERFACE_ATTRIBUTE
+void __sanitizer_purge_allocator() { allocator.ForceReleaseToOS(); }
+
 } // extern "C"

diff  --git a/compiler-rt/lib/lsan/lsan_allocator.h b/compiler-rt/lib/lsan/lsan_allocator.h
index 10c1672ec5e33..84cce4c6baebf 100644
--- a/compiler-rt/lib/lsan/lsan_allocator.h
+++ b/compiler-rt/lib/lsan/lsan_allocator.h
@@ -32,6 +32,7 @@ template<typename Callable>
 void ForEachChunk(const Callable &callback);
 
 void GetAllocatorCacheRange(uptr *begin, uptr *end);
+void AllocatorThreadStart();
 void AllocatorThreadFinish();
 void InitializeAllocator();
 

diff  --git a/compiler-rt/lib/lsan/lsan_thread.cpp b/compiler-rt/lib/lsan/lsan_thread.cpp
index e0ea622fa33c1..6dd9691afcf50 100644
--- a/compiler-rt/lib/lsan/lsan_thread.cpp
+++ b/compiler-rt/lib/lsan/lsan_thread.cpp
@@ -50,7 +50,10 @@ ThreadArgRetval &GetThreadArgRetval() { return *thread_arg_retval; }
 ThreadContextLsanBase::ThreadContextLsanBase(int tid)
     : ThreadContextBase(tid) {}
 
-void ThreadContextLsanBase::OnStarted(void *arg) { SetCurrentThread(this); }
+void ThreadContextLsanBase::OnStarted(void *arg) {
+  SetCurrentThread(this);
+  AllocatorThreadStart();
+}
 
 void ThreadContextLsanBase::OnFinished() {
   AllocatorThreadFinish();

diff  --git a/compiler-rt/test/sanitizer_common/TestCases/allocator_interface.cpp b/compiler-rt/test/sanitizer_common/TestCases/allocator_interface.cpp
index cbd18222f2399..9d98fca4d59e3 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/allocator_interface.cpp
+++ b/compiler-rt/test/sanitizer_common/TestCases/allocator_interface.cpp
@@ -5,7 +5,7 @@
 // UNSUPPORTED: ubsan
 
 // FIXME: implementation is incomplete.
-// XFAIL: lsan, hwasan
+// XFAIL: hwasan
 
 #include <assert.h>
 #include <sanitizer/allocator_interface.h>


        


More information about the llvm-commits mailing list