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-8837] A strange runtime crash #51344

Closed
swift-ci opened this issue Sep 25, 2018 · 5 comments
Closed

[SR-8837] A strange runtime crash #51344

swift-ci opened this issue Sep 25, 2018 · 5 comments

Comments

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Sep 25, 2018

Previous ID SR-8837
Radar rdar://problem/44762114
Original Reporter linqingmo (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Xcode 10.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 4.2Regression, OptimizedOnly, RunTimeCrash
Assignee None
Priority Medium

md5: f34e2266d5f786b4ed1630b2247b5261

Issue Description:

I updated my iOS app from Swift 4.1 to Swift 4.2, then a very strange runtime crash appeared. It can compile and it crashes at runtime (only release mode). Even same code will lead to different crash.

// HVApi.framework
// Message.swift
open class Message {
    public required init() {}
    open func send() {
        
    }
}

// HVNetwork.framework
// Api.swift
open class Api: Message {
    public private(set) var url: String
    
    public init(url: String) {
        self.url = url
        super.init()
    }
    
    public required init() {
        fatalError("init() has not been implemented")
    }
}
// DecodableApi.swift
open class DecodableApi<Value: Decodable>: Api {
    private var _result: Value?
}

// HVBasic.framework
// BasicApi.swift
import HVNetwork
open class BasicApi<Value: Decodable>: DecodableApi<Value> {
    public let fun: String
    public let op: String
    
    public init(url: String, fun: String, op: String) {
        self.fun = fun
        self.op = op
        super.init(url: url)
    }
    
    public required init() {
        fatalError("init() has not been implemented")
    }
}
// Salt.swift
public struct Salt: Decodable {
    public let salt: String?
    
    open class Api: DecodableApi<Salt> {
        public private(set) var salt: String?
        public override init(url: String) {
            super.init(url: url)
        }
        
        public required init() {
            fatalError("init() has not been implemented")
        }
    }
}

// app target
// BasicApi.swift
import HVBasic

class BasicApi<Value: Decodable>: HVBasic.BasicApi<Value> {
    init(fun: String, op: String) {
        super.init(url: "", fun: fun, op: op)
    }
    
    required init() {
        fatalError("init() has not been implemented")
    }
}
// News.swift
import HVNetwork

struct News {
    let title: String
    let link: String?
    let pubDate: Date
    let desc: String?
    
    final class Api: HVNetwork.Api {
        required init() {
            super.init(url: "https://www.test.com")
        }
    }
}
// ViewController.swift
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let api = News.Api()
        api.send()
    }
}

It will crash at release mode. And the error is strange. It will crash at Salt.Api.init(url:). No idea why, since News.Api has nothing to do with Salt.Api. The stranger thing is that if I delete app's BasicApi.swift (Useless code in this case, isn't it?), it will not crash.

Same code, but different crash error.

// Test.framework
// Message.swift
open class Message {
    public required init() {}
    open func send() {
        
    }
}
// Api.swift
open class Api: Message {
    public private(set) var url: String
    
    public init(url: String) {
        self.url = url
        super.init()
    }
    
    public required init() {
        fatalError("init() has not been implemented")
    }
}
// DecodableApi.swift
open class DecodableApi<Value: Decodable>: Api {
    private var _result: Value?
}
// BasicApi.swift
open class BasicApi<Value: Decodable>: DecodableApi<Value> {
    public let fun: String
    public let op: String
    
    public init(url: String, fun: String, op: String) {
        self.fun = fun
        self.op = op
        super.init(url: url)
    }
    
    public required init() {
        fatalError("init() has not been implemented")
    }
}
// Salt.swift
public struct Salt: Decodable {
    public let salt: String?
    
    open class Api: DecodableApi<Salt> {
        public private(set) var salt: String?
        public override init(url: String) {
            super.init(url: url)
        }
        
        public required init() {
            fatalError("init() has not been implemented")
        }
    }
}

// app target
// BasicApi.swift
import Test

class BasicApi<Value: Decodable>: Test.BasicApi<Value> {
    init(fun: String, op: String) {
        super.init(url: "", fun: fun, op: op)
    }
    
    required init() {
        fatalError("init() has not been implemented")
    }
}
// News.swift
import HVNetwork

struct News {
    let title: String
    let link: String?
    let pubDate: Date
    let desc: String?
    
    final class Api: HVNetwork.Api {
        required init() {
            super.init(url: "https://www.test.com")
        }
    }
}
// ViewController.swift
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let api = News.Api()
        api.send()
    }
}

It will crash at release mode.
RuntimeCrash(49642,0x108170380) malloc: Heap corruption detected, free list is damaged at 0x600002df9080
*** Incorrect guard value: 0
RuntimeCrash(49642,0x108170380) malloc: *** set a breakpoint in malloc_error_break to debug // The crash message appears many time in Swift 4.2.

@belkadan
Copy link
Contributor

@belkadan belkadan commented Sep 25, 2018

@swift-ci create

@rjmccall
Copy link
Member

@rjmccall rjmccall commented Sep 26, 2018

We believe this is fixed in trunk. You may be interested in trying out a nightly snapshot here: https://swift.org/download/#releases.

@swift-ci
Copy link
Collaborator Author

@swift-ci swift-ci commented Sep 26, 2018

Comment by LinQingmo (JIRA)

@rjmccall Sorry, do you mean it is fixed in Swift 5? I tried the latest Swift 4.2 snapshot, it crashed. Will it be release in maybe something like Swift 4.2.1?

@rjmccall
Copy link
Member

@rjmccall rjmccall commented Sep 26, 2018

Yes, Swift 5. It seems to be too late to get it fixed in 4.2.1; you'll need to find a workaround, I'm afraid.

@swift-ci
Copy link
Collaborator Author

@swift-ci swift-ci commented Sep 26, 2018

Comment by LinQingmo (JIRA)

@rjmccall Thanks. Indeed, I did find a workaround. I changed Salt.Api to Salt._Api then it worked. Though, I have no idea why it works. Just afraid whether it will occur in other cases, should be very carefully to test. Thanks.

@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
Projects
None yet
Development

No branches or pull requests

3 participants