False positive CF leak involving 'autorelease' wrapper
| Bugzilla Link | 2580 |
| Resolution | WORKSFORME |
| Resolved on | Dec 23, 2018 06:50 |
| Version | unspecified |
| OS | MacOS X |
| Reporter | LLVM Bugzilla Contributor |
| CC | @tkremenek,@haoNoQ |
Extended Description
Run this against one of my projects, specifically RegexKitLite (http://regexkit.sourceforge.net/) (this is wrt/ the 'Lite' ICU version).
Since this is meant to be used by other people embedding the source in to their project, I have to try to accommodate their choice of Retain/Release or GC. This makes things a bit tricky...
static id rkl_CFAutorelease (CFTypeRef obj) { return([(id)obj autorelease]); }
To mange the release obligation of CF objects in RR mode, the above function is called with the CF object as the argument.
clang is picking this up as a leak. Certainly a good 'odd-ball' corner case.. :) Did not check the GC flow. You can download the RegexKitLite source from the sourceforge site (the lite version is just a header and a single .m file)
Found the source code at https://github.com/wezm/RegexKitLite but could not reproduce the bug.
Generally, inter-procedural analysis has been implemented, and additionally the retain count checker has been taught to explicitly detect various wrappers around retain/release/autorelease operations. This bug doesn't require cross-translation-unit analysis because the project consists of only one tranlation unit.
So most likely this shouldn't be an issue anymore.
It's slightly more complicated than the one line of code given in the bug report. If OBJC_GC is defined, the actual RegexKitLite code does a one time check at run time to determine whether or not garbage collection is present and active, and then determines which rkl_CFAutorelease method is appropriate (via a trampoline). If GC is not present and not active, there is no need to call (NS|CF)MakeCollectable(), only [obj autorelease]. If GC is present AND active, then the only thing required is CFMakeCollectable to 'autorelease' the CF object.
If OBJC_GC is not defined then the source is being compiled without -fobjc-gc (et al), therefore GC will not, can not, ever be active so no call to (CF|NS)MakeCollectable() is ever required. When you are creating a lot of objects (as can often be the case when doing regex ops on strings), this tiny overhead adds up in a measurable way.
This is a case of 'excessive optimization', but it actually pays off in this particular case. A 'while loop' that processes a large text file contained in a NSString using regexes has a tendency to create an awful lot of intermediate string objects as a by product of those regex matches.
Hey, something just popped in to my head: Maybe add something to clang that allows you to mark a function/method as 'autoreleasing', similar to attribute((malloc)). This eliminates the need for complicated analysis to automatically grok that an object is being leaked, instead it can rely on the programmer to hint that 'an object passed via rkl_CFAutorelease, that was marked with attribute((objc_autoreleases)), is autoreleased for leak detection purposes.
static id rkl_CFAutorelease (CFTypeRef obj) { return([(id)obj autorelease]); }
This code is incorrect. If you are writing dual mode code, it should be written as
static id rkl_CFAutorelease (CFTypeRef obj) { return([(NSMakeCollectable(obj) autorelease]); }
Ted,
I figured as much, but instead of assuming it was a known limitation, I'd file a bug as requested. Some times it's just useful to have a collection of real world code examples, especially when you go to implement the functionality and need to test it.
Absolutely. I was only saying that I was aware of the problem just so that you knew it was on my radar, but having another test case and bug report is a good thing.
Since this piece of code (regexkitlite) is meant to be two files, the .m and .h, that you can just drop in to your project and gain objc regex functionality, I have to accommodate at the source level whatever memory management style the programmer has chosen.
The first decision is made by the OBJC_GC preprocessor define. If it's not enabled, it's safe to just use the simplest means available ([(id)cfObject autorelease]).
If GC is enabled at compile time, then rkl_CFAutorelease is set to a function pointer that's used as a trampoline. It's initialized to point to the 'first time called' function, which essentially does a [NSGarbageCollector defaultCollector] == NULL check. If collecting IS enabled, then it rewrites the function pointer to point to a function that does CFMakeCollectable only. If collecting is not enabled, then the function pointer is rewritten to the generic [(id)cfObject autorelease] function.
Neat. This is a more elaborate case than I had imagined, so I'm glad you filed this bug report. It's not a surprise that people will resort to lots of tricks so support dual GC/non-GC modes.
BTW, you can close the bug if you'd like, I opened it mostly to be thorough.
It's very much worth keeping open. I'll close it when we support handling this example automatically, or decide to never support it.
Thanks so much for filing it!
Ted,
I figured as much, but instead of assuming it was a known limitation, I'd file a bug as requested. Some times it's just useful to have a collection of real world code examples, especially when you go to implement the functionality and need to test it.
WRT/ GC leaks, the code snippet I posted was only 'half the story, specifically the #ifndef OBJC_GC side of things.
Since this piece of code (regexkitlite) is meant to be two files, the .m and .h, that you can just drop in to your project and gain objc regex functionality, I have to accommodate at the source level whatever memory management style the programmer has chosen.
The first decision is made by the OBJC_GC preprocessor define. If it's not enabled, it's safe to just use the simplest means available ([(id)cfObject autorelease]).
If GC is enabled at compile time, then rkl_CFAutorelease is set to a function pointer that's used as a trampoline. It's initialized to point to the 'first time called' function, which essentially does a [NSGarbageCollector defaultCollector] == NULL check. If collecting IS enabled, then it rewrites the function pointer to point to a function that does CFMakeCollectable only. If collecting is not enabled, then the function pointer is rewritten to the generic [(id)cfObject autorelease] function.
This (hopefully!) allows for invisible and correct operation in all three modes of operation, RR only, RR || GC, or GC only and requires no intervention of the programmers part to select the right way with the largest OS version support possible.
BTW, you can close the bug if you'd like, I opened it mostly to be thorough.
This problem comes from the analyzer's current lack of inter-procedural analysis, which misses wrapper functions to calls to CFRelease or [... autorelease]. We're planning on bringing inter-procedural analysis online over time.
We've also thought about adding GCC-style attributes that programmers can attach to the function prototypes of wrapper functions to tell the checker more information about a function without requiring that it can analyze the function's implementation or that its implementation is available (e.g., this function should be treated like an "autorelease"). We can wrap these attributes in macros that expand to nothing when you aren't using clang (e.g., when compiling with gcc). Even with inter-procedural analysis, attributes like these are useful in cases where it cannot see all the code (i.e., some function implementations are in libraries).
Incidentally, the code example you provided may actually leak memory in GC mode. While Objective-C objects that derive from NSObject are actually automatically garbage collected, Core Foundation objects are not. In GC mode, the 'autorelease' message is a no-op, so if you are expecting it to release a CF object in GC mode it will actually get leaked. If you use GC with Core Foundation objects, make sure you use CFMakeCollectable with them at some point, or otherwise be prepared to maintain their retain/release counts yourself.
assigned to @tkremenek