]> git.saurik.com Git - apple/dyld.git/blob - unit-tests/test-cases/image-state-change/main.c
dyld-132.13.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
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 const char* singleUnmappedHandler(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[])
68 {
69 printf("singleUnmappedHandler(%s)\n", info[0].imageFilePath);
70 if ( state != dyld_image_state_terminated ) {
71 FAIL("image-state-change: singleUnmappedHandler passed state %d", state);
72 exit(0);
73 }
74 if ( infoCount != 1 ) {
75 FAIL("image-state-change: singleUnmappedHandler given %d images", infoCount);
76 exit(0);
77 }
78 ++singleUnMappedCount;
79 return NULL;
80 }
81
82 static void loadAndUnLoad()
83 {
84 void* handle = dlopen("foo.bundle", RTLD_LAZY);
85 if ( handle == NULL ) {
86 FAIL("image-state-change: dlopen(foo.bundle) failed: %s", dlerror());
87 exit(0);
88 }
89
90 int result = dlclose(handle);
91 if ( result != 0 ) {
92 FAIL("image-state-change: dlclose(handle) returned %d, %s", result, dlerror());
93 exit(0);
94 }
95 }
96
97
98 int main(int argc, const char* argv[])
99 {
100 // tell dyld we want to know when images are mapped
101 dyld_register_image_state_change_handler(dyld_image_state_dependents_mapped, true, batchMappedHandler);
102 dyld_register_image_state_change_handler(dyld_image_state_mapped, false, singleMappedHandler);
103 dyld_register_image_state_change_handler(dyld_image_state_terminated, false, singleUnmappedHandler);
104 // with batch mode we get notified of existing images, but not with single mode, so re-sync counts
105 batchMappedCount=0;
106
107 loadAndUnLoad();
108
109 loadAndUnLoad();
110
111 if ( (singleMappedCount == batchMappedCount) && (singleMappedCount == singleUnMappedCount) )
112 PASS("image-state-change");
113 else
114 FAIL("image-state-change: batch count=%d, single count=%d", batchMappedCount, singleMappedCount);
115
116 return EXIT_SUCCESS;
117 }