[compiler-rt] 9fbfdd2 - scudo: Tweak how we align UserPtr. NFCI.

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


Author: Peter Collingbourne
Date: 2019-12-10T12:09:47-08:00
New Revision: 9fbfdd2bfe42a6d0a8e680c64a968e9cfc065fd3

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

LOG: scudo: Tweak how we align UserPtr. NFCI.

Instead of testing whether the pointer is aligned, just align it
unconditionally and compare it to the original pointer.

This moves the computation of UserPtr up to before we start preparing the
header, so that the memory tagging code will be able to read the original
header containing the bounds of the previous allocation before it gets
potentially clobbered by the pointer realignment code.

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

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 02c998e666de..53e0bf7d7302 100644
--- a/compiler-rt/lib/scudo/standalone/combined.h
+++ b/compiler-rt/lib/scudo/standalone/combined.h
@@ -221,11 +221,13 @@ template <class Params> class Allocator {
     if (UNLIKELY(ZeroContents && ClassId))
       memset(Block, 0, PrimaryT::getSizeByClassId(ClassId));
 
+    const uptr UnalignedUserPtr =
+        reinterpret_cast<uptr>(Block) + Chunk::getHeaderSize();
+    const uptr UserPtr = roundUpTo(UnalignedUserPtr, Alignment);
+
     Chunk::UnpackedHeader Header = {};
-    uptr UserPtr = reinterpret_cast<uptr>(Block) + Chunk::getHeaderSize();
-    if (UNLIKELY(!isAligned(UserPtr, Alignment))) {
-      const uptr AlignedUserPtr = roundUpTo(UserPtr, Alignment);
-      const uptr Offset = AlignedUserPtr - UserPtr;
+    if (UNLIKELY(UnalignedUserPtr != UserPtr)) {
+      const uptr Offset = UserPtr - UnalignedUserPtr;
       DCHECK_GE(Offset, 2 * sizeof(u32));
       // The BlockMarker has no security purpose, but is specifically meant for
       // the chunk iteration function that can be used in debugging situations.
@@ -233,7 +235,6 @@ template <class Params> class Allocator {
       // based on its block address.
       reinterpret_cast<u32 *>(Block)[0] = BlockMarker;
       reinterpret_cast<u32 *>(Block)[1] = static_cast<u32>(Offset);
-      UserPtr = AlignedUserPtr;
       Header.Offset = (Offset >> MinAlignmentLog) & Chunk::OffsetMask;
     }
     Header.ClassId = ClassId & Chunk::ClassIdMask;


        


More information about the llvm-commits mailing list