美文网首页
ISA指针探索

ISA指针探索

作者: Kates | 来源:发表于2021-06-17 14:34 被阅读0次

    1.什么是isa指针呢?

    众所众知的每个对象里面都有一个isa指针,而这个isa指针指向哪里呢?
    首先我们先来看一段代码

    @interface HFStudent : NSObject
    @end
    
    HFStudent *p = [[HFStudent alloc] init];
    
    (lldb) x/4gx p
    0x280898130: 0x000001a1021e56a1 0x0000000000000000
    0x280898140: 0x0000000000000000 0x0000000000000000
    
    (lldb) x/4gx 0x000001a1021e56a1
    0x1021e56a1: 0x9800000001021e56 0x4000000001d88802
    0x1021e56b1: 0x000003000281fac4 0x2400018010000000
    

    我们知道对象的首地址指向的就是isa指针即0x000001a1021e56a1,在这个地址里面都包含了什么信息呢?从上面的输出我们看不出来

    2 源码分析isa

    接下来我们通过源码来分析看看这个isa指针到底是什么
    首先我们跟踪调试来到

    initIsa(cls, true, hasCxxDtor); 
    
    objc_object::initIsa(Class cls, bool nonpointer, UNUSED_WITHOUT_INDEXED_ISA_AND_DTOR_BIT bool hasCxxDtor)
    { 
        ASSERT(!isTaggedPointer()); 
        isa_t newisa(0);
        if (!nonpointer) {
            newisa.setClass(cls, this);
        } else {
            ASSERT(!DisableNonpointerIsa);
            ASSERT(!cls->instancesRequireRawIsa());
    #if SUPPORT_INDEXED_ISA
            ASSERT(cls->classArrayIndex() > 0);
            newisa.bits = ISA_INDEX_MAGIC_VALUE;
            newisa.has_cxx_dtor = hasCxxDtor;
            newisa.indexcls = (uintptr_t)cls->classArrayIndex();
    #else
            newisa.bits = ISA_MAGIC_VALUE;
    #   if ISA_HAS_CXX_DTOR_BIT
            newisa.has_cxx_dtor = hasCxxDtor;
    #   endif
            newisa.setClass(cls, this); 
    #endif
            newisa.extra_rc = 1;
        }
        isa = newisa;
    }
    
    通过断点调试简化代码如下
    objc_object::initIsa(Class cls, bool nonpointer, UNUSED_WITHOUT_INDEXED_ISA_AND_DTOR_BIT bool hasCxxDtor)
    { 
        ASSERT(!isTaggedPointer()); 
        isa_t newisa(0);
        if (!nonpointer) {
            newisa.setClass(cls, this);
        } else {
            ASSERT(!DisableNonpointerIsa);
            ASSERT(!cls->instancesRequireRawIsa());
            newisa.bits = ISA_MAGIC_VALUE;
    #   if ISA_HAS_CXX_DTOR_BIT
            newisa.has_cxx_dtor = hasCxxDtor;
    #   endif
            newisa.setClass(cls, this); 
            newisa.extra_rc = 1;
        }
        isa = newisa;
    }
    

    通过源码得出isa的类型为isa_t ,接下来我们看看isa_t的源码

    union isa_t {
        isa_t() { }
        isa_t(uintptr_t value) : bits(value) { }
    
        uintptr_t bits;
    
    private:
        // Accessing the class requires custom ptrauth operations, so
        // force clients to go through setClass/getClass by making this
        // private.
        Class cls;
    
    public:
    #if defined(ISA_BITFIELD)
        struct {
            ISA_BITFIELD;  // defined in isa.h
        };
    
        bool isDeallocating() {
            return extra_rc == 0 && has_sidetable_rc == 0;
        }
        void setDeallocating() {
            extra_rc = 0;
            has_sidetable_rc = 0;
        }
    #endif
    
        void setClass(Class cls, objc_object *obj);
        Class getClass(bool authenticated);
        Class getDecodedClass(bool authenticated);
    };
    

    从源码中得知isa是一个联合体
    接下来我们补充一些关于联合体和isa相关知识

    3. union 联合体

    结构体(struct)中所有变量是“共存”的——优点是“有容乃大”,全面;缺点是struct内存空间的分配是粗放的,不管用不用,全分配。
    而联合体(union)中是各变量是“互斥”的——缺点就是不够“包容”;但优点是内存使用更为精细灵活,也节省了内存空间。
    接下来我们通过例子来说明

    union HFUni {
        int a;
        char b;
        short c;
    };
    
     union HFUni uni;
    uni.a = 100;
    NSLog(@"a=%d---b=%d---c=%d---%lu", uni.a, uni.b, uni.c, sizeof(uni));
    uni.b = 'a';
    NSLog(@"a=%d---b=%d---c=%d---%lu", uni.a, uni.b, uni.c, sizeof(uni));
    uni.c = 200;
    NSLog(@"a=%d---b=%d---c=%d---%lu", uni.a, uni.b, uni.c, sizeof(uni));
    
    2021-06-16 17:18:34.806007+0800 StoreKitDemo[2084:455336] a=100---b=100---c=100---4
    2021-06-16 17:18:34.806222+0800 StoreKitDemo[2084:455336] a=97---b=97---c=97---4
    2021-06-16 17:18:37.421626+0800 StoreKitDemo[2084:455336] a=200---b=-56---c=200---4
    

    从结果中我们看到联合体的大小是4字节也就是联合体里面最大类型大小,这边通过赋值也可以看到每一次赋值,联合体里面所有的变量都发生变化,这也证明了各变量是互斥的。

    4 isa补充

    nonpointer:表示是否对 isa 指针开启指针优化
    0:纯isa指针,1:不⽌是类对象地址,isa 中包含了类信息、对象的引⽤计数等

    has_assoc:关联对象标志位,0没有,1存在

    has_cxx_dtor:该对象是否有 C++ 或者 Objc 的析构器,如果有析构函数,则需要做析构逻辑,
    如果没有,则可以更快的释放对象

    shiftcls:
    存储类指针的值。开启指针优化的情况下,在 arm64 架构中有 33 位⽤来存储类指针。

    magic:⽤于调试器判断当前对象是真的对象还是没有初始化的空间

    weakly_referenced:志对象是否被指向或者曾经指向⼀个 ARC 的弱变量,
    没有弱引⽤的对象可以更快释放

    deallocating:标志对象是否正在释放内存

    has_sidetable_rc:当对象引⽤技术⼤于 10 时,则需要借⽤该变量存储进位
    extra_rc:当表示该对象的引⽤计数值,实际上是引⽤计数值减 1,
    例如,如果对象的引⽤计数为 10,那么 extra_rc 为 9。如果引⽤计数⼤于 10,
    则需要使⽤到下⾯的 has_sidetable_rc。

    有了上面的知识,我们知道shiftcls存放的是类的信息,至于是在什么时候存放的呢?我们看到代码newisa.setClass(cls, this); 跟踪进去调试看看

    inline void
    isa_t::setClass(Class newCls, UNUSED_WITHOUT_PTRAUTH objc_object *obj)
    {
        // Match the conditional in isa.h.
    #if __has_feature(ptrauth_calls) || TARGET_OS_SIMULATOR
    #   if ISA_SIGNING_SIGN_MODE == ISA_SIGNING_SIGN_NONE
        // No signing, just use the raw pointer.
        uintptr_t signedCls = (uintptr_t)newCls;
    
    #   elif ISA_SIGNING_SIGN_MODE == ISA_SIGNING_SIGN_ONLY_SWIFT
        // We're only signing Swift classes. Non-Swift classes just use
        // the raw pointer
        uintptr_t signedCls = (uintptr_t)newCls;
        if (newCls->isSwiftStable())
            signedCls = (uintptr_t)ptrauth_sign_unauthenticated((void *)newCls, ISA_SIGNING_KEY, ptrauth_blend_discriminator(obj, ISA_SIGNING_DISCRIMINATOR));
    
    #   elif ISA_SIGNING_SIGN_MODE == ISA_SIGNING_SIGN_ALL
        // We're signing everything
        uintptr_t signedCls = (uintptr_t)ptrauth_sign_unauthenticated((void *)newCls, ISA_SIGNING_KEY, ptrauth_blend_discriminator(obj, ISA_SIGNING_DISCRIMINATOR));
    
    #   else
    #       error Unknown isa signing mode.
    #   endif
    
        shiftcls_and_sig = signedCls >> 3;
    
    #elif SUPPORT_INDEXED_ISA
        // Indexed isa only uses this method to set a raw pointer class.
        // Setting an indexed class is handled separately.
        cls = newCls;
    
    #else // Nonpointer isa, no ptrauth
        shiftcls = (uintptr_t)newCls >> 3;
    #endif
    }
    

    通过断点和类内部变化我们发现在指向shiftcls = (uintptr_t)newCls >> 3时


    643A22C1-4FB0-4E3C-8F32-4B768C018A19.png

    变化成


    BD7A4085-4B5E-41E2-A188-511FD610DBC7.png

    由此可见shiftcls = (uintptr_t)newCls >> 3 正是设置类关联的代码

    x86平台
    #   define ISA_MASK        0x00007ffffffffff8ULL
    #   define ISA_MAGIC_MASK  0x001f800000000001ULL
    #   define ISA_MAGIC_VALUE 0x001d800000000001ULL
    #   define ISA_HAS_CXX_DTOR_BIT 1
    #   define ISA_BITFIELD                                                        \
          uintptr_t nonpointer        : 1;                                         \
          uintptr_t has_assoc         : 1;                                         \
          uintptr_t has_cxx_dtor      : 1;                                         \
          uintptr_t shiftcls          : 44; /*MACH_VM_MAX_ADDRESS 0x7fffffe00000*/ \
          uintptr_t magic             : 6;                                         \
          uintptr_t weakly_referenced : 1;                                         \
          uintptr_t unused            : 1;                                         \
          uintptr_t has_sidetable_rc  : 1;                                         \
          uintptr_t extra_rc          : 8
    
    arm64平台
    #     define ISA_MASK        0x0000000ffffffff8ULL
    #     define ISA_MAGIC_MASK  0x000003f000000001ULL
    #     define ISA_MAGIC_VALUE 0x000001a000000001ULL
    #     define ISA_HAS_CXX_DTOR_BIT 1
    #     define ISA_BITFIELD                                                      \
            uintptr_t nonpointer        : 1;                                       \
            uintptr_t has_assoc         : 1;                                       \
            uintptr_t has_cxx_dtor      : 1;                                       \
            uintptr_t shiftcls          : 33; /*MACH_VM_MAX_ADDRESS 0x1000000000*/ \
            uintptr_t magic             : 6;                                       \
            uintptr_t weakly_referenced : 1;                                       \
            uintptr_t unused            : 1;                                       \
            uintptr_t has_sidetable_rc  : 1;                                       \
            uintptr_t extra_rc          : 19
    

    通过上面的宏,我们可以知道shiftcls在x86平台上是从3-47,在arm64平台上是3-37位,这样我们就可以通过代码示例来证明
    环境:iphone 8(arm64)

    HFStudent *p = [[HFStudent alloc] init];
    NSLog(@"%@---%p", p, p);
    
    (lldb) x/4gx p
    0x2807ec6c0: 0x000001a100e7d98d 0x0000000000000000
    0x2807ec6d0: 0x0000000000000000 0x0000000000000000
    (lldb) p 0x000001a100e7d98d & 0x0000000ffffffff8ULL
    (unsigned long long) $1 = 4310161800
    (lldb) po 0x000001a100e7d98d & 0x0000000ffffffff8ULL
    HFStudent
    
    0x000001a100e7d98d 为p的isa指针
    isa指针 & 0x0000000ffffffff8ULL -> HFStudent  
    

    相关文章

      网友评论

          本文标题:ISA指针探索

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