Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | /* * Copyright (c) 2018 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include "internal.h" #pragma mark Assertions static_assert(_OS_CRASH_LAST < OS_CRASH_PORT_ARRAY_COUNT, "array not large enough"); #pragma mark Private Utilities static thread_state_flavor_t _exception_thread_state_flavor(void) { thread_state_flavor_t flavor = 0; #if defined(__i386__) || defined(__x86_64__) flavor = x86_THREAD_STATE; #elif defined(__arm__) || defined(__arm64__) flavor = ARM_THREAD_STATE; #else // defined(__i386__) || defined(__x86_64__) #error "unsupported architecture" #endif // defined(__i386__) || defined(__x86_64__) return flavor; } static exception_mask_t _exception_mask(os_crash_flags_t flags) { exception_mask_t mask = 0; if (flags & OS_CRASH_FLAG_CRASH) { mask |= EXC_MASK_CRASH; } if (flags & OS_CRASH_FLAG_GUARD) { mask |= EXC_MASK_GUARD; } if (flags & OS_CRASH_FLAG_RESOURCE) { mask |= EXC_MASK_RESOURCE; } if (flags & OS_CRASH_FLAG_CORPSE) { mask |= EXC_MASK_CORPSE_NOTIFY; mask &= (exception_mask_t)(~EXC_MASK_CRASH); } return mask; } static exception_behavior_t _exception_behavior(void) { return (EXCEPTION_STATE_IDENTITY|MACH_EXCEPTION_CODES); } static void _os_exception_port_set(os_crash_port_t *ep, os_crash_type_t type, mach_port_t p) { kern_return_t kr = KERN_FAILURE; ep->oep_type = type; ep->oep_port = p; kr = mach_port_mod_refs(mach_task_self(), p, MACH_PORT_RIGHT_SEND, 1); if (kr == KERN_SUCCESS) { ep->oep_port = p; } else { ep->oep_port = MACH_PORT_DEAD; } } #pragma mark API kern_return_t os_crash_set_reporter_port(mach_port_t where, os_crash_flags_t flags, mach_port_t p) { kern_return_t kr = KERN_FAILURE; exception_mask_t mask = _exception_mask(flags); exception_behavior_t bhvr = _exception_behavior(); thread_state_flavor_t flvr = _exception_thread_state_flavor(); mach_port_t host_priv = MACH_PORT_NULL; // If we're not privleged to get the host-privileged port, no big deal, then // the comparison below will fail. (void)host_get_host_priv_port(mach_host_self(), &host_priv); if (flags == OS_CRASH_FLAG_INIT) { return KERN_SUCCESS; } if (where == mach_host_self() || where == host_priv) { kr = host_set_exception_ports(where, mask, p, bhvr, flvr); } else { kr = task_set_exception_ports(where, mask, p, bhvr, flvr); } return kr; } kern_return_t os_crash_get_reporter_port_array(mach_port_t where, os_crash_port_array_t array) { kern_return_t kr = KERN_FAILURE; exception_mask_t masks[EXC_TYPES_COUNT]; mach_msg_type_number_t nmasks = 0; exception_port_t ports[EXC_TYPES_COUNT]; exception_behavior_t behaviors[EXC_TYPES_COUNT]; thread_state_flavor_t flavors[EXC_TYPES_COUNT]; exception_mask_t mask = EXC_MASK_CRASH | EXC_MASK_GUARD | EXC_MASK_RESOURCE | EXC_MASK_CORPSE_NOTIFY; size_t i = 0; size_t j = 0; mach_port_t host_priv = MACH_PORT_NULL; (void)host_get_host_priv_port(mach_host_self(), &host_priv); if (where == mach_host_self() || where == host_priv) { kr = host_get_exception_ports(mach_host_self(), mask, masks, &nmasks, ports, behaviors, flavors); } else { kr = task_get_exception_ports(where, mask, masks, &nmasks, ports, behaviors, flavors); } if (kr) { goto __out; } bzero(array, sizeof(array[0]) * OS_CRASH_PORT_ARRAY_COUNT); for (i = 0; i < nmasks; i++) { if (masks[i] & EXC_MASK_CRASH) { _os_exception_port_set(&array[j], OS_CRASH_CRASH, ports[i]); j++; } if (masks[i] & EXC_MASK_GUARD) { _os_exception_port_set(&array[j], OS_CRASH_GUARD, ports[i]); j++; } if (masks[i] & EXC_MASK_RESOURCE) { _os_exception_port_set(&array[j], OS_CRASH_RESOURCE, ports[i]); j++; } if (masks[i] & EXC_MASK_CORPSE_NOTIFY) { _os_exception_port_set(&array[j], OS_CRASH_CORPSE, ports[i]); j++; } kr = mach_port_deallocate(mach_task_self(), ports[i]); os_assert_mach("deallocate port", kr); } __out: return kr; } void os_crash_port_array_deallocate(os_crash_port_array_t array) { size_t i = 0; for (i = 0; i < 16; i++) { if (MACH_PORT_VALID(array[i].oep_port)) { kern_return_t kr = KERN_FAILURE; kr = mach_port_deallocate(mach_task_self(), array[i].oep_port); os_assert_mach("deallocate port", kr); } } } kern_return_t os_crash_spawnattr_set_reporter_port(posix_spawnattr_t *psattr, os_crash_flags_t flags, mach_port_t p) { int error = -1; exception_mask_t mask = _exception_mask(flags); exception_behavior_t bhvr = _exception_behavior(); thread_state_flavor_t flvr = _exception_thread_state_flavor(); if (!MACH_PORT_VALID(p)) { return KERN_INVALID_NAME; } if (flags == OS_CRASH_FLAG_INIT) { return KERN_SUCCESS; } error = posix_spawnattr_setexceptionports_np(psattr, mask, p, bhvr, flvr); os_assert_zero(error); return KERN_SUCCESS; } |