[hwasan] Fix allocator_interface implementation
authorVitaly Buka <vitalybuka@google.com>
Wed, 24 May 2023 23:51:12 +0000 (16:51 -0700)
committerVitaly Buka <vitalybuka@google.com>
Thu, 25 May 2023 01:05:42 +0000 (18:05 -0700)
__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/D151389

compiler-rt/lib/hwasan/hwasan_allocator.cpp
compiler-rt/lib/hwasan/hwasan_allocator.h
compiler-rt/lib/hwasan/hwasan_thread.cpp
compiler-rt/test/sanitizer_common/TestCases/allocator_interface.cpp

index b81f1e3..d3e8266 100644 (file)
@@ -149,8 +149,9 @@ void HwasanAllocatorInit() {
   atomic_store_relaxed(&hwasan_allocator_tagging_enabled,
                        !flags()->disable_allocator_tagging);
   SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null);
-  allocator.Init(common_flags()->allocator_release_to_os_interval_ms,
-                 GetAliasRegionStart());
+  allocator.InitLinkerInitialized(
+      common_flags()->allocator_release_to_os_interval_ms,
+      GetAliasRegionStart());
   for (uptr i = 0; i < sizeof(tail_magic); i++)
     tail_magic[i] = GetCurrentThread()->GenerateRandomTag();
   if (common_flags()->max_allocation_size_mb) {
@@ -165,8 +166,11 @@ void HwasanAllocatorLock() { allocator.ForceLock(); }
 
 void HwasanAllocatorUnlock() { allocator.ForceUnlock(); }
 
+void AllocatorThreadStart(AllocatorCache *cache) { allocator.InitCache(cache); }
+
 void AllocatorThreadFinish(AllocatorCache *cache) {
   allocator.SwallowCache(cache);
+  allocator.DestroyCache(cache);
 }
 
 static uptr TaggedSize(uptr size) {
index 0168b49..efe253d 100644 (file)
@@ -88,6 +88,7 @@ typedef SizeClassAllocator64<AP64> PrimaryAllocator;
 typedef CombinedAllocator<PrimaryAllocator> Allocator;
 typedef Allocator::AllocatorCache AllocatorCache;
 
+void AllocatorThreadStart(AllocatorCache *cache);
 void AllocatorThreadFinish(AllocatorCache *cache);
 
 class HwasanChunkView {
index b8a9d28..f5eb79f 100644 (file)
@@ -58,6 +58,7 @@ void Thread::Init(uptr stack_buffer_start, uptr stack_buffer_size,
 #endif
   InitStackAndTls(state);
   dtls_ = DTLS_Get();
+  AllocatorThreadStart(allocator_cache());
 }
 
 void Thread::InitStackRingBuffer(uptr stack_buffer_start,
index 9d98fca..c2f3044 100644 (file)
@@ -4,9 +4,6 @@
 // No allocator.
 // UNSUPPORTED: ubsan
 
-// FIXME: implementation is incomplete.
-// XFAIL: hwasan
-
 #include <assert.h>
 #include <sanitizer/allocator_interface.h>
 #include <stdio.h>