forked from RetVal/objc-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classpair.m
376 lines (317 loc) · 11 KB
/
classpair.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// TEST_CFLAGS -Wno-deprecated-declarations
#include "test.h"
#include "testroot.i"
#include <objc/runtime.h>
#include <string.h>
#ifndef OBJC_NO_GC
#include <objc/objc-auto.h>
#include <auto_zone.h>
#endif
@protocol Proto
-(void) instanceMethod;
+(void) classMethod;
@optional
-(void) instanceMethod2;
+(void) classMethod2;
@end
@protocol Proto2
-(void) instanceMethod;
+(void) classMethod;
@optional
-(void) instanceMethod2;
+(void) classMethod_that_does_not_exist;
@end
@protocol Proto3
-(void) instanceMethod;
+(void) classMethod_that_does_not_exist;
@optional
-(void) instanceMethod2;
+(void) classMethod2;
@end
static int super_initialize;
@interface Super : TestRoot
@property int superProp;
@end
@implementation Super
@dynamic superProp;
+(void)initialize { super_initialize++; }
+(void) classMethod { fail("+[Super classMethod] called"); }
+(void) classMethod2 { fail("+[Super classMethod2] called"); }
-(void) instanceMethod { fail("-[Super instanceMethod] called"); }
-(void) instanceMethod2 { fail("-[Super instanceMethod2] called"); }
@end
@interface WeakSuper : Super { __weak id weakIvar; } @end
@implementation WeakSuper @end
static int state;
static void instance_fn(id self, SEL _cmd __attribute__((unused)))
{
testassert(!class_isMetaClass(object_getClass(self)));
state++;
}
static void class_fn(id self, SEL _cmd __attribute__((unused)))
{
testassert(class_isMetaClass(object_getClass(self)));
state++;
}
static void fail_fn(id self __attribute__((unused)), SEL _cmd)
{
fail("fail_fn '%s' called", sel_getName(_cmd));
}
static void cycle(void)
{
Class cls;
BOOL ok;
objc_property_t prop;
char namebuf[256];
testassert(!objc_getClass("Sub"));
testassert([Super class]);
// Test subclass with bells and whistles
cls = objc_allocateClassPair([Super class], "Sub", 0);
testassert(cls);
#ifndef OBJC_NO_GC
if (objc_collectingEnabled()) {
testassert(auto_zone_size(objc_collectableZone(), objc_unretainedPointer(cls)));
testassert(auto_zone_size(objc_collectableZone(), objc_unretainedPointer(object_getClass(cls))));
}
#endif
class_addMethod(cls, @selector(instanceMethod),
(IMP)&instance_fn, "v@:");
class_addMethod(object_getClass(cls), @selector(classMethod),
(IMP)&class_fn, "v@:");
class_addMethod(object_getClass(cls), @selector(initialize),
(IMP)&class_fn, "v@:");
class_addMethod(object_getClass(cls), @selector(load),
(IMP)&fail_fn, "v@:");
ok = class_addProtocol(cls, @protocol(Proto));
testassert(ok);
ok = class_addProtocol(cls, @protocol(Proto));
testassert(!ok);
char attrname[2];
char attrvalue[2];
objc_property_attribute_t attrs[1];
unsigned int attrcount = sizeof(attrs) / sizeof(attrs[0]);
attrs[0].name = attrname;
attrs[0].value = attrvalue;
strcpy(attrname, "T");
strcpy(attrvalue, "x");
strcpy(namebuf, "subProp");
ok = class_addProperty(cls, namebuf, attrs, attrcount);
testassert(ok);
strcpy(namebuf, "subProp");
ok = class_addProperty(cls, namebuf, attrs, attrcount);
testassert(!ok);
strcpy(attrvalue, "i");
class_replaceProperty(cls, namebuf, attrs, attrcount);
strcpy(namebuf, "superProp");
ok = class_addProperty(cls, namebuf, attrs, attrcount);
testassert(!ok);
bzero(namebuf, sizeof(namebuf));
bzero(attrs, sizeof(attrs));
bzero(attrname, sizeof(attrname));
bzero(attrvalue, sizeof(attrvalue));
#ifndef __LP64__
# define size 4
# define align 2
#else
#define size 8
# define align 3
#endif
/*
{
int ivar;
id ivarid;
id* ivaridstar;
Block_t ivarblock;
}
*/
ok = class_addIvar(cls, "ivar", 4, 2, "i");
testassert(ok);
ok = class_addIvar(cls, "ivarid", size, align, "@");
testassert(ok);
ok = class_addIvar(cls, "ivaridstar", size, align, "^@");
testassert(ok);
ok = class_addIvar(cls, "ivarblock", size, align, "@?");
testassert(ok);
ok = class_addIvar(cls, "ivar", 4, 2, "i");
testassert(!ok);
ok = class_addIvar(object_getClass(cls), "classvar", 4, 2, "i");
testassert(!ok);
objc_registerClassPair(cls);
// should call cls's +initialize, not super's
// Provoke +initialize using class_getMethodImplementation(class method)
// in order to test getNonMetaClass's slow case
super_initialize = 0;
state = 0;
class_getMethodImplementation(object_getClass(cls), @selector(class));
testassert(super_initialize == 0);
testassert(state == 1);
testassert(cls == [cls class]);
testassert(cls == objc_getClass("Sub"));
testassert(!class_isMetaClass(cls));
testassert(class_isMetaClass(object_getClass(cls)));
testassert(class_getSuperclass(cls) == [Super class]);
testassert(class_getSuperclass(object_getClass(cls)) == object_getClass([Super class]));
testassert(class_getInstanceSize(cls) >= sizeof(Class) + 4 + 3*size);
testassert(class_conformsToProtocol(cls, @protocol(Proto)));
if (objc_collectingEnabled()) {
testassert(0 == strcmp((char *)class_getIvarLayout(cls), "\x01\x13"));
testassert(NULL == class_getWeakIvarLayout(cls));
}
class_addMethod(cls, @selector(instanceMethod2),
(IMP)&instance_fn, "v@:");
class_addMethod(object_getClass(cls), @selector(classMethod2),
(IMP)&class_fn, "v@:");
ok = class_addIvar(cls, "ivar2", 4, 4, "i");
testassert(!ok);
ok = class_addIvar(object_getClass(cls), "classvar2", 4, 4, "i");
testassert(!ok);
ok = class_addProtocol(cls, @protocol(Proto2));
testassert(ok);
ok = class_addProtocol(cls, @protocol(Proto2));
testassert(!ok);
ok = class_addProtocol(cls, @protocol(Proto));
testassert(!ok);
attrs[0].name = attrname;
attrs[0].value = attrvalue;
strcpy(attrname, "T");
strcpy(attrvalue, "i");
strcpy(namebuf, "subProp2");
ok = class_addProperty(cls, namebuf, attrs, attrcount);
testassert(ok);
strcpy(namebuf, "subProp");
ok = class_addProperty(cls, namebuf, attrs, attrcount);
testassert(!ok);
strcpy(namebuf, "superProp");
ok = class_addProperty(cls, namebuf, attrs, attrcount);
testassert(!ok);
bzero(namebuf, sizeof(namebuf));
bzero(attrs, sizeof(attrs));
bzero(attrname, sizeof(attrname));
bzero(attrvalue, sizeof(attrvalue));
prop = class_getProperty(cls, "subProp");
testassert(prop);
testassert(0 == strcmp(property_getName(prop), "subProp"));
testassert(0 == strcmp(property_getAttributes(prop), "Ti"));
prop = class_getProperty(cls, "subProp2");
testassert(prop);
testassert(0 == strcmp(property_getName(prop), "subProp2"));
testassert(0 == strcmp(property_getAttributes(prop), "Ti"));
// note: adding more methods here causes a false leak check failure
state = 0;
[cls classMethod];
[cls classMethod2];
testassert(state == 2);
// put instance tests on a separate thread so they
// are reliably GC'd before class destruction
testonthread(^{
id obj = [cls new];
state = 0;
[obj instanceMethod];
[obj instanceMethod2];
testassert(state == 2);
RELEASE_VAR(obj);
});
// Test ivar layouts of sub-subclass
Class cls2 = objc_allocateClassPair(cls, "SubSub", 0);
testassert(cls2);
/*
{
id ivarid2;
id idarray[16];
void* ptrarray[16];
char a;
char b;
char c;
}
*/
ok = class_addIvar(cls2, "ivarid2", size, align, "@");
testassert(ok);
ok = class_addIvar(cls2, "idarray", 16*sizeof(id), align, "[16@]");
testassert(ok);
ok = class_addIvar(cls2, "ptrarray", 16*sizeof(void*), align, "[16^]");
testassert(ok);
ok = class_addIvar(cls2, "a", 1, 0, "c");
testassert(ok);
ok = class_addIvar(cls2, "b", 1, 0, "c");
testassert(ok);
ok = class_addIvar(cls2, "c", 1, 0, "c");
testassert(ok);
objc_registerClassPair(cls2);
if (objc_collectingEnabled()) {
testassert(0 == strcmp((char *)class_getIvarLayout(cls2), "\x01\x1f\x05\xf0\x10"));
testassert(NULL == class_getWeakIvarLayout(cls2));
}
// 1-byte ivars should be well packed
testassert(ivar_getOffset(class_getInstanceVariable(cls2, "b")) ==
ivar_getOffset(class_getInstanceVariable(cls2, "a")) + 1);
testassert(ivar_getOffset(class_getInstanceVariable(cls2, "c")) ==
ivar_getOffset(class_getInstanceVariable(cls2, "b")) + 1);
testcollect(); // GC: finalize "obj" above before disposing its class
objc_disposeClassPair(cls2);
objc_disposeClassPair(cls);
testassert(!objc_getClass("Sub"));
// Test unmodified ivar layouts
cls = objc_allocateClassPair([Super class], "Sub2", 0);
testassert(cls);
objc_registerClassPair(cls);
if (objc_collectingEnabled()) {
const char *l1, *l2;
l1 = (char *)class_getIvarLayout([Super class]);
l2 = (char *)class_getIvarLayout(cls);
testassert(l1 == l2 || 0 == strcmp(l1, l2));
l1 = (char *)class_getWeakIvarLayout([Super class]);
l2 = (char *)class_getWeakIvarLayout(cls);
testassert(l1 == l2 || 0 == strcmp(l1, l2));
}
objc_disposeClassPair(cls);
cls = objc_allocateClassPair([WeakSuper class], "Sub3", 0);
testassert(cls);
objc_registerClassPair(cls);
if (objc_collectingEnabled()) {
const char *l1, *l2;
l1 = (char *)class_getIvarLayout([WeakSuper class]);
l2 = (char *)class_getIvarLayout(cls);
testassert(l1 == l2 || 0 == strcmp(l1, l2));
l1 = (char *)class_getWeakIvarLayout([WeakSuper class]);
l2 = (char *)class_getWeakIvarLayout(cls);
testassert(l1 == l2 || 0 == strcmp(l1, l2));
}
objc_disposeClassPair(cls);
// Test layout setters
if (objc_collectingEnabled()) {
cls = objc_allocateClassPair([Super class], "Sub4", 0);
testassert(cls);
class_setIvarLayout(cls, (uint8_t *)"foo");
class_setWeakIvarLayout(cls, NULL);
objc_registerClassPair(cls);
testassert(0 == strcmp("foo", (char *)class_getIvarLayout(cls)));
testassert(NULL == class_getWeakIvarLayout(cls));
objc_disposeClassPair(cls);
cls = objc_allocateClassPair([Super class], "Sub5", 0);
testassert(cls);
class_setIvarLayout(cls, NULL);
class_setWeakIvarLayout(cls, (uint8_t *)"bar");
objc_registerClassPair(cls);
testassert(NULL == class_getIvarLayout(cls));
testassert(0 == strcmp("bar", (char *)class_getWeakIvarLayout(cls)));
objc_disposeClassPair(cls);
}
}
int main()
{
int count = 1000;
testonthread(^{ cycle(); });
testonthread(^{ cycle(); });
testonthread(^{ cycle(); });
leak_mark();
while (count--) {
testonthread(^{ cycle(); });
}
#if __OBJC_GC__
testwarn("rdar://19042235 possible leaks suppressed under GC");
leak_check(16000);
#else
leak_check(0);
#endif
succeed(__FILE__);
}