xref: /xnu-8792.41.9/tests/exc_helpers.c (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributions /*
2*5c2921b0SApple OSS Distributions  * Copyright (c) 2019 Apple Computer, Inc. All rights reserved.
3*5c2921b0SApple OSS Distributions  *
4*5c2921b0SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5c2921b0SApple OSS Distributions  *
6*5c2921b0SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5c2921b0SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5c2921b0SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5c2921b0SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5c2921b0SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5c2921b0SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5c2921b0SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5c2921b0SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5c2921b0SApple OSS Distributions  *
15*5c2921b0SApple OSS Distributions  * Please obtain a copy of the License at
16*5c2921b0SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5c2921b0SApple OSS Distributions  *
18*5c2921b0SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5c2921b0SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5c2921b0SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5c2921b0SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5c2921b0SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5c2921b0SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5c2921b0SApple OSS Distributions  * limitations under the License.
25*5c2921b0SApple OSS Distributions  *
26*5c2921b0SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5c2921b0SApple OSS Distributions  */
28*5c2921b0SApple OSS Distributions 
29*5c2921b0SApple OSS Distributions #include "exc_helpers.h"
30*5c2921b0SApple OSS Distributions 
31*5c2921b0SApple OSS Distributions #include <darwintest.h>
32*5c2921b0SApple OSS Distributions #include <ptrauth.h>
33*5c2921b0SApple OSS Distributions #include <stdbool.h>
34*5c2921b0SApple OSS Distributions #include <stdlib.h>
35*5c2921b0SApple OSS Distributions 
36*5c2921b0SApple OSS Distributions #if __arm64__
37*5c2921b0SApple OSS Distributions #define EXCEPTION_THREAD_STATE          ARM_THREAD_STATE64
38*5c2921b0SApple OSS Distributions #define EXCEPTION_THREAD_STATE_COUNT    ARM_THREAD_STATE64_COUNT
39*5c2921b0SApple OSS Distributions #elif __x86_64__
40*5c2921b0SApple OSS Distributions #define EXCEPTION_THREAD_STATE          x86_THREAD_STATE
41*5c2921b0SApple OSS Distributions #define EXCEPTION_THREAD_STATE_COUNT    x86_THREAD_STATE_COUNT
42*5c2921b0SApple OSS Distributions #else
43*5c2921b0SApple OSS Distributions #error Unsupported architecture
44*5c2921b0SApple OSS Distributions #endif
45*5c2921b0SApple OSS Distributions 
46*5c2921b0SApple OSS Distributions #define EXCEPTION_IDENTITY_PROTECTED 4
47*5c2921b0SApple OSS Distributions 
48*5c2921b0SApple OSS Distributions /**
49*5c2921b0SApple OSS Distributions  * mach_exc_server() is a MIG-generated function that verifies the message
50*5c2921b0SApple OSS Distributions  * that was received is indeed a mach exception and then calls
51*5c2921b0SApple OSS Distributions  * catch_mach_exception_raise_state() to handle the exception.
52*5c2921b0SApple OSS Distributions  */
53*5c2921b0SApple OSS Distributions extern boolean_t mach_exc_server(mach_msg_header_t *, mach_msg_header_t *);
54*5c2921b0SApple OSS Distributions 
55*5c2921b0SApple OSS Distributions extern kern_return_t
56*5c2921b0SApple OSS Distributions catch_mach_exception_raise(
57*5c2921b0SApple OSS Distributions 	mach_port_t exception_port,
58*5c2921b0SApple OSS Distributions 	mach_port_t thread,
59*5c2921b0SApple OSS Distributions 	mach_port_t task,
60*5c2921b0SApple OSS Distributions 	exception_type_t type,
61*5c2921b0SApple OSS Distributions 	exception_data_t codes,
62*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t code_count);
63*5c2921b0SApple OSS Distributions 
64*5c2921b0SApple OSS Distributions extern kern_return_t
65*5c2921b0SApple OSS Distributions catch_mach_exception_raise_identity_protected(
66*5c2921b0SApple OSS Distributions 	__unused mach_port_t      exception_port,
67*5c2921b0SApple OSS Distributions 	uint64_t                  thread_id,
68*5c2921b0SApple OSS Distributions 	mach_port_t               task_id_token,
69*5c2921b0SApple OSS Distributions 	exception_type_t          exception,
70*5c2921b0SApple OSS Distributions 	mach_exception_data_t     codes,
71*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t    codeCnt);
72*5c2921b0SApple OSS Distributions 
73*5c2921b0SApple OSS Distributions extern kern_return_t
74*5c2921b0SApple OSS Distributions catch_mach_exception_raise_backtrace(
75*5c2921b0SApple OSS Distributions 	__unused mach_port_t exception_port,
76*5c2921b0SApple OSS Distributions 	mach_port_t kcdata_object,
77*5c2921b0SApple OSS Distributions 	exception_type_t exception,
78*5c2921b0SApple OSS Distributions 	mach_exception_data_t codes,
79*5c2921b0SApple OSS Distributions 	__unused mach_msg_type_number_t codeCnt);
80*5c2921b0SApple OSS Distributions 
81*5c2921b0SApple OSS Distributions extern kern_return_t
82*5c2921b0SApple OSS Distributions catch_mach_exception_raise_state(
83*5c2921b0SApple OSS Distributions 	mach_port_t exception_port,
84*5c2921b0SApple OSS Distributions 	exception_type_t type,
85*5c2921b0SApple OSS Distributions 	exception_data_t codes,
86*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t code_count,
87*5c2921b0SApple OSS Distributions 	int *flavor,
88*5c2921b0SApple OSS Distributions 	thread_state_t in_state,
89*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t in_state_count,
90*5c2921b0SApple OSS Distributions 	thread_state_t out_state,
91*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t *out_state_count);
92*5c2921b0SApple OSS Distributions 
93*5c2921b0SApple OSS Distributions extern kern_return_t
94*5c2921b0SApple OSS Distributions catch_mach_exception_raise_state_identity(
95*5c2921b0SApple OSS Distributions 	mach_port_t exception_port,
96*5c2921b0SApple OSS Distributions 	mach_port_t thread,
97*5c2921b0SApple OSS Distributions 	mach_port_t task,
98*5c2921b0SApple OSS Distributions 	exception_type_t type,
99*5c2921b0SApple OSS Distributions 	exception_data_t codes,
100*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t code_count,
101*5c2921b0SApple OSS Distributions 	int *flavor,
102*5c2921b0SApple OSS Distributions 	thread_state_t in_state,
103*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t in_state_count,
104*5c2921b0SApple OSS Distributions 	thread_state_t out_state,
105*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t *out_state_count);
106*5c2921b0SApple OSS Distributions 
107*5c2921b0SApple OSS Distributions static exc_handler_callback_t exc_handler_callback;
108*5c2921b0SApple OSS Distributions static exc_handler_protected_callback_t exc_handler_protected_callback;
109*5c2921b0SApple OSS Distributions static exc_handler_backtrace_callback_t exc_handler_backtrace_callback;
110*5c2921b0SApple OSS Distributions 
111*5c2921b0SApple OSS Distributions /**
112*5c2921b0SApple OSS Distributions  * This has to be defined for linking purposes, but it's unused.
113*5c2921b0SApple OSS Distributions  */
114*5c2921b0SApple OSS Distributions kern_return_t
catch_mach_exception_raise(mach_port_t exception_port,mach_port_t thread,mach_port_t task,exception_type_t type,exception_data_t codes,mach_msg_type_number_t code_count)115*5c2921b0SApple OSS Distributions catch_mach_exception_raise(
116*5c2921b0SApple OSS Distributions 	mach_port_t exception_port,
117*5c2921b0SApple OSS Distributions 	mach_port_t thread,
118*5c2921b0SApple OSS Distributions 	mach_port_t task,
119*5c2921b0SApple OSS Distributions 	exception_type_t type,
120*5c2921b0SApple OSS Distributions 	exception_data_t codes,
121*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t code_count)
122*5c2921b0SApple OSS Distributions {
123*5c2921b0SApple OSS Distributions #pragma unused(exception_port, thread, task, type, codes, code_count)
124*5c2921b0SApple OSS Distributions 	T_FAIL("Triggered catch_mach_exception_raise() which shouldn't happen...");
125*5c2921b0SApple OSS Distributions 	__builtin_unreachable();
126*5c2921b0SApple OSS Distributions }
127*5c2921b0SApple OSS Distributions 
128*5c2921b0SApple OSS Distributions kern_return_t
catch_mach_exception_raise_identity_protected(__unused mach_port_t exception_port,uint64_t thread_id,mach_port_t task_id_token,exception_type_t exception,mach_exception_data_t codes,mach_msg_type_number_t codeCnt)129*5c2921b0SApple OSS Distributions catch_mach_exception_raise_identity_protected(
130*5c2921b0SApple OSS Distributions 	__unused mach_port_t      exception_port,
131*5c2921b0SApple OSS Distributions 	uint64_t                  thread_id,
132*5c2921b0SApple OSS Distributions 	mach_port_t               task_id_token,
133*5c2921b0SApple OSS Distributions 	exception_type_t          exception,
134*5c2921b0SApple OSS Distributions 	mach_exception_data_t     codes,
135*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t    codeCnt)
136*5c2921b0SApple OSS Distributions {
137*5c2921b0SApple OSS Distributions 	T_LOG("Caught a mach exception!\n");
138*5c2921b0SApple OSS Distributions 
139*5c2921b0SApple OSS Distributions 	/* There should only be two code values. */
140*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(codeCnt, 2, "Two code values were provided with the mach exception");
141*5c2921b0SApple OSS Distributions 
142*5c2921b0SApple OSS Distributions 	/**
143*5c2921b0SApple OSS Distributions 	 * The code values should be 64-bit since MACH_EXCEPTION_CODES was specified
144*5c2921b0SApple OSS Distributions 	 * when setting the exception port.
145*5c2921b0SApple OSS Distributions 	 */
146*5c2921b0SApple OSS Distributions 	mach_exception_data_t codes_64 = (mach_exception_data_t)(void *)codes;
147*5c2921b0SApple OSS Distributions 	T_LOG("Mach exception codes[0]: %#llx, codes[1]: %#llx\n", codes_64[0], codes_64[1]);
148*5c2921b0SApple OSS Distributions 
149*5c2921b0SApple OSS Distributions 	exc_handler_protected_callback(task_id_token, thread_id, exception, codes_64);
150*5c2921b0SApple OSS Distributions 
151*5c2921b0SApple OSS Distributions 	T_LOG("Assuming the thread state modification was done in the callback, skipping it");
152*5c2921b0SApple OSS Distributions 
153*5c2921b0SApple OSS Distributions 	/* Return KERN_SUCCESS to tell the kernel to keep running the victim thread. */
154*5c2921b0SApple OSS Distributions 	return KERN_SUCCESS;
155*5c2921b0SApple OSS Distributions }
156*5c2921b0SApple OSS Distributions 
157*5c2921b0SApple OSS Distributions /**
158*5c2921b0SApple OSS Distributions  * This has to be defined for linking purposes, but it's unused.
159*5c2921b0SApple OSS Distributions  */
160*5c2921b0SApple OSS Distributions kern_return_t
catch_mach_exception_raise_state(mach_port_t exception_port,exception_type_t type,exception_data_t codes,mach_msg_type_number_t code_count,int * flavor,thread_state_t in_state,mach_msg_type_number_t in_state_count,thread_state_t out_state,mach_msg_type_number_t * out_state_count)161*5c2921b0SApple OSS Distributions catch_mach_exception_raise_state(
162*5c2921b0SApple OSS Distributions 	mach_port_t exception_port,
163*5c2921b0SApple OSS Distributions 	exception_type_t type,
164*5c2921b0SApple OSS Distributions 	exception_data_t codes,
165*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t code_count,
166*5c2921b0SApple OSS Distributions 	int *flavor,
167*5c2921b0SApple OSS Distributions 	thread_state_t in_state,
168*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t in_state_count,
169*5c2921b0SApple OSS Distributions 	thread_state_t out_state,
170*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t *out_state_count)
171*5c2921b0SApple OSS Distributions {
172*5c2921b0SApple OSS Distributions #pragma unused(exception_port, type, codes, code_count, flavor, in_state, in_state_count, out_state, out_state_count)
173*5c2921b0SApple OSS Distributions 	T_FAIL("Triggered catch_mach_exception_raise_state() which shouldn't happen...");
174*5c2921b0SApple OSS Distributions 	__builtin_unreachable();
175*5c2921b0SApple OSS Distributions }
176*5c2921b0SApple OSS Distributions 
177*5c2921b0SApple OSS Distributions /**
178*5c2921b0SApple OSS Distributions  * Called by mach_exc_server() to handle the exception. This will call the
179*5c2921b0SApple OSS Distributions  * test's exception-handler callback and will then modify
180*5c2921b0SApple OSS Distributions  * the thread state to move to the next instruction.
181*5c2921b0SApple OSS Distributions  */
182*5c2921b0SApple OSS Distributions kern_return_t
catch_mach_exception_raise_state_identity(mach_port_t exception_port __unused,mach_port_t thread,mach_port_t task,exception_type_t type,exception_data_t codes,mach_msg_type_number_t code_count,int * flavor,thread_state_t in_state,mach_msg_type_number_t in_state_count,thread_state_t out_state,mach_msg_type_number_t * out_state_count)183*5c2921b0SApple OSS Distributions catch_mach_exception_raise_state_identity(
184*5c2921b0SApple OSS Distributions 	mach_port_t exception_port __unused,
185*5c2921b0SApple OSS Distributions 	mach_port_t thread,
186*5c2921b0SApple OSS Distributions 	mach_port_t task,
187*5c2921b0SApple OSS Distributions 	exception_type_t type,
188*5c2921b0SApple OSS Distributions 	exception_data_t codes,
189*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t code_count,
190*5c2921b0SApple OSS Distributions 	int *flavor,
191*5c2921b0SApple OSS Distributions 	thread_state_t in_state,
192*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t in_state_count,
193*5c2921b0SApple OSS Distributions 	thread_state_t out_state,
194*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t *out_state_count)
195*5c2921b0SApple OSS Distributions {
196*5c2921b0SApple OSS Distributions 	T_LOG("Caught a mach exception!\n");
197*5c2921b0SApple OSS Distributions 
198*5c2921b0SApple OSS Distributions 	/* There should only be two code values. */
199*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(code_count, 2, "Two code values were provided with the mach exception");
200*5c2921b0SApple OSS Distributions 
201*5c2921b0SApple OSS Distributions 	/**
202*5c2921b0SApple OSS Distributions 	 * The code values should be 64-bit since MACH_EXCEPTION_CODES was specified
203*5c2921b0SApple OSS Distributions 	 * when setting the exception port.
204*5c2921b0SApple OSS Distributions 	 */
205*5c2921b0SApple OSS Distributions 	mach_exception_data_t codes_64 = (mach_exception_data_t)(void *)codes;
206*5c2921b0SApple OSS Distributions 	T_LOG("Mach exception codes[0]: %#llx, codes[1]: %#llx\n", codes_64[0], codes_64[1]);
207*5c2921b0SApple OSS Distributions 
208*5c2921b0SApple OSS Distributions 	/* Verify that we're receiving the expected thread state flavor. */
209*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(*flavor, EXCEPTION_THREAD_STATE, "The thread state flavor is EXCEPTION_THREAD_STATE");
210*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(in_state_count, EXCEPTION_THREAD_STATE_COUNT, "The thread state count is EXCEPTION_THREAD_STATE_COUNT");
211*5c2921b0SApple OSS Distributions 
212*5c2921b0SApple OSS Distributions 	size_t advance_pc = exc_handler_callback(task, thread, type, codes_64);
213*5c2921b0SApple OSS Distributions 
214*5c2921b0SApple OSS Distributions 	/**
215*5c2921b0SApple OSS Distributions 	 * Increment the PC by the requested amount so the thread doesn't cause
216*5c2921b0SApple OSS Distributions 	 * another exception when it resumes.
217*5c2921b0SApple OSS Distributions 	 */
218*5c2921b0SApple OSS Distributions 	*out_state_count = in_state_count; /* size of state object in 32-bit words */
219*5c2921b0SApple OSS Distributions 	memcpy((void*)out_state, (void*)in_state, in_state_count * 4);
220*5c2921b0SApple OSS Distributions 
221*5c2921b0SApple OSS Distributions #if __arm64__
222*5c2921b0SApple OSS Distributions 	arm_thread_state64_t *state = (arm_thread_state64_t*)(void *)out_state;
223*5c2921b0SApple OSS Distributions 
224*5c2921b0SApple OSS Distributions 	void *pc = (void*)(arm_thread_state64_get_pc(*state) + advance_pc);
225*5c2921b0SApple OSS Distributions 	/* Have to sign the new PC value when pointer authentication is enabled. */
226*5c2921b0SApple OSS Distributions 	pc = ptrauth_sign_unauthenticated(pc, ptrauth_key_function_pointer, 0);
227*5c2921b0SApple OSS Distributions 	arm_thread_state64_set_pc_fptr(*state, pc);
228*5c2921b0SApple OSS Distributions #else
229*5c2921b0SApple OSS Distributions 	(void)advance_pc;
230*5c2921b0SApple OSS Distributions 	T_FAIL("catch_mach_exception_raise_state() not fully implemented on this architecture");
231*5c2921b0SApple OSS Distributions 	__builtin_unreachable();
232*5c2921b0SApple OSS Distributions #endif
233*5c2921b0SApple OSS Distributions 
234*5c2921b0SApple OSS Distributions 	/* Return KERN_SUCCESS to tell the kernel to keep running the victim thread. */
235*5c2921b0SApple OSS Distributions 	return KERN_SUCCESS;
236*5c2921b0SApple OSS Distributions }
237*5c2921b0SApple OSS Distributions 
238*5c2921b0SApple OSS Distributions kern_return_t
catch_mach_exception_raise_backtrace(__unused mach_port_t exception_port,mach_port_t kcdata_object,exception_type_t exception,mach_exception_data_t codes,__unused mach_msg_type_number_t codeCnt)239*5c2921b0SApple OSS Distributions catch_mach_exception_raise_backtrace(
240*5c2921b0SApple OSS Distributions 	__unused mach_port_t exception_port,
241*5c2921b0SApple OSS Distributions 	mach_port_t kcdata_object,
242*5c2921b0SApple OSS Distributions 	exception_type_t exception,
243*5c2921b0SApple OSS Distributions 	mach_exception_data_t codes,
244*5c2921b0SApple OSS Distributions 	__unused mach_msg_type_number_t codeCnt)
245*5c2921b0SApple OSS Distributions {
246*5c2921b0SApple OSS Distributions 	return exc_handler_backtrace_callback(kcdata_object, exception, codes);
247*5c2921b0SApple OSS Distributions }
248*5c2921b0SApple OSS Distributions 
249*5c2921b0SApple OSS Distributions mach_port_t
create_exception_port(exception_mask_t exception_mask)250*5c2921b0SApple OSS Distributions create_exception_port(exception_mask_t exception_mask)
251*5c2921b0SApple OSS Distributions {
252*5c2921b0SApple OSS Distributions 	return create_exception_port_behavior64(exception_mask, EXCEPTION_STATE_IDENTITY);
253*5c2921b0SApple OSS Distributions }
254*5c2921b0SApple OSS Distributions 
255*5c2921b0SApple OSS Distributions mach_port_t
create_exception_port_behavior64(exception_mask_t exception_mask,exception_behavior_t behavior)256*5c2921b0SApple OSS Distributions create_exception_port_behavior64(exception_mask_t exception_mask, exception_behavior_t behavior)
257*5c2921b0SApple OSS Distributions {
258*5c2921b0SApple OSS Distributions 	mach_port_t exc_port = MACH_PORT_NULL;
259*5c2921b0SApple OSS Distributions 	mach_port_t task = mach_task_self();
260*5c2921b0SApple OSS Distributions 	mach_port_t thread = mach_thread_self();
261*5c2921b0SApple OSS Distributions 	kern_return_t kr = KERN_SUCCESS;
262*5c2921b0SApple OSS Distributions 
263*5c2921b0SApple OSS Distributions 	if (((unsigned int)behavior & ~MACH_EXCEPTION_MASK) != EXCEPTION_STATE_IDENTITY &&
264*5c2921b0SApple OSS Distributions 	    ((unsigned int)behavior & ~MACH_EXCEPTION_MASK) != EXCEPTION_IDENTITY_PROTECTED) {
265*5c2921b0SApple OSS Distributions 		T_FAIL("Passed behavior (%d) is not supported by exc_helpers.", behavior);
266*5c2921b0SApple OSS Distributions 	}
267*5c2921b0SApple OSS Distributions 
268*5c2921b0SApple OSS Distributions 	behavior |= MACH_EXCEPTION_CODES;
269*5c2921b0SApple OSS Distributions 
270*5c2921b0SApple OSS Distributions 	/* Create the mach port the exception messages will be sent to. */
271*5c2921b0SApple OSS Distributions 	kr = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &exc_port);
272*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Allocated mach exception port");
273*5c2921b0SApple OSS Distributions 
274*5c2921b0SApple OSS Distributions 	/**
275*5c2921b0SApple OSS Distributions 	 * Insert a send right into the exception port that the kernel will use to
276*5c2921b0SApple OSS Distributions 	 * send the exception thread the exception messages.
277*5c2921b0SApple OSS Distributions 	 */
278*5c2921b0SApple OSS Distributions 	kr = mach_port_insert_right(task, exc_port, exc_port, MACH_MSG_TYPE_MAKE_SEND);
279*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Inserted a SEND right into the exception port");
280*5c2921b0SApple OSS Distributions 
281*5c2921b0SApple OSS Distributions 	/* Tell the kernel what port to send exceptions to. */
282*5c2921b0SApple OSS Distributions 	kr = thread_set_exception_ports(
283*5c2921b0SApple OSS Distributions 		thread,
284*5c2921b0SApple OSS Distributions 		exception_mask,
285*5c2921b0SApple OSS Distributions 		exc_port,
286*5c2921b0SApple OSS Distributions 		(exception_behavior_t)((unsigned int)behavior),
287*5c2921b0SApple OSS Distributions 		EXCEPTION_THREAD_STATE);
288*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Set the exception port to my custom handler");
289*5c2921b0SApple OSS Distributions 
290*5c2921b0SApple OSS Distributions 	return exc_port;
291*5c2921b0SApple OSS Distributions }
292*5c2921b0SApple OSS Distributions 
293*5c2921b0SApple OSS Distributions struct thread_params {
294*5c2921b0SApple OSS Distributions 	mach_port_t exc_port;
295*5c2921b0SApple OSS Distributions 	bool run_once;
296*5c2921b0SApple OSS Distributions };
297*5c2921b0SApple OSS Distributions 
298*5c2921b0SApple OSS Distributions /**
299*5c2921b0SApple OSS Distributions  * Thread to handle the mach exception.
300*5c2921b0SApple OSS Distributions  *
301*5c2921b0SApple OSS Distributions  * @param arg The exception port to wait for a message on.
302*5c2921b0SApple OSS Distributions  */
303*5c2921b0SApple OSS Distributions static void *
exc_server_thread(void * arg)304*5c2921b0SApple OSS Distributions exc_server_thread(void *arg)
305*5c2921b0SApple OSS Distributions {
306*5c2921b0SApple OSS Distributions 	struct thread_params *params = arg;
307*5c2921b0SApple OSS Distributions 	mach_port_t exc_port = params->exc_port;
308*5c2921b0SApple OSS Distributions 	bool run_once = params->run_once;
309*5c2921b0SApple OSS Distributions 	free(params);
310*5c2921b0SApple OSS Distributions 
311*5c2921b0SApple OSS Distributions 	/**
312*5c2921b0SApple OSS Distributions 	 * mach_msg_server_once is a helper function provided by libsyscall that
313*5c2921b0SApple OSS Distributions 	 * handles creating mach messages, blocks waiting for a message on the
314*5c2921b0SApple OSS Distributions 	 * exception port, calls mach_exc_server() to handle the exception, and
315*5c2921b0SApple OSS Distributions 	 * sends a reply based on the return value of mach_exc_server().
316*5c2921b0SApple OSS Distributions 	 */
317*5c2921b0SApple OSS Distributions #define MACH_MSG_REPLY_SIZE 4096
318*5c2921b0SApple OSS Distributions 	kern_return_t kr;
319*5c2921b0SApple OSS Distributions 	if (run_once) {
320*5c2921b0SApple OSS Distributions 		kr = mach_msg_server_once(mach_exc_server, MACH_MSG_REPLY_SIZE, exc_port, 0);
321*5c2921b0SApple OSS Distributions 	} else {
322*5c2921b0SApple OSS Distributions 		kr = mach_msg_server(mach_exc_server, MACH_MSG_REPLY_SIZE, exc_port, 0);
323*5c2921b0SApple OSS Distributions 	}
324*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Received mach exception message");
325*5c2921b0SApple OSS Distributions 
326*5c2921b0SApple OSS Distributions 	pthread_exit((void*)0);
327*5c2921b0SApple OSS Distributions 	__builtin_unreachable();
328*5c2921b0SApple OSS Distributions }
329*5c2921b0SApple OSS Distributions 
330*5c2921b0SApple OSS Distributions static void
_run_exception_handler(mach_port_t exc_port,void * preferred_callback,void * callback,bool run_once,exception_behavior_t behavior)331*5c2921b0SApple OSS Distributions _run_exception_handler(mach_port_t exc_port, void *preferred_callback, void *callback, bool run_once, exception_behavior_t behavior)
332*5c2921b0SApple OSS Distributions {
333*5c2921b0SApple OSS Distributions 	if (behavior & MACH_EXCEPTION_BACKTRACE_PREFERRED) {
334*5c2921b0SApple OSS Distributions 		T_QUIET; T_ASSERT_NE(NULL, preferred_callback, "Require a preferred callback");
335*5c2921b0SApple OSS Distributions 		exc_handler_backtrace_callback = (exc_handler_backtrace_callback_t)preferred_callback;
336*5c2921b0SApple OSS Distributions 	}
337*5c2921b0SApple OSS Distributions 
338*5c2921b0SApple OSS Distributions 	behavior &= ~MACH_EXCEPTION_MASK;
339*5c2921b0SApple OSS Distributions 
340*5c2921b0SApple OSS Distributions 	switch (behavior) {
341*5c2921b0SApple OSS Distributions 	case EXCEPTION_STATE_IDENTITY:
342*5c2921b0SApple OSS Distributions 		exc_handler_callback = (exc_handler_callback_t)callback;
343*5c2921b0SApple OSS Distributions 		break;
344*5c2921b0SApple OSS Distributions 	case EXCEPTION_IDENTITY_PROTECTED:
345*5c2921b0SApple OSS Distributions 		exc_handler_protected_callback = (exc_handler_protected_callback_t)callback;
346*5c2921b0SApple OSS Distributions 		break;
347*5c2921b0SApple OSS Distributions 	default:
348*5c2921b0SApple OSS Distributions 		T_FAIL("Unsupported behavior");
349*5c2921b0SApple OSS Distributions 		break;
350*5c2921b0SApple OSS Distributions 	}
351*5c2921b0SApple OSS Distributions 
352*5c2921b0SApple OSS Distributions 	pthread_t exc_thread;
353*5c2921b0SApple OSS Distributions 
354*5c2921b0SApple OSS Distributions 	/* Spawn the exception server's thread. */
355*5c2921b0SApple OSS Distributions 	struct thread_params *params = malloc(sizeof(*params));
356*5c2921b0SApple OSS Distributions 	params->exc_port = exc_port;
357*5c2921b0SApple OSS Distributions 	params->run_once = run_once;
358*5c2921b0SApple OSS Distributions 	int err = pthread_create(&exc_thread, (pthread_attr_t*)0, exc_server_thread, params);
359*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(err, "Spawned exception server thread");
360*5c2921b0SApple OSS Distributions 
361*5c2921b0SApple OSS Distributions 	/* No need to wait for the exception server to be joined when it exits. */
362*5c2921b0SApple OSS Distributions 	pthread_detach(exc_thread);
363*5c2921b0SApple OSS Distributions }
364*5c2921b0SApple OSS Distributions 
365*5c2921b0SApple OSS Distributions void
run_exception_handler(mach_port_t exc_port,exc_handler_callback_t callback)366*5c2921b0SApple OSS Distributions run_exception_handler(mach_port_t exc_port, exc_handler_callback_t callback)
367*5c2921b0SApple OSS Distributions {
368*5c2921b0SApple OSS Distributions 	run_exception_handler_behavior64(exc_port, NULL, (void *)callback, EXCEPTION_STATE_IDENTITY);
369*5c2921b0SApple OSS Distributions }
370*5c2921b0SApple OSS Distributions 
371*5c2921b0SApple OSS Distributions void
run_exception_handler_behavior64(mach_port_t exc_port,void * preferred_callback,void * callback,exception_behavior_t behavior)372*5c2921b0SApple OSS Distributions run_exception_handler_behavior64(mach_port_t exc_port, void *preferred_callback,
373*5c2921b0SApple OSS Distributions     void *callback, exception_behavior_t behavior)
374*5c2921b0SApple OSS Distributions {
375*5c2921b0SApple OSS Distributions 	if (((unsigned int)behavior & ~MACH_EXCEPTION_MASK) != EXCEPTION_STATE_IDENTITY &&
376*5c2921b0SApple OSS Distributions 	    ((unsigned int)behavior & ~MACH_EXCEPTION_MASK) != EXCEPTION_IDENTITY_PROTECTED) {
377*5c2921b0SApple OSS Distributions 		T_FAIL("Passed behavior (%d) is not supported by exc_helpers.", behavior);
378*5c2921b0SApple OSS Distributions 	}
379*5c2921b0SApple OSS Distributions 
380*5c2921b0SApple OSS Distributions 	_run_exception_handler(exc_port, (void *)preferred_callback, (void *)callback, true, behavior);
381*5c2921b0SApple OSS Distributions }
382*5c2921b0SApple OSS Distributions 
383*5c2921b0SApple OSS Distributions void
repeat_exception_handler(mach_port_t exc_port,exc_handler_callback_t callback)384*5c2921b0SApple OSS Distributions repeat_exception_handler(mach_port_t exc_port, exc_handler_callback_t callback)
385*5c2921b0SApple OSS Distributions {
386*5c2921b0SApple OSS Distributions 	_run_exception_handler(exc_port, NULL, (void *)callback, false, EXCEPTION_STATE_IDENTITY);
387*5c2921b0SApple OSS Distributions }
388