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-8158] Printing type(of: Codable & Protocol type ) EXC_BAD_ACCESS #50690

Closed
swift-ci opened this issue Jun 30, 2018 · 10 comments
Closed

[SR-8158] Printing type(of: Codable & Protocol type ) EXC_BAD_ACCESS #50690

swift-ci opened this issue Jun 30, 2018 · 10 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution runtime The Swift Runtime standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-8158
Radar rdar://problem/41725205
Original Reporter Glorysaber (JIRA User)
Type Bug
Status Closed
Resolution Done
Environment

MacOS 10.14 Beta (18A314k), Swift 4.2 included in Xcode Version 10.0 beta 2 (10L177m) and also occurs in Swift 4.1

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug, RunTimeCrash, Runtime
Assignee None
Priority Medium

md5: dfcf32059f3181555b09f3e28f1976d2

Issue Description:

When printing a type of Inheriting class that is stored as a typealias to a composition between any protocol and the Codable protocol, you get a EXC_BAD_ACCESS with the below error in Swift 4.2.

protocol Foo {}

typealias Bar = Foo & Codable

class InheritingClass: Bar { }
  
let theObject: Bar = InheritingClass()

print("\(type(of:theObject))")
 thread #&#8203;1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x7ffee62bb000) frame #&#8203;0: 0x000000010d897ae8 libswiftCore.dylib`swift::metadataimpl::NonFixedValueWitnesses<swift::metadataimpl::NonFixedExistentialMetatypeBox, true>::initializeWithCopy(swift::OpaqueValue*, swift::OpaqueValue*, swift::TargetMetadata<swift::InProcess> const*) + 184
 frame #&#8203;1: 0x000000010d6144bc libswiftCore.dylib`Swift._print_unlocked<A, B where B: Swift.TextOutputStream>(A, inout B) -> () + 316
 frame #&#8203;2: 0x00007fff3ce051ca CoreFoundation`_CFStringCreateByAddingPercentEncodingWithAllowedCharacters.hexchars + 13818
 frame #&#8203;3: 0x00007fff68c76e40 libobjc.A.dylib`cache_getImp + 144

This does not happen if: the class directly inherits from Codable and another protocol, the protocol also inherits from Codable, or the object is not casted to the typealias.

This is a problem and can lead to hard to find bugs in cases where you may store the types and then try to print them later.

@belkadan
Copy link
Contributor

belkadan commented Jul 2, 2018

Bizarre. Thanks, Stephen.

@swift-ci create

@mikeash
Copy link
Contributor

mikeash commented Jul 6, 2018

Thanks for the simple and short test case. Worked (crashed) on the first try here.

@swift-ci
Copy link
Collaborator Author

Comment by Leandro Barbosa (JIRA)

Just to add that I found this post while searching for my own weird issue and it wasn't related to type(of). Any kind of access to the type will cause it. This is a simplified version of what I was trying to do:

import Foundation

protocol Proto {
}

typealias CodableProtoObject = NSObject & Codable & Proto

class Foo: CodableProtoObject {
}

class Bar: CodableProtoObject { 
}

var codableTypes: [CodableProtoObject.Type] = []
print("we're fine until here...")
codableTypes = [Foo.self, Bar.self]
print("we'll never get here")

I tried not using the alias and using the types directly but got the same outcome.

Removing Codable or Proto from the typealias is enough to avoid the error.

Environment: MacOS 10.13.6 (17G65), XCode 9.4.1 (9F2000)

@swift-ci
Copy link
Collaborator Author

Comment by Leandro Barbosa (JIRA)

The issue disappears if I restrict the protocol to the classes/protocol in question which is a valid workaround for my use case.

This example works:

import Foundation

protocol Proto: NSObject & Codable {
}

typealias CodableProtoObject = NSObject & Codable & Proto

class Foo: CodableProtoObject {
    
}
class Bar: CodableProtoObject {
    
}
var codableTypes: [CodableProtoObject.Type] = []
print("this is ok...")
codableTypes = [Foo.self, Bar.self]
print("this is fine too!")

@mikeash
Copy link
Contributor

mikeash commented Dec 7, 2018

Pull request for the fix: #21119 Turned out to be a really simple incorrect cast. Just a bit hard to track down due to cause and effect being fairly far removed from each other.

@swift-ci
Copy link
Collaborator Author

Comment by Stephen (JIRA)

I can confirm that in Xcode Version 10.2 (10E125) and Swift version 5.0 (swiftlang-1001.0.69.3 clang-1001.0.47) that the bug has been fixed and the code now performs correctly.

@swift-ci
Copy link
Collaborator Author

Comment by Stephen (JIRA)

Issue resolved in Swift version 5.0 (swiftlang-1001.0.69.3 clang-1001.0.47)

@mikeash
Copy link
Contributor

mikeash commented Apr 12, 2019

Great, thanks for confirming!

@swift-ci
Copy link
Collaborator Author

Comment by Stephen (JIRA)

No problem 🙂

@AnnaZaks
Copy link
Mannequin

AnnaZaks mannequin commented Jul 23, 2019

Closing the issue since the fix has been confirmed.

@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. crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution runtime The Swift Runtime standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

4 participants