2 // BUILD: $CC linked1.m -dynamiclib -o $BUILD_DIR/liblinked1.dylib -install_name $RUN_DIR/liblinked1.dylib -lobjc
3 // BUILD: $CC linked2.m -dynamiclib -o $BUILD_DIR/liblinked2.dylib -install_name $RUN_DIR/liblinked2.dylib -lobjc
4 // BUILD: $CC main.m -o $BUILD_DIR/_dyld_for_each_objc_protocol.exe $BUILD_DIR/liblinked1.dylib $BUILD_DIR/liblinked2.dylib -lobjc
6 // RUN: ./_dyld_for_each_objc_protocol.exe
8 // The preoptimized objc protocol information is available via _dyld_for_each_objc_protocol().
9 // This test ensures that we match the objc behaviour when there are duplicates.
10 // For objc today, it walks the images in reverse load order, so the deepest library will be
11 // the canonical definition of a protocol.
13 #include <mach-o/dyld_priv.h>
14 #include <objc/runtime.h>
20 #include "test_support.h"
22 // All the libraries have a copy of DyldProtocol
23 @protocol DyldProtocol
26 // Only the main executable has DyldMainProtocol
27 @protocol DyldMainProtocol
31 static void* useDyldProtocol() {
32 return (void*)@protocol(DyldProtocol);
36 static void* useDyldMainProtocol() {
37 return (void*)@protocol(DyldMainProtocol);
40 extern id objc_getProtocol(const char *name);
42 static bool gotDyldProtocolMain = false;
43 static bool gotDyldProtocolLinked = false;
44 static bool gotDyldProtocolLinked2 = false;
46 static bool isInImage(void* ptr, const char* name) {
48 if ( dladdr(ptr, &info) == 0 ) {
49 FAIL("dladdr(protocol, xx) failed");
52 return strstr(info.dli_fname, name) != NULL;
55 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
56 // This API is only available with dyld3 and shared caches. If we have dyld2 then don't do anything
57 const char* testDyldMode = getenv("TEST_DYLD_MODE");
60 size_t unusedCacheLen;
61 bool haveSharedCache = _dyld_get_shared_cache_range(&unusedCacheLen) != 0;
62 if (!strcmp(testDyldMode, "2") || !haveSharedCache) {
63 __block bool sawProtocol = false;
64 _dyld_for_each_objc_protocol("DyldProtocol", ^(void* protocolPtr, bool isLoaded, bool* stop) {
68 FAIL("dyld2 shouldn't see any protocols");
70 PASS("dyld2 or no shared cache");
73 // Check that DyldProtocol comes from liblinked2 as it is last in load order
74 id runtimeDyldProtocol = objc_getProtocol("DyldProtocol");
75 if (!isInImage(runtimeDyldProtocol, "liblinked2")) {
76 FAIL("DyldProtocol should have come from liblinked2");
79 // Check that DyldLinkedProtocol comes from liblinked2 as it is last in load order
80 id runtimeDyldLinkedProtocol = objc_getProtocol("DyldLinkedProtocol");
81 if (!isInImage(runtimeDyldLinkedProtocol, "liblinked2")) {
82 FAIL("DyldLinkedProtocol should have come from liblinked2");
85 // Walk all the implementations of "DyldProtocol"
86 _dyld_for_each_objc_protocol("DyldProtocol", ^(void* protocolPtr, bool isLoaded, bool* stop) {
87 // We should walk these in the order liblinked2, liblinked, main exe
88 if (!gotDyldProtocolLinked2) {
89 if (!isInImage(protocolPtr, "liblinked2")) {
90 FAIL("Optimized DyldProtocol should have come from liblinked2");
93 FAIL("Optimized DyldProtocol isLoaded should have been set on liblinked2");
95 gotDyldProtocolLinked2 = true;
98 if (!gotDyldProtocolLinked) {
99 if (!isInImage(protocolPtr, "liblinked1")) {
100 FAIL("Optimized DyldProtocol should have come from liblinked");
103 FAIL("Optimized DyldProtocol isLoaded should have been set on liblinked");
105 gotDyldProtocolLinked = true;
108 if (!gotDyldProtocolMain) {
109 if (!isInImage(protocolPtr, "_dyld_for_each_objc_protocol.exe")) {
110 FAIL("Optimized DyldProtocol should have come from main exe");
113 FAIL("Optimized DyldProtocol isLoaded should have been set on main exe");
115 gotDyldProtocolMain = true;
118 FAIL("Unexpected Optimized DyldProtocol");
122 // Visit again, and return liblinked2's DyldProtocol
123 __block void* DyldProtocolImpl = nil;
124 _dyld_for_each_objc_protocol("DyldProtocol", ^(void* protocolPtr, bool isLoaded, bool* stop) {
125 DyldProtocolImpl = protocolPtr;
128 if (!isInImage(DyldProtocolImpl, "liblinked2")) {
129 FAIL("_dyld_for_each_objc_protocol should have returned DyldProtocol from liblinked2");
132 // Visit DyldMainProtocol and make sure it makes the callback for just the result from main.exe
133 __block void* DyldMainProtocolImpl = nil;
134 _dyld_for_each_objc_protocol("DyldMainProtocol", ^(void* protocolPtr, bool isLoaded, bool* stop) {
135 DyldMainProtocolImpl = protocolPtr;
138 if (!isInImage(DyldMainProtocolImpl, "_dyld_for_each_objc_protocol.exe")) {
139 FAIL("_dyld_for_each_objc_protocol should have returned DyldMainProtocol from main.exe");