[iOS]深入理解__bridge - OC对象与C++对象的引用转换

0x0 crash

昨天在iOS Geekers钉钉群里聊到一个问题, 下面的代码会crash:

    void* a = new char;
    id ext = (__bridge id)a;

crash现场如图:
screenshot

看看挂的地方:
screenshot

挂在objc_retain里面:
screenshot

objc_retain的作用是对oc对象做retain用的, 我们对指令做一下简单的解析:

libobjc.A.dylib`objc_retain:
    0x1810d00a0 <+0>:  cbz    x0, 0x1810d00c8           ; <+40>    // 判断x0也就是传进来的第一个参数是不是nil, 在这里x0是变量a, 也就是char类型的指针
    0x1810d00a4 <+4>:  tbnz   x0, #63, 0x1810d00cc      ; <+44>    // 判断x0寄存器的第63位是不是0
    0x1810d00a8 <+8>:  ldr    x8, [x0]                             // 取x0指针的内容放入x8, 正常情况下这里是oc对象的isa, 传进来的并不是oc对象, 没有isa. 这里取出来的是0x0, 见下图
    0x1810d00ac <+12>: and    x8, x8, #0xffffffff8                 // x8'与操作'0xffffffff8(ISA_MASK), '与'完还是0
->  0x1810d00b0 <+16>: ldrb   w8, [x8, #32]                        // 取x8为基地址偏移量32的内存内容. 也就是x8+0x20, 也就是0+0x20=0x20. 0x20是一个保留地址不可读写, 直接挂!

screenshot

0x1 解决

那么问题来, 为什么这里会有一次retain操作导致挂掉呢? 看看代码:

   id ext = (__bridge id)a;

id ext, 这种写法隐含了__strong id ext, ext对a做了一次强引用, 而强引用就会对被引用的对象做一次retain.

那我们就不强引用就好了:

    void* a = new char;
    __unsafe_unretained id ext = (__bridge id)a;

那这样就仅仅只是把a指针赋值给了ext指针, 并没有做强引用不会触发retain而导致crash, 不过a作为一个c++对象, 内存管理自己要做好!

0x2 用起来

看如下代码:

void test_bridge_parameter (id p0) {

}

void test_bridge() {
    void* a = new char;
    __unsafe_unretained id ext = (__bridge id)a;
    test_bridge_parameter(ext);
}

这里我们把ext作为参数, 传递给了test_bridge_parameter. 你觉得这段代码能够正确执行么?

当然不能!!!
screenshot

卧槽, 咋挂函数的第0行了? 看看调试:
screenshot
screenshot
这里调用了 objc_storeStrong, 而objc_storeStrong又调用了objc_retain, 也就是我们传进来的c++对象ext又被retain了. 为什么呢?

objc_storeStrong 有两个参数 p0, p1, 作用是把p1赋给p0并强引用一次.

void
objc_storeStrong(id *location, id obj)
{
    id prev = *location;
    if (obj == prev) {
        return;
    }
    objc_retain(obj);
    *location = obj;
    objc_release(prev);
}

在函数调用的参数传递时, 会把传进来的参数, 按照参数表里的定义的属性, 做相关的赋值.

void test_bridge_parameter (id p0)

里面的id p0, 同样隐含__strong id p0, 改成

void test_bridge_parameter (__unsafe_unretained id p0)

去掉参数赋值时的强引用即可.

0x3 CFTypeRef和OC对象的关系

前面的例子中 char类并不是一个CFTypeRef类型, 导致并不能被转换为OC对象:

/* Base "type" of all "CF objects", and polymorphic functions on them */
typedef const CF_BRIDGED_TYPE(id) void * CFTypeRef;

typedef const struct CF_BRIDGED_TYPE(NSString) __CFString * CFStringRef;

那么CFTypeRef类型的对象是怎么转为OC对象的呢?
来一段代码:

void test_bridge() {
    CFStringRef helloCF = CFSTR("hello, world");
}

反汇编:

testbridge`test_bridge:
    0x10000ac78 <+0>:  sub    sp, sp, #16               ; =16 
    0x10000ac7c <+4>:  adrp   x8, 2
    0x10000ac80 <+8>:  add    x8, x8, #32               ; =32 
    0x10000ac84 <+12>: str    x8, [sp, #8]
->  0x10000ac88 <+16>: add    sp, sp, #16               ; =16 
    0x10000ac8c <+20>: ret   

bridge一下, 为了避免__strong带来的多余指令, 这里用__unsafe_unretained来避免:

void test_bridge() {
    CFStringRef helloCF = CFSTR("hello, world");
    __unsafe_unretained NSString *helloNS = (__bridge id)helloCF;
}

反汇编:

testbridge`test_bridge:
    0x1000f2c70 <+0>:  sub    sp, sp, #16               ; =16 
    0x1000f2c74 <+4>:  adrp   x8, 2
    0x1000f2c78 <+8>:  add    x8, x8, #32               ; =32 
    0x1000f2c7c <+12>: str    x8, [sp, #8]
    0x1000f2c80 <+16>: ldr    x8, [sp, #8]
    0x1000f2c84 <+20>: str    x8, [sp]
->  0x1000f2c88 <+24>: add    sp, sp, #16               ; =16 
    0x1000f2c8c <+28>: ret    

对比两段汇编, 会发现区别仅仅在与多出来两调指令, 一条是把x8从栈偏移量8位置里面弄出来, 另一条把x8扔到栈的偏移量0的位置, 仅仅只做了简单的赋值, 而并没有任何对数据进行任何的修改. 那不就意味着CFTypeRef和对应的OC类型的数据结构是一样的?

screenshot

图中两者的内容都是"hello, world", 但是数据类型却不同!

我们看看sp(栈)的偏移量0和偏移量8的内容, 先用reg re读出sp的地址:

   sp = 0x000000016fd13aa0

screenshot

图中高亮的位置, 可以看出, 偏移量0和偏移量8的两个64位地址里面存的内容是一模一样的(指向string对象的指针). 因吹丝挺!!!

并不是所有的类都可以无损转换, 只有toll-free bridged types 才可以

0x4 __bridge_transfer 和 __bridge_retained

授人以鱼不如受人以渔, 大家自己动手看看汇编代码差别! (其实是我懒... -_-!!)

切汇编代码调试的方法是: Xcode顶部导航 -> Debug -> Debug Workflow -> Always Show Disassembly

__bridge在llvm文档的说明如下:

A bridged cast is a C-style cast annotated with one of three keywords:

    (__bridge T) op casts the operand to the destination type T. If T is a retainable object pointer type, then op must have a non-retainable pointer type. If T is a non-retainable pointer type, then op must have a retainable object pointer type. Otherwise the cast is ill-formed. There is no transfer of ownership, and ARC inserts no retain operations.
    (__bridge_retained T) op casts the operand, which must have retainable object pointer type, to the destination type, which must be a non-retainable pointer type. ARC retains the value, subject to the usual optimizations on local values, and the recipient is responsible for balancing that +1.
    (__bridge_transfer T) op casts the operand, which must have non-retainable pointer type, to the destination type, which must be a retainable object pointer type. ARC will release the value at the end of the enclosing full-expression, subject to the usual optimizations on local values.

0x5 参考

  1. objc_storeStrong: http://opensource.apple.com/source/objc4/objc4-647/runtime/NSObject.mm
  2. bridged casts: http://clang.llvm.org/docs/AutomaticReferenceCounting.html#bridged-casts

  1. toll-free bridged types:https://developer.apple.com/library/ios/documentation/CoreFoundation/Conceptual/CFDesignConcepts/Articles/tollFreeBridgedTypes.html

https://yq.aliyun.com/articles/58964

猜你喜欢

转载自blog.csdn.net/args_/article/details/53018656
今日推荐