[LLVMbugs] [Bug 17778] New: ARC optimizer prematurely releases result of ternary operator when assigning to __attribute__(objc_precise_lifetime) variable

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Fri Nov 1 17:21:11 PDT 2013


http://llvm.org/bugs/show_bug.cgi?id=17778

            Bug ID: 17778
           Summary: ARC optimizer prematurely releases result of ternary
                    operator when assigning to
                    __attribute__(objc_precise_lifetime) variable
           Product: clang
           Version: 3.3
          Hardware: All
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: bgertzfield at gmail.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

Created attachment 11474
  --> http://llvm.org/bugs/attachment.cgi?id=11474&action=edit
Repro case for bug

These two statements perform identically with -O0:

__attribute__((objc_precise_lifetime)) Scoped *scoped1 = [[Scoped alloc] init];
__attribute__((objc_precise_lifetime)) Scoped *scoped2 = DoSomeCheck() ?
[[Scoped alloc] init] : nil;

However, with -O1 or higher, the second statement releases the object before
assigning it to 'scoped2'. Attached test case reproduces issue:

2013-11-01 16:53:13.479 testARC-opt[85825:707] -[Scoped init] <Scoped:
0x7ff758c081a0>
2013-11-01 16:53:13.484 testARC-opt[85825:707] At the end of first block
2013-11-01 16:53:13.485 testARC-opt[85825:707] -[Scoped dealloc] <Scoped:
0x7ff758c081a0>

2013-11-01 16:53:13.486 testARC-opt[85825:707] -[Scoped init] <Scoped:
0x7ff759b00470>
2013-11-01 16:53:13.486 testARC-opt[85825:707] -[Scoped dealloc] <Scoped:
0x7ff759b00470>
2013-11-01 16:53:13.487 testARC-opt[85825:707] At the end of second block

Interestingly, the following statements behave the same as the first statement,
suggesting it's not just the ternary operator at fault:

Scoped *scopeMe = DoSomeCheck() ? [[Scoped alloc] init] : nil;
__attribute__((objc_precise_lifetime)) Scoped *scoped3 = scopeMe;


Reproduced in Clang 3.3 as shipped with Xcode 5.0 (5A1412).

% clang -v
Apple LLVM version 5.0 (clang-500.2.75) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.4.0
Thread model: posix

% cat testARC.m
#import <Foundation/Foundation.h>

static BOOL gSomeValue;

static BOOL DoSomeCheck(void)
{
  return gSomeValue;
}

@interface Scoped : NSObject
@end

@implementation Scoped

- (instancetype)init
{
  self = [super init];
  NSLog(@"%s %@", __func__, self);
  return self;
}

- (void)dealloc
{
  NSLog(@"%s %@", __func__, self);
}

@end

int main(int argc, char **argv)
{
  gSomeValue = YES;

  {
    __attribute__((objc_precise_lifetime)) Scoped *scoped = [[Scoped alloc]
init];
    NSLog(@"At the end of first block");
  }

  {
    __attribute__((objc_precise_lifetime)) Scoped *scoped = DoSomeCheck() ?
[[Scoped alloc] init] : nil;
    NSLog(@"At the end of second block");
  }

  {
    Scoped *scopeMe = DoSomeCheck() ? [[Scoped alloc] init] : nil;
    __attribute__((objc_precise_lifetime)) Scoped *scoped = scopeMe;
    NSLog(@"At the end of third block");
  }
}

% clang -fobjc-arc -O0 -o testARC-dbg ./testARC.m
% clang -fobjc-arc -Os -o testARC-opt ./testARC.m

% ./testARC-dbg                                  
2013-11-01 16:53:27.387 testARC-dbg[85843:707] -[Scoped init] <Scoped:
0x7f9102c081c0>
2013-11-01 16:53:27.389 testARC-dbg[85843:707] At the end of first block
2013-11-01 16:53:27.389 testARC-dbg[85843:707] -[Scoped dealloc] <Scoped:
0x7f9102c081c0>
2013-11-01 16:53:27.390 testARC-dbg[85843:707] -[Scoped init] <Scoped:
0x7f9103b009d0>
2013-11-01 16:53:27.390 testARC-dbg[85843:707] At the end of second block
2013-11-01 16:53:27.390 testARC-dbg[85843:707] -[Scoped dealloc] <Scoped:
0x7f9103b009d0>
2013-11-01 16:53:27.391 testARC-dbg[85843:707] -[Scoped init] <Scoped:
0x7f9102c02b40>
2013-11-01 16:53:27.408 testARC-dbg[85843:707] At the end of third block
2013-11-01 16:53:27.412 testARC-dbg[85843:707] -[Scoped dealloc] <Scoped:
0x7f9102c02b40>

% ./testARC-opt
2013-11-01 16:53:13.479 testARC-opt[85825:707] -[Scoped init] <Scoped:
0x7ff758c081a0>
2013-11-01 16:53:13.484 testARC-opt[85825:707] At the end of first block
2013-11-01 16:53:13.485 testARC-opt[85825:707] -[Scoped dealloc] <Scoped:
0x7ff758c081a0>
2013-11-01 16:53:13.486 testARC-opt[85825:707] -[Scoped init] <Scoped:
0x7ff759b00470>
2013-11-01 16:53:13.486 testARC-opt[85825:707] -[Scoped dealloc] <Scoped:
0x7ff759b00470>
2013-11-01 16:53:13.487 testARC-opt[85825:707] At the end of second block
2013-11-01 16:53:13.489 testARC-opt[85825:707] -[Scoped init] <Scoped:
0x7ff759900140>
2013-11-01 16:53:13.489 testARC-opt[85825:707] At the end of third block
2013-11-01 16:53:13.490 testARC-opt[85825:707] -[Scoped dealloc] <Scoped:
0x7ff759900140>

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20131102/8f6ae8b4/attachment.html>


More information about the llvm-bugs mailing list