objc_lookUpClass does not behave as documented

Number:rdar://FB10003729 Date Originated:May 1, 2022
Status:Open Resolved:Not yet
Product:Objective-C Runtime Product Version:
Classification:Incorrect/Unexpected Behavior Reproducible:Yes
The documentation for `objc_lookUpClass` states:

> objc_getClass(_:) is different from this function in that if the class is not registered, objc_getClass(_:) calls the class handler callback and then checks a second time to see whether the class is registered. *****This function does not call the class handler callback.*****

Not only does the documentation explicitly guarantee that this function shouldn’t call the class handler callbacks, it’s in fact the sole difference between `objc_lookUpClass` and `objc_lookUpClass`.

As it turns out, both APIs call the handler chain. This violates the documented behaviour, and makes these two functions are exactly identical in behaviour.

I’ve isolated the change as occurring between these two versions of the open-sourced ObjC runtime:

* https://opensource.apple.com/source/objc4/objc4-274/runtime/objc-runtime.m.auto.html
* https://opensource.apple.com/source/objc4/objc4-371/runtime/objc-runtime-new.m.auto.html

I’ve attached a really simple snippet that replicated the issue.


import ObjectiveC
import Foundation

var thePrevHook: objc_hook_getClass? = nil

func myGetClassHook(className: UnsafePointer<Int8>, outClass: AutoreleasingUnsafeMutablePointer<AnyClass?>) -> ObjCBool {
    fatalError("This should never have been called, because `objc_lookUpClass` is not supposed to call the class callbacks.")

objc_setHook_getClass(myGetClassHook, &thePrevHook)

let newClass: AnyClass? = objc_lookUpClass("NoClassExistsWithThisName") // 💥
print("This should be nil:", newClass as Any)



The documentation has been changed in Xcode 14. I've closed the radar.

Now objc_getClass and objc_lookUpClass are both documented to behave identically, and explicit references to the class handler chain have been removed.

By AlexanderMomchilov at June 25, 2022, 1:10 a.m. (reply...)

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!