rules_swift icon indicating copy to clipboard operation
rules_swift copied to clipboard

Crash in release build using new Swift 5.7 features.

Open mikhailmulyar opened this issue 3 years ago • 5 comments

Here are sample app which crashes only in release build using Bazel (-c opt). Everything works fine in debug build. In optimised build it crashes with EXC_BAD_ACCESS or dyld: missing symbol called. Actually I though it is Xcode beta issue, so I've tried to reproduce this issue in Xcode build with different combinations of optimisations -O/-Osize and compilation mode singlefile/wholemodule. But it is working fine in Xcode build in all cases.

App crashes when accessing parametrised protocol variable in another protocol (any Protocol<Value>). It only happens in iOS 15 and lower. It works fine in all cases in iOS 16.

import SwiftUI

protocol SomeProto: AnyObject {
    var proto: any SomeGenericProto<Int> { get set }
    func getElements() -> [Int]
}

class SomeImpl: SomeProto {
    var proto: any SomeGenericProto<Int> = SomeGenericImpl(elements: [1,2,3])
    
    func getElements() -> [Int] {
        proto.getElements()
    }
}

protocol SomeGenericProto<Element> {
    associatedtype Element

    func getElements() -> [Element]
}

final class SomeGenericImpl<Element>: SomeGenericProto {
    
    private let elements: [Element]

    init(elements: [Element]) {
        self.elements = elements
    }

    func getElements() -> [Element] {
        elements
    }
}

struct ContentView: View {

    var someValue: any SomeProto = SomeImpl()

    var body: some View {
        VStack {
            Text("Hello, world!" + "\(someValue.getElements().last ?? 0)")
        }
        .padding()
    }
}

ios-app.zip

mikhailmulyar avatar Aug 23 '22 07:08 mikhailmulyar

Untitled

I don't see an issue running your sample project on a 15.5 simulator, can you be more specific on how to repro? I'm testing with Xcode 14 beta 5

keith avatar Aug 23 '22 17:08 keith

Here is what I'm doing.

  1. Do bazel build //ios-app:ios-app -c opt
  2. Go to bazel-out/applebin_ios-ios_sim_arm64-opt.../bin/ios-app/ios-app_archive-root/Payload/
  3. Do xcrun simctl install booted ios-app.app or just drag and drop file to simulator app Resulting installed app crashes on start

mikhailmulyar avatar Aug 23 '22 20:08 mikhailmulyar

I copied your code into a fresh Xcode project and also made it crash:

testinxcode.zip

If you can verify this also crashes, then it sounds like we should submit a bug report upstream.

keith avatar Aug 23 '22 21:08 keith

Yes you are right. It is crashing. I somehow missed it out. But for me its crashing only with optimisation level -Osize and working fine with -O in Xcode. Anyway need to report this one further. However build with Bazel it seems crashes in all cases except debug. Why it is crashing without --features=swift.opt_uses_osize set?

mikhailmulyar avatar Aug 24 '22 07:08 mikhailmulyar

Yea I thought that was interesting too, I didn't debug further. You can try to reduce the other various flags that bazel is passing to sidestep it. I was thinking at some point it had something to do with -dead_strip, which is passed with -c opt, but I was able to repro with -c dbg --swiftcopt=-O as well.

keith avatar Aug 24 '22 16:08 keith