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-10042] KeyPath causes crash in SIL using Swift 5 compiler, Xcode 10.2 b4 #52445

Open
swift-ci opened this issue Mar 4, 2019 · 6 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

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Mar 4, 2019

Previous ID SR-10042
Radar rdar://problem/48001932
Original Reporter alanzeino (JIRA User)
Type Bug
Status Reopened
Resolution

Attachment: Download

Environment

Xcode 10.2 beta 4

macOS 10.14.3

Swift 5 compiler, Swift 5 mode (also repros with 4.2 mode)

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

md5: b8e0a3634ca71355284a609568ffb0b6

Issue Description:

We've found a weird compiler crash during SIL emission. Verified still exists with Xcode 10.2 beta 4 using the Swift 5 compiler.

You need:

  • A protocol inside another framework

  • That protocol is @objc

  • An extension on that protocol with a property

  • A function that takes the KeyPath as an argument

  • To call the function, passing in the property from the extension

Here's the sample case:

In App:

import ThemeModule 
let myVar = Foo().bar(\.primaryText)
 
class Foo: UIView {
    func bar(_ keypath: KeyPath<Theme, UIColor>) -> Self {
        return self
    }
}

In a Dynamic Framework (called `ThemeModule` in this sample):

import  UIKit 
 
@objc
public protocol Theme: AnyObject {
    var foo: UIColor { get }
}
public extension Theme {   
    var primaryText: UIColor {
        return foo
    }
}

And the Segmentation Fault:

1. While emitting IR SIL function "@globalinit_33_84039C51AE3BDA1B5372E05F9BC4EC98_func0".
 for declaration 0x7f9b935f0200 (at /Users/alan/Desktop/KeyPathCrash/KeyPathCrash/AppDelegate.swift:12:1)
0  swift                    0x0000000112b69eb3 PrintStackTraceSignalHandler(void*) + 51
1  swift                    0x0000000112b6968c SignalHandler(int) + 348
2  libsystem_platform.dylib 0x00007fff7ee20b3d _sigtramp + 29
3  libsystem_malloc.dylib   0x00007fff7ede7b54 tiny_free_no_lock + 1035
4  swift                    0x000000010ff40a6a swift::Mangle::ASTMangler::appendAnyGenericType(swift::GenericTypeDecl const*) + 26
5  swift                    0x000000010ff3e70a swift::Mangle::ASTMangler::appendType(swift::Type) + 426
6  swift                    0x000000010ebc1100 swift::irgen::IRGenMangler::mangleTypeForReflection(swift::irgen::IRGenModule&, swift::Type) + 240
7  swift                    0x000000010eb69743 swift::irgen::IRGenModule::getTypeRef(swift::CanType, swift::irgen::MangledTypeRefRole) + 547
8  swift                    0x000000010eb162c4 void llvm::function_ref<void (swift::irgen::GenericRequirement)>::callback_fn<emitKeyPathComponent(swift::irgen::IRGenModule&, swift::irgen::ConstantStructBuilder&, swift::KeyPathPatternComponent const&, bool, swift::GenericEnvironment*, llvm::ArrayRef<swift::irgen::GenericRequirement>, swift::CanType, llvm::ArrayRef<KeyPathIndexOperand>, bool)::$_7>(long, swift::irgen::GenericRequirement) + 292
9  swift                    0x000000010ffcfa42 swift::GenericSignature::forEachParam(llvm::function_ref<void (swift::GenericTypeParamType*, bool)>) const + 690
10 swift                    0x000000010eb0e4dd emitKeyPathComponent(swift::irgen::IRGenModule&, swift::irgen::ConstantStructBuilder&, swift::KeyPathPatternComponent const&, bool, swift::GenericEnvironment*, llvm::ArrayRef<swift::irgen::GenericRequirement>, swift::CanType, llvm::ArrayRef<KeyPathIndexOperand>, bool) + 2413
11 swift                    0x000000010eb0cd91 swift::irgen::IRGenModule::getAddrOfKeyPathPattern(swift::KeyPathPattern*, swift::SILLocation) + 3121
12 swift                    0x000000010ebd4395 swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 19605
13 swift                    0x000000010ebccd4a swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 9898
14 swift                    0x000000010ea8b829 swift::irgen::IRGenerator::emitLazyDefinitions() + 1353
15 swift                    0x000000010ebac8f0 performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, swift::SourceFile*, llvm::GlobalVariable**) + 1232
16 swift                    0x000000010e9ce679 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 49417
17 swift                    0x000000010e9bec9e swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6862
18 swift                    0x000000010e95cdae main + 1246
19 libdyld.dylib            0x00007fff7ec35ed9 start + 1
20 libdyld.dylib            0x000000000000004c start + 2168234356
 
error: Segmentation fault: 11
 

I have attached the Sample Project as well.

@theblixguy
Copy link
Collaborator

cc @jckarter

@swift-ci
Copy link
Collaborator Author

swift-ci commented Mar 6, 2019

Comment by Alan Zeino (JIRA)

Found one more way to trigger the crash:

public class Bar {
    public var myProperty: KeyPath<Theme, UIColor> { return \Theme.primaryText }
}

@jckarter
Copy link
Member

jckarter commented Mar 6, 2019

The problem has to do with referring to protocol extension properties across module boundaries. You may be able to work around it by wrapping or redefining `primaryText` in a protocol extension local to the module that forms the key path.

@jckarter
Copy link
Member

jckarter commented Mar 6, 2019

It looks like it also avoids the problem if you refer to the property on a type that conforms to Theme rather than on Theme itself. You could try this:

class Foo: UIView {
    func bar<T: Theme>(_ keypath: KeyPath<T, UIColor>) -> Self {
        return self
    }
}

@AnnaZaks
Copy link
Mannequin

AnnaZaks mannequin commented Mar 7, 2019

alanzeino (JIRA User), Could you check if the workaround works for you?

Thanks!
Anna

@swift-ci
Copy link
Collaborator Author

swift-ci commented Mar 7, 2019

Comment by Alan Zeino (JIRA)

Yep we ended up using the first workaround; redeclaring the extension local to the code that causes the crash.

Thanks for the prompt reply this was a big help as this was the only blocker with Swift 5 for us 🙂

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

No branches or pull requests

4 participants