]> git.saurik.com Git - apple/objc4.git/blob - test/customrr.m
objc4-779.1.tar.gz
[apple/objc4.git] / test / customrr.m
1 // These options must match customrr2.m
2 // TEST_CONFIG MEM=mrc
3 /*
4 TEST_BUILD
5 $C{COMPILE} $DIR/customrr.m -fvisibility=default -o customrr.exe -fno-objc-convert-messages-to-runtime-calls
6 $C{COMPILE} -bundle -bundle_loader customrr.exe $DIR/customrr-cat1.m -o customrr-cat1.bundle
7 $C{COMPILE} -bundle -bundle_loader customrr.exe $DIR/customrr-cat2.m -o customrr-cat2.bundle
8 END
9 */
10
11
12 #include "test.h"
13 #include <dlfcn.h>
14 #include <Foundation/NSObject.h>
15
16 static int Retains;
17 static int Releases;
18 static int Autoreleases;
19 static int RetainCounts;
20 static int PlusRetains;
21 static int PlusReleases;
22 static int PlusAutoreleases;
23 static int PlusRetainCounts;
24 static int Allocs;
25 static int AllocWithZones;
26
27 static int SubRetains;
28 static int SubReleases;
29 static int SubAutoreleases;
30 static int SubRetainCounts;
31 static int SubPlusRetains;
32 static int SubPlusReleases;
33 static int SubPlusAutoreleases;
34 static int SubPlusRetainCounts;
35 static int SubAllocs;
36 static int SubAllocWithZones;
37
38 static int Imps;
39
40 static id imp_fn(id self, SEL _cmd __unused, ...)
41 {
42 Imps++;
43 return self;
44 }
45
46 static void zero(void) {
47 Retains = 0;
48 Releases = 0;
49 Autoreleases = 0;
50 RetainCounts = 0;
51 PlusRetains = 0;
52 PlusReleases = 0;
53 PlusAutoreleases = 0;
54 PlusRetainCounts = 0;
55 Allocs = 0;
56 AllocWithZones = 0;
57
58 SubRetains = 0;
59 SubReleases = 0;
60 SubAutoreleases = 0;
61 SubRetainCounts = 0;
62 SubPlusRetains = 0;
63 SubPlusReleases = 0;
64 SubPlusAutoreleases = 0;
65 SubPlusRetainCounts = 0;
66 SubAllocs = 0;
67 SubAllocWithZones = 0;
68
69 Imps = 0;
70 }
71
72
73 id HackRetain(id self, SEL _cmd __unused) { Retains++; return self; }
74 void HackRelease(id self __unused, SEL _cmd __unused) { Releases++; }
75 id HackAutorelease(id self, SEL _cmd __unused) { Autoreleases++; return self; }
76 NSUInteger HackRetainCount(id self __unused, SEL _cmd __unused) { RetainCounts++; return 1; }
77 id HackPlusRetain(id self, SEL _cmd __unused) { PlusRetains++; return self; }
78 void HackPlusRelease(id self __unused, SEL _cmd __unused) { PlusReleases++; }
79 id HackPlusAutorelease(id self, SEL _cmd __unused) { PlusAutoreleases++; return self; }
80 NSUInteger HackPlusRetainCount(id self __unused, SEL _cmd __unused) { PlusRetainCounts++; return 1; }
81 id HackAlloc(Class self, SEL _cmd __unused) { Allocs++; return class_createInstance(self, 0); }
82 id HackAllocWithZone(Class self, SEL _cmd __unused) { AllocWithZones++; return class_createInstance(self, 0); }
83
84
85 @interface OverridingSub : NSObject @end
86 @implementation OverridingSub
87
88 -(id) retain { SubRetains++; return self; }
89 +(id) retain { SubPlusRetains++; return self; }
90 -(oneway void) release { SubReleases++; }
91 +(oneway void) release { SubPlusReleases++; }
92 -(id) autorelease { SubAutoreleases++; return self; }
93 +(id) autorelease { SubPlusAutoreleases++; return self; }
94 -(NSUInteger) retainCount { SubRetainCounts++; return 1; }
95 +(NSUInteger) retainCount { SubPlusRetainCounts++; return 1; }
96
97 @end
98
99 @interface OverridingASub : NSObject @end
100 @implementation OverridingASub
101 +(id) alloc { SubAllocs++; return class_createInstance(self, 0); }
102 @end
103
104 @interface OverridingAWZSub : NSObject @end
105 @implementation OverridingAWZSub
106 +(id) allocWithZone:(NSZone * __unused)z { SubAllocWithZones++; return class_createInstance(self, 0); }
107 @end
108
109 @interface OverridingAAWZSub : NSObject @end
110 @implementation OverridingAAWZSub
111 +(id) alloc { SubAllocs++; return class_createInstance(self, 0); }
112 +(id) allocWithZone:(NSZone * __unused)z { SubAllocWithZones++; return class_createInstance(self, 0); }
113 @end
114
115
116 @interface InheritingSub : NSObject @end
117 @implementation InheritingSub @end
118
119 @interface InheritingSub2 : NSObject @end
120 @implementation InheritingSub2 @end
121 @interface InheritingSub2_2 : InheritingSub2 @end
122 @implementation InheritingSub2_2 @end
123
124 @interface InheritingSub3 : NSObject @end
125 @implementation InheritingSub3 @end
126 @interface InheritingSub3_2 : InheritingSub3 @end
127 @implementation InheritingSub3_2 @end
128
129 @interface InheritingSub4 : NSObject @end
130 @implementation InheritingSub4 @end
131 @interface InheritingSub4_2 : InheritingSub4 @end
132 @implementation InheritingSub4_2 @end
133
134 @interface InheritingSub5 : NSObject @end
135 @implementation InheritingSub5 @end
136 @interface InheritingSub5_2 : InheritingSub5 @end
137 @implementation InheritingSub5_2 @end
138
139 @interface InheritingSub6 : NSObject @end
140 @implementation InheritingSub6 @end
141 @interface InheritingSub6_2 : InheritingSub6 @end
142 @implementation InheritingSub6_2 @end
143
144 @interface InheritingSub7 : NSObject @end
145 @implementation InheritingSub7 @end
146 @interface InheritingSub7_2 : InheritingSub7 @end
147 @implementation InheritingSub7_2 @end
148
149 @interface InheritingSubCat : NSObject @end
150 @implementation InheritingSubCat @end
151 @interface InheritingSubCat_2 : InheritingSubCat @end
152 @implementation InheritingSubCat_2 @end
153
154
155 extern uintptr_t OBJC_CLASS_$_UnrealizedSubA1;
156 @interface UnrealizedSubA1 : NSObject @end
157 @implementation UnrealizedSubA1 @end
158 extern uintptr_t OBJC_CLASS_$_UnrealizedSubA2;
159 @interface UnrealizedSubA2 : NSObject @end
160 @implementation UnrealizedSubA2 @end
161 extern uintptr_t OBJC_CLASS_$_UnrealizedSubA3;
162 @interface UnrealizedSubA3 : NSObject @end
163 @implementation UnrealizedSubA3 @end
164
165 extern uintptr_t OBJC_CLASS_$_UnrealizedSubB1;
166 @interface UnrealizedSubB1 : NSObject @end
167 @implementation UnrealizedSubB1 @end
168 extern uintptr_t OBJC_CLASS_$_UnrealizedSubB2;
169 @interface UnrealizedSubB2 : NSObject @end
170 @implementation UnrealizedSubB2 @end
171 extern uintptr_t OBJC_CLASS_$_UnrealizedSubB3;
172 @interface UnrealizedSubB3 : NSObject @end
173 @implementation UnrealizedSubB3 @end
174
175 extern uintptr_t OBJC_CLASS_$_UnrealizedSubC1;
176 @interface UnrealizedSubC1 : NSObject @end
177 @implementation UnrealizedSubC1 @end
178 extern uintptr_t OBJC_CLASS_$_UnrealizedSubC2;
179 @interface UnrealizedSubC2 : NSObject @end
180 @implementation UnrealizedSubC2 @end
181 extern uintptr_t OBJC_CLASS_$_UnrealizedSubC3;
182 @interface UnrealizedSubC3 : NSObject @end
183 @implementation UnrealizedSubC3 @end
184
185
186 int main(int argc __unused, char **argv)
187 {
188 objc_autoreleasePoolPush();
189
190 // Hack NSObject's RR methods.
191 // Don't use runtime functions to do this -
192 // we want the runtime to think that these are NSObject's real code
193 {
194 #if __has_feature(ptrauth_calls)
195 typedef IMP __ptrauth_objc_method_list_imp MethodListIMP;
196 #else
197 typedef IMP MethodListIMP;
198 #endif
199
200 Class cls = [NSObject class];
201 IMP imp = class_getMethodImplementation(cls, @selector(retain));
202 MethodListIMP *m = (MethodListIMP *)
203 class_getInstanceMethod(cls, @selector(retain));
204 testassert(m[2] == imp); // verify Method struct is as we expect
205
206 m = (MethodListIMP *)class_getInstanceMethod(cls, @selector(retain));
207 m[2] = (IMP)HackRetain;
208 m = (MethodListIMP *)class_getInstanceMethod(cls, @selector(release));
209 m[2] = (IMP)HackRelease;
210 m = (MethodListIMP *)class_getInstanceMethod(cls, @selector(autorelease));
211 m[2] = (IMP)HackAutorelease;
212 m = (MethodListIMP *)class_getInstanceMethod(cls, @selector(retainCount));
213 m[2] = (IMP)HackRetainCount;
214 m = (MethodListIMP *)class_getClassMethod(cls, @selector(retain));
215 m[2] = (IMP)HackPlusRetain;
216 m = (MethodListIMP *)class_getClassMethod(cls, @selector(release));
217 m[2] = (IMP)HackPlusRelease;
218 m = (MethodListIMP *)class_getClassMethod(cls, @selector(autorelease));
219 m[2] = (IMP)HackPlusAutorelease;
220 m = (MethodListIMP *)class_getClassMethod(cls, @selector(retainCount));
221 m[2] = (IMP)HackPlusRetainCount;
222 m = (MethodListIMP *)class_getClassMethod(cls, @selector(alloc));
223 m[2] = (IMP)HackAlloc;
224 m = (MethodListIMP *)class_getClassMethod(cls, @selector(allocWithZone:));
225 m[2] = (IMP)HackAllocWithZone;
226
227 _objc_flush_caches(cls);
228
229 imp = class_getMethodImplementation(cls, @selector(retain));
230 testassert(imp == (IMP)HackRetain); // verify hack worked
231 }
232
233 Class cls = [NSObject class];
234 Class icl = [InheritingSub class];
235 Class ocl = [OverridingSub class];
236 /*
237 Class oa1 = [OverridingASub class];
238 Class oa2 = [OverridingAWZSub class];
239 Class oa3 = [OverridingAAWZSub class];
240 */
241 NSObject *obj = [NSObject new];
242 InheritingSub *inh = [InheritingSub new];
243 OverridingSub *ovr = [OverridingSub new];
244
245 Class ccc;
246 id ooo;
247 Class cc2;
248 id oo2;
249
250 void *dlh;
251
252
253 #if __x86_64__
254 // vtable dispatch can introduce bypass just like the ARC entrypoints
255 #else
256 testprintf("method dispatch does not bypass\n");
257 zero();
258
259 [obj retain];
260 testassert(Retains == 1);
261 [obj release];
262 testassert(Releases == 1);
263 [obj autorelease];
264 testassert(Autoreleases == 1);
265
266 [cls retain];
267 testassert(PlusRetains == 1);
268 [cls release];
269 testassert(PlusReleases == 1);
270 [cls autorelease];
271 testassert(PlusAutoreleases == 1);
272
273 [inh retain];
274 testassert(Retains == 2);
275 [inh release];
276 testassert(Releases == 2);
277 [inh autorelease];
278 testassert(Autoreleases == 2);
279
280 [icl retain];
281 testassert(PlusRetains == 2);
282 [icl release];
283 testassert(PlusReleases == 2);
284 [icl autorelease];
285 testassert(PlusAutoreleases == 2);
286
287 [ovr retain];
288 testassert(SubRetains == 1);
289 [ovr release];
290 testassert(SubReleases == 1);
291 [ovr autorelease];
292 testassert(SubAutoreleases == 1);
293
294 [ocl retain];
295 testassert(SubPlusRetains == 1);
296 [ocl release];
297 testassert(SubPlusReleases == 1);
298 [ocl autorelease];
299 testassert(SubPlusAutoreleases == 1);
300
301 [UnrealizedSubA1 retain];
302 testassert(PlusRetains == 3);
303 [UnrealizedSubA2 release];
304 testassert(PlusReleases == 3);
305 [UnrealizedSubA3 autorelease];
306 testassert(PlusAutoreleases == 3);
307 #endif
308
309
310 testprintf("objc_msgSend() does not bypass\n");
311 zero();
312
313 id (*retain_fn)(id, SEL) = (id(*)(id, SEL))objc_msgSend;
314 void (*release_fn)(id, SEL) = (void(*)(id, SEL))objc_msgSend;
315 id (*autorelease_fn)(id, SEL) = (id(*)(id, SEL))objc_msgSend;
316
317 retain_fn(obj, @selector(retain));
318 testassert(Retains == 1);
319 release_fn(obj, @selector(release));
320 testassert(Releases == 1);
321 autorelease_fn(obj, @selector(autorelease));
322 testassert(Autoreleases == 1);
323
324 retain_fn(cls, @selector(retain));
325 testassert(PlusRetains == 1);
326 release_fn(cls, @selector(release));
327 testassert(PlusReleases == 1);
328 autorelease_fn(cls, @selector(autorelease));
329 testassert(PlusAutoreleases == 1);
330
331 retain_fn(inh, @selector(retain));
332 testassert(Retains == 2);
333 release_fn(inh, @selector(release));
334 testassert(Releases == 2);
335 autorelease_fn(inh, @selector(autorelease));
336 testassert(Autoreleases == 2);
337
338 retain_fn(icl, @selector(retain));
339 testassert(PlusRetains == 2);
340 release_fn(icl, @selector(release));
341 testassert(PlusReleases == 2);
342 autorelease_fn(icl, @selector(autorelease));
343 testassert(PlusAutoreleases == 2);
344
345 retain_fn(ovr, @selector(retain));
346 testassert(SubRetains == 1);
347 release_fn(ovr, @selector(release));
348 testassert(SubReleases == 1);
349 autorelease_fn(ovr, @selector(autorelease));
350 testassert(SubAutoreleases == 1);
351
352 retain_fn(ocl, @selector(retain));
353 testassert(SubPlusRetains == 1);
354 release_fn(ocl, @selector(release));
355 testassert(SubPlusReleases == 1);
356 autorelease_fn(ocl, @selector(autorelease));
357 testassert(SubPlusAutoreleases == 1);
358
359 retain_fn((Class)&OBJC_CLASS_$_UnrealizedSubB1, @selector(retain));
360 testassert(PlusRetains == 3);
361 release_fn((Class)&OBJC_CLASS_$_UnrealizedSubB2, @selector(release));
362 testassert(PlusReleases == 3);
363 autorelease_fn((Class)&OBJC_CLASS_$_UnrealizedSubB3, @selector(autorelease));
364 testassert(PlusAutoreleases == 3);
365
366
367 testprintf("arc function bypasses instance but not class or override\n");
368 zero();
369
370 objc_retain(obj);
371 testassert(Retains == 0);
372 objc_release(obj);
373 testassert(Releases == 0);
374 objc_autorelease(obj);
375 testassert(Autoreleases == 0);
376
377 #if SUPPORT_NONPOINTER_ISA
378 objc_retain(cls);
379 testassert(PlusRetains == 0);
380 objc_release(cls);
381 testassert(PlusReleases == 0);
382 objc_autorelease(cls);
383 testassert(PlusAutoreleases == 0);
384 #else
385 objc_retain(cls);
386 testassert(PlusRetains == 1);
387 objc_release(cls);
388 testassert(PlusReleases == 1);
389 objc_autorelease(cls);
390 testassert(PlusAutoreleases == 1);
391 #endif
392
393 objc_retain(inh);
394 testassert(Retains == 0);
395 objc_release(inh);
396 testassert(Releases == 0);
397 objc_autorelease(inh);
398 testassert(Autoreleases == 0);
399
400 #if SUPPORT_NONPOINTER_ISA
401 objc_retain(icl);
402 testassert(PlusRetains == 0);
403 objc_release(icl);
404 testassert(PlusReleases == 0);
405 objc_autorelease(icl);
406 testassert(PlusAutoreleases == 0);
407 #else
408 objc_retain(icl);
409 testassert(PlusRetains == 2);
410 objc_release(icl);
411 testassert(PlusReleases == 2);
412 objc_autorelease(icl);
413 testassert(PlusAutoreleases == 2);
414 #endif
415
416 objc_retain(ovr);
417 testassert(SubRetains == 1);
418 objc_release(ovr);
419 testassert(SubReleases == 1);
420 objc_autorelease(ovr);
421 testassert(SubAutoreleases == 1);
422
423 objc_retain(ocl);
424 testassert(SubPlusRetains == 1);
425 objc_release(ocl);
426 testassert(SubPlusReleases == 1);
427 objc_autorelease(ocl);
428 testassert(SubPlusAutoreleases == 1);
429
430 #if SUPPORT_NONPOINTER_ISA
431 objc_retain((Class)&OBJC_CLASS_$_UnrealizedSubC1);
432 testassert(PlusRetains == 1);
433 objc_release((Class)&OBJC_CLASS_$_UnrealizedSubC2);
434 testassert(PlusReleases == 1);
435 objc_autorelease((Class)&OBJC_CLASS_$_UnrealizedSubC3);
436 testassert(PlusAutoreleases == 1);
437 #else
438 objc_retain((Class)&OBJC_CLASS_$_UnrealizedSubC1);
439 testassert(PlusRetains == 3);
440 objc_release((Class)&OBJC_CLASS_$_UnrealizedSubC2);
441 testassert(PlusReleases == 3);
442 objc_autorelease((Class)&OBJC_CLASS_$_UnrealizedSubC3);
443 testassert(PlusAutoreleases == 3);
444 #endif
445
446 testprintf("unrelated addMethod does not clobber\n");
447 zero();
448
449 class_addMethod(cls, @selector(unrelatedMethod), (IMP)imp_fn, "");
450
451 objc_retain(obj);
452 testassert(Retains == 0);
453 objc_release(obj);
454 testassert(Releases == 0);
455 objc_autorelease(obj);
456 testassert(Autoreleases == 0);
457
458
459 testprintf("add class method does not clobber\n");
460 zero();
461
462 objc_retain(obj);
463 testassert(Retains == 0);
464 objc_release(obj);
465 testassert(Releases == 0);
466 objc_autorelease(obj);
467 testassert(Autoreleases == 0);
468
469 class_addMethod(object_getClass(cls), @selector(retain), (IMP)imp_fn, "");
470
471 objc_retain(obj);
472 testassert(Retains == 0);
473 objc_release(obj);
474 testassert(Releases == 0);
475 objc_autorelease(obj);
476 testassert(Autoreleases == 0);
477
478
479 testprintf("addMethod clobbers (InheritingSub2, retain)\n");
480 zero();
481
482 ccc = [InheritingSub2 class];
483 ooo = [ccc new];
484 cc2 = [InheritingSub2_2 class];
485 oo2 = [cc2 new];
486
487 objc_retain(ooo);
488 testassert(Retains == 0);
489 objc_release(ooo);
490 testassert(Releases == 0);
491 objc_autorelease(ooo);
492 testassert(Autoreleases == 0);
493
494 objc_retain(oo2);
495 testassert(Retains == 0);
496 objc_release(oo2);
497 testassert(Releases == 0);
498 objc_autorelease(oo2);
499 testassert(Autoreleases == 0);
500
501 class_addMethod(ccc, @selector(retain), (IMP)imp_fn, "");
502
503 objc_retain(ooo);
504 testassert(Retains == 0);
505 testassert(Imps == 1);
506 objc_release(ooo);
507 testassert(Releases == 1);
508 objc_autorelease(ooo);
509 testassert(Autoreleases == 1);
510
511 objc_retain(oo2);
512 testassert(Retains == 0);
513 testassert(Imps == 2);
514 objc_release(oo2);
515 testassert(Releases == 2);
516 objc_autorelease(oo2);
517 testassert(Autoreleases == 2);
518
519
520 testprintf("addMethod clobbers (InheritingSub3, release)\n");
521 zero();
522
523 ccc = [InheritingSub3 class];
524 ooo = [ccc new];
525 cc2 = [InheritingSub3_2 class];
526 oo2 = [cc2 new];
527
528 objc_retain(ooo);
529 testassert(Retains == 0);
530 objc_release(ooo);
531 testassert(Releases == 0);
532 objc_autorelease(ooo);
533 testassert(Autoreleases == 0);
534
535 objc_retain(oo2);
536 testassert(Retains == 0);
537 objc_release(oo2);
538 testassert(Releases == 0);
539 objc_autorelease(oo2);
540 testassert(Autoreleases == 0);
541
542 class_addMethod(ccc, @selector(release), (IMP)imp_fn, "");
543
544 objc_retain(ooo);
545 testassert(Retains == 1);
546 objc_release(ooo);
547 testassert(Releases == 0);
548 testassert(Imps == 1);
549 objc_autorelease(ooo);
550 testassert(Autoreleases == 1);
551
552 objc_retain(oo2);
553 testassert(Retains == 2);
554 objc_release(oo2);
555 testassert(Releases == 0);
556 testassert(Imps == 2);
557 objc_autorelease(oo2);
558 testassert(Autoreleases == 2);
559
560
561 testprintf("addMethod clobbers (InheritingSub4, autorelease)\n");
562 zero();
563
564 ccc = [InheritingSub4 class];
565 ooo = [ccc new];
566 cc2 = [InheritingSub4_2 class];
567 oo2 = [cc2 new];
568
569 objc_retain(ooo);
570 testassert(Retains == 0);
571 objc_release(ooo);
572 testassert(Releases == 0);
573 objc_autorelease(ooo);
574 testassert(Autoreleases == 0);
575
576 objc_retain(oo2);
577 testassert(Retains == 0);
578 objc_release(oo2);
579 testassert(Releases == 0);
580 objc_autorelease(oo2);
581 testassert(Autoreleases == 0);
582
583 class_addMethod(ccc, @selector(autorelease), (IMP)imp_fn, "");
584
585 objc_retain(ooo);
586 testassert(Retains == 1);
587 objc_release(ooo);
588 testassert(Releases == 1);
589 objc_autorelease(ooo);
590 testassert(Autoreleases == 0);
591 testassert(Imps == 1);
592
593 objc_retain(oo2);
594 testassert(Retains == 2);
595 objc_release(oo2);
596 testassert(Releases == 2);
597 objc_autorelease(oo2);
598 testassert(Autoreleases == 0);
599 testassert(Imps == 2);
600
601
602 testprintf("addMethod clobbers (InheritingSub5, retainCount)\n");
603 zero();
604
605 ccc = [InheritingSub5 class];
606 ooo = [ccc new];
607 cc2 = [InheritingSub5_2 class];
608 oo2 = [cc2 new];
609
610 objc_retain(ooo);
611 testassert(Retains == 0);
612 objc_release(ooo);
613 testassert(Releases == 0);
614 objc_autorelease(ooo);
615 testassert(Autoreleases == 0);
616
617 objc_retain(oo2);
618 testassert(Retains == 0);
619 objc_release(oo2);
620 testassert(Releases == 0);
621 objc_autorelease(oo2);
622 testassert(Autoreleases == 0);
623
624 class_addMethod(ccc, @selector(retainCount), (IMP)imp_fn, "");
625
626 objc_retain(ooo);
627 testassert(Retains == 1);
628 objc_release(ooo);
629 testassert(Releases == 1);
630 objc_autorelease(ooo);
631 testassert(Autoreleases == 1);
632 // no bypassing call for -retainCount
633
634 objc_retain(oo2);
635 testassert(Retains == 2);
636 objc_release(oo2);
637 testassert(Releases == 2);
638 objc_autorelease(oo2);
639 testassert(Autoreleases == 2);
640 // no bypassing call for -retainCount
641
642
643 testprintf("setSuperclass to clean super does not clobber (InheritingSub6)\n");
644 zero();
645
646 ccc = [InheritingSub6 class];
647 ooo = [ccc new];
648 cc2 = [InheritingSub6_2 class];
649 oo2 = [cc2 new];
650
651 objc_retain(ooo);
652 testassert(Retains == 0);
653 objc_release(ooo);
654 testassert(Releases == 0);
655 objc_autorelease(ooo);
656 testassert(Autoreleases == 0);
657
658 objc_retain(oo2);
659 testassert(Retains == 0);
660 objc_release(oo2);
661 testassert(Releases == 0);
662 objc_autorelease(oo2);
663 testassert(Autoreleases == 0);
664
665 class_setSuperclass(ccc, [InheritingSub class]);
666
667 objc_retain(ooo);
668 testassert(Retains == 0);
669 objc_release(ooo);
670 testassert(Releases == 0);
671 objc_autorelease(ooo);
672 testassert(Autoreleases == 0);
673
674 objc_retain(oo2);
675 testassert(Retains == 0);
676 objc_release(oo2);
677 testassert(Releases == 0);
678 objc_autorelease(oo2);
679 testassert(Autoreleases == 0);
680
681
682 testprintf("setSuperclass to dirty super clobbers (InheritingSub7)\n");
683 zero();
684
685 ccc = [InheritingSub7 class];
686 ooo = [ccc new];
687 cc2 = [InheritingSub7_2 class];
688 oo2 = [cc2 new];
689
690 objc_retain(ooo);
691 testassert(Retains == 0);
692 objc_release(ooo);
693 testassert(Releases == 0);
694 objc_autorelease(ooo);
695 testassert(Autoreleases == 0);
696
697 objc_retain(oo2);
698 testassert(Retains == 0);
699 objc_release(oo2);
700 testassert(Releases == 0);
701 objc_autorelease(oo2);
702 testassert(Autoreleases == 0);
703
704 class_setSuperclass(ccc, [OverridingSub class]);
705
706 objc_retain(ooo);
707 testassert(SubRetains == 1);
708 objc_release(ooo);
709 testassert(SubReleases == 1);
710 objc_autorelease(ooo);
711 testassert(SubAutoreleases == 1);
712
713 objc_retain(oo2);
714 testassert(SubRetains == 2);
715 objc_release(oo2);
716 testassert(SubReleases == 2);
717 objc_autorelease(oo2);
718 testassert(SubAutoreleases == 2);
719
720
721 testprintf("category replacement of unrelated method does not clobber (InheritingSubCat)\n");
722 zero();
723
724 ccc = [InheritingSubCat class];
725 ooo = [ccc new];
726 cc2 = [InheritingSubCat_2 class];
727 oo2 = [cc2 new];
728
729 objc_retain(ooo);
730 testassert(Retains == 0);
731 objc_release(ooo);
732 testassert(Releases == 0);
733 objc_autorelease(ooo);
734 testassert(Autoreleases == 0);
735
736 objc_retain(oo2);
737 testassert(Retains == 0);
738 objc_release(oo2);
739 testassert(Releases == 0);
740 objc_autorelease(oo2);
741 testassert(Autoreleases == 0);
742
743 dlh = dlopen("customrr-cat1.bundle", RTLD_LAZY);
744 testassert(dlh);
745
746 objc_retain(ooo);
747 testassert(Retains == 0);
748 objc_release(ooo);
749 testassert(Releases == 0);
750 objc_autorelease(ooo);
751 testassert(Autoreleases == 0);
752
753 objc_retain(oo2);
754 testassert(Retains == 0);
755 objc_release(oo2);
756 testassert(Releases == 0);
757 objc_autorelease(oo2);
758 testassert(Autoreleases == 0);
759
760
761 testprintf("category replacement clobbers (InheritingSubCat)\n");
762 zero();
763
764 ccc = [InheritingSubCat class];
765 ooo = [ccc new];
766 cc2 = [InheritingSubCat_2 class];
767 oo2 = [cc2 new];
768
769 objc_retain(ooo);
770 testassert(Retains == 0);
771 objc_release(ooo);
772 testassert(Releases == 0);
773 objc_autorelease(ooo);
774 testassert(Autoreleases == 0);
775
776 objc_retain(oo2);
777 testassert(Retains == 0);
778 objc_release(oo2);
779 testassert(Releases == 0);
780 objc_autorelease(oo2);
781 testassert(Autoreleases == 0);
782
783 dlh = dlopen("customrr-cat2.bundle", RTLD_LAZY);
784 testassert(dlh);
785
786 objc_retain(ooo);
787 testassert(Retains == 1);
788 objc_release(ooo);
789 testassert(Releases == 1);
790 objc_autorelease(ooo);
791 testassert(Autoreleases == 1);
792
793 objc_retain(oo2);
794 testassert(Retains == 2);
795 objc_release(oo2);
796 testassert(Releases == 2);
797 objc_autorelease(oo2);
798 testassert(Autoreleases == 2);
799
800
801 testprintf("allocateClassPair with clean super does not clobber\n");
802 zero();
803
804 objc_retain(inh);
805 testassert(Retains == 0);
806 objc_release(inh);
807 testassert(Releases == 0);
808 objc_autorelease(inh);
809 testassert(Autoreleases == 0);
810
811 ccc = objc_allocateClassPair([InheritingSub class], "CleanClassPair", 0);
812 objc_registerClassPair(ccc);
813 ooo = [ccc new];
814
815 objc_retain(inh);
816 testassert(Retains == 0);
817 objc_release(inh);
818 testassert(Releases == 0);
819 objc_autorelease(inh);
820 testassert(Autoreleases == 0);
821
822 objc_retain(ooo);
823 testassert(Retains == 0);
824 objc_release(ooo);
825 testassert(Releases == 0);
826 objc_autorelease(ooo);
827 testassert(Autoreleases == 0);
828
829
830 testprintf("allocateClassPair with clobbered super clobbers\n");
831 zero();
832
833 ccc = objc_allocateClassPair([OverridingSub class], "DirtyClassPair", 0);
834 objc_registerClassPair(ccc);
835 ooo = [ccc new];
836
837 objc_retain(ooo);
838 testassert(SubRetains == 1);
839 objc_release(ooo);
840 testassert(SubReleases == 1);
841 objc_autorelease(ooo);
842 testassert(SubAutoreleases == 1);
843
844
845 testprintf("allocateClassPair with clean super and override clobbers\n");
846 zero();
847
848 ccc = objc_allocateClassPair([InheritingSub class], "Dirty2ClassPair", 0);
849 class_addMethod(ccc, @selector(autorelease), (IMP)imp_fn, "");
850 objc_registerClassPair(ccc);
851 ooo = [ccc new];
852
853 objc_retain(ooo);
854 testassert(Retains == 1);
855 objc_release(ooo);
856 testassert(Releases == 1);
857 objc_autorelease(ooo);
858 testassert(Autoreleases == 0);
859 testassert(Imps == 1);
860
861
862 // method_setImplementation and method_exchangeImplementations only
863 // clobber when manipulating NSObject. We can only test one at a time.
864 // To test both, we need two tests: customrr and customrr2.
865
866 // These tests also check recursive clobber.
867
868 #if TEST_EXCHANGEIMPLEMENTATIONS
869 testprintf("exchangeImplementations clobbers (recursive)\n");
870 #else
871 testprintf("setImplementation clobbers (recursive)\n");
872 #endif
873 zero();
874
875 objc_retain(obj);
876 testassert(Retains == 0);
877 objc_release(obj);
878 testassert(Releases == 0);
879 objc_autorelease(obj);
880 testassert(Autoreleases == 0);
881
882 objc_retain(inh);
883 testassert(Retains == 0);
884 objc_release(inh);
885 testassert(Releases == 0);
886 objc_autorelease(inh);
887 testassert(Autoreleases == 0);
888
889 Method meth = class_getInstanceMethod(cls, @selector(retainCount));
890 testassert(meth);
891 #if TEST_EXCHANGEIMPLEMENTATIONS
892 method_exchangeImplementations(meth, meth);
893 #else
894 method_setImplementation(meth, (IMP)imp_fn);
895 #endif
896
897 objc_retain(obj);
898 testassert(Retains == 1);
899 objc_release(obj);
900 testassert(Releases == 1);
901 objc_autorelease(obj);
902 testassert(Autoreleases == 1);
903
904 objc_retain(inh);
905 testassert(Retains == 2);
906 objc_release(inh);
907 testassert(Releases == 2);
908 objc_autorelease(inh);
909 testassert(Autoreleases == 2);
910
911
912 // do not add more tests here - the recursive test must be LAST
913
914 succeed(basename(argv[0]));
915 }