From 89ae290b58e20fc5f56b7bfae4b34e7fef06e1b1 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Tue, 26 Feb 2019 13:38:23 +0000 Subject: [PATCH] [Sanitizer] Add interceptor for pthread_sigmask Summary: pthread_sigmask is just like sigprocmask, except that its behavior in multithreaded programs is explicitly specified. Sanitizers were lacking a common interceptor for pthread_sigmask (although some specific sanitizers defined custom version), which lead to false positives (at least in msan) when using this function. The interceptor implementation, and its test are based on the equivalent code for sigprocmask. Reviewers: eugenis, vitalybuka Subscribers: kubamracek, delcypher, jfb, jdoerfert, llvm-commits, #sanitizers Tags: #llvm, #sanitizers Differential Revision: https://reviews.llvm.org/D58382 llvm-svn: 354874 --- compiler-rt/lib/esan/esan_interceptors.cpp | 1 + compiler-rt/lib/msan/tests/msan_test.cc | 8 ++++++++ .../sanitizer_common_interceptors.inc | 20 ++++++++++++++++++++ .../sanitizer_platform_interceptors.h | 1 + compiler-rt/lib/tsan/rtl/tsan_interceptors.cc | 1 + 5 files changed, 31 insertions(+) diff --git a/compiler-rt/lib/esan/esan_interceptors.cpp b/compiler-rt/lib/esan/esan_interceptors.cpp index 2f454f0..1e8e67c 100644 --- a/compiler-rt/lib/esan/esan_interceptors.cpp +++ b/compiler-rt/lib/esan/esan_interceptors.cpp @@ -45,6 +45,7 @@ DECLARE_REAL_AND_INTERCEPTOR(void *, malloc, uptr) // We provide our own version: #undef SANITIZER_INTERCEPT_SIGPROCMASK +#undef SANITIZER_INTERCEPT_PTHREAD_SIGMASK #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!EsanIsInitialized) diff --git a/compiler-rt/lib/msan/tests/msan_test.cc b/compiler-rt/lib/msan/tests/msan_test.cc index 4bac6f9..a3a9830 100644 --- a/compiler-rt/lib/msan/tests/msan_test.cc +++ b/compiler-rt/lib/msan/tests/msan_test.cc @@ -2579,6 +2579,14 @@ TEST(MemorySanitizer, sigprocmask) { EXPECT_NOT_POISONED(s); } +TEST(MemorySanitizer, pthread_sigmask) { + sigset_t s; + EXPECT_POISONED(s); + int res = pthread_sigmask(SIG_BLOCK, 0, &s); + ASSERT_EQ(0, res); + EXPECT_NOT_POISONED(s); +} + struct StructWithDtor { ~StructWithDtor(); }; diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index 6e0538d..5e0be8b 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -4036,6 +4036,25 @@ INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set, #define INIT_SIGPROCMASK #endif +#if SANITIZER_INTERCEPT_PTHREAD_SIGMASK +INTERCEPTOR(int, pthread_sigmask, int how, __sanitizer_sigset_t *set, + __sanitizer_sigset_t *oldset) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, pthread_sigmask, how, set, oldset); + if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set)); + // FIXME: under ASan the call below may write to freed memory and corrupt + // its metadata. See + // https://github.com/google/sanitizers/issues/321. + int res = REAL(pthread_sigmask)(how, set, oldset); + if (!res && oldset) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset)); + return res; +} +#define INIT_PTHREAD_SIGMASK COMMON_INTERCEPT_FUNCTION(pthread_sigmask); +#else +#define INIT_PTHREAD_SIGMASK +#endif + #if SANITIZER_INTERCEPT_BACKTRACE INTERCEPTOR(int, backtrace, void **buffer, int size) { void *ctx; @@ -9595,6 +9614,7 @@ static void InitializeCommonInterceptors() { INIT_SIGSETOPS; INIT_SIGPENDING; INIT_SIGPROCMASK; + INIT_PTHREAD_SIGMASK; INIT_BACKTRACE; INIT__EXIT; INIT_PTHREAD_MUTEX_LOCK; diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h index b49eb55..9c71714 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -311,6 +311,7 @@ (SI_FREEBSD || SI_NETBSD || SI_MAC || SI_LINUX_NOT_ANDROID || SI_SOLARIS) #define SANITIZER_INTERCEPT_SIGPENDING SI_POSIX #define SANITIZER_INTERCEPT_SIGPROCMASK SI_POSIX +#define SANITIZER_INTERCEPT_PTHREAD_SIGMASK SI_POSIX #define SANITIZER_INTERCEPT_BACKTRACE \ (SI_FREEBSD || SI_NETBSD || SI_OPENBSD || SI_LINUX_NOT_ANDROID || SI_SOLARIS) #define SANITIZER_INTERCEPT_GETMNTENT SI_LINUX diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc b/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc index e4b8def..9ea7d89 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc +++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc @@ -2221,6 +2221,7 @@ static void HandleRecvmsg(ThreadState *thr, uptr pc, #define NEED_TLS_GET_ADDR #endif #undef SANITIZER_INTERCEPT_TLS_GET_ADDR +#undef SANITIZER_INTERCEPT_PTHREAD_SIGMASK #define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name) #define COMMON_INTERCEPT_FUNCTION_VER(name, ver) \ -- 2.7.4