Skip to content

Commit

Permalink
Thread safety analysis: Support builtin pointer-to-member operators
Browse files Browse the repository at this point in the history
We consider an access to x.*pm as access of the same kind into x, and
an access to px->*pm as access of the same kind into *px. Previously we
missed reads and writes in the .* case, and operations to the pointed-to
data for ->* (we didn't miss accesses to the pointer itself, because
that requires an LValueToRValue cast that we treat independently).

We added support for overloaded operator->* in D124966.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D129514
  • Loading branch information
aaronpuchert committed Jul 14, 2022
1 parent 18a6ab5 commit bfe63ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
11 changes: 11 additions & 0 deletions clang/lib/Analysis/ThreadSafety.cpp
Expand Up @@ -1679,6 +1679,17 @@ void BuildLockset::checkAccess(const Expr *Exp, AccessKind AK,
return;
}

if (const auto *BO = dyn_cast<BinaryOperator>(Exp)) {
switch (BO->getOpcode()) {
case BO_PtrMemD: // .*
return checkAccess(BO->getLHS(), AK, POK);
case BO_PtrMemI: // ->*
return checkPtAccess(BO->getLHS(), AK, POK);
default:
return;
}
}

if (const auto *AE = dyn_cast<ArraySubscriptExpr>(Exp)) {
checkPtAccess(AE->getLHS(), AK, POK);
return;
Expand Down
5 changes: 5 additions & 0 deletions clang/test/SemaCXX/warn-thread-safety-analysis.cpp
Expand Up @@ -4870,6 +4870,8 @@ class PtGuardedByCorrectnessTest {
int sa[10] GUARDED_BY(mu1);
Cell sc[10] GUARDED_BY(mu1);

static constexpr int Cell::*pa = &Cell::a;

void test1() {
mu1.Lock();
if (a == 0) doSomething(); // OK, we don't dereference.
Expand All @@ -4889,9 +4891,11 @@ class PtGuardedByCorrectnessTest {

if (c->a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'c' requires holding mutex 'mu2'}}
c->a = 0; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}}
c->*pa = 0; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}}

if ((*c).a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'c' requires holding mutex 'mu2'}}
(*c).a = 0; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}}
(*c).*pa = 0; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}}

if (a[0] == 42) doSomething(); // expected-warning {{reading the value pointed to by 'a' requires holding mutex 'mu2'}}
a[0] = 57; // expected-warning {{writing the value pointed to by 'a' requires holding mutex 'mu2' exclusively}}
Expand Down Expand Up @@ -4923,6 +4927,7 @@ class PtGuardedByCorrectnessTest {
sa[0] = 57; // expected-warning {{writing variable 'sa' requires holding mutex 'mu1' exclusively}}
if (sc[0].a == 42) doSomething(); // expected-warning {{reading variable 'sc' requires holding mutex 'mu1'}}
sc[0].a = 57; // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}}
sc[0].*pa = 57; // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}}

if (*sa == 42) doSomething(); // expected-warning {{reading variable 'sa' requires holding mutex 'mu1'}}
*sa = 57; // expected-warning {{writing variable 'sa' requires holding mutex 'mu1' exclusively}}
Expand Down

0 comments on commit bfe63ab

Please sign in to comment.