Skip to content

Commit

Permalink
重命名为recover并添加单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFruity committed Feb 23, 2021
1 parent 70241ea commit 1d19130
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 23 deletions.
2 changes: 1 addition & 1 deletion OCRunner.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "OCRunner"
s.version = "1.0.9"
s.version = "1.0.10"
s.summary = "OCRunner"
s.description = <<-DESC
Execute Objective-C code Dynamically. iOS hotfix SDK.
Expand Down
3 changes: 2 additions & 1 deletion OCRunner/ORInterpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ NS_ASSUME_NONNULL_BEGIN
+ (void)excuteBinaryPatchFile:(NSString *)path;
+ (void)excuteJsonPatchFile:(NSString *)path;
+ (void)excuteNodes:(NSArray *)nodes;
+ (void)reverse;
+ (void)recover;
+ (void)recoverWithClearEnvironment:(BOOL)clear;
@end

NS_ASSUME_NONNULL_END
20 changes: 12 additions & 8 deletions OCRunner/ORInterpreter.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import "ORInterpreter.h"
#import "RunnerClasses+Execute.h"
#import "RunnerClasses+Reverse.h"
#import "RunnerClasses+Recover.h"
#import "MFScopeChain.h"
#import "ORSearchedFunction.h"
#import "MFValue.h"
Expand Down Expand Up @@ -125,19 +125,23 @@ + (NSArray *)linkFunctions:(NSArray *)nodes scope:(MFScopeChain *)scope{
return normalStatements;
}

+ (void)reverse{
+ (void)recover{
[self recoverWithClearEnvironment:YES];
}
+ (void)recoverWithClearEnvironment:(BOOL)clear{
if (ORInterpreter.shared.currentNodes == nil) {
return;
}
for (ORNode *node in ORInterpreter.shared.currentNodes) {
[node reverse];
[node recover];
}
[[MFScopeChain topScope] clear];
[[MFStaticVarTable shareInstance] clear];
[[ORStructDeclareTable shareInstance] clear];
[[ORTypeSymbolTable shareInstance] clear];
[[ORffiResultCache shared] clear];
if (clear) {
[[MFScopeChain topScope] clear];
[[MFStaticVarTable shareInstance] clear];
[[ORStructDeclareTable shareInstance] clear];
[[ORTypeSymbolTable shareInstance] clear];
}
ORInterpreter.shared.currentNodes = [NSArray array];

}
@end
1 change: 1 addition & 0 deletions OCRunner/RunEnv/MFValue.m
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ - (void)setTypeBySearchInTypeSymbolTable{
}

- (void)writePointer:(void *)pointer typeEncode:(const char *)typeEncode{
typeEncode = typeEncode == NULL ? OCTypeStringPointer : typeEncode;
if (pointer == NULL) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion OCRunner/RunEnv/ORStructDeclare.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ - (void)addTypePair:(ORTypeVarPair *)typePair{
[self addTypePair:typePair forAlias:typePair.var.varname];
}
- (void)addTypePair:(ORTypeVarPair *)typePair forAlias:(NSString *)alias{
ORSymbolItem *item = [self symbolItemForTypeName:alias];
ORSymbolItem *item = [self symbolItemForTypeName:typePair.type.name];
if (item == nil) {
item = [[ORSymbolItem alloc] init];
item.typeEncode = [NSString stringWithUTF8String:typePair.typeEncode];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#import <Foundation/Foundation.h>
#import <ORPatchFile/ORPatchFile.h>
NS_ASSUME_NONNULL_BEGIN
@protocol OCReverse <NSObject>
- (void)reverse;
@protocol OCRecover <NSObject>
- (void)recover;
@end

@interface ORNode (Reverse) <OCReverse>
- (void)reverse;
@interface ORNode (Recover) <OCRecover>
- (void)recover;
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Jiang on 2021/2/1.
//

#import "RunnerClasses+Reverse.h"
#import "RunnerClasses+Recover.h"
#import "MFScopeChain.h"
#import "util.h"
#import "MFMethodMapTable.h"
Expand All @@ -20,7 +20,7 @@
#import "ORCoreImp.h"
#import "ORSearchedFunction.h"
#import "ORffiResultCache.h"
void reverse_method(BOOL isClassMethod, Class clazz, SEL sel){
void recover_method(BOOL isClassMethod, Class clazz, SEL sel){
NSString *orgSelName = [NSString stringWithFormat:@"ORG%@",NSStringFromSelector(sel)];
SEL orgsel = NSSelectorFromString(orgSelName);
Method ocMethod;
Expand All @@ -38,7 +38,7 @@ void reverse_method(BOOL isClassMethod, Class clazz, SEL sel){
}

@implementation ORNode (Reverse)
- (void)reverse{
- (void)recover{

}
@end
Expand All @@ -51,13 +51,13 @@ - (void)deallocffiReusltForKey:(NSValue *)key{
or_ffi_result_free(result);
}
}
- (void)reverse{
- (void)recover{
Class class = NSClassFromString(self.className);
// Reverse时,释放ffi_closure和ffi_type
for (ORMethodImplementation *imp in self.methods) {
SEL sel = NSSelectorFromString(imp.declare.selectorName);
BOOL isClassMethod = imp.declare.isClassMethod;
reverse_method(isClassMethod, class, sel);
recover_method(isClassMethod, class, sel);
[self deallocffiReusltForKey:[NSValue valueWithPointer:(__bridge void *)imp]];
CFRelease((__bridge CFTypeRef)(imp));
}
Expand All @@ -67,8 +67,8 @@ - (void)reverse{
NSString *str2 = name.length > 1 ? [name substringFromIndex:1] : nil;
SEL getterSEL = NSSelectorFromString(name);
SEL setterSEL = NSSelectorFromString([NSString stringWithFormat:@"set%@%@:",str1,str2]);
reverse_method(NO, class, getterSEL);
reverse_method(NO, class, setterSEL);
recover_method(NO, class, getterSEL);
recover_method(NO, class, setterSEL);
[self deallocffiReusltForKey:[NSValue valueWithPointer:(__bridge void *)prop]];
CFRelease((__bridge CFTypeRef)(prop));
}
Expand Down
2 changes: 1 addition & 1 deletion OCRunnerArm64.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "OCRunnerArm64"
s.version = "1.0.9"
s.version = "1.0.10"
s.summary = "OCRunnerArm64"
s.description = <<-DESC
Only Support Arm64, Execute Objective-C code Dynamically. iOS hotfix SDK.
Expand Down
6 changes: 6 additions & 0 deletions OCRunnerDemo/OCRunnerDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
0480FEE82580A77F00D6D914 /* ORWeakPropertyAndIvar.m in Sources */ = {isa = PBXBuildFile; fileRef = 0480FEE72580A77F00D6D914 /* ORWeakPropertyAndIvar.m */; };
04A634D125E4AB5A00DB31A6 /* ORRecoverClass.m in Sources */ = {isa = PBXBuildFile; fileRef = 04A634C925E4A79300DB31A6 /* ORRecoverClass.m */; };
32C54D1C256B4E6D00D067A2 /* jsonpatch in Resources */ = {isa = PBXBuildFile; fileRef = 32C54D1B256B4E6D00D067A2 /* jsonpatch */; };
994370B6E6826AB9C293F686 /* Pods_OCRunnerDemo_OCRunnerDemoUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2566C827C6925E5FB35FC01D /* Pods_OCRunnerDemo_OCRunnerDemoUITests.framework */; };
D71B1056F7894677E4150783 /* Pods_OCRunnerDemo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4A0001072323984F1566D9A7 /* Pods_OCRunnerDemo.framework */; };
Expand Down Expand Up @@ -53,6 +54,8 @@
038B17776BFE18A7DEB3184D /* Pods-OCRunnerDemo-OCRunnerDemoTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OCRunnerDemo-OCRunnerDemoTests.release.xcconfig"; path = "Target Support Files/Pods-OCRunnerDemo-OCRunnerDemoTests/Pods-OCRunnerDemo-OCRunnerDemoTests.release.xcconfig"; sourceTree = "<group>"; };
0480FEE62580A77F00D6D914 /* ORWeakPropertyAndIvar.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ORWeakPropertyAndIvar.h; sourceTree = "<group>"; };
0480FEE72580A77F00D6D914 /* ORWeakPropertyAndIvar.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ORWeakPropertyAndIvar.m; sourceTree = "<group>"; };
04A634C825E4A79300DB31A6 /* ORRecoverClass.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ORRecoverClass.h; sourceTree = "<group>"; };
04A634C925E4A79300DB31A6 /* ORRecoverClass.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ORRecoverClass.m; sourceTree = "<group>"; };
0EA667C064BFFB79847D34F5 /* Pods-OCRunnerDemoTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OCRunnerDemoTests.release.xcconfig"; path = "Target Support Files/Pods-OCRunnerDemoTests/Pods-OCRunnerDemoTests.release.xcconfig"; sourceTree = "<group>"; };
1C9D422B1CDEE6B7B4FBECF4 /* Pods-OCRunnerDemo-OCRunnerDemoUITests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OCRunnerDemo-OCRunnerDemoUITests.release.xcconfig"; path = "Target Support Files/Pods-OCRunnerDemo-OCRunnerDemoUITests/Pods-OCRunnerDemo-OCRunnerDemoUITests.release.xcconfig"; sourceTree = "<group>"; };
2566C827C6925E5FB35FC01D /* Pods_OCRunnerDemo_OCRunnerDemoUITests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_OCRunnerDemo_OCRunnerDemoUITests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -248,6 +251,8 @@
EEBF3344248E84A900DDE2E9 /* ORTestClassProperty.m */,
EEBF3345248E84A900DDE2E9 /* ORTestReplaceClass.h */,
EEBF334A248E84A900DDE2E9 /* ORTestReplaceClass.m */,
04A634C825E4A79300DB31A6 /* ORRecoverClass.h */,
04A634C925E4A79300DB31A6 /* ORRecoverClass.m */,
);
name = TestClass;
path = ../../OCRunnerTests/TestClass;
Expand Down Expand Up @@ -566,6 +571,7 @@
EEBF3351248E84AA00DDE2E9 /* ORTestReplaceClass.m in Sources */,
EEBF3353248E84D100DDE2E9 /* ORTestWithObjc.m in Sources */,
EEBF334E248E84AA00DDE2E9 /* ORTestClassProperty.m in Sources */,
04A634D125E4AB5A00DB31A6 /* ORRecoverClass.m in Sources */,
EEBF334F248E84AA00DDE2E9 /* ORTestClassIvar.m in Sources */,
EEBF334C248E84AA00DDE2E9 /* ORCallOCPropertyBlockTest.m in Sources */,
EEBF333E248E848100DDE2E9 /* OCRunnerTests.swift in Sources */,
Expand Down
29 changes: 29 additions & 0 deletions OCRunnerTests/ORTestWithObjc.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import <OCRunner.h>
#import "ORTypeVarPair+TypeEncode.h"
#import <oc2mangoLib/oc2mangoLib.h>
#import "ORRecoverClass.h"
#import <objc/message.h>
#import "ORWeakPropertyAndIvar.h"
@interface ORTestWithObjc : XCTestCase
Expand Down Expand Up @@ -577,6 +578,34 @@ - (void)testWeakPropertyAndIvar{
}
XCTAssert([ivarWeak isEqualToString:@"dealloc"]);
}
- (void)testRecover{
NSString *source = @"\
@interface ORRecoverClass : NSObject\
@property (nonatomic, assign)int value1;\
@property (nonatomic, copy)NSString *value2;\
@end\
@implementation ORRecoverClass\
+ (int)classMethodTest{ return 0; }\
- (int)methodTest1{ return 0; }\
- (int)methodTest2{ return 0; }\
- (int)value1{ return 1; }\
- (NSString *)value2{ return @\"123\"; }\
@end";
AST *ast = [OCParser parseSource:source];
[ORInterpreter excuteNodes:ast.nodes];
ORRecoverClass *object = [ORRecoverClass new];
XCTAssert([object value1] == 1);
XCTAssert([[object value2] isEqual:@"123"]);
XCTAssert([object methodTest1] == 0);
XCTAssert([object methodTest2] == 0);
XCTAssert([ORRecoverClass classMethodTest] == 0);
[ORInterpreter recoverWithClearEnvironment:NO];
XCTAssert([object value1] == 0);
XCTAssert([object value2] == nil);
XCTAssert([object methodTest1] == 1);
XCTAssert([object methodTest2] == 1);
XCTAssert([ORRecoverClass classMethodTest] == 1);
}
- (void)testOCRecursiveFunctionPerformanceExample {
[self measureBlock:^{
fibonaccia(20);
Expand Down
21 changes: 21 additions & 0 deletions OCRunnerTests/TestClass/ORRecoverClass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// ORRecoverClass.h
// OCRunnerDemoTests
//
// Created by Jiang on 2021/2/23.
// Copyright © 2021 SilverFruity. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface ORRecoverClass : NSObject
@property (nonatomic, assign)int value1;
@property (nonatomic, copy)NSString *value2;
+ (int)classMethodTest;
- (int)methodTest1;
- (int)methodTest2;
@end

NS_ASSUME_NONNULL_END
22 changes: 22 additions & 0 deletions OCRunnerTests/TestClass/ORRecoverClass.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// ORRecoverClass.m
// OCRunnerDemoTests
//
// Created by Jiang on 2021/2/23.
// Copyright © 2021 SilverFruity. All rights reserved.
//

#import "ORRecoverClass.h"

@implementation ORRecoverClass
+ (int)classMethodTest{
return 1;
}
- (int)methodTest1{
return 1;
}
- (int)methodTest2{
return 1;
}

@end

0 comments on commit 1d19130

Please sign in to comment.