]> git.saurik.com Git - apple/objc4.git/blob - test/swiftMetadataInitializerRealloc.m
objc4-781.2.tar.gz
[apple/objc4.git] / test / swiftMetadataInitializerRealloc.m
1 /*
2 TEST_CONFIG MEM=mrc
3 TEST_BUILD
4 $C{COMPILE} $DIR/swiftMetadataInitializerRealloc-dylib1.m -o libswiftMetadataInitializerRealloc-dylib1.dylib -dynamiclib -Wno-deprecated-objc-pointer-introspection
5 $C{COMPILE} $DIR/swiftMetadataInitializerRealloc-dylib2.m -o libswiftMetadataInitializerRealloc-dylib2.dylib -dynamiclib -L. -lswiftMetadataInitializerRealloc-dylib1
6 $C{COMPILE} $DIR/swiftMetadataInitializerRealloc.m -o swiftMetadataInitializerRealloc.exe -L. -lswiftMetadataInitializerRealloc-dylib1 -Wno-deprecated-objc-pointer-introspection
7 END
8 */
9
10 #include "test.h"
11 #include "swift-class-def.m"
12
13
14 // _objc_swiftMetadataInitializer hooks for the classes in swift-class-def.m
15
16 Class initSuper(Class cls __unused, void *arg __unused)
17 {
18 // This test provokes objc's callback out of superclass order.
19 // SwiftSub's init is first. SwiftSuper's init is never called.
20
21 fail("SwiftSuper's init should not have been called");
22 }
23
24 bool isRealized(Class cls)
25 {
26 // check the is-realized bits directly
27
28 #if __LP64__
29 # define mask (~(uintptr_t)7)
30 #else
31 # define mask (~(uintptr_t)3)
32 #endif
33 #define RW_REALIZED (1<<31)
34
35 uintptr_t rw = ((uintptr_t *)cls)[4] & mask; // class_t->data
36 return ((uint32_t *)rw)[0] & RW_REALIZED; // class_rw_t->flags
37 }
38
39 SWIFT_CLASS(SwiftSuper, NSObject, initSuper);
40 SWIFT_CLASS(RealSwiftSub, SwiftSuper, initSub);
41
42 SWIFT_STUB_CLASS(SwiftSub, initSub);
43
44 OBJC_EXPORT _Nullable Class
45 objc_loadClassref(_Nullable Class * _Nonnull clsref);
46
47 static int SubInits = 0;
48 Class initSub(Class cls, void *arg)
49 {
50 testprintf("initSub callback\n");
51
52 testassert(SubInits == 0);
53 SubInits++;
54 testassert(arg == nil);
55 testassert(cls == RawSwiftSub);
56 testassert(!isRealized(RawSwiftSuper));
57
58 // Copy the class to the heap to ensure they're registered properly.
59 // Classes in the data segment are automatically "known" even if not
60 // added as a known class. Swift dynamically allocates classes from
61 // a statically allocated space in the dylib, then allocates from
62 // the heap after it runs out of room there. Code that only works
63 // when the class is in a dylib can fail a long time down the road
64 // when something finally exceeds the capacity of that space.
65 // Example: rdar://problem/50707074
66 Class HeapSwiftSub = (Class)malloc(OBJC_MAX_CLASS_SIZE);
67 memcpy(HeapSwiftSub, RawRealSwiftSub, OBJC_MAX_CLASS_SIZE);
68
69 testprintf("initSub beginning _objc_realizeClassFromSwift\n");
70 _objc_realizeClassFromSwift(HeapSwiftSub, cls);
71 testprintf("initSub finished _objc_realizeClassFromSwift\n");
72
73 testassert(isRealized(RawSwiftSuper));
74 testassert(isRealized(HeapSwiftSub));
75
76 testprintf("Returning reallocated class %p\n", HeapSwiftSub);
77
78 return HeapSwiftSub;
79 }
80
81
82 @interface SwiftSub (Addition)
83 - (int)number;
84 @end
85 @implementation SwiftSub (Addition)
86 - (int)number { return 42; }
87 @end
88
89 @interface NSObject (DylibCategories)
90 - (const char *)dylib1ACategoryInSameDylib;
91 - (const char *)dylib1BCategoryInSameDylib;
92 - (const char *)dylib1ACategoryInOtherDylib;
93 - (const char *)dylib1BCategoryInOtherDylib;
94 - (const char *)dylib1ACategoryInApp;
95 - (const char *)dylib1BCategoryInApp;
96 + (const char *)dylib1ACategoryInAppClassMethod;
97 + (const char *)dylib1BCategoryInAppClassMethod;
98 + (void)testFromOtherDylib;
99 @end
100
101 extern int Dylib1AInits;
102 extern int Dylib1BInits;
103 SWIFT_STUB_CLASSREF(SwiftDylib1A);
104 SWIFT_STUB_CLASSREF(SwiftDylib1B);
105 void Dylib1Test(void);
106
107 @interface SwiftDylib1A: NSObject @end
108 @interface SwiftDylib1B: NSObject @end
109
110 @implementation SwiftDylib1A (Category)
111 - (const char *)dylib1ACategoryInApp { return "dylib1ACategoryInApp"; }
112 + (const char *)dylib1ACategoryInAppClassMethod { return "dylib1ACategoryInAppClassMethod"; }
113 @end
114 @implementation SwiftDylib1B (Category)
115 - (const char *)dylib1BCategoryInApp { return "dylib1BCategoryInApp"; }
116 + (const char *)dylib1BCategoryInAppClassMethod { return "dylib1BCategoryInAppClassMethod"; }
117 @end
118
119
120 int main()
121 {
122 #define LOG(fmt, expr) testprintf(#expr " is " #fmt "\n", expr);
123 LOG(%p, SwiftSubClassref);
124 Class loadedSwiftSub = objc_loadClassref(&SwiftSubClassref);
125 LOG(%p, SwiftSubClassref);
126 LOG(%p, loadedSwiftSub);
127 LOG(%p, [loadedSwiftSub class]);
128 LOG(%p, [loadedSwiftSub superclass]);
129 LOG(%p, [RawSwiftSuper class]);
130
131 id obj = [[loadedSwiftSub alloc] init];
132 LOG(%p, obj);
133 LOG(%d, [obj number]);
134
135 LOG(%p, SwiftDylib1AClassref);
136 testassert(Dylib1AInits == 0);
137 testassert((uintptr_t)SwiftDylib1AClassref & 1);
138 Class SwiftDylib1A = objc_loadClassref(&SwiftDylib1AClassref);
139 testassert(((uintptr_t)SwiftDylib1AClassref & 1) == 0);
140 testassert(SwiftDylib1A == [SwiftDylib1A class]);
141 testassert(SwiftDylib1A == SwiftDylib1AClassref);
142 testassert(Dylib1AInits == 1);
143 LOG(%p, SwiftDylib1A);
144
145 LOG(%p, SwiftDylib1BClassref);
146 testassert(Dylib1BInits == 0);
147 testassert((uintptr_t)SwiftDylib1BClassref & 1);
148 Class SwiftDylib1B = objc_loadClassref(&SwiftDylib1BClassref);
149 testassert(((uintptr_t)SwiftDylib1BClassref & 1) == 0);
150 testassert(SwiftDylib1B == [SwiftDylib1B class]);
151 testassert(SwiftDylib1B == SwiftDylib1BClassref);
152 testassert(Dylib1BInits == 1);
153 LOG(%p, SwiftDylib1B);
154
155 Dylib1Test();
156
157 testassert(strcmp([[SwiftDylib1A new] dylib1ACategoryInSameDylib], "dylib1ACategoryInSameDylib") == 0);
158 testassert(strcmp([[SwiftDylib1B new] dylib1BCategoryInSameDylib], "dylib1BCategoryInSameDylib") == 0);
159 testassert(strcmp([[SwiftDylib1A new] dylib1ACategoryInApp], "dylib1ACategoryInApp") == 0);
160 testassert(strcmp([[SwiftDylib1B new] dylib1BCategoryInApp], "dylib1BCategoryInApp") == 0);
161
162 void *handle = dlopen("libswiftMetadataInitializerRealloc-dylib2.dylib", RTLD_LAZY);
163 testassert(handle);
164
165 testassert(strcmp([[SwiftDylib1A new] dylib1ACategoryInOtherDylib], "dylib1ACategoryInOtherDylib") == 0);
166 testassert(strcmp([[SwiftDylib1B new] dylib1BCategoryInOtherDylib], "dylib1BCategoryInOtherDylib") == 0);
167 testassert(strcmp([SwiftDylib1A dylib1ACategoryInAppClassMethod], "dylib1ACategoryInAppClassMethod") == 0);
168 testassert(strcmp([SwiftDylib1B dylib1BCategoryInAppClassMethod], "dylib1BCategoryInAppClassMethod") == 0);
169 [SwiftDylib1A testFromOtherDylib];
170
171 testassert(objc_getClass("RealSwiftSub"));
172 testassert(objc_getClass("RealSwiftDylib1A"));
173 testassert(objc_getClass("RealSwiftDylib1B"));
174
175 succeed(__FILE__);
176 }