2 // BUILD: $CC missing.m -dynamiclib -o $TEMP_DIR/libmissing.dylib -install_name $BUILD_DIR/libmissing.dylib -lobjc
3 // BUILD: $CC lib1.m -dynamiclib -o $BUILD_DIR/liblinked1.dylib -install_name $RUN_DIR/liblinked1.dylib -lobjc
4 // BUILD: $CC lib2.m -dynamiclib -o $BUILD_DIR/liblinked2.dylib -install_name $RUN_DIR/liblinked2.dylib -lobjc $TEMP_DIR/libmissing.dylib
5 // BUILD: $CC main.mm -o $BUILD_DIR/_dyld_for_each_objc_class-missing-weak.exe -lobjc $BUILD_DIR/liblinked1.dylib $BUILD_DIR/liblinked2.dylib $TEMP_DIR/libmissing.dylib -lc++
7 // RUN: ./_dyld_for_each_objc_class-missing-weak.exe
9 // liblinked2 weakly links libmissing and so has a missing weak superclass.
10 // This means we should not see classes from liblinked be returned from _dyld_for_each_objc_class
11 // liblinked1 itself has classes which are fine so shoud be in the map.
12 // At runtime, objc is going to walk the images in reverse load order so will see the classes in liblinked2 first
13 // which are the ones we couldn't optimize. But objc should then check the closure class map and choose the class
16 #include <mach-o/dyld_priv.h>
18 #import <Foundation/Foundation.h>
20 // All the libraries have a copy of DyldClass
21 @interface DyldClass : NSObject
24 @implementation DyldClass
27 // Only the main executable has DyldMainClass
28 @interface DyldMainClass : NSObject
31 @implementation DyldMainClass
34 extern Class OBJC_CLASS_$_DyldClass;
35 extern Class OBJC_CLASS_$_DyldMainClass;
37 Class getMainDyldClass() {
38 return (Class)&OBJC_CLASS_$_DyldClass;
41 Class getMainDyldMainClass() {
42 return (Class)&OBJC_CLASS_$_DyldMainClass;
45 extern "C" int printf(const char*, ...);
47 extern "C" id objc_getClass(const char *name);
49 // Get the DyldClass from liblinked1.dylib
50 extern "C" id getLinked1DyldClass();
52 // Get the DyldLinkedClass from liblinked1.dylib
53 extern "C" id getLinked1DyldLinkedClass();
55 // Get the DyldLinkedClass from liblinked.dylib
56 extern "C" id getLinked2DyldLinkedClass();
58 // Get the DyldClass from libmissing.dylib
59 // Note, this is weak_import and missing so this must fail
60 __attribute__((weak_import))
61 extern "C" id getMissingDyldClass();
63 static bool gotDyldClassMain = false;
64 static bool gotDyldClassLinked1 = false;
67 printf("[BEGIN] _dyld_for_each_objc_class-missing-weak\n");
69 // This API is only available with dyld3 and shared caches. If we have dyld2 then don't do anything
70 const char* testDyldMode = getenv("TEST_DYLD_MODE");
73 size_t unusedCacheLen;
74 bool haveSharedCache = _dyld_get_shared_cache_range(&unusedCacheLen) != 0;
75 if (!strcmp(testDyldMode, "2") || !haveSharedCache) {
76 __block bool sawClass = false;
77 _dyld_for_each_objc_class("DyldClass", ^(void* classPtr, bool isLoaded, bool* stop) {
81 printf("[FAIL] _dyld_for_each_objc_class: dyld2 shouldn't see any classes\n");
84 printf("[PASS] _dyld_for_each_objc_class (dyld2 or no shared cache)\n");
88 // Make sure libmissing.dylib is actually missing
89 if (&getMissingDyldClass != nil) {
90 printf("[FAIL] _dyld_for_each_objc_class-missing-weak: libmissing needs to be missing\n");
94 // DyldClass in liblinked1 should exist as its superclass is just NSObject
95 if (getLinked1DyldClass() == nil) {
96 printf("[FAIL] _dyld_for_each_objc_class-missing-weak: liblinked1 DyldClass should exist\n");
100 // DyldLinkedClass in liblinked1 should exist as its superclass is just NSObject
101 if (getLinked1DyldLinkedClass() == nil) {
102 printf("[FAIL] _dyld_for_each_objc_class-missing-weak: liblinked1 DyldLinkedClass should exist\n");
106 // DyldLinkedClass in liblinked2 should exist as its superclass is just NSObject
107 if (getLinked2DyldLinkedClass() == nil) {
108 printf("[FAIL] _dyld_for_each_objc_class-missing-weak: liblinked2 DyldLinkedClass should exist\n");
112 // Check that DyldMainClass comes main.exe as that is its only definition
113 id runtimeDyldMainClass = objc_getClass("DyldMainClass");
114 if (runtimeDyldMainClass != getMainDyldMainClass()) {
115 printf("[FAIL] _dyld_for_each_objc_class-missing-weak: DyldMainClass should have come from main.exe\n");
119 // Check that DyldClass comes liblinked1 as it should be missing from liblinked2
120 id runtimeDyldClass = objc_getClass("DyldClass");
121 if (runtimeDyldClass != getLinked1DyldClass()) {
122 printf("[FAIL] _dyld_for_each_objc_class-missing-weak: DyldClass should have come from liblinked1\n");
126 // Check that DyldLinkedClass comes from liblinked2
127 // Note, this changes once the objc runtime has adopted our changes. Don't test it for now
129 id runtimeDyldLinkedClass = objc_getClass("DyldLinkedClass");
130 if (runtimeDyldLinkedClass != getLinked2DyldLinkedClass()) {
131 printf("[FAIL] _dyld_for_each_objc_class-missing-weak: DyldLinkedClass should have come from liblinked2\n");
136 _dyld_for_each_objc_class("DyldClass", ^(void* classPtr, bool isLoaded, bool* stop) {
137 // We should walk these in the order liblinked, main exe
138 if (!gotDyldClassLinked1) {
139 if (classPtr != getLinked1DyldClass()) {
140 printf("[FAIL] _dyld_for_each_objc_class-missing-weak: DyldClass should have come from liblinked1\n");
145 printf("[FAIL] _dyld_for_each_objc_class-missing-weak: DyldClass isLoaded should have been set on liblinked1\n");
149 gotDyldClassLinked1 = true;
152 if (!gotDyldClassMain) {
153 if (classPtr != getMainDyldClass()) {
154 printf("[FAIL] _dyld_for_each_objc_class-missing-weak: DyldClass should have come from main exe\n");
159 printf("[FAIL] _dyld_for_each_objc_class-missing-weak: DyldClass isLoaded should have been set on main exe\n");
163 gotDyldClassMain = true;
166 printf("[FAIL] _dyld_for_each_objc_class-missing-weak: Unexpected DyldClass\n");
170 if (!gotDyldClassLinked1) {
171 printf("[FAIL] _dyld_for_each_objc_class-missing-weak: _dyld_for_each_objc_class should have seen DyldClass in liblinked1\n");
175 if (!gotDyldClassMain) {
176 printf("[FAIL] _dyld_for_each_objc_class-missing-weak: _dyld_for_each_objc_class should have seen DyldClass in main.exe\n");
180 // Visit again, and return liblinked1's DyldClass
181 // Visit again, and return liblinked2's DyldClass
182 __block void* dyldClassImpl = nil;
183 _dyld_for_each_objc_class("DyldClass", ^(void* classPtr, bool isLoaded, bool* stop) {
184 dyldClassImpl = classPtr;
187 if (dyldClassImpl != getLinked1DyldClass()) {
188 printf("[FAIL] _dyld_for_each_objc_class-missing-weak: _dyld_for_each_objc_class should have returned DyldClass from liblinked1\n");
192 // Visit again, and return liblinked1's DyldClass
194 _dyld_for_each_objc_class("DyldClass", ^(void* classPtr, bool isLoaded, bool* stop) {
195 // We should walk these in the order liblinked, main exe
196 // And return the one from main.exe
197 if (classPtr == getLinked1DyldClass())
199 dyldClassImpl = classPtr;
202 if (dyldClassImpl != getMainDyldClass()) {
203 printf("[FAIL] _dyld_for_each_objc_class-missing-weak: _dyld_for_each_objc_class should have returned DyldClass from main.exe\n");
207 printf("[PASS] _dyld_for_each_objc_class-missing-weak\n");