美文网首页
对super关键字的小验证

对super关键字的小验证

作者: 01_Jack | 来源:发表于2020-02-29 23:36 被阅读0次

前言

你知道objc_msgSendSuper2(注意,不是objc_msgSendSuper)吗?如果不知道,这篇小文有必要看一下。

0x00 code

@interface A : NSObject
@end

@implementation A
- (instancetype)init {
    printf("self----%p\n", self);
    printf("class A----%p\n", [self class]);
    self = [super init];
    return self;
}
@end


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        [[A alloc] init];
    }
    return 0;
}

通过clang重写后可得到如下代码:

struct objc_selector; struct objc_class;
struct __rw_objc_super { 
    struct objc_object *object; 
    struct objc_object *superClass; 
    __rw_objc_super(struct objc_object *o, struct objc_object *s) : object(o), superClass(s) {} 
};

// @implementation A

static instancetype _I_A_init(A * self, SEL _cmd) {
    printf("self----%p\n", self);
    printf("class A----%p\n", ((Class (*)(id, SEL))(void *)objc_msgSend)((id)self, sel_registerName("class")));
    self = ((A *(*)(__rw_objc_super *, SEL))(void *)objc_msgSendSuper)((__rw_objc_super){(id)self, (id)class_getSuperclass(objc_getClass("A"))}, sel_registerName("init"));
    return self;
}
// @end


int main(int argc, const char * argv[]) {
    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; 
        ((A *(*)(id, SEL))(void *)objc_msgSend)((id)((A *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("A"), sel_registerName("alloc")), sel_registerName("init"));
    }
    return 0;
}

在main函数中可以看到我们熟知的objc_msgSend,但这不是本文的主角。在init函数中可以看到另一个熟知的objc_msgSendSuper,本文由此展开。

0x01 objc_super 与 objc_msgSendSuper

先从super关键字对应的结构体说起:

#ifndef OBJC_SUPER
#define OBJC_SUPER

/// Specifies the superclass of an instance. 
struct objc_super {
    /// Specifies an instance of a class.
    __unsafe_unretained _Nonnull id receiver;

    /// Specifies the particular superclass of the instance to message. 
#if !defined(__cplusplus)  &&  !__OBJC2__
    /* For compatibility with old objc-runtime.h header */
    __unsafe_unretained _Nonnull Class class;
#else
    __unsafe_unretained _Nonnull Class super_class;
#endif
    /* super_class is the first class to search */
};
#endif

<objc/message.h>中的源码来看,当未定义__cplusplus__OBJC2__时,对应的是receiverclass,否则对应receiversuper_class。由于现在都是__OBJC2__,显然这里使用的是super_class。

struct objc_selector; struct objc_class;
struct __rw_objc_super { 
    struct objc_object *object; 
    struct objc_object *superClass; 
    __rw_objc_super(struct objc_object *o, struct objc_object *s) : object(o), superClass(s) {} 
};

static instancetype _I_A_init(A * self, SEL _cmd) {
    printf("self----%p\n", self);
    printf("class A----%p\n", ((Class (*)(id, SEL))(void *)objc_msgSend)((id)self, sel_registerName("class")));
    self = ((A *(*)(__rw_objc_super *, SEL))(void *)objc_msgSendSuper)((__rw_objc_super){(id)self, (id)class_getSuperclass(objc_getClass("A"))}, sel_registerName("init"));
    return self;
}

从重写结果来看,__rw_objc_super结构体中有个objc_object类型的结构体指针superClass,并且这里是通过objc_msgSendSuper来调用的

似乎分析结果指向这两点:
1.objc_super结构体中的Class是消息接收者的父类
2.super对应的消息发送是通过objc_msgSendSuper实现的

然而真实情况并非如此!

0x02 汇编分析

将XCode调为汇编模式,并在self = [super init]处打上断点,运行程序后控制台做如下操作:

可以看到这里调用的是objc_msgSendSuper2,而非objc_msgSendSuper。并且图中两个标记分别为1、2的地方地址都相同,可见实际调用过程中super对应结构体中的Class是消息接收者的类本身,而非其父类!

0x03 objc_msgSendSuper2

<objc-abi.h>中可以看到如下代码:

#if __OBJC2__
// objc_msgSendSuper2() takes the current search class, not its superclass.
OBJC_EXPORT id _Nullable
objc_msgSendSuper2(struct objc_super * _Nonnull super, SEL _Nonnull op, ...)
    OBJC_AVAILABLE(10.6, 2.0, 9.0, 1.0, 2.0);
...
#endif

注释中也写了takes the current search class, not its superclass,可见此时objc_super中的Class确实是消息接收者的类本身,而非其父类。

0x04 小结

super调用某方法底层是通过objc_msgSendSuper2实现的(而非objc_msgSendSuper),此时结构体objc_super中的Class成员变量是消息接收者的类本身(而非其父类)。


Have fun!

相关文章

网友评论

      本文标题:对super关键字的小验证

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