[FN] RetainCount: Use-after-free through @​autorelease.
| Bugzilla Link | 2505 |
| Version | unspecified |
| OS | MacOS X |
| Blocks | llvm/llvm-bugzilla-archive#2665 |
| Reporter | LLVM Bugzilla Contributor |
| CC | @tkremenek,@haoNoQ |
Extended Description
In a non-GC enrivonment, clang static analyzer stops tracking reference counts as soon as it sees an "autorelease" message sent to an object (tracking is stopped in CFRefCount.cpp:879).
This causes clang to miss possible memory leaks and uses after release. Some concrete examples:
-
(void)useAfterRelease { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *str = [[NSString alloc] initWithString:@"some string data"]; // rc(str)=1
[str autorelease]; // rc(str)=1, added to local autorelease pool 'pool' [pool release]; // rc(str)=0, since local autorelease pool 'pool' is released
NSLog(@"%@", str); // 'str' is used after release. Not reported by clang checker. }
-
(void)memoryLeak { NSString *str = [[NSString alloc] initWithString:@"some string data"]; // rc(str)=1
[str retain]; // rc(str)=2 [str autorelease]; // rc(str)=2, and will be 1 after the current event. // 'str' is leaked, since it goes out of scope. Not reported by clang checker. }
Clang should probably track local autorelease pools (which may be nested) and handle 'autorelease' messages just as it handles 'release' messages, but taking into account the knowledge about autorelease pools.
Clang revision used: 52881
mentioned in issue llvm/llvm-bugzilla-archive#2665
We currently already find the leak:
test.m:27:1: warning: Potential leak of an object stored into 'str'
}
^
test.m:22:19: note: Method returns an instance of NSString with a +1 retain count
NSString *str = [[NSString alloc] initWithString:@"some string data"]; // rc(str)=1
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.m:24:3: note: Reference count incremented. The object now has a +2 retain count
[str retain]; // rc(str)=2
^~~~~~~~~~~~
test.m:25:3: note: Object autoreleased
[str autorelease]; // rc(str)=2, and will be 1 after the current event.
^~~~~~~~~~~~~~~~~
test.m:27:1: note: Object leaked: object allocated and stored into 'str' is not
referenced later in this execution path and has a retain count of +1
}
^
We still don't find the use-after-free bug, so i'll keep this report to track the use-after-free false negative. We also don't find it with the modern @autorelease syntax, even though it does make the use-after-free harder to trigger, eg.:
-
(void)useAfterReleaseModernized { NSString *escape; @autoreleasepool { NSString *str = [[NSString alloc] initWithString:@"some string data"]; // rc(str)=1 escape = str; [str autorelease]; // rc(str)=1, added to local autorelease pool 'pool' }
NSLog(@"%@", escape); // 'str' is used after release. }
Tracked internally as rdar://problem/46924233
Thanks Nikita.
The crashes are now fixed:
http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20090511/017194.html
I tried the CFRef checker in clang rev #71635, and it seems to catch all those incorrect uses of autorelease nicely. It still doesn't catch those cases when objects are released because the autorelease pool there're in is released, but this is expected.
I did find one weird crash, though. I'm not sure if I somehow compiled clang incorrectly or if this is a real bug. Maybe you could check if you can reproduce it?
Clang crashes when I try to analyze the following class:
@interface TheClass : NSObject @end @implementation TheClass
-
(void)m1 { NSString *s = [[NSString alloc] init];
[s retain]; [s autorelease]; }
-
(void)m2 { NSString *s = [[[NSString alloc] init] autorelease];
[s retain]; }
-
(void)m3 { NSString *s = [[[NSString alloc] init] autorelease];
[s retain]; [s autorelease]; }
-
(void)m4 { NSString *s = [[NSString alloc] init];
[s retain]; }
-
(void)m5 { NSString *s = [[NSString alloc] init];
[s autorelease]; } @end
The output is included below. Note that only methods m1 and m2 cause the crash. There's no crashes if only methods m3, m4 and m5 are included in the class.
Output:
Assertion failed: (isa
- /Users/nzhuk/Desktop/AutoreleasePoolTest/TheClass.m:23:1: current parser token '-'
The line 23 of TheClass.m looks like this:
- (void)m2
Sounds great. I'll give it a try today and attach the results to this PR.
While there isn't complete support yet for tracking the lifetime of autorelease pools, the analyzer can now flag many errors related to not using autorelease pools correctly (e.g., sending -autorelease to objects you don't own, over-releasing an object, etc.).
Nikita: Can you give this a try using the latest checker release? I'd like to shake out any obvious bugs.
Having the retain/release checker maintain a local stack of autorelease pools is not that difficult, and I agree would make an excellent enhancement to the tool. These are great test cases that could go directly into the Clang test suite.
assigned to @tkremenek