[PATCH] D14379: [tsan] Fix pthread_once interceptor for OS X

Kuba Brecka via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 5 03:28:11 PST 2015


kubabrecka created this revision.
kubabrecka added reviewers: kcc, dvyukov, glider, samsonov.
kubabrecka added subscribers: llvm-commits, zaks.anna, ismailp, jasonk, jevinskie.

TSan has a re-implementation of pthread_once in its interceptor, which assumes that the `pthread_once_t *once_control` pointer is actually pointing to a "storage" which is zero-initialized and used for the atomic operations.  However, that's not true on OS X, where pthread_once_t is a structure, that contains a header (with a magic value) and the actual storage follows after that.  This patch skips the header to make the interceptor work on OS X.

http://reviews.llvm.org/D14379

Files:
  lib/tsan/rtl/tsan_interceptors.cc

Index: lib/tsan/rtl/tsan_interceptors.cc
===================================================================
--- lib/tsan/rtl/tsan_interceptors.cc
+++ lib/tsan/rtl/tsan_interceptors.cc
@@ -1292,7 +1292,11 @@
   SCOPED_INTERCEPTOR_RAW(pthread_once, o, f);
   if (o == 0 || f == 0)
     return EINVAL;
-  atomic_uint32_t *a = static_cast<atomic_uint32_t*>(o);
+  atomic_uint32_t *a;
+  if (!SANITIZER_MAC)
+    a = static_cast<atomic_uint32_t*>(o);
+  else  // On OS X, pthread_once_t has a header with a long-sized signature.
+    a = static_cast<atomic_uint32_t*>((void *)((char *)o + sizeof(long)));
   u32 v = atomic_load(a, memory_order_acquire);
   if (v == 0 && atomic_compare_exchange_strong(a, &v, 1,
                                                memory_order_relaxed)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D14379.39337.patch
Type: text/x-patch
Size: 781 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151105/51f80514/attachment.bin>


More information about the llvm-commits mailing list