1 // xpc leaks memory in dlopen(). Disable it.
2 // TEST_ENV XPC_SERVICES_UNAVAILABLE=1
5 $C{COMPILE} $DIR/unload4.m -o unload4.dylib -dynamiclib
6 $C{COMPILE_C} $DIR/unload3.c -o unload3.dylib -dynamiclib
7 $C{COMPILE} $DIR/unload2.m -o unload2.bundle -bundle $C{FORCE_LOAD_ARCLITE}
8 $C{COMPILE} $DIR/unload.m -o unload.exe -framework Foundation
13 i386 Mac doesn't have libarclite
15 ld: warning: ignoring file .* which is not the architecture being linked \(i386\).*
21 #include <objc/runtime.h>
27 #if __has_feature(objc_arc)
31 testwarn("rdar://11368528 confused by Foundation");
37 static id forward_handler(void)
42 static BOOL hasName(const char * const *names, const char *query)
45 while ((name = *names++)) {
46 if (strstr(name, query)) return YES;
56 unsigned int imageCount, imageCount0;
60 names = objc_copyImageNames(&imageCount0);
64 void *bundle = dlopen("unload2.bundle", RTLD_LAZY);
67 names = objc_copyImageNames(&imageCount);
69 testassert(imageCount == imageCount0 + 1);
70 testassert(hasName(names, "unload2.bundle"));
73 Class small = objc_getClass("SmallClass");
74 Class big = objc_getClass("BigClass");
78 name = class_getImageName(small);
80 testassert(strstr(name, "unload2.bundle"));
81 name = class_getImageName(big);
83 testassert(strstr(name, "unload2.bundle"));
90 // give BigClass and BigClass->isa large method caches (4692641)
91 // Flush caches part way through to test large empty caches.
92 for (i = 0; i < 3000; i++) {
93 sprintf(buf, "method_%d", i);
94 SEL sel = sel_registerName(buf);
95 ((void(*)(id, SEL))objc_msgSend)(o2, sel);
96 ((void(*)(id, SEL))objc_msgSend)(object_getClass(o2), sel);
98 _objc_flush_caches(object_getClass(o2));
99 for (i = 0; i < 17000; i++) {
100 sprintf(buf, "method_%d", i);
101 SEL sel = sel_registerName(buf);
102 ((void(*)(id, SEL))objc_msgSend)(o2, sel);
103 ((void(*)(id, SEL))objc_msgSend)(object_getClass(o2), sel);
111 int err = dlclose(bundle);
112 testassert(err == 0);
113 err = dlclose(bundle);
114 testassert(err == -1); // already closed
116 _objc_flush_caches(nil);
118 testassert(objc_getClass("SmallClass") == NULL);
119 testassert(objc_getClass("BigClass") == NULL);
121 names = objc_copyImageNames(&imageCount);
123 testassert(imageCount == imageCount0);
124 testassert(! hasName(names, "unload2.bundle"));
127 // these selectors came from the bundle
128 testassert(0 == strcmp("unload2_instance_method", sel_getName(sel_registerName("unload2_instance_method"))));
129 testassert(0 == strcmp("unload2_category_method", sel_getName(sel_registerName("unload2_category_method"))));
131 // This protocol came from the bundle.
132 // It isn't unloaded cleanly (rdar://20664713), but neither
133 // may it cause the protocol table to crash after unloading.
134 testassert(objc_getProtocol("SmallProtocol"));
140 objc_setForwardHandler((void*)&forward_handler, (void*)&forward_handler);
142 #if defined(__arm__) || defined(__arm64__)
145 int count = is_guardmalloc() ? 10 : 100;
150 // fixme heap use goes up 512 bytes after the 2nd cycle only - bad or not?
160 // 5359412 Make sure dylibs with nothing other than image_info can close
161 void *dylib = dlopen("unload3.dylib", RTLD_LAZY);
163 int err = dlclose(dylib);
164 testassert(err == 0);
165 err = dlclose(dylib);
166 testassert(err == -1); // already closed
168 // Make sure dylibs with real objc content cannot close
169 dylib = dlopen("unload4.dylib", RTLD_LAZY);
171 err = dlclose(dylib);
172 testassert(err == 0);
173 err = dlclose(dylib);
174 testassert(err == -1); // already closed