Segmentation fault 11 since Xcode 11.4

Since Xcode 11.4 I'm getting segmentation fault 11 when trying to compile my project.
Also having the crash with Xcode 11.4.1, Xcode 11.5 and Xcode 11.6.
It is working with Xcode 11.3.1.
I really need to make updates to my app but I can't compile since few months now.

0  swift                    0x000000010971d4ea PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x000000010971ccc0 SignalHandler(int) + 352
2  libsystem_platform.dylib 0x00007fff6b15f5fd _sigtramp + 29
3  swift                    0x00000001063af9e0 void llvm::function_ref<void (swift::GenericTypeParamType*, bool)>::callback_fn<swift::SubstitutionMap::Storage::Profile(llvm::FoldingSetNodeID&, swift::GenericSignature, llvm::ArrayRef<swift::Type>, llvm::ArrayRef<swift::ProtocolConformanceRef>)::$_11>(long, swift::GenericTypeParamType*, bool) + 0
4  swift                    0x000000010589af82 swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 1442
5  swift                    0x00000001058ed9c3 swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, llvm::Optional<swift::SILLocation>) + 131
6  swift                    0x00000001058e1848 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int) + 280
7  swift                    0x00000001059224ed swift::Lowering::SILGenModule::emitGlobalInitialization(swift::PatternBindingDecl*, unsigned int) + 2285
8  swift                    0x000000010588e4f1 swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 1985
9  swift                    0x000000010588cf4c swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*) + 1356
10 swift                    0x000000010588efce swift::SILModule::constructSIL(swift::ModuleDecl*, swift::Lowering::TypeConverter&, swift::SILOptions&, swift::FileUnit*) + 1438
11 swift                    0x000000010546ed51 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 48065
12 swift                    0x00000001053e6b73 main + 1283
13 libdyld.dylib            0x00007fff6af66cc9 start + 1
error: Segmentation fault: 11 (in target 'Easytopia' from project 'Easytopia')

Are you using SwiftUI? We found in a SwiftUI project that any extension on a SwiftUI type defined in a framework and used outside of it, needed to be marked @inlinable. I don’t know if it’s a bug that was introduced in swift 5.2 or something specific to SwiftUI.

Not in my main target. But I use Cocoapods, so maybe one of the pod is using it and I'm using it without knowing. I will dig into it.

Edit : I don't find any reference to swiftUI in any targets / pods / subprojects / libraries

I have just tested with Xcode 12 beta. Same error, but the log seems more verbose

0  swift                    0x00000001099eff65 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift                    0x00000001099eef65 llvm::sys::RunSignalHandlers() + 85
2  swift                    0x00000001099f051f SignalHandler(int) + 111
3  libsystem_platform.dylib 0x00007fff6b15f5fd _sigtramp + 29
4  libsystem_malloc.dylib   0x00007fff6b11fa3d tiny_malloc_from_free_list + 555
5  swift                    0x0000000105a82e9b swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 5179
6  swift                    0x0000000105a74c08 swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 40
7  swift                    0x0000000105a291d1 (anonymous namespace)::SILGenApply::visitExpr(swift::Expr*) + 33
8  swift                    0x0000000105a0cde5 swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 277
9  swift                    0x0000000105a81aa9 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 73
10 swift                    0x0000000105a71165 swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, llvm::Optional<swift::SILLocation>) + 133
11 swift                    0x0000000105a60618 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int) + 280
12 swift                    0x0000000105aa7fe6 swift::Lowering::SILGenFunction::emitLazyGlobalInitializer(swift::PatternBindingDecl*, unsigned int) + 166
13 swift                    0x00000001059fcd8b swift::Lowering::SILGenModule::emitLazyGlobalInitializer(llvm::StringRef, swift::PatternBindingDecl*, unsigned int) + 1291
14 swift                    0x0000000105aa7c5d swift::Lowering::SILGenModule::emitGlobalInitialization(swift::PatternBindingDecl*, unsigned int) + 1117
15 swift                    0x0000000105b0e5b1 swift::ASTVisitor<SILGenExtension, void, void, void, void, void, void>::visit(swift::Decl*) + 369
16 swift                    0x0000000105b0996b SILGenExtension::emitExtension(swift::ExtensionDecl*) + 59
17 swift                    0x0000000105a01673 swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 1059
18 swift                    0x00000001059ff5c9 (anonymous namespace)::SILGenModuleRAII::emitSourceFile(swift::SourceFile*) + 1417
19 swift                    0x00000001059fef89 swift::SILGenSourceFileRequest::evaluate(swift::Evaluator&, swift::SILGenDescriptor) const + 137
20 swift                    0x0000000105af9c41 swift::SimpleRequest<swift::SILGenSourceFileRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> > (swift::SILGenDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::SILGenSourceFileRequest const&, swift::Evaluator&) + 33
21 swift                    0x0000000105a05dbc llvm::Expected<swift::SILGenSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::SILGenSourceFileRequest>(swift::SILGenSourceFileRequest const&) + 972
22 swift                    0x0000000105a00e1d swift::performSILGeneration(swift::FileUnit&, swift::Lowering::TypeConverter&, swift::SILOptions const&) + 173
23 swift                    0x00000001055734bf swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 20639
24 swift                    0x00000001054f4a97 main + 1255
25 libdyld.dylib            0x00007fff6af66cc9 start + 1
26 libdyld.dylib            0x0000000000000165 start + 18446603338721629341
error: Segmentation fault: 11 (in target 'Easytopia' from project 'Easytopia')

The best way that I know to debug this is download the 5.2 development snapshot tool chain and try to build your app. It has assertions enabled, so it will likely better point you to what went wrong. https://swift.org/download/#snapshots

There were a few things that didn’t build, I think keychain APIs if I remember correctly, so I had commented them out.

Also you may want to try the latest Xcode release, the issue could already be fixed there.

I have downloaded swift-5.2.3-dev snapshot toolchain and used it in Xcode 11.5.

I have this log now :
Assertion failed: (isa<X>(Val) && "cast<Ty>() argument of incompatible type!"), function cast, file /Users/buildnode/jenkins/workspace/oss-swift-5.2-package-osx/llvm-project/llvm/include/llvm/Support/Casting.h, line 264.

And the error has changed to Abort trap: 6 :

0  swift                    0x000000010a41c0d8 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x000000010a41b2f5 llvm::sys::RunSignalHandlers() + 85
2  swift                    0x000000010a41c6b0 SignalHandler(int) + 272
3  libsystem_platform.dylib 0x00007fff6b15f5fd _sigtramp + 29
4  swift                    0x000000010c87d948 cmark_strbuf__initbuf + 144859
5  libsystem_c.dylib        0x00007fff6b035808 abort + 120
6  libsystem_c.dylib        0x00007fff6b034ac6 err + 0
7  swift                    0x000000010a50bdb3 (anonymous namespace)::SILGenApply::visitExpr(swift::Expr*) (.cold.5) + 35
8  swift                    0x0000000106ac05d6 (anonymous namespace)::SILGenApply::visitExpr(swift::Expr*) + 438
9  swift                    0x0000000106ac233c (anonymous namespace)::SILGenApply::visitApplyExpr(swift::ApplyExpr*) + 2700
10 swift                    0x0000000106aa9830 swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 144
11 swift                    0x0000000106b070ea swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 106
12 swift                    0x0000000106afa0cc swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, llvm::Optional<swift::SILLocation>) + 300
13 swift                    0x0000000106aebfcf swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int) + 335
14 swift                    0x0000000106b24758 swift::Lowering::SILGenFunction::emitLazyGlobalInitializer(swift::PatternBindingDecl*, unsigned int) + 248
15 swift                    0x0000000106a9f1dd swift::Lowering::SILGenModule::emitLazyGlobalInitializer(llvm::StringRef, swift::PatternBindingDecl*, unsigned int) + 813
16 swift                    0x0000000106b2436b swift::Lowering::SILGenModule::emitGlobalInitialization(swift::PatternBindingDecl*, unsigned int) + 1195
17 swift                    0x0000000106b78969 SILGenExtension::visitPatternBindingDecl(swift::PatternBindingDecl*) + 89
18 swift                    0x0000000106b7560b SILGenExtension::emitExtension(swift::ExtensionDecl*) + 59
19 swift                    0x0000000106b755c5 swift::Lowering::SILGenModule::visitExtensionDecl(swift::ExtensionDecl*) + 21
20 swift                    0x0000000106aa0af3 swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*) + 915
21 swift                    0x0000000106aa1a32 swift::SILModule::constructSIL(swift::ModuleDecl*, swift::Lowering::TypeConverter&, swift::SILOptions&, swift::FileUnit*) + 322
22 swift                    0x0000000106aa1e11 swift::performSILGeneration(swift::ModuleDecl*, swift::Lowering::TypeConverter&, swift::SILOptions&) + 17
23 swift                    0x00000001067a4f21 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 8801
24 swift                    0x00000001067a1d51 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 5153
25 swift                    0x000000010673de8e main + 734
26 libdyld.dylib            0x00007fff6af66cc9 start + 1
27 libdyld.dylib            0x00000000000000f8 start + 2500432944
error: Abort trap: 6 (in target 'Easytopia' from project 'Easytopia')

What to do now ?

Could you file a bug report on https://bugs.swift.org with an anonymized/minimized version of the code that is triggering the crash? My guess is that with the assertion crash, you must've seen a message with "when lowering (...)" or something like that, giving you some clues on what the compiler is processing when it crashed. If you can't anonymize/minimize it, you could also file a bug report on https://feedbackassistant.apple.com and attach your project.

2 Likes

I don’t find any relevant log I can exploit. I have no clue on where is the error as I did not change any code since few months. Just using new Xcode version did raise the error (11.4.x, 11.5, 11.6 and 12). I then can’t minimize the code causing crash because I don’t know what is causing crash and it is a relatively big project.
Everything is compiling in Xcode 11.3.x with exact same code.
I already did file a bug on feedback assistant a month ago with a zip of the project, but no update since.

Thanks for filing the bug report. Could you share the exact feedback number?

Feedback number is FB7710238

Ok, I have a minimal code to reproduce this issue.
This is greatly inspired by the Moya library.

import Foundation

struct Endpoint {
    var url: URL
    
    func urlRequest() -> URLRequest {
        return URLRequest(url: url)
    }
}

protocol TargetType {
    var method: String { get }
}

protocol ProviderType {
    associatedtype Target: TargetType
}

struct Provider<Target: TargetType>: ProviderType {
    public typealias RequestResultClosure = (Result<URLRequest, Error>) -> Void
    public typealias RequestClosure = (Endpoint, @escaping RequestResultClosure) -> Void
}

enum MyProvider: TargetType {
    case get
    
    var method: String {
        return "GET"
    }
}

struct Namespace {
    static let authenticationRequestClosure: Provider.RequestClosure = { endpoint, done in
        done(.success(endpoint.urlRequest()))
    }
}

Now line 49
static let authenticationRequestClosure: Provider.RequestClosure = { endpoint, done in ... }
It is crashing

But if you specify the generic type for Provider
static let authenticationRequestClosure: Provider<MyProvider>.RequestClosure = { endpoint, done in ... }
The build pass

With swift 5.1 the generic type seems to be inferred as Provider<TargetType> without explicitly giving generic type.

I have updated the feedback bug report with this answer

2 Likes

Were you able to translate this to your project (i.e. add more type annotations) and have a workaround that made the project compile successfully with a recent Xcode?

The problem is that RequestClosure and RequestResultClosure types are defined in a generic struct, but they don't use the generic type Target: TargetType.

So the expecting behavior is that I can use these closure type aliases without specifying TargetType by declaring

static let closure: Provider.RequestClosure = ...

But the compiler requires an explicit generic type or it crashes. But I can't give a generic type as I use this closure for any TargetType.

My workaround is to define the closure without the type aliases :

static let closure: (Endpoint, @escaping (Result<URLRequest, Error>) -> Void) -> Void = ...

Or move the type aliases outside generic struct.

1 Like

Ok, glad to see you at least have a workaround though!

Terms of Service

Privacy Policy

Cookie Policy