block hook就是勾住block进行逻辑注入,且不影响原有block逻辑。
依赖OC的运行时机制,拦截方法比较容易,但是拦截block却没那么简单
前置知识1:Block数据结构
后面的介绍和分析都用到了block的数据结构,这里先整理一下。
Block的定义在Block_private.h
中,点击查看源码。
#define BLOCK_DESCRIPTOR_1 1
struct Block_descriptor_1 {
uintptr_t reserved;
uintptr_t size;
};
#define BLOCK_DESCRIPTOR_2 1
struct Block_descriptor_2 {
// requires BLOCK_HAS_COPY_DISPOSE
void (*copy)(void *dst, const void *src);
void (*dispose)(const void *);
};
#define BLOCK_DESCRIPTOR_3 1
struct Block_descriptor_3 {
// requires BLOCK_HAS_SIGNATURE
const char *signature;
const char *layout; // contents depend on BLOCK_HAS_EXTENDED_LAYOUT
};
struct Block_layout {
void *isa;
volatile int32_t flags; // contains ref count
int32_t reserved;
void (*invoke)(void *, ...);
struct Block_descriptor_1 *descriptor;
// imported variables
};
- isa指针:指向父类结构体,就是
_NSConcreteStackBlock
,_NSConcreteMallocBlock
,_NSConcreteGlobalBlock
这几个,就是对应在分配内存时的对应区域,不深入。 - flags:按bit位表示一些block的附加信息,比如判断block类型、判断block引用计数、判断block是否需要执行辅助函数等。
-
// Values for Block_layout->flags to describe block objects enum { BLOCK_DEALLOCATING = (0x0001), // runtime BLOCK_REFCOUNT_MASK = (0xfffe), // runtime BLOCK_NEEDS_FREE = (1 << 24), // runtime BLOCK_HAS_COPY_DISPOSE = (1 << 25), // compiler BLOCK_HAS_CTOR = (1 << 26), // compiler: helpers have C++ code BLOCK_IS_GC = (1 << 27), // runtime BLOCK_IS_GLOBAL = (1 << 28), // compiler BLOCK_USE_STRET = (1 << 29), // compiler: undefined if !BLOCK_HAS_SIGNATURE BLOCK_HAS_SIGNATURE = (1 << 30), // compiler BLOCK_HAS_EXTENDED_LAYOUT=(1 << 31) // compiler };
- reserved:保留变量,我的理解是表示block内部的变量数。
- invoke:函数指针,指向具体的block实现的函数调用地址。
- descriptor:block附加描述信息,主要保存了内存size以及copy,dispose函数的指针,签名以及layout等信息。通过源码可以发现,Block_layout中只包含了Block_descriptor_1,这是因为在捕获不同类型变量或者没用到外部变量时,编译器会改变结构体的结构,按需添加Block_descriptor_2和Block_descriptor_3,所以需要BLOCK_HAS_COPY_DISPOSE和BLOCK_HAS_SIGNATURE等枚举来判断
- variables:因为block有闭包性,所以可以访问block外部的局部变量。这些variables就是复制到结构体中的外部局部变量或变量的地址(__block关键词,复制引用地址实现访问)
另外定义了Block_byref,是变量在被__block修饰时由编译器来生成,暂时不关注。
前置知识2:libffi 动态调用C函数
基本流程如下:
1.准备好参数数据以及ffi_type数组、返回值内存指针、函数指针
2.创建与函数特征相匹配的函数原型:ffi_cif对象
3.使用ffi_call来完成函数调用
需要用到的API:
/* 封装函数原型
ffi_prep_cif returns a libffi status code, of type ffi_status. This will be either FFI_OK if everything worked properly; FFI_BAD_TYPEDEF if one of the ffi_type objects is incorrect; or FFI_BAD_ABI if the abi parameter is invalid.
*/
ffi_status ffi_prep_cif(ffi_cif *cif,
ffi_abi abi, //abi is the ABI to use; normally FFI_DEFAULT_ABI is what you want. Multiple ABIs for more information.
unsigned int nargs, //nargs is the number of arguments that this function accepts. ‘libffi’ does not yet handle varargs functions; see Missing Features for more information.
ffi_type *rtype, //rtype is a pointer to an ffi_type structure that describes the return type of the function. See Types.
ffi_type **atypes); //argtypes is a vector of ffi_type pointers. argtypes must have nargs elements. If nargs is 0, this argument is ignored.
/* 调用指定函数
This calls the function fn according to the description given in cif. cif must have already been prepared using ff