Skip to content

Commit

Permalink
[analyzer] [RetainCountChecker] Support 'taggedRetain' and 'taggedRel…
Browse files Browse the repository at this point in the history
…ease'

Differential Revision: https://reviews.llvm.org/D57211

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@352530 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
George Karpenkov committed Jan 31, 2019
1 parent 7f82ceb commit 5eafaa0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/Analysis/RetainSummaryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ RetainSummaryManager::getSummaryForOSObject(const FunctionDecl *FD,
if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
const CXXRecordDecl *Parent = MD->getParent();
if (TrackOSObjects && Parent && isOSObjectSubclass(Parent)) {
if (FName == "release")
if (FName == "release" || FName == "taggedRelease")
return getOSSummaryReleaseRule(FD);

if (FName == "retain")
if (FName == "retain" || FName == "taggedRetain")
return getOSSummaryRetainRule(FD);

if (FName == "free")
Expand Down
4 changes: 4 additions & 0 deletions test/Analysis/os_object_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ struct OSMetaClassBase {

virtual void retain() const;
virtual void release() const;

virtual void taggedRetain(const void * tag = nullptr) const;
virtual void taggedRelease(const void * tag = nullptr) const;

virtual void free();
virtual ~OSMetaClassBase(){};
};
Expand Down
3 changes: 1 addition & 2 deletions test/Analysis/os_smart_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ struct smart_ptr {
}

T * operator->() const {
OSPTR_LOG("Dereference smart_ptr with %p\n", pointer);
return pointer;
}

Expand Down Expand Up @@ -84,6 +83,6 @@ struct smart_ptr {

T *pointer;
};
}
} // namespace os

#endif /* _OS_SMART_POINTER_H */
20 changes: 16 additions & 4 deletions test/Analysis/osobject-retain-release.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,12 +593,12 @@ void test_smart_ptr_uaf() {
// expected-note@-1{{Returning from constructor for 'smart_ptr<OSObject>'}}
// expected-note@os_smart_ptr.h:13{{Taking true branch}}
// expected-note@os_smart_ptr.h:14{{Calling 'smart_ptr::_retain'}}
// expected-note@os_smart_ptr.h:72{{Reference count incremented. The object now has a +2 retain count}}
// expected-note@os_smart_ptr.h:71{{Reference count incremented. The object now has a +2 retain count}}
// expected-note@os_smart_ptr.h:14{{Returning from 'smart_ptr::_retain'}}
} // expected-note{{Calling '~smart_ptr'}}
// expected-note@os_smart_ptr.h:35{{Taking true branch}}
// expected-note@os_smart_ptr.h:36{{Calling 'smart_ptr::_release'}}
// expected-note@os_smart_ptr.h:77{{Reference count decremented. The object now has a +1 retain count}}
// expected-note@os_smart_ptr.h:76{{Reference count decremented. The object now has a +1 retain count}}
// expected-note@os_smart_ptr.h:36{{Returning from 'smart_ptr::_release'}}
// expected-note@-5{{Returning from '~smart_ptr'}}
obj->release(); // expected-note{{Object released}}
Expand All @@ -613,12 +613,12 @@ void test_smart_ptr_leak() {
// expected-note@-1{{Returning from constructor for 'smart_ptr<OSObject>'}}
// expected-note@os_smart_ptr.h:13{{Taking true branch}}
// expected-note@os_smart_ptr.h:14{{Calling 'smart_ptr::_retain'}}
// expected-note@os_smart_ptr.h:72{{Reference count incremented. The object now has a +2 retain count}}
// expected-note@os_smart_ptr.h:71{{Reference count incremented. The object now has a +2 retain count}}
// expected-note@os_smart_ptr.h:14{{Returning from 'smart_ptr::_retain'}}
} // expected-note{{Calling '~smart_ptr'}}
// expected-note@os_smart_ptr.h:35{{Taking true branch}}
// expected-note@os_smart_ptr.h:36{{Calling 'smart_ptr::_release'}}
// expected-note@os_smart_ptr.h:77{{Reference count decremented. The object now has a +1 retain count}}
// expected-note@os_smart_ptr.h:76{{Reference count decremented. The object now has a +1 retain count}}
// expected-note@os_smart_ptr.h:36{{Returning from 'smart_ptr::_release'}}
// expected-note@-5{{Returning from '~smart_ptr'}}
} // expected-warning{{Potential leak of an object stored into 'obj'}}
Expand Down Expand Up @@ -648,3 +648,15 @@ void test_free_on_escaped_object_diagnostics() {
// expected-warning@-1{{'free' called on an object that may be referenced elsewhere}}
}

void test_tagged_retain_no_leak() {
OSObject *obj = new OSObject;
obj->taggedRelease();
}

void test_tagged_retain_no_uaf() {
OSObject *obj = new OSObject;
obj->taggedRetain();
obj->release();
obj->release();
}

0 comments on commit 5eafaa0

Please sign in to comment.