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 _mach_assert(__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
) {
88 kr
= mach_port_destruct(mach_task_self(), r
.mrr_name
, srd
, ctx
);
89 _mach_assert("destruct recv right", kr
);
93 mach_right_send_create(mach_right_recv_t r
)
95 kern_return_t kr
= KERN_FAILURE
;
97 kr
= mach_port_insert_right(mach_task_self(), r
.mrr_name
, r
.mrr_name
,
98 MACH_MSG_TYPE_MAKE_SEND
);
99 _mach_assert("create send right", kr
);
101 return mach_right_send(r
.mrr_name
);
105 mach_right_send_retain(mach_right_send_t s
)
107 kern_return_t kr
= KERN_FAILURE
;
108 mach_right_send_t rs
= MACH_RIGHT_SEND_NULL
;
110 kr
= mach_port_mod_refs(mach_task_self(), s
.mrs_name
,
111 MACH_PORT_RIGHT_SEND
, 1);
116 case KERN_INVALID_RIGHT
:
117 rs
.mrs_name
= MACH_PORT_DEAD
;
119 case KERN_INVALID_NAME
:
120 // mach_port_mod_refs() will return success when given either
121 // MACH_PORT_DEAD or MACH_PORT_NULL with send or send-once right
122 // operations, so this is always fatal.
124 _mach_assert("retain send right", kr
);
131 mach_right_send_release(mach_right_send_t s
)
133 kern_return_t kr
= KERN_FAILURE
;
135 kr
= mach_port_mod_refs(mach_task_self(), s
.mrs_name
,
136 MACH_PORT_RIGHT_SEND
, -1);
140 case KERN_INVALID_RIGHT
:
141 kr
= mach_port_mod_refs(mach_task_self(), s
.mrs_name
,
142 MACH_PORT_RIGHT_DEAD_NAME
, -1);
143 _mach_assert("release dead name", kr
);
146 _mach_assert("release send right", kr
);
150 mach_right_send_once_t
151 mach_right_send_once_create(mach_right_recv_t r
)
153 mach_msg_type_name_t right
= 0;
154 mach_port_t so
= MACH_PORT_NULL
;
155 kern_return_t kr
= mach_port_extract_right(mach_task_self(), r
.mrr_name
,
156 MACH_MSG_TYPE_MAKE_SEND_ONCE
, &so
, &right
);
157 _mach_assert("create send-once right", kr
);
159 return mach_right_send_once(so
);
163 mach_right_send_once_consume(mach_right_send_once_t so
)
165 kern_return_t kr
= KERN_FAILURE
;
167 kr
= mach_port_mod_refs(mach_task_self(), so
.mrso_name
,
168 MACH_PORT_RIGHT_SEND_ONCE
, -1);
172 case KERN_INVALID_RIGHT
:
173 kr
= mach_port_mod_refs(mach_task_self(), so
.mrso_name
,
174 MACH_PORT_RIGHT_DEAD_NAME
, -1);
175 _mach_assert("release dead name", kr
);
178 _mach_assert("consume send-once right", kr
);