dyld-733.6.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 static int singleUnMappedCount = 0;
38 static int batchBoundCount = 0;
39 static int singleDepsInitedCount = 0;
40 static int singleBoundCount = 0;
41
42 static const char* batchMappedHandler(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[])
43 {
44 for (uint32_t i=0; i < infoCount; ++i)
45 printf("batchMappedHandler(): %u/%u -> %s\n", i, infoCount, info[i].imageFilePath);
46 if ( state != dyld_image_state_dependents_mapped ) {
47 FAIL("image-state-change: batchMappedHandler passed state %d", state);
48 exit(0);
49 }
50 batchMappedCount += infoCount;
51 return NULL;
52 }
53
54 static const char* batchBoundHandler(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[])
55 {
56 for (uint32_t i=0; i < infoCount; ++i)
57 printf("batchBoundHandler(): %u/%u -> %s\n", i, infoCount, info[i].imageFilePath);
58 if ( state != dyld_image_state_bound ) {
59 FAIL("image-state-change: batchBoundHandler passed state %d", state);
60 exit(0);
61 }
62 batchBoundCount += infoCount;
63 return NULL;
64 }
65
66 static const char* singleMappedHandler(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[])
67 {
68 printf("singleMappedHandler(%s)\n", info[0].imageFilePath);
69 if ( state != dyld_image_state_mapped ) {
70 FAIL("image-state-change: singleMappedHandler passed state %d", state);
71 exit(0);
72 }
73 if ( infoCount != 1 ) {
74 FAIL("image-state-change: singleMappedHandler given %d images", infoCount);
75 exit(0);
76 }
77 ++singleMappedCount;
78 return NULL;
79 }
80
81
82 static const char* singleBoundHandler(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[])
83 {
84 printf("singleBoundHandler(%s)\n", info[0].imageFilePath);
85 if ( state != dyld_image_state_bound ) {
86 FAIL("image-state-change: singleBoundHandler passed state %d", state);
87 exit(0);
88 }
89 if ( infoCount != 1 ) {
90 FAIL("image-state-change: singleBoundHandler given %d images", infoCount);
91 exit(0);
92 }
93 ++singleBoundCount;
94 return NULL;
95 }
96
97
98
99 static const char* singleDepsInitedHandler(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[])
100 {
101 printf("singleDepsInitedHandler(%s)\n", info[0].imageFilePath);
102 if ( state != dyld_image_state_dependents_initialized ) {
103 FAIL("image-state-change: singleDepsInitedHandler passed state %d", state);
104 exit(0);
105 }
106 if ( infoCount != 1 ) {
107 FAIL("image-state-change: singleDepsInitedHandler given %d images", infoCount);
108 exit(0);
109 }
110 ++singleDepsInitedCount;
111 return NULL;
112 }
113
114
115
116 static const char* singleUnmappedHandler(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[])
117 {
118 printf("singleUnmappedHandler(%s)\n", info[0].imageFilePath);
119 if ( state != dyld_image_state_terminated ) {
120 FAIL("image-state-change: singleUnmappedHandler passed state %d", state);
121 exit(0);
122 }
123 if ( infoCount != 1 ) {
124 FAIL("image-state-change: singleUnmappedHandler given %d images", infoCount);
125 exit(0);
126 }
127 ++singleUnMappedCount;
128 return NULL;
129 }
130
131 static void loadAndUnLoad()
132 {
133 void* handle = dlopen("foo.bundle", RTLD_LAZY);
134 if ( handle == NULL ) {
135 FAIL("image-state-change: dlopen(foo.bundle) failed: %s", dlerror());
136 exit(0);
137 }
138
139 int result = dlclose(handle);
140 if ( result != 0 ) {
141 FAIL("image-state-change: dlclose(handle) returned %d, %s", result, dlerror());
142 exit(0);
143 }
144 }
145
146
147 int main(int argc, const char* argv[])
148 {
149 // tell dyld we want to know when images are mapped
150 dyld_register_image_state_change_handler(dyld_image_state_dependents_mapped, true, batchMappedHandler);
151 dyld_register_image_state_change_handler(dyld_image_state_mapped, false, singleMappedHandler);
152 dyld_register_image_state_change_handler(dyld_image_state_bound, true, batchBoundHandler);
153 dyld_register_image_state_change_handler(dyld_image_state_bound, false, singleBoundHandler);
154 dyld_register_image_state_change_handler(dyld_image_state_dependents_initialized, false, singleDepsInitedHandler);
155 dyld_register_image_state_change_handler(dyld_image_state_terminated, false, singleUnmappedHandler);
156
157 // with batch mode we get notified of existing images, but not with single mode, so re-sync counts
158 batchMappedCount=0;
159
160 loadAndUnLoad();
161
162 loadAndUnLoad();
163
164 if ( (singleMappedCount == batchMappedCount) && (singleMappedCount == singleUnMappedCount) )
165 PASS("image-state-change");
166 else
167 FAIL("image-state-change: batch count=%d, single count=%d", batchMappedCount, singleMappedCount);
168
169 return EXIT_SUCCESS;
170 }