-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathZDLibffiDemoTests.m
More file actions
251 lines (202 loc) · 7.65 KB
/
ZDLibffiDemoTests.m
File metadata and controls
251 lines (202 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//
// ZDLibffiDemoTests.m
// ZDLibffiDemoTests
//
// Created by Zero.D.Saber on 2019/12/12.
// Copyright © 2019 Zero.D.Saber. All rights reserved.
//
// libffi基本用法
#import <XCTest/XCTest.h>
#import "NSObject+ZDAOP.h"
NSUInteger const MaxCount = 100000;
@interface ZDLibffiDemoTests : XCTestCase
@end
@implementation ZDLibffiDemoTests
- (void)setUp {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
- (void)testExample {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
//#########################################################
#pragma mark - #######################################################
#pragma mark - 调用C函数
static int cFunc(int a , int b, int c) {
int x = a + b + c;
return x;
}
- (void)testCallCFunc {
ffi_cif cif;
ffi_type *argTypes[] = {&ffi_type_sint, &ffi_type_sint, &ffi_type_sint};
ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 3, &ffi_type_sint, argTypes);
int a = 123;
int b = 456;
int c = 890;
void **args = alloca(sizeof(void *) * 3);
args[0] = &a;
args[1] = &b;
args[2] = &c;
int retValue;
ffi_call(&cif, (void *)cFunc, &retValue, args);
int m = cFunc(a, b, c);
XCTAssertEqual(retValue, m);
}
#pragma mark - 调用OC方法
// 直接调用OC方法
- (void)testCallObjC1 {
SEL selector = @selector(a:b:c:);
NSMethodSignature *signature = [self methodSignatureForSelector:selector];
ffi_cif *cif = alloca(sizeof(ffi_cif));
ffi_type *argTypes[] = {&ffi_type_pointer, &ffi_type_schar, &ffi_type_sint, &ffi_type_pointer, &ffi_type_pointer};
ffi_prep_cif(cif, FFI_DEFAULT_ABI, (uint32_t)signature.numberOfArguments, &ffi_type_pointer, argTypes);
NSInteger arg1 = 100;
NSString *arg2 = @"hello";
id arg3 = NSObject.class;
void *args[] = {(void *)&self, &selector, &arg1, &arg2, &arg3};
__unsafe_unretained id ret = nil;
IMP func = [self methodForSelector:selector];
ffi_call(cif, func, &ret, args);
NSLog(@"===== %@", ret);
}
- (id)a:(NSInteger)a b:(NSString *)b c:(id)c {
NSString *ret = [NSString stringWithFormat:@"%zd + %@ + %@", a, b, c];
NSLog(@"result = %@", ret);
return ret;
}
- (void)testCallObjC2 {
SEL selector = @selector(aa:bb:cc:);
NSMethodSignature *signature = [self methodSignatureForSelector:selector];
ffi_cif *cif = alloca(sizeof(ffi_cif));
ffi_type *argTypes[] = {&ffi_type_pointer, &ffi_type_schar, &ffi_type_sint, &ffi_type_pointer, &ffi_type_pointer};
ffi_type *retType = &ffi_type_sint;
ffi_prep_cif(cif, FFI_DEFAULT_ABI, (uint32_t)signature.numberOfArguments, retType, argTypes);
NSInteger arg1 = 100;
NSString *arg2 = @"hello";
id arg3 = NSObject.class;
void *args[] = {(void *)&self, &selector, &arg1, &arg2, &arg3};
// https://github.com/bang590/JSPatch/blob/e80a5573af223936647e5869871804bfd7751d4f/Extensions/JPCFunction/JPCFunction.m
#if 0
// 正常
IMP afunc = [self methodForSelector:selector];
void *retPtr = alloca(retType->size);
ffi_call(cif, afunc, retPtr, args);
NSLog(@"===== %d", *(int *)retPtr);
#endif
#if 1
// 正常
// 如果先定义ret再获取bfunc则crash。why?
IMP bfunc = [self methodForSelector:selector];
int ret;
ffi_call(cif, bfunc, &ret, args);
NSLog(@"===== %d", ret);
#endif
#if 0
// crash
int ret;
IMP bfunc = [self methodForSelector:selector];
ffi_call(cif, bfunc, &ret, args);
NSLog(@"===== %d", ret);
#endif
}
- (int)aa:(NSInteger)a bb:(NSString *)b cc:(id)c {
NSString *ret = [NSString stringWithFormat:@"%zd + %@ + %@", a, b, c];
NSLog(@"result = %@", ret);
return 100;
}
#pragma mark - --------------------------
#pragma mark - BindC
struct ZDUserData {
char *c;
int a;
void *imp;
};
static void bindCFunc(ffi_cif *cif, int *ret, void **args, void *userdata) {
struct ZDUserData ud = *(struct ZDUserData *)userdata;
*ret = 999;
printf("==== %s, %d\n", ud.c, *(int *)ret);
//ffi_call(cif, ud.imp, ret, args); //再调用此方法会进入死循环
}
- (void)testBindCFunc {
ffi_cif cif;
ffi_type *argTypes[] = {&ffi_type_sint, &ffi_type_sint, &ffi_type_sint};
unsigned int argTypesCount = sizeof(argTypes) / sizeof(ffi_type *);
ffi_prep_cif(&cif, FFI_DEFAULT_ABI, argTypesCount, &ffi_type_sint, argTypes);
// 新定义一个C函数指针
int(*newCFunc)(int, int, int) = NULL;
ffi_closure *cloure = ffi_closure_alloc(sizeof(ffi_closure), (void *)&newCFunc);
struct ZDUserData userdata = {"元旦快乐", 8888, newCFunc};
// 将newCFunc与bingCFunc关联
ffi_status status = ffi_prep_closure_loc(cloure, &cif, (void *)bindCFunc, &userdata, newCFunc);
if (status != FFI_OK) {
NSLog(@"新函数指针生成失败");
return;
}
int ret = newCFunc(11, 34, 24);
printf("********** %d\n", ret);
ffi_closure_free(cloure);
}
#pragma mark - HookOC
static void zdfunc(ffi_cif *cif, void *ret, void **args, void *userdata) {
ZDFfiHookInfo *info = (__bridge ZDFfiHookInfo *)userdata;
// 打印参数
NSMethodSignature *methodSignature = info.signature;
NSInteger beginIndex = 2;
if (info.isBlock) {
beginIndex = 1;
}
for (NSInteger i = beginIndex; i < methodSignature.numberOfArguments; ++i) {
id argValue = ZD_ArgumentAtIndex(methodSignature, args, i);
NSLog(@"arg ==> index: %ld, value: %@", i, argValue);
}
// https://github.com/sunnyxx/libffi-iOS/blob/master/Demo/ViewController.m
// 根据cif (函数原型,函数指针,返回值内存指针,函数参数) 调用这个函数
ffi_call(cif, info->_originalIMP, ret, args);
}
- (void)testHookOC {
SEL selector = @selector(x:y:z:);
NSMethodSignature *signature = [self methodSignatureForSelector:selector];
IMP originIMP = [self methodForSelector:selector];
//ffi_type *argTypes[] = {&ffi_type_pointer, &ffi_type_pointer, &ffi_type_sint, &ffi_type_pointer, &ffi_type_pointer};
ffi_type **argTypes = calloc(signature.numberOfArguments, sizeof(ffi_type *));
argTypes[0] = &ffi_type_pointer;
argTypes[1] = &ffi_type_schar;
argTypes[2] = &ffi_type_sint;
argTypes[3] = &ffi_type_pointer;
argTypes[4] = &ffi_type_pointer;
ffi_cif *cif = calloc(1, sizeof(ffi_cif));
ffi_prep_cif(cif, FFI_DEFAULT_ABI, (uint32_t)signature.numberOfArguments, &ffi_type_pointer, argTypes);
IMP newIMP = NULL;
ffi_closure *cloure = ffi_closure_alloc(sizeof(ffi_closure), (void *)&newIMP);
ZDFfiHookInfo *info = ({
ZDFfiHookInfo *model = ZDFfiHookInfo.new;
model->_cif = cif;
model->_argTypes = argTypes;
model->_closure = cloure;
model->_originalIMP = originIMP;
model->_newIMP = newIMP;
model.signature = signature;
model;
});
ffi_status status = ffi_prep_closure_loc(cloure, cif, zdfunc, (__bridge void *)info, newIMP);
if (status != FFI_OK) {
NSLog(@"新函数指针生成失败");
return;
}
//替换实现
Method method = class_getInstanceMethod(self.class, selector);
method_setImplementation(method, newIMP);
NSInteger arg1 = 100;
NSString *arg2 = @"Zero.D.Saber";
id arg3 = @(909090);
[self x:arg1 y:arg2 z:arg3];
}
- (id)x:(NSInteger)a y:(NSString *)b z:(id)c {
NSString *ret = [NSString stringWithFormat:@"%zd + %@ + %@", a, b, c];
NSLog(@"result = %@", ret);
return ret;
}
@end