You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
objc_getClassList crashed with an iOS 16 available class, in device that lower than iOS 16 #61215
#if canImport(RoomPlan)importRoomPlan@available(iOS16,*)classFoo:NSObject{varbar:CapturedRoom?}#endifimportSwiftUI@mainstructMetadataAccessorCrashApp:App{varbody:someScene{WindowGroup{Text("").onAppear{
// Crash here
let _ =objc_getClassList(nil,0)}}}}
Steps To Reproduce
Steps to reproduce the behavior:
This is, unfortunately, a known problem. The ObjC runtime doesn't know about the limited availability and tries to instantiate the class even though it won't work on the older OS version.
If possible, avoid objc_getClassList and objc_copyClassList. In addition to this problem, they're extremely inefficient and it's usually better to maintain an explicit list of the classes you're interested in.
It's also possible to work around this problem by adding an unused generic parameter to the class. @available(iOS 16, *) class Foo<T>: NSObject will hide it from the ObjC runtime so it will no longer try to instantiate Foo.
Reacted by Kem Chen, Patrick Bush and Vijay Tholpadi
This is, unfortunately, a known problem. The ObjC runtime doesn't know about the limited availability and tries to instantiate the class even though it won't work on the older OS version.
If possible, avoid objc_getClassList and objc_copyClassList. In addition to this problem, they're extremely inefficient and it's usually better to maintain an explicit list of the classes you're interested in.
It's also possible to work around this problem by adding an unused generic parameter to the class. @available(iOS 16, *) class Foo<T>: NSObject will hide it from the ObjC runtime so it will no longer try to instantiate Foo.
Ok, look likes I should go with the generic parameter way. Thanks for your explanation.
We hit this in production (a third-party SDK calls objc_copyClassList, so we can't avoid the call), and found a second workaround besides the unused generic parameter.
Sharing in case it could help others.
When does it actually crash?
Not every @available class crashes. It crashes only when the class needs runtime metadata initialization, i.e. its layout can't be computed at compile time. In the sample, CapturedRoom is a non-frozen struct from another module, so Foo's field offsets are only known at runtime. The compiler then emits a metadata initializer and sets RO_HAS_SWIFT_INITIALIZER (0x40) on the class.
objc_getClassList/objc_copyClassList call realizeAllClasses, which runs that initializer. The initializer asks for CapturedRoom's metadata, which doesn't exist below iOS 16, and it crashes. A field whose layout is fixed does not set the flag, even if its type only exists on a newer OS. Class references are an example (e.g. var x: SomeIOS16Class?). Such a class is realized from static metadata only and does not crash. This is what we observed, not a documented guarantee.
Workaround: erase the field so the layout is fixed
@available(iOS16,*)classFoo:NSObject{privatevar_bar:Any? // fixed-size existential, no runtime layout needed
varbar:CapturedRoom?{get{_baras?CapturedRoom}set{_bar=newValue}}}
Foo loses the 0x40 flag, stays a normal ObjC-visible class, and call sites don't change. This matters when the generic parameter isn't an option, e.g. the class is used from Objective-C, or changing it to Foo<T> would ripple through every use site. Storing a closure (() -> CapturedRoom) or a class reference works the same way.
Pitfall: a generic superclass does not always help
classBase<T>:NSObject{}@available(iOS16,*)classFoo:Base<Int>{varbar:CapturedRoom?} // still crashes
A non-generic class with a generic (or resilient) ancestor becomes an ObjC stub class. It moves from __objc_classlist to __objc_stublist, and realizeAllClasses initializes stub classes too, so it still crashes. Likewise, a subclass of UIHostingController<SomeIOS16View> crashes even with no stored properties, because its superclass's generic argument is missing.
How to spot affected classes in a binary:
classes whose class_ro_t flags include 0x40 (e.g. via otool -ov), plus every entry in __objc_stublist. Any of them that store, or inherit with, a type newer than the deployment target will crash.
Activity
theblixguy commentedon Sep 21, 2022
CC @mikeash
mikeash commentedon Sep 21, 2022
This is, unfortunately, a known problem. The ObjC runtime doesn't know about the limited availability and tries to instantiate the class even though it won't work on the older OS version.
If possible, avoid
objc_getClassListandobjc_copyClassList. In addition to this problem, they're extremely inefficient and it's usually better to maintain an explicit list of the classes you're interested in.It's also possible to work around this problem by adding an unused generic parameter to the class.
@available(iOS 16, *) class Foo<T>: NSObjectwill hide it from the ObjC runtime so it will no longer try to instantiateFoo.kemchenj commentedon Sep 22, 2022
Ok, look likes I should go with the generic parameter way. Thanks for your explanation.
liuxuan30 commentedon Nov 6, 2026
A new approach to workaround
We hit this in production (a third-party SDK calls
objc_copyClassList, so we can't avoid the call), and found a second workaround besides the unused generic parameter.Sharing in case it could help others.
When does it actually crash?
Not every
@availableclass crashes. It crashes only when the class needs runtime metadata initialization, i.e. its layout can't be computed at compile time. In the sample,CapturedRoomis a non-frozen struct from another module, soFoo's field offsets are only known at runtime. The compiler then emits a metadata initializer and setsRO_HAS_SWIFT_INITIALIZER(0x40) on the class.objc_getClassList/objc_copyClassListcallrealizeAllClasses, which runs that initializer. The initializer asks forCapturedRoom's metadata, which doesn't exist below iOS 16, and it crashes. A field whose layout is fixed does not set the flag, even if its type only exists on a newer OS. Class references are an example (e.g.var x: SomeIOS16Class?). Such a class is realized from static metadata only and does not crash. This is what we observed, not a documented guarantee.Workaround: erase the field so the layout is fixed
Fooloses the0x40flag, stays a normal ObjC-visible class, and call sites don't change. This matters when the generic parameter isn't an option, e.g. the class is used from Objective-C, or changing it toFoo<T>would ripple through every use site. Storing a closure (() -> CapturedRoom) or a class reference works the same way.Pitfall: a generic superclass does not always help
A non-generic class with a generic (or resilient) ancestor becomes an ObjC stub class. It moves from
__objc_classlistto__objc_stublist, andrealizeAllClassesinitializes stub classes too, so it still crashes. Likewise, a subclass ofUIHostingController<SomeIOS16View>crashes even with no stored properties, because its superclass's generic argument is missing.How to spot affected classes in a binary:
classes whose
class_ro_tflags include0x40(e.g. viaotool -ov), plus every entry in__objc_stublist. Any of them that store, or inherit with, a type newer than the deployment target will crash.chore(ai): add Mutex TODO and Swift issue links
fix(ai): replace Mutex to fix crash on iOS < 18 (#17024)