This is an archive of the discontinued LLVM Phabricator instance.

[analyzer] RetainCountChecker: The callback in dispatch_data_create() doesn't free the return symbol.
ClosedPublic

Authored by NoQ on Dec 5 2016, 5:42 AM.

Details

Summary

The hack that was previously applied to CGBitmapContextCreateWithData() is now extended to another function, dispatch_data_create().

This function accepts a callback block, and the analyzer erroneously assumes that this callback may free up some data, which results in being unable to detect a leak of the dispatch_data_t object. In fact, the callback only frees the buffer that is passed into the function, but the returned object should be released separately unless in ARC mode.

Diff Detail

Repository
rL LLVM

Event Timeline

NoQ updated this revision to Diff 80258.Dec 5 2016, 5:42 AM
NoQ retitled this revision from to [analyzer] RetainCountChecker: The callback in dispatch_data_create() doesn't free the return symbol..
NoQ updated this object.
NoQ added reviewers: zaks.anna, dcoughlin.
NoQ added a subscriber: cfe-commits.
NoQ updated this revision to Diff 80264.Dec 5 2016, 7:00 AM

Update the comments in the surrounding code accordingly.

zaks.anna added inline comments.Dec 5 2016, 9:17 AM
test/Analysis/dispatch-data-leak.m
50 ↗(On Diff #80264)

I think we should remove testing of malloc -it does not add anything here and is a bit confusing since one might think that malloc_"but is related to the dispatch test case.

dcoughlin edited edge metadata.Dec 5 2016, 9:50 AM

Looks good to me!

To ease the future maintenance burden I would suggest moving the test into 'retain-release-arc.m' unless it really needs to be in its own separate file.

test/Analysis/dispatch-data-leak.m
59 ↗(On Diff #80264)

I think it would also be good to also add tests showing that we don't warn when the object is properly released. It would be good to test both ObjC -release (under MRR) and the dispatch_release() macro (under MRR and ARC).

NoQ updated this revision to Diff 80432.Dec 6 2016, 8:54 AM
NoQ retitled this revision from [analyzer] RetainCountChecker: The callback in dispatch_data_create() doesn't free the return symbol. to [analyzer] RetainCountChecker: Improve support for libdispatch APIs..
NoQ updated this object.
NoQ edited edge metadata.

Add more tests.

Not sure we need to stay merged with retain-release-arc.m, as we're not really reusing many declarations across these files.

As well, handle the case when dispatch_retain and dispatch_release are regular functions.

NoQ marked 2 inline comments as done.Dec 6 2016, 8:55 AM
In D27409#614601, @NoQ wrote:

Not sure we need to stay merged with retain-release-arc.m, as we're not really reusing many declarations across these files.

I find it more helpful to organize tests around functionality ('retain count checker') than around bugs ('dispatch data leak', 'PRxyzab.c'). Organizing around functionality helps future contributors and maintainers know where to add a new test and where to look for existing tests. In my view this critical for developers who are new to the project or who are learning a new subsystem. It also typically reduces the number of 'clang' RUN lines, which have a cost in maintenance and running time.

The analyzer currently doesn't do any checking for dispatch retain/release APIs in C. It similarly doesn't do any checking in Objective-C when OS_OBJECT_USE_OBJC is 0 (and thus the dispatch types are defined to their C-struct versions). This happens when the user explicitly sets -DOS_OBJECT_USE_OBJC=0 (to safely share dispatch between ARC and non-ARC projects) and also under certain combinations of deployment targets and architectures where the runtime support for the feature is not present (for example, earlier than iOS 6.0).

My feeling is that for this patch it is fine to continue the policy of not diagnosing dispatch leaks/overreleases when OS_OBJECT_USE_OBJC is 0. Adding this support to the retain count checker is a larger project and in my opinion it should be done separately. To that end, I would recommend removing the summary creation for dispatch_retain/dispatch_release().

That said, people do use these APIs in C (and with -DOS_OBJECT_USE_OBJC=0 in ObjC), so it would be great test to make sure that this patch don't introduce any new false positives in these situations. I think it important to suck in enough of the header typedefs used when -DOS_OBJECT_USE_OBJC=0 to write these tests to make sure we don't regress when using the C-based APIs.

test/Analysis/dispatch-data-leak.m
4 ↗(On Diff #80432)

Looks like there is a typo here ('MARCOS' vs. 'MACROS')?

NoQ updated this revision to Diff 80606.Dec 7 2016, 8:57 AM
NoQ retitled this revision from [analyzer] RetainCountChecker: Improve support for libdispatch APIs. to [analyzer] RetainCountChecker: The callback in dispatch_data_create() doesn't free the return symbol..
NoQ updated this object.

Revert the attempt to support dispatch_{retain,release}. Disable the relevant tests.

Move tests to retain-release-arc.m.

NoQ marked an inline comment as done.Dec 7 2016, 8:58 AM
dcoughlin accepted this revision.Dec 7 2016, 4:57 PM
dcoughlin edited edge metadata.

Ship it! :-)

This revision is now accepted and ready to land.Dec 7 2016, 4:57 PM
This revision was automatically updated to reflect the committed changes.