This repository was archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathkevent_qos.c
More file actions
1767 lines (1417 loc) · 57.3 KB
/
Copy pathkevent_qos.c
File metadata and controls
1767 lines (1417 loc) · 57.3 KB
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* kevent_qos: Tests Synchronous IPC QOS override.
*/
#ifdef T_NAMESPACE
#undef T_NAMESPACE
#endif
#include <darwintest.h>
#include <darwintest_multiprocess.h>
#include <dispatch/dispatch.h>
#include <pthread.h>
#include <launch.h>
#include <mach/mach.h>
#include <mach/message.h>
#include <mach/mach_voucher.h>
#include <pthread/workqueue_private.h>
#include <voucher/ipc_pthread_priority_types.h>
#include <servers/bootstrap.h>
#include <stdlib.h>
#include <sys/event.h>
#include <unistd.h>
#include <crt_externs.h>
#include <mach/mach_port.h>
#include <mach/mach_sync_ipc.h>
T_GLOBAL_META(T_META_NAMESPACE("xnu.kevent_qos"));
#define ARRAYLEN(arr) (sizeof(arr) / sizeof(arr[0]))
#define INTERMITTENT_TIMEOUT_SEC (3)
#define RECV_TIMEOUT_SECS (4)
#define SEND_TIMEOUT_SECS (6)
#define HELPER_TIMEOUT_SECS (15)
#define ENV_VAR_QOS (3)
static const char *qos_env[ENV_VAR_QOS] = {"XNU_TEST_QOS_BO", "XNU_TEST_QOS_QO", "XNU_TEST_QOS_AO"};
static const char *qos_name_env[ENV_VAR_QOS] = {"XNU_TEST_QOS_NAME_BO", "XNU_TEST_QOS_NAME_QO", "XNU_TEST_QOS_NAME_AO"};
#define ENV_VAR_FUNCTION (1)
static const char *wl_function_name = "XNU_TEST_WL_FUNCTION";
static qos_class_t g_expected_qos[ENV_VAR_QOS];
static const char *g_expected_qos_name[ENV_VAR_QOS];
#define ENV_QOS_BEFORE_OVERRIDE (0)
#define ENV_QOS_QUEUE_OVERRIDE (1)
#define ENV_QOS_AFTER_OVERRIDE (2)
struct test_msg {
mach_msg_header_t header;
mach_msg_body_t body;
mach_msg_port_descriptor_t port_descriptor;
mach_msg_option_t opts;
mach_msg_priority_t qos;
};
#pragma mark pthread callbacks
static void
thread_create_at_qos(qos_class_t qos, void * (*function)(void *));
static void
send(mach_port_t send_port, mach_port_t reply_port, mach_port_t msg_port, mach_msg_priority_t qos, mach_msg_option_t options);
static void
enable_kevent(uint64_t *workloop_id, unsigned long long port);
static void
populate_kevent(struct kevent_qos_s *kev, unsigned long long port);
static void
worker_cb(pthread_priority_t __unused priority)
{
T_FAIL("a worker thread was created");
}
static void
event_cb(void ** __unused events, int * __unused nevents)
{
T_FAIL("a kevent routine was called instead of workloop");
}
static uint32_t
get_user_promotion_basepri(void)
{
mach_msg_type_number_t count = THREAD_POLICY_STATE_COUNT;
struct thread_policy_state thread_policy;
boolean_t get_default = FALSE;
mach_port_t thread_port = pthread_mach_thread_np(pthread_self());
kern_return_t kr = thread_policy_get(thread_port, THREAD_POLICY_STATE,
(thread_policy_t)&thread_policy, &count, &get_default);
T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_policy_get");
return thread_policy.thps_user_promotion_basepri;
}
#define EXPECT_QOS_EQ(qos, ...) do { \
if ((qos) == QOS_CLASS_USER_INTERACTIVE) { \
T_EXPECT_EFFECTIVE_QOS_EQ(QOS_CLASS_USER_INITIATED, __VA_ARGS__); \
T_EXPECT_EQ(47u, get_user_promotion_basepri(), __VA_ARGS__); \
} else { \
T_EXPECT_EFFECTIVE_QOS_EQ(qos, __VA_ARGS__); \
} \
} while (0)
#define EXPECT_TEST_MSG(_ke) do { \
struct kevent_qos_s *ke = _ke; \
mach_msg_header_t *hdr = (mach_msg_header_t *)ke->ext[0]; \
T_ASSERT_NOTNULL(hdr, "has a message"); \
T_ASSERT_EQ(hdr->msgh_size, (uint32_t)sizeof(struct test_msg), "of the right size"); \
struct test_msg *tmsg = (struct test_msg *)hdr; \
if (tmsg->opts & MACH_SEND_PROPAGATE_QOS) { \
T_EXPECT_EQ(tmsg->qos, ((uint32_t)(ke->ext[2] >> 32)), \
"propagation works"); \
} \
} while (0)
/*
* Basic WL handler callback, it sleeps for n seconds and then checks the
* effective Qos of the servicer thread.
*/
static void
workloop_cb_test_intransit(uint64_t *workloop_id __unused, void **eventslist, int *events)
{
T_LOG("Workloop handler workloop_cb_test_intransit called. "
"Will wait for %d seconds to make sure client enqueues the sync msg \n",
2 * RECV_TIMEOUT_SECS);
EXPECT_TEST_MSG(*eventslist);
/* Wait for the client to send the high priority message to override the qos */
sleep(2 * RECV_TIMEOUT_SECS);
/* Skip the test if we can't check Qos */
if (geteuid() != 0) {
T_SKIP("kevent_qos test requires root privileges to run.");
}
/* The effective Qos should be the one expected after override */
EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_AFTER_OVERRIDE],
"dispatch_source event handler QoS should be %s", g_expected_qos_name[ENV_QOS_AFTER_OVERRIDE]);
*events = 0;
T_END;
}
/*
* WL handler which checks if the servicer thread has correct Qos.
*/
static void
workloop_cb_test_sync_send(uint64_t *workloop_id __unused, void **eventslist, int *events)
{
T_LOG("Workloop handler workloop_cb_test_sync_send called");
EXPECT_TEST_MSG(*eventslist);
if (geteuid() != 0) {
T_SKIP("kevent_qos test requires root privileges to run.");
}
/* The effective Qos should be the one expected after override */
EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_AFTER_OVERRIDE],
"dispatch_source event handler QoS should be %s", g_expected_qos_name[ENV_QOS_AFTER_OVERRIDE]);
*events = 0;
T_END;
}
/*
* WL handler which checks the overridden Qos and then enables the knote and checks
* for the Qos again if that dropped the sync ipc override.
*/
static void
workloop_cb_test_sync_send_and_enable(uint64_t *workloop_id, struct kevent_qos_s **eventslist, int *events)
{
unsigned override_priority;
unsigned reenable_priority;
T_LOG("Workloop handler workloop_cb_test_sync_send_and_enable called");
EXPECT_TEST_MSG(*eventslist);
if (geteuid() != 0) {
T_SKIP("kevent_qos test requires root privileges to run.");
}
/* The effective Qos should be the one expected after override */
EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_AFTER_OVERRIDE],
"dispatch_source event handler QoS should be %s", g_expected_qos_name[ENV_QOS_AFTER_OVERRIDE]);
/* Snapshot the current override priority */
override_priority = get_user_promotion_basepri();
/* Enable the knote */
struct kevent_qos_s *kev = *eventslist;
enable_kevent(workloop_id, kev->ident);
/*
* Check if the override has been dropped, check for priority instead of qos since
* there will be async qos push.
*/
reenable_priority = get_user_promotion_basepri();
T_EXPECT_LT(reenable_priority, override_priority,
"thread's current override priority %d should be less than override priority prior to enabling knote %d",
reenable_priority, override_priority);
*events = 0;
T_END;
}
/*
* WL handler receives the first message and checks sync ipc override, then enables the knote
* and receives 2nd message and checks it sync ipc override.
*/
static int send_two_sync_handler_called = 0;
static void
workloop_cb_test_send_two_sync(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist, int *events)
{
T_LOG("Workloop handler workloop_cb_test_send_two_sync called for %d time", send_two_sync_handler_called + 1);
EXPECT_TEST_MSG(*eventslist);
if (geteuid() != 0) {
T_SKIP("kevent_qos test requires root privileges to run.");
}
T_LOG("Number of events received is %d\n", *events);
if (send_two_sync_handler_called == 0) {
/* The effective Qos should be the one expected after override */
EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_AFTER_OVERRIDE],
"dispatch_source event handler QoS should be %s", g_expected_qos_name[ENV_QOS_AFTER_OVERRIDE]);
/* Enable the knote to get 2nd message */
struct kevent_qos_s *kev = *eventslist;
uint64_t port = kev->ident;
populate_kevent(kev, port);
*events = 1;
} else {
EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_BEFORE_OVERRIDE],
"dispatch_source event handler QoS should be %s", g_expected_qos_name[ENV_QOS_BEFORE_OVERRIDE]);
*events = 0;
T_END;
}
send_two_sync_handler_called++;
}
/*
* Checks the sync ipc override and then waits for client to destroy the
* special reply port and checks if that removes the sync ipc override.
*/
static boolean_t two_send_and_destroy_test_passed = FALSE;
static int two_send_and_destroy_handler = 0;
static void
workloop_cb_test_two_send_and_destroy(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist __unused, int *events)
{
T_LOG("Workloop handler workloop_cb_test_two_send_and_destroy called %d times", two_send_and_destroy_handler + 1);
EXPECT_TEST_MSG(*eventslist);
if (geteuid() != 0) {
T_SKIP("kevent_qos test requires root privileges to run.");
}
if (two_send_and_destroy_handler == 0) {
/* Sleep to make sure the mqueue gets full */
sleep(RECV_TIMEOUT_SECS);
/* The effective Qos should be the one expected after override */
EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_AFTER_OVERRIDE],
"dispatch_source event handler QoS should be %s", g_expected_qos_name[ENV_QOS_AFTER_OVERRIDE]);
sleep(SEND_TIMEOUT_SECS);
/* Special reply port should have been destroyed, check Qos again */
EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_BEFORE_OVERRIDE],
"dispatch_source event handler QoS should be %s", g_expected_qos_name[ENV_QOS_BEFORE_OVERRIDE]);
two_send_and_destroy_test_passed = TRUE;
} else {
if (two_send_and_destroy_test_passed) {
T_END;
}
}
/* Enable the knote to get next message */
struct kevent_qos_s *kev = *eventslist;
uint64_t port = kev->ident;
populate_kevent(kev, port);
*events = 1;
two_send_and_destroy_handler++;
T_LOG("Handler returning \n");
}
static mach_port_type_t
get_reply_port(struct kevent_qos_s *kev)
{
mach_msg_header_t *hdr;
mach_port_t reply_port;
mach_port_type_t type;
kern_return_t kr;
hdr = (void*)kev->ext[0];
T_QUIET; T_ASSERT_NOTNULL(hdr, "msg hdr");
reply_port = hdr->msgh_remote_port;
T_QUIET;T_ASSERT_TRUE(MACH_PORT_VALID(reply_port), "reply port valid");
kr = mach_port_type(mach_task_self(), reply_port, &type);
T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_type");
T_QUIET; T_ASSERT_TRUE(type & MACH_PORT_TYPE_SEND_ONCE, "send once received");
return reply_port;
}
static void
send_reply(mach_port_t reply_port)
{
kern_return_t kr;
struct {
mach_msg_header_t header;
} send_msg = {
.header = {
.msgh_remote_port = reply_port,
.msgh_local_port = MACH_PORT_NULL,
.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE, 0),
.msgh_id = 0x100,
.msgh_size = sizeof(send_msg),
},
};
kr = mach_msg(&(send_msg.header),
MACH_SEND_MSG,
send_msg.header.msgh_size,
0,
MACH_PORT_NULL,
0,
0);
T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "server mach_msg");
}
static void
populate_kevent(struct kevent_qos_s *kev, unsigned long long port)
{
memset(kev, 0, sizeof(struct kevent_qos_s));
kev->ident = port;
kev->filter = EVFILT_MACHPORT;
kev->flags = EV_ADD | EV_ENABLE | EV_UDATA_SPECIFIC | EV_DISPATCH | EV_VANISHED;
kev->fflags = (MACH_RCV_MSG | MACH_RCV_LARGE | MACH_RCV_LARGE_IDENTITY |
MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_CTX) |
MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0));
kev->data = 1;
}
static void
enable_kevent(uint64_t *workloop_id, unsigned long long port)
{
kern_return_t kr;
struct kevent_qos_s kev;
populate_kevent(&kev, port);
struct kevent_qos_s kev_err[] = {{ 0 }};
kr = kevent_id(*workloop_id, &kev, 1, kev_err, 1, NULL,
NULL, KEVENT_FLAG_WORKLOOP | KEVENT_FLAG_ERROR_EVENTS | KEVENT_FLAG_DYNAMIC_KQ_MUST_EXIST);
T_QUIET; T_ASSERT_POSIX_SUCCESS(kr, "kevent_id");
}
/*
* WL handler which sends a msg to the client from handler.
*/
static void
workloop_cb_test_sync_send_reply(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist, int *events)
{
T_LOG("Workloop handler workloop_cb_test_sync_send_reply called");
if (geteuid() != 0) {
T_SKIP("kevent_qos test requires root privileges to run.");
}
T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
/* send reply */
send_reply(get_reply_port(*eventslist));
*events = 0;
}
/*
* WL handler which deallocates reply port.
*/
static void
workloop_cb_test_sync_send_deallocate(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist, int *events)
{
mach_port_t reply_port;
kern_return_t kr;
T_LOG("Workloop handler workloop_cb_test_sync_send_deallocate called");
if (geteuid() != 0) {
T_SKIP("kevent_qos test requires root privileges to run.");
}
T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
reply_port = get_reply_port(*eventslist);
/* deallocate port */
kr = mach_port_deallocate(mach_task_self(), reply_port);
T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_deallocate");
*events = 0;
T_LOG("Handler returning \n");
}
/*
* WL handler which sends a msg to the client before enabling the event from handler.
*/
static void
workloop_cb_test_sync_send_reply_kevent(uint64_t *workloop_id, struct kevent_qos_s **eventslist, int *events)
{
T_LOG("Workloop handler workloop_cb_test_sync_send_reply_kevent called");
if (geteuid() != 0) {
T_SKIP("kevent_qos test requires root privileges to run.");
}
T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
T_QUIET; T_ASSERT_EQ_INT(((*eventslist)->filter), EVFILT_MACHPORT, "received EVFILT_MACHPORT");
struct kevent_qos_s *kev = *eventslist;
/* send reply */
send_reply(get_reply_port(kev));
/* Enable the knote */
enable_kevent(workloop_id, kev->ident);
*events = 0;
T_LOG("Handler returning \n");
}
/*
* WL handler which sends a msg to the client before enabling the event from pthread.
*/
static void
workloop_cb_test_sync_send_reply_kevent_pthread(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist, int *events)
{
T_LOG("Workloop handler workloop_cb_test_sync_send_reply_kevent_pthread called");
if (geteuid() != 0) {
T_SKIP("kevent_qos test requires root privileges to run.");
}
T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
struct kevent_qos_s *kev = *eventslist;
/* send reply */
send_reply(get_reply_port(kev));
populate_kevent(kev, kev->ident);
*events = 1;
T_LOG("Handler returning \n");
}
/*
* WL handler which sends a msg to the client after reenabling the event.
*/
static void
workloop_cb_test_sync_send_kevent_reply(uint64_t *workloop_id, struct kevent_qos_s **eventslist, int *events)
{
T_LOG("workloop handler workloop_cb_test_sync_send_kevent_reply called");
if (geteuid() != 0) {
T_SKIP("kevent_qos test requires root privileges to run.");
}
T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
struct kevent_qos_s *kev = *eventslist;
mach_port_t reply_port = get_reply_port(*eventslist);
/* Enable the knote */
enable_kevent(workloop_id, kev->ident);
/* send reply */
send_reply(reply_port);
*events = 0;
T_LOG("Handler returning \n");
}
/*
* WL handler that does nothing.
*/
static void
workloop_cb_test_sync_send_do_nothing(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist, int *events)
{
T_LOG("Workloop handler workloop_cb_test_sync_send_do_nothing called");
if (geteuid() != 0) {
T_SKIP("kevent_qos test requires root privileges to run.");
}
T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
/* do nothing */
*events = 0;
T_LOG("Handler returning \n");
}
/*
* WL handler that returns the event to reenable.
*/
static void
workloop_cb_test_sync_send_do_nothing_kevent_pthread(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist, int *events)
{
T_LOG("Workloop handler workloop_cb_test_sync_send_do_nothing_kevent_pthread called");
if (geteuid() != 0) {
T_SKIP("kevent_qos test requires root privileges to run.");
}
T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
struct kevent_qos_s *kev = *eventslist;
populate_kevent(kev, kev->ident);
*events = 1;
T_LOG("handler returning \n");
}
/*
* WL handler that exits.
*/
static void
workloop_cb_test_sync_send_do_nothing_exit(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist, __unused int *events)
{
T_LOG("workloop handler workloop_cb_test_sync_send_do_nothing_exit called");
if (geteuid() != 0) {
T_SKIP("kevent_qos test requires root privileges to run.");
}
T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
/* call exit */
exit(0);
}
/*
* WL handler which:
* first sync sends a msg to the client and reenables kevent after
* second sync sends a msg and reenables kevent after.
*/
static void
workloop_cb_test_sync_send_reply_kevent_reply_kevent(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist, int *events)
{
T_LOG("Workloop handler workloop_cb_test_sync_send_reply_kevent_reply_kevent called");
if (geteuid() != 0) {
T_SKIP("kevent_qos test requires root privileges to run.");
}
T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
struct kevent_qos_s *kev = *eventslist;
/* send reply */
send_reply(get_reply_port(kev));
populate_kevent(kev, kev->ident);
*events = 1;
T_LOG("Handler returning \n");
}
/*
* WL handler which:
* first sync reenables kevent and after sends a msg
* second sync sends a msg and reenables kevent after.
*/
static int workloop_cb_test_sync_send_kevent_reply_reply_kevent_handler_called = 0;
static void
workloop_cb_test_sync_send_kevent_reply_reply_kevent(uint64_t *workloop_id, struct kevent_qos_s **eventslist, int *events)
{
T_LOG("workloop handler workloop_cb_test_sync_send_kevent_reply_reply_kevent called");
if (geteuid() != 0) {
T_SKIP("kevent_qos test requires root privileges to run.");
}
T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
struct kevent_qos_s *kev = *eventslist;
mach_port_t reply_port = get_reply_port(kev);
if (workloop_cb_test_sync_send_kevent_reply_reply_kevent_handler_called == 0) {
workloop_cb_test_sync_send_kevent_reply_reply_kevent_handler_called = 1;
/* Enable the knote */
enable_kevent(workloop_id, kev->ident);
/* send reply */
send_reply(reply_port);
*events = 0;
} else {
/* send reply */
send_reply(reply_port);
/* Enable the knote */
enable_kevent(workloop_id, kev->ident);
*events = 0;
}
T_LOG("Handler returning \n");
}
/*
* WL handler which:
* first sync reenables kevent and after sends a msg
* second sync reenables kevent and after sends a msg
*/
static void
workloop_cb_test_sync_send_kevent_reply_kevent_reply(uint64_t *workloop_id, struct kevent_qos_s **eventslist, int *events)
{
T_LOG("workloop handler workloop_cb_test_sync_send_kevent_reply_kevent_reply called");
if (geteuid() != 0) {
T_SKIP("kevent_qos test requires root privileges to run.");
}
T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
struct kevent_qos_s *kev = *eventslist;
mach_port_t reply_port = get_reply_port(kev);
/* Enable the knote */
enable_kevent(workloop_id, kev->ident);
/* send reply */
send_reply(reply_port);
*events = 0;
T_LOG("Handler returning \n");
}
/*
* WL handler which:
* first sync ends a msg and reenables kevent after
* second sync reenables kevent and sends a msg after
*/
static int workloop_cb_test_sync_send_reply_kevent_kevent_reply_handler_called = 0;
static void
workloop_cb_test_sync_send_reply_kevent_kevent_reply(uint64_t *workloop_id, struct kevent_qos_s **eventslist, int *events)
{
T_LOG("workloop handler workloop_cb_test_sync_send_reply_kevent_kevent_reply called");
if (geteuid() != 0) {
T_SKIP("kevent_qos test requires root privileges to run.");
}
T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
struct kevent_qos_s *kev = *eventslist;
mach_port_t reply_port = get_reply_port(kev);
if (workloop_cb_test_sync_send_reply_kevent_kevent_reply_handler_called == 0) {
workloop_cb_test_sync_send_reply_kevent_kevent_reply_handler_called = 1;
/* send reply */
send_reply(reply_port);
populate_kevent(kev, kev->ident);
*events = 1;
} else {
/* Enable the knote */
enable_kevent(workloop_id, kev->ident);
/* send reply */
send_reply(reply_port);
*events = 0;
}
T_LOG("Handler returning \n");
}
#pragma mark Mach receive
#define KEVENT_QOS_SERVICE_NAME "com.apple.xnu.test.kevent_qos"
static mach_port_t
get_server_port(void)
{
mach_port_t port;
kern_return_t kr = bootstrap_check_in(bootstrap_port,
KEVENT_QOS_SERVICE_NAME, &port);
T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "server bootstrap_check_in");
return port;
}
static void
env_set_qos(char **env, qos_class_t qos[], const char *qos_name[], const char *wl_function)
{
int i;
char *qos_str, *qos_name_str;
for (i = 0; i < ENV_VAR_QOS; i++) {
T_QUIET; T_ASSERT_POSIX_SUCCESS(asprintf(&qos_str, "%s=%d", qos_env[i] , qos[i]),
NULL);
T_QUIET; T_ASSERT_POSIX_SUCCESS(
asprintf(&qos_name_str, "%s=%s", qos_name_env[i], qos_name[i]), NULL);
env[2 * i] = qos_str;
env[2 * i + 1] = qos_name_str;
}
T_QUIET; T_ASSERT_POSIX_SUCCESS(asprintf(&env[2 * i], "%s=%s", wl_function_name, wl_function),
NULL);
env[2 * i + 1] = NULL;
}
static void
environ_get_qos(qos_class_t qos[], const char *qos_name[], const char **wl_function)
{
char *qos_str;
char *qos_end;
int i;
for (i = 0; i < ENV_VAR_QOS; i++) {
qos_str = getenv(qos_env[i]);
T_QUIET; T_ASSERT_NOTNULL(qos_str, "getenv(%s)", qos_env[i]);
unsigned long qos_l = strtoul(qos_str, &qos_end, 10);
T_QUIET; T_ASSERT_EQ(*qos_end, '\0', "getenv(%s) = '%s' should be an "
"integer", qos_env[i], qos_str);
T_QUIET; T_ASSERT_LT(qos_l, (unsigned long)100, "getenv(%s) = '%s' should "
"be less than 100", qos_env[i], qos_str);
qos[i] = (qos_class_t)qos_l;
qos_name[i] = getenv(qos_name_env[i]);
T_QUIET; T_ASSERT_NOTNULL(qos_name[i], "getenv(%s)", qos_name_env[i]);
}
*wl_function = getenv(wl_function_name);
T_QUIET; T_ASSERT_NOTNULL(*wl_function, "getenv(%s)", wl_function_name);
}
static mach_voucher_t
create_pthpriority_voucher(mach_msg_priority_t qos)
{
char voucher_buf[sizeof(mach_voucher_attr_recipe_data_t) + sizeof(ipc_pthread_priority_value_t)];
mach_voucher_t voucher = MACH_PORT_NULL;
kern_return_t ret;
ipc_pthread_priority_value_t ipc_pthread_priority_value =
(ipc_pthread_priority_value_t)qos;
mach_voucher_attr_raw_recipe_array_t recipes;
mach_voucher_attr_raw_recipe_size_t recipe_size = 0;
mach_voucher_attr_recipe_t recipe =
(mach_voucher_attr_recipe_t)&voucher_buf[recipe_size];
recipe->key = MACH_VOUCHER_ATTR_KEY_PTHPRIORITY;
recipe->command = MACH_VOUCHER_ATTR_PTHPRIORITY_CREATE;
recipe->previous_voucher = MACH_VOUCHER_NULL;
memcpy((char *)&recipe->content[0], &ipc_pthread_priority_value, sizeof(ipc_pthread_priority_value));
recipe->content_size = sizeof(ipc_pthread_priority_value_t);
recipe_size += sizeof(mach_voucher_attr_recipe_data_t) + recipe->content_size;
recipes = (mach_voucher_attr_raw_recipe_array_t)&voucher_buf[0];
ret = host_create_mach_voucher(mach_host_self(),
recipes,
recipe_size,
&voucher);
T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "client host_create_mach_voucher");
return voucher;
}
static void
send(
mach_port_t send_port,
mach_port_t reply_port,
mach_port_t msg_port,
mach_msg_priority_t qos,
mach_msg_option_t options)
{
kern_return_t ret = 0;
struct test_msg send_msg = {
.header = {
.msgh_remote_port = send_port,
.msgh_local_port = reply_port,
.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND,
reply_port ? MACH_MSG_TYPE_MAKE_SEND_ONCE : 0,
MACH_MSG_TYPE_MOVE_SEND,
MACH_MSGH_BITS_COMPLEX),
.msgh_id = 0x100,
.msgh_size = sizeof(send_msg),
},
.body = {
.msgh_descriptor_count = 1,
},
.port_descriptor = {
.name = msg_port,
.disposition = MACH_MSG_TYPE_MOVE_RECEIVE,
.type = MACH_MSG_PORT_DESCRIPTOR,
},
.opts = options,
};
if (msg_port == MACH_PORT_NULL) {
send_msg.body.msgh_descriptor_count = 0;
}
if ((options & MACH_SEND_PROPAGATE_QOS) == 0) {
send_msg.header.msgh_voucher_port = create_pthpriority_voucher(qos);
send_msg.qos = qos;
} else {
qos_class_t qc;
int relpri;
pthread_get_qos_class_np(pthread_self(), &qc, &relpri);
send_msg.qos = (uint32_t)_pthread_qos_class_encode(qc, relpri, 0);
}
ret = mach_msg(&(send_msg.header),
MACH_SEND_MSG |
MACH_SEND_TIMEOUT |
MACH_SEND_OVERRIDE|
((reply_port ? MACH_SEND_SYNC_OVERRIDE : 0) | options),
send_msg.header.msgh_size,
0,
MACH_PORT_NULL,
10000,
0);
T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "client mach_msg");
}
static kern_return_t
receive(
mach_port_t rcv_port,
mach_port_t notify_port)
{
kern_return_t ret = 0;
struct test_msg rcv_msg = {
.header = {
.msgh_remote_port = MACH_PORT_NULL,
.msgh_local_port = rcv_port,
.msgh_size = sizeof(rcv_msg),
},
};
T_LOG("Client: Starting sync receive\n");
ret = mach_msg(&(rcv_msg.header),
MACH_RCV_MSG |
MACH_RCV_TIMEOUT |
MACH_RCV_SYNC_WAIT,
0,
rcv_msg.header.msgh_size,
rcv_port,
SEND_TIMEOUT_SECS * 1000,
notify_port);
return ret;
}
T_HELPER_DECL(qos_get_special_reply_port,
"Test get_special_reply_port and it's corner cases.")
{
mach_port_t special_reply_port;
mach_port_t new_special_reply_port;
special_reply_port = thread_get_special_reply_port();
T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port), "get_thread_special_reply_port");
new_special_reply_port = thread_get_special_reply_port();
T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(new_special_reply_port), "get_thread_special_reply_port");
mach_port_destroy(mach_task_self(), special_reply_port);
mach_port_destroy(mach_task_self(), new_special_reply_port);
new_special_reply_port = thread_get_special_reply_port();
T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(new_special_reply_port), "get_thread_special_reply_port");
T_END;
}
static void *
qos_client_send_to_intransit(void *arg __unused)
{
mach_port_t qos_send_port;
mach_port_t msg_port;
mach_port_t special_reply_port;
kern_return_t kr = bootstrap_look_up(bootstrap_port,
KEVENT_QOS_SERVICE_NAME, &qos_send_port);
T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client bootstrap_look_up");
special_reply_port = thread_get_special_reply_port();
T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port), "get_thread_special_reply_port");
/* Create a rcv right to send in a msg */
kr = mach_port_allocate(mach_task_self(),
MACH_PORT_RIGHT_RECEIVE,
&msg_port);
T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client mach_port_allocate");
kr = mach_port_insert_right(mach_task_self(),
msg_port,
msg_port,
MACH_MSG_TYPE_MAKE_SEND);
T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client mach_port_insert_right");
/* Send an empty msg on the port to fire the WL thread */
send(qos_send_port, MACH_PORT_NULL, MACH_PORT_NULL,
(uint32_t)_pthread_qos_class_encode(g_expected_qos[ENV_QOS_BEFORE_OVERRIDE], 0, 0), 0);
/* Sleep 3 seconds for the server to start */
sleep(3);
/* Send the message with msg port as in-transit port, this msg will not be dequeued */
send(qos_send_port, MACH_PORT_NULL, msg_port,
(uint32_t)_pthread_qos_class_encode(g_expected_qos[ENV_QOS_BEFORE_OVERRIDE], 0, 0), 0);
/* Send 5 messages to msg port to make sure the port is full */
for (int i = 0; i < 5; i++) {
send(msg_port, MACH_PORT_NULL, MACH_PORT_NULL,
(uint32_t)_pthread_qos_class_encode(g_expected_qos[ENV_QOS_BEFORE_OVERRIDE], 0, 0), 0);
}
T_LOG("Sent 5 msgs, now trying to send sync ipc messgae, which will block with a timeout\n");
/* Send the message to the in-transit port, it should block and override the rcv's workloop */
send(msg_port, special_reply_port, MACH_PORT_NULL,
(uint32_t)_pthread_qos_class_encode(g_expected_qos[ENV_QOS_AFTER_OVERRIDE], 0, 0), 0);
T_LOG("Client done sending messages, now waiting for server to end the test");
T_ASSERT_FAIL("client timed out");
return NULL;
}
T_HELPER_DECL(qos_client_send_to_intransit_with_thr_pri,
"Send synchronous messages from a pri thread to an intransit port")
{
thread_create_at_qos(g_expected_qos[ENV_QOS_AFTER_OVERRIDE], qos_client_send_to_intransit);
sleep(HELPER_TIMEOUT_SECS);
}
static void
thread_create_at_qos(qos_class_t qos, void * (*function)(void *))
{
qos_class_t qos_thread;
pthread_t thread;
pthread_attr_t attr;
int ret;
ret = setpriority(PRIO_DARWIN_ROLE, 0, PRIO_DARWIN_ROLE_UI_FOCAL);
if (ret != 0) {
T_LOG("set priority failed\n");