tsan: enable clone interceptor only on Linux
authorDmitry Vyukov <dvyukov@google.com>
Thu, 11 Nov 2021 18:17:17 +0000 (19:17 +0100)
committerDmitry Vyukov <dvyukov@google.com>
Thu, 11 Nov 2021 18:27:47 +0000 (19:27 +0100)
Clone does not exist on Mac.
There are chances it will break on other OSes.
Enable it incrementally starting with Linux only,
other OSes can enable it later as needed.

Reviewed By: melver, thakis

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

compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
compiler-rt/test/tsan/Linux/clone_deadlock.cpp [new file with mode: 0644]
compiler-rt/test/tsan/clone_deadlock.cpp [deleted file]

index a712a3ad5bd9cbdaedb66c9adeb59031868d9b98..c7dcd07b85296061d60077b535f77f06b6ab8496 100644 (file)
@@ -2210,6 +2210,7 @@ TSAN_INTERCEPTOR(int, vfork, int fake) {
   return WRAP(fork)(fake);
 }
 
+#if SANITIZER_LINUX
 TSAN_INTERCEPTOR(int, clone, int (*fn)(void *), void *stack, int flags,
                  void *arg, int *parent_tid, void *tls, pid_t *child_tid) {
   SCOPED_INTERCEPTOR_RAW(clone, fn, stack, flags, arg, parent_tid, tls,
@@ -2233,6 +2234,7 @@ TSAN_INTERCEPTOR(int, clone, int (*fn)(void *), void *stack, int flags,
   ForkParentAfter(thr, pc);
   return pid;
 }
+#endif
 
 #if !SANITIZER_MAC && !SANITIZER_ANDROID
 typedef int (*dl_iterate_phdr_cb_t)(__sanitizer_dl_phdr_info *info, SIZE_T size,
@@ -2865,7 +2867,9 @@ void InitializeInterceptors() {
 
   TSAN_INTERCEPT(fork);
   TSAN_INTERCEPT(vfork);
+#if SANITIZER_LINUX
   TSAN_INTERCEPT(clone);
+#endif
 #if !SANITIZER_ANDROID
   TSAN_INTERCEPT(dl_iterate_phdr);
 #endif
diff --git a/compiler-rt/test/tsan/Linux/clone_deadlock.cpp b/compiler-rt/test/tsan/Linux/clone_deadlock.cpp
new file mode 100644 (file)
index 0000000..70846da
--- /dev/null
@@ -0,0 +1,40 @@
+// RUN: %clangxx_tsan -O1 %s -o %t && %env_tsan_opts=atexit_sleep_ms=0 %run %t 2>&1 | FileCheck %s
+#include "../test.h"
+#include <errno.h>
+#include <sched.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+long counter;
+
+static void *incrementer(void *arg) {
+  for (;;)
+    __sync_fetch_and_add(&counter, 1);
+  return 0;
+}
+
+static int cloned(void *arg) {
+  for (int i = 0; i < 1000; i++)
+    __sync_fetch_and_add(&counter, 1);
+  exit(0);
+  return 0;
+}
+
+int main() {
+  barrier_init(&barrier, 2);
+  pthread_t th;
+  pthread_create(&th, 0, incrementer, 0);
+  for (int i = 0; i < 100; i++) {
+    char stack[64 << 10] __attribute__((aligned(64)));
+    int pid = clone(cloned, stack + sizeof(stack), SIGCHLD, 0);
+    if (pid == -1) {
+      fprintf(stderr, "failed to clone: %d\n", errno);
+      exit(1);
+    }
+    while (wait(0) != pid) {
+    }
+  }
+  fprintf(stderr, "DONE\n");
+}
+
+// CHECK: DONE
diff --git a/compiler-rt/test/tsan/clone_deadlock.cpp b/compiler-rt/test/tsan/clone_deadlock.cpp
deleted file mode 100644 (file)
index 317ccda..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-// RUN: %clangxx_tsan -O1 %s -o %t && %env_tsan_opts=atexit_sleep_ms=0 %run %t 2>&1 | FileCheck %s
-#include "test.h"
-#include <errno.h>
-#include <sched.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-
-long counter;
-
-static void *incrementer(void *arg) {
-  for (;;)
-    __sync_fetch_and_add(&counter, 1);
-  return 0;
-}
-
-static int cloned(void *arg) {
-  for (int i = 0; i < 1000; i++)
-    __sync_fetch_and_add(&counter, 1);
-  exit(0);
-  return 0;
-}
-
-int main() {
-  barrier_init(&barrier, 2);
-  pthread_t th;
-  pthread_create(&th, 0, incrementer, 0);
-  for (int i = 0; i < 100; i++) {
-    char stack[64 << 10] __attribute__((aligned(64)));
-    int pid = clone(cloned, stack + sizeof(stack), SIGCHLD, 0);
-    if (pid == -1) {
-      fprintf(stderr, "failed to clone: %d\n", errno);
-      exit(1);
-    }
-    while (wait(0) != pid) {
-    }
-  }
-  fprintf(stderr, "DONE\n");
-}
-
-// CHECK: DONE