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-14071] Enum case as protocol witness uses incorrect ARC semantics for payload #56460

Closed
kabiroberai opened this issue Jan 20, 2021 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@kabiroberai
Copy link
Contributor

Previous ID SR-14071
Radar rdar://problem/73406514
Original Reporter @kabiroberai
Type Bug
Status Resolved
Resolution Duplicate
Environment

Operating System: macOS Big Sur 11.1 (20C69)

Toolchain: Xcode 12.3 (12C33)

Target Platform: macOS

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: ec3380d9c01408ad14573d2d139a63ff

duplicates:

  • SR-14041 Protocol allows to create a nested enum without explicit indirect keyword resulting in a runtime crash

Issue Description:

When using an (enum case with a reference-type associated value) as a protocol witness, the Swift compiler appears to be using incorrect ARC semantics while passing the associated value argument, resulting in an over-release or memory leak of the passed object.

For example:

class MyClass {}

protocol MyProtocol {
    static func value(_: MyClass) -> Self
}

enum MyEnum: MyProtocol {
    case value(MyClass)
}

func doCrash<P: MyProtocol>(_ type: P.Type) {
    // malloc: Double free of object 0x...
    _ = P.value(MyClass())
}

doCrash(MyEnum.self)

Furthermore, using a regular static method as a protocol witness fixes the issue:

class MyClass {}

protocol MyProtocol {
    static func value(_: MyClass) -> Self
}

enum MyEnum: MyProtocol {
    case _value(MyClass)
    static func value(_ obj: MyClass) -> MyEnum {
        ._value(obj)
    }
}

func doCrash<P: MyProtocol>(_ type: P.Type) {
    // No crash!
    _ = P.value(MyClass())
}

doCrash(MyEnum.self)
@typesanitizer
Copy link

@swift-ci create

@kabiroberai
Copy link
Contributor Author

Just tested this out using Xcode 12.4 as well as development snapshot 2021-02-02-a (Swift 5.4) and I can confirm that the issue is still reproducible.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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
Projects
None yet
Development

No branches or pull requests

2 participants