Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tsan] Lock/Unlock allocator and stacks on fork #96600

Conversation

vitalybuka
Copy link
Collaborator

We do that for other Sanitizers, and we
should do the same for TSAN.
There are know deadlocks reports here.

@llvmbot
Copy link
Collaborator

llvmbot commented Jun 25, 2024

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Vitaly Buka (vitalybuka)

Changes

We do that for other Sanitizers, and we
should do the same for TSAN.
There are know deadlocks reports here.


Full diff: https://github.com/llvm/llvm-project/pull/96600.diff

2 Files Affected:

  • (modified) compiler-rt/lib/tsan/rtl/tsan_mman.cpp (+10)
  • (modified) compiler-rt/test/sanitizer_common/TestCases/Posix/fork_threaded.c (-3)
diff --git a/compiler-rt/lib/tsan/rtl/tsan_mman.cpp b/compiler-rt/lib/tsan/rtl/tsan_mman.cpp
index 1e579c6a020e9..e129e9af272f5 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 13240234a1c79..27b67db0c0a38 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
 

Created using spr 1.3.4

[skip ci]
Created using spr 1.3.4
Copy link
Collaborator

@dvyukov dvyukov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that tsan simply disables itself after multithreaded fork, so it produces false sense to operation with die_after_fork=0. And it still can deadlock on mutex/atomic operations and maybe more.

@fmayer
Copy link
Contributor

fmayer commented Jun 25, 2024

nit: and stack depot? "and stacks" is confusing a bit.

Created using spr 1.3.4

[skip ci]
Created using spr 1.3.4
@vitalybuka vitalybuka changed the base branch from users/vitalybuka/spr/main.tsan-lockunlock-allocator-and-stacks-on-fork to main June 25, 2024 17:04
@vitalybuka vitalybuka merged commit c0dc134 into main Jun 25, 2024
5 of 6 checks passed
@vitalybuka vitalybuka deleted the users/vitalybuka/spr/tsan-lockunlock-allocator-and-stacks-on-fork branch June 25, 2024 17:05
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
We do that for other Sanitizers, and we
should do the same for TSAN.
There are know deadlocks reports here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants