]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2013 Apple 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 | int main() | |
33 | { | |
34 | Dl_info info; | |
35 | ||
36 | // load foo | |
37 | void* handleFoo = dlopen("libfoo.dylib", RTLD_LAZY); | |
38 | if ( handleFoo == NULL ) { | |
39 | FAIL("dlclose-dylib-dynamic-ref: dlopen(\"libfoo.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror()); | |
40 | exit(0); | |
41 | } | |
42 | ||
43 | // load bar | |
44 | void* handleBar = dlopen("libbar.dylib", RTLD_LAZY); | |
45 | if ( handleBar == NULL ) { | |
46 | FAIL("dlclose-dylib-dynamic-ref: dlopen(\"libbar.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror()); | |
47 | exit(0); | |
48 | } | |
49 | ||
50 | // load baz | |
51 | void* handleBaz = dlopen("libbaz.dylib", RTLD_LAZY); | |
52 | if ( handleBaz == NULL ) { | |
53 | FAIL("dlclose-dylib-dynamic-ref: dlopen(\"libbaz.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror()); | |
54 | exit(0); | |
55 | } | |
56 | ||
57 | ||
58 | void* sym_foo = dlsym(handleFoo, "foo"); | |
59 | if ( sym_foo == NULL ) { | |
60 | FAIL("dlclose-dylib-dynamic-ref: dlsym(handleFoo, \"foo\") failed"); | |
61 | exit(0); | |
62 | } | |
63 | ||
64 | void* sym_bar = dlsym(handleBar, "bar"); | |
65 | if ( sym_bar == NULL ) { | |
66 | FAIL("dlclose-dylib-dynamic-ref: dlsym(handleBar, \"bar\") failed"); | |
67 | exit(0); | |
68 | } | |
69 | ||
70 | void* sym_baz = dlsym(handleBaz, "baz"); | |
71 | if ( sym_baz == NULL ) { | |
72 | FAIL("dlclose-dylib-dynamic-ref: dlsym(handleBaz, \"baz\") failed"); | |
73 | exit(0); | |
74 | } | |
75 | ||
76 | // since foo loaded first, bar and baz should have a dynamic reference to foo because all | |
77 | // the weak mydata symbols were coaleseced to the one in foo. | |
78 | ||
79 | ||
80 | if ( dlclose(handleFoo) != 0 ) { | |
81 | FAIL("dlclose-dylib-dynamic-ref: dlclose(handleBar) != 0, dlerrr()=%s", dlerror()); | |
82 | exit(0); | |
83 | } | |
84 | ||
85 | // sym_foo should still be accessible via dladdr() because libbar and libbaz | |
86 | // have dynamic references to libfoo. | |
87 | if ( dladdr(sym_foo, &info) == 0 ) { | |
88 | FAIL("dlclose-dylib-dynamic-ref: dladdr(sym_base) == 0, but should have succeeded"); | |
89 | exit(0); | |
90 | } | |
91 | ||
92 | ||
93 | if ( dlclose(handleBar) != 0 ) { | |
94 | FAIL("dlclose-dylib-dynamic-ref: dlclose(handleBar) != 0, dlerrr()=%s", dlerror()); | |
95 | exit(0); | |
96 | } | |
97 | ||
98 | ||
99 | // sym_bar should not be accessible via dladdr() because libbar was dlclose'ed | |
100 | if ( dladdr(sym_bar, &info) != 0 ) { | |
101 | FAIL("dlclose-dylib-dynamic-ref: dladdr(sym_bar) != 0, but should have failed"); | |
102 | exit(0); | |
103 | } | |
104 | ||
105 | // sym_foo should still be accessible via dladdr() because of external libbaz | |
106 | // has a dynamic references to libfoo. | |
107 | if ( dladdr(sym_foo, &info) == 0 ) { | |
108 | FAIL("dlclose-dylib-dynamic-ref: dladdr(sym_base) == 0, but should have succeeded"); | |
109 | exit(0); | |
110 | } | |
111 | ||
112 | ||
113 | if ( dlclose(handleBaz) != 0 ) { | |
114 | FAIL("dlclose-dylib-dynamic-ref: dlclose(handleBar) != 0, dlerrr()=%s", dlerror()); | |
115 | exit(0); | |
116 | } | |
117 | ||
118 | // sym_baz should not be accessible via dladdr() because libbar was dlclose'ed | |
119 | if ( dladdr(sym_baz, &info) != 0 ) { | |
120 | FAIL("dlclose-dylib-dynamic-ref: dladdr(sym_baz) != 0, but should have failed"); | |
121 | exit(0); | |
122 | } | |
123 | ||
124 | ||
125 | // sym_foo should finally be inaccessible via dladdr() because all dynamic references to libfoo are gone | |
126 | if ( dladdr(sym_foo, &info) != 0 ) { | |
127 | FAIL("dlclose-dylib-dynamic-ref: dladdr(sym_foo) == 0, but should have succeeded"); | |
128 | exit(0); | |
129 | } | |
130 | ||
131 | ||
132 | PASS("dlclose-dylib-dynamic-ref"); | |
133 | return EXIT_SUCCESS; | |
134 | } |