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