[scudo] Calling iterateOverChunks requires holding lock
authorChia-hung Duan <chiahungduan@google.com>
Wed, 15 Feb 2023 01:31:20 +0000 (01:31 +0000)
committerChia-hung Duan <chiahungduan@google.com>
Wed, 15 Feb 2023 23:44:44 +0000 (23:44 +0000)
Ensure the allocator is disabled before visiting all chunks.

Reviewed By: cferris

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

compiler-rt/lib/scudo/standalone/primary32.h
compiler-rt/lib/scudo/standalone/primary64.h
compiler-rt/lib/scudo/standalone/secondary.h
compiler-rt/lib/scudo/standalone/wrappers_c.inc

index 544f0ee..2a204ca 100644 (file)
@@ -204,11 +204,14 @@ public:
     }
   }
 
-  template <typename F>
-  void iterateOverBlocks(F Callback) NO_THREAD_SAFETY_ANALYSIS {
+  template <typename F> void iterateOverBlocks(F Callback) {
     uptr MinRegionIndex = NumRegions, MaxRegionIndex = 0;
     for (uptr I = 0; I < NumClasses; I++) {
       SizeClassInfo *Sci = getSizeClassInfo(I);
+      // TODO: The call of `iterateOverBlocks` requires disabling
+      // SizeClassAllocator32. We may consider locking each region on demand
+      // only.
+      Sci->Mutex.assertHeld();
       if (Sci->MinRegionIndex < MinRegionIndex)
         MinRegionIndex = Sci->MinRegionIndex;
       if (Sci->MaxRegionIndex > MaxRegionIndex)
index 1c9c615..3c53c03 100644 (file)
@@ -226,12 +226,15 @@ public:
     }
   }
 
-  template <typename F>
-  void iterateOverBlocks(F Callback) NO_THREAD_SAFETY_ANALYSIS {
+  template <typename F> void iterateOverBlocks(F Callback) {
     for (uptr I = 0; I < NumClasses; I++) {
       if (I == SizeClassMap::BatchClassId)
         continue;
       RegionInfo *Region = getRegionInfo(I);
+      // TODO: The call of `iterateOverBlocks` requires disabling
+      // SizeClassAllocator64. We may consider locking each region on demand
+      // only.
+      Region->Mutex.assertHeld();
       const uptr BlockSize = getSizeByClassId(I);
       const uptr From = Region->RegionBeg;
       const uptr To = From + Region->AllocatedUser;
index e98e020..3a11c47 100644 (file)
@@ -456,8 +456,9 @@ public:
     Mutex.unlock();
   }
 
-  template <typename F>
-  void iterateOverBlocks(F Callback) const NO_THREAD_SAFETY_ANALYSIS {
+  template <typename F> void iterateOverBlocks(F Callback) const {
+    Mutex.assertHeld();
+
     for (const auto &H : InUseBlocks) {
       uptr Ptr = reinterpret_cast<uptr>(&H) + LargeBlock::getHeaderSize();
       if (allocatorSupportsMemoryTagging<Config>())
index bbe3617..6c4f10d 100644 (file)
@@ -238,7 +238,10 @@ INTERFACE WEAK int SCUDO_PREFIX(malloc_info)(UNUSED int options, FILE *stream) {
     if (size < max_size)
       sizes[size]++;
   };
+
+  SCUDO_ALLOCATOR.disable();
   SCUDO_ALLOCATOR.iterateOverChunks(0, -1ul, callback, sizes);
+  SCUDO_ALLOCATOR.enable();
 
   fputs("<malloc version=\"scudo-1\">\n", stream);
   for (scudo::uptr i = 0; i != max_size; ++i)