[compiler-rt] [TSan][test-only] Make TSan os_unfair_lock.c test check the weak symbol before usage (PR #161173)

Dan Blackwell via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 29 04:19:23 PDT 2025


https://github.com/DanBlackwell created https://github.com/llvm/llvm-project/pull/161173

The os_unfair_lock.c test can currently fail with a null dereference if compiled on a platform with `os_unfair_lock_lock_with_flags`, but then executed on a platform without the function.

This patch fixes this by first checking whether the symbol exists, and falls back to `os_unfair_lock_lock` if not.

rdar://160596542

>From a78b11ae430c057e0eab6320d50f4e8bc5ca323e Mon Sep 17 00:00:00 2001
From: Dan Blackwell <dan_blackwell at apple.com>
Date: Mon, 29 Sep 2025 12:06:22 +0100
Subject: [PATCH] [TSan][test-only] Make TSan os_unfair_lock.c test check the
 weak symbol before using

The os_unfair_lock.c test can currently fail with a null dereference if compiled on a platform with os_unfair_lock_lock_with_flags, but then executed on a platform without the function.

This patch fixes this by first checking whether the symbol exists, and falls back to os_unfair_lock_lock if not.

rdar://160596542
---
 compiler-rt/test/tsan/Darwin/os_unfair_lock.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/test/tsan/Darwin/os_unfair_lock.c b/compiler-rt/test/tsan/Darwin/os_unfair_lock.c
index e2a491aa98d6d..883154c372c7f 100644
--- a/compiler-rt/test/tsan/Darwin/os_unfair_lock.c
+++ b/compiler-rt/test/tsan/Darwin/os_unfair_lock.c
@@ -22,8 +22,12 @@ void *ThreadWithFlags(void *a) {
     defined(__VISIONOS_2_0) || defined(__WATCHOS_11_0)
 #  pragma clang diagnostic push
 #  pragma clang diagnostic ignored "-Wunguarded-availability-new"
-  os_unfair_lock_lock_with_flags(&lock, OS_UNFAIR_LOCK_FLAG_ADAPTIVE_SPIN);
-  flags_available = 1;
+  if (os_unfair_lock_lock_with_flags) {
+    os_unfair_lock_lock_with_flags(&lock, OS_UNFAIR_LOCK_FLAG_ADAPTIVE_SPIN);
+    flags_available = 1;
+  } else {
+    os_unfair_lock_lock(&lock);
+  }
 #  pragma clang diagnostic pop
 #else
   os_unfair_lock_lock(&lock);



More information about the llvm-commits mailing list