[cfe-dev] ARC + array literal patch feedback request
Jesse Rusak
me at jesserusak.com
Thu Mar 14 18:44:30 PDT 2013
Hi all,
As a way to get my feet wet with Clang, I'm interested in solving what I think is a bug in array literals under ARC. I have a start on a patch, but I'd love for someone to tell me if I'm going in a reasonable direction. The motivating code is:
@implementation AClass {
id ivar;
}
- (void)test {
id local = ivar;
[self aMethod];
@[local, ivar];
}
//...
The bug is that the object stored in `local`, which is retained before the call to `aMethod`, is released before the call to build the array (in -O1). If `aMethod` modifies `ivar`, then this can cause the `local` value to be deallocated before it's passed to the array. It seems that the array literal code gen produces a local buffer that the objects are stored in, and that buffer is passed to +[NSArray arrayWithObjects:count:]. Between the store to that buffer and the call, there's a window where those objects could be released, since the ARC optimizer doesn't know about the value stored in the temporary buffer.
The solution I've put together is to mark that temporary buffer as strong and have a cleanup for it afterwards. (And ditto for dictionary literals and their key buffer.) This seems like it's the "right thing" to do, but will be extra work at runtime. The alternatives seem to be to make ARC understand that the value is still needed, or to require `local` in the above code be marked with objc_precise_lifetime, which seems surprising to me.
I'm attaching a (tiny) patch with my approach and the emitted LLVM before and after. I'd love to hear any feedback about the solution in general and any particulars about the code. If it seems reasonable, I'll work on adjusting the array literal tests so they pass again in the hopes of getting it committed.
Thanks!
Jesse
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: before_and_after.txt
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130314/118db4a8/attachment.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: patch_wip.txt
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130314/118db4a8/attachment-0001.txt>
More information about the cfe-dev
mailing list