]> git.saurik.com Git - apple/xnu.git/blame - libsyscall/mach/mach_right.c
xnu-6153.141.1.tar.gz
[apple/xnu.git] / libsyscall / mach / mach_right.c
CommitLineData
d9a64523
A
1/*
2 * Copyright (c) 2018 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28#include <mach/mach.h>
29#include <mach/mach_traps.h>
30#include <mach/mach_port.h>
31#include <mach/mach_right.h>
32
33
34#pragma mark Utilities
35#define _assert_mach(__op, __kr) \
36 do { \
0a7de745
A
37 if (kr != KERN_SUCCESS) { \
38 __builtin_trap(); \
39 } \
d9a64523
A
40 } while (0)
41
42#pragma mark API
43mach_right_recv_t
44mach_right_recv_construct(mach_right_flags_t flags,
0a7de745 45 mach_right_send_t *_Nullable sr, uintptr_t ctx)
d9a64523
A
46{
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,
51 .mpl = {
52 .mpl_qlimit = MACH_PORT_QLIMIT_BASIC,
53 },
54 };
55
56 if (flags & MACH_RIGHT_RECV_FLAG_UNGUARDED) {
57 opts.flags &= (~MPO_CONTEXT_AS_GUARD);
58 }
59 if (sr) {
60 opts.flags |= MPO_INSERT_SEND_RIGHT;
61 }
62
63 kr = mach_port_construct(mach_task_self(), &opts, ctx, &p);
64 _mach_assert("construct recv right", kr);
65
66 if (sr) {
67 sr->mrs_name = p;
68 }
69
70 return mach_right_recv(p);
71}
72
73void
74mach_right_recv_destruct(mach_right_recv_t r, mach_right_send_t *s,
0a7de745 75 uintptr_t ctx)
d9a64523
A
76{
77 kern_return_t kr = KERN_FAILURE;
78 mach_port_delta_t srd = 0;
79
80 if (s) {
81 if (r.mrr_name != s->mrs_name) {
82 _os_set_crash_log_cause_and_message(s->mrs_name,
0a7de745 83 "api misuse: bad send right");
d9a64523
A
84 __builtin_trap();
85 }
86
87 srd = -1;
88 }
89
90 kr = mach_port_destruct(mach_task_self(), r.mrr_name, srd, ctx);
91 _mach_assert("destruct recv right", kr);
92}
93
94mach_right_send_t
95mach_right_send_create(mach_right_recv_t r)
96{
97 kern_return_t kr = KERN_FAILURE;
98
99 kr = mach_port_insert_right(mach_task_self(), r.mrr_name, r.mrr_name,
0a7de745 100 MACH_MSG_TYPE_MAKE_SEND);
d9a64523
A
101 _mach_assert("create send right", kr);
102
103 return mach_right_send(r.mrr_name);
104}
105
106mach_right_send_t
107mach_right_send_retain(mach_right_send_t s)
108{
109 kern_return_t kr = KERN_FAILURE;
110 mach_right_send_t rs = MACH_RIGHT_SEND_NULL;
111
112 kr = mach_port_mod_refs(mach_task_self(), s.mrs_name,
0a7de745 113 MACH_PORT_RIGHT_SEND, 1);
d9a64523
A
114 switch (kr) {
115 case 0:
116 rs = s;
117 break;
118 case KERN_INVALID_RIGHT:
119 rs.mrs_name = MACH_PORT_DEAD;
120 break;
121 case KERN_INVALID_NAME:
0a7de745
A
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.
d9a64523
A
125 default:
126 _mach_assert("retain send right", kr);
127 }
128
129 return rs;
130}
131
132void
133mach_right_send_release(mach_right_send_t s)
134{
135 kern_return_t kr = KERN_FAILURE;
136
137 kr = mach_port_mod_refs(mach_task_self(), s.mrs_name,
0a7de745 138 MACH_PORT_RIGHT_SEND, -1);
d9a64523
A
139 switch (kr) {
140 case 0:
141 break;
142 case KERN_INVALID_RIGHT:
143 kr = mach_port_mod_refs(mach_task_self(), s.mrs_name,
0a7de745 144 MACH_PORT_RIGHT_DEAD_NAME, -1);
d9a64523
A
145 _mach_assert("release dead name", kr);
146 break;
147 default:
148 _mach_assert("release send right", kr);
149 }
150}
151
152mach_right_send_once_t
153mach_right_send_once_create(mach_right_recv_t r)
154{
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,
0a7de745 158 MACH_MSG_TYPE_MAKE_SEND_ONCE, &so, &right);
d9a64523
A
159 _mach_assert("create send-once right", kr);
160
161 return mach_right_send_once(so);
162}
163
164void
165mach_right_send_once_consume(mach_right_send_once_t so)
166{
167 kern_return_t kr = KERN_FAILURE;
168
169 kr = mach_port_mod_refs(mach_task_self(), so.mrso_name,
0a7de745 170 MACH_PORT_RIGHT_SEND_ONCE, -1);
d9a64523
A
171 switch (kr) {
172 case 0:
173 break;
174 case KERN_INVALID_RIGHT:
175 kr = mach_port_mod_refs(mach_task_self(), so.mrso_name,
0a7de745 176 MACH_PORT_RIGHT_DEAD_NAME, -1);
d9a64523
A
177 _mach_assert("release dead name", kr);
178 break;
179 default:
180 _mach_assert("consume send-once right", kr);
181 }
182}