[compiler-rt] r318078 - [tsan] Deadly signal handler for tsan

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 13 11:56:20 PST 2017


Author: vitalybuka
Date: Mon Nov 13 11:56:20 2017
New Revision: 318078

URL: http://llvm.org/viewvc/llvm-project?rev=318078&view=rev
Log:
[tsan] Deadly signal handler for tsan

Summary: https://github.com/google/sanitizers/issues/637

Reviewers: eugenis

Subscribers: kubamracek, llvm-commits

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

Modified:
    compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc
    compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/assert.cc
    compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/ill.cc
    compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/dedup_token_length_test.cc
    compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/dump_instruction_bytes.cc
    compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fpe.cc
    compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/sanitizer_set_report_fd_test.cc

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc?rev=318078&r1=318077&r2=318078&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc Mon Nov 13 11:56:20 2017
@@ -1943,21 +1943,6 @@ static void rtl_sigaction(int sig, __san
   rtl_generic_sighandler(true, sig, info, ctx);
 }
 
-static int sigaction_impl(int sig, __sanitizer_sigaction *act,
-                          __sanitizer_sigaction *old);
-static __sanitizer_sighandler_ptr signal_impl(int sig,
-                                              __sanitizer_sighandler_ptr h);
-
-TSAN_INTERCEPTOR(int, sigaction, int sig, __sanitizer_sigaction *act,
-                 __sanitizer_sigaction *old) {
-  return sigaction_impl(sig, act, old);
-}
-
-TSAN_INTERCEPTOR(__sanitizer_sighandler_ptr, signal, int sig,
-                 __sanitizer_sighandler_ptr h) {
-  return signal_impl(sig, h);
-}
-
 TSAN_INTERCEPTOR(int, raise, int sig) {
   SCOPED_TSAN_INTERCEPTOR(raise, sig);
   ThreadSignalContext *sctx = SigCtx(thr);
@@ -2271,9 +2256,20 @@ static void HandleRecvmsg(ThreadState *t
 
 #include "sanitizer_common/sanitizer_common_interceptors.inc"
 
-// TODO(vitalybuka): use sanitizer_signal_interceptors.inc here.
+static int sigaction_impl(int sig, const __sanitizer_sigaction *act,
+                          __sanitizer_sigaction *old);
+static __sanitizer_sighandler_ptr signal_impl(int sig,
+                                              __sanitizer_sighandler_ptr h);
+
+#define SIGNAL_INTERCEPTOR_SIGACTION_IMPL(signo, act, oldact) \
+  { return sigaction_impl(signo, act, oldact); }
+
+#define SIGNAL_INTERCEPTOR_SIGNAL_IMPL(func, signo, handler) \
+  { return (uptr)signal_impl(signo, (__sanitizer_sighandler_ptr)handler); }
+
+#include "sanitizer_common/sanitizer_signal_interceptors.inc"
 
-int sigaction_impl(int sig, __sanitizer_sigaction *act,
+int sigaction_impl(int sig, const __sanitizer_sigaction *act,
                    __sanitizer_sigaction *old) {
   // Note: if we call REAL(sigaction) directly for any reason without proxying
   // the signal handler through rtl_sigaction, very bad things will happen.
@@ -2289,8 +2285,8 @@ int sigaction_impl(int sig, __sanitizer_
   // some bytes from old value and some bytes from new value.
   // Use volatile to prevent insertion of memcpy.
   sigactions[sig].handler =
-      *(volatile __sanitizer_sighandler_ptr *)&act->handler;
-  sigactions[sig].sa_flags = *(volatile int *)&act->sa_flags;
+      *(volatile __sanitizer_sighandler_ptr const *)&act->handler;
+  sigactions[sig].sa_flags = *(volatile int const *)&act->sa_flags;
   internal_memcpy(&sigactions[sig].sa_mask, &act->sa_mask,
                   sizeof(sigactions[sig].sa_mask));
 #if !SANITIZER_FREEBSD && !SANITIZER_MAC && !SANITIZER_NETBSD
@@ -2506,6 +2502,7 @@ void InitializeInterceptors() {
   new(interceptor_ctx()) InterceptorContext();
 
   InitializeCommonInterceptors();
+  InitializeSignalInterceptors();
 
 #if !SANITIZER_MAC
   // We can not use TSAN_INTERCEPT to get setjmp addr,
@@ -2613,8 +2610,6 @@ void InitializeInterceptors() {
   TSAN_INTERCEPT(rmdir);
   TSAN_INTERCEPT(closedir);
 
-  TSAN_INTERCEPT(sigaction);
-  TSAN_INTERCEPT(signal);
   TSAN_INTERCEPT(sigsuspend);
   TSAN_INTERCEPT(sigblock);
   TSAN_INTERCEPT(sigsetmask);

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc?rev=318078&r1=318077&r2=318078&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc Mon Nov 13 11:56:20 2017
@@ -324,6 +324,21 @@ static void CheckShadowMapping() {
   }
 }
 
+#if !SANITIZER_GO
+static void OnStackUnwind(const SignalContext &sig, const void *,
+                          BufferedStackTrace *stack) {
+  uptr top = 0;
+  uptr bottom = 0;
+  bool fast = common_flags()->fast_unwind_on_fatal;
+  if (fast) GetThreadStackTopAndBottom(false, &top, &bottom);
+  stack->Unwind(kStackTraceMax, sig.pc, sig.bp, sig.context, top, bottom, fast);
+}
+
+static void TsanOnDeadlySignal(int signo, void *siginfo, void *context) {
+  HandleDeadlySignal(siginfo, context, GetTid(), &OnStackUnwind, nullptr);
+}
+#endif
+
 void Initialize(ThreadState *thr) {
   // Thread safe because done before all threads exist.
   static bool is_initialized = false;
@@ -361,6 +376,7 @@ void Initialize(ThreadState *thr) {
 #if !SANITIZER_GO
   InitializeShadowMemory();
   InitializeAllocatorLate();
+  InstallDeadlySignalHandlers(TsanOnDeadlySignal);
 #endif
   // Setup correct file descriptor for error reports.
   __sanitizer_set_report_path(common_flags()->log_path);

Modified: compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/assert.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/assert.cc?rev=318078&r1=318077&r2=318078&view=diff
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/assert.cc (original)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/assert.cc Mon Nov 13 11:56:20 2017
@@ -7,9 +7,6 @@
 // RUN: %env_tool_opts=handle_abort=1 not         %run %t 2>&1 | FileCheck --check-prefix=CHECK1 %s
 // clang-format on
 
-// FIXME: implement in other sanitizers.
-// XFAIL: tsan
-
 #include <assert.h>
 #include <stdio.h>
 #include <sanitizer/asan_interface.h>

Modified: compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/ill.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/ill.cc?rev=318078&r1=318077&r2=318078&view=diff
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/ill.cc (original)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/ill.cc Mon Nov 13 11:56:20 2017
@@ -7,9 +7,6 @@
 // RUN: %env_tool_opts=handle_sigill=1 not         %run %t 2>&1 | FileCheck --check-prefix=CHECK1 %s
 // clang-format on
 
-// FIXME: implement in other sanitizers.
-// XFAIL: tsan
-
 // FIXME: seems to fail on ARM
 // REQUIRES: x86_64-target-arch
 #include <assert.h>

Modified: compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/dedup_token_length_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/dedup_token_length_test.cc?rev=318078&r1=318077&r2=318078&view=diff
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/dedup_token_length_test.cc (original)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/dedup_token_length_test.cc Mon Nov 13 11:56:20 2017
@@ -7,8 +7,6 @@
 // RUN: env %tool_options='abort_on_error=0, dedup_token_length=3' not %run %t 2>&1   | FileCheck %s --check-prefix=CHECK3 --match-full-lines
 
 // REQUIRES: stable-runtime
-// FIXME: implement SEGV handler in other sanitizers.
-// XFAIL: tsan
 
 volatile int *null = 0;
 

Modified: compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/dump_instruction_bytes.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/dump_instruction_bytes.cc?rev=318078&r1=318077&r2=318078&view=diff
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/dump_instruction_bytes.cc (original)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/dump_instruction_bytes.cc Mon Nov 13 11:56:20 2017
@@ -8,8 +8,6 @@
 // clang-format on
 
 // REQUIRES: x86-target-arch
-// FIXME: implement in other sanitizers.
-// XFAIL: tsan
 
 int main() {
 #if defined(__x86_64__)

Modified: compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fpe.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fpe.cc?rev=318078&r1=318077&r2=318078&view=diff
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fpe.cc (original)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fpe.cc Mon Nov 13 11:56:20 2017
@@ -4,9 +4,6 @@
 // RUN: %env_tool_opts=handle_sigfpe=0 not --crash %run %t 2>&1 | FileCheck --check-prefix=CHECK0 %s
 // RUN: %env_tool_opts=handle_sigfpe=1 not         %run %t 2>&1 | FileCheck --check-prefix=CHECK1 %s
 
-// FIXME: implement in other sanitizers, not just asan.
-// XFAIL: tsan
-
 // FIXME: seems to fail on ARM
 // REQUIRES: x86_64-target-arch
 #include <assert.h>

Modified: compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/sanitizer_set_report_fd_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/sanitizer_set_report_fd_test.cc?rev=318078&r1=318077&r2=318078&view=diff
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/sanitizer_set_report_fd_test.cc (original)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/sanitizer_set_report_fd_test.cc Mon Nov 13 11:56:20 2017
@@ -6,8 +6,6 @@
 
 // REQUIRES: stable-runtime
 // XFAIL: android && asan
-// FIXME: implement SEGV handler in other sanitizers, not just asan.
-// XFAIL: tsan
 
 #include <sanitizer/common_interface_defs.h>
 #include <stdio.h>




More information about the llvm-commits mailing list