Skip to content

Commit 0bc626d

Browse files
committed
tsan: refactor guard_acquire/release
Introduce named consts for magic values we use. Differential Revision: https://reviews.llvm.org/D107445
1 parent 0556138 commit 0bc626d

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -844,16 +844,26 @@ TSAN_INTERCEPTOR(int, posix_memalign, void **memptr, uptr align, uptr sz) {
844844
}
845845
#endif
846846

847+
// Both __cxa_guard_acquire and pthread_once 0-initialize
848+
// the object initially. pthread_once does not have any
849+
// other ABI requirements. __cxa_guard_acquire assumes
850+
// that any non-0 value in the first byte means that
851+
// initialization is completed. Contents of the remaining
852+
// bytes are up to us.
853+
constexpr u32 kGuardInit = 0;
854+
constexpr u32 kGuardDone = 1;
855+
constexpr u32 kGuardRunning = 1 << 16;
856+
847857
static int guard_acquire(ThreadState *thr, uptr pc, atomic_uint32_t *g) {
848858
OnPotentiallyBlockingRegionBegin();
849859
auto on_exit = at_scope_exit(&OnPotentiallyBlockingRegionEnd);
850860
for (;;) {
851861
u32 cmp = atomic_load(g, memory_order_acquire);
852-
if (cmp == 0) {
853-
if (atomic_compare_exchange_strong(g, &cmp, 1 << 16,
862+
if (cmp == kGuardInit) {
863+
if (atomic_compare_exchange_strong(g, &cmp, kGuardRunning,
854864
memory_order_relaxed))
855865
return 1;
856-
} else if (cmp == 1) {
866+
} else if (cmp == kGuardDone) {
857867
if (!thr->in_ignored_lib)
858868
Acquire(thr, pc, (uptr)g);
859869
return 0;
@@ -866,7 +876,7 @@ static int guard_acquire(ThreadState *thr, uptr pc, atomic_uint32_t *g) {
866876
static void guard_release(ThreadState *thr, uptr pc, atomic_uint32_t *g) {
867877
if (!thr->in_ignored_lib)
868878
Release(thr, pc, (uptr)g);
869-
atomic_store(g, 1, memory_order_release);
879+
atomic_store(g, kGuardDone, memory_order_release);
870880
}
871881

872882
// __cxa_guard_acquire and friends need to be intercepted in a special way -
@@ -899,7 +909,7 @@ STDCXX_INTERCEPTOR(void, __cxa_guard_release, atomic_uint32_t *g) {
899909

900910
STDCXX_INTERCEPTOR(void, __cxa_guard_abort, atomic_uint32_t *g) {
901911
SCOPED_INTERCEPTOR_RAW(__cxa_guard_abort, g);
902-
atomic_store(g, 0, memory_order_relaxed);
912+
atomic_store(g, kGuardInit, memory_order_relaxed);
903913
}
904914

905915
namespace __tsan {

0 commit comments

Comments
 (0)