c语言含括四个子函数,Runtime之objc_msgSend底层窥探上

在Object-c中,方法调用大家都会,但它的底层到底是怎么实现的呢,如何通过SEL来找到IMP的呢。开始我也不知道,幸亏苹果开源了runtime的部分源码,可以在里面找到答案。

SEL怎么找到IMP

objc_msgSend的过程大致是

1、先从接收者类的cache中查找方法,找到了直接返回IMP

2、没找到就从消息接收者的方法列表查找.

3、从Superclass的缓存列表找

4、从Superclass的方法列表找

5、动态方法解析

6、消息转发

下图是1-4的过程,5、6会会放在objc_msgSend底层窥探下分享

ff6d6304da35

消息发送过程

caches是快速查找,用的是汇编实现,再者慢速查找,

用C实现。为什么缓存的实现要用汇编来写呢?很多朋友觉得汇编比C的效率高啊,速度快。这种是其一,其二是C语言中,在一个函数里保护未知的参数,并且跳转到任一的函数指针不太可能实现,C语言并不含括实现这些需求的一些必要特性。

runtime源码下载地址https://opensource.apple.com/,我用得是MacOS 10.14版本,里面找到objc4-750下载,就是runtime源码。

打开runtime源码,通过搜索_objc_msgSend可以搜索到不同处理器的汇编文件,我这里用的是arm64,为什么objc_msgSend前面要加下划线呢,汇编里面的都加下划线,C/C++的可以去掉一个下划线,我也不清楚其用意,可能为了高大尚

ff6d6304da35

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 // calls imp or objc_msgSend_uncached

1、首先,我们进入_objc_msgSend

2、LNilOrTagged 检查是否空或特殊结构体

LNilOrTagged:

b.eq LReturnZero // nil check

// tagged

adrp x10, _objc_debug_taggedpointer_classes@PAGE

add x10, x10, _objc_debug_taggedpointer_classes@PAGEOFF

ubfx x11, x0, #60, #4

ldr x16, [x10, x11, LSL #3]

adrp x10, _OBJC_CLASS_$___NSUnrecognizedTaggedPointer@PAGE

add x10, x10, _OBJC_CLASS_$___NSUnrecognizedTaggedPointer@PAGEOFF

cmp x10, x16

b.ne LGetIsaDone

// ext tagged

adrp x10, _objc_debug_taggedpointer_ext_classes@PAGE

add x10, x10, _objc_debug_taggedpointer_ext_classes@PAGEOFF

ubfx x11, x0, #52, #8

ldr x16, [x10, x11, LSL #3]

b LGetIsaDone

如果是空,返回LReturnZero,特殊结构体,一顿汇编操作,返回LGetIsaDon。曾经给面试官问过,如果一个空对象调用方法,会产生怎样的结果。 当时给出答案说不会有任何反应,直到看到这里,如果为nil check,返回LReturnZero才恍然大悟。

3、isa赋值,然后通过isa指针找到class

4、CacheLookup NORMAL 查找缓存列表,到这里终于查找缓存列表的方法了。

CacheLookup 查找缓存列表

CacheLoopup分别有三种不同类型的参数NORMAL|GETIMP|LOOKUP,上面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)

.macro是一个宏,在这段代码里面我们可以观察到CacheLookup查找缓存有三种结果CacheHit、CheckMiss、add

CacheHit 已找到IMP

.macro CacheHit

.if $0 == NORMAL

TailCallCachedImp x17, x12 // authenticate and call imp

.elseif $0 == GETIMP

mov p0, p17

AuthAndResignAsIMP x0, x12 // authenticate imp and re-sign as IMP

ret // return IMP

.elseif $0 == LOOKUP

AuthAndResignAsIMP x17, x12 // authenticate imp and re-sign as IMP

ret // return imp via x17

CacheLoopup传得是NORMAL,这里就是在缓存里面找到imp了,返回imp

2、CheckMiss caches里没找到IMP

.macro CheckMiss

// miss if bucket->sel == 0

.if $0 == GETIMP

cbz p9, LGetImpMiss

.elseif $0 == NORMAL

cbz p9, __objc_msgSend_uncached

.elseif $0 == LOOKUP

cbz p9, __objc_msgLookup_uncached

.else

.abort oops

.endif

.endmacro

因为CacheLoopup传的是NORMAL,这里会执行__objc_msgSend_uncached,缓存里面没有,那我们要去哪里找呢?应该要去方法列表找吧。让我们看看该函数又是怎么实现的

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,就是我们要找的方法列表查询

.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)]

// receiver and selector already in x0 and x1

mov x2, x16

bl __class_lookupMethodAndLoadCache3

// 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

这里我们看到了一句关键的代码执行__class_lookupMethodAndLoadCache3,但怎么搜索都找不到该方法的实现,怎么搞?还记得我上面那个objc_msgSend过程吗,caches是快速查找,用汇编实现,那现在caches里面没找到,那方法列表查找就应该在C/C++文件里面找了,我们去掉一个下划线,搜索结果有两个m文件,objc-runtime-new.mm和objc-class-old.mm。后者是Objective-C 2.0之前的,所以我们看objc-runtime-new.mm

IMP _class_lookupMethodAndLoadCache3(id obj, SEL sel, Class cls)

{

return lookUpImpOrForward(cls, sel, obj,

YES/*initialize*/, NO/*cache*/, YES/*resolver*/);

}

看到_class_lookupMethodAndLoadCache3方法里面调用了lookUpImpOrForward函数并返回一个IMP,我们继续看看该函数实现

接收者方法列表查询IMP

// Try this class's cache.

imp = cache_getImp(cls, sel);

if (imp) goto done;

// Try this class's method lists.

{

Method meth = getMethodNoSuper_nolock(cls, sel);

if (meth) {

//找到imp后,添加到缓存列表

log_and_fill_cache(cls, meth->imp, sel, inst, cls);

imp = meth->imp;

goto done;

}

}

前面是对cls的判断,如果没初始化,就初始化之类的操作,之后我们看到cache_getImp,这里为什么又调多一次从caches里面查找IMP的方法呢,因为Object-C是动态语言,随时对系统进行操作,防止数据问题,所以乐观的查多一次。万一真的查找到,直接返回IMP。没查找到的话就进入该类的方法列表查找

static method_t *

getMethodNoSuper_nolock(Class cls, SEL sel)

{

runtimeLock.assertLocked();

assert(cls->isRealized());

// fixme nil cls?

// fixme nil 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;

}

通过for循环查找methods里面method_t,method_t是一个结构体,里面包含SEL和IMP,这里我就不一一说了。查找到了就会调用log_and_fill_cache添加到缓存列表里并返回IMP

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);

}

如果该类的方法列表找不到就先去Superclass的caches里面查找

Superclass caches查找IMP、Superclass方法列表查找IMP

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.

//找到imp后,添加到缓存列表

log_and_fill_cache(cls, imp, sel, inst, curClass);

goto done;

}

else {

// Found a forward:: entry in a superclass.

// Stop searching, but don't cache yet; call method

// resolver for this class first.

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;

}

}

这里为什么要用for循环去查找呢,大家还记得runtime的方法查找流程图吗,Superclass找不到,继续找Superclass的父类,一直找到NSObject位置,因为是没有Superclass的,所以for循环里面curClass != nil,如果为nil,证明已经到了NSObject,可以终止循环了

1、先通过Superclass cache,如果查找到了IMP,_objc_msgForward_impcache是什么呢,我们通过搜索,发现又跳到了汇编

* id _objc_msgForward(id self, SEL _cmd,...);

*

* _objc_msgForward is the externally-callable

* function returned by things like method_getImplementation().

* _objc_msgForward_impcache is the function pointer actually stored in

* method caches.

*

********************************************************************/

STATIC_ENTRY __objc_msgForward_impcache

// No stret specialization.

b __objc_msgForward

END_ENTRY __objc_msgForward_impcache

给的描述大致是_objc_msgForward_impcache是实际存储在方法缓存中的函数指针,在里面执行了__objc_msgForward,该函数类似于method_getImplementation(),返回一个IMP。那上面的 if (imp != (IMP)_objc_msgForward_impcache)可以理解为superclass缓存表里面拿到的imp如果跟该类的imp不匹配,就说明该类的caches不存在imp,就会调用log_and_fill_cache进行添加缓存处理,反之如果匹配,就直接break返回。

superclass的方法列表查询跟本类的查询过程是一样的,只是传入的类是父类,也就是下面代码的curClass

// 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;

}

objc_msgSend的消息过程就到这了,如果还是没有找到IMP会发生什么呢,objc_msgSend底层窥探下会跟大家一起探讨。

上述内容如说的不正确,请大家留言指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值