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} -Xlinker -undefined -Xlinker dynamic_lookup
8 $C{COMPILE} $DIR/unload.m -o unload.exe -framework Foundation
14 ld: warning: -undefined dynamic_lookup is deprecated on .*
20 #include <objc/runtime.h>
26 #if __has_feature(objc_arc)
30 testwarn("rdar://11368528 confused by Foundation");
36 static id forward_handler(void)
41 static BOOL hasName(const char * const *names, const char *query)
44 while ((name = *names++)) {
45 if (strstr(name, query)) return YES;
55 unsigned int imageCount, imageCount0;
59 names = objc_copyImageNames(&imageCount0);
63 void *bundle = dlopen("unload2.bundle", RTLD_LAZY);
66 names = objc_copyImageNames(&imageCount);
68 testassert(imageCount == imageCount0 + 1);
69 testassert(hasName(names, "unload2.bundle"));
72 Class small = objc_getClass("SmallClass");
73 Class big = objc_getClass("BigClass");
74 Class missing = objc_getClass("SubclassOfMissingWeakImport");
79 name = class_getImageName(small);
81 testassert(strstr(name, "unload2.bundle"));
82 name = class_getImageName(big);
84 testassert(strstr(name, "unload2.bundle"));
91 // give BigClass and BigClass->isa large method caches (4692641)
92 // Flush caches part way through to test large empty caches.
93 for (i = 0; i < 3000; i++) {
94 sprintf(buf, "method_%d", i);
95 SEL sel = sel_registerName(buf);
96 ((void(*)(id, SEL))objc_msgSend)(o2, sel);
97 ((void(*)(id, SEL))objc_msgSend)(object_getClass(o2), sel);
99 _objc_flush_caches(object_getClass(o2));
100 for (i = 0; i < 17000; i++) {
101 sprintf(buf, "method_%d", i);
102 SEL sel = sel_registerName(buf);
103 ((void(*)(id, SEL))objc_msgSend)(o2, sel);
104 ((void(*)(id, SEL))objc_msgSend)(object_getClass(o2), sel);
112 int err = dlclose(bundle);
113 testassert(err == 0);
114 err = dlclose(bundle);
115 testassert(err == -1); // already closed
117 _objc_flush_caches(nil);
119 testassert(objc_getClass("SmallClass") == NULL);
120 testassert(objc_getClass("BigClass") == NULL);
122 names = objc_copyImageNames(&imageCount);
124 testassert(imageCount == imageCount0);
125 testassert(! hasName(names, "unload2.bundle"));
128 // these selectors came from the bundle
129 testassert(0 == strcmp("unload2_instance_method", sel_getName(sel_registerName("unload2_instance_method"))));
130 testassert(0 == strcmp("unload2_category_method", sel_getName(sel_registerName("unload2_category_method"))));
132 // This protocol came from the bundle.
133 // It isn't unloaded cleanly (rdar://20664713), but neither
134 // may it cause the protocol table to crash after unloading.
135 testassert(objc_getProtocol("SmallProtocol"));
141 char *useClosures = getenv("DYLD_USE_CLOSURES");
142 int dyld3 = useClosures != NULL && useClosures[0] != '0';
144 objc_setForwardHandler((void*)&forward_handler, (void*)&forward_handler);
146 #if defined(__arm__) || defined(__arm64__)
149 int count = is_guardmalloc() ? 10 : 100;
154 // fixme heap use goes up 512 bytes after the 2nd cycle only - bad or not?
159 for (int i = 0; i < count; i++) {
162 // dyld3 currently leaks 8 bytes for each dlopen/dlclose pair, so accommodate it. rdar://problem/53769254
163 leak_check(dyld3 ? (count * sizeof(void *)) : 0);
165 // 5359412 Make sure dylibs with nothing other than image_info can close
166 void *dylib = dlopen("unload3.dylib", RTLD_LAZY);
168 int err = dlclose(dylib);
169 testassert(err == 0);
170 err = dlclose(dylib);
171 // dyld3 doesn't error when dlclosing the dylib twice. This is probably expected. rdar://problem/53769374
173 testassert(err == -1); // already closed
175 // Make sure dylibs with real objc content cannot close
176 dylib = dlopen("unload4.dylib", RTLD_LAZY);
178 err = dlclose(dylib);
179 testassert(err == 0);
180 err = dlclose(dylib);
181 // dyld3 doesn't error when dlclosing the dylib twice. This is probably expected. rdar://problem/53769374
183 testassert(err == -1); // already closed