[PATCH] D35016: [tsan] Use pthread_sigmask instead of sigprocmask to block signals in a thread on Darwin

Kuba (Brecka) Mracek via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 5 09:27:15 PDT 2017


kubamracek created this revision.
kubamracek added a project: Sanitizers.

On Darwin, sigprocmask changes the signal mask for the entire process.  This has some unwanted consequences, because e.g. internal_start_thread wants to disable signals only in the current thread (to make the new thread inherit the signal mask), which is currently broken on Darwin.  This patch switches to pthread_sigmask.


Repository:
  rL LLVM

https://reviews.llvm.org/D35016

Files:
  lib/sanitizer_common/sanitizer_mac.cc
  test/tsan/Darwin/signals-blocked.cc


Index: test/tsan/Darwin/signals-blocked.cc
===================================================================
--- test/tsan/Darwin/signals-blocked.cc
+++ test/tsan/Darwin/signals-blocked.cc
@@ -0,0 +1,75 @@
+// RUN: %clangxx_tsan %s -o %t && %run %t 2>&1 | FileCheck %s
+
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include <errno.h>
+
+volatile bool signal_delivered;
+
+static void handler(int sig) {
+  if (sig == SIGALRM)
+    signal_delivered = true;
+}
+
+static void* thr(void *p) {
+  sigset_t sigset;
+  sigemptyset(&sigset);
+  sigaddset(&sigset, SIGALRM);
+  int ret = pthread_sigmask(SIG_UNBLOCK, &sigset, NULL);
+  if (ret) abort();
+
+  struct sigaction act = {};
+  act.sa_handler = &handler;
+  if (sigaction(SIGALRM, &act, 0)) {
+    perror("sigaction");
+    exit(1);
+  }
+
+  itimerval t;
+  t.it_value.tv_sec = 0;
+  t.it_value.tv_usec = 10000;
+  t.it_interval = t.it_value;
+  if (setitimer(ITIMER_REAL, &t, 0)) {
+    perror("setitimer");
+    exit(1);
+  }
+
+  while (!signal_delivered) {
+    usleep(1000);
+  }
+
+  t.it_value.tv_usec = 0;
+  if (setitimer(ITIMER_REAL, &t, 0)) {
+    perror("setitimer");
+    exit(1);
+  }
+
+  fprintf(stderr, "SIGNAL DELIVERED\n");
+
+  return 0;
+}
+
+int main() {
+  sigset_t sigset;
+  sigemptyset(&sigset);
+  sigaddset(&sigset, SIGALRM);
+  int ret = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
+  if (ret) abort();
+
+  pthread_t th;
+  pthread_create(&th, 0, thr, 0);
+  pthread_join(th, 0);
+
+  fprintf(stderr, "DONE\n");
+  return 0;
+}
+
+// CHECK-NOT: WARNING: ThreadSanitizer:
+// CHECK: SIGNAL DELIVERED
+// CHECK: DONE
+// CHECK-NOT: WARNING: ThreadSanitizer:
Index: lib/sanitizer_common/sanitizer_mac.cc
===================================================================
--- lib/sanitizer_common/sanitizer_mac.cc
+++ lib/sanitizer_common/sanitizer_mac.cc
@@ -508,10 +508,10 @@
   // Start the thread with signals blocked, otherwise it can steal user signals.
   __sanitizer_sigset_t set, old;
   internal_sigfillset(&set);
-  internal_sigprocmask(SIG_SETMASK, &set, &old);
+  pthread_sigmask(SIG_SETMASK, &set, &old);
   pthread_t th;
   pthread_create(&th, 0, (void*(*)(void *arg))func, arg);
-  internal_sigprocmask(SIG_SETMASK, &old, 0);
+  pthread_sigmask(SIG_SETMASK, &old, 0);
   return th;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D35016.105281.patch
Type: text/x-patch
Size: 2394 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170705/07f8f290/attachment.bin>


More information about the llvm-commits mailing list