Skip to content

objc_getClassList crashed with an iOS 16 available class, in device that lower than iOS 16 #61215

Open

Description

@kemchenj

Describe the bug

objc_getClassList crash in the code below:

#if canImport(RoomPlan)

import RoomPlan

@available(iOS 16, *)
class Foo: NSObject {
  var bar: CapturedRoom?
}

#endif

import SwiftUI

@main
struct MetadataAccessorCrashApp: App {
  var body: some Scene {
    WindowGroup {
      Text("")
        .onAppear {
          // Crash here
          let _ = objc_getClassList(nil, 0)
        }
    }
  }
}

Steps To Reproduce
Steps to reproduce the behavior:

  1. Clone the repository https://github.com/kemchenj/TypeMetadataAccessorCrash
  2. Open MetadataAccessorCrash.xcodeproj project
  3. Build and run on any iOS simulator or device that OS version lower than iOS 16 (e.g. iOS 15.5)
  4. App will crash when objc_getClassList was called

Expected behavior

Not crash.

Screenshots

Stacktrace:

Screen Shot 2022-09-21 at 09 11 10

Environment (please fill out the following information)

  • OS: macOS
  • Xcode 14.0

Additional context

Activity

  1. added
    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.
    on Sep 21, 2022
  2. theblixguy commented on Sep 21, 2022

    @theblixguy
    Member
  3. mikeash commented on Sep 21, 2022

    @mikeash
    Contributor

    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.

  4. kemchenj commented on Sep 22, 2022

    @kemchenj
    Author

    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.

  5. liuxuan30 commented on Nov 6, 2026

    @liuxuan30

    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 @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(iOS 16, *) 
    class Foo: NSObject { 
      private var _bar: Any? // fixed-size existential, no runtime layout needed 
      var bar: CapturedRoom? { 
        get { _bar as? 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

    class Base<T>: NSObject {} 
    
    @available(iOS 16, *) 
    class Foo: Base<Int> { var bar: 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址