[compiler-rt] 94407e1 - [scudo] Change configuration for Trusty, use mmap()

Chia-hung Duan via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 5 11:28:47 PDT 2023


Author: Dmitriy Filchenko
Date: 2023-06-05T18:28:10Z
New Revision: 94407e1bba9807193afde61c56b6125c0fc0b1d1

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

LOG: [scudo] Change configuration for Trusty, use mmap()

Trusty runs in memory constrained environments, with many apps
having only one page (4KB) of heap memory available. However, we
still want to mmap() multiples of PAGE_SIZE at a time.

Additionally, switch Scudo from using sbrk() to mmap().

Reviewed By: cferris

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

Added: 
    

Modified: 
    compiler-rt/lib/scudo/standalone/allocator_config.h
    compiler-rt/lib/scudo/standalone/size_class_map.h
    compiler-rt/lib/scudo/standalone/trusty.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/scudo/standalone/allocator_config.h b/compiler-rt/lib/scudo/standalone/allocator_config.h
index df972d1ea5d22..1150157e25f63 100644
--- a/compiler-rt/lib/scudo/standalone/allocator_config.h
+++ b/compiler-rt/lib/scudo/standalone/allocator_config.h
@@ -276,15 +276,12 @@ struct TrustyConfig {
   using TSDRegistryT = TSDRegistrySharedT<A, 1U, 1U>; // Shared, max 1 TSD.
 
   struct Primary {
-    static const bool MaySupportMemoryTagging = false;
     using SizeClassMap = TrustySizeClassMap;
-    // Some apps have 1 page of heap total so small regions are necessary.
-    static const uptr RegionSizeLog = 10U;
-    static const uptr GroupSizeLog = 10U;
+    static const uptr RegionSizeLog = 28U;
+    static const uptr GroupSizeLog = 20U;
     typedef u32 CompactPtrT;
     static const bool EnableRandomOffset = false;
-    // Trusty is extremely memory-constrained so minimally round up map calls.
-    static const uptr MapSizeIncrement = 1UL << 4;
+    static const uptr MapSizeIncrement = 1UL << 12;
     static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
     static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
     static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;

diff  --git a/compiler-rt/lib/scudo/standalone/size_class_map.h b/compiler-rt/lib/scudo/standalone/size_class_map.h
index 766562495ec7a..2a6e298f93666 100644
--- a/compiler-rt/lib/scudo/standalone/size_class_map.h
+++ b/compiler-rt/lib/scudo/standalone/size_class_map.h
@@ -311,13 +311,11 @@ struct SvelteSizeClassConfig {
 
 typedef FixedSizeClassMap<SvelteSizeClassConfig> SvelteSizeClassMap;
 
-// Trusty is configured to only have one region containing blocks of size
-// 2^7 bytes.
 struct TrustySizeClassConfig {
   static const uptr NumBits = 1;
-  static const uptr MinSizeLog = 7;
-  static const uptr MidSizeLog = 7;
-  static const uptr MaxSizeLog = 7;
+  static const uptr MinSizeLog = 5;
+  static const uptr MidSizeLog = 5;
+  static const uptr MaxSizeLog = 15;
   static const u16 MaxNumCachedHint = 12;
   static const uptr MaxBytesCachedLog = 10;
   static const uptr SizeDelta = 0;

diff  --git a/compiler-rt/lib/scudo/standalone/trusty.cpp b/compiler-rt/lib/scudo/standalone/trusty.cpp
index c08a4e6f43319..8d73eeb36767a 100644
--- a/compiler-rt/lib/scudo/standalone/trusty.cpp
+++ b/compiler-rt/lib/scudo/standalone/trusty.cpp
@@ -12,17 +12,17 @@
 
 #include "common.h"
 #include "mutex.h"
-#include "string_utils.h"
 #include "trusty.h"
 
 #include <errno.h>           // for errno
+#include <lk/err_ptr.h>      // for PTR_ERR and IS_ERR
 #include <stdio.h>           // for printf()
 #include <stdlib.h>          // for getenv()
 #include <sys/auxv.h>        // for getauxval()
 #include <time.h>            // for clock_gettime()
+#include <trusty_err.h>      // for lk_err_to_errno()
 #include <trusty_syscalls.h> // for _trusty_brk()
-
-#define SBRK_ALIGN 32
+#include <uapi/mm.h>         // for MMAP flags
 
 namespace scudo {
 
@@ -30,35 +30,35 @@ uptr getPageSize() { return getauxval(AT_PAGESZ); }
 
 void NORETURN die() { abort(); }
 
-void *map(UNUSED void *Addr, uptr Size, UNUSED const char *Name, uptr Flags,
+void *map(void *Addr, uptr Size, const char *Name, uptr Flags,
           UNUSED MapPlatformData *Data) {
-  // Calling _trusty_brk(0) returns the current program break.
-  uptr ProgramBreak = reinterpret_cast<uptr>(_trusty_brk(0));
-  uptr Start;
-  uptr End;
-
-  Start = roundUp(ProgramBreak, SBRK_ALIGN);
-  // Don't actually extend the heap if MAP_NOACCESS flag is set since this is
-  // the case where Scudo tries to reserve a memory region without mapping
-  // physical pages.
+  uint32_t MmapFlags =
+      MMAP_FLAG_ANONYMOUS | MMAP_FLAG_PROT_READ | MMAP_FLAG_PROT_WRITE;
+
+  // If the MAP_NOACCESS flag is set, Scudo tries to reserve
+  // a memory region without mapping physical pages. This corresponds
+  // to MMAP_FLAG_NO_PHYSICAL in Trusty.
   if (Flags & MAP_NOACCESS)
-    return reinterpret_cast<void *>(Start);
-
-  // Attempt to extend the heap by Size bytes using _trusty_brk.
-  End = roundUp(Start + Size, SBRK_ALIGN);
-  ProgramBreak =
-      reinterpret_cast<uptr>(_trusty_brk(reinterpret_cast<void *>(End)));
-  if (ProgramBreak < End) {
-    errno = ENOMEM;
+    MmapFlags |= MMAP_FLAG_NO_PHYSICAL;
+  if (Addr)
+    MmapFlags |= MMAP_FLAG_FIXED_NOREPLACE;
+
+  void *P = (void *)_trusty_mmap(Addr, Size, MmapFlags, 0);
+
+  if (IS_ERR(P)) {
+    errno = lk_err_to_errno(PTR_ERR(P));
     dieOnMapUnmapError(Size);
     return nullptr;
   }
-  return reinterpret_cast<void *>(Start); // Base of new reserved region.
+
+  return P;
 }
 
-// Unmap is a no-op since Trusty uses sbrk instead of memory mapping.
 void unmap(UNUSED void *Addr, UNUSED uptr Size, UNUSED uptr Flags,
-           UNUSED MapPlatformData *Data) {}
+           UNUSED MapPlatformData *Data) {
+  if (_trusty_munmap(Addr, Size) != 0)
+    dieOnMapUnmapError();
+}
 
 void setMemoryPermission(UNUSED uptr Addr, UNUSED uptr Size, UNUSED uptr Flags,
                          UNUSED MapPlatformData *Data) {}


        


More information about the llvm-commits mailing list