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