2 * Copyright (c) 2018 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <mach/mach.h>
29 #include <mach/mach_traps.h>
30 #include <mach/mach_port.h>
31 #include <mach/mach_right.h>
34 #pragma mark Utilities
35 #define _assert_mach(__op, __kr) \
37 if (kr != KERN_SUCCESS) { \
44 mach_right_recv_construct(mach_right_flags_t flags
,
45 mach_right_send_t
*_Nullable sr
, uintptr_t ctx
)
47 kern_return_t kr
= KERN_FAILURE
;
48 mach_port_t p
= MACH_PORT_NULL
;
49 mach_port_options_t opts
= {
50 .flags
= MPO_CONTEXT_AS_GUARD
,
52 .mpl_qlimit
= MACH_PORT_QLIMIT_BASIC
,
56 if (flags
& MACH_RIGHT_RECV_FLAG_UNGUARDED
) {
57 opts
.flags
&= (~MPO_CONTEXT_AS_GUARD
);
60 opts
.flags
|= MPO_INSERT_SEND_RIGHT
;
63 kr
= mach_port_construct(mach_task_self(), &opts
, ctx
, &p
);
64 _mach_assert("construct recv right", kr
);
70 return mach_right_recv(p
);
74 mach_right_recv_destruct(mach_right_recv_t r
, mach_right_send_t
*s
,
77 kern_return_t kr
= KERN_FAILURE
;
78 mach_port_delta_t srd
= 0;
81 if (r
.mrr_name
!= s
->mrs_name
) {
82 _os_set_crash_log_cause_and_message(s
->mrs_name
,
83 "api misuse: bad send right");
90 kr
= mach_port_destruct(mach_task_self(), r
.mrr_name
, srd
, ctx
);
91 _mach_assert("destruct recv right", kr
);
95 mach_right_send_create(mach_right_recv_t r
)
97 kern_return_t kr
= KERN_FAILURE
;
99 kr
= mach_port_insert_right(mach_task_self(), r
.mrr_name
, r
.mrr_name
,
100 MACH_MSG_TYPE_MAKE_SEND
);
101 _mach_assert("create send right", kr
);
103 return mach_right_send(r
.mrr_name
);
107 mach_right_send_retain(mach_right_send_t s
)
109 kern_return_t kr
= KERN_FAILURE
;
110 mach_right_send_t rs
= MACH_RIGHT_SEND_NULL
;
112 kr
= mach_port_mod_refs(mach_task_self(), s
.mrs_name
,
113 MACH_PORT_RIGHT_SEND
, 1);
118 case KERN_INVALID_RIGHT
:
119 rs
.mrs_name
= MACH_PORT_DEAD
;
121 case KERN_INVALID_NAME
:
122 // mach_port_mod_refs() will return success when given either
123 // MACH_PORT_DEAD or MACH_PORT_NULL with send or send-once right
124 // operations, so this is always fatal.
126 _mach_assert("retain send right", kr
);
133 mach_right_send_release(mach_right_send_t s
)
135 kern_return_t kr
= KERN_FAILURE
;
137 kr
= mach_port_mod_refs(mach_task_self(), s
.mrs_name
,
138 MACH_PORT_RIGHT_SEND
, -1);
142 case KERN_INVALID_RIGHT
:
143 kr
= mach_port_mod_refs(mach_task_self(), s
.mrs_name
,
144 MACH_PORT_RIGHT_DEAD_NAME
, -1);
145 _mach_assert("release dead name", kr
);
148 _mach_assert("release send right", kr
);
152 mach_right_send_once_t
153 mach_right_send_once_create(mach_right_recv_t r
)
155 mach_msg_type_name_t right
= 0;
156 mach_port_t so
= MACH_PORT_NULL
;
157 kern_return_t kr
= mach_port_extract_right(mach_task_self(), r
.mrr_name
,
158 MACH_MSG_TYPE_MAKE_SEND_ONCE
, &so
, &right
);
159 _mach_assert("create send-once right", kr
);
161 return mach_right_send_once(so
);
165 mach_right_send_once_consume(mach_right_send_once_t so
)
167 kern_return_t kr
= KERN_FAILURE
;
169 kr
= mach_port_mod_refs(mach_task_self(), so
.mrso_name
,
170 MACH_PORT_RIGHT_SEND_ONCE
, -1);
174 case KERN_INVALID_RIGHT
:
175 kr
= mach_port_mod_refs(mach_task_self(), so
.mrso_name
,
176 MACH_PORT_RIGHT_DEAD_NAME
, -1);
177 _mach_assert("release dead name", kr
);
180 _mach_assert("consume send-once right", kr
);