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
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 regression run-time crash Bug → crash: Swift code crashed during execution swift 4.2

Comments

@swift-ci
Copy link
Collaborator

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

@swift-ci create

@rjmccall
Copy link
Member

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

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

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

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
@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 regression run-time crash Bug → crash: Swift code crashed during execution swift 4.2
Projects
None yet
Development

No branches or pull requests

4 participants