[cfe-dev] ObjC block code crashes when not enabling optimisations

Zhang via cfe-dev cfe-dev at lists.llvm.org
Wed May 15 03:03:34 PDT 2019


Hi:
I'll admit I'm extraordinarily bad at Objective-C, but is there any reason that this piece of code crashes when not explicitly enabling optimization on macOS?


```
#import <Foundation/Foundation.h>
NSArray* gooo(){
  int val=10;
  return [[NSArray alloc] initWithObjects:^{NSLog(@"%d",val);},^{NSLog(@"%d",val);},nil];
}


int main(int argc, char const *argv[]) {
  for(id blk in gooo()){
    NSLog(@"%@",blk);
  }
  return 0;
}

```


CFE has following version info:


```

 ~/Downloads/clang+llvm-8.0.0-x86_64-apple-darwin/bin/clang -v
 
clang version 8.0.0 (tags/RELEASE_800/final)
 
Target: x86_64-apple-darwin18.2.0
 
Thread model: posix

```


Then when I do:

``clang 123.m -fobjc-arc -O0`` or ``clang 123.m -fobjc-arc -O1``

I get various sort of crash like the following:




```

2019-05-15 10:41:04.873 a.out[12425:668511] <__NSMallocBlock__: 0x7f8c464035a0>

 

[1]    12425 segmentation fault  ./a.out 0O1

```




or 




```

2019-05-15 10:40:59.454 a.out[12412:668167] <__NSMallocBlock__: 0x7f8824e03690>

objc[12412]: Attempt to use unknown class 0x7f8824e00000.

 

[1]    12412 abort      ./a.out 0O1

```







However, when I turn on optimization with O2 or -O3


```
clang 123.m -fobjc-arc -O2
 
2019-05-15 10:41:09.236 a.out[12435:668774] <__NSMallocBlock__: 0x7f8713505290>
 
2019-05-15 10:41:09.236 a.out[12435:668774] <__NSStackBlock__: 0x7ffee67c45e8>

```
Initially I thought it was some issue related to missing objc_blockCopy, however after reading through the LLVM assembly I realized that either case only one of the two blocks used as argument are copied. So at this point I'm very lost
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190515/ee11f84e/attachment.html>


More information about the cfe-dev mailing list