美文网首页
方法的查找流程

方法的查找流程

作者: Easting | 来源:发表于2019-12-28 20:37 被阅读0次

    在前面的对象的本质探索中我们可以通过clang -rewrite-objc main.o可以编译出main.cpp文件出来。

    探索消息发送的本质

    1. 我们在main.m文件里面定义一个sayHello方法
    int main(int argc, const char * argv[]) {
            @autoreleasepool {
            XDPerson *person = [XDPerson alloc];
            [person sayHello];
          }
          return 0;
    }
    
    1. clang这个main.m文件之后,我们可以在相应的main.cpp文件里面可以看到
    int main(int argc, const char * argv[]) {
            /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; 
              XDPerson *person = ((XDPerson *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("XDPerson"), sel_registerName("alloc"));
    
              ((void (*)(id, SEL))(void *)objc_msgSend)((id)person, sel_registerName("sayHello"));
            }
            return 0;
    }
    
    1. 我们把第2步的main函数里面的sayHello函数抽出来然后精简一下

    objc_msgSend(person,sel_registerName("sayHello"));

    结论:

    • 消息发送的本质就是调用objc_msgSend函数;
    • objc_msgSend函数的参数, 对象(或者是类对象) + 方法名

    探索objc_msgSend的查找流程

    objc_msgSend查找流程由以下两部分组成:

    • 快速查找流程;
    • 慢速查找流程。

    快速查找流程

    1. 汇编断点查看,可以看到在汇编底层在call objc_msgSend
    XDTest`main:
        0x100000d40 <+0>:   pushq  %rbp
        ...省略部分信息...
        0x100000d8b <+75>:  callq  *0x27f(%rip)   ; (void *)0x0000000100343bc0: objc_msgSend
        ...省略部分信息...
        0x100000db0 <+112>: popq   %rbp
        0x100000db1 <+113>: retq  
    
    1. 全局搜索可以再objc-msg-arm64.s文件中可以看到ENTRY objc_msgSend,查找从这里就开始了。
        ENTRY _objc_msgSend
        UNWIND _objc_msgSend, NoFrame
    
        cmp p0, #0          // nil check and tagged pointer check
    #if SUPPORT_TAGGED_POINTERS
        b.le    LNilOrTagged        //  (MSB tagged pointer looks negative)
    #else
        b.eq    LReturnZero
    #endif
        ldr p13, [x0]       // p13 = isa                   
        GetClassFromIsa_p16 p13     // p16 = class         
    LGetIsaDone:
        CacheLookup NORMAL
    

    主线流程介绍

    • cmp p0, #0检查是否为空,或者是否taggedPointer。 SUPPORT_TAGGED_POINTERS__LP64__宏定义的值为1。
      b.le LNilOrTagged实质上就是去check,但是我们知道肯定不会为Nill,并且是nontaggedpointer(后面会介绍)。
    • ldr p13, [x0]x0就是isa给到p13
    • GetClassFromIsa_p16 p13 顾名思义就是通过p13找到class
    • LGetIsaDone完成了之后 就开始CacheLookup,传入的参数是NORMAl

    上面就是查找的主线流程了,下面我们开始对主要的步骤进行探索。

    1. GetClassFromIsa_p16 p13探索
    .macro GetClassFromIsa_p16 /* src */
    #if SUPPORT_INDEXED_ISA
        ...省略部分信息...
    #elif __LP64__
        // 64-bit packed isa
        and p16, $0, #ISA_MASK
    #else
        ...省略部分信息...
    #endif
    .endmacro
    
    • SUPPORT_INDEXED_ISA查找宏定义就可以知道值为0.
    • and p16, $0, #ISA_MASK实际的入口就是这里。
      意思就是将第一个参数(即上面传入的isa)&ISA_MASK 之后赋值给p16,这就很明显的取到了我们的目标类(元类)了。
    1. LGetIsaDoneisa我们已经完成,开始CacheLookup入参NORMAL
    .macro CacheLookup        
        // p1 = SEL, p16 = isa
        ldp p10, p11, [x16, #CACHE] // p10 = buckets, p11 = occupied|mask
    #if !__LP64__
        and w11, w11, 0xffff    // p11 = mask
    #endif
        and w12, w1, w11        // x12 = _cmd & mask
        add p12, p10, p12, LSL #(1+PTRSHIFT)
                         // p12 = buckets + ((_cmd & mask) << (1+PTRSHIFT))
    
        ldp p17, p9, [x12]      // {imp, sel} = *bucket
    1:  cmp p9, p1          // if (bucket->sel != _cmd)
        b.ne    2f          //     scan more
        CacheHit $0         // call or return imp           
        
    2:  // not hit: p12 = not-hit bucket
        CheckMiss $0            // miss if bucket->sel == 0  
        cmp p12, p10        // wrap if bucket == buckets
        b.eq    3f
        ldp p17, p9, [x12, #-BUCKET_SIZE]!  // {imp, sel} = *--bucket
        b   1b          // loop
    
    3:  // wrap: p12 = first bucket, w11 = mask              
        add p12, p12, w11, UXTW #(1+PTRSHIFT)
                                    // p12 = buckets + (mask << 1+PTRSHIFT)
    
        // Clone scanning loop to miss instead of hang when cache is corrupt.
        // The slow path may detect any corruption and halt later.
    
        ldp p17, p9, [x12]      // {imp, sel} = *bucket
    1:  cmp p9, p1          // if (bucket->sel != _cmd)
        b.ne    2f          //     scan more
        CacheHit $0         // call or return imp
        
    2:  // not hit: p12 = not-hit bucket
        CheckMiss $0            // miss if bucket->sel == 0
        cmp p12, p10        // wrap if bucket == buckets
        b.eq    3f
        ldp p17, p9, [x12, #-BUCKET_SIZE]!  // {imp, sel} = *--bucket
        b   1b          // loop
    
    3:  // double wrap
        JumpMiss $0
        
    .endmacro
    

    看到这里我们头皮发麻,不要慌,我们开始对第2阶段一步一步分析。

    1. ldp p10, p11, [x16, #CACHE],我们通过平移指针的地址16个字节之后可以找到objc_class结构体里面的cache_t指针。这里可以通过objec_class: cache_t分析这个章节看到结构体的相关信息与介绍。
      #CACHE是一个宏定义且值为8,然后一分为两个内存段。
    1. 0000 1222 就给p10实际上就是buckets指针赋值。
    2. 0023 0033 就给p11实际上就是occupied|mask赋值。mask属性在结构体里面是在occupied属性的前面,但是赋值在后面的原因是因为iOS是小端模式。
    1. 通过hash函数、平移、取值一列操作,找打当前sel对应hash表里面的imp.
        and w12, w1, w11        // x12 = _cmd & mask
        add p12, p10, p12, LSL #(1+PTRSHIFT)
                         // p12 = buckets + ((_cmd & mask) << (1+PTRSHIFT))
        ldp p17, p9, [x12]      // {imp, sel} = *bucket
    
    1. 接下来的流程就和cache_t里面的查找流程相同,找不到就开始递归查找,直到找到为止。上面我们可以看到会有两次123这样的流程,第二次123就是防止多线程调用的时候给的一次容错机会。
    2. 我们主要看cacheHit cacheMiss JumpMiss
    • cacheHit 缓存命中,表示找到了sel对应的imp,直接返回imp
    • cacheMiss当前对应的bucket没找到,继续递归查找。
    • JumpMiss递归查找介绍,没有缓存命中,跳出当前流程。
    1. JumpMiss 流程 参数$0就是NORMAL
    .macro JumpMiss
    .if $0 == GETIMP
        b   LGetImpMiss
    .elseif $0 == NORMAL
        b   __objc_msgSend_uncached
    .elseif $0 == LOOKUP
        b   __objc_msgLookup_uncached
    .else
    .abort oops
    .endif
    .endmacro
    

    我们看直接进入__objc_msgSend_uncached

    1. __objc_msgSend_uncached探索
        STATIC_ENTRY __objc_msgSend_uncached      
        UNWIND __objc_msgSend_uncached, FrameWithNoSaves
    
        MethodTableLookup       
        TailCallFunctionPointer x17
    
        END_ENTRY __objc_msgSend_uncached
    

    MethodTableLookup查找

    1. MethodTableLookup探索
    .macro MethodTableLookup
        
        // push frame
        ...省略部分信息...
    
        // save parameter registers: x0..x8, q0..q7
        ...省略部分信息...
    
        // receiver and selector already in x0 and x1
        mov x2, x16
        bl  __class_lookupMethodAndLoadCache3  
    
        // IMP in x0
        mov x17, x0
        
        // restore registers and return
        ...省略部分信息...
        AuthenticateLR
    
    .endmacro
    

    一顿疯狂的汇编操作,最后的流程就是到了__class_lookupMethodAndLoadCache3这个函数里面,这个时候我们就继续搜索这么一个函数,会发现搜索不到它了。

    其实到这里为止,汇编的快速查找流程已经结束了。下面附上快速查找主线流程图。 方法快速查找流程.png

    慢速查找流程

    在上面快速查找流程中,没有缓存命名就会走__class_lookupMethodAndLoadCache3这个函数里面,那么它对应在慢速查找流程中的哪个函数呢,下面我们通过汇编调试。

    libobjc.A.dylib`_objc_msgSend_uncached:
        0x100344610 <+0>:   pushq  %rbp
        ...省略部分信息...
    ->  0x10034464f <+63>:  callq  0x100344ad0   ; ::_class_lookupMethodAndLoadCache3(id, SEL, Class) at objc-runtime-new.mm:5246
    ...省略部分信息...
    

    通过调试 我们看到了c++函数_class_lookupMethodAndLoadCache3

    加下来我们开始探索_class_lookupMethodAndLoadCache3

    1. 看源码
    IMP _class_lookupMethodAndLoadCache3(id obj, SEL sel, Class cls)
    {
        return lookUpImpOrForward(cls, sel, obj, 
                                  YES/*initialize*/, NO/*cache*/, YES/*resolver*/);
    }
    
    1. lookUpImpOrForward,这是什么意思?查找imp或者forward转发
    IMP lookUpImpOrForward(Class cls, SEL sel, id inst, 
                           bool initialize, bool cache, bool resolver)
    {
        IMP imp = nil;
        bool triedResolver = NO;
    
        runtimeLock.assertUnlocked();
    
        // Optimistic cache lookup
        if (cache) {
            imp = cache_getImp(cls, sel);
            if (imp) return imp;
        }
    
        runtimeLock.lock();
        checkIsKnownClass(cls);
    
        if (!cls->isRealized()) {
            realizeClass(cls);
        }
    
        if (initialize  &&  !cls->isInitialized()) {
            runtimeLock.unlock();
            _class_initialize (_class_getNonMetaClass(cls, inst));
            runtimeLock.lock();
        }
    
     retry:    
        runtimeLock.assertLocked();
    
        imp = cache_getImp(cls, sel);
        if (imp) goto done;
    
        // Try this class's method lists.
        {
            Method meth = getMethodNoSuper_nolock(cls, sel);
            if (meth) {
                log_and_fill_cache(cls, meth->imp, sel, inst, cls);
                imp = meth->imp;
                goto done;
            }
        }
    
        // Try superclass caches and method lists.
        {
            unsigned attempts = unreasonableClassCount();
            for (Class curClass = cls->superclass;
                 curClass != nil;
                 curClass = curClass->superclass)
            {
                // Halt if there is a cycle in the superclass chain.
                if (--attempts == 0) {
                    _objc_fatal("Memory corruption in class list.");
                }
                
                // Superclass cache.
                imp = cache_getImp(curClass, sel);
                if (imp) {
                    if (imp != (IMP)_objc_msgForward_impcache) {
                        // Found the method in a superclass. Cache it in this class.
                        log_and_fill_cache(cls, imp, sel, inst, curClass);
                        goto done;
                    }
                    else {
                        break;
                    }
                }
                
                // Superclass method list.
                Method meth = getMethodNoSuper_nolock(curClass, sel);
                if (meth) {
                    log_and_fill_cache(cls, meth->imp, sel, inst, curClass);
                    imp = meth->imp;
                    goto done;
                }
            }
        }
    
        // No implementation found. Try method resolver once.
    
        if (resolver  &&  !triedResolver) {
            runtimeLock.unlock();
            _class_resolveMethod(cls, sel, inst);
            runtimeLock.lock();
            triedResolver = YES;
            goto retry;
        }
    
        // No implementation found, and method resolver didn't help. 
        // Use forwarding.
    
        imp = (IMP)_objc_msgForward_impcache;
        cache_fill(cls, sel, imp, inst);
    
     done:
        runtimeLock.unlock();
    
        return imp;
    }
    

    这一步就是我们消息慢速查找的流程,接下来对第2步我们逐步分析

    瓦解lookUpImpOrForwad
    1. 先看是否有缓存,有就直接返回
    if (cache) {
            imp = cache_getImp(cls, sel);
            if (imp) return imp;
        }
    
    1. runtimeLock.lock();分析这里加锁的原因

      反正查找方法A的时候,查找方法B进来,导致返回的imp错误。

    2. 检查和准备类

    checkIsKnownClass(cls);
    
        if (!cls->isRealized()) {
            realizeClass(cls);
        }
    

    checkIsKnownClass(cls);检查类是否在内存中存在
    realizeClass(cls);准备该类的方法,并准备到class_re_t这个结构体里面,这里就不贴相应的代码了,可以自己去查看。

    1. 准备就绪,开始从类里面去查找方法
    {
            Method meth = getMethodNoSuper_nolock(cls, sel);
            if (meth) {
                log_and_fill_cache(cls, meth->imp, sel, inst, cls);
                imp = meth->imp;
                goto done;
            }
    }
    
    • {}这里括号代表局部作用域;
    • 从当前类里面找,找到之后cache起来;
    • getMethodNoSuper_nolocklog_and_fill_cache会在下面介绍。

    5 自己方法里面没有找到就开始从父类里面去查找了。

    // Try superclass caches and method lists.
    {
      ...省略部分信息...
    }
    

    代码部分省略,和第4步的流程相同,这里我们要熟记isa的指向分析与类的继承关系,会很好的帮助到我们的理解,在isa初始化&指向分析这篇文章里面已经做了介绍,

    到这里为止慢速查找的流程已经结束了。现在我们对第4步中两个重要的函数分析

    1. getMethodNoSuper_nolock 两个入参cls sel
    static method_t *
    getMethodNoSuper_nolock(Class cls, SEL sel)
    {
        for (auto mlists = cls->data()->methods.beginLists(), 
                  end = cls->data()->methods.endLists(); 
             mlists != end;
             ++mlists)
        {
            method_t *m = search_method_list(*mlists, sel);
            if (m) return m;
        }
        return nil;
    }
    

    class_rw_t结构体的methods开始遍历

    • 分析search_method_list
    static method_t *search_method_list(const method_list_t *mlist, SEL sel)
    {
        int methodListIsFixedUp = mlist->isFixedUp();
        int methodListHasExpectedSize = mlist->entsize() == sizeof(method_t);
        
        if (__builtin_expect(methodListIsFixedUp && methodListHasExpectedSize, 1)) {
            return findMethodInSortedMethodList(sel, mlist);
        } else {
            // Linear search of unsorted method list
            for (auto& meth : *mlist) {
                if (meth.name == sel) return &meth;
            }
        }
    
        return nil;
    }
    

    这里分了两步

    • findMethodInSortedMethodList有序查找,有序查找对应的算法是二分法,同样也是根据name来判断。
    • else里面是针对无序的结构,直接遍历查找,name相同直接返回
    1. log_and_fill_cache分析
    static void
    log_and_fill_cache(Class cls, IMP imp, SEL sel, id receiver, Class implementer)
    {
    #if SUPPORT_MESSAGE_LOGGING
        if (objcMsgLogEnabled) {
            bool cacheIt = logMessageSend(implementer->isMetaClass(), 
                                          cls->nameForLogging(),
                                          implementer->nameForLogging(), 
                                          sel);
            if (!cacheIt) return;
        }
    #endif
        cache_fill (cls, sel, imp, receiver);
    }
    

    SUPPORT_MESSAGE_LOGGING宏定义在我们iOS的情况下为0,
    走向cache_fill,这里就与objec_class: cache_t分析得分析衔接上来了。

    到这里我们的慢速查找流程结束了,下面附上慢速查找主线流程图。


    方法慢速查找流程图.png

    接下来就要开始forward转发的流程,下一篇文章中继续探索。

    相关文章

      网友评论

          本文标题:方法的查找流程

          本文链接:https://www.haomeiwen.com/subject/fnmsoctx.html