From b32c1d6c3aadf26b650e4b12907398be16929159 Mon Sep 17 00:00:00 2001 From: Vitaly Buka Date: Tue, 25 Jun 2024 10:05:25 -0700 Subject: [PATCH] [tsan] Lock/Unlock allocator and stacks on fork (#96600) We do that for other Sanitizers, and we should do the same for TSAN. There are know deadlocks reports here. --- compiler-rt/lib/tsan/rtl/tsan_mman.cpp | 10 ++++++++++ .../sanitizer_common/TestCases/Posix/fork_threaded.c | 3 --- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/compiler-rt/lib/tsan/rtl/tsan_mman.cpp b/compiler-rt/lib/tsan/rtl/tsan_mman.cpp index 1e579c6a020e9f..e129e9af272f5f 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_mman.cpp +++ b/compiler-rt/lib/tsan/rtl/tsan_mman.cpp @@ -17,6 +17,7 @@ #include "sanitizer_common/sanitizer_common.h" #include "sanitizer_common/sanitizer_errno.h" #include "sanitizer_common/sanitizer_placement_new.h" +#include "sanitizer_common/sanitizer_stackdepot.h" #include "tsan_flags.h" #include "tsan_interface.h" #include "tsan_report.h" @@ -119,9 +120,18 @@ ScopedGlobalProcessor::~ScopedGlobalProcessor() { void AllocatorLockBeforeFork() SANITIZER_NO_THREAD_SAFETY_ANALYSIS { global_proc()->internal_alloc_mtx.Lock(); InternalAllocatorLock(); +#if !SANITIZER_APPLE + // OS X allocates from hooks, see 6a3958247a. + allocator()->ForceLock(); + StackDepotLockBeforeFork(); +#endif } void AllocatorUnlockAfterFork(bool child) SANITIZER_NO_THREAD_SAFETY_ANALYSIS { +#if !SANITIZER_APPLE + StackDepotUnlockAfterFork(child); + allocator()->ForceUnlock(); +#endif InternalAllocatorUnlock(); global_proc()->internal_alloc_mtx.Unlock(); } diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/fork_threaded.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/fork_threaded.c index 13240234a1c79b..27b67db0c0a38c 100644 --- a/compiler-rt/test/sanitizer_common/TestCases/Posix/fork_threaded.c +++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/fork_threaded.c @@ -6,9 +6,6 @@ // FIXME: It probably hangs on this platform. // UNSUPPORTED: ppc -// FIXME: TSAN does not lock allocator. -// UNSUPPORTED: tsan - // FIXME: False stack overflow report // UNSUPPORTED: android && asan