[PATCH] D65851: [Sanitizer] Using huge page on FreeBSD for shadow mapping

David CARLIER via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 7 01:46:31 PDT 2019


devnexen created this revision.
devnexen added reviewers: emaste, dim, vitalybuka.
devnexen created this object with visibility "All Users".
Herald added subscribers: llvm-commits, Sanitizers, krytarowski, kubamracek.
Herald added projects: LLVM, Sanitizers.

- Unless explicit configuration, using FreeBSD super pages feature for shadow mapping.
- as an only for now.


Repository:
  rCRT Compiler Runtime

https://reviews.llvm.org/D65851

Files:
  compiler-rt/lib/asan/asan_shadow_setup.cpp
  compiler-rt/lib/sanitizer_common/sanitizer_common.h
  compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
  compiler-rt/lib/sanitizer_common/sanitizer_win.cpp


Index: compiler-rt/lib/sanitizer_common/sanitizer_win.cpp
===================================================================
--- compiler-rt/lib/sanitizer_common/sanitizer_win.cpp
+++ compiler-rt/lib/sanitizer_common/sanitizer_win.cpp
@@ -239,6 +239,11 @@
   return true;
 }
 
+bool MmapFixedSuperPage(uptr fixed_addr, uptr size, const char *name) {
+  // FIXME: Windows support large pages too. Might be worth checking
+  return MmapFixedNoReserve(fixed_addr, size, name);
+}
+
 // Memory space mapped by 'MmapFixedOrDie' must have been reserved by
 // 'MmapFixedNoAccess'.
 void *MmapFixedOrDie(uptr fixed_addr, uptr size, const char *name) {
Index: compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
===================================================================
--- compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
+++ compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
@@ -320,6 +320,29 @@
   return true;
 }
 
+bool MmapFixedSuperPage(uptr fixed_addr, uptr size, const char *name) {
+#if SANITIZER_FREEBSD
+  if (common_flags()->no_huge_pages_for_shadow)
+    return MmapFixedNoReserve(fixed_addr, size, name);
+  size = RoundUpTo(size, GetPageSizeCached());
+  fixed_addr = RoundDownTo(fixed_addr, GetPageSizeCached());
+  // MAP_NORESERVE is implicit with FreeBSD
+  uptr p = MmapNamed((void *)fixed_addr, size, PROT_READ | PROT_WRITE,
+                     MAP_PRIVATE | MAP_FIXED | MAP_ALIGNED_SUPER | MAP_ANON, name);
+  int reserrno;
+  if (internal_iserror(p, &reserrno)) {
+    Report("ERROR: %s failed to "
+           "allocate 0x%zx (%zd) bytes at address %zx (errno: %d)\n",
+           SanitizerToolName, size, size, fixed_addr, reserrno);
+    return false;
+  }
+  IncreaseTotalMmap(size);
+  return true;
+#else
+  return MmapFixedNoReserve(fixed_addr, size, name);
+#endif
+}
+
 uptr ReservedAddressRange::Init(uptr size, const char *name, uptr fixed_addr) {
   base_ = fixed_addr ? MmapFixedNoAccess(fixed_addr, size, name)
                      : MmapNoAccess(size);
Index: compiler-rt/lib/sanitizer_common/sanitizer_common.h
===================================================================
--- compiler-rt/lib/sanitizer_common/sanitizer_common.h
+++ compiler-rt/lib/sanitizer_common/sanitizer_common.h
@@ -100,6 +100,7 @@
 void *MmapOrDieOnFatalError(uptr size, const char *mem_type);
 bool MmapFixedNoReserve(uptr fixed_addr, uptr size, const char *name = nullptr)
      WARN_UNUSED_RESULT;
+bool MmapFixedSuperPage(uptr fixed_addr, uptr size, const char *name = nullptr);
 void *MmapNoReserveOrDie(uptr size, const char *mem_type);
 void *MmapFixedOrDie(uptr fixed_addr, uptr size, const char *name = nullptr);
 // Behaves just like MmapFixedOrDie, but tolerates out of memory condition, in
Index: compiler-rt/lib/asan/asan_shadow_setup.cpp
===================================================================
--- compiler-rt/lib/asan/asan_shadow_setup.cpp
+++ compiler-rt/lib/asan/asan_shadow_setup.cpp
@@ -30,6 +30,8 @@
   CHECK_EQ(((end + 1) % GetMmapGranularity()), 0);
   uptr size = end - beg + 1;
   DecreaseTotalMmap(size);  // Don't count the shadow against mmap_limit_mb.
+#if SANITIZER_LINUX
+  // Only Linux supports THP feature
   if (!MmapFixedNoReserve(beg, size, name)) {
     Report(
         "ReserveShadowMemoryRange failed while trying to map 0x%zx bytes. "
@@ -38,6 +40,15 @@
     Abort();
   }
   SetShadowRegionHugePageMode(beg, size);
+#else
+  if (!MmapFixedSuperPage(beg, size, name)) {
+    Report(
+        "ReserveShadowMemoryRange failed while trying to map 0x%zx bytes. "
+        "Perhaps you're using ulimit -v\n",
+        size);
+    Abort();
+  }
+#endif
   if (common_flags()->use_madv_dontdump) DontDumpShadowMemory(beg, size);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65851.213815.patch
Type: text/x-patch
Size: 3739 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190807/ee150cff/attachment.bin>


More information about the llvm-commits mailing list