-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathcategory.m
241 lines (205 loc) · 7.61 KB
/
category.m
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
// TEST_CFLAGS -Wl,-no_objc_category_merging
#include "test.h"
#include "testroot.i"
#include <string.h>
#include <objc/runtime.h>
#if __LP64__
# define PTR " .quad "
#else
# define PTR " .long "
#endif
static int state = 0;
@interface Super : TestRoot @end
@implementation Super
-(void)instancemethod { fail("-instancemethod not overridden by category"); }
+(void)method { fail("+method not overridden by category"); }
@end
@interface Super (Category) @end
@implementation Super (Category)
+(void)method {
testprintf("in [Super(Category) method]\n");
testassert(self == [Super class]);
testassert(state == 0);
state = 1;
}
-(void)instancemethod {
testprintf("in [Super(Category) instancemethod]\n");
testassert(object_getClass(self) == [Super class]);
testassert(state == 1);
state = 2;
}
@end
@interface Super (PropertyCategory)
@property int i;
@property(class) int i;
@end
@implementation Super (PropertyCategory)
- (int)i { return 0; }
- (void)setI:(int)value { (void)value; }
+ (int)i { return 0; }
+ (void)setI:(int)value { (void)value; }
@end
// rdar://5086110 memory smasher in category with class method and property
@interface Super (r5086110)
@property int property5086110;
@end
@implementation Super (r5086110)
+(void)method5086110 {
fail("method method5086110 called!");
}
- (int)property5086110 { fail("property5086110 called!"); return 0; }
- (void)setProperty5086110:(int)value { fail("setProperty5086110 called!"); (void)value; }
@end
// rdar://25605427 incorrect handling of class properties in 10.11 and earlier
@interface Super25605427 : TestRoot
@property(class, readonly) int i;
@end
@implementation Super25605427
+(int)i { return 0; }
@end
@interface Super25605427 (r25605427a)
@property(readonly) int r25605427a1;
@end
@implementation Super25605427 (r25605427a)
-(int)r25605427a1 { return 0; }
+(int)r25605427a2 { return 0; }
@end
@interface Super25605427 (r25605427b)
@property(readonly) int r25605427b1;
@end
@implementation Super25605427 (r25605427b)
-(int)r25605427b1 { return 0; }
+(int)r25605427b2 { return 0; }
@end
@interface Super25605427 (r25605427c)
@property(readonly) int r25605427c1;
@end
@implementation Super25605427 (r25605427c)
-(int)r25605427c1 { return 0; }
+(int)r25605427c2 { return 0; }
@end
@interface Super25605427 (r25605427d)
@property(readonly) int r25605427d1;
@end
@implementation Super25605427 (r25605427d)
-(int)r25605427d1 { return 0; }
+(int)r25605427d2 { return 0; }
@end
@interface PropertyClass : Super {
int q;
}
@property(readonly) int q;
@end
@implementation PropertyClass
@synthesize q;
@end
@interface PropertyClass (PropertyCategory)
@property int q;
@end
@implementation PropertyClass (PropertyCategory)
@dynamic q;
@end
// Manually build a category that goes in __objc_catlist2.
#if __has_feature(ptrauth_calls)
#define SIGNED_CATEGORY_IMP "@AUTH(ia,0,addr)"
#define SIGNED_METHOD_LIST "@AUTH(da,0xC310,addr) "
#else
#define SIGNED_CATEGORY_IMP
#define SIGNED_METHOD_LIST
#endif
#if TARGET_OS_EXCLAVEKIT
// On ExclaveKit, all method lists are signed
#define SIGNED_OBJC_SEL "@AUTH(da,0x57c2,addr)"
#define SIGNED_METHOD_TYPES "@AUTH(da,0xdec6,addr)"
#else
#define SIGNED_OBJC_SEL
#define SIGNED_METHOD_TYPES
#endif
asm(
" .section __DATA,__objc_const \n"
"L_catlist2CategoryName: \n"
" .asciz \"Category_catlist2\" \n"
"L_catlist2MethodString: \n"
" .asciz \"catlist2Method\" \n"
"L_catlist2MethodTypes: \n"
" .asciz \"i16@0:8\" \n"
" .p2align 3 \n"
"l_OBJC_$_CATEGORY_INSTANCE_METHODS_Super_$_Category_catlist2: \n"
" .long 24 \n"
" .long 1 \n"
" " PTR " L_catlist2MethodString" SIGNED_OBJC_SEL " \n"
" " PTR " L_catlist2MethodTypes" SIGNED_METHOD_TYPES " \n"
" " PTR " _catlist2MethodImplementation" SIGNED_CATEGORY_IMP" \n"
" .p2align 3 \n"
"l_OBJC_$_CATEGORY_Super_$_Category_catlist2: \n"
" " PTR " L_catlist2CategoryName \n"
" " PTR " _OBJC_CLASS_$_Super \n"
" " PTR " l_OBJC_$_CATEGORY_INSTANCE_METHODS_Super_$_Category_catlist2" SIGNED_METHOD_LIST "\n"
" " PTR " 0 \n"
" " PTR " 0 \n"
" " PTR " 0 \n"
" " PTR " 0 \n"
" .long 64 \n"
" .space 4 \n"
" .section __DATA,__objc_catlist2 \n"
" .p2align 3 \n"
" " PTR " l_OBJC_$_CATEGORY_Super_$_Category_catlist2 \n"
" .text \n"
);
@interface Super (Category_catlist2)
- (int)catlist2Method;
@end
EXTERN_C int catlist2MethodImplementation(id self __unused, SEL _cmd __unused) {
return 0;
}
int main()
{
{
// rdar://25605427 bugs in 10.11 and earlier when metaclass
// has a property and category has metaclass additions.
// Memory smasher in buildPropertyList (caught by guard malloc)
Class cls = [Super25605427 class];
// Incorrect attachment of instance properties from category to metacls
testassert(class_getProperty(cls, "r25605427d1"));
testassert(! class_getProperty(object_getClass(cls), "r25605427d1"));
}
// methods introduced by category
state = 0;
[Super method];
[[Super new] instancemethod];
testassert(state == 2);
// property introduced by category
objc_property_t p = class_getProperty([Super class], "i");
testassert(p);
testassert(0 == strcmp(property_getName(p), "i"));
testassert(property_getAttributes(p));
objc_property_t p2 = class_getProperty(object_getClass([Super class]), "i");
testassert(p2);
testassert(p != p2);
testassert(0 == strcmp(property_getName(p2), "i"));
testassert(property_getAttributes(p2));
// methods introduced by category's property
Method m;
m = class_getInstanceMethod([Super class], @selector(i));
testassert(m);
m = class_getInstanceMethod([Super class], @selector(setI:));
testassert(m);
m = class_getClassMethod([Super class], @selector(i));
testassert(m);
m = class_getClassMethod([Super class], @selector(setI:));
testassert(m);
// class's property shadowed by category's property
objc_property_t *plist = class_copyPropertyList([PropertyClass class], NULL);
testassert(plist);
testassert(plist[0]);
testassert(0 == strcmp(property_getName(plist[0]), "q"));
testassert(0 == strcmp(property_getAttributes(plist[0]), "Ti,D"));
testassert(plist[1]);
testassert(0 == strcmp(property_getName(plist[1]), "q"));
testassert(0 == strcmp(property_getAttributes(plist[1]), "Ti,R,Vq"));
testassert(!plist[2]);
free(plist);
// method introduced by category in catlist2
testassert([[Super new] catlist2Method] == 0);
succeed(__FILE__);
}