]> git.saurik.com Git - apple/dyld.git/blob - unit-tests/test-cases/dlclose-dylib-unload/main.c
dyld-851.27.tar.gz
[apple/dyld.git] / unit-tests / test-cases / dlclose-dylib-unload / main.c
1 /*
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 #include <stdio.h> // fprintf(), NULL
24 #include <stdlib.h> // exit(), EXIT_SUCCESS
25 #include <dlfcn.h>
26
27 #include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
28
29
30
31
32
33 void verifyfoo()
34 {
35 // open same dylib three times
36 void* handle1 = dlopen("libfoo.dylib", RTLD_LAZY);
37 if ( handle1 == NULL ) {
38 FAIL("dlclose-dylib-unload: dlopen(\"libfoo.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
39 exit(0);
40 }
41
42 void* handle2 = dlopen("libfoo.dylib", RTLD_LAZY);
43 if ( handle2 == NULL ) {
44 FAIL("dlclose-dylib-unload: dlopen(\"libfoo.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
45 exit(0);
46 }
47
48 void* handle3 = dlopen("libfoo.dylib", RTLD_LAZY);
49 if ( handle3 == NULL ) {
50 FAIL("dlclose-dylib-unload: dlopen(\"libfoo.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
51 exit(0);
52 }
53
54 // get symbol
55 void* sym = dlsym(handle1, "foo");
56 if ( sym == NULL ) {
57 FAIL("dlclose-dylib-unload: dlsym(handle1, \"foo\") failed");
58 exit(0);
59 }
60
61 // close same bundle three times
62 if ( dlclose(handle3) != 0 ) {
63 FAIL("dlclose-dylib-unload: dlclose(handle3) != 0, dlerrr()=%s", dlerror());
64 exit(0);
65 }
66
67 if ( dlclose(handle2) != 0 ) {
68 FAIL("dlclose-dylib-unload: dlclose(handle2) != 0, dlerrr()=%s", dlerror());
69 exit(0);
70 }
71
72 if ( dlclose(handle1) != 0 ) {
73 FAIL("dlclose-dylib-unload: dlclose(handle1) != 0, dlerrr()=%s", dlerror());
74 exit(0);
75 }
76
77 // symbol foo should no longer be accessible via dladdr()
78 Dl_info info;
79 if ( dladdr(sym, &info) != 0 ) {
80 FAIL("dlclose-dylib-unload: dladdr(foo_sym) != 0, but should have failed");
81 //exit(0);
82 }
83
84 // extra close should fail
85 if ( dlclose(handle1) == 0 ) {
86 FAIL("dlclose-dylib-unload: dlclose(foo_handle4) == 0, but should have failed");
87 //exit(0);
88 }
89
90 }
91
92
93
94 void verifybar()
95 {
96 // open same dylib three times
97 void* handle1 = dlopen("libbar.dylib", RTLD_LAZY);
98 if ( handle1 == NULL ) {
99 FAIL("dlclose-dylib-unload: dlopen(\"libbar.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
100 exit(0);
101 }
102
103 void* handle2 = dlopen("libbar.dylib", RTLD_LAZY);
104 if ( handle2 == NULL ) {
105 FAIL("dlclose-dylib-unload: dlopen(\"libbar.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
106 exit(0);
107 }
108
109 void* handle3 = dlopen("libbar.dylib", RTLD_LAZY);
110 if ( handle3 == NULL ) {
111 FAIL("dlclose-dylib-unload: dlopen(\"libbar.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
112 exit(0);
113 }
114
115 // get symbol
116 void* sym = dlsym(handle1, "bar");
117 if ( sym == NULL ) {
118 FAIL("dlclose-dylib-unload: dlsym(handle1, \"bar\") failed");
119 exit(0);
120 }
121
122 // close same bundle three times
123 if ( dlclose(handle3) != 0 ) {
124 FAIL("dlclose-dylib-unload: dlclose(handle3) != 0, dlerrr()=%s", dlerror());
125 exit(0);
126 }
127
128 if ( dlclose(handle2) != 0 ) {
129 FAIL("dlclose-dylib-unload: dlclose(handle2) != 0, dlerrr()=%s", dlerror());
130 exit(0);
131 }
132
133 if ( dlclose(handle1) != 0 ) {
134 FAIL("dlclose-dylib-unload: dlclose(handle1) != 0, dlerrr()=%s", dlerror());
135 exit(0);
136 }
137
138 // symbol bar should still longer be accessible via dladdr() because of external reference to libbar.dylib
139 Dl_info info;
140 if ( dladdr(sym, &info) == 0 ) {
141 FAIL("dlclose-dylib-unload: dladdr(bar_sym) == 0, but should have succeeded");
142 exit(0);
143 }
144
145 // extra close should fail
146 if ( dlclose(handle1) == 0 ) {
147 FAIL("dlclose-dylib-unload: dlclose(bar_handle4) == 0, but should have failed");
148 exit(0);
149 }
150 }
151
152
153 // verify libbar.dylib can be loaded and unloaded
154 // verify libbar.dylib can be loaded, but cannot be unloaded (because main executable links against it)
155 int main()
156 {
157 verifyfoo();
158 verifybar();
159
160 PASS("dlclose-dylib-unload");
161 return EXIT_SUCCESS;
162 }