mattgallagher / CwlPreconditionTesting

A Mach exception handler that allows Swift precondition failures to be caught and tested.
ISC License
175 stars 46 forks source link

Under Xcode 9.3 catchBadInstruction no longer handles FatalError() #12

Closed drekka closed 6 years ago

drekka commented 6 years ago

You've probably heard this, but I thought I'd log it anyway. Under Xcode 9.3, catchBadInstruction() is no longer catching Swift FatalError()s. This is effecting any tests which are attempting to deal with code that executes this command and frameworks which support catching fatals (Nimble's throwAssertion()).

The only known workaround is to turn off debugging the executable, however this kills debugging tests and is also the default setting.

So I'm hoping you can find some magic to work around the problem. I've included a simple test project which shows the problem. Just run the single unit test in Xcode 9.3 and you'll see the debugger halt on the FatalError() instead of returning to the unit test.

test.zip

drekka commented 6 years ago

Just did a test under Xcode Version 9.4 beta (9Q1019a) and it's still not working so I'm assuming at this stage that this is not a one off 9.3 issue.

mattgallagher commented 6 years ago

It's possible this is a known issue where catchBadInstruction is working as expected but lldb breaks at the error. I'll try to look at this later this week but I suspect the only fix is to disable debugging when you're running these tests.

drekka commented 6 years ago

Yep. Figured that might be the problem. I'm looking into alternatives to using fatalError() which is where this all started. Thanks

natechan commented 6 years ago

You can avoid breaking to the debugger by setting the global _swift_reportFatalErrorsToDebugger ( https://github.com/apple/swift/blob/master/include/swift/Runtime/Debug.h#L218 ) to false--you'll need to declare it extern. One place you might consider is in an override of class func setUp() in a subclass of XCTestCase that all your tests inherit from.

mattgallagher commented 6 years ago

Thanks for the heads-up, @natechan, that's exactly what's needed. I wouldn't ordinarily encourage using underscore prefixed features from the Swift runtime but since this is strictly for testing purposes (NOT FOR DEPLOYED CODE), I've included it in the setUp function of the tests so tests can proceed normally.