[compiler-rt] r217908 - tsan: support longjmp out of signal handlers

Dmitry Vyukov dvyukov at google.com
Tue Sep 16 14:48:22 PDT 2014


Author: dvyukov
Date: Tue Sep 16 16:48:22 2014
New Revision: 217908

URL: http://llvm.org/viewvc/llvm-project?rev=217908&view=rev
Log:
tsan: support longjmp out of signal handlers
Fixes https://code.google.com/p/thread-sanitizer/issues/detail?id=75


Added:
    compiler-rt/trunk/test/tsan/signal_longjmp.cc
Modified:
    compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h

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=217908&r1=217907&r2=217908&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc Tue Sep 16 16:48:22 2014
@@ -390,6 +390,11 @@ static void SetJmp(ThreadState *thr, upt
   buf->sp = sp;
   buf->mangled_sp = mangled_sp;
   buf->shadow_stack_pos = thr->shadow_stack_pos;
+  SignalContext *sctx = SigCtx(thr);
+  buf->int_signal_send = sctx ? sctx->int_signal_send : 0;
+  buf->in_blocking_func = sctx ? sctx->in_blocking_func : false;
+  buf->in_signal_handler = atomic_load(&thr->in_signal_handler,
+      memory_order_relaxed);
 }
 
 static void LongJmp(ThreadState *thr, uptr *env) {
@@ -402,6 +407,13 @@ static void LongJmp(ThreadState *thr, up
       // Unwind the stack.
       while (thr->shadow_stack_pos > buf->shadow_stack_pos)
         FuncExit(thr);
+      SignalContext *sctx = SigCtx(thr);
+      if (sctx) {
+        sctx->int_signal_send = buf->int_signal_send;
+        sctx->in_blocking_func = buf->in_blocking_func;
+      }
+      atomic_store(&thr->in_signal_handler, buf->in_signal_handler,
+          memory_order_relaxed);
       JmpBufGarbageCollect(thr, buf->sp - 1);  // do not collect buf->sp
       return;
     }

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h?rev=217908&r1=217907&r2=217908&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h Tue Sep 16 16:48:22 2014
@@ -308,6 +308,9 @@ struct SignalContext;
 struct JmpBuf {
   uptr sp;
   uptr mangled_sp;
+  int int_signal_send;
+  bool in_blocking_func;
+  uptr in_signal_handler;
   uptr *shadow_stack_pos;
 };
 

Added: compiler-rt/trunk/test/tsan/signal_longjmp.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/signal_longjmp.cc?rev=217908&view=auto
==============================================================================
--- compiler-rt/trunk/test/tsan/signal_longjmp.cc (added)
+++ compiler-rt/trunk/test/tsan/signal_longjmp.cc Tue Sep 16 16:48:22 2014
@@ -0,0 +1,63 @@
+// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
+
+// Test case for longjumping out of signal handler:
+// https://code.google.com/p/thread-sanitizer/issues/detail?id=71
+
+#include <setjmp.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+sigjmp_buf fault_jmp;
+volatile int fault_expected;
+
+void sigfault_handler(int sig) {
+  if (!fault_expected)
+    abort();
+
+  /* just return from sighandler to proper place */
+  fault_expected = 0;
+  siglongjmp(fault_jmp, 1);
+}
+
+#define MUST_FAULT(code) do { \
+  fault_expected = 1; \
+  if (!sigsetjmp(fault_jmp, 1)) { \
+    code; /* should pagefault -> sihandler does longjmp */ \
+    fprintf(stderr, "%s not faulted\n", #code); \
+    abort(); \
+  } else { \
+    fprintf(stderr, "%s faulted ok\n", #code); \
+  } \
+} while (0)
+
+int main() {
+  struct sigaction act;
+  act.sa_handler  = sigfault_handler;
+  act.sa_flags    = 0;
+  if (sigemptyset(&act.sa_mask)) {
+    perror("sigemptyset");
+    exit(1);
+  }
+
+  if (sigaction(SIGSEGV, &act, NULL)) {
+    perror("sigaction");
+    exit(1);
+  }
+
+  MUST_FAULT(((volatile int *volatile)0)[0] = 0);
+  MUST_FAULT(((volatile int *volatile)0)[1] = 1);
+  MUST_FAULT(((volatile int *volatile)0)[3] = 1);
+
+  // Ensure that tsan does not think that we are
+  // in a signal handler.
+  void *volatile p = malloc(10);
+  ((volatile int*)p)[1] = 1;
+  free((void*)p);
+
+  fprintf(stderr, "DONE\n");
+  return 0;
+}
+
+// CHECK-NOT: WARNING: ThreadSanitizer
+// CHECK: DONE





More information about the llvm-commits mailing list