Skip to content

Commit a70f021

Browse files
committed
Thread Safety Analysis: Handle address-of followed by dereference
Correctly analyze expressions where the address of a guarded variable is taken and immediately dereferenced, such as (*(type-specifier *)&x). Previously, such patterns would result in false negatives.
1 parent 79d8a34 commit a70f021

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

clang/lib/Analysis/ThreadSafety.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,8 @@ void ThreadSafetyAnalyzer::checkAccess(const FactSet &FSet, const Expr *Exp,
17651765
void ThreadSafetyAnalyzer::checkPtAccess(const FactSet &FSet, const Expr *Exp,
17661766
AccessKind AK,
17671767
ProtectedOperationKind POK) {
1768+
// Strips off paren- and cast-expressions, checking if we encounter any other
1769+
// operator that should be delegated to checkAccess() instead.
17681770
while (true) {
17691771
if (const auto *PE = dyn_cast<ParenExpr>(Exp)) {
17701772
Exp = PE->getSubExpr();
@@ -1780,6 +1782,14 @@ void ThreadSafetyAnalyzer::checkPtAccess(const FactSet &FSet, const Expr *Exp,
17801782
Exp = CE->getSubExpr();
17811783
continue;
17821784
}
1785+
if (const auto *UO = dyn_cast<UnaryOperator>(Exp)) {
1786+
if (UO->getOpcode() == UO_AddrOf) {
1787+
// Pointer access via pointer taken of variable, so the dereferenced
1788+
// variable is not actually a pointer.
1789+
checkAccess(FSet, UO->getSubExpr(), AK, POK);
1790+
return;
1791+
}
1792+
}
17831793
break;
17841794
}
17851795

clang/test/Sema/warn-thread-safety-analysis.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
__attribute__ ((shared_locks_required(__VA_ARGS__)))
2525
#define NO_THREAD_SAFETY_ANALYSIS __attribute__ ((no_thread_safety_analysis))
2626

27+
#define __READ_ONCE(x) (*(const volatile __typeof__(x) *)&(x))
28+
#define __WRITE_ONCE(x, val) do { *(volatile __typeof__(x) *)&(x) = (val); } while (0)
29+
2730
// Define the mutex struct.
2831
// Simplified only for test purpose.
2932
struct LOCKABLE Mutex {};
@@ -142,9 +145,19 @@ int main(void) {
142145
(void)(get_value(b_) == 1);
143146
mutex_unlock(foo_.mu_);
144147

148+
a_ = 0; // expected-warning{{writing variable 'a_' requires holding mutex 'foo_.mu_'}}
149+
__WRITE_ONCE(a_, 0); // expected-warning{{writing variable 'a_' requires holding mutex 'foo_.mu_'}}
150+
(void)(a_ == 0); // expected-warning{{reading variable 'a_' requires holding mutex 'foo_.mu_'}}
151+
(void)(__READ_ONCE(a_) == 0); // expected-warning{{reading variable 'a_' requires holding mutex 'foo_.mu_'}}
152+
(void)(*b_ == 0); // expected-warning{{reading the value pointed to by 'b_' requires holding mutex 'foo_.mu_'}}
145153
c_ = 0; // expected-warning{{writing variable 'c_' requires holding any mutex exclusively}}
146154
(void)(*d_ == 0); // expected-warning{{reading the value pointed to by 'd_' requires holding any mutex}}
147155
mutex_exclusive_lock(foo_.mu_);
156+
a_ = 0;
157+
__WRITE_ONCE(a_, 0);
158+
(void)(a_ == 0);
159+
(void)(__READ_ONCE(a_) == 0);
160+
(void)(*b_ == 0);
148161
c_ = 1;
149162
(void)(*d_ == 1);
150163
mutex_unlock(foo_.mu_);

0 commit comments

Comments
 (0)