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-11527] App crash with outlined init w/ copy of Any? #53928

Open
swift-ci opened this issue Sep 26, 2019 · 3 comments
Open

[SR-11527] App crash with outlined init w/ copy of Any? #53928

swift-ci opened this issue Sep 26, 2019 · 3 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 run-time crash Bug → crash: Swift code crashed during execution

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-11527
Radar rdar://problem/55746273
Original Reporter helge (JIRA User)
Type Bug
Environment

Xcode 11A1027
macOS 10.14.6

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

md5: 3676ba883a040c2d7a539c666a7d81e3

Issue Description:

An app built w/ Swift 5.1 crashes with `outlined init w/ copy of Any?`.

Code in question (https://github.com/DirectToSwift/CoreDataToSwiftUI/blob/develop/Sources/DirectToSwiftUI/Support/EquatableType.swift#L57):

public func eq(_ a: Any?, _ b: Any?) -> Bool {
  if let a = a, let b = b { // <== this line
    guard let a = a as? EquatableType else { return false }
    return a.isEqual(to: b)
  }
  else {
    return a == nil && b == nil
  }
}

To repro:

git clone -b bugs/outlined-init-with-copy-of-any-1 https://github.com/DirectToSwift/DVDRentalCoreData.git

Build & run (iOS app, iPhone 11 Pro Max as target)

BTW: This happened w/ that exact commit (97955e694ba3f8e26663cb5b2fb3895bd23f46b7), the commit of the app before works fine. The relevant change is this:

extension DynamicEnvironmentPathes {
  var staffUser : Staff? { set { user = newValue! } get { user as? Staff } }
}
...
  \.staffUser?.username == "Mike" => \.visibleEntityNames <= [ "Actor", "Film" ], // the crashing rule

Before I was using a trampoline (the `.p`) to access the managed object using a dynamic keypath, this works (but I want to get away from it):

  \.user?.p.username == "Mike" => \.visibleEntityNames <= [ "Actor", "Film" ],

I played around with it, and it related to the result returned by the key path:

  init<Value>(keyPath: Swift.KeyPath<RuleContext, Any?>, value: Value) {
    self.init { ruleContext in
      if let x = ruleContext[keyPath: keyPath] {
        // return eq(x!, value)
        if let v = value as? EquatableType {
          return v.isEqual(to: x) // touch x, we crash
        }
        if let t = x as? EquatableType { // touch x, we crash
          return t.isEqual(to: value)
        }

The key path in question is captured by the closure.

BT:

* thread #&#8203;1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
  * frame #&#8203;0: 0x0000000105c0f07e DVDRental`outlined init with copy of Any? at <compiler-generated>:0
    frame #&#8203;1: 0x0000000105c5b3cb DVDRental`eq(a=some, b="Mike") at EquatableType.swift:58:14
    frame #&#8203;2: 0x0000000105c0a540 DVDRental`closure #&#8203;1 in RuleKeyPathPredicate.init<Value>(ruleContext=SwiftUIRules.RuleContext @ 0x00007ffeea00d440, keyPath=0x0000600003ec60a0, value="Mike") at RuleKeyPathPredicateExtras.swift:13:7
    frame #&#8203;3: 0x0000000105d2deb7 DVDRental`RuleKeyPathPredicate.evaluate(ruleContext=SwiftUIRules.RuleContext @ 0x00007ffeea00d4d0, self=(predicate = 0x0000000105c0a5f0 DVDRental`partial apply forwarder for closure #&#8203;1 (SwiftUIRules.RuleContext) -> Swift.Bool in (extension in DirectToSwiftUI):SwiftUIRules.RuleKeyPathPredicate.init<A>(keyPath: Swift.KeyPath<SwiftUIRules.RuleContext, Swift.Optional<Any>>, value: A1) -> SwiftUIRules.RuleKeyPathPredicate<A> at <compiler-generated>, debugInfo = "")) at RuleKeyPathPredicate.swift:29:12
    frame #&#8203;4: 0x0000000105d2defa DVDRental`protocol witness for RulePredicate.evaluate(in:) in conformance RuleKeyPathPredicate<A> at <compiler-generated>:0
    frame #&#8203;5: 0x0000000105d36698 DVDRental`RuleModel.resolveValueForTypeID(typeID=(_value = 0x0000000105d63618 DVDRental`type metadata for DirectToSwiftUI.D2SKeys.visibleEntityNames), context=SwiftUIRules.RuleContext @ 0x00007ffeea00d7c0, self=0x000060000025d590) at RuleModel.swift:98:25
    frame #&#8203;6: 0x0000000105d325fc DVDRental`RuleContext.resolveValueForTypeID(typeID=(_value = 0x0000000105d63618 DVDRental`type metadata for DirectToSwiftUI.D2SKeys.visibleEntityNames), self=SwiftUIRules.RuleContext @ 0x00007ffeea00d960) at RuleContext.swift:72:30
    frame #&#8203;7: 0x0000000105d32f71 DVDRental`protocol witness for DynamicEnvironmentValues.resolveValueForTypeID(_:) in conformance RuleContext at <compiler-generated>:0
    frame #&#8203;8: 0x0000000105d28104 DVDRental`DynamicEnvironmentValues.optional<Self>(key=DirectToSwiftUI.D2SKeys.visibleEntityNames, self=SwiftUIRules.RuleContext @ 0x00007ffeea00dd10) at DynamicEnvironmentValues.swift:54:16
    frame #&#8203;9: 0x0000000105d27d4a DVDRental`DynamicEnvironmentValues.subscript.getter(key=DirectToSwiftUI.D2SKeys.visibleEntityNames, self=SwiftUIRules.RuleContext @ 0x00007ffeea00dd10) at DynamicEnvironmentValues.swift:34:18
    frame #&#8203;10: 0x0000000105d27297 DVDRental`EnvironmentValues.subscript.getter(key=DirectToSwiftUI.D2SKeys.visibleEntityNames, self=SwiftUI.EnvironmentValues @ 0x00007ffeea00dd60) at DynamicEnvironmentPathes.swift:37:29
    frame #&#8203;11: 0x0000000105d272c9 DVDRental`protocol witness for DynamicEnvironmentPathes.subscript.getter in conformance EnvironmentValues at <compiler-generated>:0
    frame #&#8203;12: 0x0000000105c3729a DVDRental`DynamicEnvironmentPathes.visibleEntityNames.getter(self=<no summary available>) at EnvironmentPathes.swift:334:15
    frame #&#8203;13: 0x0000000105c9331f DVDRental`key path getter for DynamicEnvironmentPathes.visibleEntityNames : EnvironmentValues at <compiler-generated>:0
    frame #&#8203;14: 0x00007fff50e3aaa3 libswiftCore.dylib`Swift.RawKeyPathComponent._projectReadOnly<A, B, C>(_: A, to: B.Type, endingWith: C.Type) -> Swift.RawKeyPathComponent.ProjectionResult<B, C> + 563
    frame #&#8203;15: 0x00007fff50e3a6fe libswiftCore.dylib`project2 #&#8203;1 <A, B><A1><A2>(A2.Type) -> Swift.Optional<B> in project #&#8203;1 <A, B><A1>(A1) -> Swift.Optional<B> in closure #&#8203;1 (Swift.KeyPathBuffer) -> B in Swift.KeyPath._projectReadOnly(from: A) -> B + 190
    frame #&#8203;16: 0x00007fff50e3a4c1 libswiftCore.dylib`closure #&#8203;1 (Swift.KeyPathBuffer) -> B in Swift.KeyPath._projectReadOnly(from: A) -> B + 545
    frame #&#8203;17: 0x00007fff50e3c4a9 libswiftCore.dylib`swift_getAtKeyPath + 185
    frame #&#8203;18: 0x00007fff2c5db032 SwiftUI`SwiftUI.(EnvironmentBox in _7B48F30970137591804EEB8D0D309152).update(property: inout SwiftUI.Environment<A>, context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>) -> Swift.Bool + 562
    frame #&#8203;19: 0x00007fff2c58dd96 SwiftUI`static SwiftUI.(BoxVTable in _68550FF604D39F05971FE35A26EE75B0).update(ptr: Swift.UnsafeMutableRawPointer, property: Swift.UnsafeMutableRawPointer, context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>) -> Swift.Bool + 198
    frame #&#8203;20: 0x00007fff2c58d081 SwiftUI`SwiftUI._DynamicPropertyBuffer.update(container: Swift.UnsafeMutableRawPointer, context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>) -> Swift.Bool + 129
    frame #&#8203;21: 0x00007fff2bfcdd89 SwiftUI`SwiftUI.(DynamicPropertyBody in _9F92ACD17B554E8AB7D29ABB1E796415).update(context: inout AttributeGraph.AttributeContext<SwiftUI.(DynamicPropertyBody in _9F92ACD17B554E8AB7D29ABB1E796415)<A>>) -> () + 457
    frame #&#8203;22: 0x00007fff2bfcea70 SwiftUI`protocol witness for static AttributeGraph.UntypedAttribute._update(_: Swift.UnsafeMutableRawPointer, graph: __C.AGGraphRef, attribute: __C.AGAttribute) -> () in conformance SwiftUI.(DynamicPropertyBody in _9F92ACD17B554E8AB7D29ABB1E796415)<A> : AttributeGraph.UntypedAttribute in SwiftUI + 32
    frame #&#8203;23: 0x00007fff2f8f1849 AttributeGraph`partial apply forwarder + 25
    frame #&#8203;24: 0x00007fff2f8d9255 AttributeGraph`AG::Graph::UpdateStack::update() + 1111
    frame #&#8203;25: 0x00007fff2f8d9513 AttributeGraph`AG::Graph::update_attribute(unsigned int, bool) + 377
    frame #&#8203;26: 0x00007fff2f8dc698 AttributeGraph`AG::Graph::value_ref(AG::attribute_t, AGTypeID, bool*) + 84
    frame #&#8203;27: 0x00007fff2f8e4324 AttributeGraph`AG::Graph::EntryExit::update_entry() const + 60
    frame #&#8203;28: 0x00007fff2f8d9255 AttributeGraph`AG::Graph::UpdateStack::update() + 1111
    frame #&#8203;29: 0x00007fff2f8d9513 AttributeGraph`AG::Graph::update_attribute(unsigned int, bool) + 377
    frame #&#8203;30: 0x00007fff2f8de131 AttributeGraph`AG::Subgraph::update(unsigned int) + 929
    frame #&#8203;31: 0x00007fff2c10d100 SwiftUI`SwiftUI.ViewGraph.(runTransaction in _D63C4EB7F2B205694B6515509E76E98B)(in: __C.AGGraphRef) -> () + 224
    frame #&#8203;32: 0x00007fff2c10d517 SwiftUI`closure #&#8203;1 (__C.AGGraphRef) -> (prefs: Swift.Bool, idealSize: Swift.Bool, outputs: SwiftUI.ViewGraph.Outputs) in SwiftUI.ViewGraph.updateOutputs(at: SwiftUI.Time) -> () + 103
    frame #&#8203;33: 0x00007fff2c10d1d1 SwiftUI`SwiftUI.ViewGraph.updateOutputs(at: SwiftUI.Time) -> () + 145
    frame #&#8203;34: 0x00007fff2c4af579 SwiftUI`closure #&#8203;1 () -> () in closure #&#8203;1 () -> () in (extension in SwiftUI):SwiftUI.ViewRendererHost.render(interval: Swift.Double, updateDisplayList: Swift.Bool) -> () + 1001
    frame #&#8203;35: 0x00007fff2c4aef8a SwiftUI`closure #&#8203;1 () -> () in (extension in SwiftUI):SwiftUI.ViewRendererHost.render(interval: Swift.Double, updateDisplayList: Swift.Bool) -> () + 634
    frame #&#8203;36: 0x00007fff2c4a4274 SwiftUI`(extension in SwiftUI):SwiftUI.ViewRendererHost.render(interval: Swift.Double, updateDisplayList: Swift.Bool) -> () + 436
    frame #&#8203;37: 0x00007fff2c65a182 SwiftUI`SwiftUI._UIHostingView.layoutSubviews() -> () + 226
    frame #&#8203;38: 0x00007fff2c65a1a5 SwiftUI`@objc SwiftUI._UIHostingView.layoutSubviews() -> () + 21
    frame #&#8203;39: 0x00007fff47a52ad5 UIKitCore`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2478
    frame #&#8203;40: 0x00007fff2b06e91d QuartzCore`-[CALayer layoutSublayers] + 255
    frame #&#8203;41: 0x00007fff2b073323 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 517
    frame #&#8203;42: 0x00007fff2b07fa7c QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 80
    frame #&#8203;43: 0x00007fff2afc6e54 QuartzCore`CA::Context::commit_transaction(CA::Transaction*, double) + 324
    frame #&#8203;44: 0x00007fff2affc32f QuartzCore`CA::Transaction::commit() + 643
    frame #&#8203;45: 0x00007fff475906cd UIKitCore`__34-[UIApplication _firstCommitBlock]_block_invoke_2 + 81
    frame #&#8203;46: 0x00007fff23b0d09c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    frame #&#8203;47: 0x00007fff23b0c808 CoreFoundation`__CFRunLoopDoBlocks + 312
    frame #&#8203;48: 0x00007fff23b07694 CoreFoundation`__CFRunLoopRun + 1284
    frame #&#8203;49: 0x00007fff23b06e66 CoreFoundation`CFRunLoopRunSpecific + 438
    frame #&#8203;50: 0x00007fff38346bb0 GraphicsServices`GSEventRunModal + 65
    frame #&#8203;51: 0x00007fff47578dd0 UIKitCore`UIApplicationMain + 1621
    frame #&#8203;52: 0x0000000105bef3cb DVDRental`main at AppDelegate.swift:12:7
    frame #&#8203;53: 0x00007fff516ecd29 libdyld.dylib`start + 1
    frame #&#8203;54: 0x00007fff516ecd29 libdyld.dylib`start + 1
@belkadan
Copy link
Contributor

@swift-ci create

@swift-ci
Copy link
Collaborator Author

Comment by Anthony Platanios (JIRA)

I'm running into a similar issue while using Swift for TensorFlow. I get a segfault when trying to initialize a struct that also happens during `outlined init with copy of`. Has anyone looked into this yet? In my case I'm not even using keypaths. I'm just initializing the properties of a struct.

@swift-ci
Copy link
Collaborator Author

Comment by Anthony Platanios (JIRA)

I get the error when trying to initialize this struct.

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

No branches or pull requests

3 participants