1 /* 2 * Copyright (c) 2019 Apple Computer, Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28 29 #ifndef EXC_HELPERS_H 30 #define EXC_HELPERS_H 31 32 #include <mach/mach.h> 33 #include <mach/exception.h> 34 #include <stdbool.h> 35 #include <stdint.h> 36 #include <mach/thread_status.h> 37 38 /** 39 * Set verbose_exc_helper = true to log exception information with T_LOG(). 40 * The default is true. 41 */ 42 extern bool verbose_exc_helper; 43 44 /** 45 * Callback invoked by run_exception_handler() when a Mach exception is 46 * received. 47 * 48 * @param task the task causing the exception 49 * @param thread the task causing the exception 50 * @param type exception type received from the kernel 51 * @param codes exception codes received from the kernel 52 * @param pc the (ptrauth-stripped) program counter of the exception 53 * 54 * @return how much the exception handler should advance the program 55 * counter, in bytes (in order to move past the code causing the 56 * exception); OR the special value EXC_HELPER_HALT to 57 * let the process crash instead of continuing. 58 */ 59 typedef size_t (*exc_handler_callback_t)(mach_port_t task, mach_port_t thread, 60 exception_type_t type, mach_exception_data_t codes, uint64_t pc); 61 62 typedef size_t (*exc_handler_protected_callback_t)(task_id_token_t token, uint64_t thread_d, 63 exception_type_t type, mach_exception_data_t codes); 64 65 typedef size_t (*exc_handler_state_protected_callback_t)(task_id_token_t token, uint64_t thread_d, 66 exception_type_t type, mach_exception_data_t codes, thread_state_t in_state, 67 mach_msg_type_number_t in_state_count, thread_state_t out_state, mach_msg_type_number_t *out_state_count); 68 69 typedef kern_return_t (*exc_handler_backtrace_callback_t)(kcdata_object_t kcdata_object, 70 exception_type_t type, mach_exception_data_t codes); 71 72 #define EXC_HELPER_HALT ((size_t)INTPTR_MAX) 73 74 /** 75 * Allocates a Mach port and configures it to receive exception messages, 76 * and installs it as the exception handler for the current thread. 77 * 78 * @param exception_mask exception types that this Mach port should receive 79 * 80 * @return a newly-allocated and -configured Mach port 81 */ 82 mach_port_t 83 create_exception_port(exception_mask_t exception_mask); 84 85 mach_port_t 86 create_exception_port_behavior64(exception_mask_t exception_mask, exception_behavior_t behavior); 87 88 /** 89 * Installs an exception port created with create_exception_port() 90 * as the exception handler for the current thread. 91 */ 92 void 93 set_thread_exception_port(mach_port_t exc_port, exception_mask_t exception_mask); 94 95 void 96 set_thread_exception_port_behavior64(mach_port_t exc_port, exception_mask_t exception_mask, exception_behavior_t behavior); 97 98 /** 99 * Handles one exception received on the provided Mach port, by running the 100 * provided callback. 101 * 102 * @param exc_port Mach port configured to receive exception messages 103 * @param callback callback to run when an exception is received 104 */ 105 void 106 run_exception_handler(mach_port_t exc_port, exc_handler_callback_t callback); 107 108 void 109 run_exception_handler_behavior64(mach_port_t exc_port, void *preferred_callback, void *callback, 110 exception_behavior_t behavior, bool run_once); 111 112 /** 113 * Handles every exception received on the provided Mach port, by running the 114 * provided callback. 115 * 116 * @param exc_port Mach port configured to receive exception messages 117 * @param callback callback to run when an exception is received 118 */ 119 void 120 repeat_exception_handler(mach_port_t exc_port, exc_handler_callback_t callback); 121 122 #endif /* EXC_HELPERS_H */ 123