在上一篇文章 底层原理(07)中,分析了方法的快速查找流程即缓存查找,如果缓存中没有找到就会。。。下面就会进入方法慢速查找流程。。。
一. __objc_msgSend_uncached
在当前类中,缓存查找流程中如果没有到目标方法,跳转 MissLabelDynamic
流程, MissLabelDynamic
= __objc_msgSend_uncached
,搜索__objc_msgSend_uncached
并找到入口,真机汇编
代码如下:
.endmacro
STATIC_ENTRY __objc_msgSend_uncached
UNWIND __objc_msgSend_uncached, FrameWithNoSaves
// THIS IS NOT A CALLABLE C FUNCTION
// Out-of-band p15 is the class to search
MethodTableLookup
TailCallFunctionPointer x17
END_ENTRY __objc_msgSend_uncached
复制代码
这里 TailCallFunctionPointer
接收个参数 x17
,TailCallFunctionPointer
里做了什么? 找到它:
.macro TailCallFunctionPointer
// $0 = 函数指针的值
// $0 = function pointer value
br $0
.endmacro
复制代码
TailCallFunctionPointer
就一行汇编代码 br $0
.因为 $0
= p17
,br $0
的意思读取 p17
寄存器中的地址并且跳转到该地址,因为我们是在查询方法就是根据 sel
找 imp
,在根据TailCallFunctionPointer
提示。猜测 p17
寄存器应该存的是imp
p17
寄存器在__objc_msgSend_uncached
没有赋值的地方,那么只能在MethodTableLookup
赋值。代码如下
.macro MethodTableLookup
SAVE_REGS MSGSEND
// lookUpImpOrForward(obj, sel, cls, LOOKUP_INITIALIZE | LOOKUP_RESOLVER)
// receiver and selector already in x0 and x1
// x16(class) 赋值给 x2
mov x2, x16
// x3 = 3
mov x3, #3
// bl:b:跳转 l:链接寄存器
// 在跳转到_lookUpImpOrForward之前,
// 将下一条指令的地址保存到 lr寄存器中,既将(mov x17, x0)的指令地址保存在lr中
// 当_lookUpImpOrForwar执行完以后,执行lr寄存器中的地址,即:mov x17, x0
bl _lookUpImpOrForward
// IMP in x0
// x0 中保存了 _lookUpImpOrForward 的返回值,即:找到的imp
mov x17, x0
RESTORE_REGS MSGSEND
.endmacro
复制代码
通过_lookUpImpOrForward
查询到imp
,将imp
赋值给x17
寄存器。
到这里已经对 __objc_msgSend_uncached
流程有了大概的了解,即:
__objc_msgSend_uncached
-->MethodTableLookup
-->_lookUpImpOrForward
(返回imp) -->TailCallFunctionPointer x17
(x17 = imp) -->br $0
调用imp
二. lookUpImpOrForward
2.1 lookUpImpOrForward
源码分析
上面知道,_lookUpImpOrForward
返回了 imp
。全局搜索_lookUpImpOrForward
,发现汇编里面没有_lookUpImpOrForward
的定义或者实现,全局搜索 lookUpImpOrForward
找到了 c++ 代码:
// behavior = 3
IMP lookUpImpOrForward(id inst, SEL sel, Class cls, int behavior)
{
// 消息转发 imp
const IMP forward_imp = (IMP)_objc_msgForward_impcache;
IMP imp = nil;
Class curClass;
runtimeLock.assertUnlocked();
// 判断类是否已初始化
// 发送给类的第一条消息通常是+new或+alloc或+self会初始化类
if (slowpath(!cls->isInitialized())) {
// LOOKUP_NOCACHE = 8
// 没有初始化 behavior = 3|8 = 11
behavior |= LOOKUP_NOCACHE;
}
// 加锁防止多线程访问出现错乱
runtimeLock.lock();
// 是否注册类 是否被dyld加载的类
checkIsKnownClass(cls);
// 实现类包括实现isa走位中的父类和元类
// 初始化类和父类
cls = realizeAndInitializeIfNeeded_locked(inst, cls, behavior & LOOKUP_INITIALIZE);
runtimeLock.assertLocked();
curClass = cls;
// 获取锁后,代码再次查找类的缓存,但绝大多数情况下,证据表明大部分时间都未命中,因此浪费时间。
// 唯一没有执行某种缓存查找的代码路径就是class_getInstanceMethod()。
for (unsigned attempts = unreasonableClassCount();;) {
// 判断是否有共享缓存缓存优化,一般是系统的方法比如NSLog,一般的方法不会走
if (curClass->cache.isConstantOptimizedCache(/* strict */true)) {
#if CONFIG_USE_PREOPT_CACHES
// 再一次查询共享缓存,因为可能在你查询过程中
// 别的线程调用了这个方法,共享缓存中有了直接去查询
imp = cache_getImp(curClass, sel);
// 如果imp存在即缓存中有 跳转到done_unlock流程
if (imp) goto done_unlock;
curClass = curClass->cache.preoptFallbackClass();
#endif
} else {
// curClass method list.
// 在curClass类中采用二分查找算法查找methodlist
method_t *meth = getMethodNoSuper_nolock(curClass, sel);
// 如果找到了sel对应的方法
if (meth) {
// 获取对应的imp
imp = meth->imp(false);
// 跳转到 done 流程
goto done;
}
// 直到 superCls 为nil,走if里面的流程:imp = forward_imp;
if (slowpath((curClass = curClass->getSuperclass()) == nil)) {
// 没找到方法实现,使用 forward_imp
imp = forward_imp;
break;
}
}
// 如果父类中存在循环则停止
if (slowpath(--attempts == 0)) {
_objc_fatal("Memory corruption in class list.");
}
// Superclass cache.
// 去父类的缓存中查找imp
imp = cache_getImp(curClass, sel);
// 如果父类返回的是forward_imp 停止查找,那么就跳出循环
if (slowpath(imp == forward_imp)) {
break;
}
// 如果缓存中有就跳转done流程
if (fastpath(imp)) {
goto done;
}
}
// 如果查询方法的没有实现,系统会尝试一次方法解析
// behavior = 3,LOOKUP_RESOLVER = 2;
// 3 & 2 = 2,条件成立,进入 if {}
if (slowpath(behavior & LOOKUP_RESOLVER)) {
// behavior = behavior ^ LOOKUP_RESOLVER = 3 ^ 2 = 1
behavior ^= LOOKUP_RESOLVER;
// 动态方法决议
return resolveMethod_locked(inst, sel, cls, behavior);
}
done:
// behavior = 3,LOOKUP_NOCACHE = 8;0011 & 1000 = 0
// 条件满足
if (fastpath((behavior & LOOKUP_NOCACHE) == 0)) {
#if CONFIG_USE_PREOPT_CACHES
while (cls->cache.isConstantOptimizedCache(/* strict */true)) {
cls = cls->cache.preoptFallbackClass();
}
#endif
// 将查询到的sel和imp插入到缓存 注意:插入的是当前类的缓存
log_and_fill_cache(cls, imp, sel, inst, curClass);
}
done_unlock:
// 解锁
runtimeLock.unlock();
// 如果 (behavior & LOOKUP_NIL)成立则 behavior != LOOKUP_NIL
// 且imp == forward_imp 没有查询到直接返回 nil\
if (slowpath((behavior & LOOKUP_NIL) && imp == forward_imp)) {
return nil;
}
return imp;
}
复制代码
2.2 慢速查找流程总结
- 是否注册类,如果没有直接报错
- 是否实现
cls
,如果没有实现,则先去实现类
以及相关的isa走位链
和isa继承链
中类的实现,目的是方法查找时到父类中去查询 - 类是否初始化,如果没有则初始化,这一步我觉着是创建类对象比如调用类方法时,就是类对象调用实例方法
cls
开始遍历查询
- 判断是否有共享缓存,目的是有可能在查过过程中这个方法被调用缓存了,如果有的话直接从缓存中取,没有共享缓存则开始到本类中查询
- 在类中采用二分查找算法查找
methodlist
中的方法,如果找到插入缓存中,循环结束
父类
缓存中查询
- 如果父类中存在循环则终止查询,跳出循环
- 此时
curClass
=superclass
,到父类的缓存中找,如果找到则插入到本类
的缓存中。如果父类中返回的是forward_imp
则跳出遍历,执行消息转发 - 如果本类中没有找到此时的
curClass
=superclass
进入和cls
类相同的查找流程进行遍历循环,直到curClass
=nil
,imp
=forward_imp
进行消息转发
动态方法决议
- 如果
cls
以及父类
都没有查询到,此时系统会给你一次机会,判断是否执行过动态方法决议,如果没有则走动态方法决议 - 如果动态决议方法执行过,
imp
=forward_imp
会走done
流程插入缓存
,会走done_unlock
流程return imp
进入消息转发阶段
lookUpImpOrForward
流程图
2.3 realizeAndInitializeIfNeeded_locked
实现和实例化
static Class
realizeAndInitializeIfNeeded_locked(id inst, Class cls, bool initialize)
{
runtimeLock.assertLocked();
// !cls->isRealized()小概率发生 cls->isRealized()大概率是YES
// 判断类是否实现 目的是实现isa走位图中的isa走位链和父类链
if (slowpath(!cls->isRealized())) {
cls = realizeClassMaybeSwiftAndLeaveLocked(cls, runtimeLock);
// runtimeLock may have been dropped but is now locked again
}
// 类是否初始化 没有先去初始化
if (slowpath(initialize && !cls->isInitialized())) {
cls = initializeAndLeaveLocked(cls, inst, runtimeLock);
// runtimeLock may have been dropped but is now locked again
// If sel == initialize, class_initialize will send +initialize and
// then the messenger will send +initialize again after this
// procedure finishes. Of course, if this is not being called
// from the messenger then it won't happen. 2778172
}
return cls;
}
复制代码
-
realizeClassMaybeSwiftAndLeaveLocked
方法中的realizeClassWithoutSwift
就是去实现类的isa
走位链和继承链中相关的类 -
initializeAndMaybeRelock
的initializeNonMetaClass
就是初始化类和父类的
2.4 分法查找算法
ALWAYS_INLINE static method_t *
findMethodInSortedMethodList(SEL key, const method_list_t *list, const getNameFunc &getName)
{
ASSERT(list);
auto first = list->begin(); //第一个method的位置
auto base = first;
decltype(first) probe; //相当于mid
//把key直接转换成uintptr_t 因为修复过后的method_list_t中的元素是排过序的
uintptr_t keyValue = (uintptr_t)key;
uint32_t count;
//count = 数组的个数 (count >>= 1 = count = count >> 1)
//count >> 1 就相当于(count/2) 取整
//1.假如 count = list->count = 8 //2 count = 7 >> 1 = 3
for (count = list->count; count != 0; count >>= 1) {
/*
1. 首地址 + (下标) //地址偏移 中间值 probe = base + 4
2. 中间值 probe = base(首地址)+ 6
*/
probe = base + (count >> 1);
//获取中间的sel的值也是强转后的值
uintptr_t probeValue = (uintptr_t)getName(probe);
if (keyValue == probeValue) {// 如果 目标key == 中间位置的key 匹配成功
//分类覆盖,分类中有相同名字的方法,如果有分类的方法我们就获取分类的方法,多个分类看编译的顺序
while (probe > first && keyValue == (uintptr_t)getName((probe - 1))) {
probe--;
}
//返回方法的地址
return &*probe;
}
//如果 keyValue > 中间的位置的值
if (keyValue > probeValue) {
/*
1.base = probe + 1 = 4 + 1 = base(首地址) + 5 向上移 一位
2.base = probe + 1 ;向上移 一位
*/
base = probe + 1;
// 8 -1 = 7 因为比过一次没中 然后循环
count--;
}
}
return nil;//查询完没找到返回nil
}
复制代码
方法列表中的方法是经过修复的,意思就是按照sel
大小进行过排序的
二分法查找算法
其实就是每次找到范围内的中间
位置和keyValue
比较,如果相等直接返回查找到的方法(当然如果有分类方法就返回分类方法)- 如果不相等则继续二分法查询,不断缩小查询的范围,如果最后还是没有查询到则返回
nil
2.5 cache_getImp
快速查找流程是汇编实现的
STATIC_ENTRY _cache_getImp
// 获取 isa 放到 p16,这里 needs_auth = 0
GetClassFromIsa_p16 p0, 0
// 查找,注意这里 Mode = GETIMP,
CacheLookup GETIMP, _cache_getImp, LGetImpMissDynamic, LGetImpMissConstant
// 缓存未命中,返回0
LGetImpMissDynamic:
mov p0, #0
ret
END_ENTRY _cache_getImp
复制代码
.macro GetClassFromIsa_p16 src, needs_auth, auth_address
/* note: auth_address is not required if !needs_auth */
#if SUPPORT_INDEXED_ISA // armv7k or arm64_32
...//省略
1:
#elif __LP64__
.if \needs_auth == 0 // _cache_getImp takes an authed class already
mov p16, \src
.else //needs_auth = 1 所以走下面的流程
// 64-bit packed isa
//把 \src 和 \auth_address 传进ExtractISA 得到的结果赋值给p16寄存器
ExtractISA p16, \src, \auth_address
.endif
#else
// 32-bit raw isa
mov p16, \src
#endif
复制代码
直接走needs_auth
==0
,p0
=curClass
。把p0
寄存器的值赋值给p16
寄存器,p16
= curClass
CacheLookup
方法前面已经探究过了在这不细说了CacheLookup GETIMP
, _cache_getImp
, LGetImpMissDynamic
, LGetImpMissConstant
- 如果缓存没有命中走
LGetImpMissDynamic
流程 - 如果缓存命中
Mode
=GETIMP
.macro CacheHit
.if $0 == NORMAL
TailCallCachedImp x17, x10, x1, x16 // authenticate and call imp
.elseif $0 == GETIMP
mov p0, p17
cbz p0, 9f //如果imp = 0直接跳转9流程 return 0
AuthAndResignAsIMP x0, x10, x1, x16 // authenticate imp and re-sign as IMP 9:
ret
复制代码
p17
=imp
,把p17
寄存器的值赋值给p0
寄存器,x0
=p0
=imp
- 如果
imp
=0
直接跳转9
流程return 0
AuthAndResignAsIMP
也是一个宏
,对imp
进行解码,拿到解码后的imp
返回
.macro AuthAndResignAsIMP
// $0 = cache imp , $1 = buckets的地址, $2 = SEL $3 = class
// $0 = $0 ^ $3 = imp ^ class = 解码后的imp
eor $0, $0, $3
.endmacro
复制代码
缓存中获取imp
是编码过的,此时imp
^ class
= 解码后的imp
三 实例查找
创建一个 YJPerson
类,声明个实例方法 say1
,不实现 say1
,然后调用:
奔溃:
unrecognized
经典的崩溃信息,[perosn say1]
也走了快速查找流程,慢速查找流程,动态方法决议,最后消息转发,最后还是没找到报unrecognized
,全局搜索doesNotRecognizeSelector
或者unrecognized selector sent to instance
,在源码中搜索:
// Replaced by CF (throws an NSException)
+ (void)doesNotRecognizeSelector:(SEL)sel {
_objc_fatal("+[%s %s]: unrecognized selector sent to instance %p",
class_getName(self), sel_getName(sel), self);
}
// Replaced by CF (throws an NSException)
- (void)doesNotRecognizeSelector:(SEL)sel {
_objc_fatal("-[%s %s]: unrecognized selector sent to instance %p",
object_getClassName(self), sel_getName(sel), self);
}
复制代码
最后怎么调用doesNotRecognizeSelector
后面在 消息转发篇章 会做详细的说明
分类测试 创建有个 NSObject+YJ
分类里面添加一个对象方法 testMethod
,并在分类中实现
YJPerson
类调用对象方法调用成功了,为什么?因为在 OC
底层没有所谓的对象方法和类方法之分,对于底层来说都是对象方法;获取一个类方法实际上就是获取元类的对象方法:
Method class_getClassMethod(Class cls, SEL sel)
{
if (!cls || !sel) return nil;
// 返回元类的实例方法
return class_getInstanceMethod(cls->getMeta(), sel);
}
复制代码
这里查找流程:
YJPerson类
没有 -->YJPerson元类
没有 -->NSObject根元类
没有 -->NSObject根类
--> 找到了