[compiler-rt] r365816 - [scudo][standalone] NFC corrections

Kostya Kortchinsky via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 11 12:55:53 PDT 2019


Author: cryptoad
Date: Thu Jul 11 12:55:53 2019
New Revision: 365816

URL: http://llvm.org/viewvc/llvm-project?rev=365816&view=rev
Log:
[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

Modified:
    compiler-rt/trunk/lib/scudo/standalone/chunk.h
    compiler-rt/trunk/lib/scudo/standalone/combined.h
    compiler-rt/trunk/lib/scudo/standalone/local_cache.h
    compiler-rt/trunk/lib/scudo/standalone/primary32.h
    compiler-rt/trunk/lib/scudo/standalone/primary64.h
    compiler-rt/trunk/lib/scudo/standalone/report.cc
    compiler-rt/trunk/lib/scudo/standalone/tests/chunk_test.cc

Modified: compiler-rt/trunk/lib/scudo/standalone/chunk.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/standalone/chunk.h?rev=365816&r1=365815&r2=365816&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/standalone/chunk.h (original)
+++ compiler-rt/trunk/lib/scudo/standalone/chunk.h Thu Jul 11 12:55:53 2019
@@ -97,12 +97,6 @@ const AtomicPackedHeader *getConstAtomic
       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.

Modified: compiler-rt/trunk/lib/scudo/standalone/combined.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/standalone/combined.h?rev=365816&r1=365815&r2=365816&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/standalone/combined.h (original)
+++ compiler-rt/trunk/lib/scudo/standalone/combined.h Thu Jul 11 12:55:53 2019
@@ -45,7 +45,7 @@ public:
       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 @@ private:
       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 @@ private:
     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;

Modified: compiler-rt/trunk/lib/scudo/standalone/local_cache.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/standalone/local_cache.h?rev=365816&r1=365815&r2=365816&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/standalone/local_cache.h (original)
+++ compiler-rt/trunk/lib/scudo/standalone/local_cache.h Thu Jul 11 12:55:53 2019
@@ -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 @@ template <class SizeClassAllocator> stru
       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 @@ private:
     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 @@ private:
     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);

Modified: compiler-rt/trunk/lib/scudo/standalone/primary32.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/standalone/primary32.h?rev=365816&r1=365815&r2=365816&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/standalone/primary32.h (original)
+++ compiler-rt/trunk/lib/scudo/standalone/primary32.h Thu Jul 11 12:55:53 2019
@@ -162,7 +162,9 @@ public:
   }
 
   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 @@ private:
       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);

Modified: compiler-rt/trunk/lib/scudo/standalone/primary64.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/standalone/primary64.h?rev=365816&r1=365815&r2=365816&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/standalone/primary64.h (original)
+++ compiler-rt/trunk/lib/scudo/standalone/primary64.h Thu Jul 11 12:55:53 2019
@@ -166,7 +166,9 @@ public:
   }
 
   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 @@ private:
   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;

Modified: compiler-rt/trunk/lib/scudo/standalone/report.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/standalone/report.cc?rev=365816&r1=365815&r2=365816&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/standalone/report.cc (original)
+++ compiler-rt/trunk/lib/scudo/standalone/report.cc Thu Jul 11 12:55:53 2019
@@ -52,7 +52,7 @@ void NORETURN reportCheckFailed(const ch
 // 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) {

Modified: compiler-rt/trunk/lib/scudo/standalone/tests/chunk_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/standalone/tests/chunk_test.cc?rev=365816&r1=365815&r2=365816&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/standalone/tests/chunk_test.cc (original)
+++ compiler-rt/trunk/lib/scudo/standalone/tests/chunk_test.cc Thu Jul 11 12:55:53 2019
@@ -30,7 +30,6 @@ TEST(ScudoChunkTest, ChunkBasic) {
                                      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 @@ TEST(ScudoChunkTest, CorruptHeader) {
                                      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++) {




More information about the llvm-commits mailing list