OC底层-方法的本质探究之方法的慢速查找

679

慢速查找的引入和定位

  • 上篇文章分析了,方法的本质是消息的发送,并主要探究了方法的快速查找流程,即去类的缓存中查找。如果缓存中没有查找到,就会JumpMiss,进入__objc_msgSend_uncached流程。
  • __objc_msgSend_uncached,会走方法列表查询流程MethodTableLookup。汇编源码如下
STATIC_ENTRY __objc_msgSend_uncached
UNWIND __objc_msgSend_uncached, FrameWithNoSaves

// THIS IS NOT A CALLABLE C FUNCTION
// Out-of-band p16 is the class to search
    
MethodTableLookup 
TailCallFunctionPointer x17

END_ENTRY __objc_msgSend_uncached
  • MethodTableLookup中最重要的方法,即慢速查找流程的重点_lookUpImpOrForward。这里有两个细节去找到_lookUpImpOrForward方法。
    • 搜索lookUpImpOrForward在汇编代码中,我们是没有找到的。这里可以猜测在底层c/ c++层,即objc层面实现了 lookUpImpOrForward。有一个经验分享:在汇编层方法是__两个下划线,到c/c++层去掉一个下划线_,到OC层再去掉一个下划线。
    • 也可以通过程序来验证汇编的流程找到lookUpImpOrForward在哪。具体操作为,
      • 开启汇编调试【Debug -- Debug worlflow -- 勾选Always show Disassembly】,运行程序
      • 运行到objc_msgSend,摁住ctrol+stepinto,进入objc_msgSend的汇编流程
      • 运行到_objc_msgSend_uncached,摁住ctrol+stepinto,进入_objc_msgSend_uncached的汇编流程
      • _objc_msgSend_uncached的汇编流程找到lookUpImpOrForward的位置。
.macro MethodTableLookup
	
	// push frame
	SignLR
	stp	fp, lr, [sp, #-16]!
	mov	fp, sp

	// save parameter registers: x0..x8, q0..q7
	sub	sp, sp, #(10*8 + 8*16)
	stp	q0, q1, [sp, #(0*16)]
	stp	q2, q3, [sp, #(2*16)]
	stp	q4, q5, [sp, #(4*16)]
	stp	q6, q7, [sp, #(6*16)]
	stp	x0, x1, [sp, #(8*16+0*8)]
	stp	x2, x3, [sp, #(8*16+2*8)]
	stp	x4, x5, [sp, #(8*16+4*8)]
	stp	x6, x7, [sp, #(8*16+6*8)]
	str	x8,     [sp, #(8*16+8*8)]

	// lookUpImpOrForward(obj, sel, cls, LOOKUP_INITIALIZE | LOOKUP_RESOLVER)
	// receiver and selector already in x0 and x1
	mov	x2, x16
	mov	x3, #3
	bl	_lookUpImpOrForward

	// IMP in x0
	mov	x17, x0
	
	// restore registers and return
	ldp	q0, q1, [sp, #(0*16)]
	ldp	q2, q3, [sp, #(2*16)]
	ldp	q4, q5, [sp, #(4*16)]
	ldp	q6, q7, [sp, #(6*16)]
	ldp	x0, x1, [sp, #(8*16+0*8)]
	ldp	x2, x3, [sp, #(8*16+2*8)]
	ldp	x4, x5, [sp, #(8*16+4*8)]
	ldp	x6, x7, [sp, #(8*16+6*8)]
	ldr	x8,     [sp, #(8*16+8*8)]

	mov	sp, fp
	ldp	fp, lr, [sp], #16
	AuthenticateLR

.endmacro

慢速查找的流程

  • lookUpImpOrForward 代码如下:
IMP lookUpImpOrForward(id inst, SEL sel, Class cls, int behavior)
{
    const IMP forward_imp = (IMP)_objc_msgForward_impcache;
    IMP imp = nil;
    Class curClass;

    runtimeLock.assertUnlocked();

    // Optimistic cache lookup
    if (fastpath(behavior & LOOKUP_CACHE)) {
        imp = cache_getImp(cls, sel);
        if (imp) goto done_nolock;
    }

    // runtimeLock is held during isRealized and isInitialized checking
    // to prevent races against concurrent realization.

    // runtimeLock is held during method search to make
    // method-lookup + cache-fill atomic with respect to method addition.
    // Otherwise, a category could be added but ignored indefinitely because
    // the cache was re-filled with the old value after the cache flush on
    // behalf of the category.

    runtimeLock.lock();

    // We don't want people to be able to craft a binary blob that looks like
    // a class but really isn't one and do a CFI attack.
    //
    // To make these harder we want to make sure this is a class that was
    // either built into the binary or legitimately registered through
    // objc_duplicateClass, objc_initializeClassPair or objc_allocateClassPair.
    //
    // TODO: this check is quite costly during process startup.
    checkIsKnownClass(cls);

    if (slowpath(!cls->isRealized())) {
        cls = realizeClassMaybeSwiftAndLeaveLocked(cls, runtimeLock);
        // runtimeLock may have been dropped but is now locked again
    }

    if (slowpath((behavior & LOOKUP_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
    }

    runtimeLock.assertLocked();
    curClass = cls;

    // The code used to lookpu the class's cache again right after
    // we take the lock but for the vast majority of the cases
    // evidence shows this is a miss most of the time, hence a time loss.
    //
    // The only codepath calling into this without having performed some
    // kind of cache lookup is class_getInstanceMethod().

    for (unsigned attempts = unreasonableClassCount();;) {
        // curClass method list.
        Method meth = getMethodNoSuper_nolock(curClass, sel);
        if (meth) {
            imp = meth->imp;
            goto done;
        }

        if (slowpath((curClass = curClass->superclass) == nil)) {
            // No implementation found, and method resolver didn't help.
            // Use forwarding.
            imp = forward_imp;
            break;
        }

        // Halt if there is a cycle in the superclass chain.
        if (slowpath(--attempts == 0)) {
            _objc_fatal("Memory corruption in class list.");
        }

        // Superclass cache.
        imp = cache_getImp(curClass, sel);
        if (slowpath(imp == forward_imp)) {
            // Found a forward:: entry in a superclass.
            // Stop searching, but don't cache yet; call method
            // resolver for this class first.
            break;
        }
        if (fastpath(imp)) {
            // Found the method in a superclass. Cache it in this class.
            goto done;
        }
    }

    // No implementation found. Try method resolver once.

    if (slowpath(behavior & LOOKUP_RESOLVER)) {
        behavior ^= LOOKUP_RESOLVER;
        return resolveMethod_locked(inst, sel, cls, behavior);
    }

 done:
    log_and_fill_cache(cls, imp, sel, inst, curClass);
    runtimeLock.unlock();
 done_nolock:
    if (slowpath((behavior & LOOKUP_NIL) && imp == forward_imp)) {
        return nil;
    }
    return imp;
}

一、多线程防护再查找缓存

if (fastpath(behavior & LOOKUP_CACHE)) {
        imp = cache_getImp(cls, sel);
        if (imp) goto done_nolock;
    }
  • 这里会再进行一次快速查找流程,如果查到IMP,就返回IMP。这里是对多线程的一个保护举动。如果慢速查找开始之前,有一个线程缓存了cache,就再查找一遍。

二、确定类

确定类是已知类

checkIsKnownClass(cls);

static void
checkIsKnownClass(Class cls)
{
    if (slowpath(!isKnownClass(cls))) {
        _objc_fatal("Attempt to use unknown class %p.", cls);
    }
}
  • 确定是已知类。当前传入的cls是否是已经存在和声明的类。如果不是,就会报错。

确定类已经实现

 if (slowpath(!cls->isRealized())) {
        cls = realizeClassMaybeSwiftAndLeaveLocked(cls, runtimeLock);
        // runtimeLock may have been dropped but is now locked again
    }
  • 确定当前类是否已经实现,如果没有实现,就对类的所有信息进行赋值,如前面分析的一样,realizeClassWithoutSwift方法包含了对类的class_rw_t,class_ro_t,superclass,initClassIsa,flags,确定的继承链和mete继承类等所有信息赋值,确定我们查询的类的信息的完整性。

确定类已经初始化

if (slowpath((behavior & LOOKUP_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
    }
  • 确定当前的类是否初始化,系统层会默认为我们初始化当前类。即lookUpImpOrNil(nil, @selector(initialize), cls, LOOKUP_INITIALIZE); 本质上是调用了initialize方法。

三、查找IMP

去本类的继承链方法列表查找

  • 核心循环,去继承链循环。首先去继承链的方法列表查询,核心方法getMethodNoSuper_nolock
  • getMethodNoSuper_nolock -> search_method_list_inline。这里有一个细节是对类的 cls->data()->methods()方法列表查询,属于双层循环查找。可以简单推测类的方法在类中的method_array_t中存储是二维数组。method_array_t -> method_list_t ->method_t
  • 方法的查找findMethodInSortedMethodList 这里采用了经典二分法查找方法列表。
  • 如果查询到sel,就返回IMP。并在当前类cache_fill缓存方法,便于下次的快速查找。没有就向下。

去继承链的缓存查找

  • 核心方法 cache_getImp
    • 如果父类的缓存中找到sel,就返回IMP。并在当前类cache_fill缓存方法,便于下次的快速查找。
    • 如果没有找到就继续递归到父类的父类,直到nil。imp = forward_imp跳出递归。
    • 有一种特殊情况,刚好父类缓存了本类的forward_imp,我们也不做处理,直接跳出。

动态方法决议查找

    if (slowpath(behavior & LOOKUP_RESOLVER)) {
        behavior ^= LOOKUP_RESOLVER;
        return resolveMethod_locked(inst, sel, cls, behavior);
    }
  • 如果继承链的循环方法列表里和继承链的缓存中都没有,系统为我们设置了一次补救机会,resolveMethod_locked动态方法决议。
    • 这里通过与或控制了进入的条件,如果没进行到动态方法决议,则执行动态方法决议

    • 如果执行过一次动态方法决议,则方法进入消息转发流程。

动态方法决议和消息的转发待续

  • 这里时间不是很充裕了,有点累了,下次再针对动态方法决议和消息的转发详细研究,再写博客。