Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
#include <darwintest.h>

#include <stdlib.h>
#include <sys/mman.h>
#include <mach/mach.h>
#include <pthread.h>
#include <signal.h>
#include <libkern/OSAtomic.h>
#include <TargetConditionals.h>

T_GLOBAL_META(
	T_META_NAMESPACE("xnu.ipc"),
	T_META_RADAR_COMPONENT_NAME("xnu"),
	T_META_RADAR_COMPONENT_VERSION("IPC"),
	T_META_OWNER("skwok2"),
	T_META_TIMEOUT(15),
	T_META_TAG_VM_PREFERRED,
	T_META_RUN_CONCURRENTLY(TRUE),
	T_META_ASROOT(true));

#define NOT_START 0
#define SUSPENDED 1
#define RESUMED 2
#define RUNNING 3
#define SLEEP_INTERVAL 1000

typedef struct {
	pthread_mutex_t mutex;
	pthread_cond_t cond;
	int flag;
} shared_data_t;

typedef struct {
	pid_t pid;
	task_t task;
	volatile int should_exit;
} test_task_t;

static void
cleanup_shared_data(shared_data_t *shared_data)
{
	pthread_mutex_destroy(&shared_data->mutex);
	pthread_cond_destroy(&shared_data->cond);
}

static void
init_process_shared_data(shared_data_t *shared_data)
{
	int ret;
	pthread_mutexattr_t mattr;
	pthread_condattr_t cattr;

	/* Initialize mutex */
	ret = pthread_mutexattr_init(&mattr);
	T_QUIET; T_ASSERT_EQ(ret, 0, "pthread_mutexattr_init");
	ret = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
	T_QUIET; T_ASSERT_EQ(ret, 0, "pthread_mutexattr_setpshared");
	ret = pthread_mutex_init(&shared_data->mutex, &mattr);
	T_QUIET; T_ASSERT_EQ(ret, 0, "pthread_mutex_init");
	pthread_mutexattr_destroy(&mattr);

	/* Initialize condition variable */
	ret = pthread_condattr_init(&cattr);
	T_QUIET; T_ASSERT_EQ(ret, 0, "pthread_condattr_init");
	ret = pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
	T_QUIET; T_ASSERT_EQ(ret, 0, "pthread_condattr_setpshared");
	ret = pthread_cond_init(&shared_data->cond, &cattr);
	T_QUIET; T_ASSERT_EQ(ret, 0, "pthread_cond_init");
	pthread_condattr_destroy(&cattr);

	shared_data->flag = NOT_START;
}

static void
wait_resume_child(shared_data_t *shared_data)
{
	pthread_mutex_lock(&shared_data->mutex);
	while (shared_data->flag != SUSPENDED) {
		/* wait until task_suspend is called */
		pthread_cond_wait(&shared_data->cond, &shared_data->mutex);
	}
	shared_data->flag = RESUMED;
	OSMemoryBarrier();
	pthread_cond_signal(&shared_data->cond);
	pthread_mutex_unlock(&shared_data->mutex);
}

static void
mark_suspended(shared_data_t *shared_data)
{
	pthread_mutex_lock(&shared_data->mutex);
	T_QUIET; T_ASSERT_EQ(shared_data->flag, NOT_START, "task started already??");
	shared_data->flag = SUSPENDED;
	OSMemoryBarrier();
	pthread_cond_signal(&shared_data->cond);
	pthread_mutex_unlock(&shared_data->mutex);
}

static void
assert_resumed(shared_data_t *shared_data)
{
	pthread_mutex_lock(&shared_data->mutex);
	T_QUIET; T_ASSERT_NE(shared_data->flag, NOT_START, "task not started yet??");
	while (shared_data->flag == SUSPENDED) {
		/* wait until task is resumed */
		pthread_cond_wait(&shared_data->cond, &shared_data->mutex);
	}
	T_QUIET; T_ASSERT_EQ(shared_data->flag, RESUMED, "task not resumed??");
	pthread_mutex_unlock(&shared_data->mutex);
}

static integer_t
get_suspend_count(task_t task)
{
	kern_return_t kr;
	task_basic_info_data_t basic_info;
	mach_msg_type_number_t bi_count = TASK_BASIC_INFO_COUNT;

	kr = task_info(task, TASK_BASIC_INFO,
	    (task_info_t)&basic_info, &bi_count);
	T_QUIET; T_ASSERT_EQ(kr, KERN_SUCCESS, "task_info");

	return basic_info.suspend_count;
}

static task_t
get_task_for_pid(pid_t pid)
{
	kern_return_t kr;
	task_t task = TASK_NULL;

	kr = task_for_pid(mach_task_self(), pid, &task);
	T_ASSERT_MACH_SUCCESS(kr, "task_for_pid");
	T_ASSERT_NE(task, TASK_NULL, "task_for_pid task");

	return task;
}

T_DECL(task_suspend_and_resume,
    "calling legacy task_suspend and task_resume",
    T_META_TIMEOUT(15))
{
	kern_return_t kr;
	test_task_t target_task;
	shared_data_t *shared_data = NULL;
	pid_t child;

	/* Create shared data structure */
	shared_data = mmap(NULL, sizeof(shared_data_t), PROT_READ | PROT_WRITE,
	    MAP_SHARED | MAP_ANONYMOUS, -1, 0);
	T_ASSERT_NE(shared_data, MAP_FAILED, "mmap");

	init_process_shared_data(shared_data);

	child = fork();
	T_ASSERT_NE(child, -1, "fork");

	if (child == 0) {
		/* Child process */
		wait_resume_child(shared_data);
		exit(0);
	}

	/* Parent process */
	target_task.pid = child;
	kr = task_for_pid(mach_task_self(), child, &target_task.task);
	T_ASSERT_MACH_SUCCESS(kr, "task_for_pid");

	kr = task_suspend(target_task.task);
	T_ASSERT_EQ(kr, KERN_SUCCESS, "task_suspend");
	mark_suspended(shared_data);

	T_ASSERT_EQ(get_suspend_count(target_task.task), 1, "suspend_count == 1");

	kr = task_resume(target_task.task);
	T_ASSERT_EQ(kr, KERN_SUCCESS, "task_resume");

	assert_resumed(shared_data);

	wait(NULL);
	cleanup_shared_data(shared_data);
	mach_port_deallocate(mach_task_self(), target_task.task);
	munmap(shared_data, sizeof(shared_data_t));
}

T_DECL(task_suspend2_and_resume2,
    "calling task_suspend2 and task_resume2",
    T_META_TIMEOUT(15))
{
	kern_return_t kr;
	test_task_t target_task;
	task_suspension_token_t token = TASK_NULL;
	shared_data_t *shared_data = NULL;
	pid_t child;

	/* Create shared data structure */
	shared_data = mmap(NULL, sizeof(shared_data_t), PROT_READ | PROT_WRITE,
	    MAP_SHARED | MAP_ANONYMOUS, -1, 0);
	T_ASSERT_NE(shared_data, MAP_FAILED, "mmap");

	init_process_shared_data(shared_data);

	child = fork();
	T_ASSERT_NE(child, -1, "fork");

	if (child == 0) {
		/* Child process */
		wait_resume_child(shared_data);
		exit(0);
	}

	/* Parent process */
	target_task.pid = child;
	kr = task_for_pid(mach_task_self(), child, &target_task.task);
	T_ASSERT_MACH_SUCCESS(kr, "task_for_pid");

	kr = task_suspend2(target_task.task, &token);
	T_ASSERT_EQ(kr, KERN_SUCCESS, "task_suspend2");
	T_ASSERT_NE(token, TASK_NULL, "task_suspend2 token");
	mark_suspended(shared_data);

	T_ASSERT_EQ(get_suspend_count(target_task.task), 1, "suspend_count == 1");

	kr = task_resume2(token);
	T_ASSERT_EQ(kr, KERN_SUCCESS, "task_resume2");

	assert_resumed(shared_data);

	wait(NULL);
	cleanup_shared_data(shared_data);
	mach_port_deallocate(mach_task_self(), target_task.task);
	munmap(shared_data, sizeof(shared_data_t));
}

T_DECL(task_suspend2_auto_resume,
    "task resume after the suspending process exited using task_suspend2",
    T_META_TIMEOUT(15))
{
	pid_t child0 = 0, child1 = 0;
	task_t target_task;
	task_suspension_token_t token = TASK_NULL;
	kern_return_t kr;
	shared_data_t *shared_data = NULL;

	/* Create shared data structure */
	shared_data = mmap(NULL, sizeof(shared_data_t), PROT_READ | PROT_WRITE,
	    MAP_SHARED | MAP_ANONYMOUS, -1, 0);
	T_ASSERT_NE(shared_data, MAP_FAILED, "mmap");

	init_process_shared_data(shared_data);

	child0 = fork();
	T_ASSERT_NE(child0, -1, "fork child0");

	if (child0) {
		child1 = fork();
		T_ASSERT_NE(child1, -1, "fork child1");
	}

	/* This is child1 */
	if (child0 != 0 && child1 == 0) {
		/* suspend child0 */
		target_task = get_task_for_pid(child0);
		kr = task_suspend2(target_task, &token);
		T_ASSERT_EQ(kr, KERN_SUCCESS, "task_suspend2");
		T_ASSERT_NE(token, TASK_NULL, "task_suspend2 token");
		T_ASSERT_EQ(get_suspend_count(target_task), 1, "suspend_count == 1");

		/* Signal we've called task_suspend2() */
		mark_suspended(shared_data);

		/* exit without resuming task */
		T_LOG("suspender exiting");
		exit(0);
	}

	/* This is child 0 */
	if (child0 == 0 && child1 == 0) {
		/* Wait until we are unsuspended by child1's exit */
		wait_resume_child(shared_data);

		T_LOG("suspended task exiting");
		exit(0);
	}

	/* This is parent, wait for both children */
	wait(NULL);
	wait(NULL);

	assert_resumed(shared_data);

	/* Clean up */
	cleanup_shared_data(shared_data);
	munmap(shared_data, sizeof(shared_data_t));
}

static void
loop_child(void)
{
	while (1) {
		usleep(SLEEP_INTERVAL);
	}
}

T_DECL(task_resume_incorrect_count,
    "calling legacy task_resume twice",
    T_META_TIMEOUT(15))
{
	kern_return_t kr;
	test_task_t target_task;
	pid_t child;

	child = fork();
	T_ASSERT_NE(child, -1, "fork");

	if (child == 0) {
		loop_child();
	}

	target_task.pid = child;
	kr = task_for_pid(mach_task_self(), child, &target_task.task);
	T_ASSERT_MACH_SUCCESS(kr, "task_for_pid");

	kr = task_suspend(target_task.task);
	T_ASSERT_EQ(kr, KERN_SUCCESS, "task_suspend");

	kr = task_resume(target_task.task);
	T_ASSERT_EQ(kr, KERN_SUCCESS, "task_resume");

	kr = task_resume(target_task.task);
	T_ASSERT_EQ(kr, KERN_FAILURE, "calling task_resume twice");

	kill(child, SIGKILL);
	wait(NULL);
	mach_port_deallocate(mach_task_self(), target_task.task);
}

T_DECL(task_resume2_incorrect_count,
    "calling task_resume2 twice",
    T_META_TIMEOUT(15))
{
	kern_return_t kr;
	test_task_t target_task;
	task_suspension_token_t token = TASK_NULL;
	pid_t child;

	child = fork();
	T_ASSERT_NE(child, -1, "fork");

	if (child == 0) {
		loop_child();
	}

	target_task.pid = child;
	kr = task_for_pid(mach_task_self(), child, &target_task.task);
	T_ASSERT_MACH_SUCCESS(kr, "task_for_pid");

	kr = task_suspend2(target_task.task, &token);
	T_ASSERT_EQ(kr, KERN_SUCCESS, "task_suspend2");
	T_ASSERT_NE(token, TASK_NULL, "task_suspend2 token");

	kr = task_resume2(token);
	T_ASSERT_EQ(kr, KERN_SUCCESS, "task_resume2");

	kr = task_resume2(token);
	T_ASSERT_EQ(kr, MACH_SEND_INVALID_DEST, "calling task_resume2 twice");

	kill(child, SIGKILL);
	wait(NULL);
	mach_port_deallocate(mach_task_self(), target_task.task);
}

T_DECL(task_suspend2_multiple_token,
    "test calling task suspend2 multiple times",
    T_META_TIMEOUT(15))
{
	kern_return_t kr;
	test_task_t target_task;
	task_suspension_token_t token1 = TASK_NULL;
	task_suspension_token_t token2 = TASK_NULL;
	pid_t child;

	child = fork();
	T_ASSERT_NE(child, -1, "fork");

	if (child == 0) {
		loop_child();
	}

	target_task.pid = child;
	kr = task_for_pid(mach_task_self(), child, &target_task.task);
	T_ASSERT_MACH_SUCCESS(kr, "task_for_pid");

	kr = task_suspend2(target_task.task, &token1);
	T_ASSERT_EQ(kr, KERN_SUCCESS, "task_suspend2");
	T_ASSERT_NE(token1, TASK_NULL, "task_suspend2 token");
	T_ASSERT_EQ(get_suspend_count(target_task.task), 1, "suspend_count == 1");

	kr = task_suspend2(target_task.task, &token2);
	T_ASSERT_EQ(kr, KERN_SUCCESS, "task_suspend2");
	T_ASSERT_NE(token2, TASK_NULL, "task_suspend2 token");
	T_ASSERT_EQ(get_suspend_count(target_task.task), 2, "suspend_count == 2");

	kr = task_resume2(token1);
	T_ASSERT_EQ(kr, KERN_SUCCESS, "task_resume2");
	T_ASSERT_EQ(get_suspend_count(target_task.task), 1, "suspend_count == 1");

	kr = task_resume2(token2);
	T_ASSERT_EQ(kr, KERN_SUCCESS, "task_resume2");
	T_ASSERT_EQ(get_suspend_count(target_task.task), 0, "suspend_count == 0");

	kill(child, SIGKILL);
	wait(NULL);
	mach_port_deallocate(mach_task_self(), target_task.task);
}

T_DECL(task_suspend_error_conditions,
    "test error conditions with invalid parameters",
    T_META_TIMEOUT(15))
{
	kern_return_t kr;
	task_suspension_token_t token = TASK_NULL;
	task_t invalid_task = (task_t)0xdeadbeef;
	task_suspension_token_t invalid_token = (task_suspension_token_t)0xdeadbeef;

	/* Note: calling task_suspend2() with NULL token will seg fault */

	/* Test invalid task handle */
	kr = task_suspend(invalid_task);
	T_ASSERT_EQ(kr, MACH_SEND_INVALID_DEST, "task_suspend with invalid task");

	kr = task_resume(invalid_task);
	T_ASSERT_EQ(kr, MACH_SEND_INVALID_DEST, "task_resume with invalid task");

	kr = task_suspend2(invalid_task, &token);
	T_ASSERT_EQ(kr, MACH_SEND_INVALID_DEST, "task_suspend2 with invalid task");

	kr = task_resume2(invalid_token);
	T_ASSERT_EQ(kr, MACH_SEND_INVALID_DEST, "task_resume2 with invalid token");

	/* Test TASK_NULL */
	kr = task_suspend(TASK_NULL);
	T_ASSERT_EQ(kr, MACH_SEND_INVALID_DEST, "task_suspend with TASK_NULL");

	kr = task_resume(TASK_NULL);
	T_ASSERT_EQ(kr, MACH_SEND_INVALID_DEST, "task_resume with TASK_NULL");

	kr = task_suspend2(TASK_NULL, &token);
	T_ASSERT_EQ(kr, MACH_SEND_INVALID_DEST, "task_suspend2 with TASK_NULL");

	kr = task_resume2(TASK_NULL);
	T_ASSERT_EQ(kr, MACH_SEND_INVALID_DEST, "task_resume2 with TASK_NULL");
}

#define NUM_CONCURRENT_TASKS 5
typedef struct {
	task_t target_task;
	pthread_mutex_t *mutex;
	pthread_cond_t *cond;
	volatile int ready_count;
} concurrent_test_data_t;

static void *
concurrent_suspender(void *arg)
{
	kern_return_t kr;
	task_suspension_token_t token;
	concurrent_test_data_t *data = (concurrent_test_data_t *)arg;

	/* Signal we're ready and wait for all tasks */
	pthread_mutex_lock(data->mutex);
	data->ready_count++;
	while (data->ready_count < NUM_CONCURRENT_TASKS) {
		pthread_cond_wait(data->cond, data->mutex);
	}
	pthread_cond_broadcast(data->cond);
	pthread_mutex_unlock(data->mutex);

	/* All tasks suspend the target simultaneously */
	kr = task_suspend2(data->target_task, &token);
	T_QUIET; T_ASSERT_EQ(kr, KERN_SUCCESS, "concurrent task_suspend2");
	T_QUIET; T_ASSERT_NE(token, TASK_NULL, "concurrent token");

	/* Wait a bit to let all suspensions complete */
	usleep(SLEEP_INTERVAL);

	kr = task_resume2(token);
	T_QUIET; T_ASSERT_EQ(kr, KERN_SUCCESS, "concurrent task_resume2");

	return NULL;
}

T_DECL(task_suspend_concurrent,
    "test concurrent suspend/resume operations on same task",
    T_META_TIMEOUT(20))
{
	pthread_t suspender_tasks[NUM_CONCURRENT_TASKS];
	test_task_t target_task;
	concurrent_test_data_t data;
	pthread_mutex_t mutex;
	pthread_cond_t cond;
	int ret;
	pid_t child;

	/* Initialize synchronization */
	ret = pthread_mutex_init(&mutex, NULL);
	T_ASSERT_EQ(ret, 0, "pthread_mutex_init");
	ret = pthread_cond_init(&cond, NULL);
	T_ASSERT_EQ(ret, 0, "pthread_cond_init");

	/* Create target task */
	child = fork();
	T_ASSERT_NE(child, -1, "fork");

	if (child == 0) {
		loop_child();
	}

	target_task.pid = child;
	ret = task_for_pid(mach_task_self(), child, &target_task.task);
	T_ASSERT_MACH_SUCCESS(ret, "task_for_pid");

	/* Initialize test data */
	data.target_task = target_task.task;
	data.mutex = &mutex;
	data.cond = &cond;
	data.ready_count = 0;

	/* Create suspender tasks */
	for (int i = 0; i < NUM_CONCURRENT_TASKS; i++) {
		ret = pthread_create(&suspender_tasks[i], NULL, concurrent_suspender, &data);
		T_ASSERT_EQ(ret, 0, "pthread_create suspender");
	}

	/* Wait for all suspender tasks to complete */
	for (int i = 0; i < NUM_CONCURRENT_TASKS; i++) {
		pthread_join(suspender_tasks[i], NULL);
	}

	/* Verify target task is not suspended */
	T_ASSERT_EQ(get_suspend_count(target_task.task), 0, "final suspend_count == 0");

	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&cond);
	kill(child, SIGKILL);
	wait(NULL);
	mach_port_deallocate(mach_task_self(), target_task.task);
}

T_DECL(task_suspend_mixed_api_no_sender,
    "test no-sender notification doesn't affect legacy suspensions",
    T_META_TIMEOUT(20))
{
	pid_t child0 = 0, child1 = 0;
	task_t target_task;
	task_suspension_token_t token = TASK_NULL;
	kern_return_t kr;
	shared_data_t *shared_data = NULL;

	/* Create shared data structure */
	shared_data = mmap(NULL, sizeof(shared_data_t), PROT_READ | PROT_WRITE,
	    MAP_SHARED | MAP_ANONYMOUS, -1, 0);
	T_ASSERT_NE(shared_data, MAP_FAILED, "mmap");

	init_process_shared_data(shared_data);

	child0 = fork();
	T_ASSERT_NE(child0, -1, "fork child0");

	if (child0) {
		child1 = fork();
		T_ASSERT_NE(child1, -1, "fork child1");
	}

	/* This is child1 - uses task_suspend2 and exits */
	if (child0 != 0 && child1 == 0) {
		target_task = get_task_for_pid(child0);

		/* Suspend with task_suspend2 */
		kr = task_suspend2(target_task, &token);
		T_ASSERT_EQ(kr, KERN_SUCCESS, "task_suspend2");
		T_ASSERT_NE(token, TASK_NULL, "task_suspend2 token");

		/* Wait for parent to also suspend (parent will mark_suspended) */
		pthread_mutex_lock(&shared_data->mutex);
		while (shared_data->flag != SUSPENDED) {
			pthread_cond_wait(&shared_data->cond, &shared_data->mutex);
		}
		pthread_mutex_unlock(&shared_data->mutex);

		T_ASSERT_EQ(get_suspend_count(target_task), 2, "suspend_count == 2");

		/* Exit without resuming - should trigger no-sender notification */
		T_LOG("task_suspend2 process exiting");
		exit(0);
	}

	/* This is child0 - target process */
	if (child0 == 0 && child1 == 0) {
		/* Wait indefinitely - parent will check our state and resume us */
		while (1) {
			usleep(SLEEP_INTERVAL);
		}
	}

	/* This is parent */
	/* Now also suspend with legacy API */
	target_task = get_task_for_pid(child0);
	kr = task_suspend(target_task);
	T_ASSERT_EQ(kr, KERN_SUCCESS, "task_suspend legacy");

	/* Signal child1 that we've also suspended */
	mark_suspended(shared_data);

	/* Wait for child1 to exit - this should auto-resume the task_suspend2 suspension */
	wait(NULL);
	usleep(SLEEP_INTERVAL); /* Give time for no-sender notification to process */

	/* Verify that legacy suspension is still active despite no-sender notification */
	T_ASSERT_EQ(get_suspend_count(target_task), 1, "legacy suspension should remain");

	/* Resume the legacy suspension */
	kr = task_resume(target_task);
	T_ASSERT_EQ(kr, KERN_SUCCESS, "task_resume legacy");
	T_ASSERT_EQ(get_suspend_count(target_task), 0, "suspend_count == 0");

	/* Kill child0 since it's now running */
	kill(child0, SIGKILL);
	wait(NULL);

	/* Clean up */
	cleanup_shared_data(shared_data);
	munmap(shared_data, sizeof(shared_data_t));
}

#define TASK_EXITING_OR_EXITED(kr) \
	((kr) == KERN_INVALID_ARGUMENT || \
	 (kr) == MACH_SEND_INVALID_DEST || \
	 (kr) == MIG_SERVER_DIED)

T_DECL(task_suspend_sigkill_target,
    "test SIGKILL on suspended process",
    T_META_TIMEOUT(20))
{
	pid_t child = 0;
	task_t target_task;
	task_suspension_token_t token = TASK_NULL;
	kern_return_t kr;
	int status;
	shared_data_t *shared_data = NULL;

	/* Create shared data structure */
	shared_data = mmap(NULL, sizeof(shared_data_t), PROT_READ | PROT_WRITE,
	    MAP_SHARED | MAP_ANONYMOUS, -1, 0);
	T_ASSERT_NE(shared_data, MAP_FAILED, "mmap");

	init_process_shared_data(shared_data);

	child = fork();
	T_ASSERT_NE(child, -1, "fork child");

	/* This is child - target process */
	if (child == 0) {
		/* Signal that we're running */
		pthread_mutex_lock(&shared_data->mutex);
		shared_data->flag = RUNNING;
		pthread_cond_signal(&shared_data->cond);
		pthread_mutex_unlock(&shared_data->mutex);

		/* Main task loops indefinitely */
		while (1) {
			usleep(SLEEP_INTERVAL);
		}
	}

	/* This is parent */
	/* Wait for child to signal it's running */
	pthread_mutex_lock(&shared_data->mutex);
	while (shared_data->flag != RUNNING) {
		pthread_cond_wait(&shared_data->cond, &shared_data->mutex);
	}
	pthread_mutex_unlock(&shared_data->mutex);

	target_task = get_task_for_pid(child);

	/* Suspend the main task */
	kr = task_suspend2(target_task, &token);
	T_ASSERT_EQ(kr, KERN_SUCCESS, "task_suspend2");
	T_ASSERT_NE(token, TASK_NULL, "task_suspend2 token");
	T_ASSERT_EQ(get_suspend_count(target_task), 1, "suspend_count == 1");

	/* Kill the suspended process */
	kill(child, SIGKILL);

	/* Wait for child to die */
	pid_t waited = waitpid(child, &status, 0);
	T_ASSERT_EQ(waited, child, "waitpid");
	T_ASSERT_TRUE(WIFSIGNALED(status), "child was signaled");
	T_ASSERT_EQ(WTERMSIG(status), SIGKILL, "child killed by SIGKILL");

	/* Try to resume the dead process - should fail */
	kr = task_resume2(token);
	T_LOG("task_resume2 returned %d", kr);
	T_ASSERT_TRUE(TASK_EXITING_OR_EXITED(kr), "resume2 on dead process should fail");

	/* Clean up */
	cleanup_shared_data(shared_data);
	munmap(shared_data, sizeof(shared_data_t));
}

T_DECL(task_suspend_stale_handle,
    "test suspending tasks with stale handles after process death",
    T_META_TIMEOUT(15))
{
	int status;
	pid_t child = 0;
	kern_return_t kr;
	task_t target_task;
	task_suspension_token_t token = TASK_NULL;

	child = fork();
	T_ASSERT_NE(child, -1, "fork child");

	/* This is child - loop indefinitely until killed */
	if (child == 0) {
		while (1) {
			usleep(SLEEP_INTERVAL);
		}
	}

	/* Get task handle while child is alive */
	target_task = get_task_for_pid(child);
	T_ASSERT_NE(target_task, TASK_NULL, "get_task_for_pid");

	/* Kill the child process */
	kill(child, SIGKILL);
	pid_t waited = waitpid(child, &status, 0);
	T_ASSERT_EQ(waited, child, "waitpid");
	T_ASSERT_TRUE(WIFSIGNALED(status), "child was signaled");
	T_ASSERT_EQ(WTERMSIG(status), SIGKILL, "child killed by SIGKILL");

	/* Now try to suspend using the stale task handle */
	kr = task_suspend(target_task);
	T_LOG("task_suspend returned %d", kr);
	T_ASSERT_TRUE(TASK_EXITING_OR_EXITED(kr), "task_suspend on dead process should fail");

	kr = task_suspend2(target_task, &token);
	T_LOG("task_suspend2 returned %d", kr);
	T_ASSERT_TRUE(TASK_EXITING_OR_EXITED(kr), "task_suspend2 on dead process should fail");

	kr = task_resume(target_task);
	T_LOG("task_resume returned %d", kr);
	T_ASSERT_TRUE(TASK_EXITING_OR_EXITED(kr), "task_resume on dead process should fail");
}