SwiftUI crash when running optimized (-O/-Osize) builds on iOS 14 in conjunction with availability checks

Originator:kyle.hickinson
Number:rdar://FB9812596 Date Originated:December 17, 2021
Status:Open Resolved:No
Product:SwiftUI Framework Product Version:
Classification:Application Crash Reproducible:
 
Preconditions:

- Build with Xcode 13.2 or higher (Both 13.2.1 and 13.3 beta tested and still crashing)
- Build using -O or -Osize 
- Run on iOS 14 (tested up to 14.8.1)

Summary:

If you have a ViewBuilder which performs an availability check (“if #available”) and within that limited availability you use an API only available in that version—such as using an iOS 15 API from within a 15 runtime check) the application will crash when attempting to demangle the symbols.

Code:

extension View {
    @ViewBuilder func crash() -> some View {
        if #available(iOS 15.0, *) {
            self.searchable(text: .constant("Test"))
        } else {
            self
        }
    }
}

@main
struct CrashSampleApp: App {
    var body: some Scene {
        WindowGroup {
            Text("Test")
              .crash()
        }
    }
}

Crash log:

```
#0  0x000000018e2e6f98 in swift::ResolveAsSymbolicReference::operator()(swift::Demangle::__runtime::SymbolicReferenceKind, swift::Demangle::__runtime::Directness, int, void const*) ()
#1  0x000000018e30461c in swift::Demangle::__runtime::Demangler::demangleSymbolicReference(unsigned char) ()
#2  0x000000018e301880 in swift::Demangle::__runtime::Demangler::demangleType(__swift::__runtime::llvm::StringRef, std::__1::function<swift::Demangle::__runtime::Node* (swift::Demangle::__runtime::SymbolicReferenceKind, swift::Demangle::__runtime::Directness, int, void const*)>) ()
#3  0x000000018e2ec510 in swift_getTypeByMangledNameImpl(swift::MetadataRequest, __swift::__runtime::llvm::StringRef, void const* const*, std::__1::function<swift::TargetMetadata<swift::InProcess> const* (unsigned int, unsigned int)>, std::__1::function<swift::TargetWitnessTable<swift::InProcess> const* (swift::TargetMetadata<swift::InProcess> const*, unsigned int)>) ()
#4  0x000000018e2e9f0c in swift::swift_getTypeByMangledName(swift::MetadataRequest, __swift::__runtime::llvm::StringRef, void const* const*, std::__1::function<swift::TargetMetadata<swift::InProcess> const* (unsigned int, unsigned int)>, std::__1::function<swift::TargetWitnessTable<swift::InProcess> const* (swift::TargetMetadata<swift::InProcess> const*, unsigned int)>) ()
#5  0x000000018e2ea120 in swift_getTypeByMangledNameInContext ()
#6  0x00000001022170f4 in __swift_instantiateConcreteTypeFromMangledName ()
#7  0x0000000102216b5c in specialized static ViewBuilder.buildLimitedAvailability<Ï„_0_0>(_:) [inlined] ()
#8  0x0000000102216b50 in specialized View.crash() at ../CrashSample/CrashSample/CrashSampleApp.swift:14
#9  0x0000000102216eac in closure #1 in CrashSampleApp.body.getter at ../CrashSample/CrashSample/CrashSampleApp.swift:25
#10 0x00000001b328d83c in WindowGroup.init(content:) ()
#11 0x0000000102216f90 in CrashSampleApp.body.getter [inlined] at ../CrashSample/CrashSample/CrashSampleApp.swift:23
#12 0x0000000102216f10 in protocol witness for App.body.getter in conformance CrashSampleApp at ../CrashSample/<compiler-generated>:22
#13 0x00000001b2ee4454 in partial apply for closure #1 in AppBodyAccessor.updateBody(of:changed:) ()
#14 0x00000001b3286188 in closure #1 in BodyAccessor.setBody(_:) ()
#15 0x00000001b2ee3e08 in AppBodyAccessor.updateBody(of:changed:) ()
#16 0x00000001b32862b0 in StaticBody.updateValue() ()
#17 0x00000001b2fa29a4 in partial apply for implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<Ï„_0_0>(_:) ()
#18 0x00000001a953b1c0 in AG::Graph::UpdateStack::update() ()
#19 0x00000001a953b5c8 in AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) ()
#20 0x00000001a95404f0 in AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) ()
#21 0x00000001a95511cc in AGGraphGetValue ()
#22 0x00000001b3285c68 in StaticBody.container.getter ()
#23 0x00000001b3286294 in StaticBody.updateValue() ()
#24 0x00000001b2fa29a4 in partial apply for implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<Ï„_0_0>(_:) ()
#25 0x00000001a953b1c0 in AG::Graph::UpdateStack::update() ()
#26 0x00000001a953b5c8 in AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) ()
#27 0x00000001a95404f0 in AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) ()
#28 0x00000001a95511cc in AGGraphGetValue ()
#29 0x00000001b328ed7c in WindowGroupSceneList.MakeList.id.getter ()
#30 0x00000001b328ee18 in WindowGroupSceneList.MakeList.updateValue() ()
#31 0x00000001b2e6a4f4 in partial apply for specialized implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<Ï„_0_0>(_:) ()
#32 0x00000001a953b1c0 in AG::Graph::UpdateStack::update() ()
#33 0x00000001a953b5c8 in AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) ()
#34 0x00000001a953ff54 in AG::Graph::value_ref(AG::AttributeID, AGSwiftMetadata const*, bool*) ()
#35 0x00000001a9551214 in AGGraphGetValue ()
#36 0x00000001b2ee32fc in AppGraph.sceneList() ()
#37 0x00000001b35cb328 in AppSceneDelegate.makeSceneHost(restorationSceneItemID:userActivity:urlContexts:) ()
#38 0x00000001b35c990c in AppSceneDelegate.scene(_:willConnectTo:options:) ()
#39 0x00000001b35ca234 in @objc AppSceneDelegate.scene(_:willConnectTo:options:) ()
#40 0x0000000183916dac in +[UIScene _sceneForFBSScene:create:withSession:connectionOptions:] ()
#41 0x00000001843f1cd4 in -[UIApplication _connectUISceneFromFBSScene:transitionContext:] ()
#42 0x00000001843f200c in -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] ()
#43 0x0000000183f32d70 in -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] ()
#44 0x000000018566b090 in -[FBSScene _callOutQueue_agent_didCreateWithTransitionContext:completion:] ()
#45 0x00000001856925bc in __94-[FBSWorkspaceScenesClient createWithSceneID:groupID:parameters:transitionContext:completion:]_block_invoke.176 ()
#46 0x00000001856787b0 in -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] ()
#47 0x00000001856922d4 in __94-[FBSWorkspaceScenesClient createWithSceneID:groupID:parameters:transitionContext:completion:]_block_invoke ()
#48 0x00000001026ad884 in _dispatch_client_callout ()
#49 0x00000001026b0b8c in _dispatch_block_invoke_direct ()
#50 0x00000001856b692c in __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ ()
#51 0x00000001856b65f4 in -[FBSSerialQueue _targetQueue_performNextIfPossible] ()
#52 0x00000001856b6ac8 in -[FBSSerialQueue _performNextFromRunLoopSource] ()
#53 0x000000018036631c in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#54 0x000000018036621c in __CFRunLoopDoSource0 ()
#55 0x00000001803655e4 in __CFRunLoopDoSources0 ()
#56 0x000000018035fbb4 in __CFRunLoopRun ()
#57 0x000000018035f3bc in CFRunLoopRunSpecific ()
#58 0x000000018afdd70c in GSEventRunModal ()
#59 0x00000001843f03d0 in -[UIApplication _run] ()
#60 0x00000001843f51ac in UIApplicationMain ()
#61 0x00000001b34c6f6c in closure #1 in KitRendererCommon(_:) ()
#62 0x00000001b34c6efc in runApp<Ï„_0_0>(_:) ()
#63 0x00000001b309ca74 in static App.main() ()
#64 0x0000000102217050 in static CrashSampleApp.$main() [inlined] at ../CrashSample/CrashSample/CrashSampleApp.swift:20
#65 0x0000000102217038 in main ()
#66 0x00000001023410f4 in start ()
```

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!