-
Notifications
You must be signed in to change notification settings - Fork 468
/
Copy pathmach.c
3250 lines (2959 loc) · 104 KB
/
mach.c
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
/*
* Copyright (c) 2008-2016 Apple Inc. All rights reserved.
*
* @APPLE_APACHE_LICENSE_HEADER_START@
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @APPLE_APACHE_LICENSE_HEADER_END@
*/
#include "internal.h"
#if HAVE_MACH
#define DISPATCH_MACH_RETURN_IMMEDIATE_SEND_RESULT 0x1
#define DISPATCH_MACH_REGISTER_FOR_REPLY 0x2
#define DISPATCH_MACH_WAIT_FOR_REPLY 0x4
#define DISPATCH_MACH_OPTIONS_MASK 0xffff
#define DM_SEND_STATUS_SUCCESS 0x1
#define DM_SEND_STATUS_RETURNING_IMMEDIATE_SEND_RESULT 0x2
#define DM_CHECKIN_CANCELED ((dispatch_mach_msg_t)~0ul)
DISPATCH_ENUM(dispatch_mach_send_invoke_flags, uint32_t,
DM_SEND_INVOKE_NONE = 0x0,
DM_SEND_INVOKE_MAKE_DIRTY = 0x1,
DM_SEND_INVOKE_NEEDS_BARRIER = 0x2,
DM_SEND_INVOKE_CAN_RUN_BARRIER = 0x4,
DM_SEND_INVOKE_IMMEDIATE_SEND = 0x8,
);
#define DM_SEND_INVOKE_IMMEDIATE_SEND_MASK \
((dispatch_mach_send_invoke_flags_t)DM_SEND_INVOKE_IMMEDIATE_SEND)
static inline mach_msg_option_t _dispatch_mach_checkin_options(void);
static mach_port_t _dispatch_mach_msg_get_remote_port(dispatch_object_t dou);
static mach_port_t _dispatch_mach_msg_get_reply_port(dispatch_object_t dou);
static void _dispatch_mach_msg_disconnected(dispatch_mach_t dm,
mach_port_t local_port, mach_port_t remote_port);
static inline void _dispatch_mach_msg_reply_received(dispatch_mach_t dm,
dispatch_mach_reply_wait_refs_t dwr, mach_port_t local_port);
static dispatch_mach_msg_t _dispatch_mach_msg_create_reply_disconnected(
dispatch_object_t dou, dispatch_mach_reply_refs_t dmr,
dispatch_mach_reason_t reason);
static bool _dispatch_mach_reconnect_invoke(dispatch_mach_t dm,
dispatch_object_t dou);
static inline mach_msg_header_t* _dispatch_mach_msg_get_msg(
dispatch_mach_msg_t dmsg);
static void _dispatch_mach_send_push(dispatch_mach_t dm, dispatch_object_t dou,
dispatch_qos_t qos);
static void _dispatch_mach_push_send_barrier_drain(dispatch_mach_t dm,
dispatch_qos_t qos);
static void _dispatch_mach_handle_or_push_received_msg(dispatch_mach_t dm,
dispatch_mach_msg_t dmsg, pthread_priority_t pp);
static void _dispatch_mach_push_async_reply_msg(dispatch_mach_t dm,
dispatch_mach_msg_t dmsg, dispatch_queue_t drq);
static dispatch_queue_t _dispatch_mach_msg_context_async_reply_queue(
void *ctxt);
static dispatch_continuation_t _dispatch_mach_msg_async_reply_wrap(
dispatch_mach_msg_t dmsg, dispatch_mach_t dm);
static void _dispatch_mach_notification_kevent_unregister(dispatch_mach_t dm);
static void _dispatch_mach_notification_kevent_register(dispatch_mach_t dm,
mach_port_t send);
// For tests only.
DISPATCH_EXPORT void _dispatch_mach_hooks_install_default(void);
#pragma mark -
#pragma mark dispatch to XPC callbacks
void
dispatch_mach_hooks_install_4libxpc(dispatch_mach_xpc_hooks_t hooks)
{
if (hooks->version < DISPATCH_MACH_XPC_MIN_HOOKS_VERSION) {
DISPATCH_CLIENT_CRASH(hooks,
"trying to install hooks with unsupported version");
}
if (!os_atomic_cmpxchg(&_dispatch_mach_xpc_hooks,
&_dispatch_mach_xpc_hooks_default, hooks, relaxed)) {
DISPATCH_CLIENT_CRASH(_dispatch_mach_xpc_hooks,
"dispatch_mach_hooks_install_4libxpc called twice");
}
}
void
_dispatch_mach_hooks_install_default(void)
{
os_atomic_store(&_dispatch_mach_xpc_hooks,
&_dispatch_mach_xpc_hooks_default, relaxed);
}
#pragma mark -
#pragma mark dispatch_mach_t
static dispatch_mach_t
_dispatch_mach_create(const char *label, dispatch_queue_t q, void *context,
dispatch_mach_handler_function_t handler, bool handler_is_block,
bool is_xpc)
{
dispatch_mach_recv_refs_t dmrr;
dispatch_mach_send_refs_t dmsr;
dispatch_mach_t dm;
dm = _dispatch_queue_alloc(mach, DQF_MUTABLE, 1,
DISPATCH_QUEUE_INACTIVE | DISPATCH_QUEUE_ROLE_INNER)._dm;
dm->dq_label = label;
dm->dm_is_xpc = is_xpc;
dmrr = dux_create(&_dispatch_mach_type_recv, 0, 0)._dmrr;
dispatch_assert(dmrr->du_is_direct);
dmrr->du_owner_wref = _dispatch_ptr2wref(dm);
dmrr->dmrr_handler_func = handler;
dmrr->dmrr_handler_ctxt = context;
dmrr->dmrr_handler_is_block = handler_is_block;
dm->dm_recv_refs = dmrr;
dmsr = dux_create(&_dispatch_mach_type_send, 0,
DISPATCH_MACH_SEND_POSSIBLE|DISPATCH_MACH_SEND_DEAD)._dmsr;
dmsr->du_owner_wref = _dispatch_ptr2wref(dm);
dm->dm_send_refs = dmsr;
if (unlikely(!q)) {
q = _dispatch_get_default_queue(true);
} else {
_dispatch_retain(q);
}
dm->do_targetq = q;
_dispatch_object_debug(dm, "%s", __func__);
return dm;
}
dispatch_mach_t
dispatch_mach_create(const char *label, dispatch_queue_t q,
dispatch_mach_handler_t handler)
{
dispatch_block_t bb = _dispatch_Block_copy((void*)handler);
return _dispatch_mach_create(label, q, bb,
(dispatch_mach_handler_function_t)_dispatch_Block_invoke(bb), true,
false);
}
dispatch_mach_t
dispatch_mach_create_f(const char *label, dispatch_queue_t q, void *context,
dispatch_mach_handler_function_t handler)
{
return _dispatch_mach_create(label, q, context, handler, false, false);
}
dispatch_mach_t
dispatch_mach_create_4libxpc(const char *label, dispatch_queue_t q,
void *context, dispatch_mach_handler_function_t handler)
{
return _dispatch_mach_create(label, q, context, handler, false, true);
}
void
_dispatch_mach_dispose(dispatch_mach_t dm, bool *allow_free)
{
_dispatch_object_debug(dm, "%s", __func__);
_dispatch_unote_dispose(dm->dm_recv_refs);
dm->dm_recv_refs = NULL;
_dispatch_unote_dispose(dm->dm_send_refs);
dm->dm_send_refs = NULL;
if (dm->dm_xpc_term_refs) {
_dispatch_unote_dispose(dm->dm_xpc_term_refs);
dm->dm_xpc_term_refs = NULL;
}
_dispatch_lane_class_dispose(dm, allow_free);
}
void
dispatch_mach_connect(dispatch_mach_t dm, mach_port_t receive,
mach_port_t send, dispatch_mach_msg_t checkin)
{
dispatch_mach_send_refs_t dmsr = dm->dm_send_refs;
if (MACH_PORT_VALID(receive)) {
dm->dm_recv_refs->du_ident = receive;
}
dmsr->dmsr_send = send;
if (MACH_PORT_VALID(send)) {
if (checkin) {
dispatch_mach_msg_t dmsg = checkin;
dispatch_retain(dmsg);
dmsg->dmsg_options = _dispatch_mach_checkin_options();
dmsr->dmsr_checkin_port = _dispatch_mach_msg_get_remote_port(dmsg);
}
dmsr->dmsr_checkin = checkin;
}
uint32_t disconnect_cnt = os_atomic_and_orig2o(dmsr, dmsr_disconnect_cnt,
~DISPATCH_MACH_NEVER_CONNECTED, relaxed);
if (unlikely(!(disconnect_cnt & DISPATCH_MACH_NEVER_CONNECTED))) {
DISPATCH_CLIENT_CRASH(disconnect_cnt, "Channel already connected");
}
_dispatch_object_debug(dm, "%s", __func__);
return dispatch_activate(dm);
}
static inline void
_dispatch_mach_reply_list_insert(dispatch_mach_send_refs_t dmsr,
dispatch_mach_reply_refs_t dmr)
{
_dispatch_unfair_lock_lock(&dmsr->dmsr_replies_lock);
dispatch_assert(!_LIST_IS_ENQUEUED(dmr, dmr_list));
LIST_INSERT_HEAD(&dmsr->dmsr_replies, dmr, dmr_list);
_dispatch_unfair_lock_unlock(&dmsr->dmsr_replies_lock);
}
static inline void
_dispatch_mach_reply_list_remove_locked(dispatch_mach_reply_refs_t dmr)
{
dispatch_assert(_LIST_IS_ENQUEUED(dmr, dmr_list));
LIST_REMOVE(dmr, dmr_list);
_LIST_MARK_NOT_ENQUEUED(dmr, dmr_list);
}
static inline bool
_dispatch_mach_reply_list_tryremove(dispatch_mach_send_refs_t dmsr,
dispatch_mach_reply_refs_t dmr)
{
bool removed;
_dispatch_unfair_lock_lock(&dmsr->dmsr_replies_lock);
if ((removed = _LIST_IS_ENQUEUED(dmr, dmr_list))) {
_dispatch_mach_reply_list_remove_locked(dmr);
}
_dispatch_unfair_lock_unlock(&dmsr->dmsr_replies_lock);
return removed;
}
#define DMRU_DELETE_ACK DUU_DELETE_ACK
#define DMRU_PROBE DUU_PROBE
#define DMRU_MUST_SUCCEED DUU_MUST_SUCCEED
#define DMRU_DUU_MASK 0x0f
#define DMRU_DISCONNECTED 0x10
#define DMRU_REMOVE 0x20
#define DMRU_ASYNC_MERGE 0x40
#define DMRU_CANCEL 0x80
DISPATCH_NOINLINE
static void
_dispatch_mach_reply_unregister(dispatch_mach_t dm,
dispatch_mach_reply_refs_t dmr, uint32_t options)
{
// - async waiters have a dmr of type &_dispatch_mach_type_reply
// heap-allocated in _dispatch_mach_reply_kevent_register().
//
// - sync waiters have a dmr of type DISPATCH_MACH_TYPE_WAITER,
// stack-allocated in _dispatch_mach_send_and_wait_for_reply().
bool sync_waiter = (dux_type(dmr) == DISPATCH_MACH_TYPE_WAITER);
dispatch_mach_send_refs_t dmsr = dm->dm_send_refs;
bool disconnected = (options & DMRU_DISCONNECTED);
bool wakeup = false;
_dispatch_debug("machport[0x%08x]: unregistering for%s reply%s, ctxt %p",
(mach_port_t)dmr->du_ident, sync_waiter ? " sync" : "",
(options & DMRU_CANCEL) ? " (canceled)" :
disconnected ? " (disconnected)" : "", dmr->dmr_ctxt);
if (options & DMRU_REMOVE) {
_dispatch_unfair_lock_lock(&dmsr->dmsr_replies_lock);
_dispatch_mach_reply_list_remove_locked(dmr);
if (LIST_EMPTY(&dmsr->dmsr_replies) && dmsr->dmsr_disconnect_cnt) {
wakeup = true;
}
_dispatch_unfair_lock_unlock(&dmsr->dmsr_replies_lock);
}
if (_dispatch_unote_registered(dmr) &&
!_dispatch_unote_unregister(dmr, options & DMRU_DUU_MASK)) {
dispatch_assert(!sync_waiter); // sync waiters never use kevent
if (options & DMRU_CANCEL) {
// when canceling, failed unregistrations are put back in the list
// the caller has the lock held
LIST_INSERT_HEAD(&dmsr->dmsr_replies, dmr, dmr_list);
}
return;
}
dispatch_mach_msg_t dmsgr = NULL;
dispatch_queue_t drq = NULL;
if (disconnected) {
if (dm->dm_is_xpc && dmr->dmr_ctxt) {
drq = _dispatch_mach_msg_context_async_reply_queue(dmr->dmr_ctxt);
}
dmsgr = _dispatch_mach_msg_create_reply_disconnected(NULL, dmr,
drq ? DISPATCH_MACH_ASYNC_WAITER_DISCONNECTED
: DISPATCH_MACH_DISCONNECTED);
// _dispatch_mach_msg_create_reply_disconnected() consumes the voucher
dispatch_assert(dmr->dmr_voucher == NULL);
} else if (dmr->dmr_voucher) {
_voucher_release(dmr->dmr_voucher);
dmr->dmr_voucher = NULL;
}
if (!sync_waiter) {
_dispatch_unote_dispose(dmr);
}
if (dmsgr) {
if (drq) {
_dispatch_mach_push_async_reply_msg(dm, dmsgr, drq);
} else {
_dispatch_mach_handle_or_push_received_msg(dm, dmsgr, 0);
}
}
if (options & DMRU_ASYNC_MERGE) {
if (wakeup) {
return dx_wakeup(dm, 0,
DISPATCH_WAKEUP_CONSUME_2 | DISPATCH_WAKEUP_MAKE_DIRTY);
}
return _dispatch_release_2_tailcall(dm);
}
}
DISPATCH_NOINLINE
static void
_dispatch_mach_reply_waiter_register(dispatch_mach_t dm,
dispatch_mach_reply_wait_refs_t dwr, mach_port_t reply_port,
dispatch_mach_msg_t dmsg)
{
dispatch_mach_reply_refs_t dmr = &dwr->dwr_refs;
dmr->du_owner_wref = _dispatch_ptr2wref(dm);
dmr->du_filter = EVFILT_MACHPORT;
dmr->du_ident = reply_port;
if (!dmr->dmr_reply_port_owned) {
if (dmsg->dmsg_voucher) {
dmr->dmr_voucher = _voucher_retain(dmsg->dmsg_voucher);
}
dmr->dmr_priority = dmsg->dmsg_priority;
// make reply context visible to leaks rdar://11777199
dmr->dmr_ctxt = dmsg->do_ctxt;
}
_dispatch_debug("machport[0x%08x]: registering for sync reply, ctxt %p",
reply_port, dmsg->do_ctxt);
_dispatch_mach_reply_list_insert(dm->dm_send_refs, dmr);
}
DISPATCH_NOINLINE
static void
_dispatch_mach_reply_kevent_register(dispatch_mach_t dm, mach_port_t reply_port,
dispatch_mach_msg_t dmsg)
{
dispatch_mach_reply_refs_t dmr;
dispatch_priority_t mpri, pri, overcommit;
dispatch_qos_t fallback;
dispatch_wlh_t wlh;
dmr = dux_create(&_dispatch_mach_type_reply, reply_port, 0)._dmr;
dispatch_assert(dmr->du_is_direct);
dmr->du_owner_wref = _dispatch_ptr2wref(dm);
if (dmsg->dmsg_voucher) {
dmr->dmr_voucher = _voucher_retain(dmsg->dmsg_voucher);
}
dmr->dmr_priority = dmsg->dmsg_priority;
// make reply context visible to leaks rdar://11777199
dmr->dmr_ctxt = dmsg->do_ctxt;
dispatch_queue_t drq = NULL;
if (dm->dm_is_xpc && dmsg->do_ctxt) {
drq = _dispatch_mach_msg_context_async_reply_queue(dmsg->do_ctxt);
}
if (unlikely(!drq && _dispatch_unote_wlh(dm->dm_recv_refs))) {
wlh = _dispatch_unote_wlh(dm->dm_recv_refs);
pri = dm->dq_priority;
} else if (dx_hastypeflag(drq, QUEUE_ROOT)) {
wlh = DISPATCH_WLH_ANON;
if (_dispatch_is_in_root_queues_array(drq)) {
pri = drq->dq_priority;
} else {
pri = DISPATCH_PRIORITY_FLAG_MANAGER;
}
} else if (!(pri = _dispatch_queue_compute_priority_and_wlh(drq, &wlh))) {
wlh = DISPATCH_WLH_ANON;
pri = drq->dq_priority;
}
mpri = _dispatch_priority_from_pp_strip_flags(dmsg->dmsg_priority);
overcommit = pri & DISPATCH_PRIORITY_FLAG_OVERCOMMIT;
fallback = _dispatch_priority_fallback_qos(pri);
if (pri & DISPATCH_PRIORITY_REQUESTED_MASK) {
pri &= DISPATCH_PRIORITY_REQUESTED_MASK;
if (pri < mpri) pri = mpri;
pri |= overcommit;
} else if (fallback && mpri) {
pri = mpri | overcommit;
} else if (fallback && !mpri) {
pri = _dispatch_priority_make(fallback, 0) | overcommit;
} else {
pri = DISPATCH_PRIORITY_FLAG_MANAGER;
wlh = DISPATCH_WLH_ANON;
}
_dispatch_debug("machport[0x%08x]: registering for reply, ctxt %p",
reply_port, dmsg->do_ctxt);
_dispatch_mach_reply_list_insert(dm->dm_send_refs, dmr);
if (!_dispatch_unote_register(dmr, wlh, pri)) {
uint32_t options = DMRU_MUST_SUCCEED | DMRU_REMOVE | DMRU_DISCONNECTED;
_dispatch_mach_reply_unregister(dm, dmr, options);
}
}
#pragma mark -
#pragma mark dispatch_mach_msg
DISPATCH_ALWAYS_INLINE DISPATCH_CONST
static inline bool
_dispatch_use_mach_special_reply_port(void)
{
#if DISPATCH_USE_MACH_SEND_SYNC_OVERRIDE
return true;
#else
#define thread_get_special_reply_port() ({__builtin_trap(); MACH_PORT_NULL;})
return false;
#endif
}
static void
_dispatch_destruct_reply_port(mach_port_t reply_port,
enum thread_destruct_special_reply_port_rights rights)
{
kern_return_t kr = KERN_SUCCESS;
if (_dispatch_use_mach_special_reply_port()) {
kr = thread_destruct_special_reply_port(reply_port, rights);
} else if (rights == THREAD_SPECIAL_REPLY_PORT_ALL ||
rights == THREAD_SPECIAL_REPLY_PORT_RECEIVE_ONLY) {
kr = mach_port_destruct(mach_task_self(), reply_port, 0, 0);
}
DISPATCH_VERIFY_MIG(kr);
dispatch_assume_zero(kr);
}
static mach_port_t
_dispatch_get_thread_reply_port(void)
{
mach_port_t reply_port, mrp;
if (_dispatch_use_mach_special_reply_port()) {
mrp = _dispatch_get_thread_special_reply_port();
} else {
mrp = _dispatch_get_thread_mig_reply_port();
}
if (mrp) {
reply_port = mrp;
_dispatch_debug("machport[0x%08x]: borrowed thread sync reply port",
reply_port);
} else {
if (_dispatch_use_mach_special_reply_port()) {
reply_port = thread_get_special_reply_port();
_dispatch_set_thread_special_reply_port(reply_port);
} else {
reply_port = mach_reply_port();
_dispatch_set_thread_mig_reply_port(reply_port);
}
if (unlikely(!MACH_PORT_VALID(reply_port))) {
DISPATCH_CLIENT_CRASH(_dispatch_use_mach_special_reply_port(),
"Unable to allocate reply port, possible port leak");
}
_dispatch_debug("machport[0x%08x]: allocated thread sync reply port",
reply_port);
}
_dispatch_debug_machport(reply_port);
return reply_port;
}
static void
_dispatch_clear_thread_reply_port(mach_port_t reply_port)
{
mach_port_t mrp;
if (_dispatch_use_mach_special_reply_port()) {
mrp = _dispatch_get_thread_special_reply_port();
} else {
mrp = _dispatch_get_thread_mig_reply_port();
}
if (reply_port != mrp) {
if (mrp) {
_dispatch_debug("machport[0x%08x]: did not clear thread sync reply "
"port (found 0x%08x)", reply_port, mrp);
}
return;
}
if (_dispatch_use_mach_special_reply_port()) {
_dispatch_set_thread_special_reply_port(MACH_PORT_NULL);
} else {
_dispatch_set_thread_mig_reply_port(MACH_PORT_NULL);
}
_dispatch_debug_machport(reply_port);
_dispatch_debug("machport[0x%08x]: cleared thread sync reply port",
reply_port);
}
static void
_dispatch_set_thread_reply_port(mach_port_t reply_port)
{
_dispatch_debug_machport(reply_port);
mach_port_t mrp;
if (_dispatch_use_mach_special_reply_port()) {
mrp = _dispatch_get_thread_special_reply_port();
} else {
mrp = _dispatch_get_thread_mig_reply_port();
}
if (mrp) {
_dispatch_destruct_reply_port(reply_port,
THREAD_SPECIAL_REPLY_PORT_ALL);
_dispatch_debug("machport[0x%08x]: deallocated sync reply port "
"(found 0x%08x)", reply_port, mrp);
} else {
if (_dispatch_use_mach_special_reply_port()) {
_dispatch_set_thread_special_reply_port(reply_port);
} else {
_dispatch_set_thread_mig_reply_port(reply_port);
}
_dispatch_debug("machport[0x%08x]: restored thread sync reply port",
reply_port);
}
}
static inline mach_port_t
_dispatch_mach_msg_get_remote_port(dispatch_object_t dou)
{
mach_msg_header_t *hdr = _dispatch_mach_msg_get_msg(dou._dmsg);
mach_port_t remote = hdr->msgh_remote_port;
return remote;
}
static inline mach_port_t
_dispatch_mach_msg_get_reply_port(dispatch_object_t dou)
{
mach_msg_header_t *hdr = _dispatch_mach_msg_get_msg(dou._dmsg);
mach_port_t local = hdr->msgh_local_port;
if (!MACH_PORT_VALID(local) || MACH_MSGH_BITS_LOCAL(hdr->msgh_bits) !=
MACH_MSG_TYPE_MAKE_SEND_ONCE) return MACH_PORT_NULL;
return local;
}
static inline void
_dispatch_mach_msg_set_reason(dispatch_mach_msg_t dmsg, mach_error_t err,
unsigned long reason)
{
dispatch_assert_zero(reason & ~(unsigned long)code_emask);
dmsg->dmsg_error = ((err || !reason) ? err :
err_local|err_sub(0x3e0)|(mach_error_t)reason);
}
static inline unsigned long
_dispatch_mach_msg_get_reason(dispatch_mach_msg_t dmsg, mach_error_t *err_ptr)
{
mach_error_t err = dmsg->dmsg_error;
if ((err & system_emask) == err_local && err_get_sub(err) == 0x3e0) {
*err_ptr = 0;
return err_get_code(err);
}
*err_ptr = err;
return err ? DISPATCH_MACH_MESSAGE_SEND_FAILED : DISPATCH_MACH_MESSAGE_SENT;
}
static inline dispatch_mach_msg_t
_dispatch_mach_msg_create_recv(mach_msg_header_t *hdr, mach_msg_size_t siz,
dispatch_mach_reply_refs_t dmr, uint32_t flags, pthread_priority_t pp)
{
dispatch_mach_msg_destructor_t destructor;
dispatch_mach_msg_t dmsg;
voucher_t voucher;
if (dmr) {
_voucher_mach_msg_clear(hdr, false); // deallocate reply message voucher
pp = dmr->dmr_priority;
voucher = dmr->dmr_voucher;
dmr->dmr_voucher = NULL; // transfer reference
} else {
voucher = voucher_create_with_mach_msg(hdr);
pp = _dispatch_priority_compute_propagated(pp, 0);
}
destructor = (flags & DISPATCH_EV_MSG_NEEDS_FREE) ?
DISPATCH_MACH_MSG_DESTRUCTOR_FREE :
DISPATCH_MACH_MSG_DESTRUCTOR_DEFAULT;
dmsg = dispatch_mach_msg_create(hdr, siz, destructor, NULL);
if (!(flags & DISPATCH_EV_MSG_NEEDS_FREE)) {
_dispatch_ktrace2(DISPATCH_MACH_MSG_hdr_move,
(uint64_t)hdr, (uint64_t)dmsg->dmsg_buf);
}
dmsg->dmsg_voucher = voucher;
dmsg->dmsg_priority = pp;
dmsg->do_ctxt = dmr ? dmr->dmr_ctxt : NULL;
_dispatch_mach_msg_set_reason(dmsg, 0, DISPATCH_MACH_MESSAGE_RECEIVED);
_dispatch_voucher_debug("mach-msg[%p] create", voucher, dmsg);
_dispatch_voucher_ktrace_dmsg_push(dmsg);
return dmsg;
}
void
_dispatch_mach_merge_msg(dispatch_unote_t du, uint32_t flags,
mach_msg_header_t *hdr, mach_msg_size_t siz,
pthread_priority_t msg_pp, pthread_priority_t ovr_pp)
{
if (flags & EV_VANISHED) {
DISPATCH_CLIENT_CRASH(du._du->du_ident,
"Unexpected EV_VANISHED (do not destroy random mach ports)");
}
_dispatch_debug_machport(hdr->msgh_remote_port);
_dispatch_debug("machport[0x%08x]: received msg id 0x%x, reply on 0x%08x",
hdr->msgh_local_port, hdr->msgh_id, hdr->msgh_remote_port);
dispatch_mach_t dm = _dispatch_wref2ptr(du._dmrr->du_owner_wref);
if (unlikely(_dispatch_queue_atomic_flags(dm) & DSF_CANCELED)) {
_dispatch_debug("machport[0x%08x]: drop msg id 0x%x, reply on 0x%08x",
hdr->msgh_local_port, hdr->msgh_id, hdr->msgh_remote_port);
mach_msg_destroy(hdr);
if (flags & DISPATCH_EV_MSG_NEEDS_FREE) {
free(hdr);
}
} else {
// Once the mach channel disarming is visible, cancellation will switch
// to immediately destroy messages. If we're preempted here, then the
// whole cancellation sequence may be complete by the time we really
// enqueue the message.
//
// _dispatch_mach_msg_invoke_with_mach() is responsible for filtering it
// out to keep the promise that DISPATCH_MACH_DISCONNECTED is the last
// event sent.
dispatch_mach_msg_t dmsg;
dmsg = _dispatch_mach_msg_create_recv(hdr, siz, NULL, flags, msg_pp);
_dispatch_mach_handle_or_push_received_msg(dm, dmsg, ovr_pp);
}
if (unlikely(_dispatch_unote_needs_delete(du))) {
return dx_wakeup(dm, 0, DISPATCH_WAKEUP_EVENT |
DISPATCH_WAKEUP_CONSUME_2 | DISPATCH_WAKEUP_MAKE_DIRTY);
}
return _dispatch_release_2_tailcall(dm);
}
void
_dispatch_mach_reply_merge_msg(dispatch_unote_t du, uint32_t flags,
mach_msg_header_t *hdr, mach_msg_size_t siz,
pthread_priority_t msg_pp, pthread_priority_t ovr_pp)
{
dispatch_mach_reply_refs_t dmr = du._dmr;
dispatch_mach_t dm = _dispatch_wref2ptr(dmr->du_owner_wref);
bool canceled = (_dispatch_queue_atomic_flags(dm) & DSF_CANCELED);
dispatch_mach_msg_t dmsg = NULL;
_dispatch_debug_machport(hdr->msgh_remote_port);
_dispatch_debug("machport[0x%08x]: received msg id 0x%x, reply on 0x%08x",
hdr->msgh_local_port, hdr->msgh_id, hdr->msgh_remote_port);
if (!canceled) {
dmsg = _dispatch_mach_msg_create_recv(hdr, siz, dmr, flags, msg_pp);
}
if (dmsg) {
dispatch_queue_t drq = NULL;
if (dm->dm_is_xpc && dmsg->do_ctxt) {
drq = _dispatch_mach_msg_context_async_reply_queue(dmsg->do_ctxt);
}
if (drq) {
_dispatch_mach_push_async_reply_msg(dm, dmsg, drq);
} else {
_dispatch_mach_handle_or_push_received_msg(dm, dmsg, ovr_pp);
}
} else {
_dispatch_debug("machport[0x%08x]: drop msg id 0x%x, reply on 0x%08x",
hdr->msgh_local_port, hdr->msgh_id, hdr->msgh_remote_port);
mach_msg_destroy(hdr);
if (flags & DISPATCH_EV_MSG_NEEDS_FREE) {
free(hdr);
}
}
uint32_t options = DMRU_ASYNC_MERGE | DMRU_REMOVE;
options |= DMRU_MUST_SUCCEED | DMRU_DELETE_ACK;
if (canceled) options |= DMRU_DISCONNECTED;
dispatch_assert(_dispatch_unote_needs_delete(dmr));
_dispatch_mach_reply_unregister(dm, dmr, options); // consumes the +2
}
DISPATCH_ALWAYS_INLINE
static void
_dispatch_mach_stack_probe(void *addr, size_t size)
{
#if TARGET_OS_MAC && DISPATCH_MIN_REQUIRED_OSX_AT_LEAST(101400) && \
(defined(__x86_64__) || defined(__arm64__))
// <rdar://problem/40708879> there should be a __has_feature() macro test
// for this, for now we approximate it, for when the compiler
// is generating calls to ____chkstk_darwin on our behalf
(void)addr; (void)size;
#else
for (mach_vm_address_t p = mach_vm_trunc_page(addr + vm_page_size);
p < (mach_vm_address_t)addr + size; p += vm_page_size) {
*(char*)p = 0; // ensure alloca buffer doesn't overlap with stack guard
}
#endif
}
DISPATCH_ALWAYS_INLINE
static inline dispatch_mach_msg_t
_dispatch_mach_msg_reply_recv(dispatch_mach_t dm,
dispatch_mach_reply_wait_refs_t dwr, mach_port_t reply_port,
mach_port_t send)
{
if (unlikely(!MACH_PORT_VALID(reply_port))) {
DISPATCH_CLIENT_CRASH(reply_port, "Invalid reply port");
}
void *ctxt = dwr->dwr_refs.dmr_ctxt;
mach_msg_header_t *hdr, *hdr2 = NULL;
void *hdr_copyout_addr;
mach_msg_size_t siz, msgsiz = 0;
mach_msg_return_t kr;
mach_msg_option_t options;
mach_port_t notify = MACH_PORT_NULL;
siz = mach_vm_round_page(DISPATCH_MACH_RECEIVE_MAX_INLINE_MESSAGE_SIZE +
DISPATCH_MACH_TRAILER_SIZE);
hdr = alloca(siz);
_dispatch_mach_stack_probe(hdr, siz);
options = DISPATCH_MACH_RCV_OPTIONS & (~MACH_RCV_VOUCHER);
if (MACH_PORT_VALID(send)) {
notify = send;
options |= MACH_RCV_SYNC_WAIT;
}
retry:
_dispatch_debug_machport(reply_port);
_dispatch_debug("machport[0x%08x]: MACH_RCV_MSG %s", reply_port,
(options & MACH_RCV_TIMEOUT) ? "poll" : "wait");
kr = mach_msg(hdr, options, 0, siz, reply_port, MACH_MSG_TIMEOUT_NONE,
notify);
hdr_copyout_addr = hdr;
_dispatch_debug_machport(reply_port);
_dispatch_debug("machport[0x%08x]: MACH_RCV_MSG (size %u, opts 0x%x) "
"returned: %s - 0x%x", reply_port, siz, options,
mach_error_string(kr), kr);
switch (kr) {
case MACH_RCV_TOO_LARGE:
if (unlikely(hdr->msgh_size > UINT_MAX - DISPATCH_MACH_TRAILER_SIZE)) {
DISPATCH_CLIENT_CRASH(hdr->msgh_size, "Overlarge message");
}
if (options & MACH_RCV_LARGE) {
msgsiz = hdr->msgh_size + DISPATCH_MACH_TRAILER_SIZE;
hdr2 = malloc(msgsiz);
if (dispatch_assume(hdr2)) {
hdr = hdr2;
siz = msgsiz;
}
options |= MACH_RCV_TIMEOUT;
options &= ~MACH_RCV_LARGE;
goto retry;
}
_dispatch_log("BUG in libdispatch client: "
"dispatch_mach_send_and_wait_for_reply: dropped message too "
"large to fit in memory: id = 0x%x, size = %u", hdr->msgh_id,
hdr->msgh_size);
break;
case MACH_RCV_INVALID_NAME: // rdar://problem/21963848
case MACH_RCV_PORT_CHANGED: // rdar://problem/21885327
case MACH_RCV_PORT_DIED:
// channel was disconnected/canceled and reply port destroyed
_dispatch_debug("machport[0x%08x]: sync reply port destroyed, ctxt %p: "
"%s - 0x%x", reply_port, ctxt, mach_error_string(kr), kr);
if (dwr->dwr_refs.dmr_reply_port_owned) {
_dispatch_destruct_reply_port(reply_port,
THREAD_SPECIAL_REPLY_PORT_SEND_ONLY);
}
goto out;
case MACH_MSG_SUCCESS:
if (hdr->msgh_remote_port) {
_dispatch_debug_machport(hdr->msgh_remote_port);
}
_dispatch_debug("machport[0x%08x]: received msg id 0x%x, size = %u, "
"reply on 0x%08x", hdr->msgh_local_port, hdr->msgh_id,
hdr->msgh_size, hdr->msgh_remote_port);
siz = hdr->msgh_size + DISPATCH_MACH_TRAILER_SIZE;
if (hdr2 && siz < msgsiz) {
void *shrink = realloc(hdr2, msgsiz);
if (shrink) hdr = hdr2 = shrink;
}
break;
case MACH_RCV_INVALID_NOTIFY:
default:
DISPATCH_INTERNAL_CRASH(kr, "Unexpected error from mach_msg_receive");
break;
}
_dispatch_mach_msg_reply_received(dm, dwr, hdr->msgh_local_port);
hdr->msgh_local_port = MACH_PORT_NULL;
if (unlikely((dm->dq_atomic_flags & DSF_CANCELED) || kr)) {
if (!kr) mach_msg_destroy(hdr);
goto out;
}
dispatch_mach_msg_t dmsg;
dispatch_mach_msg_destructor_t destructor = (!hdr2) ?
DISPATCH_MACH_MSG_DESTRUCTOR_DEFAULT :
DISPATCH_MACH_MSG_DESTRUCTOR_FREE;
dmsg = dispatch_mach_msg_create(hdr, siz, destructor, NULL);
if (!hdr2 || hdr != hdr_copyout_addr) {
_dispatch_ktrace2(DISPATCH_MACH_MSG_hdr_move,
(uint64_t)hdr_copyout_addr,
(uint64_t)_dispatch_mach_msg_get_msg(dmsg));
}
dmsg->do_ctxt = ctxt;
return dmsg;
out:
free(hdr2);
return NULL;
}
static inline void
_dispatch_mach_msg_reply_received(dispatch_mach_t dm,
dispatch_mach_reply_wait_refs_t dwr, mach_port_t local_port)
{
dispatch_mach_reply_refs_t dmr = &dwr->dwr_refs;
bool removed = _dispatch_mach_reply_list_tryremove(dm->dm_send_refs, dmr);
mach_port_t reply_port = (mach_port_t)dmr->du_ident;
if (removed) {
_dispatch_debug("machport[0x%08x]: unregistered for sync reply, ctxt %p",
reply_port, dmr->dmr_ctxt);
}
if (dmr->dmr_reply_port_owned) {
if (local_port != reply_port &&
(removed || MACH_PORT_VALID(local_port))) {
DISPATCH_CLIENT_CRASH(local_port,
"Reply received on unexpected port");
}
if (removed) {
_dispatch_set_thread_reply_port(reply_port);
} else {
_dispatch_destruct_reply_port(reply_port,
THREAD_SPECIAL_REPLY_PORT_SEND_ONLY);
}
return;
}
if (!MACH_PORT_VALID(local_port) || !removed) {
// port moved/destroyed during receive, or reply waiter was never
// registered or already removed (disconnected)
return;
}
mach_msg_header_t *hdr;
dispatch_mach_msg_t dmsg;
dmsg = dispatch_mach_msg_create(NULL, sizeof(mach_msg_header_t),
DISPATCH_MACH_MSG_DESTRUCTOR_DEFAULT, &hdr);
hdr->msgh_local_port = local_port;
dmsg->dmsg_voucher = dmr->dmr_voucher;
dmr->dmr_voucher = NULL; // transfer reference
dmsg->dmsg_priority = dmr->dmr_priority;
dmsg->do_ctxt = dmr->dmr_ctxt;
_dispatch_mach_msg_set_reason(dmsg, 0, DISPATCH_MACH_REPLY_RECEIVED);
return _dispatch_mach_handle_or_push_received_msg(dm, dmsg, 0);
}
static inline void
_dispatch_mach_msg_disconnected(dispatch_mach_t dm, mach_port_t local_port,
mach_port_t remote_port)
{
mach_msg_header_t *hdr;
dispatch_mach_msg_t dmsg;
dmsg = dispatch_mach_msg_create(NULL, sizeof(mach_msg_header_t),
DISPATCH_MACH_MSG_DESTRUCTOR_DEFAULT, &hdr);
if (local_port) hdr->msgh_local_port = local_port;
if (remote_port) hdr->msgh_remote_port = remote_port;
_dispatch_mach_msg_set_reason(dmsg, 0, DISPATCH_MACH_DISCONNECTED);
_dispatch_debug("machport[0x%08x]: %s right disconnected", local_port ?
local_port : remote_port, local_port ? "receive" : "send");
return _dispatch_mach_handle_or_push_received_msg(dm, dmsg, 0);
}
static inline dispatch_mach_msg_t
_dispatch_mach_msg_create_reply_disconnected(dispatch_object_t dou,
dispatch_mach_reply_refs_t dmr, dispatch_mach_reason_t reason)
{
dispatch_mach_msg_t dmsg = dou._dmsg, dmsgr;
mach_port_t reply_port = dmsg ? dmsg->dmsg_reply :(mach_port_t)dmr->du_ident;
if (!reply_port) {
if (!dmsg && dmr->dmr_voucher) {
_voucher_release(dmr->dmr_voucher);
dmr->dmr_voucher = NULL;
}
return NULL;
}
if (dmr && !_dispatch_unote_registered(dmr) && dmr->dmr_reply_port_owned) {
if (dmr->dmr_voucher) {
_voucher_release(dmr->dmr_voucher);
dmr->dmr_voucher = NULL;
}
// deallocate owned reply port to break _dispatch_mach_msg_reply_recv
// out of waiting in mach_msg(MACH_RCV_MSG).
//
// after this call, dmr can become invalid
_dispatch_destruct_reply_port(reply_port,
THREAD_SPECIAL_REPLY_PORT_RECEIVE_ONLY);
return NULL;
}
mach_msg_header_t *hdr;
dmsgr = dispatch_mach_msg_create(NULL, sizeof(mach_msg_header_t),
DISPATCH_MACH_MSG_DESTRUCTOR_DEFAULT, &hdr);
hdr->msgh_local_port = reply_port;
if (dmsg) {
dmsgr->dmsg_priority = dmsg->dmsg_priority;
dmsgr->do_ctxt = dmsg->do_ctxt;
dmsgr->dmsg_voucher = dmsg->dmsg_voucher;
if (dmsgr->dmsg_voucher) _voucher_retain(dmsgr->dmsg_voucher);
} else {
dmsgr->dmsg_priority = dmr->dmr_priority;
dmsgr->do_ctxt = dmr->dmr_ctxt;
dmsgr->dmsg_voucher = dmr->dmr_voucher;
dmr->dmr_voucher = NULL; // transfer reference
}
_dispatch_mach_msg_set_reason(dmsgr, 0, reason);
_dispatch_debug("machport[0x%08x]: reply disconnected, ctxt %p",
hdr->msgh_local_port, dmsgr->do_ctxt);
return dmsgr;
}
DISPATCH_NOINLINE
static void
_dispatch_mach_msg_not_sent(dispatch_mach_t dm, dispatch_object_t dou,
dispatch_mach_reply_wait_refs_t dwr)
{
dispatch_mach_msg_t dmsg = dou._dmsg, dmsgr;
dispatch_queue_t drq = NULL;
mach_msg_header_t *msg = _dispatch_mach_msg_get_msg(dmsg);
mach_msg_option_t msg_opts = dmsg->dmsg_options;
_dispatch_debug("machport[0x%08x]: not sent msg id 0x%x, ctxt %p, "
"msg_opts 0x%x, kvoucher 0x%08x, reply on 0x%08x",
msg->msgh_remote_port, msg->msgh_id, dmsg->do_ctxt,
msg_opts, msg->msgh_voucher_port, dmsg->dmsg_reply);
unsigned long reason = (msg_opts & DISPATCH_MACH_REGISTER_FOR_REPLY) ?
0 : DISPATCH_MACH_MESSAGE_NOT_SENT;
if (dm->dm_is_xpc && dmsg->do_ctxt) {
drq = _dispatch_mach_msg_context_async_reply_queue(dmsg->do_ctxt);
}
dmsgr = _dispatch_mach_msg_create_reply_disconnected(dmsg,
dwr ? &dwr->dwr_refs : NULL,
drq ? DISPATCH_MACH_ASYNC_WAITER_DISCONNECTED
: DISPATCH_MACH_DISCONNECTED);
_dispatch_mach_msg_set_reason(dmsg, 0, reason);
_dispatch_mach_handle_or_push_received_msg(dm, dmsg, 0);
if (dmsgr) {
if (drq) {
_dispatch_mach_push_async_reply_msg(dm, dmsgr, drq);
} else {
_dispatch_mach_handle_or_push_received_msg(dm, dmsgr, 0);
}
}
}
DISPATCH_NOINLINE
static uint32_t
_dispatch_mach_msg_send(dispatch_mach_t dm, dispatch_object_t dou,
dispatch_mach_reply_wait_refs_t dwr, dispatch_qos_t qos,
dispatch_mach_send_invoke_flags_t send_flags)
{
dispatch_mach_send_refs_t dsrr = dm->dm_send_refs;
dispatch_mach_msg_t dmsg = dou._dmsg, dmsgr = NULL;
voucher_t voucher = dmsg->dmsg_voucher;
dispatch_queue_t drq = NULL;
mach_voucher_t ipc_kvoucher = MACH_VOUCHER_NULL;
uint32_t send_status = 0;
bool clear_voucher = false, kvoucher_move_send = false;
mach_msg_header_t *msg = _dispatch_mach_msg_get_msg(dmsg);
bool is_reply = (MACH_MSGH_BITS_REMOTE(msg->msgh_bits) ==
MACH_MSG_TYPE_MOVE_SEND_ONCE);
mach_port_t reply_port = dmsg->dmsg_reply;
if (!is_reply) {
dm->dm_needs_mgr = 0;
if (unlikely(dsrr->dmsr_checkin && dmsg != dsrr->dmsr_checkin)) {
// send initial checkin message
if (unlikely(_dispatch_unote_registered(dsrr) &&
_dispatch_queue_get_current() != _dispatch_mgr_q._as_dq)) {
// send kevent must be uninstalled on the manager queue
dm->dm_needs_mgr = true;
goto out;
}
if (unlikely(!_dispatch_mach_msg_send(dm,
dsrr->dmsr_checkin, NULL, qos, DM_SEND_INVOKE_NONE))) {
goto out;
}
dsrr->dmsr_checkin = NULL;
}
}
mach_msg_return_t kr = 0;
mach_msg_option_t opts = 0, msg_opts = dmsg->dmsg_options;
if (!(msg_opts & DISPATCH_MACH_REGISTER_FOR_REPLY)) {