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

via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 15 22:21:55 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

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

Author: None (gbMattN)

<details>
<summary>Changes</summary>

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

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


2 Files Affected:

- (modified) compiler-rt/lib/sanitizer_common/sanitizer_deadlock_detector.h (+1-1) 
- (added) compiler-rt/test/tsan/many_held_mutex.cpp (+23) 


``````````diff
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..479aa20a7b9df3
--- /dev/null
+++ b/compiler-rt/test/tsan/many_held_mutex.cpp
@@ -0,0 +1,23 @@
+// RUN: %clangxx_tsan %s -fsanitize=thread -o %t && %run %t 2>&1 | Filecheck %s
+
+#include <mutex>
+#include <stdio.h>
+
+int main(){
+    const unsigned short NUM_OF_MTX = 128;
+    std::mutex mutexes[NUM_OF_MTX];
+
+    for(int i = 0; i < NUM_OF_MTX; i++){
+        mutexes[i].lock();
+    }
+    for(int i = 0; i < NUM_OF_MTX; i++){
+        mutexes[i].unlock();
+    }
+
+    printf("Success\n");
+
+    return 0;
+}
+
+// CHECK: Success
+// CHECK-NOT: ThreadSanitizer: CHECK failed

``````````

</details>


https://github.com/llvm/llvm-project/pull/116409


More information about the llvm-commits mailing list