crash with simple simd code in Swift

I'm struggling to get an app working on Catalina 10.15.2 using Xcode 11.3.1, Swift 5. I could manage to boil down an example of the crash for Xcode Playgrounds:


import Cocoa
import simd

var viewMatrix = double4x4(1)
var modelMatrix = double4x4(1)

let modelViewMatrix = viewMatrix * modelMatrix

var m = modelViewMatrix.transpose.inverse

print(m)


I get an


error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=EXC_I386_GPFLT).


like in the full app. Any idea what's wrong and why the code it crashing? App code works fine running natively on macOS 10.14.6 and iOS 13.


Thanks!

Replies

Yes, surprising bug:


In OSX 10.14, playground OSX, this works

import simd
 
var viewMatrix = double4x4(1)
var modelMatrix = double4x4(1)
let modelViewMatrix = viewMatrix * modelMatrix
var m = modelViewMatrix.transpose.inverse

print(m)


simd_double4x4([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]])


The same crashes on Catalina.


But this works:


import simd
 
var viewMatrix = double4x4(1)
var modelMatrix = double4x4(1)
let modelViewMatrix = viewMatrix * modelMatrix
var m = modelViewMatrix.transpose.inverse
 
print(viewMatrix)
print(modelViewMatrix)
print(m)


simd_double4x4([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]])

simd_double4x4([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]])

simd_double4x4([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]])



Removing any of line 8 or 9 causes crash


This code crashes in an OSX App with EXC_BAD_ACCESS (code= EXC_i386_GPFLT) error.

Same error without transpose, just inverse (logic).the following causes crash in app but not in playground:


import simd

var viewMatrix = double4x4(1)
var m = viewMatrix.inverse

print(m)


I replaced double4x4 by float4x4, it works in code and in playground, even without additional print.


Tested with 3x3, same problem.


There is definitely a bug here. Should file a bug report (I'll do it).


EDITED: bug filed. FB7534934

Thanks for confirming it! I filed a radar: FB7519739

Great, let's wait and see.

Maybe you can close the thread, there is not workaround to expect (except using float4x4)


EDITED

Previous tests were on 11.3.

Tested with Xcode 11.3.1 (11C504). Still crashes in app.

Tested on XCode 10.2ß2, crashes also in app.

Exact, works in iOS13 simulator on macOS 10.14.6.

Works also in simulator on Catalina.

Note: you don't import Cocoa in an iOS App. 😉

Any idea what's wrong and why the code it crashing?


I do suspect a bug in the library that handles double operations (the problem is visibly there since a long time, but not problem with float).

Any idea what's wrong and why the code it crashing?

The crash seems to be caused by an alignment issue. There is disagreement between the compiler and the system as to how these values should be aligned, and alignment is critical for the SIMD instructions that actually back all of this stuff.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

So, that's a bug in library ? Compiler ?


What is really suprising is that adding print statement eliminates the crash in some cases (I detailed in bug report FB7534934).

So, that's a bug in library ? Compiler ?

The jury is still out on that one (-:

What is really suprising is that adding

print
statement eliminates the crash in some cases

This is probably because adding a

print
statement causes the compiler to lay out the stack differently, which changes the alignment of the value passed to the system.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I was afraid of such an answer. Since I only experience these crashes when trying to invert matrices, I might write my own function and hope for the best. But a timely fix in the OS or compiler would of course be much more welcome. If one could only get an estimate for the time frame ... 😉 Thanks for the help!

Surprisingly, I cannot reproduce the bug anymaore, neither on Mojave nor Catalina.


Could you test again ?

Nothing has changed for me using Xcode 11.3.1 and macOS 10.15.2. Did you install some of the 10.15.3 betas?

No, I got the same environment. Tested both on Mojave and Catalina, app and playground.

May be that just illustrate what Quinn was saying.

But I needed to send a code for bug report, useless.


Did you attach code in your own bug report ?

May be that just illustrate what Quinn was saying.

Right. Because this is a stack alignment issue, even tiny perturbations in your code or tools can cause it to come and go.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Yes, the code I posted initally is also attached to the bug report. This issue only ever occured to me on Catalina, all iOS platforms and Mojave are unaffected.

After replacing every occurence of the double4x4 inverse function with my own function, the App works correctly. My bet is on the double4x4 inverse function to be incorrect.


The most simple replacement that works for me is just an encapsulation of the inverse function call by another function:


extension double4x4 {
  public var inverseReplacement: double4x4 {
    return self.inverse
  }
}


However, I would not ship an app with this bug.