Repository navigation
Expand file tree
/
Copy pathJPDMethodNeutralizer.mm
More file actions
129 lines (112 loc) · 3.52 KB
/
Copy pathJPDMethodNeutralizer.mm
File metadata and controls
129 lines (112 loc) · 3.52 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
#import "JPDMethodNeutralizer.h"
#import <objc/message.h>
#include <stdint.h>
#include <stdlib.h>
static void JPDDisabledVoid(id self, SEL _cmd, ...) {
(void)self;
(void)_cmd;
}
static id JPDDisabledObject(id self, SEL _cmd, ...) {
(void)self;
(void)_cmd;
return nil;
}
static uintptr_t JPDDisabledInteger(id self, SEL _cmd, ...) {
(void)self;
(void)_cmd;
return 0;
}
static double JPDDisabledFloatingPoint(id self, SEL _cmd, ...) {
(void)self;
(void)_cmd;
return 0.0;
}
static NSMethodSignature *JPDDisabledMethodSignature(id self, SEL _cmd,
SEL selector) {
(void)_cmd;
Class dispatchClass = object_getClass(self);
Method method = class_getInstanceMethod(dispatchClass, selector);
const char *typeEncoding = method == NULL ? NULL : method_getTypeEncoding(method);
return typeEncoding == NULL
? nil
: [NSMethodSignature signatureWithObjCTypes:typeEncoding];
}
static void JPDDisabledForwardInvocation(id self, SEL _cmd,
NSInvocation *invocation) {
(void)self;
(void)_cmd;
NSUInteger returnLength = invocation.methodSignature.methodReturnLength;
if (returnLength == 0) {
return;
}
void *zeroValue = calloc(1, returnLength);
if (zeroValue != NULL) {
[invocation setReturnValue:zeroValue];
free(zeroValue);
}
}
static char JPDReturnTypeForMethod(Method method) {
char returnType[32] = {0};
method_getReturnType(method, returnType, sizeof(returnType));
const char *type = returnType;
while (*type == 'r' || *type == 'n' || *type == 'N' || *type == 'o' ||
*type == 'O' || *type == 'R' || *type == 'V') {
type++;
}
return *type;
}
static IMP JPDReplacementForMethod(Method method) {
switch (JPDReturnTypeForMethod(method)) {
case 'v':
return (IMP)JPDDisabledVoid;
case '@':
case '#':
return (IMP)JPDDisabledObject;
case 'f':
case 'd':
case 'D':
return (IMP)JPDDisabledFloatingPoint;
case 'B':
case 'c':
case 'C':
case 's':
case 'S':
case 'i':
case 'I':
case 'l':
case 'L':
case 'q':
case 'Q':
case '^':
case ':':
case '*':
return (IMP)JPDDisabledInteger;
default:
return (IMP)_objc_msgForward;
}
}
NSUInteger JPDNeutralizeMethodsDeclaredByClass(Class targetClass) {
unsigned int methodCount = 0;
Method *methods = class_copyMethodList(targetClass, &methodCount);
NSUInteger neutralizedCount = 0;
for (unsigned int index = 0; index < methodCount; index++) {
Method method = methods[index];
SEL selector = method_getName(method);
if (selector == @selector(methodSignatureForSelector:) ||
selector == @selector(forwardInvocation:)) {
continue;
}
IMP replacement = JPDReplacementForMethod(method);
if (method_getImplementation(method) == replacement) {
continue;
}
method_setImplementation(method, replacement);
neutralizedCount++;
}
free(methods);
class_replaceMethod(targetClass, @selector(methodSignatureForSelector:),
(IMP)JPDDisabledMethodSignature, "@@::");
class_replaceMethod(targetClass, @selector(forwardInvocation:),
(IMP)JPDDisabledForwardInvocation, "v@:@");
return neutralizedCount;
}