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-9903] Segmentation Fault: Malformed KeyPath Code #52309

Closed
daveanderson mannequin opened this issue Feb 11, 2019 · 2 comments
Closed

[SR-9903] Segmentation Fault: Malformed KeyPath Code #52309

daveanderson mannequin opened this issue Feb 11, 2019 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@daveanderson
Copy link
Mannequin

daveanderson mannequin commented Feb 11, 2019

Previous ID SR-9903
Radar None
Original Reporter @daveanderson
Type Bug
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 54eb1c2206f79ed0432e7c5e05e21ff2

Issue Description:

While experimenting with an idea using KeyPaths (which I've not heavily used previously) the Swift compiler (4.2, Xcode 10.1) began crashing with the following stack trace:

0  swift                    0x0000000105f6e59a PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x0000000105f6dd4e SignalHandler(int) + 302
2  libsystem_platform.dylib 0x00007fff6ea24b3d _sigtramp + 29
3  libsystem_platform.dylib 000000000000000000 _sigtramp + 2438837472
4  swift                    0x0000000102c5ca4c (anonymous namespace)::SILGenWitnessTable<(anonymous namespace)::SILGenConformance>::addMethod(swift::SILDeclRef) + 60
5  swift                    0x0000000102c5c934 swift::SILWitnessVisitor<(anonymous namespace)::SILGenConformance>::visitAbstractStorageDecl(swift::AbstractStorageDecl*) + 52
6  swift                    0x0000000102c5beb5 swift::SILWitnessVisitor<(anonymous namespace)::SILGenConformance>::visitProtocolDecl(swift::ProtocolDecl*) + 741
7  swift                    0x0000000102c5b66c swift::Lowering::SILGenModule::getWitnessTable(swift::ProtocolConformance*) + 364
8  swift                    0x0000000102c5fcbb swift::Lowering::SILGenModule::visitExtensionDecl(swift::ExtensionDecl*) + 587
9  swift                    0x0000000102b791eb swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*, unsigned int) + 939
10 swift                    0x0000000102b7ada5 swift::SILModule::constructSIL(swift::ModuleDecl*, swift::SILOptions&, swift::FileUnit*, llvm::Optional<unsigned int>, bool) + 1333
11 swift                    0x00000001021fb36e performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 28990
12 swift                    0x00000001021f0d35 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7717
13 swift                    0x0000000102196965 main + 1349
14 libdyld.dylib            0x00007fff6e839ed9 start + 1
Stack dump:

It took me a few minutes to figure out the file of my source code that was the cause of the error and after doing some more reading on KeyPaths I discovered I wasn't satisfying the requirements of the generics associated with KeyPaths.

While I was able to work around the seg fault, the compiler does not seem robust in this area. I received no diagnostic messaging, only the unceremonious crash.

While I'm not in a position to share my source code (which I have subsequently changed significantly), I believe the offending code was

public protocol KeyPathIdentifiable {
    typealias KeyPathStringIdentifier = (KeyPath, String)
    typealias KeyPathDateIdentifier = (KeyPath, Date)
    typealias KeyPathIdentifier = (KeyPath, Comparable)
    typealias KeyPathIdentifierCombination = [[KeyPathIdentifier]]

    var keyPathIdentifier: [KeyPathIdentifierCombination] { get }
}

I can replicate the crash by adding this snippet to my project. (I now understand why this code is wrong, it's just unfortunate that it causes a crash.)

No, I have not tried this with the Swift 5 compiler.

Here is one other compiler stack traces from my project build history related to efforts in this area.

0  swift                    0x000000010be6259a PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x000000010be61d4e SignalHandler(int) + 302
2  libsystem_platform.dylib 0x00007fff6ea24b3d _sigtramp + 29
3  libsystem_platform.dylib 000000000000000000 _sigtramp + 2438837472
4  swift                    0x000000010934b35e swift::serialization::Serializer::writeDecl(swift::Decl const*) + 72990
5  swift                    0x0000000109370b6f swift::serialization::Serializer::writeAllDeclsAndTypes() + 87183
6  swift                    0x00000001093896b6 swift::serialization::Serializer::writeAST(llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, bool) + 5030
7  swift                    0x00000001093975df swift::serialization::Serializer::writeToStream(llvm::raw_ostream&, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::SILModule const*, swift::SerializationOptions const&) + 4271
8  swift                    0x000000010939b475 void llvm::function_ref<void (llvm::raw_ostream&)>::callback_fn<swift::serialize(llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::SerializationOptions const&, swift::SILModule const*)::$_4>(long, llvm::raw_ostream&) + 181
9  swift                    0x000000010939b6bb withOutputFile(swift::ASTContext&, llvm::StringRef, llvm::function_ref<void (llvm::raw_ostream&)>) + 571
10 swift                    0x000000010939b2a3 swift::serialize(llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::SerializationOptions const&, swift::SILModule const*) + 275
11 swift                    0x00000001080fd43e std::__1::__function::__func<performCompileStepsPostSILGen(swift::CompilerInstance&, swift::CompilerInvocation&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, bool, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, bool, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*)::$_10, std::__1::allocator<performCompileStepsPostSILGen(swift::CompilerInstance&, swift::CompilerInvocation&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, bool, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, bool, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*)::$_10>, void ()>::operator()() + 526
12 swift                    0x00000001080f1e36 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 39942
13 swift                    0x00000001080e4d35 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7717
14 swift                    0x000000010808a965 main + 1349
15 libdyld.dylib            0x00007fff6e839ed9 start + 1
Stack dump:
@belkadan
Copy link
Contributor

It is in fact fixed in Swift 5!

<stdin>:4:42: error: reference to generic type 'KeyPath' requires arguments in <...>
    typealias KeyPathStringIdentifier = (KeyPath, String)
                                         ^
                                                <Any, Any>
Swift.KeyPath:1:14: note: generic type 'KeyPath' declared here
public class KeyPath<Root, Value> : PartialKeyPath<Root> {
             ^
<stdin>:5:40: error: reference to generic type 'KeyPath' requires arguments in <...>
    typealias KeyPathDateIdentifier = (KeyPath, Date)
                                       ^
                                              <Any, Any>
Swift.KeyPath:1:14: note: generic type 'KeyPath' declared here
public class KeyPath<Root, Value> : PartialKeyPath<Root> {
             ^
<stdin>:6:36: error: reference to generic type 'KeyPath' requires arguments in <...>
    typealias KeyPathIdentifier = (KeyPath, Comparable)
                                   ^
                                          <Any, Any>
Swift.KeyPath:1:14: note: generic type 'KeyPath' declared here
public class KeyPath<Root, Value> : PartialKeyPath<Root> {
             ^

@daveanderson
Copy link
Mannequin Author

daveanderson mannequin commented Feb 12, 2019

Thanks for the confirmation. I attempted to validate the improved error messaging in my project but unfortunately I encountered https://bugs.swift.org/browse/SR-9910 in the process.

@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
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

1 participant