]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/dyld_process_info_notify.dtest/main.c
d76d44f5fdc4035454c988a39c87c2640f0094d8
[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 #include <Availability.h>
22
23
24 extern char** environ;
25
26 #if __x86_64__
27 cpu_type_t otherArch[] = { CPU_TYPE_I386 };
28 #elif __i386__
29 cpu_type_t otherArch[] = { CPU_TYPE_X86_64 };
30 #elif __arm64__
31 cpu_type_t otherArch[] = { CPU_TYPE_ARM };
32 #elif __arm__
33 cpu_type_t otherArch[] = { CPU_TYPE_ARM64 };
34 #endif
35
36 struct task_and_pid {
37 pid_t pid;
38 task_t task;
39 };
40
41 static struct task_and_pid launchTest(const char* testProgPath, const char* arg1, bool launchOtherArch, bool launchSuspended)
42 {
43 //fprintf(stderr, "launchTest() launchOtherArch=%d, launchSuspended=%d, arg=%s\n", launchOtherArch, launchSuspended, arg1);
44 posix_spawnattr_t attr = 0;
45 if ( posix_spawnattr_init(&attr) != 0 ) {
46 printf("[FAIL] dyld_process_info_notify posix_spawnattr_init()\n");
47 exit(0);
48 }
49 if ( launchSuspended ) {
50 if ( posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED) != 0 ) {
51 printf("[FAIL] dyld_process_info_notify POSIX_SPAWN_START_SUSPENDED\n");
52 exit(0);
53 }
54 }
55 if ( launchOtherArch ) {
56 size_t copied;
57 if ( posix_spawnattr_setbinpref_np(&attr, 1, otherArch, &copied) != 0 ) {
58 printf("[FAIL] dyld_process_info_notify posix_spawnattr_setbinpref_np()\n");
59 exit(0);
60 }
61 }
62
63 struct task_and_pid child;
64 const char* argv[] = { testProgPath, arg1, NULL };
65 int psResult = posix_spawn(&child.pid, testProgPath, NULL, &attr, (char**)argv, environ);
66 if ( psResult != 0 ) {
67 printf("[FAIL] dyld_process_info_notify posix_spawn(%s) failed, err=%d\n", testProgPath, psResult);
68 exit(0);
69 }
70 if (posix_spawnattr_destroy(&attr) != 0) {
71 printf("[FAIL] dyld_process_info_notify posix_spawnattr_destroy()\n");
72 exit(0);
73 }
74 if ( task_for_pid(mach_task_self(), child.pid, &child.task) != KERN_SUCCESS ) {
75 printf("[FAIL] dyld_process_info_notify task_for_pid()\n");
76 (void)kill(child.pid, SIGKILL);
77 exit(0);
78 }
79
80 return child;
81 }
82
83 static void killTest(struct task_and_pid tp) {
84 int r = kill(tp.pid, SIGKILL);
85 waitpid(tp.pid, &r, 0);
86 }
87
88 static void wait_util_task_suspended(task_t task)
89 {
90 struct task_basic_info info;
91 do {
92 unsigned count = TASK_BASIC_INFO_COUNT;
93 kern_return_t kr = task_info(task, TASK_BASIC_INFO, (task_info_t)&info, &count);
94 //fprintf(stderr, "task_info() => %d, suspendCount=%d\n", kr, info.suspend_count);
95 sleep(1);
96 } while ( info.suspend_count == 0 );
97 }
98
99
100 static bool monitor(struct task_and_pid tp, bool disconnectEarly, bool attachLate)
101 {
102 kern_return_t kr;
103 __block bool sawMainExecutable = false;
104 __block bool sawlibSystem = false;
105 __block bool gotTerminationNotice = false;
106 __block bool gotEarlyNotice = false;
107 __block bool gotMainNotice = false;
108 __block bool gotMainNoticeBeforeAllInitialDylibs = false;
109 __block bool gotFooNoticeBeforeMain = false;
110
111 __block int libFooLoadCount = 0;
112 __block int libFooUnloadCount = 0;
113 dispatch_semaphore_t taskDone = dispatch_semaphore_create(0);
114
115 dispatch_queue_t serviceQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
116
117 unsigned count = 0;
118 dyld_process_info_notify handle;
119 do {
120 handle = _dyld_process_info_notify(tp.task, serviceQueue,
121 ^(bool unload, uint64_t timestamp, uint64_t machHeader, const uuid_t uuid, const char* path) {
122 if ( strstr(path, "/target.exe") != NULL )
123 sawMainExecutable = true;
124 if ( strstr(path, "/libSystem") != NULL )
125 sawlibSystem = true;
126 if ( strstr(path, "/libfoo.dylib") != NULL ) {
127 if ( !gotMainNotice )
128 gotFooNoticeBeforeMain = true;
129 if ( unload )
130 ++libFooUnloadCount;
131 else
132 ++libFooLoadCount;
133 if ( disconnectEarly ) {
134 gotEarlyNotice = true;
135 dispatch_semaphore_signal(taskDone);
136 }
137 }
138 //fprintf(stderr, "unload=%d, 0x%012llX <%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X> %s\n",
139 // unload, machHeader, uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
140 // uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15], path);
141 },
142 ^{
143 //fprintf(stderr, "target exited\n");
144 gotTerminationNotice = true;
145 dispatch_semaphore_signal(taskDone);
146 },
147 &kr);
148 ++count;
149 if ( handle == NULL )
150 fprintf(stderr, "_dyld_process_info_notify() returned NULL, result=%d, count=%d\n", kr, count);
151 } while ( (handle == NULL) && (count < 5) );
152
153 if ( handle == NULL ) {
154 return false;
155 }
156
157 if (!attachLate) {
158 // If the process starts suspended register for main(),
159 // otherwise skip since this test is a race between
160 // process setup and notification registration
161 _dyld_process_info_notify_main(handle, ^{
162 //fprintf(stderr, "target entering main()\n");
163 gotMainNotice = true;
164 if ( !sawMainExecutable || !sawlibSystem )
165 gotMainNoticeBeforeAllInitialDylibs = true;
166 });
167 } else {
168 // if process suspends itself, wait until it has done so
169 wait_util_task_suspended(tp.task);
170 }
171
172 // resume from initial suspend
173 kill(tp.pid, SIGCONT);
174
175 // block waiting for notification that target has exited
176 bool gotSignal = (dispatch_semaphore_wait(taskDone, dispatch_time(DISPATCH_TIME_NOW, 10LL * NSEC_PER_SEC)) == 0);
177 _dyld_process_info_notify_release(handle);
178
179
180 if ( !gotSignal ) {
181 fprintf(stderr, "did not get exit signal\n");
182 return false;
183 }
184
185 // Do not run any tests associated with startup unless the kernel suspended us
186 // before main()
187 if (!attachLate) {
188 if ( !sawMainExecutable ) {
189 fprintf(stderr, "did not get load notification of main executable\n");
190 return false;
191 }
192
193 if ( !gotMainNotice ) {
194 fprintf(stderr, "did not get notification of main()\n");
195 return false;
196 }
197
198 if ( gotMainNoticeBeforeAllInitialDylibs ) {
199 fprintf(stderr, "notification of main() arrived before all initial dylibs\n");
200 return false;
201 }
202
203 if ( gotFooNoticeBeforeMain ) {
204 fprintf(stderr, "notification of main() arrived after libfoo load notice\n");
205 return false;
206 }
207
208 if ( !sawlibSystem ) {
209 fprintf(stderr, "did not get load notification of libSystem\n");
210 return false;
211 }
212 }
213
214 if ( disconnectEarly ) {
215 if ( libFooLoadCount != 1 ) {
216 fprintf(stderr, "got %d load notifications about libFoo instead of 1\n", libFooLoadCount);
217 return false;
218 }
219 if ( libFooUnloadCount != 0 ) {
220 fprintf(stderr, "got %d unload notifications about libFoo instead of 1\n", libFooUnloadCount);
221 return false;
222 }
223 }
224 else {
225 if ( libFooLoadCount != 3 ) {
226 fprintf(stderr, "got %d load notifications about libFoo instead of 3\n", libFooLoadCount);
227 return false;
228 }
229 if ( libFooUnloadCount != 3 ) {
230 fprintf(stderr, "got %d unload notifications about libFoo instead of 3\n", libFooUnloadCount);
231 return false;
232 }
233 }
234
235 return true;
236 }
237
238 static void validateMaxNotifies(struct task_and_pid tp)
239 {
240 dispatch_queue_t serviceQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
241 dyld_process_info_notify handles[10];
242 for (int i=0; i < 10; ++i) {
243 kern_return_t kr;
244 handles[i] = _dyld_process_info_notify(tp.task, serviceQueue,
245 ^(bool unload, uint64_t timestamp, uint64_t machHeader, const uuid_t uuid, const char* path) {
246 //fprintf(stderr, "unload=%d, 0x%012llX <%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X> %s\n",
247 // unload, machHeader, uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
248 // uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15], path);
249 },
250 ^{
251 //fprintf(stderr, "target exited\n");
252 },
253 &kr);
254 if ( handles[i] == NULL ) {
255 if ( i == 8 ) {
256 // expected failure, because only 8 simultaneous connections allowed
257 // release one and try again
258 _dyld_process_info_notify_release(handles[4]);
259 handles[4] = NULL;
260 }
261 else {
262 fprintf(stderr, "_dyld_process_info_notify() returned NULL and kern_result=%d, on count=%d\n", kr, i);
263 killTest(tp);
264 exit(0);
265 }
266 }
267 }
268 // release all
269 for (int i=0; i < 10; ++i) {
270 if ( handles[i] != NULL ) {
271 _dyld_process_info_notify_release(handles[i]);
272 }
273 }
274 dispatch_release(serviceQueue);
275 }
276
277 int main(int argc, const char* argv[])
278 {
279 printf("[BEGIN] dyld_process_info_notify\n");
280 if ( argc < 2 ) {
281 printf("[FAIL] dyld_process_info_notify missing argument\n");
282 exit(0);
283 }
284 const char* testProgPath = argv[1];
285
286 dispatch_async(dispatch_get_main_queue(), ^{
287 struct task_and_pid child;
288
289 // test 1) launch test program suspended in same arch as this program
290 child = launchTest(testProgPath, "", false, true);
291 if ( ! monitor(child, false, false) ) {
292 printf("[FAIL] dyld_process_info_notify launch suspended missed some notifications\n");
293 killTest(child);
294 exit(0);
295 }
296 killTest(child);
297
298 // test 2) launch test program in same arch as this program where it sleeps itself
299 child = launchTest(testProgPath, "suspend-in-main", false, false);
300 validateMaxNotifies(child);
301 if ( ! monitor(child, false, true) ) {
302 printf("[FAIL] dyld_process_info_notify launch suspend-in-main missed some notifications\n");
303 killTest(child);
304 exit(0);
305 }
306 killTest(child);
307
308 #if __MAC_OS_X_VERSION_MIN_REQUIRED
309 // test 3) launch test program suspended in opposite arch as this program
310 child = launchTest(testProgPath, "", true, true);
311 if ( ! monitor(child, false, false) ) {
312 printf("[FAIL] dyld_process_info_notify launch suspended other arch missed some notifications\n");
313 killTest(child);
314 exit(0);
315 }
316 killTest(child);
317
318 // test 4) launch test program in opposite arch as this program where it sleeps itself
319 child = launchTest(testProgPath, "suspend-in-main", true, false);
320 if ( ! monitor(child, false, true) ) {
321 printf("[FAIL] dyld_process_info_notify launch other arch suspend-in-main missed some notifications\n");
322 killTest(child);
323 exit(0);
324 }
325 killTest(child);
326 #endif
327
328 // test 5) launch test program where we disconnect from it after first dlopen
329 child = launchTest(testProgPath, "", false, true);
330 if ( ! monitor(child, true, false) ) {
331 printf("[FAIL] dyld_process_info_notify connect/disconnect missed some notifications\n");
332 killTest(child);
333 exit(0);
334 }
335 killTest(child);
336
337 printf("[PASS] dyld_process_info_notify\n");
338 exit(0);
339 });
340
341 dispatch_main();
342 }