[scudo][standalone] NFC corrections

Summary:
A few corrections:
- rename `TransferBatch::MaxCached` to `getMaxCached` to conform with
  the style guide;
- move `getBlockBegin` from `Chunk::` to `Allocator::`: I believe it
  was a fallacy to have this be a `Chunk` method, as chunks'
  relationship to backend blocks are up to the frontend allocator. It
  makes more sense now, particularly with regard to the offset. Update
  the associated chunk test as the method isn't available there
  anymore;
- add a forgotten `\n` to a log string;
- for `releaseToOs`, instead of starting at `1`, start at `0` and
  `continue` on `BatchClassId`: in the end it's identical but doesn't
  assume a particular class id for batches;
- change a `CHECK` to a `reportOutOfMemory`: it's a clearer message

Reviewers: hctim, morehouse, eugenis, vitalybuka

Reviewed By: hctim

Subscribers: delcypher, #sanitizers, llvm-commits

Tags: #llvm, #sanitizers

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

llvm-svn: 365816
diff --git a/compiler-rt/lib/scudo/standalone/chunk.h b/compiler-rt/lib/scudo/standalone/chunk.h
index ec0b0ee..76ef661 100644
--- a/compiler-rt/lib/scudo/standalone/chunk.h
+++ b/compiler-rt/lib/scudo/standalone/chunk.h
@@ -97,12 +97,6 @@
       reinterpret_cast<uptr>(Ptr) - getHeaderSize());
 }
 
-INLINE void *getBlockBegin(const void *Ptr, UnpackedHeader *Header) {
-  return reinterpret_cast<void *>(reinterpret_cast<uptr>(Ptr) -
-                                  getHeaderSize() -
-                                  (Header->Offset << SCUDO_MIN_ALIGNMENT_LOG));
-}
-
 // We do not need a cryptographically strong hash for the checksum, but a CRC
 // type function that can alert us in the event a header is invalid or
 // corrupted. Ideally slightly better than a simple xor of all fields.
diff --git a/compiler-rt/lib/scudo/standalone/combined.h b/compiler-rt/lib/scudo/standalone/combined.h
index fbbc7c8..4c1c119 100644
--- a/compiler-rt/lib/scudo/standalone/combined.h
+++ b/compiler-rt/lib/scudo/standalone/combined.h
@@ -45,7 +45,7 @@
       NewHeader.State = Chunk::State::Available;
       Chunk::compareExchangeHeader(Allocator.Cookie, Ptr, &NewHeader, &Header);
 
-      void *BlockBegin = Chunk::getBlockBegin(Ptr, &Header);
+      void *BlockBegin = Allocator::getBlockBegin(Ptr, &NewHeader);
       const uptr ClassId = Header.ClassId;
       if (ClassId)
         Cache.deallocate(ClassId, BlockBegin);
@@ -482,12 +482,19 @@
       reportSanityCheckError("class ID");
   }
 
+  static INLINE void *getBlockBegin(const void *Ptr,
+                                    Chunk::UnpackedHeader *Header) {
+    return reinterpret_cast<void *>(reinterpret_cast<uptr>(Ptr) -
+                                    Chunk::getHeaderSize() -
+                                    (Header->Offset << MinAlignmentLog));
+  }
+
   // Return the size of a chunk as requested during its allocation.
   INLINE uptr getSize(const void *Ptr, Chunk::UnpackedHeader *Header) {
     const uptr SizeOrUnusedBytes = Header->SizeOrUnusedBytes;
     if (Header->ClassId)
       return SizeOrUnusedBytes;
-    return SecondaryT::getBlockEnd(Chunk::getBlockBegin(Ptr, Header)) -
+    return SecondaryT::getBlockEnd(getBlockBegin(Ptr, Header)) -
            reinterpret_cast<uptr>(Ptr) - SizeOrUnusedBytes;
   }
 
@@ -505,7 +512,7 @@
     if (BypassQuarantine) {
       NewHeader.State = Chunk::State::Available;
       Chunk::compareExchangeHeader(Cookie, Ptr, &NewHeader, Header);
-      void *BlockBegin = Chunk::getBlockBegin(Ptr, Header);
+      void *BlockBegin = getBlockBegin(Ptr, &NewHeader);
       const uptr ClassId = NewHeader.ClassId;
       if (ClassId) {
         bool UnlockRequired;
diff --git a/compiler-rt/lib/scudo/standalone/local_cache.h b/compiler-rt/lib/scudo/standalone/local_cache.h
index 6d51019..2acc288 100644
--- a/compiler-rt/lib/scudo/standalone/local_cache.h
+++ b/compiler-rt/lib/scudo/standalone/local_cache.h
@@ -10,6 +10,7 @@
 #define SCUDO_LOCAL_CACHE_H_
 
 #include "internal_defs.h"
+#include "report.h"
 #include "stats.h"
 
 namespace scudo {
@@ -39,7 +40,7 @@
       DCHECK_LE(I, Count);
       return Batch[I];
     }
-    static u32 MaxCached(uptr Size) {
+    static u32 getMaxCached(uptr Size) {
       return Min(MaxNumCached, SizeClassMap::getMaxCachedHint(Size));
     }
     TransferBatch *Next;
@@ -140,7 +141,7 @@
     for (uptr I = 0; I < NumClasses; I++) {
       PerClass *P = &PerClassArray[I];
       const uptr Size = SizeClassAllocator::getSizeByClassId(I);
-      P->MaxCount = 2 * TransferBatch::MaxCached(Size);
+      P->MaxCount = 2 * TransferBatch::getMaxCached(Size);
       P->ClassSize = Size;
     }
   }
@@ -166,7 +167,9 @@
     const u32 Count = Min(C->MaxCount / 2, C->Count);
     const uptr FirstIndexToDrain = C->Count - Count;
     TransferBatch *B = createBatch(ClassId, C->Chunks[FirstIndexToDrain]);
-    CHECK(B);
+    if (UNLIKELY(!B))
+      reportOutOfMemory(
+          SizeClassAllocator::getSizeByClassId(SizeClassMap::BatchClassId));
     B->setFromArray(&C->Chunks[FirstIndexToDrain], Count);
     C->Count -= Count;
     Allocator->pushBatch(ClassId, B);
diff --git a/compiler-rt/lib/scudo/standalone/primary32.h b/compiler-rt/lib/scudo/standalone/primary32.h
index eade88a..2b2fa8b 100644
--- a/compiler-rt/lib/scudo/standalone/primary32.h
+++ b/compiler-rt/lib/scudo/standalone/primary32.h
@@ -162,7 +162,9 @@
   }
 
   void releaseToOS() {
-    for (uptr I = 1; I < NumClasses; I++) {
+    for (uptr I = 0; I < NumClasses; I++) {
+      if (I == SizeClassMap::BatchClassId)
+        continue;
       SizeClassInfo *Sci = getSizeClassInfo(I);
       ScopedLock L(Sci->Mutex);
       releaseToOSMaybe(Sci, I, /*Force=*/true);
@@ -291,7 +293,7 @@
       return nullptr;
     C->getStats().add(StatMapped, RegionSize);
     const uptr Size = getSizeByClassId(ClassId);
-    const u32 MaxCount = TransferBatch::MaxCached(Size);
+    const u32 MaxCount = TransferBatch::getMaxCached(Size);
     DCHECK_GT(MaxCount, 0);
     const uptr NumberOfBlocks = RegionSize / Size;
     DCHECK_GT(NumberOfBlocks, 0);
diff --git a/compiler-rt/lib/scudo/standalone/primary64.h b/compiler-rt/lib/scudo/standalone/primary64.h
index 89a43cc..035182b 100644
--- a/compiler-rt/lib/scudo/standalone/primary64.h
+++ b/compiler-rt/lib/scudo/standalone/primary64.h
@@ -166,7 +166,9 @@
   }
 
   void releaseToOS() {
-    for (uptr I = 1; I < NumClasses; I++) {
+    for (uptr I = 0; I < NumClasses; I++) {
+      if (I == SizeClassMap::BatchClassId)
+        continue;
       RegionInfo *Region = getRegionInfo(I);
       ScopedLock L(Region->Mutex);
       releaseToOSMaybe(Region, I, /*Force=*/true);
@@ -249,7 +251,7 @@
   NOINLINE TransferBatch *populateFreeList(CacheT *C, uptr ClassId,
                                            RegionInfo *Region) {
     const uptr Size = getSizeByClassId(ClassId);
-    const u32 MaxCount = TransferBatch::MaxCached(Size);
+    const u32 MaxCount = TransferBatch::getMaxCached(Size);
 
     const uptr RegionBeg = Region->RegionBeg;
     const uptr MappedUser = Region->MappedUser;
diff --git a/compiler-rt/lib/scudo/standalone/report.cc b/compiler-rt/lib/scudo/standalone/report.cc
index 2e453a1..47cd951 100644
--- a/compiler-rt/lib/scudo/standalone/report.cc
+++ b/compiler-rt/lib/scudo/standalone/report.cc
@@ -52,7 +52,7 @@
 // Generic string fatal error message.
 void NORETURN reportError(const char *Message) {
   ScopedErrorReport Report;
-  Report.append("%s", Message);
+  Report.append("%s\n", Message);
 }
 
 void NORETURN reportInvalidFlag(const char *FlagType, const char *Value) {
diff --git a/compiler-rt/lib/scudo/standalone/tests/chunk_test.cc b/compiler-rt/lib/scudo/standalone/tests/chunk_test.cc
index fea975b..c3a21e4 100644
--- a/compiler-rt/lib/scudo/standalone/tests/chunk_test.cc
+++ b/compiler-rt/lib/scudo/standalone/tests/chunk_test.cc
@@ -30,7 +30,6 @@
                                      HeaderSize);
   scudo::Chunk::storeHeader(Cookie, P, &Header);
   memset(P, 'A', Size);
-  EXPECT_EQ(scudo::Chunk::getBlockBegin(P, &Header), Block);
   scudo::Chunk::loadHeader(Cookie, P, &Header);
   EXPECT_TRUE(scudo::Chunk::isValid(Cookie, P, &Header));
   EXPECT_FALSE(scudo::Chunk::isValid(InvalidCookie, P, &Header));
@@ -70,7 +69,6 @@
                                      HeaderSize);
   scudo::Chunk::storeHeader(Cookie, P, &Header);
   memset(P, 'A', Size);
-  EXPECT_EQ(scudo::Chunk::getBlockBegin(P, &Header), Block);
   scudo::Chunk::loadHeader(Cookie, P, &Header);
   // Simulate a couple of corrupted bits per byte of header data.
   for (scudo::uptr I = 0; I < sizeof(scudo::Chunk::PackedHeader); I++) {