Compiler miscompiles @catch blocks in Objective-C, leading to crashes

Originator:ben
Number:rdar://35590717 Date Originated:11/16/2017
Status:Closed Resolved:Duplicate of 12975823
Product:Xcode Product Version:9.1 (9B55)
Classification:Crash/Hang/Data Loss Reproducible:Always
 
Summary:

XCode allows code with a @try/@catch block but missing a return value to compile, e.g.:

```
static id invoke() {
  NSArray *array = @[];

  @try {
    somethingThatThrows();
    return array;
  }
  @catch (NSException *exception) {
    NSLog(@"Caught %@", exception);
  }

  // should fail with a compile error due to missing return stmt, but doesn't
}
```

Longer example: https://gist.github.com/bnham/b682d69dd2ec4944e599d44197d0beb9

This seems to lead to a variety of bad behaviors:

 - Sometimes it segfaults instantly
 - Sometimes it segfaults after running a few times
 - Sometimes the generated code makes no sense, e.g. a call to objc_catch_end is followed immediately by a constant pool, leading to a crash with EXC_BAD_INSTRUCTION
 - Sometimes the same exception is rethrown over and over into the exception handler until the program crashes

I've attached a sample app showing the problem. On my iOS 11.1 (15B93) iPhone 6S using code build by Xcode 9.1 (9B55), the app segfaults after tapping "Test Exception Handling" twice. If add a return statement at the end of the function, it no longer crashes.

Steps to Reproduce:

Run the attached app and tap "Test Exception Handling" twice.

Expected Results:

The compiler should catch the missing return statement at compile time. It should also not generate exception handling code that crashes.

Actual Results:

The compiler doesn't catch the missing return statement at compile time, and the exception handling code crashes.

Version/Build:

iOS 11.1 (15B93) / XCode 9.1 (9B55)

Configuration:

iPhone 6S

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!