This is an archive of the discontinued LLVM Phabricator instance.

[tsan] Add support for OS X OSAtomic* functions
ClosedPublic

Authored by kubamracek on Mar 27 2016, 10:13 AM.

Details

Summary

OS X provides atomic functions in libkern/OSAtomic.h. These provide atomic guarantees and they have alternatives which have barrier semantics. This patch adds proper TSan support for these functions (all from libkern/OSAtomic.h):

  • OSAtomicAdd32
  • OSAtomicAdd32Barrier
  • OSAtomicAdd64
  • OSAtomicAdd64Barrier
  • OSAtomicAnd32
  • OSAtomicAnd32Barrier
  • OSAtomicAnd32Orig
  • OSAtomicAnd32OrigBarrier
  • OSAtomicCompareAndSwap32
  • OSAtomicCompareAndSwap32Barrier
  • OSAtomicCompareAndSwap64
  • OSAtomicCompareAndSwap64Barrier
  • OSAtomicCompareAndSwapInt
  • OSAtomicCompareAndSwapIntBarrier
  • OSAtomicCompareAndSwapLong
  • OSAtomicCompareAndSwapLongBarrier
  • OSAtomicCompareAndSwapPtr
  • OSAtomicCompareAndSwapPtrBarrier
  • OSAtomicDecrement32
  • OSAtomicDecrement32Barrier
  • OSAtomicDecrement64
  • OSAtomicDecrement64Barrier
  • OSAtomicDequeue
  • OSAtomicEnqueue
  • OSAtomicFifoDequeue
  • OSAtomicFifoEnqueue
  • OSAtomicIncrement32
  • OSAtomicIncrement32Barrier
  • OSAtomicIncrement64
  • OSAtomicIncrement64Barrier
  • OSAtomicOr32
  • OSAtomicOr32Barrier
  • OSAtomicOr32Orig
  • OSAtomicOr32OrigBarrier
  • OSAtomicTestAndClear
  • OSAtomicTestAndClearBarrier
  • OSAtomicTestAndSet
  • OSAtomicTestAndSetBarrier
  • OSAtomicXor32
  • OSAtomicXor32Barrier
  • OSAtomicXor32Orig
  • OSAtomicXor32OrigBarrier

Diff Detail

Event Timeline

kubamracek retitled this revision from to [tsan] Add support for OS X OSAtomic* functions.
kubamracek updated this object.
kubamracek added reviewers: dvyukov, samsonov, glider, kcc.
kubamracek updated this revision to Diff 51829.Mar 28 2016, 1:32 PM

Updating patch, OSAtomicFifoEnqueue and OSAtomicFifoDequeue are only available on OS X.

kubamracek updated this revision to Diff 51889.EditedMar 29 2016, 3:06 AM

Updating patch to use "tsan_interface.h" instead of the system clang's <sanitizer/tsan_interface_atomic.h>. Relies on http://reviews.llvm.org/D18543.

dvyukov added inline comments.Apr 4 2016, 1:01 AM
lib/tsan/rtl/tsan_interceptors_mac.cc
121

This is not correct. If you separate the operation itself and the memory barrier, release barrier needs to precede the operation itself (while acquire barrier needs to follow the operation itself). If you split this into acquire+operation+release, it will increase cost of the operation significantly, as we will lock sync mutex twice and process vector clock twice.
I would suggest to use __tsan_atomic8_fetch_or/and to implement these bit flag operations. For NoBarrier versions you can pass mo_relaxed and it should be as cheap as calling the REAL function.

135

same here

143

Release must precede the operation. Also I would use __tsan_release(item).
If you do it after, then consumer can dequeue the item and do acquire before we do release. This will lead to false positives.

149

__tsan_acquire(item)

160

__tsan_release(item) before the operation.

167

__tsan_acquire(item)

kubamracek updated this revision to Diff 52536.Apr 4 2016, 3:13 AM

Addressing review comments.

dvyukov accepted this revision.Apr 4 2016, 3:56 AM
dvyukov edited edge metadata.
This revision is now accepted and ready to land.Apr 4 2016, 3:56 AM
This revision was automatically updated to reflect the committed changes.