]>
git.saurik.com Git - apple/dyld.git/blob - unit-tests/test-cases/dlclose-dylib-ref-count/main.c
2 * Copyright (c) 2012 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
23 #include <stdio.h> // fprintf(), NULL
24 #include <stdlib.h> // exit(), EXIT_SUCCESS
27 #include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
37 void* handleBar
= dlopen("libbar.dylib", RTLD_LAZY
);
38 if ( handleBar
== NULL
) {
39 FAIL("dlclose-dylib-ref-count: dlopen(\"libbar.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
44 void* handleFoo
= dlopen("libfoo.dylib", RTLD_LAZY
);
45 if ( handleFoo
== NULL
) {
46 FAIL("dlclose-dylib-ref-count: dlopen(\"libfoo.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
50 void* sym_base
= dlsym(handleBar
, "base");
51 if ( sym_base
== NULL
) {
52 FAIL("dlclose-dylib-ref-count: dlsym(handleBar, \"base\") failed");
56 void* sym_foo
= dlsym(handleFoo
, "foo");
57 if ( sym_foo
== NULL
) {
58 FAIL("dlclose-dylib-ref-count: dlsym(handleBar, \"base\") failed");
62 void* sym_bar
= dlsym(handleBar
, "bar");
63 if ( sym_bar
== NULL
) {
64 FAIL("dlclose-dylib-ref-count: dlsym(handleBar, \"base\") failed");
68 if ( dlclose(handleBar
) != 0 ) {
69 FAIL("dlclose-dylib-ref-count: dlclose(handleBar) != 0, dlerrr()=%s", dlerror());
73 // sym_base should still be accessible via dladdr() because of external reference from libfoo.dylib
74 if ( dladdr(sym_base
, &info
) == 0 ) {
75 FAIL("dlclose-dylib-ref-count: dladdr(sym_base) == 0, but should have succeeded");
79 // sym_foo should still be accessible via dladdr() because libfoo was dlopen'ed
80 if ( dladdr(sym_foo
, &info
) == 0 ) {
81 FAIL("dlclose-dylib-ref-count: dladdr(sym_foo) == 0, but should have succeeded");
85 // sym_bar should not be accessible via dladdr() because libbar was dlclose'ed
86 if ( dladdr(sym_bar
, &info
) != 0 ) {
87 FAIL("dlclose-dylib-ref-count: dladdr(sym_bar) != 0, but should have failed");
91 if ( dlclose(handleFoo
) != 0 ) {
92 FAIL("dlclose-dylib-ref-count: dlclose(handleBar) != 0, dlerrr()=%s", dlerror());
96 // sym_base should no longer be accessible via dladdr() because libfoo and libbar both closed
97 if ( dladdr(sym_base
, &info
) != 0 ) {
98 FAIL("dlclose-dylib-ref-count: dladdr(base) != 0, but should have failed");
102 // sym_foo should still be accessible via dladdr() because libfoo was dlclose'ed
103 if ( dladdr(sym_foo
, &info
) != 0 ) {
104 FAIL("dlclose-dylib-ref-count: dladdr(sym_foo) != 0, but should have failed");
108 PASS("dlclose-dylib-ref-count");