[compiler-rt] 4d4a353 - [TSan] Increase the number of simultaneously locked mutexes that a thread can hold (#116409)

via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 24 00:58:43 PST 2024


Author: gbMattN
Date: 2024-11-24T00:58:39-08:00
New Revision: 4d4a353b8eddb0728d5b278befdccda4de484319

URL: https://github.com/llvm/llvm-project/commit/4d4a353b8eddb0728d5b278befdccda4de484319
DIFF: https://github.com/llvm/llvm-project/commit/4d4a353b8eddb0728d5b278befdccda4de484319.diff

LOG: [TSan] Increase the number of simultaneously locked mutexes that a thread can hold (#116409)

I've run into an issue where TSan can't be used on some code without
turning off deadlock detection because a thread tries to hold too many
mutexes. It would be preferable to be able to use deadlock detection as
that is a major benefit of TSan.

Its mentioned in https://github.com/google/sanitizers/issues/950 that
the 64 mutex limit was an arbitrary number. I've increased it to 128 and
all the tests still pass. Considering the increasing number of cores on
CPUs and how programs can now use more threads to take advantage of it,
I think raising the limit to 128 would be some good future proofing

---------

Co-authored-by: Vitaly Buka <vitalybuka at google.com>

Added: 
    compiler-rt/test/tsan/many_held_mutex.cpp

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_deadlock_detector.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_deadlock_detector.h b/compiler-rt/lib/sanitizer_common/sanitizer_deadlock_detector.h
index 0749f633b4bcf5..1664b92b213692 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_deadlock_detector.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_deadlock_detector.h
@@ -120,7 +120,7 @@ class DeadlockDetectorTLS {
     u32 lock;
     u32 stk;
   };
-  LockWithContext all_locks_with_contexts_[64];
+  LockWithContext all_locks_with_contexts_[128];
   uptr n_all_locks_;
 };
 

diff  --git a/compiler-rt/test/tsan/many_held_mutex.cpp b/compiler-rt/test/tsan/many_held_mutex.cpp
new file mode 100644
index 00000000000000..76e072b35a2336
--- /dev/null
+++ b/compiler-rt/test/tsan/many_held_mutex.cpp
@@ -0,0 +1,21 @@
+// RUN: %clangxx_tsan -O1 %s %link_libcxx_tsan -fsanitize=thread -o %t
+// RUN: %run %t 128
+
+#include <mutex>
+#include <string>
+#include <vector>
+
+int main(int argc, char *argv[]) {
+  int num_of_mtx = std::atoi(argv[1]);
+
+  std::vector<std::mutex> mutexes(num_of_mtx);
+
+  for (auto &mu : mutexes) {
+    mu.lock();
+  }
+  for (auto &mu : mutexes) {
+    mu.unlock();
+  }
+
+  return 0;
+}


        


More information about the llvm-commits mailing list