Skip to content

Thread Safety Analysis: Differentiate between lock sets at real join points and expected/actual sets at function end #105526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions clang/lib/Analysis/ThreadSafety.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down Expand Up @@ -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);
}
Expand All @@ -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)
Expand Down
20 changes: 8 additions & 12 deletions clang/test/SemaCXX/warn-thread-safety-analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading