Xcode 13.2.1: Crash about swift::Demangle/ResolveAsSymbolicReference when use SwiftUI if-available

I found a problem with the latest Xcode 13.2/13.2.1 when compiling SwiftUI if-#available structure. When building with Release mode, the app always crashes on an older system.

import SwiftUI

struct ContentView: View {
  var body: some View {
    List {
      if #available(iOS 15.0, *) {
        Text("Hello").badge(1)
      } else {
        Text("Hello") // 💥 always crash: iOS 14 + release mode
      }
    }
  }
}

Sample crash log:

OS Version:          iPhone OS 14.8.1 (18H107)
Release Type:        User
Baseband Version:    n/a
Report Version:      104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000000
VM Region Info: 0 is not in any region.  Bytes before following region: 4328914944
...

Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [54802]
Triggered by Thread:  0

Thread 0 name:
Thread 0 Crashed:
0   libswiftCore.dylib            	0x00000001b0a74750 swift::ResolveAsSymbolicReference::operator()(swift::Demangle::__runtime::SymbolicReferenceKind, swift::Demangle::__runtime::Directness, int, void const*) + 124 (MetadataValues.h:1263)
1   libswiftCore.dylib            	0x00000001b0a96018 swift::Demangle::__runtime::Demangler::demangleSymbolicReference(unsigned char) + 212 (functional:1880)
2   libswiftCore.dylib            	0x00000001b0a92d04 swift::Demangle::__runtime::Demangler::demangleType(__swift::__runtime::llvm::StringRef, std::__1::function<swift::Demangle::__runtime::Node* (swift::Demangle::__runtime::SymbolicReferenceKind, swi... + 200 (Demangler.cpp:589)
3   libswiftCore.dylib            	0x00000001b0a7a660 swift_getTypeByMangledNameImpl(swift::MetadataRequest, __swift::__runtime::llvm::StringRef, void const* const*, std::__1::function<swift::TargetMetadata<swift::InProcess> const* (unsigned int, unsi... + 496 (MetadataLookup.cpp:69)
4   libswiftCore.dylib            	0x00000001b0a77ab0 swift::swift_getTypeByMangledName(swift::MetadataRequest, __swift::__runtime::llvm::StringRef, void const* const*, std::__1::function<swift::TargetMetadata<swift::InProcess> const* (unsigned int, u... + 604 (CompatibilityOverride.def:153)
...
Answered by fespinozacast in 702968022

I tested this in the 13.3 beta (13E5086k) and this issue is not longer happening although I didn't find anything about this in the release notes https://developer.apple.com/documentation/xcode-release-notes/xcode-13_3-release-notes

If you apply validation at the body level it does not cause an error. It is a possible temporary solution.

I opened FB9816443. I believe this issue affects all SwiftUI app that build with Xcode 13.2/13.2.1.

Confirm, I experience the same crash on Xcode 13.2/13.2.1. Code inside of body

                    if #available(iOS 15.0, *) {
                        textField15
                    } else {
                        textField // crash on iOS 14.7.1
                    }

Stack Trace

Date/Time:           2021-12-21 22:15:39.8220 -0600
Launch Time:         2021-12-21 22:02:37.5490 -0600
OS Version:          iPhone OS 14.7.1 (18G82)
Release Type:        User
Baseband Version:    1.80.02
Report Version:      104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000000
VM Region Info: 0 is not in any region.  Bytes before following region: 4333060096
      REGION TYPE                 START - END      [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      __TEXT                   102454000-102458000 [   16K] r-x/r-x SM=COW  ...stop-appstore

Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [356]
Triggered by Thread:  0

Thread 0 name:
Thread 0 Crashed:
0   libswiftCore.dylib            	0x00000001a6996750 swift::ResolveAsSymbolicReference::operator()(swift::Demangle::__runtime::SymbolicReferenceKind, swift::Demangle::__runtime::Directness, int, void const*) + 124 (MetadataValues.h:1263)
1   libswiftCore.dylib            	0x00000001a69b8018 swift::Demangle::__runtime::Demangler::demangleSymbolicReference(unsigned char) + 212 (functional:1880)
2   libswiftCore.dylib            	0x00000001a69b4d04 swift::Demangle::__runtime::Demangler::demangleType(__swift::__runtime::llvm::StringRef, std::__1::function<swift::Demangle::__runtime::Node* (swift::Demangle::__runtime::SymbolicReferenceKind, swi... + 200 (Demangler.cpp:589)
3   libswiftCore.dylib            	0x00000001a699c660 swift_getTypeByMangledNameImpl(swift::MetadataRequest, __swift::__runtime::llvm::StringRef, void const* const*, std::__1::function<swift::TargetMetadata<swift::InProcess> const* (unsigned int, unsi... + 496 (MetadataLookup.cpp:69)
4   libswiftCore.dylib            	0x00000001a6999ab0 swift::swift_getTypeByMangledName(swift::MetadataRequest, __swift::__runtime::llvm::StringRef, void const* const*, std::__1::function<swift::TargetMetadata<swift::InProcess> const* (unsigned int, u... + 604 (CompatibilityOverride.def:153)
5   libswiftCore.dylib            	0x00000001a6999d08 swift_getTypeByMangledNameInContext + 152 (MetadataLookup.cpp:1735)
6   wingstop-appstore             	0x000000010281bac8 __swift_instantiateConcreteTypeFromMangledName + 52 (<compiler-generated>:0)
7   wingstop-appstore             	0x000000010283251c specialized HStack.init(alignment:spacing:content:) + 36 (<compiler-generated>:0)

I ran into the same crash on Xcode 13.2.1. Many users cannot launch an app by this crash. The problem occurs only on iOS14, but not on iOS15.

Date/Time:           2021-12-22 00:00:12.3437 +0900
Launch Time:         2021-12-22 00:00:12.1677 +0900
OS Version:          iPhone OS 14.7.1 (18G82)
Release Type:        User
Baseband Version:    2.05.01
Report Version:      104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000000
VM Region Info: 0 is not in any region.  Bytes before following region: 4297752576
      REGION TYPE                 START - END      [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      __TEXT                   1002a8000-1002ac000 [   16K] r-x/r-x SM=COW  .../MoodRecorder

Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [63104]
Triggered by Thread:  0

Thread 0 name:
Thread 0 Crashed:
0   libswiftCore.dylib            	0x00000001a7f46750 swift::ResolveAsSymbolicReference::operator()(swift::Demangle::__runtime::SymbolicReferenceKind, swift::Demangle::__runtime::Directness, int, void const*) + 124 (MetadataValues.h:1263)
1   libswiftCore.dylib            	0x00000001a7f68018 swift::Demangle::__runtime::Demangler::demangleSymbolicReference(unsigned char) + 212 (functional:1880)
2   libswiftCore.dylib            	0x00000001a7f64d04 swift::Demangle::__runtime::Demangler::demangleType(__swift::__runtime::llvm::StringRef, std::__1::function<swift::Demangle::__runtime::Node* (swift::Demangle::__runtime::SymbolicReferenceKind, swi... + 200 (Demangler.cpp:589)
3   libswiftCore.dylib            	0x00000001a7f4c660 swift_getTypeByMangledNameImpl(swift::MetadataRequest, __swift::__runtime::llvm::StringRef, void const* const*, std::__1::function<swift::TargetMetadata<swift::InProcess> const* (unsigned int, unsi... + 496 (MetadataLookup.cpp:69)
4   libswiftCore.dylib            	0x00000001a7f49ab0 swift::swift_getTypeByMangledName(swift::MetadataRequest, __swift::__runtime::llvm::StringRef, void const* const*, std::__1::function<swift::TargetMetadata<swift::InProcess> const* (unsigned int, u... + 604 (CompatibilityOverride.def:153)
5   libswiftCore.dylib            	0x00000001a7f49d08 swift_getTypeByMangledNameInContext + 152 (MetadataLookup.cpp:1735)
6   MoodRecorder                  	0x00000001002acf68 __swift_instantiateConcreteTypeFromMangledName + 52 (<compiler-generated>:0)
7   SwiftUI                       	0x00000001aa8bb22c partial apply for closure #1 in ViewBodyAccessor.updateBody(of:changed:) + 40 (CustomView.swift:61)
8   SwiftUI                       	0x00000001aac61c8c closure #1 in BodyAccessor.setBody(_:) + 52 (DynamicProperty.swift:227)
9   SwiftUI                       	0x00000001aa8b31f8 ViewBodyAccessor.updateBody(of:changed:) + 1520 (DynamicProperty.swift:226)
10  SwiftUI                       	0x00000001aac62168 DynamicBody.updateValue() + 492 (DynamicProperty.swift:272)
11  SwiftUI                       	0x00000001aa8dbb00 partial apply for implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 32 (<compiler-generated>:0)
12  AttributeGraph                	0x00000001cd5cc77c AG::Graph::UpdateStack::update() + 492 (ag-closure.h:108)

code-block

I committed a code-level support request about this compiler bug several days ago. However, they replied that there is no workaround available, and the bug is still being investigated.

Finally I realize I have to solve this apple bug by myself.

Possible workarounds I found:

  1. Go back to use Xcode 13.1
  2. Keep using Xcode 13.2 but disable compiler optimization with -Onone
  3. Write a simple container view like @available(iOS 15, *) struct Wrapper<Content: View>: View {…}. And wrap the true branch of each if-#available in ViewBuilder with it.

Hello; maybe I found a solution to this problem

before this code crashed on iOS 14 when I used dismissDisable()


extension View {
@ViewBuilder
    func dismissDisable() -> some View {
        if #available(iOS 15.0, *) {
            self.interactiveDismissDisabled()
        } else {
            self
        }
    }
}

I changed this code by :

@available(iOS 15.0, *)
struct DismissDisableModifier: ViewModifier {

    func body(content: Content) -> some View {
        content.interactiveDismissDisabled()
    }
    
}

extension View {
@ViewBuilder
    func dismissDisable() -> some View {
        if #available(iOS 15.0, *) {
            self.modifier(DismissDisableModifier())
        } else {
            self
        }
    }
}

And seems to pass on iOS14 !

I believe I ran into the same issue and was able to create a Technical Support Incident with Apple and there is a build-level flag / workaround (no code change) that you can set which should allow it to work. Since the support is under NDA and you may have other specific issues they suggested filing an incident so they can debug more specifically. They can reference my TSI number (788945838) to see if the workaround my Apple contact provided works for your case.

Accepted Answer

I tested this in the 13.3 beta (13E5086k) and this issue is not longer happening although I didn't find anything about this in the release notes https://developer.apple.com/documentation/xcode-release-notes/xcode-13_3-release-notes

I just received a response for FB9816443 from Apple. I haven't tested it yet, but it looks like a viable solution.

Add following options to “Other Swift Flags” in the build settings:

-Xllvm -sil-disable-pass=alloc-stack-hoisting

Hi @Gong, I can confirm the flag can help with Xcode 13.2.1 and earlier version.

And Xcode 13.3 has the fix.

My demo project: https://github.com/imWildCat/GetOpaqueTypeMetadataSwiftUICrash

Keeping that code in ViewModifier worked for me.

I have the same crash, and the crash device is Arm Mac, suspected to be installed through mac catalyst App, how do I fix it

Xcode 13.2.1: Crash about swift::Demangle/ResolveAsSymbolicReference when use SwiftUI if-available
 
 
Q