[compiler-rt] e966416 - scudo: Move getChunkFromBlock() allocated check into caller. NFCI.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 10 12:09:35 PST 2019


Author: Peter Collingbourne
Date: 2019-12-10T12:08:52-08:00
New Revision: e966416ff15178bf982430085be9ae69e5a511fa

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

LOG: scudo: Move getChunkFromBlock() allocated check into caller. NFCI.

With tag-on-free we will need to get the chunk of a deallocated block. Change
getChunkFromBlock() so that it doesn't check that the chunk is allocated,
and move the check into the caller, so that it can be reused for this purpose.

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

Added: 
    

Modified: 
    compiler-rt/lib/scudo/standalone/combined.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/scudo/standalone/combined.h b/compiler-rt/lib/scudo/standalone/combined.h
index b355a4746fae..02c998e666de 100644
--- a/compiler-rt/lib/scudo/standalone/combined.h
+++ b/compiler-rt/lib/scudo/standalone/combined.h
@@ -418,10 +418,11 @@ template <class Params> class Allocator {
     auto Lambda = [this, From, To, Callback, Arg](uptr Block) {
       if (Block < From || Block >= To)
         return;
-      uptr ChunkSize;
-      const uptr ChunkBase = getChunkFromBlock(Block, &ChunkSize);
-      if (ChunkBase != InvalidChunk)
-        Callback(ChunkBase, ChunkSize, Arg);
+      uptr Chunk;
+      Chunk::UnpackedHeader Header;
+      if (getChunkFromBlock(Block, &Chunk, &Header) &&
+          Header.State == Chunk::State::Allocated)
+        Callback(Chunk, getSize(reinterpret_cast<void *>(Chunk), &Header), Arg);
     };
     Primary.iterateOverBlocks(Lambda);
     Secondary.iterateOverBlocks(Lambda);
@@ -483,9 +484,7 @@ template <class Params> class Allocator {
   static_assert(MinAlignment >= sizeof(Chunk::PackedHeader),
                 "Minimal alignment must at least cover a chunk header.");
 
-  // Constants used by the chunk iteration mechanism.
   static const u32 BlockMarker = 0x44554353U;
-  static const uptr InvalidChunk = ~static_cast<uptr>(0);
 
   GlobalStats Stats;
   TSDRegistryT TSDRegistry;
@@ -593,20 +592,13 @@ template <class Params> class Allocator {
     }
   }
 
-  // This only cares about valid busy chunks. This might change in the future.
-  uptr getChunkFromBlock(uptr Block, uptr *Size) {
+  bool getChunkFromBlock(uptr Block, uptr *Chunk,
+                         Chunk::UnpackedHeader *Header) {
     u32 Offset = 0;
     if (reinterpret_cast<u32 *>(Block)[0] == BlockMarker)
       Offset = reinterpret_cast<u32 *>(Block)[1];
-    const uptr P = Block + Offset + Chunk::getHeaderSize();
-    const void *Ptr = reinterpret_cast<const void *>(P);
-    Chunk::UnpackedHeader Header;
-    if (!Chunk::isValid(Cookie, Ptr, &Header) ||
-        Header.State != Chunk::State::Allocated)
-      return InvalidChunk;
-    if (Size)
-      *Size = getSize(Ptr, &Header);
-    return P;
+    *Chunk = Block + Offset + Chunk::getHeaderSize();
+    return Chunk::isValid(Cookie, reinterpret_cast<void *>(*Chunk), Header);
   }
 
   uptr getStats(ScopedString *Str) {


        


More information about the llvm-commits mailing list