基于Aspects框架的iOS热修复方案

作者 : 开心源码 本文共10407个字,预计阅读时间需要27分钟 发布时间: 2022-05-13 共415人阅读

原文地址

背景

  1. JSPatch 无法审核,就算进行深度的代码混淆仍然无法逃脱苹果审核机制
  2. App 审核加快,但是仍然无法很好的控制线上 Bug 的影响范围
  3. 目前未发现有其余可替代方案,只能另寻他径

目标

JSPatch 可以任意替换和新添加方法,甚至可以用来开发新模块。但是假如纯粹用来修复线上bug的话,我们并不需要如此强大的功能。热修复只要要具有以下几点功能足以:

  1. 方法替换为空实现
  2. 方法参数修改
  3. 方法返回值修改
  4. 方法调用前后插入自己设置代码
    • 支持任意 OC 方法调用
    • 支持赋值语句
    • 支持 if 语句:==、!=、>、>=、<、<=、||、&&
    • 支持 super 调用
    • 支持自己设置局部变量
    • 支持 return 语句

JPAspect

JPAspect 一款轻量级、无侵入和无审核风险的 iOS 热修复框架。JPAspect 通过下发指定规则的 json 就可轻松实现线上 Bug 修复。JPAspect 已实现上述所有功能,具体实现请参考代码。

原理

Runtime 术语

1. SEL2. IMP3. Method4. NSMethodSignature5. NSInvocation6. void _objc_msgForward(void /* id receiver, SEL sel, ... */ ) 7. id _Nullable objc_msgSend(id _Nullable self, SEL _Nonnull op, ...)8. Objective-C type encodings

Runtime 基本操作

  • Class 反射创立
// 1NSClassFromString(@"NSObject");// 2 objc_getClass("NSObject");
  • SEL 反射创立
// 1@selector(init);// 2sel_registerName("init");// 3NSSelectorFromString(@"init");
  • 方法替换
static void cc_forwardInvocation(id slf, SEL sel, NSInvocation *invocation) {    // do what you want to do}class_replaceMethod(klass, @selector(forwardInvocation:), (IMP)cc_forwardInvocation, "v@:@");
  • 方法新添加
Class tClass = NSClassFromString(@"UIViewController");SEL selector = NSSelectorFromString(@"viewDidLoad");Method targetMethod = class_getInstanceMethod(tClass, selector);IMP targetMethodIMP = method_getImplementation(targetMethod);const char *typeEncoding = method_getTypeEncoding(targetMethod);SEL aliasSelector = NSSelectorFromString([@"cc" stringByAppendingFormat:@"_%@", NSStringFromSelector(selector)]);class_addMethod(klass, aliasSelector, method_getImplementation(targetMethod), typeEncoding);
  • 新类创立
Class cls = objc_allocateClassPair([NSObject class], “CCObject”, 0);objc_registerClassPair(cls);
  • 消息转发
// 1. 正常转发+ (BOOL)resolveClassMethod:(SEL)sel + (BOOL)resolveInstanceMethod:(SEL)sel - (id)forwardingTargetForSelector:(SEL)aSelector- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector- (void)forwardInvocation:(NSInvocation *)anInvocation// 2. 自己设置转发void _objc_msgForward(void /* id receiver, SEL sel, ... */ ) 

Method Invoke 的几种方式

@interface People : NSObject- (void)helloWorld;@end
  1. 常规调用
  2. 反射调用
  3. objc_msgSend
  4. C 函数调用
  5. NSInvocation 调用
// 常规调用People *people = [[People alloc] init];[people helloWorld];// 反射调用    Class cls = NSClassFromString(@"People");id obj = [[cls alloc] init];[obj performSelector:NSSelectorFromString(@"helloWorld")];// objc_msgSend((void(*)(id, SEL))objc_msgSend)(people, sel_registerName("helloWorld"));// C 函数调用Method initMethod = class_getInstanceMethod([People class], @selector(helloWorld));IMP imp = method_getImplementation(initMethod);((void (*) (id, SEL))imp)(people, @selector(helloWorld));// NSInvocation 调用NSMethodSignature *sig = [[People class] instanceMethodSignatureForSelector:sel_registerName("helloWorld")];NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];invocation.target = people;invocation.selector = sel_registerName("helloWorld");[invocation invoke];

Aspects 原理分析

新版热修复是基于 Aspects 框架开发的,无审核风险,已上线。Aspects 和 JSPatch 的都是基于消息转发实现的。

一、简介

  • AspectsContainer:Tracks all aspects for an object/class
  • AspectIdentifier:Tracks a single aspect

二、Hook 流程

  1. 检查 selector 能否可以替换,里面涉及少量黑名单等判断
  2. 获取 AspectsContainer,假如为空则创立并绑定目标类
  3. 创立 AspectIdentifier,引用自己设置实现(block)和 AspectOptions 等信息
  4. 将目标类 forwardInvocation: 方法替换为自己设置方法
  5. 目标类新添加一个带有aspects_前缀的方法,新方法(aliasSelector)实现跟目标方法相同
  6. 将目标方法实现替换为 _objc_msgForward
// 将目标类 **forwardInvocation:** 方法替换为自己设置方法IMP originalImplementation = class_replaceMethod(klass, @selector(forwardInvocation:), (IMP)__ASPECTS_ARE_BEING_CALLED__, "v@:@");if (originalImplementation) {    class_addMethod(klass, NSSelectorFromString(AspectsForwardInvocationSelectorName), originalImplementation, "v@:@");}// 目标类新添加一个带有` aspects_`前缀的方法,新方法(aliasSelector)实现跟目标方法相同Method targetMethod = class_getInstanceMethod(klass, selector);IMP targetMethodIMP = method_getImplementation(targetMethod);const char *typeEncoding = method_getTypeEncoding(targetMethod);SEL aliasSelector = NSSelectorFromString([AspectsMessagePrefix stringByAppendingFormat:@"_%@", NSStringFromSelector(selector)]);class_addMethod(klass, aliasSelector, method_getImplementation(targetMethod), typeEncoding);// 将目标方法实现替换为 `_objc_msgForward`class_replaceMethod(klass, selector, aspect_getMsgForwardIMP(self, selector), typeEncoding);

三、Invoke 流程

  1. 调用目标方法进入消息转发流程
  2. 调用自己设置 __ASPECTS_ARE_BEING_CALLED__ 方法
  3. 获取对应 invocation,将 invocation.selector 设置为 aliasSelector
  4. 通过 aliasSelector 获取对应 AspectsContainer
  5. 根据 AspectOptions 调用客户自定实现(目标方法调用前/后/替换)

四、Aspects 使用遇到的问题

  • 使用了自旋锁,存在优先级反转问题,使用 pthread_mutex_lock 代替就可
  • 特殊 struct 判断逻辑不够全面,例如:NSRange, NSPoint等 在 32 位架构下有问题,需要自行兼容
#if defined(__LP64__) && __LP64__    if (valueSize == 16) {        methodReturnsStructValue = NO;    }#endif
  • 类方法无法直接 hook, 不过可以 hook 其 Meta class 元类方式进行处理
object_getClass(targetCls)
  • 无法同时 hook 一个类的实例方法和类方法,起因是使用了相同的 swizzledClasse key, 处理如下:
static Class aspect_swizzleClassInPlace(Class klass) {    NSCParameterAssert(klass);    NSString *className = [NSString stringWithFormat:@"%@_%p", NSStringFromClass(klass), klass];    _aspect_modifySwizzledClasses(^(NSMutableSet *swizzledClasses) {        if (![swizzledClasses containsObject:className]) {            aspect_swizzleForwardInvocation(klass);            [swizzledClasses addObject:className];        }    });    return klass;}static void aspect_undoSwizzleClassInPlace(Class klass) {    NSCParameterAssert(klass);    NSString *className = [NSString stringWithFormat:@"%@_%p", NSStringFromClass(klass), klass];    _aspect_modifySwizzledClasses(^(NSMutableSet *swizzledClasses) {        if ([swizzledClasses containsObject:className]) {            aspect_undoSwizzleForwardInvocation(klass);            [swizzledClasses removeObject:className];        }    });}

开发中遇到的坑

一、Illegal Instruction Crash

从 -forwardInvocation: 里的 NSInvocation 对象取参数值时,若参数值是id类型,一般会这样取:

id value = nil;[invocation getArgument:&value atIndex:2];

但是这种写法存在 crash 风险。例如 Hook NSMutableArray 的 insertObject:atIndex: 方法.你会发现在有些系统调用会出现 EXC_BAD_INSTRUCTION 崩溃

[NSClassFromString(@"__NSArrayM") aspect_hookSelector:@selector(insertObject:atIndex:) withOptions:AspectPositionInstead usingBlock:^(id<AspectInfo> info){        NSLog(@"insertObject:atIndex: hook");                id value = nil;        [info.originalInvocation getArgument:&value atIndex:2];        if (value) {            [info.originalInvocation invoke];        }    } error:NULL];

开启 Zombie objects 下的异常打印

-[UITraitCollection retain]: message sent to deallocated instance 0x6000007cde00    

崩溃起因分析:

  1. NSInvocation 不会引用参数,介绍可以看官方文档(This class does not retain the arguments for the contained invocation by default)
  2. ARC 在隐式赋值不会自动插入 retain 语句
  3. ARC 下 id value 相当于 __strong id vaule,所以在退出作用域时会自动插入 release 语句。
  4. 综上123可以得出:参数对象会多调用一次 release 方法,导致对象提前释放。假如此时再对该对象发送消息则会发生野指针崩溃

处理办法:

1、将 value 变成 __unsafe_unretained 或者 __weak,让 ARC 在它退出作用域时不插入 release 语句

__unsafe_unretained id value = nil;

2、通过 __bridge 转换让 value 持有返回对象,显示赋值

id value = nil;void *result;[invocation getArgument:&result atIndex:2];value = (__bridge id)result;

二、Memory leak

背景:

由于要支持参数替换,所以要从 -forwardInvocation: 里的 NSInvocation 对象取返回值,而后替换为自己设置的参数。一般生成一个对象都会调用 alloc 方法,而后再初始化

内存泄漏起因分析:

1、根据内存管理规则可知,当调用 alloc / new / copy / mutableCopy 方法的返回对象的 retainCount = 1。

2、假如方法有返回值的话,ARC会在 return 后自动插入 autorelease,所以一般的常规返回是没有问题的。

3、ARC 对隐式赋值不会自动插入 autorelease,所以少了一次 release,从而导致内存泄漏。

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];invocation.target = [NSObject class];invocation.selector = sel_registerName([@"alloc" UTF8String]);[invocation invoke];id returnValue = nil;[invocation getReturnValue:&returnValue];return returnValue;    

处理办法:

  1. 把返回对象的内存管理权移交出来,让外部对象管理其内存。因为是显示赋值,ARC机制生效。
  2. 采用常规方法调用代替 NSInvocation
id target = [NSObject class];NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];invocation.target = target;invocation.selector = sel_registerName([@"alloc" UTF8String]);[invocation invoke];id resultObj = nil,void *result;[invocation getReturnValue:&result];// 方法1    if ([selName isEqualToString:@"alloc"] ||    [selName isEqualToString:@"new"] ||    [selName isEqualToString:@"copy"] ||    [selName isEqualToString:@"mutableCopy"]) {    resultObj = (__bridge_transfer id)result;} else {    resultObj = (__bridge id)result;}// 方法2if ([selName isEqualToString:@"alloc"]) {    resultObj = [[target class] alloc];} else if ([selName isEqualToString:@"new"]) {    resultObj = [[target class] new];} else if ([selName isEqualToString:@"copy"]) {    resultObj = [target copy];} else if ([selName isEqualToString:@"mutableCopy"]) {    resultObj = [target mutableCopy];} else {    expectObj = (__bridge id)result;}

功能实现简介

一、方法替换为空实现

这个功能其实很容易实现,直接替换就可

[NSClassFromString(@"UIViewController")  aspect_hookSelector:@selector(viewDidLoad:) withOptions:AspectPositionInstead usingBlock:^(id<AspectInfo> info){    // 空实现} error:NULL];

二、判断方法参数

核心点就是通过 Aspect 获取目标方法 Invocation ,而后对 Invocation 的参数进行比照,假如符合期望值则继续之前原方法,例如插入的对象能否为 nil,假如为 nil 则放弃调用原方法,相当于执行了一个空方法。这个可以扩展为基础变量判断,例如数组越界判断。

[NSClassFromString(@"__NSArrayM") aspect_hookSelector:@selector(insertObject:atIndex:) withOptions:AspectPositionInstead usingBlock:^(id<AspectInfo> info){            // 当 value = nil,结束当前方法调用    __unsafe_unretained id value = nil;    [info.originalInvocation getArgument:&value atIndex:2];    if (value) {        [info.originalInvocation invoke];    }} error:NULL];

三、替换方法参数

这个也是通过 Invocation 去修改方法里面的参数,而后再调用,具体实现如下

[NSClassFromString(@"__NSArrayM") aspect_hookSelector:@selector(insertObject:atIndex:) withOptions:AspectPositionInstead usingBlock:^(id<AspectInfo> info){            // 不论外面怎样调用,每次 atIndex = 0    NSUInteger value = 0;    [info.originalInvocation setArgument:& value atIndex:3];    [info.originalInvocation invoke];} error:NULL];

四、更改方法返回值

[NSClassFromString(@"__NSArrayM") aspect_hookSelector:@selector(objectAtIndex:) withOptions:AspectPositionInstead usingBlock:^(id<AspectInfo> info){            // 不论外面怎样调用,每次都返回 nil    [info.originalInvocation invoke];    id expectValue = nil;    [info.originalInvocation setReturnValue:&expectValue];} error:NULL];

五、方法调用前后插入自己设置代码

这个实现的起来略微复杂一点,由于要实现方法前后插入方法,所以你必需要构建消息发送对象和方法参数。例如在 UIViewController 的 viewDidLoad 方法前设置其背景颜色为红色。首先需要获取 viewDidLoad 方法的 Invocation,而后通过 Invocation.target 获取到控制器对象 self,获取到 self 之后调用 objc_msgSend 方法获取 view,到这里我们已经获取到消息发送对象,而后我们用 sel_registerName 获取 setBackgroundColor: 方法的 SEL。通过 SEL 获取到函数签名 NSMethodSignature,同过函数签名去获取 setBackgroundColor: 的 Invocation,最后通过设置 Invocation 的参数为红色,而后调用 Invocation 的 invoke方法就将背景色改为 redColor。到此相信已经理解其核心原理了,我们只要要在此基础上再扩展,那么足以应付线上的 90% 以上的问题了。下面是具体实现代码。

[NSClassFromString(@"UIViewController") aspect_hookSelector:@selector(viewDidLoad) withOptions:AspectPositionInstead usingBlock:^(id<AspectInfo> aspectInfo){                   // viewDidLoad 执行前插入 self.view.backgroundColor = [UIColor redColor];    target = ((id (*)(id, SEL))objc_msgSend)(aspectInfo.originalInvocation.target, NSClassFromString(@"view"));    SEL selector = sel_registerName([@"setBackgroundColor:" UTF8String]);    NSMethodSignature *signature = [[target class] instanceMethodSignatureForSelector:selector];    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];    invocation.target = target;    invocation.selector = selector;    id value = ((id(*)(id, SEL))objc_msgSend)([UIColor class], sel_registerName("redColor"));    [invocation setArgument:&value atIndex:2];    [invocation invoke];        [info.originalInvocation invoke]; } error:NULL];

参考文献

  1. Objective-C Runtime Programming Guide
  2. NSInvocation returns value but makes app crash with EXC_BAD_ACCESS
  3. JSPatch 实现原理详解
  4. objc_msgSend_stret
  5. objc_msgSend() Tour Part 1: The Road Map
  6. -rac_signalForSelector: may fail for struct returns
说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 基于Aspects框架的iOS热修复方案

发表回复