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

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 24 00:54:15 PST 2024


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

>From 9062012f500f47454708223f84d39e5d8b98c3b7 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Fri, 15 Nov 2024 15:41:24 +0000
Subject: [PATCH 1/8] [TSan] Increase the limit of simultaniously held mutexes
 TSan can keep track of from 64 to 128

---
 .../sanitizer_deadlock_detector.h             |  2 +-
 compiler-rt/test/tsan/many_held_mutex.cpp     | 23 +++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)
 create mode 100644 compiler-rt/test/tsan/many_held_mutex.cpp

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

>From 61954f9b2526a5e0155c5a13d69fd52072215a84 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Mon, 18 Nov 2024 10:32:32 +0000
Subject: [PATCH 2/8] Fixed the test and its formatting

---
 compiler-rt/test/tsan/many_held_mutex.cpp | 24 +++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/compiler-rt/test/tsan/many_held_mutex.cpp b/compiler-rt/test/tsan/many_held_mutex.cpp
index 479aa20a7b9df3..36a599904ffe31 100644
--- a/compiler-rt/test/tsan/many_held_mutex.cpp
+++ b/compiler-rt/test/tsan/many_held_mutex.cpp
@@ -1,22 +1,22 @@
-// RUN: %clangxx_tsan %s -fsanitize=thread -o %t && %run %t 2>&1 | Filecheck %s
+// 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];
+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();
-    }
+  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");
+  printf("Success\n");
 
-    return 0;
+  return 0;
 }
 
 // CHECK: Success

>From c173dde7eeb705d97e2c8be7bda400dcd3f1f03f Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Tue, 19 Nov 2024 13:21:40 +0000
Subject: [PATCH 3/8] Tidied up the test further

---
 compiler-rt/test/tsan/many_held_mutex.cpp | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/compiler-rt/test/tsan/many_held_mutex.cpp b/compiler-rt/test/tsan/many_held_mutex.cpp
index 36a599904ffe31..94ef02a24628fb 100644
--- a/compiler-rt/test/tsan/many_held_mutex.cpp
+++ b/compiler-rt/test/tsan/many_held_mutex.cpp
@@ -1,23 +1,22 @@
-// RUN: %clangxx_tsan %s -fsanitize=thread -o %t && %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx_tsan %s -fsanitize=thread -o %t
+// RUN: %run %t 128
+// RUN: not %run %t 129
 
 #include <mutex>
-#include <stdio.h>
+#include <string>
 
-int main() {
-  const unsigned short NUM_OF_MTX = 128;
-  std::mutex mutexes[NUM_OF_MTX];
+int main(int argc, char *argv[]) {
+  int num_of_mtx = std::stoi(argv[1]);
 
-  for (int i = 0; i < NUM_OF_MTX; i++) {
+  std::mutex* mutexes = new std::mutex[num_of_mtx];
+
+  for (int i = 0; i < num_of_mtx; i++) {
     mutexes[i].lock();
   }
-  for (int i = 0; i < NUM_OF_MTX; i++) {
+  for (int i = 0; i < num_of_mtx; i++) {
     mutexes[i].unlock();
   }
 
-  printf("Success\n");
-
+  delete[] mutexes;
   return 0;
 }
-
-// CHECK: Success
-// CHECK-NOT: ThreadSanitizer: CHECK failed

>From 84c23bb423652491993b0ce2b65fa897455133a6 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Tue, 19 Nov 2024 13:28:31 +0000
Subject: [PATCH 4/8] Moved star to correct part of declaration

---
 compiler-rt/test/tsan/many_held_mutex.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler-rt/test/tsan/many_held_mutex.cpp b/compiler-rt/test/tsan/many_held_mutex.cpp
index 94ef02a24628fb..e3c9e3dcabdc19 100644
--- a/compiler-rt/test/tsan/many_held_mutex.cpp
+++ b/compiler-rt/test/tsan/many_held_mutex.cpp
@@ -8,7 +8,7 @@
 int main(int argc, char *argv[]) {
   int num_of_mtx = std::stoi(argv[1]);
 
-  std::mutex* mutexes = new std::mutex[num_of_mtx];
+  std::mutex *mutexes = new std::mutex[num_of_mtx];
 
   for (int i = 0; i < num_of_mtx; i++) {
     mutexes[i].lock();

>From 1670e0a0efe54595d82a4941ee1eba96b46631b9 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Sat, 23 Nov 2024 23:21:28 -0800
Subject: [PATCH 5/8] Fix linking c++

---
 compiler-rt/test/tsan/many_held_mutex.cpp | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/compiler-rt/test/tsan/many_held_mutex.cpp b/compiler-rt/test/tsan/many_held_mutex.cpp
index e3c9e3dcabdc19..8a5babad9ba5c7 100644
--- a/compiler-rt/test/tsan/many_held_mutex.cpp
+++ b/compiler-rt/test/tsan/many_held_mutex.cpp
@@ -1,22 +1,22 @@
-// RUN: %clangxx_tsan %s -fsanitize=thread -o %t
+// RUN: %clangxx_tsan -O1 %s %link_libcxx_tsan -fsanitize=thread -o %t
 // RUN: %run %t 128
 // RUN: not %run %t 129
 
 #include <mutex>
+#include <vector>
 #include <string>
 
 int main(int argc, char *argv[]) {
-  int num_of_mtx = std::stoi(argv[1]);
+  int num_of_mtx = std::atoi(argv[1]);
 
-  std::mutex *mutexes = new std::mutex[num_of_mtx];
+  std::vector<std::mutex> mutexes(num_of_mtx);
 
-  for (int i = 0; i < num_of_mtx; i++) {
-    mutexes[i].lock();
+  for (auto& mu : mutexes) {
+    mu.lock();
   }
-  for (int i = 0; i < num_of_mtx; i++) {
-    mutexes[i].unlock();
+  for (auto& mu : mutexes) {
+    mu.unlock();
   }
 
-  delete[] mutexes;
   return 0;
 }

>From 3f239bdcf5fe2e01374938b7904e6a731140321e Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Sun, 24 Nov 2024 00:15:51 -0800
Subject: [PATCH 6/8] fast_unwind_on_fatal

---
 compiler-rt/test/tsan/many_held_mutex.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler-rt/test/tsan/many_held_mutex.cpp b/compiler-rt/test/tsan/many_held_mutex.cpp
index 8a5babad9ba5c7..fc098fbf93f479 100644
--- a/compiler-rt/test/tsan/many_held_mutex.cpp
+++ b/compiler-rt/test/tsan/many_held_mutex.cpp
@@ -1,6 +1,6 @@
 // RUN: %clangxx_tsan -O1 %s %link_libcxx_tsan -fsanitize=thread -o %t
 // RUN: %run %t 128
-// RUN: not %run %t 129
+// RUN: %env_tsan_opts=fast_unwind_on_fatal=1 not %run %t 129
 
 #include <mutex>
 #include <vector>

>From 79993af0a5e7ce20ad16b671c9fe940e8464b460 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Sun, 24 Nov 2024 00:47:44 -0800
Subject: [PATCH 7/8] Remove %run %t 129 it still deadlocks in CHECK

---
 compiler-rt/test/tsan/many_held_mutex.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/compiler-rt/test/tsan/many_held_mutex.cpp b/compiler-rt/test/tsan/many_held_mutex.cpp
index fc098fbf93f479..54687634825a28 100644
--- a/compiler-rt/test/tsan/many_held_mutex.cpp
+++ b/compiler-rt/test/tsan/many_held_mutex.cpp
@@ -1,6 +1,5 @@
 // RUN: %clangxx_tsan -O1 %s %link_libcxx_tsan -fsanitize=thread -o %t
 // RUN: %run %t 128
-// RUN: %env_tsan_opts=fast_unwind_on_fatal=1 not %run %t 129
 
 #include <mutex>
 #include <vector>

>From a2dfe82df84e3789f0e7ad5c93c1bce61bc23849 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Sun, 24 Nov 2024 00:53:54 -0800
Subject: [PATCH 8/8] format

---
 compiler-rt/test/tsan/many_held_mutex.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/compiler-rt/test/tsan/many_held_mutex.cpp b/compiler-rt/test/tsan/many_held_mutex.cpp
index 54687634825a28..76e072b35a2336 100644
--- a/compiler-rt/test/tsan/many_held_mutex.cpp
+++ b/compiler-rt/test/tsan/many_held_mutex.cpp
@@ -2,18 +2,18 @@
 // RUN: %run %t 128
 
 #include <mutex>
-#include <vector>
 #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) {
+  for (auto &mu : mutexes) {
     mu.lock();
   }
-  for (auto& mu : mutexes) {
+  for (auto &mu : mutexes) {
     mu.unlock();
   }
 



More information about the llvm-commits mailing list