[PATCH] D71292: scudo: Tweak how we align UserPtr. NFCI.

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


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

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D71292

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
@@ -221,11 +221,13 @@
     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 @@
       // 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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71292.233156.patch
Type: text/x-patch
Size: 1458 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191210/e80e2cc8/attachment-0001.bin>


More information about the llvm-commits mailing list