Skip to content
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

[analyzer] MallocChecker: Recognize std::atomics in smart pointer suppression. #90918

Merged
merged 1 commit into from
May 9, 2024
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
19 changes: 15 additions & 4 deletions clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3447,7 +3447,7 @@ static bool isReferenceCountingPointerDestructor(const CXXDestructorDecl *DD) {
if (N.contains_insensitive("ptr") || N.contains_insensitive("pointer")) {
if (N.contains_insensitive("ref") || N.contains_insensitive("cnt") ||
N.contains_insensitive("intrusive") ||
N.contains_insensitive("shared")) {
N.contains_insensitive("shared") || N.ends_with_insensitive("rc")) {
return true;
}
}
Expand Down Expand Up @@ -3479,13 +3479,24 @@ PathDiagnosticPieceRef MallocBugVisitor::VisitNode(const ExplodedNode *N,
// original reference count is positive, we should not report use-after-frees
// on objects deleted in such destructors. This can probably be improved
// through better shared pointer modeling.
if (ReleaseDestructorLC) {
if (ReleaseDestructorLC && (ReleaseDestructorLC == CurrentLC ||
ReleaseDestructorLC->isParentOf(CurrentLC))) {
if (const auto *AE = dyn_cast<AtomicExpr>(S)) {
// Check for manual use of atomic builtins.
AtomicExpr::AtomicOp Op = AE->getOp();
if (Op == AtomicExpr::AO__c11_atomic_fetch_add ||
Op == AtomicExpr::AO__c11_atomic_fetch_sub) {
if (ReleaseDestructorLC == CurrentLC ||
ReleaseDestructorLC->isParentOf(CurrentLC)) {
BR.markInvalid(getTag(), S);
}
} else if (const auto *CE = dyn_cast<CallExpr>(S)) {
// Check for `std::atomic` and such. This covers both regular method calls
// and operator calls.
if (const auto *MD =
dyn_cast_or_null<CXXMethodDecl>(CE->getDirectCallee())) {
const CXXRecordDecl *RD = MD->getParent();
// A bit wobbly with ".contains()" because it may be like
// "__atomic_base" or something.
if (StringRef(RD->getNameAsString()).contains("atomic")) {
steakhal marked this conversation as resolved.
Show resolved Hide resolved
BR.markInvalid(getTag(), S);
}
}
Expand Down
7 changes: 7 additions & 0 deletions clang/test/Analysis/Inputs/system-header-simulator-cxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,13 @@ template<
iterator end() const { return iterator(val + 1); }
};

template <typename T>
class atomic {
public:
T operator++();
T operator--();
};

namespace execution {
class sequenced_policy {};
}
Expand Down
116 changes: 108 additions & 8 deletions clang/test/Analysis/NewDelete-atomics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef enum memory_order {
memory_order_seq_cst = __ATOMIC_SEQ_CST
} memory_order;

class Obj {
class RawObj {
int RefCnt;

public:
Expand All @@ -37,11 +37,27 @@ class Obj {
void foo();
};

class StdAtomicObj {
std::atomic<int> RefCnt;

public:
int incRef() {
return ++RefCnt;
}

int decRef() {
return --RefCnt;
}

void foo();
};

template <typename T>
class IntrusivePtr {
Obj *Ptr;
T *Ptr;

public:
IntrusivePtr(Obj *Ptr) : Ptr(Ptr) {
IntrusivePtr(T *Ptr) : Ptr(Ptr) {
Ptr->incRef();
}

Expand All @@ -55,22 +71,106 @@ class IntrusivePtr {
delete Ptr;
}

Obj *getPtr() const { return Ptr; } // no-warning
T *getPtr() const { return Ptr; } // no-warning
};

// Also IntrusivePtr but let's dodge name-based heuristics.
template <typename T>
class DifferentlyNamed {
T *Ptr;

public:
DifferentlyNamed(T *Ptr) : Ptr(Ptr) {
Ptr->incRef();
}

DifferentlyNamed(const DifferentlyNamed &Other) : Ptr(Other.Ptr) {
Ptr->incRef();
}

~DifferentlyNamed() {
// We should not take the path on which the object is deleted.
if (Ptr->decRef() == 1)
delete Ptr;
}

T *getPtr() const { return Ptr; } // no-warning
};

void testDestroyLocalRefPtr() {
IntrusivePtr p1(new Obj());
IntrusivePtr<RawObj> p1(new RawObj());
{
IntrusivePtr<RawObj> p2(p1);
}

// p1 still maintains ownership. The object is not deleted.
p1.getPtr()->foo(); // no-warning
}

void testDestroySymbolicRefPtr(const IntrusivePtr<RawObj> &p1) {
{
IntrusivePtr<RawObj> p2(p1);
}

// p1 still maintains ownership. The object is not deleted.
p1.getPtr()->foo(); // no-warning
}

void testDestroyLocalRefPtrWithAtomics() {
IntrusivePtr<StdAtomicObj> p1(new StdAtomicObj());
{
IntrusivePtr<StdAtomicObj> p2(p1);
}

// p1 still maintains ownership. The object is not deleted.
p1.getPtr()->foo(); // no-warning
}


void testDestroyLocalRefPtrWithAtomics(const IntrusivePtr<StdAtomicObj> &p1) {
{
IntrusivePtr p2(p1);
IntrusivePtr<StdAtomicObj> p2(p1);
}

// p1 still maintains ownership. The object is not deleted.
p1.getPtr()->foo(); // no-warning
}

void testDestroySymbolicRefPtr(const IntrusivePtr &p1) {
void testDestroyLocalRefPtrDifferentlyNamed() {
DifferentlyNamed<RawObj> p1(new RawObj());
{
DifferentlyNamed<RawObj> p2(p1);
}

// p1 still maintains ownership. The object is not deleted.
p1.getPtr()->foo(); // no-warning
}

void testDestroySymbolicRefPtrDifferentlyNamed(
const DifferentlyNamed<RawObj> &p1) {
{
DifferentlyNamed<RawObj> p2(p1);
}

// p1 still maintains ownership. The object is not deleted.
p1.getPtr()->foo(); // no-warning
}

void testDestroyLocalRefPtrWithAtomicsDifferentlyNamed() {
DifferentlyNamed<StdAtomicObj> p1(new StdAtomicObj());
{
DifferentlyNamed<StdAtomicObj> p2(p1);
}

// p1 still maintains ownership. The object is not deleted.
p1.getPtr()->foo(); // no-warning
}


void testDestroyLocalRefPtrWithAtomicsDifferentlyNamed(
const DifferentlyNamed<StdAtomicObj> &p1) {
{
IntrusivePtr p2(p1);
DifferentlyNamed<StdAtomicObj> p2(p1);
}

// p1 still maintains ownership. The object is not deleted.
Expand Down
Loading