]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/dyld_process_info_notify.dtest/main.c
dyld-421.2.tar.gz
[apple/dyld.git] / testing / test-cases / dyld_process_info_notify.dtest / main.c
1
2 // BUILD: $CC target.c -o $BUILD_DIR/target.exe
3 // BUILD: $CC foo.c -o $BUILD_DIR/libfoo.dylib -dynamiclib
4 // BUILD: $CC main.c -o $BUILD_DIR/dyld_process_info_notify.exe
5 // BUILD: $TASK_FOR_PID_ENABLE $BUILD_DIR/dyld_process_info_notify.exe
6
7 // RUN_TIMEOUT: 2400
8 // RUN: $SUDO ./dyld_process_info_notify.exe $RUN_DIR/target.exe
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <dlfcn.h>
13 #include <unistd.h>
14 #include <signal.h>
15 #include <spawn.h>
16 #include <errno.h>
17 #include <mach/mach.h>
18 #include <mach/machine.h>
19 #include <mach-o/dyld_process_info.h>
20 #include <dispatch/dispatch.h>
21
22
23 extern char** environ;
24
25 #if __x86_64__
26 cpu_type_t otherArch[] = { CPU_TYPE_I386 };
27 #elif __i386__
28 cpu_type_t otherArch[] = { CPU_TYPE_X86_64 };
29 #elif __arm64__
30 cpu_type_t otherArch[] = { CPU_TYPE_ARM };
31 #elif __arm__
32 cpu_type_t otherArch[] = { CPU_TYPE_ARM64 };
33 #endif
34
35 static task_t launchTest(const char* testProgPath, const char* arg1, bool launchOtherArch, bool launchSuspended)
36 {
37 //fprintf(stderr, "launchTest() launchOtherArch=%d, launchSuspended=%d, arg=%s\n", launchOtherArch, launchSuspended, arg1);
38 posix_spawnattr_t attr;
39 if ( posix_spawnattr_init(&attr) != 0 ) {
40 printf("[FAIL] dyld_process_info_notify posix_spawnattr_init()\n");
41 exit(0);
42 }
43 if ( launchSuspended ) {
44 if ( posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED) != 0 ) {
45 printf("[FAIL] dyld_process_info_notify POSIX_SPAWN_START_SUSPENDED\n");
46 exit(0);
47 }
48 }
49 if ( launchOtherArch ) {
50 size_t copied;
51 if ( posix_spawnattr_setbinpref_np(&attr, 1, otherArch, &copied) != 0 ) {
52 printf("[FAIL] dyld_process_info_notify posix_spawnattr_setbinpref_np()\n");
53 exit(0);
54 }
55 }
56
57 pid_t childPid;
58 const char* argv[] = { testProgPath, arg1, NULL };
59 int psResult = posix_spawn(&childPid, testProgPath, NULL, &attr, (char**)argv, environ);
60 if ( psResult != 0 ) {
61 printf("[FAIL] dyld_process_info_notify posix_spawn(%s) failed, err=%d\n", testProgPath, psResult);
62 exit(0);
63 }
64 //printf("child pid=%d\n", childPid);
65
66 task_t childTask = 0;
67 if ( task_for_pid(mach_task_self(), childPid, &childTask) != KERN_SUCCESS ) {
68 printf("[FAIL] dyld_process_info_notify task_for_pid()\n");
69 kill(childPid, SIGKILL);
70 exit(0);
71 }
72
73 return childTask;
74 }
75
76 static void wait_util_task_suspended(task_t task)
77 {
78 struct task_basic_info info;
79 do {
80 unsigned count = TASK_BASIC_INFO_COUNT;
81 kern_return_t kr = task_info(task, TASK_BASIC_INFO, (task_info_t)&info, &count);
82 //fprintf(stderr, "task_info() => %d, suspendCount=%d\n", kr, info.suspend_count);
83 sleep(1);
84 } while ( info.suspend_count == 0 );
85 }
86
87
88 static bool monitor(task_t task, bool disconnectEarly, bool attachLate)
89 {
90 kern_return_t kr;
91 __block bool sawMainExecutable = false;
92 __block bool sawlibSystem = false;
93 __block bool gotTerminationNotice = false;
94 __block bool gotEarlyNotice = false;
95 __block int libFooLoadCount = 0;
96 __block int libFooUnloadCount = 0;
97 dispatch_semaphore_t taskDone = dispatch_semaphore_create(0);
98
99 dispatch_queue_t serviceQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
100
101 unsigned count = 0;
102 dyld_process_info_notify handle;
103 do {
104 handle = _dyld_process_info_notify(task, serviceQueue,
105 ^(bool unload, uint64_t timestamp, uint64_t machHeader, const uuid_t uuid, const char* path) {
106 if ( strstr(path, "/target.exe") != NULL )
107 sawMainExecutable = true;
108 if ( strstr(path, "/libSystem") != NULL )
109 sawlibSystem = true;
110 if ( strstr(path, "/libfoo.dylib") != NULL ) {
111 if ( unload )
112 ++libFooUnloadCount;
113 else
114 ++libFooLoadCount;
115 if ( disconnectEarly ) {
116 gotEarlyNotice = true;
117 dispatch_semaphore_signal(taskDone);
118 }
119 }
120 //fprintf(stderr, "unload=%d, 0x%012llX <%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X> %s\n",
121 // unload, machHeader, uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
122 // uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15], path);
123 },
124 ^{
125 //fprintf(stderr, "target exited\n");
126 gotTerminationNotice = true;
127 dispatch_semaphore_signal(taskDone);
128 },
129 &kr);
130 ++count;
131 if ( handle == NULL )
132 fprintf(stderr, "_dyld_process_info_notify() returned NULL, result=%d, count=%d\n", kr, count);
133 } while ( (handle == NULL) && (count < 5) );
134
135 if ( handle == NULL ) {
136 return false;
137 }
138
139 // if process suspends itself, wait until it has done so
140 if ( attachLate )
141 wait_util_task_suspended(task);
142
143 // resume from initial suspend
144 task_resume(task);
145
146 // block waiting for notification that target has exited
147 bool gotSignal = (dispatch_semaphore_wait(taskDone, dispatch_time(DISPATCH_TIME_NOW, 10LL * NSEC_PER_SEC)) == 0);
148 _dyld_process_info_notify_release(handle);
149
150
151 if ( !gotSignal ) {
152 fprintf(stderr, "did not get exit signal\n");
153 return false;
154 }
155
156 if ( !attachLate && !sawMainExecutable ) {
157 fprintf(stderr, "did not get load notification of main executable\n");
158 return false;
159 }
160
161 if ( !attachLate && !sawlibSystem ) {
162 fprintf(stderr, "did not get load notification of libSystem\n");
163 return false;
164 }
165
166 if ( disconnectEarly ) {
167 if ( libFooLoadCount != 1 ) {
168 fprintf(stderr, "got %d load notifications about libFoo instead of 1\n", libFooLoadCount);
169 return false;
170 }
171 if ( libFooUnloadCount != 0 ) {
172 fprintf(stderr, "got %d unload notifications about libFoo instead of 1\n", libFooUnloadCount);
173 return false;
174 }
175 }
176 else {
177 if ( libFooLoadCount != 3 ) {
178 fprintf(stderr, "got %d load notifications about libFoo instead of 3\n", libFooLoadCount);
179 return false;
180 }
181 if ( libFooUnloadCount != 3 ) {
182 fprintf(stderr, "got %d unload notifications about libFoo instead of 3\n", libFooUnloadCount);
183 return false;
184 }
185 }
186
187 return true;
188 }
189
190 static void validateMaxNotifies(task_t task)
191 {
192 dispatch_queue_t serviceQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
193 dyld_process_info_notify handles[10];
194 for (int i=0; i < 10; ++i) {
195 kern_return_t kr;
196 handles[i] = _dyld_process_info_notify(task, serviceQueue,
197 ^(bool unload, uint64_t timestamp, uint64_t machHeader, const uuid_t uuid, const char* path) {
198 //fprintf(stderr, "unload=%d, 0x%012llX <%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X> %s\n",
199 // unload, machHeader, uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
200 // uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15], path);
201 },
202 ^{
203 //fprintf(stderr, "target exited\n");
204 },
205 &kr);
206 if ( handles[i] == NULL ) {
207 if ( i == 8 ) {
208 // expected failure, because only 8 simultaneous connections allowed
209 // release one and try again
210 _dyld_process_info_notify_release(handles[4]);
211 handles[4] = NULL;
212 }
213 else {
214 fprintf(stderr, "_dyld_process_info_notify() returned NULL and kern_result=%d, on count=%d\n", kr, i);
215 task_terminate(task);
216 exit(0);
217 }
218 }
219 }
220 // release all
221 for (int i=0; i < 10; ++i) {
222 if ( handles[i] != NULL ) {
223 _dyld_process_info_notify_release(handles[i]);
224 }
225 }
226 dispatch_release(serviceQueue);
227 }
228
229 int main(int argc, const char* argv[])
230 {
231 printf("[BEGIN] dyld_process_info_notify\n");
232 if ( argc < 2 ) {
233 printf("[FAIL] dyld_process_info_notify missing argument\n");
234 exit(0);
235 }
236 const char* testProgPath = argv[1];
237
238 dispatch_async(dispatch_get_main_queue(), ^{
239 task_t childTask;
240
241 // test 1) launch test program suspended in same arch as this program
242 childTask = launchTest(testProgPath, "", false, true);
243 if ( ! monitor(childTask, false, false) ) {
244 printf("[FAIL] dyld_process_info_notify launch suspended missed some notifications\n");
245 task_terminate(childTask);
246 exit(0);
247 }
248 task_terminate(childTask);
249
250 // test 2) launch test program in same arch as this program where it sleeps itself
251 childTask = launchTest(testProgPath, "suspend-in-main", false, false);
252 validateMaxNotifies(childTask);
253 if ( ! monitor(childTask, false, true) ) {
254 printf("[FAIL] dyld_process_info_notify launch suspend-in-main missed some notifications\n");
255 task_terminate(childTask);
256 exit(0);
257 }
258 task_terminate(childTask);
259
260 #if !TARGET_OS_WATCH && !TARGET_OS_TV && __LP64__
261 // test 3) launch test program suspended in opposite arch as this program
262 childTask = launchTest(testProgPath, "", true, true);
263 if ( ! monitor(childTask, false, false) ) {
264 printf("[FAIL] dyld_process_info_notify launch suspended other arch missed some notifications\n");
265 task_terminate(childTask);
266 exit(0);
267 }
268 task_terminate(childTask);
269
270 // test 4) launch test program in opposite arch as this program where it sleeps itself
271 childTask = launchTest(testProgPath, "suspend-in-main", true, false);
272 if ( ! monitor(childTask, false, true) ) {
273 printf("[FAIL] dyld_process_info_notify launch other arch suspend-in-main missed some notifications\n");
274 task_terminate(childTask);
275 exit(0);
276 }
277 task_terminate(childTask);
278 #endif
279
280 // test 5) launch test program where we disconnect from it after first dlopen
281 childTask = launchTest(testProgPath, "", false, true);
282 if ( ! monitor(childTask, true, false) ) {
283 printf("[FAIL] dyld_process_info_notify connect/disconnect missed some notifications\n");
284 task_terminate(childTask);
285 exit(0);
286 }
287 task_terminate(childTask);
288
289 printf("[PASS] dyld_process_info_notify\n");
290 exit(0);
291 });
292
293 dispatch_main();
294 }