[clang] Thread Safety Analysis: Differentiate between lock sets at real join points and expected/actual sets at function end (PR #105526)
Malek Ben Slimane via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 21 06:38:27 PDT 2024
https://github.com/malek203 created https://github.com/llvm/llvm-project/pull/105526
This fixes false positives related to returning a scoped lockable object. At the end of a function, we check managed locks instead of scoped locks.
At real join points, we skip checking managed locks because we assume that the scope keeps track of its underlying mutexes and will release them at its destruction. So, checking for the scopes is sufficient. However, at the end of a function, we aim at comparing the expected and the actual lock sets. There, we skip checking scoped locks to prevent to get duplicate warnings for the same lock.
>From 9d4dbf5c3be29175e23f1881efa8f3b7626de755 Mon Sep 17 00:00:00 2001
From: Malek Ben Slimane <malek.ben.slimane at sap.com>
Date: Tue, 20 Aug 2024 14:53:54 +0200
Subject: [PATCH] Thread Safety Analysis: Differentiate between lock sets at
real join points and expected/actual sets at function end
This fixes false positives related to returning a scoped lockable
object. At the end of a function, we check managed locks instead of
scoped locks.
At real join points, we skip checking managed locks because we assume
that the scope keeps track of its underlying mutexes and will release
them at its destruction. So, checking for the scopes is sufficient.
However, at the end of a function, we aim at comparing the expected and
the actual lock sets. There, we skip checking scoped locks to prevent to
get duplicate warnings for the same lock.
---
clang/lib/Analysis/ThreadSafety.cpp | 8 ++++++--
.../SemaCXX/warn-thread-safety-analysis.cpp | 20 ++++++++-----------
2 files changed, 14 insertions(+), 14 deletions(-)
diff --git a/clang/lib/Analysis/ThreadSafety.cpp b/clang/lib/Analysis/ThreadSafety.cpp
index e25b843c9bf83e..c4a83b069e0792 100644
--- a/clang/lib/Analysis/ThreadSafety.cpp
+++ b/clang/lib/Analysis/ThreadSafety.cpp
@@ -922,6 +922,9 @@ class ScopedLockableFactEntry : public FactEntry {
handleRemovalFromIntersection(const FactSet &FSet, FactManager &FactMan,
SourceLocation JoinLoc, LockErrorKind LEK,
ThreadSafetyHandler &Handler) const override {
+ if (LEK == LEK_LockedAtEndOfFunction || LEK == LEK_NotLockedAtEndOfFunction)
+ return;
+
for (const auto &UnderlyingMutex : UnderlyingMutexes) {
const auto *Entry = FSet.findLock(FactMan, UnderlyingMutex.Cap);
if ((UnderlyingMutex.Kind == UCK_Acquired && Entry) ||
@@ -2224,7 +2227,7 @@ void ThreadSafetyAnalyzer::intersectAndWarn(FactSet &EntrySet,
if (join(FactMan[*EntryIt], ExitFact,
EntryLEK != LEK_LockedSomeLoopIterations))
*EntryIt = Fact;
- } else if (!ExitFact.managed()) {
+ } else if (!ExitFact.managed() || EntryLEK == LEK_LockedAtEndOfFunction) {
ExitFact.handleRemovalFromIntersection(ExitSet, FactMan, JoinLoc,
EntryLEK, Handler);
}
@@ -2236,7 +2239,8 @@ void ThreadSafetyAnalyzer::intersectAndWarn(FactSet &EntrySet,
const FactEntry *ExitFact = ExitSet.findLock(FactMan, *EntryFact);
if (!ExitFact) {
- if (!EntryFact->managed() || ExitLEK == LEK_LockedSomeLoopIterations)
+ if (!EntryFact->managed() || ExitLEK == LEK_LockedSomeLoopIterations ||
+ ExitLEK == LEK_NotLockedAtEndOfFunction)
EntryFact->handleRemovalFromIntersection(EntrySetOrig, FactMan, JoinLoc,
ExitLEK, Handler);
if (ExitLEK == LEK_LockedSomePredecessors)
diff --git a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
index af9254508d8050..8477200456d985 100644
--- a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
+++ b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
@@ -6077,24 +6077,20 @@ namespace ReturnScopedLockable {
class Object {
public:
MutexLock lock() EXCLUSIVE_LOCK_FUNCTION(mutex) {
- // TODO: False positive because scoped lock isn't destructed.
- return MutexLock(&mutex); // expected-note {{mutex acquired here}}
- } // expected-warning {{mutex 'mutex' is still held at the end of function}}
+ return MutexLock(&mutex);
+ }
ReaderMutexLock lockShared() SHARED_LOCK_FUNCTION(mutex) {
- // TODO: False positive because scoped lock isn't destructed.
- return ReaderMutexLock(&mutex); // expected-note {{mutex acquired here}}
- } // expected-warning {{mutex 'mutex' is still held at the end of function}}
+ return ReaderMutexLock(&mutex);
+ }
MutexLock adopt() EXCLUSIVE_LOCKS_REQUIRED(mutex) {
- // TODO: False positive because scoped lock isn't destructed.
- return MutexLock(&mutex, true); // expected-note {{mutex acquired here}}
- } // expected-warning {{mutex 'mutex' is still held at the end of function}}
+ return MutexLock(&mutex, true);
+ }
ReaderMutexLock adoptShared() SHARED_LOCKS_REQUIRED(mutex) {
- // TODO: False positive because scoped lock isn't destructed.
- return ReaderMutexLock(&mutex, true); // expected-note {{mutex acquired here}}
- } // expected-warning {{mutex 'mutex' is still held at the end of function}}
+ return ReaderMutexLock(&mutex, true);
+ }
int x GUARDED_BY(mutex);
void needsLock() EXCLUSIVE_LOCKS_REQUIRED(mutex);
More information about the cfe-commits
mailing list