1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions * Copyright (c) 2024 Apple Inc. All rights reserved.
3*bbb1b6f9SApple OSS Distributions *
4*bbb1b6f9SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions *
6*bbb1b6f9SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions *
15*bbb1b6f9SApple OSS Distributions * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions *
18*bbb1b6f9SApple OSS Distributions * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions * limitations under the License.
25*bbb1b6f9SApple OSS Distributions *
26*bbb1b6f9SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions */
28*bbb1b6f9SApple OSS Distributions
29*bbb1b6f9SApple OSS Distributions #include <stdlib.h>
30*bbb1b6f9SApple OSS Distributions #include <unistd.h>
31*bbb1b6f9SApple OSS Distributions #include <stdbool.h>
32*bbb1b6f9SApple OSS Distributions #include <pthread.h>
33*bbb1b6f9SApple OSS Distributions #include <darwintest.h>
34*bbb1b6f9SApple OSS Distributions #include <kern/exc_guard.h>
35*bbb1b6f9SApple OSS Distributions #include <mach/task_info.h>
36*bbb1b6f9SApple OSS Distributions
37*bbb1b6f9SApple OSS Distributions #include "exc_helpers.h"
38*bbb1b6f9SApple OSS Distributions #include "exc_guard_helper.h"
39*bbb1b6f9SApple OSS Distributions #include "test_utils.h"
40*bbb1b6f9SApple OSS Distributions
41*bbb1b6f9SApple OSS Distributions /* Convenience macro for compile-time array size */
42*bbb1b6f9SApple OSS Distributions #define countof(array) \
43*bbb1b6f9SApple OSS Distributions _Pragma("clang diagnostic push") \
44*bbb1b6f9SApple OSS Distributions _Pragma("clang diagnostic error \"-Wsizeof-pointer-div\"") \
45*bbb1b6f9SApple OSS Distributions (sizeof(array)/sizeof((array)[0])) \
46*bbb1b6f9SApple OSS Distributions _Pragma("clang diagnostic pop")
47*bbb1b6f9SApple OSS Distributions
48*bbb1b6f9SApple OSS Distributions /*
49*bbb1b6f9SApple OSS Distributions * Global data shared between the code running the block and the exception handler.
50*bbb1b6f9SApple OSS Distributions * Ideally this would be thread-local data in the thread running the block,
51*bbb1b6f9SApple OSS Distributions * but the exception handler runs on a different thread and can't see it.
52*bbb1b6f9SApple OSS Distributions */
53*bbb1b6f9SApple OSS Distributions static pthread_mutex_t exc_guard_helper_mutex = PTHREAD_MUTEX_INITIALIZER;
54*bbb1b6f9SApple OSS Distributions static mach_port_t exc_guard_helper_exc_port = MACH_PORT_NULL;
55*bbb1b6f9SApple OSS Distributions
56*bbb1b6f9SApple OSS Distributions static pthread_mutex_t exc_guard_helper_request_mutex = PTHREAD_MUTEX_INITIALIZER;
57*bbb1b6f9SApple OSS Distributions static exc_guard_helper_info_t exc_guard_helper_reply;
58*bbb1b6f9SApple OSS Distributions static struct {
59*bbb1b6f9SApple OSS Distributions mach_port_t thread;
60*bbb1b6f9SApple OSS Distributions unsigned int guard_type;
61*bbb1b6f9SApple OSS Distributions } exc_guard_helper_request;
62*bbb1b6f9SApple OSS Distributions
63*bbb1b6f9SApple OSS Distributions static const char *
name_for_guard_type(unsigned guard_type)64*bbb1b6f9SApple OSS Distributions name_for_guard_type(unsigned guard_type)
65*bbb1b6f9SApple OSS Distributions {
66*bbb1b6f9SApple OSS Distributions static const char *names[] = {
67*bbb1b6f9SApple OSS Distributions [GUARD_TYPE_NONE] = "GUARD_TYPE_NONE",
68*bbb1b6f9SApple OSS Distributions [GUARD_TYPE_MACH_PORT] = "GUARD_TYPE_MACH_PORT",
69*bbb1b6f9SApple OSS Distributions [GUARD_TYPE_FD] = "GUARD_TYPE_FD",
70*bbb1b6f9SApple OSS Distributions [GUARD_TYPE_USER] = "GUARD_TYPE_USER",
71*bbb1b6f9SApple OSS Distributions [GUARD_TYPE_VN] = "GUARD_TYPE_VN",
72*bbb1b6f9SApple OSS Distributions [GUARD_TYPE_VIRT_MEMORY] = "GUARD_TYPE_VIRT_MEMORY",
73*bbb1b6f9SApple OSS Distributions [GUARD_TYPE_REJECTED_SC] = "GUARD_TYPE_REJECTED_SC",
74*bbb1b6f9SApple OSS Distributions };
75*bbb1b6f9SApple OSS Distributions const char *result = NULL;
76*bbb1b6f9SApple OSS Distributions if (guard_type < countof(names)) {
77*bbb1b6f9SApple OSS Distributions result = names[guard_type];
78*bbb1b6f9SApple OSS Distributions }
79*bbb1b6f9SApple OSS Distributions if (result == NULL) {
80*bbb1b6f9SApple OSS Distributions result = "unknown";
81*bbb1b6f9SApple OSS Distributions }
82*bbb1b6f9SApple OSS Distributions return result;
83*bbb1b6f9SApple OSS Distributions }
84*bbb1b6f9SApple OSS Distributions
85*bbb1b6f9SApple OSS Distributions static size_t
exc_guard_helper_exception_handler(__unused mach_port_t task,mach_port_t thread,exception_type_t exception,mach_exception_data_t codes,__unused uint64_t exception_pc)86*bbb1b6f9SApple OSS Distributions exc_guard_helper_exception_handler(
87*bbb1b6f9SApple OSS Distributions __unused mach_port_t task,
88*bbb1b6f9SApple OSS Distributions mach_port_t thread,
89*bbb1b6f9SApple OSS Distributions exception_type_t exception,
90*bbb1b6f9SApple OSS Distributions mach_exception_data_t codes,
91*bbb1b6f9SApple OSS Distributions __unused uint64_t exception_pc)
92*bbb1b6f9SApple OSS Distributions {
93*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_EQ(exception, EXC_GUARD, "exception type");
94*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_mutex_lock(&exc_guard_helper_request_mutex), "lock");
95*bbb1b6f9SApple OSS Distributions
96*bbb1b6f9SApple OSS Distributions if (thread != exc_guard_helper_request.thread) {
97*bbb1b6f9SApple OSS Distributions /* reject, nobody is waiting for exceptions */
98*bbb1b6f9SApple OSS Distributions if (verbose_exc_helper) {
99*bbb1b6f9SApple OSS Distributions T_LOG("exc_guard_helper caught an exception but nobody is waiting for it");
100*bbb1b6f9SApple OSS Distributions }
101*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_mutex_unlock(&exc_guard_helper_request_mutex), "unlock");
102*bbb1b6f9SApple OSS Distributions return 0;
103*bbb1b6f9SApple OSS Distributions }
104*bbb1b6f9SApple OSS Distributions
105*bbb1b6f9SApple OSS Distributions unsigned int exc_guard_type = EXC_GUARD_DECODE_GUARD_TYPE(codes[0]);
106*bbb1b6f9SApple OSS Distributions uint32_t exc_guard_flavor = EXC_GUARD_DECODE_GUARD_FLAVOR(codes[0]);
107*bbb1b6f9SApple OSS Distributions uint32_t exc_guard_target = EXC_GUARD_DECODE_GUARD_TARGET(codes[0]);
108*bbb1b6f9SApple OSS Distributions uint64_t exc_guard_payload = codes[1];
109*bbb1b6f9SApple OSS Distributions
110*bbb1b6f9SApple OSS Distributions if (exc_guard_helper_request.guard_type == exc_guard_type) {
111*bbb1b6f9SApple OSS Distributions /* okay, exception matches caller's requested guard type */
112*bbb1b6f9SApple OSS Distributions } else {
113*bbb1b6f9SApple OSS Distributions /* reject, exception's guard type is not of the requested type */
114*bbb1b6f9SApple OSS Distributions if (verbose_exc_helper) {
115*bbb1b6f9SApple OSS Distributions T_LOG("exc_guard_helper exception is not of the "
116*bbb1b6f9SApple OSS Distributions "desired guard type (expected %u, got %u)",
117*bbb1b6f9SApple OSS Distributions exc_guard_helper_request.guard_type, exc_guard_type);
118*bbb1b6f9SApple OSS Distributions }
119*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_mutex_unlock(&exc_guard_helper_request_mutex), "unlock");
120*bbb1b6f9SApple OSS Distributions return 0;
121*bbb1b6f9SApple OSS Distributions }
122*bbb1b6f9SApple OSS Distributions
123*bbb1b6f9SApple OSS Distributions if (++exc_guard_helper_reply.catch_count == 1) {
124*bbb1b6f9SApple OSS Distributions /* save the details of the first caught exception */
125*bbb1b6f9SApple OSS Distributions exc_guard_helper_reply.guard_type = exc_guard_type;
126*bbb1b6f9SApple OSS Distributions exc_guard_helper_reply.guard_flavor = exc_guard_flavor;
127*bbb1b6f9SApple OSS Distributions exc_guard_helper_reply.guard_target = exc_guard_target;
128*bbb1b6f9SApple OSS Distributions exc_guard_helper_reply.guard_payload = exc_guard_payload;
129*bbb1b6f9SApple OSS Distributions }
130*bbb1b6f9SApple OSS Distributions
131*bbb1b6f9SApple OSS Distributions if (verbose_exc_helper) {
132*bbb1b6f9SApple OSS Distributions T_LOG("exc_guard_helper caught EXC_GUARD type %u (%s), flavor %u, "
133*bbb1b6f9SApple OSS Distributions "target %u, payload 0x%llx (catch #%u in the block)",
134*bbb1b6f9SApple OSS Distributions exc_guard_type, name_for_guard_type(exc_guard_type),
135*bbb1b6f9SApple OSS Distributions exc_guard_flavor, exc_guard_target, exc_guard_payload,
136*bbb1b6f9SApple OSS Distributions exc_guard_helper_reply.catch_count);
137*bbb1b6f9SApple OSS Distributions }
138*bbb1b6f9SApple OSS Distributions
139*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_mutex_unlock(&exc_guard_helper_request_mutex), "unlock");
140*bbb1b6f9SApple OSS Distributions return 0;
141*bbb1b6f9SApple OSS Distributions }
142*bbb1b6f9SApple OSS Distributions
143*bbb1b6f9SApple OSS Distributions /*
144*bbb1b6f9SApple OSS Distributions * Set up our exception handlers if they are not already configured.
145*bbb1b6f9SApple OSS Distributions * exc_guard_helper_mutex must be held by the caller.
146*bbb1b6f9SApple OSS Distributions */
147*bbb1b6f9SApple OSS Distributions static void
initialize_exception_handlers(void)148*bbb1b6f9SApple OSS Distributions initialize_exception_handlers(void)
149*bbb1b6f9SApple OSS Distributions {
150*bbb1b6f9SApple OSS Distributions if (exc_guard_helper_exc_port == MACH_PORT_NULL) {
151*bbb1b6f9SApple OSS Distributions exc_guard_helper_exc_port = create_exception_port(EXC_MASK_GUARD);
152*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_NE(exc_guard_helper_exc_port, MACH_PORT_NULL, "exception port");
153*bbb1b6f9SApple OSS Distributions repeat_exception_handler(exc_guard_helper_exc_port, exc_guard_helper_exception_handler);
154*bbb1b6f9SApple OSS Distributions if (verbose_exc_helper) {
155*bbb1b6f9SApple OSS Distributions T_LOG("exc_guard_helper exception handlers installed");
156*bbb1b6f9SApple OSS Distributions }
157*bbb1b6f9SApple OSS Distributions }
158*bbb1b6f9SApple OSS Distributions }
159*bbb1b6f9SApple OSS Distributions
160*bbb1b6f9SApple OSS Distributions void
exc_guard_helper_init(void)161*bbb1b6f9SApple OSS Distributions exc_guard_helper_init(void)
162*bbb1b6f9SApple OSS Distributions {
163*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_mutex_lock(&exc_guard_helper_mutex), "lock");
164*bbb1b6f9SApple OSS Distributions initialize_exception_handlers();
165*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_mutex_unlock(&exc_guard_helper_mutex), "unlock");
166*bbb1b6f9SApple OSS Distributions }
167*bbb1b6f9SApple OSS Distributions
168*bbb1b6f9SApple OSS Distributions
169*bbb1b6f9SApple OSS Distributions /*
170*bbb1b6f9SApple OSS Distributions * Return EXC_GUARD behavior flags that enable guard_type (non-fatal)
171*bbb1b6f9SApple OSS Distributions * and leave all other behaviors in old_behavior unchanged.
172*bbb1b6f9SApple OSS Distributions */
173*bbb1b6f9SApple OSS Distributions static task_exc_guard_behavior_t
configure_exc_guard_of_type(unsigned int guard_type,task_exc_guard_behavior_t old_behavior)174*bbb1b6f9SApple OSS Distributions configure_exc_guard_of_type(
175*bbb1b6f9SApple OSS Distributions unsigned int guard_type,
176*bbb1b6f9SApple OSS Distributions task_exc_guard_behavior_t old_behavior)
177*bbb1b6f9SApple OSS Distributions {
178*bbb1b6f9SApple OSS Distributions /*
179*bbb1b6f9SApple OSS Distributions * Behavior flags for all known EXC_GUARD types.
180*bbb1b6f9SApple OSS Distributions * These flags are defined in mach/task_info.h.
181*bbb1b6f9SApple OSS Distributions * Some guard types cannot be configured and do not have these flags.
182*bbb1b6f9SApple OSS Distributions */
183*bbb1b6f9SApple OSS Distributions static const struct {
184*bbb1b6f9SApple OSS Distributions task_exc_guard_behavior_t set;
185*bbb1b6f9SApple OSS Distributions task_exc_guard_behavior_t clear;
186*bbb1b6f9SApple OSS Distributions } behavior_flags[] = {
187*bbb1b6f9SApple OSS Distributions [GUARD_TYPE_VIRT_MEMORY] = {
188*bbb1b6f9SApple OSS Distributions .clear = TASK_EXC_GUARD_VM_ALL,
189*bbb1b6f9SApple OSS Distributions .set = TASK_EXC_GUARD_VM_DELIVER,
190*bbb1b6f9SApple OSS Distributions },
191*bbb1b6f9SApple OSS Distributions [GUARD_TYPE_MACH_PORT] = {
192*bbb1b6f9SApple OSS Distributions .clear = TASK_EXC_GUARD_MP_ALL,
193*bbb1b6f9SApple OSS Distributions .set = TASK_EXC_GUARD_MP_DELIVER,
194*bbb1b6f9SApple OSS Distributions },
195*bbb1b6f9SApple OSS Distributions };
196*bbb1b6f9SApple OSS Distributions
197*bbb1b6f9SApple OSS Distributions /* Reject guard types not present in behavior_flags[]. */
198*bbb1b6f9SApple OSS Distributions if (guard_type >= countof(behavior_flags)) {
199*bbb1b6f9SApple OSS Distributions goto unimplemented_guard_type;
200*bbb1b6f9SApple OSS Distributions }
201*bbb1b6f9SApple OSS Distributions if (behavior_flags[guard_type].set == 0 &&
202*bbb1b6f9SApple OSS Distributions behavior_flags[guard_type].clear == 0) {
203*bbb1b6f9SApple OSS Distributions goto unimplemented_guard_type;
204*bbb1b6f9SApple OSS Distributions }
205*bbb1b6f9SApple OSS Distributions
206*bbb1b6f9SApple OSS Distributions /* Set and clear behavior flags for the requested guard type(s). */
207*bbb1b6f9SApple OSS Distributions task_exc_guard_behavior_t new_behavior = old_behavior;
208*bbb1b6f9SApple OSS Distributions new_behavior &= ~behavior_flags[guard_type].clear;
209*bbb1b6f9SApple OSS Distributions new_behavior |= behavior_flags[guard_type].set;
210*bbb1b6f9SApple OSS Distributions return new_behavior;
211*bbb1b6f9SApple OSS Distributions
212*bbb1b6f9SApple OSS Distributions unimplemented_guard_type:
213*bbb1b6f9SApple OSS Distributions /*
214*bbb1b6f9SApple OSS Distributions * No behavior_flags[] entry for this EXC_GUARD guard type.
215*bbb1b6f9SApple OSS Distributions * If task_set_exc_guard_behavior() can configure your new
216*bbb1b6f9SApple OSS Distributions * guard type then add it to behavior_flags[] above.
217*bbb1b6f9SApple OSS Distributions */
218*bbb1b6f9SApple OSS Distributions T_FAIL("guard type %u (%s) is unimplemented in exc_guard_helper",
219*bbb1b6f9SApple OSS Distributions guard_type, name_for_guard_type(guard_type));
220*bbb1b6f9SApple OSS Distributions T_END;
221*bbb1b6f9SApple OSS Distributions }
222*bbb1b6f9SApple OSS Distributions
223*bbb1b6f9SApple OSS Distributions task_exc_guard_behavior_t
enable_exc_guard_of_type(unsigned int guard_type)224*bbb1b6f9SApple OSS Distributions enable_exc_guard_of_type(unsigned int guard_type)
225*bbb1b6f9SApple OSS Distributions {
226*bbb1b6f9SApple OSS Distributions kern_return_t kr;
227*bbb1b6f9SApple OSS Distributions task_exc_guard_behavior_t old_behavior, new_behavior;
228*bbb1b6f9SApple OSS Distributions
229*bbb1b6f9SApple OSS Distributions kr = task_get_exc_guard_behavior(mach_task_self(), &old_behavior);
230*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "exc_guard_helper calling task_get_exc_guard_behavior");
231*bbb1b6f9SApple OSS Distributions
232*bbb1b6f9SApple OSS Distributions new_behavior = configure_exc_guard_of_type(guard_type, old_behavior);
233*bbb1b6f9SApple OSS Distributions
234*bbb1b6f9SApple OSS Distributions kr = task_set_exc_guard_behavior(mach_task_self(), new_behavior);
235*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr,
236*bbb1b6f9SApple OSS Distributions "exc_guard_helper calling task_set_exc_guard_behavior to enable guard type %u %s",
237*bbb1b6f9SApple OSS Distributions guard_type, name_for_guard_type(guard_type));
238*bbb1b6f9SApple OSS Distributions
239*bbb1b6f9SApple OSS Distributions return old_behavior;
240*bbb1b6f9SApple OSS Distributions }
241*bbb1b6f9SApple OSS Distributions
242*bbb1b6f9SApple OSS Distributions bool
block_raised_exc_guard_of_type(unsigned int guard_type,exc_guard_helper_info_t * const out_exc_info,exc_guard_helper_block_t block)243*bbb1b6f9SApple OSS Distributions block_raised_exc_guard_of_type(
244*bbb1b6f9SApple OSS Distributions unsigned int guard_type,
245*bbb1b6f9SApple OSS Distributions exc_guard_helper_info_t * const out_exc_info,
246*bbb1b6f9SApple OSS Distributions exc_guard_helper_block_t block)
247*bbb1b6f9SApple OSS Distributions {
248*bbb1b6f9SApple OSS Distributions if (process_is_translated() && guard_type == GUARD_TYPE_VIRT_MEMORY) {
249*bbb1b6f9SApple OSS Distributions T_FAIL("block_raised_exc_guard_of_type(GUARD_TYPE_VIRT_MEMORY) "
250*bbb1b6f9SApple OSS Distributions "does not work on translation/Rosetta (rdar://142438840)");
251*bbb1b6f9SApple OSS Distributions }
252*bbb1b6f9SApple OSS Distributions
253*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_mutex_lock(&exc_guard_helper_mutex), "lock");
254*bbb1b6f9SApple OSS Distributions initialize_exception_handlers();
255*bbb1b6f9SApple OSS Distributions
256*bbb1b6f9SApple OSS Distributions /* lock the request and reply structs against the exception handler */
257*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_mutex_lock(&exc_guard_helper_request_mutex), "lock");
258*bbb1b6f9SApple OSS Distributions
259*bbb1b6f9SApple OSS Distributions /* prepare the global request and reply struct contents */
260*bbb1b6f9SApple OSS Distributions memset(&exc_guard_helper_request, 0, sizeof(exc_guard_helper_request));
261*bbb1b6f9SApple OSS Distributions memset(&exc_guard_helper_reply, 0, sizeof(exc_guard_helper_reply));
262*bbb1b6f9SApple OSS Distributions exc_guard_helper_request.thread = mach_thread_self();
263*bbb1b6f9SApple OSS Distributions exc_guard_helper_request.guard_type = guard_type;
264*bbb1b6f9SApple OSS Distributions
265*bbb1b6f9SApple OSS Distributions /* unlock the request and reply structs so the exception handler can use them */
266*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_mutex_unlock(&exc_guard_helper_request_mutex), "unlock");
267*bbb1b6f9SApple OSS Distributions
268*bbb1b6f9SApple OSS Distributions /* run the caller's block */
269*bbb1b6f9SApple OSS Distributions if (verbose_exc_helper) {
270*bbb1b6f9SApple OSS Distributions T_LOG("exc_guard_helper calling a block");
271*bbb1b6f9SApple OSS Distributions }
272*bbb1b6f9SApple OSS Distributions block();
273*bbb1b6f9SApple OSS Distributions if (verbose_exc_helper) {
274*bbb1b6f9SApple OSS Distributions T_LOG("exc_guard_helper finished a block, %u exception%s caught",
275*bbb1b6f9SApple OSS Distributions exc_guard_helper_reply.catch_count,
276*bbb1b6f9SApple OSS Distributions exc_guard_helper_reply.catch_count == 1 ? "" : "s");
277*bbb1b6f9SApple OSS Distributions }
278*bbb1b6f9SApple OSS Distributions
279*bbb1b6f9SApple OSS Distributions /* lock the request and reply structs again */
280*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_mutex_unlock(&exc_guard_helper_request_mutex), "lock");
281*bbb1b6f9SApple OSS Distributions
282*bbb1b6f9SApple OSS Distributions /* read the reply from the exception handler */
283*bbb1b6f9SApple OSS Distributions bool result = exc_guard_helper_reply.catch_count > 0;
284*bbb1b6f9SApple OSS Distributions memcpy(out_exc_info, &exc_guard_helper_reply, sizeof(exc_guard_helper_reply));
285*bbb1b6f9SApple OSS Distributions
286*bbb1b6f9SApple OSS Distributions /* clear the request and reply before unlocking everything */
287*bbb1b6f9SApple OSS Distributions memset(&exc_guard_helper_request, 0, sizeof(exc_guard_helper_request));
288*bbb1b6f9SApple OSS Distributions memset(&exc_guard_helper_reply, 0, sizeof(exc_guard_helper_reply));
289*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_mutex_unlock(&exc_guard_helper_request_mutex), "unlock");
290*bbb1b6f9SApple OSS Distributions
291*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_mutex_unlock(&exc_guard_helper_mutex), "unlock");
292*bbb1b6f9SApple OSS Distributions
293*bbb1b6f9SApple OSS Distributions return result;
294*bbb1b6f9SApple OSS Distributions }
295*bbb1b6f9SApple OSS Distributions
296*bbb1b6f9SApple OSS Distributions bool
block_raised_exc_guard_of_type_ignoring_translated(unsigned int guard_type,exc_guard_helper_info_t * const out_exc_info,exc_guard_helper_block_t block)297*bbb1b6f9SApple OSS Distributions block_raised_exc_guard_of_type_ignoring_translated(
298*bbb1b6f9SApple OSS Distributions unsigned int guard_type,
299*bbb1b6f9SApple OSS Distributions exc_guard_helper_info_t * const out_exc_info,
300*bbb1b6f9SApple OSS Distributions exc_guard_helper_block_t block)
301*bbb1b6f9SApple OSS Distributions {
302*bbb1b6f9SApple OSS Distributions if (process_is_translated() && guard_type == GUARD_TYPE_VIRT_MEMORY) {
303*bbb1b6f9SApple OSS Distributions /* Rosetta can't recover from guard exceptions of GUARD_TYPE_VIRT_MEMORY */
304*bbb1b6f9SApple OSS Distributions T_LOG("note: exc_guard_helper calling a block with no exception "
305*bbb1b6f9SApple OSS Distributions "handler due to translation/Rosetta (rdar://142438840)");
306*bbb1b6f9SApple OSS Distributions block();
307*bbb1b6f9SApple OSS Distributions memset(out_exc_info, 0, sizeof(*out_exc_info));
308*bbb1b6f9SApple OSS Distributions return false;
309*bbb1b6f9SApple OSS Distributions }
310*bbb1b6f9SApple OSS Distributions
311*bbb1b6f9SApple OSS Distributions return block_raised_exc_guard_of_type(guard_type, out_exc_info, block);
312*bbb1b6f9SApple OSS Distributions }
313