[compiler-rt] [TSan] Add interceptor for os_unfair_lock_lock_with_flags (PR #153815)

via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 15 07:44:34 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

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

Author: Dan Blackwell (DanBlackwell)

<details>
<summary>Changes</summary>

Also update os_unfair_lock tsan test to check this function on platforms where it is available.

rdar://158294950

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


2 Files Affected:

- (modified) compiler-rt/lib/tsan/rtl/tsan_interceptors_mac.cpp (+18) 
- (modified) compiler-rt/test/tsan/Darwin/os_unfair_lock.c (+28-1) 


``````````diff
diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors_mac.cpp b/compiler-rt/lib/tsan/rtl/tsan_interceptors_mac.cpp
index 978664411fff4..91567ac5541bf 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_interceptors_mac.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors_mac.cpp
@@ -281,6 +281,24 @@ TSAN_INTERCEPTOR(void, os_unfair_lock_lock, os_unfair_lock_t lock) {
   Acquire(thr, pc, (uptr)lock);
 }
 
+// os_unfair_lock_lock_with_flags was introduced in macOS 15
+#if defined(__MAC_15_0)
+#  pragma clang diagnostic push
+#  pragma clang diagnostic ignored "-Wunguarded-availability-new"
+// We're just intercepting this - if it doesn't exist on the platform, then the
+// process shouldn't have called it in the first place.
+TSAN_INTERCEPTOR(void, os_unfair_lock_lock_with_flags, os_unfair_lock_t lock,
+                 os_unfair_lock_flags_t flags) {
+  if (!cur_thread()->is_inited || cur_thread()->is_dead) {
+    return REAL(os_unfair_lock_lock_with_flags)(lock, flags);
+  }
+  SCOPED_TSAN_INTERCEPTOR(os_unfair_lock_lock_with_flags, lock, flags);
+  REAL(os_unfair_lock_lock_with_flags)(lock, flags);
+  Acquire(thr, pc, (uptr)lock);
+}
+#  pragma clang diagnostic pop
+#endif
+
 TSAN_INTERCEPTOR(void, os_unfair_lock_lock_with_options, os_unfair_lock_t lock,
                  u32 options) {
   if (!cur_thread()->is_inited || cur_thread()->is_dead) {
diff --git a/compiler-rt/test/tsan/Darwin/os_unfair_lock.c b/compiler-rt/test/tsan/Darwin/os_unfair_lock.c
index 320e7f5e56d1d..986c37127cbe8 100644
--- a/compiler-rt/test/tsan/Darwin/os_unfair_lock.c
+++ b/compiler-rt/test/tsan/Darwin/os_unfair_lock.c
@@ -1,6 +1,7 @@
 // RUN: %clang_tsan %s -o %t
 // RUN: %run %t 2>&1 | FileCheck %s --implicit-check-not='ThreadSanitizer'
 
+#include <Availability.h>
 #include <os/lock.h>
 #include <pthread.h>
 #include <stdio.h>
@@ -15,6 +16,16 @@ void *Thread(void *a) {
   return NULL;
 }
 
+void *ThreadWithFlags(void *a) {
+  if (__builtin_available(iOS 18.0, macOS 15.0, tvOS 18.0, visionOS 2.0, watchOS 11.0, *))
+    os_unfair_lock_lock_with_flags(&lock, OS_UNFAIR_LOCK_FLAG_ADAPTIVE_SPIN);
+  else
+    os_unfair_lock_lock(&lock);
+  global_variable++;
+  os_unfair_lock_unlock(&lock);
+  return NULL;
+}
+
 int main() {
   pthread_t t1, t2;
   global_variable = 0;
@@ -23,6 +34,22 @@ int main() {
   pthread_join(t1, NULL);
   pthread_join(t2, NULL);
   fprintf(stderr, "global_variable = %ld\n", global_variable);
-}
 
 // CHECK: global_variable = 2
+
+  void *(*func)(void *) = Thread;
+  char flags_available = 0;
+  if (__builtin_available(iOS 18.0, macOS 15.0, tvOS 18.0, visionOS 2.0, watchOS 11.0, *)) {
+    func = ThreadWithFlags;
+    flags_available = 1;
+  }
+
+  pthread_create(&t1, NULL, func, NULL);
+  pthread_create(&t2, NULL, func, NULL);
+  pthread_join(t1, NULL);
+  pthread_join(t2, NULL);
+  fprintf(stderr, "global_variable = %ld, os_unfair_lock_lock_with_flags %savailable\n", 
+          global_variable, flags_available ? "" : "un");
+}
+
+// CHECK: global_variable = 4

``````````

</details>


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


More information about the llvm-commits mailing list