[clang] 6401699 - [analyzer] Model XNU try-lock nonzero return as success (#248014)

via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 3 01:44:25 PST 2026


Author: Endre Fülöp
Date: 2026-12-03T10:44:18+01:00
New Revision: 64016990a6af14d0a166d366846e257e2705821e

URL: https://github.com/llvm/llvm-project/commit/64016990a6af14d0a166d366846e257e2705821e
DIFF: https://github.com/llvm/llvm-project/commit/64016990a6af14d0a166d366846e257e2705821e.diff

LOG: [analyzer] Model XNU try-lock nonzero return as success (#248014)

XNU try-lock functions signal success by returning a nonzero value,
which is the opposite of how the pthread library does it. The
implementation of alpha.unix.PthreadLock mistakenly assumed the pthread
semantics. This was introduced as a regression in 152bc7ffdcd8 while
migrating the logic to use a CallDescriptionMap.

Fixes #246175

Assisted-by: Claude Code

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.md
    clang/lib/StaticAnalyzer/Checkers/PthreadLockChecker.cpp
    clang/test/Analysis/pthreadlock.c

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 2c1e0b45e9daec..a6c123abb4bbc2 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1743,6 +1743,12 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
   (such as `read`) via fuzzy builtin matching, and added explicit
   `CStringChecker` modeling for C23 `memset_explicit`.
 
+- Fixed a regression in the `alpha.unix.PthreadLock` checker, introduced in
+  Clang 11, where a zero return value of the XNU try-lock functions
+  (`lck_mtx_try_lock`, `lck_rw_try_lock_exclusive` and
+  `lck_rw_try_lock_shared`) was treated as a successful acquisition instead of
+  a non-zero one. (#GH246175)
+
 % comment:
 % This is for the Static Analyzer.
 % Use `####` headings for subsections:

diff  --git a/clang/lib/StaticAnalyzer/Checkers/PthreadLockChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/PthreadLockChecker.cpp
index 210d1195cbc773..68ec707b4dbb55 100644
--- a/clang/lib/StaticAnalyzer/Checkers/PthreadLockChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/PthreadLockChecker.cpp
@@ -432,7 +432,7 @@ void PthreadLockChecker::TryPthreadLock(const CallEvent &Call,
 void PthreadLockChecker::TryXNULock(const CallEvent &Call, CheckerContext &C,
                                     CheckerKind CheckKind) const {
   AcquireLockAux(Call, C, Call.getArgExpr(0), Call.getArgSVal(0), true,
-                 PthreadSemantics, CheckKind);
+                 XNUSemantics, CheckKind);
 }
 
 void PthreadLockChecker::TryFuchsiaLock(const CallEvent &Call,

diff  --git a/clang/test/Analysis/pthreadlock.c b/clang/test/Analysis/pthreadlock.c
index 7ef20341c90fca..efc8b9e7d4aa27 100644
--- a/clang/test/Analysis/pthreadlock.c
+++ b/clang/test/Analysis/pthreadlock.c
@@ -543,6 +543,16 @@ void bad33(void) {
   pthread_mutex_lock(pmtx); // expected-warning{{This lock has already been acquired}}
 }
 
+void xnu_trylock_success_holds_lock(void) {
+  if (lck_mtx_try_lock(&lck1))
+    lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}}
+}
+
+void xnu_trylock_failure_does_not_hold_lock(void) {
+  if (!lck_mtx_try_lock(&lck1))
+    lck_mtx_lock(&lck1); // no-warning
+}
+
 void nocrash1(pthread_mutex_t *mutex) {
   int ret = pthread_mutex_destroy(mutex);
   if (ret == 0) // no crash


        


More information about the cfe-commits mailing list