]> git.saurik.com Git - apple/dyld.git/blob - unit-tests/test-cases/image-state-change/main.c
dyld-95.3.tar.gz
[apple/dyld.git] / unit-tests / test-cases / image-state-change / main.c
1 /*
2 * Copyright (c) 2006 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 <stdbool.h> // fprintf(), NULL
24 #include <stdio.h> // fprintf(), NULL
25 #include <stdlib.h> // exit(), EXIT_SUCCESS
26 #include <stdbool.h>
27 #include <mach-o/dyld.h>
28 #include <mach-o/dyld_priv.h>
29 #include <dlfcn.h>
30
31 #include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
32
33
34
35 static int singleMappedCount = 0;
36 static int batchMappedCount = 0;
37
38
39
40 static const char* batchMappedHandler(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[])
41 {
42 for (uint32_t i=0; i < infoCount; ++i)
43 printf("batchMappedHandler(): %u/%u -> %s\n", i, infoCount, info[i].imageFilePath);
44 if ( state != dyld_image_state_dependents_mapped ) {
45 FAIL("image-state-change: batchMappedHandler passed state %d", state);
46 exit(0);
47 }
48 batchMappedCount += infoCount;
49 return NULL;
50 }
51
52 static const char* singleMappedHandler(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[])
53 {
54 printf("singleMappedHandler(%s)\n", info[0].imageFilePath);
55 if ( state != dyld_image_state_mapped ) {
56 FAIL("image-state-change: singleMappedHandler passed state %d", state);
57 exit(0);
58 }
59 if ( infoCount != 1 ) {
60 FAIL("image-state-change: singleMappedHandler given %d images", infoCount);
61 exit(0);
62 }
63 ++singleMappedCount;
64 return NULL;
65 }
66
67 static void loadAndUnLoad()
68 {
69 void* handle = dlopen("foo.bundle", RTLD_LAZY);
70 if ( handle == NULL ) {
71 FAIL("image-state-change: dlopen(foo.bundle) failed: %s", dlerror());
72 exit(0);
73 }
74
75 int result = dlclose(handle);
76 if ( result != 0 ) {
77 FAIL("image-state-change: dlclose(handle) returned %d, %s", result, dlerror());
78 exit(0);
79 }
80 }
81
82
83 int main(int argc, const char* argv[])
84 {
85 // tell dyld we want to know when images are mapped
86 dyld_register_image_state_change_handler(dyld_image_state_dependents_mapped, true, batchMappedHandler);
87 dyld_register_image_state_change_handler(dyld_image_state_mapped, false, singleMappedHandler);
88 // with batch mode we get notified of existing images, but not with single mode, so re-sync counts
89 batchMappedCount=0;
90
91 loadAndUnLoad();
92
93 loadAndUnLoad();
94
95 if ( singleMappedCount == batchMappedCount )
96 PASS("image-state-change");
97 else
98 FAIL("image-state-change: batch count=%d, single count=%d", batchMappedCount, singleMappedCount);
99
100 return EXIT_SUCCESS;
101 }