Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-6560] Runtime crash when accessing protocol type in optimized build #49110

Closed
ffried opened this issue Dec 8, 2017 · 8 comments
Closed
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software optimized only Flag: An issue whose reproduction requires optimized compilation run-time crash Bug → crash: Swift code crashed during execution

Comments

@ffried
Copy link
Contributor

ffried commented Dec 8, 2017

Previous ID SR-6560
Radar rdar://problem/35998785
Original Reporter @ffried
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)
Target: x86_64-apple-macosx10.9

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, OptimizedOnly, RunTimeCrash
Assignee @eeckstein
Priority Medium

md5: d748748ef979f52fd40a549e30d72c54

Issue Description:

Given three modules with the following setup:

Module 1 (Fmwk1):

public protocol MyProtocol: class {
    static func abc()
    init(with str: String) throws
}

public final class MyClass: MyProtocol {
    public enum MyError: Error { case wrongString }

    private let string: String
    public init(with str: String) throws {
        guard str != "ABC" else { throw MyError.wrongString }
        string = str
    }

    public static func abc() { print("Performed ABC!") }
}

Module 2 (Fmwk2):

import Fmwk1

public protocol SomeBaseProtocol {}

public protocol MySubProtocol: MyProtocol, SomeBaseProtocol {}

internal final class MyIntermediateClass<Underlying: MyProtocol>: MySubProtocol {
    private let underlying: Underlying

    convenience init() throws { try self.init(with: "MyString") }
    init(with str: String) throws { underlying = try Underlying(with: str) }

    static func abc() { Underlying.abc() }
}

public protocol CreatorProtocol: class {
    func createMyType() throws -> MySubProtocol
}

internal final class Creator: CreatorProtocol {
    init() {}

    func createMyType() throws -> MySubProtocol {
        return try MyIntermediateClass<MyClass>()
    }
}

public final class Distributor {
    internal static var creator = Creator()
    public init() {}

    public func createMyType() throws -> MySubProtocol {
        return try type(of: self).creator.createMyType()
    }
}

Main:

import Fmwk2

struct Worker {
    private init() {}

    static func work() {
        let myProtocolType: MySubProtocol.Type?
        do {
            myProtocolType = try type(of: Distributor().createMyType())
        } catch {
            myProtocolType = nil
            print("Failed: \(error)")
        }
        myProtocolType?.abc()
        print(myProtocolType.map { "\($0)" } ?? "nil")
    }
}

Worker.work()

The built executable crashes when accessing myProtocolType in main.swift. But only in optimized builds. Debug builds work fine.

I've attached all three files as well as a build.sh to build and link the modules. Passing -no to the build.sh call builds without optimizations. By default it builds with optimizations.

@belkadan
Copy link
Contributor

belkadan commented Dec 8, 2017

@eeckstein, who should take a look?

@eeckstein
Copy link
Member

I'll take it

@ffried
Copy link
Contributor Author

ffried commented Dec 8, 2017

A little note: We had this crash in an iOS app whose setup around this crash is more or less like the one I tried to reproduce here.

But to be honest, it wasn't really easy to reproduce. This setup is the one that most reliably crashed.

@belkadan
Copy link
Contributor

belkadan commented Dec 8, 2017

Thanks for putting in the effort to reduce it!

@eeckstein
Copy link
Member

@swift-ci create

@eeckstein
Copy link
Member

fixed on master in #13382

swift-4.1-branch: #13390

@ffried
Copy link
Contributor Author

ffried commented Dec 13, 2017

Wow, cool![]( That was fast) Thanks @eeckstein!

@eeckstein
Copy link
Member

Thanks for reducing the test case! That was a perfect bug report.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software optimized only Flag: An issue whose reproduction requires optimized compilation run-time crash Bug → crash: Swift code crashed during execution
Projects
None yet
Development

No branches or pull requests

4 participants