[compiler-rt] 58f7543 - [sanitizer] Move signal blocking code into posix

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 26 17:45:24 PDT 2023


Author: Vitaly Buka
Date: 2023-09-26T17:45:12-07:00
New Revision: 58f7543361a475f0f8e309a2b5f38a3daa405549

URL: https://github.com/llvm/llvm-project/commit/58f7543361a475f0f8e309a2b5f38a3daa405549
DIFF: https://github.com/llvm/llvm-project/commit/58f7543361a475f0f8e309a2b5f38a3daa405549.diff

LOG: [sanitizer] Move signal blocking code into posix

This will affect only Darwin, as the rest alredy do that.

Reviewed By: rsundahl

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

Added: 
    

Modified: 
    compiler-rt/lib/asan/asan_interceptors.cpp
    compiler-rt/lib/asan/asan_posix.cpp
    compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
    compiler-rt/lib/sanitizer_common/sanitizer_linux.h
    compiler-rt/lib/sanitizer_common/sanitizer_posix.cpp
    compiler-rt/lib/sanitizer_common/sanitizer_posix.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/asan/asan_interceptors.cpp b/compiler-rt/lib/asan/asan_interceptors.cpp
index fc7c5b7b8fbfade..f25d147fef6e91f 100644
--- a/compiler-rt/lib/asan/asan_interceptors.cpp
+++ b/compiler-rt/lib/asan/asan_interceptors.cpp
@@ -224,12 +224,9 @@ static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
   auto args = asanThreadArgRetval().GetArgs(self);
   t->ThreadStart(GetTid());
 
-#    if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
-        SANITIZER_SOLARIS
   __sanitizer_sigset_t sigset;
   t->GetStartData(sigset);
   SetSigProcMask(&sigset, nullptr);
-#    endif
 
   thread_return_t retval = (*args.routine)(args.arg_retval);
   asanThreadArgRetval().Finish(self, retval);
@@ -252,10 +249,7 @@ INTERCEPTOR(int, pthread_create, void *thread, void *attr,
   u32 current_tid = GetCurrentTidOrInvalid();
 
   __sanitizer_sigset_t sigset;
-#    if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
-        SANITIZER_SOLARIS
   ScopedBlockSignals block(&sigset);
-#    endif
 
   AsanThread *t = AsanThread::Create(sigset, current_tid, &stack, detached);
 

diff  --git a/compiler-rt/lib/asan/asan_posix.cpp b/compiler-rt/lib/asan/asan_posix.cpp
index e1f66641617cc14..b1660a375eb36fd 100644
--- a/compiler-rt/lib/asan/asan_posix.cpp
+++ b/compiler-rt/lib/asan/asan_posix.cpp
@@ -110,6 +110,9 @@ void PlatformTSDDtor(void *tsd) {
   key.key = nullptr;
   // Make sure that signal handler can not see a stale current thread pointer.
   atomic_signal_fence(memory_order_seq_cst);
+  // After this point it's unsafe to execute signal handlers which may be
+  // instrumented.
+  BlockSignals();
   AsanThread::TSDDtor(tsd);
 }
 #else
@@ -138,12 +141,9 @@ void PlatformTSDDtor(void *tsd) {
     CHECK_EQ(0, pthread_setspecific(tsd_key, tsd));
     return;
   }
-#    if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
-        SANITIZER_SOLARIS
   // After this point it's unsafe to execute signal handlers which may be
-  // instrumented. It's probably not just a Linux issue.
+  // instrumented.
   BlockSignals();
-#    endif
   AsanThread::TSDDtor(tsd);
 }
 #endif

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
index d2b3b63f3a7a3bd..6647b69771ce028 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
@@ -156,36 +156,6 @@ const int FUTEX_WAKE_PRIVATE = FUTEX_WAKE | FUTEX_PRIVATE_FLAG;
 
 namespace __sanitizer {
 
-void SetSigProcMask(__sanitizer_sigset_t *set, __sanitizer_sigset_t *oldset) {
-  CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, set, oldset));
-}
-
-void BlockSignals(__sanitizer_sigset_t *oldset) {
-  __sanitizer_sigset_t set;
-  internal_sigfillset(&set);
-#  if SANITIZER_LINUX && !SANITIZER_ANDROID
-  // Glibc uses SIGSETXID signal during setuid call. If this signal is blocked
-  // on any thread, setuid call hangs.
-  // See test/sanitizer_common/TestCases/Linux/setuid.c.
-  internal_sigdelset(&set, 33);
-#  endif
-#  if SANITIZER_LINUX
-  // Seccomp-BPF-sandboxed processes rely on SIGSYS to handle trapped syscalls.
-  // If this signal is blocked, such calls cannot be handled and the process may
-  // hang.
-  internal_sigdelset(&set, 31);
-#  endif
-  SetSigProcMask(&set, oldset);
-}
-
-ScopedBlockSignals::ScopedBlockSignals(__sanitizer_sigset_t *copy) {
-  BlockSignals(&saved_);
-  if (copy)
-    internal_memcpy(copy, &saved_, sizeof(saved_));
-}
-
-ScopedBlockSignals::~ScopedBlockSignals() { SetSigProcMask(&saved_, nullptr); }
-
 #  if SANITIZER_LINUX && defined(__x86_64__)
 #    include "sanitizer_syscall_linux_x86_64.inc"
 #  elif SANITIZER_LINUX && SANITIZER_RISCV64

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.h b/compiler-rt/lib/sanitizer_common/sanitizer_linux.h
index 7454369fa4192a9..fdc0ff1b854ccc6 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.h
@@ -46,22 +46,7 @@ void ReadProcMaps(ProcSelfMapsBuff *proc_maps);
 
 // Syscall wrappers.
 uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count);
-uptr internal_sigaltstack(const void* ss, void* oss);
-uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
-    __sanitizer_sigset_t *oldset);
-
-void SetSigProcMask(__sanitizer_sigset_t *set, __sanitizer_sigset_t *oldset);
-void BlockSignals(__sanitizer_sigset_t *oldset = nullptr);
-struct ScopedBlockSignals {
-  explicit ScopedBlockSignals(__sanitizer_sigset_t *copy);
-  ~ScopedBlockSignals();
-
-  ScopedBlockSignals &operator=(const ScopedBlockSignals &) = delete;
-  ScopedBlockSignals(const ScopedBlockSignals &) = delete;
-
- private:
-  __sanitizer_sigset_t saved_;
-};
+uptr internal_sigaltstack(const void *ss, void *oss);
 
 #  if SANITIZER_GLIBC
 uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp);

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_posix.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_posix.cpp
index 8d2c5b2cefbea23..3c059ebcce3a3af 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_posix.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_posix.cpp
@@ -406,6 +406,35 @@ uptr MmapNamed(void *addr, uptr length, int prot, int flags, const char *name) {
   return res;
 }
 
+void SetSigProcMask(__sanitizer_sigset_t *set, __sanitizer_sigset_t *oldset) {
+  CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, set, oldset));
+}
+
+void BlockSignals(__sanitizer_sigset_t *oldset) {
+  __sanitizer_sigset_t set;
+  internal_sigfillset(&set);
+#  if SANITIZER_LINUX && !SANITIZER_ANDROID
+  // Glibc uses SIGSETXID signal during setuid call. If this signal is blocked
+  // on any thread, setuid call hangs.
+  // See test/sanitizer_common/TestCases/Linux/setuid.c.
+  internal_sigdelset(&set, 33);
+#  endif
+#  if SANITIZER_LINUX
+  // Seccomp-BPF-sandboxed processes rely on SIGSYS to handle trapped syscalls.
+  // If this signal is blocked, such calls cannot be handled and the process may
+  // hang.
+  internal_sigdelset(&set, 31);
+#  endif
+  SetSigProcMask(&set, oldset);
+}
+
+ScopedBlockSignals::ScopedBlockSignals(__sanitizer_sigset_t *copy) {
+  BlockSignals(&saved_);
+  if (copy)
+    internal_memcpy(copy, &saved_, sizeof(saved_));
+}
+
+ScopedBlockSignals::~ScopedBlockSignals() { SetSigProcMask(&saved_, nullptr); }
 
 } // namespace __sanitizer
 

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_posix.h b/compiler-rt/lib/sanitizer_common/sanitizer_posix.h
index c5811dffea94b5f..64dff534c67416b 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_posix.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_posix.h
@@ -98,6 +98,8 @@ int internal_sigaction(int signum, const void *act, void *oldact);
 void internal_sigfillset(__sanitizer_sigset_t *set);
 void internal_sigemptyset(__sanitizer_sigset_t *set);
 bool internal_sigismember(__sanitizer_sigset_t *set, int signum);
+uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
+                          __sanitizer_sigset_t *oldset);
 
 uptr internal_execve(const char *filename, char *const argv[],
                      char *const envp[]);
@@ -124,6 +126,19 @@ void DecorateMapping(uptr addr, uptr size, const char *name);
 #    define __sanitizer_dirsiz(dp) ((dp)->d_reclen)
 #  endif
 
+void SetSigProcMask(__sanitizer_sigset_t *set, __sanitizer_sigset_t *oldset);
+void BlockSignals(__sanitizer_sigset_t *oldset = nullptr);
+struct ScopedBlockSignals {
+  explicit ScopedBlockSignals(__sanitizer_sigset_t *copy);
+  ~ScopedBlockSignals();
+
+  ScopedBlockSignals &operator=(const ScopedBlockSignals &) = delete;
+  ScopedBlockSignals(const ScopedBlockSignals &) = delete;
+
+ private:
+  __sanitizer_sigset_t saved_;
+};
+
 }  // namespace __sanitizer
 
 #endif  // SANITIZER_POSIX


        


More information about the llvm-commits mailing list