[compiler-rt] [scudo] Avoid splitting aligned allocations on Trusty (PR #69281)

Andrei Homescu via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 21 17:09:22 PST 2023


https://github.com/ahomescu updated https://github.com/llvm/llvm-project/pull/69281

>From 7dba4074024acdc4843f51b751ea2438554fb6e9 Mon Sep 17 00:00:00 2001
From: Marco Nelissen <marcone at google.com>
Date: Thu, 13 Apr 2023 18:48:57 -0700
Subject: [PATCH] [scudo] Avoid splitting unaligned allocations on Trusty

Split allocations around the pointer returned by malloc
on Trusty. Avoid splitting completely if that pointer
is not page-aligned.
---
 compiler-rt/lib/scudo/standalone/secondary.h | 37 ++++++++++++++------
 1 file changed, 27 insertions(+), 10 deletions(-)

diff --git a/compiler-rt/lib/scudo/standalone/secondary.h b/compiler-rt/lib/scudo/standalone/secondary.h
index f52a4188bcf3a6..3c7e4d548ae139 100644
--- a/compiler-rt/lib/scudo/standalone/secondary.h
+++ b/compiler-rt/lib/scudo/standalone/secondary.h
@@ -122,18 +122,35 @@ bool mapSecondary(const Options &Options, uptr CommitBase, uptr CommitSize,
   Flags |= MAP_RESIZABLE;
   Flags |= MAP_ALLOWNOMEM;
 
-  const uptr MaxUnusedCacheBytes = MaxUnusedCachePages * getPageSizeCached();
+  const uptr PageSize = getPageSizeCached();
+  const uptr MaxUnusedCacheBytes = MaxUnusedCachePages * PageSize;
   if (useMemoryTagging<Config>(Options) && CommitSize > MaxUnusedCacheBytes) {
-    const uptr UntaggedPos = Max(AllocPos, CommitBase + MaxUnusedCacheBytes);
-    return MemMap.remap(CommitBase, UntaggedPos - CommitBase, "scudo:secondary",
-                        MAP_MEMTAG | Flags) &&
-           MemMap.remap(UntaggedPos, CommitBase + CommitSize - UntaggedPos,
-                        "scudo:secondary", Flags);
-  } else {
-    const uptr RemapFlags =
-        (useMemoryTagging<Config>(Options) ? MAP_MEMTAG : 0) | Flags;
-    return MemMap.remap(CommitBase, CommitSize, "scudo:secondary", RemapFlags);
+    if (SCUDO_TRUSTY) {
+      /*
+       * On Trusty we need AllocPos to be usable for memrefs, which cannot
+       * cross multiple mappings. This means we need to split around AllocPos
+       * and not over it. We can only do this if the address is page-aligned.
+       */
+      const uptr TaggedSize = AllocPos - CommitBase;
+      if (TaggedSize != 0 && isAligned(TaggedSize, PageSize)) {
+        return MemMap.remap(CommitBase, TaggedSize, "scudo:secondary",
+                            MAP_MEMTAG | Flags) &&
+               MemMap.remap(AllocPos, CommitSize - TaggedSize,
+                            "scudo:secondary", Flags);
+      }
+      /* We could not split, so fall through to the normal code path */
+    } else {
+      const uptr UntaggedPos = Max(AllocPos, CommitBase + MaxUnusedCacheBytes);
+      return MemMap.remap(CommitBase, UntaggedPos - CommitBase,
+                          "scudo:secondary", MAP_MEMTAG | Flags) &&
+             MemMap.remap(UntaggedPos, CommitBase + CommitSize - UntaggedPos,
+                          "scudo:secondary", Flags);
+    }
   }
+
+  const uptr RemapFlags =
+      (useMemoryTagging<Config>(Options) ? MAP_MEMTAG : 0) | Flags;
+  return MemMap.remap(CommitBase, CommitSize, "scudo:secondary", RemapFlags);
 }
 
 // Template specialization to avoid producing zero-length array



More information about the llvm-commits mailing list