[PATCH] D71291: scudo: Move getChunkFromBlock() allocated check into caller. NFCI.

Peter Collingbourne via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 10 11:23:27 PST 2019


pcc created this revision.
pcc added a reviewer: cryptoad.
Herald added projects: Sanitizers, LLVM.
Herald added a subscriber: Sanitizers.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D71291

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


Index: compiler-rt/lib/scudo/standalone/combined.h
===================================================================
--- compiler-rt/lib/scudo/standalone/combined.h
+++ compiler-rt/lib/scudo/standalone/combined.h
@@ -418,10 +418,11 @@
     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 @@
   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 @@
     }
   }
 
-  // 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) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71291.233155.patch
Type: text/x-patch
Size: 2180 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191210/2e5b9938/attachment-0001.bin>


More information about the llvm-commits mailing list