[compiler-rt] [compiler-rt] SetAlternateSignalStack using MAP_STACK on linux based … (PR #100905)

David CARLIER via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 27 16:04:16 PDT 2024


https://github.com/devnexen created https://github.com/llvm/llvm-project/pull/100905

…systems.

main interest here is to have builtin guard pages.

>From f4d26f0c26c722a448ae42541ff8aff960a60e98 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Sun, 28 Jul 2024 00:00:53 +0100
Subject: [PATCH] [compiler-rt] SetAlternateSignalStack using MAP_STACK on
 linux based systems.

main interest here is to have builtin guard pages.
---
 .../sanitizer_posix_libcdep.cpp               | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
index 9ffb36f812c45..a811edd23e41f 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
@@ -83,6 +83,23 @@ bool DontDumpShadowMemory(uptr addr, uptr length) {
 #endif  // MADV_DONTDUMP
 }
 
+static void *MmapStackOrDie(uptr size, const char *mem_type) {
+#if SANITIZER_LINUX
+  // MAP_STACK on freebsd has a different meaning, on guard access the mapped region grows.
+  // unimplemented on netbsd still, could be enabled on openbsd if sanitizer is ever ported there.
+  size = RoundUpTo(size, GetPageSizeCached());
+  uptr res = MmapNamed(nullptr, size, PROT_READ | PROT_WRITE,
+                       MAP_PRIVATE | MAP_STACK, mem_type);
+  int reserrno;
+  if (UNLIKELY(internal_iserror(res, &reserrno)))
+    ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno, true);
+  IncreaseTotalMmap(size);
+  return (void *)res;
+#else
+  return MmapOrDie(size, mem_type, true);
+#endif
+}
+
 static rlim_t getlim(int res) {
   rlimit rlim;
   CHECK_EQ(0, getrlimit(res, &rlim));
@@ -196,7 +213,7 @@ void SetAlternateSignalStack() {
   // future. It is not required by man 2 sigaltstack now (they're using
   // malloc()).
   altstack.ss_size = GetAltStackSize();
-  altstack.ss_sp = (char *)MmapOrDie(altstack.ss_size, __func__);
+  altstack.ss_sp = (char *)MmapStackOrDie(altstack.ss_size, __func__);
   altstack.ss_flags = 0;
   CHECK_EQ(0, sigaltstack(&altstack, nullptr));
 }



More information about the llvm-commits mailing list