robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 1 | /* |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 2 | * |
| 3 | * honggfuzz - architecture dependent code (MAC OS X) |
| 4 | * ----------------------------------------- |
| 5 | * |
| 6 | * Author: Robert Swiecki <swiecki@google.com> Felix Gröbert |
| 7 | * <groebert@google.com> |
| 8 | * |
| 9 | * Copyright 2010-2015 by Google Inc. All Rights Reserved. |
| 10 | * |
| 11 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 12 | * not use this file except in compliance with the License. You may obtain |
| 13 | * a copy of the License at |
| 14 | * |
| 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | * |
| 17 | * Unless required by applicable law or agreed to in writing, software |
| 18 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 20 | * implied. See the License for the specific language governing |
| 21 | * permissions and limitations under the License. |
| 22 | * |
| 23 | */ |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 24 | |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 25 | #include "common.h" |
| 26 | #include "arch.h" |
| 27 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 28 | #include <ctype.h> |
groebert@google.com | b857deb | 2010-10-21 19:09:29 +0000 | [diff] [blame] | 29 | #include <dirent.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 30 | #include <errno.h> |
groebert@google.com | b857deb | 2010-10-21 19:09:29 +0000 | [diff] [blame] | 31 | #include <fcntl.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 32 | #include <signal.h> |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <sys/cdefs.h> |
| 37 | #include <sys/mman.h> |
| 38 | #include <sys/time.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <sys/wait.h> |
| 41 | #include <sys/resource.h> |
| 42 | #include <time.h> |
| 43 | #include <unistd.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 44 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 45 | #include "log.h" |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 46 | #include "util.h" |
groebert@google.com | b857deb | 2010-10-21 19:09:29 +0000 | [diff] [blame] | 47 | #include "files.h" |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 48 | |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 49 | #include <servers/bootstrap.h> |
| 50 | #include <mach/mach.h> |
| 51 | #include <mach/mach_vm.h> |
| 52 | #include <mach/mach_types.h> |
| 53 | #include <mach/i386/thread_status.h> |
| 54 | #include <mach/task_info.h> |
| 55 | #include <pthread.h> |
| 56 | |
| 57 | #include "mach_exc.h" |
| 58 | #include "mach_excServer.h" |
| 59 | |
| 60 | #import <Foundation/Foundation.h> |
| 61 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 62 | /* |
| 63 | * Interface to third_party/CrashReport_*.o |
| 64 | */ |
groebert@google.com | 43216c8 | 2015-02-13 11:11:38 +0000 | [diff] [blame] | 65 | /* *INDENT-OFF* */ |
| 66 | @interface CrashReport: NSObject - (id) initWithTask:(task_t) |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 67 | task exceptionType:(exception_type_t) |
| 68 | anExceptionType exceptionCode:(mach_exception_data_t) |
| 69 | anExceptionCode exceptionCodeCount:(mach_msg_type_number_t) |
| 70 | anExceptionCodeCount thread:(thread_t) |
| 71 | thread threadStateFlavor:(thread_state_flavor_t) |
| 72 | aThreadStateFlavor threadState:(thread_state_data_t) |
| 73 | aThreadState threadStateCount:(mach_msg_type_number_t) aThreadStateCount; |
| 74 | @end |
groebert@google.com | 43216c8 | 2015-02-13 11:11:38 +0000 | [diff] [blame] | 75 | /* *INDENT-ON* */ |
| 76 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 77 | /* |
| 78 | * Global to have exception port available in the collection thread |
| 79 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 80 | static mach_port_t g_exception_port = MACH_PORT_NULL; |
| 81 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 82 | /* |
| 83 | * From xnu/bsd/sys/proc_internal.h |
| 84 | */ |
groebert@google.com | c659c0e | 2015-02-13 11:17:00 +0000 | [diff] [blame] | 85 | #define PID_MAX 99999 |
groebert@google.com | 43216c8 | 2015-02-13 11:11:38 +0000 | [diff] [blame] | 86 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 87 | /* |
| 88 | * Global to store crash info in exception handler thread |
| 89 | */ |
groebert@google.com | c659c0e | 2015-02-13 11:17:00 +0000 | [diff] [blame] | 90 | fuzzer_t g_fuzzer_crash_information[PID_MAX + 1]; |
groebert@google.com | 43216c8 | 2015-02-13 11:11:38 +0000 | [diff] [blame] | 91 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 92 | /* |
| 93 | * Global to have a unique service name for each honggfuzz process |
| 94 | */ |
groebert@google.com | 76afff9 | 2013-06-20 14:43:41 +0000 | [diff] [blame] | 95 | char g_service_name[256]; |
groebert@google.com | 16610ee | 2013-06-19 13:06:12 +0000 | [diff] [blame] | 96 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 97 | struct { |
| 98 | bool important; |
| 99 | const char *descr; |
| 100 | } arch_sigs[NSIG]; |
| 101 | |
robert.swiecki@gmail.com | c6d2875 | 2014-01-08 12:02:37 +0000 | [diff] [blame] | 102 | __attribute__ ((constructor)) |
| 103 | void arch_initSigs(void) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 104 | { |
| 105 | for (int x = 0; x < NSIG; x++) |
| 106 | arch_sigs[x].important = false; |
| 107 | |
| 108 | arch_sigs[SIGILL].important = true; |
| 109 | arch_sigs[SIGILL].descr = "SIGILL"; |
| 110 | arch_sigs[SIGFPE].important = true; |
| 111 | arch_sigs[SIGFPE].descr = "SIGFPE"; |
| 112 | arch_sigs[SIGSEGV].important = true; |
| 113 | arch_sigs[SIGSEGV].descr = "SIGSEGV"; |
| 114 | arch_sigs[SIGBUS].important = true; |
| 115 | arch_sigs[SIGBUS].descr = "SIGBUS"; |
| 116 | arch_sigs[SIGABRT].important = true; |
| 117 | arch_sigs[SIGABRT].descr = "SIGABRT"; |
| 118 | } |
| 119 | |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 120 | const char *exception_to_string(int exception) |
groebert@google.com | b857deb | 2010-10-21 19:09:29 +0000 | [diff] [blame] | 121 | { |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 122 | switch (exception) { |
| 123 | case EXC_BAD_ACCESS: |
| 124 | return "EXC_BAD_ACCESS"; |
| 125 | case EXC_BAD_INSTRUCTION: |
| 126 | return "EXC_BAD_INSTRUCTION"; |
| 127 | case EXC_ARITHMETIC: |
| 128 | return "EXC_ARITHMETIC"; |
| 129 | case EXC_EMULATION: |
| 130 | return "EXC_EMULATION"; |
| 131 | case EXC_SOFTWARE: |
| 132 | return "EXC_SOFTWARE"; |
| 133 | case EXC_BREAKPOINT: |
| 134 | return "EXC_BREAKPOINT"; |
| 135 | case EXC_SYSCALL: |
| 136 | return "EXC_SYSCALL"; |
| 137 | case EXC_MACH_SYSCALL: |
| 138 | return "EXC_MACH_SYSCALL"; |
| 139 | case EXC_RPC_ALERT: |
| 140 | return "EXC_RPC_ALERT"; |
| 141 | case EXC_CRASH: |
| 142 | return "EXC_CRASH"; |
groebert@google.com | b857deb | 2010-10-21 19:09:29 +0000 | [diff] [blame] | 143 | } |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 144 | return "UNKNOWN"; |
groebert@google.com | b857deb | 2010-10-21 19:09:29 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 148 | * Returns true if a process exited (so, presumably, we can delete an input |
| 149 | * file) |
| 150 | */ |
robert.swiecki@gmail.com | 751f686 | 2015-02-11 15:03:26 +0000 | [diff] [blame] | 151 | static bool arch_analyzeSignal(honggfuzz_t * hfuzz, int status, fuzzer_t * fuzzer) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 152 | { |
| 153 | /* |
| 154 | * Resumed by delivery of SIGCONT |
| 155 | */ |
| 156 | if (WIFCONTINUED(status)) { |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Boring, the process just exited |
| 162 | */ |
| 163 | if (WIFEXITED(status)) { |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 164 | LOGMSG(l_DEBUG, "Process (pid %d) exited normally with status %d", |
| 165 | fuzzer->pid, WEXITSTATUS(status)); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Shouldn't really happen, but, well.. |
| 171 | */ |
| 172 | if (!WIFSIGNALED(status)) { |
| 173 | LOGMSG(l_ERROR, |
| 174 | "Process (pid %d) exited with the following status %d, please report that as a bug", |
robert.swiecki@gmail.com | 751f686 | 2015-02-11 15:03:26 +0000 | [diff] [blame] | 175 | fuzzer->pid, status); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 176 | return true; |
| 177 | } |
| 178 | |
| 179 | int termsig = WTERMSIG(status); |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 180 | LOGMSG(l_DEBUG, "Process (pid %d) killed by signal %d '%s'", |
| 181 | fuzzer->pid, termsig, strsignal(termsig)); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 182 | if (!arch_sigs[termsig].important) { |
| 183 | LOGMSG(l_DEBUG, "It's not that important signal, skipping"); |
| 184 | return true; |
| 185 | } |
| 186 | |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 187 | /* |
| 188 | * Signal is interesting |
| 189 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 190 | char newname[PATH_MAX]; |
groebert@google.com | b857deb | 2010-10-21 19:09:29 +0000 | [diff] [blame] | 191 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 192 | /* |
| 193 | * Get data from exception handler |
| 194 | */ |
groebert@google.com | 43216c8 | 2015-02-13 11:11:38 +0000 | [diff] [blame] | 195 | fuzzer->pc = g_fuzzer_crash_information[fuzzer->pid].pc; |
| 196 | fuzzer->exception = g_fuzzer_crash_information[fuzzer->pid].exception; |
| 197 | fuzzer->access = g_fuzzer_crash_information[fuzzer->pid].access; |
| 198 | fuzzer->backtrace = g_fuzzer_crash_information[fuzzer->pid].backtrace; |
| 199 | |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 200 | if (hfuzz->saveUnique) { |
| 201 | snprintf(newname, sizeof(newname), |
| 202 | "%s.%s.PC.%.16llx.STACK.%.16llx.ADDR.%.16llx.%s.%s", |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 203 | arch_sigs[termsig].descr, |
| 204 | exception_to_string(fuzzer->exception), fuzzer->pc, |
| 205 | fuzzer->backtrace, fuzzer->access, fuzzer->origFileName, hfuzz->fileExtn); |
groebert@google.com | b857deb | 2010-10-21 19:09:29 +0000 | [diff] [blame] | 206 | } else { |
groebert@google.com | b857deb | 2010-10-21 19:09:29 +0000 | [diff] [blame] | 207 | |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 208 | char localtmstr[PATH_MAX]; |
| 209 | util_getLocalTime("%F.%H.%M.%S", localtmstr, sizeof(localtmstr)); |
| 210 | |
| 211 | snprintf(newname, sizeof(newname), |
| 212 | "%s.%s.PC.%.16llx.STACK.%.16llx.ADDR.%.16llx.TIME.%s.PID.%.5d.%s.%s", |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 213 | arch_sigs[termsig].descr, |
| 214 | exception_to_string(fuzzer->exception), fuzzer->pc, |
| 215 | fuzzer->backtrace, fuzzer->access, localtmstr, |
| 216 | fuzzer->pid, fuzzer->origFileName, hfuzz->fileExtn); |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 217 | } |
| 218 | |
robert.swiecki@gmail.com | 751f686 | 2015-02-11 15:03:26 +0000 | [diff] [blame] | 219 | if (link(fuzzer->fileName, newname) == 0) { |
groebert@google.com | 43216c8 | 2015-02-13 11:11:38 +0000 | [diff] [blame] | 220 | LOGMSG(l_INFO, "Ok, that's interesting, saved '%s' as '%s'", fuzzer->fileName, newname); |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 221 | } else { |
| 222 | if (errno == EEXIST) { |
| 223 | LOGMSG(l_INFO, "It seems that '%s' already exists, skipping", newname); |
| 224 | } else { |
groebert@google.com | 43216c8 | 2015-02-13 11:11:38 +0000 | [diff] [blame] | 225 | LOGMSG_P(l_ERROR, "Couldn't link '%s' to '%s'", fuzzer->fileName, newname); |
groebert@google.com | b857deb | 2010-10-21 19:09:29 +0000 | [diff] [blame] | 226 | } |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 227 | } |
groebert@google.com | b857deb | 2010-10-21 19:09:29 +0000 | [diff] [blame] | 228 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 229 | return true; |
| 230 | } |
| 231 | |
| 232 | bool arch_launchChild(honggfuzz_t * hfuzz, char *fileName) |
| 233 | { |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 234 | #define ARGS_MAX 512 |
| 235 | char *args[ARGS_MAX + 2]; |
| 236 | |
| 237 | int x; |
| 238 | |
| 239 | for (x = 0; x < ARGS_MAX && hfuzz->cmdline[x]; x++) { |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 240 | if (!hfuzz->fuzzStdin && strcmp(hfuzz->cmdline[x], _HF_FILE_PLACEHOLDER) == 0) { |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 241 | args[x] = fileName; |
| 242 | } else { |
| 243 | args[x] = hfuzz->cmdline[x]; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | args[x++] = NULL; |
| 248 | |
| 249 | LOGMSG(l_DEBUG, "Launching '%s' on file '%s'", args[0], fileName); |
| 250 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 251 | /* |
| 252 | * Get child's bootstrap port. |
| 253 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 254 | mach_port_t child_bootstrap = MACH_PORT_NULL; |
| 255 | if (task_get_bootstrap_port(mach_task_self(), &child_bootstrap) != KERN_SUCCESS) { |
| 256 | return false; |
| 257 | } |
| 258 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 259 | /* |
| 260 | * Get exception port. |
| 261 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 262 | mach_port_t exception_port = MACH_PORT_NULL; |
| 263 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 264 | if (bootstrap_look_up(child_bootstrap, g_service_name, &exception_port) |
| 265 | != KERN_SUCCESS) { |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 266 | return false; |
| 267 | } |
| 268 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 269 | /* |
| 270 | * Here we register the exception port in the child |
| 271 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 272 | if (task_set_exception_ports(mach_task_self(), |
| 273 | EXC_MASK_CRASH, |
| 274 | exception_port, |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 275 | EXCEPTION_STATE_IDENTITY | |
| 276 | MACH_EXCEPTION_CODES, MACHINE_THREAD_STATE) != KERN_SUCCESS) { |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 280 | /* |
| 281 | * Set timeout (prof), real timeout (2*prof), and rlimit_cpu (2*prof) |
| 282 | */ |
| 283 | if (hfuzz->tmOut) { |
| 284 | struct itimerval it; |
| 285 | |
| 286 | /* |
| 287 | * The hfuzz->tmOut is real CPU usage time... |
| 288 | */ |
| 289 | it.it_value.tv_sec = hfuzz->tmOut; |
| 290 | it.it_value.tv_usec = 0; |
| 291 | it.it_interval.tv_sec = 0; |
| 292 | it.it_interval.tv_usec = 0; |
| 293 | if (setitimer(ITIMER_PROF, &it, NULL) == -1) { |
| 294 | LOGMSG_P(l_ERROR, "Couldn't set the ITIMER_PROF timer"); |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * ...so, if a process sleeps, this one should |
| 300 | * trigger a signal... |
| 301 | */ |
| 302 | it.it_value.tv_sec = hfuzz->tmOut * 2UL; |
| 303 | it.it_value.tv_usec = 0; |
| 304 | it.it_interval.tv_sec = 0; |
| 305 | it.it_interval.tv_usec = 0; |
| 306 | if (setitimer(ITIMER_REAL, &it, NULL) == -1) { |
| 307 | LOGMSG_P(l_ERROR, "Couldn't set the ITIMER_REAL timer"); |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * ..if a process sleeps and catches SIGPROF/SIGALRM |
| 313 | * rlimits won't help either |
| 314 | */ |
| 315 | struct rlimit rl; |
| 316 | |
| 317 | rl.rlim_cur = hfuzz->tmOut * 2; |
| 318 | rl.rlim_max = hfuzz->tmOut * 2; |
| 319 | if (setrlimit(RLIMIT_CPU, &rl) == -1) { |
| 320 | LOGMSG_P(l_ERROR, "Couldn't enforce the RLIMIT_CPU resource limit"); |
| 321 | return false; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * The address space limit. If big enough - roughly the size of RAM used |
| 327 | */ |
| 328 | if (hfuzz->asLimit) { |
| 329 | struct rlimit rl; |
| 330 | |
| 331 | rl.rlim_cur = hfuzz->asLimit * 1024UL * 1024UL; |
| 332 | rl.rlim_max = hfuzz->asLimit * 1024UL * 1024UL; |
| 333 | if (setrlimit(RLIMIT_AS, &rl) == -1) { |
| 334 | LOGMSG_P(l_DEBUG, "Couldn't encforce the RLIMIT_AS resource limit, ignoring"); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | if (hfuzz->nullifyStdio) { |
| 339 | util_nullifyStdio(); |
| 340 | } |
| 341 | |
| 342 | if (hfuzz->fuzzStdin) { |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 343 | /* |
| 344 | * Uglyyyyyy ;) |
| 345 | */ |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 346 | if (!util_redirectStdin(fileName)) { |
| 347 | return false; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | execvp(args[0], args); |
| 352 | |
| 353 | util_recoverStdio(); |
| 354 | LOGMSG(l_FATAL, "Failed to create new '%s' process", args[0]); |
| 355 | return false; |
| 356 | } |
| 357 | |
robert.swiecki@gmail.com | 751f686 | 2015-02-11 15:03:26 +0000 | [diff] [blame] | 358 | void arch_reapChild(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 359 | { |
groebert@google.com | 06d9380 | 2013-09-16 12:06:40 +0000 | [diff] [blame] | 360 | /* |
| 361 | * First check manually if we have expired childs |
| 362 | */ |
| 363 | |
groebert@google.com | 43216c8 | 2015-02-13 11:11:38 +0000 | [diff] [blame] | 364 | double diff = difftime(time(NULL), fuzzer->timeStarted); |
robert.swiecki@gmail.com | 751f686 | 2015-02-11 15:03:26 +0000 | [diff] [blame] | 365 | if (diff > (double)hfuzz->tmOut) { |
| 366 | LOGMSG(l_WARN, |
| 367 | "Process pid %d is overdue (%f seconds, max %f seconds %f), sending a SIGKILL", |
groebert@google.com | 43216c8 | 2015-02-13 11:11:38 +0000 | [diff] [blame] | 368 | fuzzer->pid, diff, (double)hfuzz->tmOut); |
| 369 | kill(fuzzer->pid, SIGKILL); |
groebert@google.com | 06d9380 | 2013-09-16 12:06:40 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* |
robert.swiecki@gmail.com | 751f686 | 2015-02-11 15:03:26 +0000 | [diff] [blame] | 373 | * Now check for signals using wait4 |
groebert@google.com | 06d9380 | 2013-09-16 12:06:40 +0000 | [diff] [blame] | 374 | */ |
robert.swiecki@gmail.com | 751f686 | 2015-02-11 15:03:26 +0000 | [diff] [blame] | 375 | |
robert.swiecki@gmail.com | 65cfa1c | 2015-02-11 15:59:28 +0000 | [diff] [blame] | 376 | for (;;) { |
| 377 | int status = 0; |
groebert@google.com | 43216c8 | 2015-02-13 11:11:38 +0000 | [diff] [blame] | 378 | while (wait4(fuzzer->pid, &status, WUNTRACED, NULL) != fuzzer->pid) ; |
robert.swiecki@gmail.com | 65cfa1c | 2015-02-11 15:59:28 +0000 | [diff] [blame] | 379 | LOGMSG(l_DEBUG, "Process (pid %d) came back with status %d", fuzzer->pid, status); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 380 | |
groebert@google.com | 43216c8 | 2015-02-13 11:11:38 +0000 | [diff] [blame] | 381 | if (arch_analyzeSignal(hfuzz, status, fuzzer)) { |
robert.swiecki@gmail.com | 65cfa1c | 2015-02-11 15:59:28 +0000 | [diff] [blame] | 382 | return; |
| 383 | } |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 384 | } |
robert.swiecki@gmail.com | 751f686 | 2015-02-11 15:03:26 +0000 | [diff] [blame] | 385 | } |
robert.swiecki | 28cba5c | 2011-06-22 01:38:55 +0000 | [diff] [blame] | 386 | |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 387 | void *wait_for_exception() |
| 388 | { |
| 389 | while (1) { |
| 390 | mach_msg_server_once(mach_exc_server, 4096, g_exception_port, MACH_MSG_OPTION_NONE); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Called once before fuzzing starts. Prepare mach ports for attaching crash reporter. |
| 396 | */ |
robert.swiecki | 28cba5c | 2011-06-22 01:38:55 +0000 | [diff] [blame] | 397 | bool arch_prepareParent(honggfuzz_t * hfuzz) |
| 398 | { |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 399 | char plist[PATH_MAX]; |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 400 | snprintf(plist, sizeof(plist), |
| 401 | "/Users/%s/Library/Preferences/com.apple.DebugSymbols.plist", getlogin()); |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 402 | |
| 403 | if (files_exists(plist)) { |
| 404 | LOGMSG(l_WARN, |
| 405 | "honggfuzz won't work if DBGShellCommands are set in ~/Library/Preferences/com.apple.DebugSymbols.plist"); |
| 406 | } |
| 407 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 408 | /* |
| 409 | * Allocate exception port. |
| 410 | */ |
| 411 | if (mach_port_allocate |
| 412 | (mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &g_exception_port) != KERN_SUCCESS) { |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 413 | return false; |
| 414 | } |
| 415 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 416 | /* |
| 417 | * Insert exception receive port. |
| 418 | */ |
| 419 | if (mach_port_insert_right |
| 420 | (mach_task_self(), g_exception_port, g_exception_port, |
| 421 | MACH_MSG_TYPE_MAKE_SEND) != KERN_SUCCESS) { |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 422 | return false; |
| 423 | } |
| 424 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 425 | /* |
| 426 | * Get bootstrap port. |
| 427 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 428 | mach_port_t bootstrap = MACH_PORT_NULL; |
| 429 | if (task_get_bootstrap_port(mach_task_self(), &bootstrap) != KERN_SUCCESS) { |
| 430 | return false; |
| 431 | } |
| 432 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 433 | /* |
| 434 | * Generate and register exception port service. |
| 435 | */ |
| 436 | snprintf(g_service_name, sizeof(g_service_name), |
| 437 | "com.google.code.honggfuzz.%d", util_rndGet(0, 999999)); |
groebert@google.com | 76afff9 | 2013-06-20 14:43:41 +0000 | [diff] [blame] | 438 | if (bootstrap_check_in(bootstrap, g_service_name, &g_exception_port) != KERN_SUCCESS) { |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 439 | return false; |
| 440 | } |
| 441 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 442 | /* |
| 443 | * Create a collection thread to catch the exceptions from the |
| 444 | * children |
| 445 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 446 | pthread_t exception_thread; |
| 447 | |
| 448 | if (pthread_create(&exception_thread, NULL, wait_for_exception, 0)) { |
| 449 | LOGMSG(l_FATAL, "Parent: could not create thread to wait for child's exception"); |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | if (pthread_detach(exception_thread)) { |
| 454 | LOGMSG(l_FATAL, "Parent: could not detach thread to wait for child's exception"); |
| 455 | return false; |
| 456 | } |
| 457 | |
robert.swiecki | 28cba5c | 2011-06-22 01:38:55 +0000 | [diff] [blame] | 458 | return true; |
| 459 | } |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 460 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 461 | /* |
| 462 | * Write the crash report to DEBUG |
| 463 | */ |
| 464 | void |
| 465 | write_crash_report(thread_port_t thread, |
| 466 | task_port_t task, |
| 467 | exception_type_t exception, |
| 468 | mach_exception_data_t code, |
| 469 | mach_msg_type_number_t code_count, |
| 470 | int *flavor, thread_state_t in_state, mach_msg_type_number_t in_state_count) |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 471 | { |
| 472 | |
| 473 | NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init]; |
| 474 | CrashReport *_crashReport = nil; |
| 475 | |
groebert@google.com | 43216c8 | 2015-02-13 11:11:38 +0000 | [diff] [blame] | 476 | /* *INDENT-OFF* */ |
| 477 | _crashReport = [[CrashReport alloc] initWithTask:task |
| 478 | exceptionType:exception |
| 479 | exceptionCode:code |
| 480 | exceptionCodeCount:code_count |
| 481 | thread:thread |
| 482 | threadStateFlavor:*flavor |
| 483 | threadState:(thread_state_t)in_state |
| 484 | threadStateCount:in_state_count]; |
| 485 | /* *INDENT-OFF* */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 486 | |
| 487 | NSString *crashDescription =[_crashReport description]; |
| 488 | char *description = (char *)[crashDescription UTF8String]; |
| 489 | |
| 490 | LOGMSG(l_DEBUG, "CrashReport: %s", description); |
| 491 | |
| 492 | [_crashReport release]; |
| 493 | [pool drain]; |
| 494 | } |
| 495 | |
| 496 | /* Hash the callstack in an unique way */ |
| 497 | uint64_t hash_callstack(thread_port_t thread, |
| 498 | task_port_t task, |
| 499 | exception_type_t exception, |
| 500 | mach_exception_data_t code, |
| 501 | mach_msg_type_number_t code_count, |
groebert@google.com | 43216c8 | 2015-02-13 11:11:38 +0000 | [diff] [blame] | 502 | int *flavor, |
| 503 | thread_state_t in_state, |
| 504 | mach_msg_type_number_t in_state_count) { |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 505 | |
| 506 | NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init]; |
| 507 | CrashReport *_crashReport = nil; |
| 508 | |
groebert@google.com | 43216c8 | 2015-02-13 11:11:38 +0000 | [diff] [blame] | 509 | /* *INDENT-OFF* */ |
| 510 | _crashReport = [[CrashReport alloc] initWithTask:task |
| 511 | exceptionType:exception |
| 512 | exceptionCode:code |
| 513 | exceptionCodeCount:code_count |
| 514 | thread:thread |
| 515 | threadStateFlavor:*flavor |
| 516 | threadState:(thread_state_t)in_state |
| 517 | threadStateCount:in_state_count]; |
| 518 | /* *INDENT-ON* */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 519 | |
| 520 | NSString *crashDescription =[_crashReport description]; |
| 521 | char *description = (char *)[crashDescription UTF8String]; |
| 522 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 523 | /* |
| 524 | * The callstack begins with the following word |
| 525 | */ |
groebert@google.com | 06d9380 | 2013-09-16 12:06:40 +0000 | [diff] [blame] | 526 | char *callstack = strstr(description, "Crashed:"); |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 527 | |
| 528 | if (callstack == NULL) { |
| 529 | LOGMSG(l_FATAL, "Could not find callstack in crash report %s", description); |
| 530 | } |
| 531 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 532 | /* |
| 533 | * Scroll forward to the next newline |
| 534 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 535 | char *callstack_start = strstr(callstack, "\n"); |
| 536 | |
| 537 | if (callstack_start == NULL) { |
| 538 | LOGMSG(l_FATAL, "Could not find callstack start in crash report %s", description); |
| 539 | } |
| 540 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 541 | /* |
| 542 | * Skip the newline |
| 543 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 544 | callstack_start++; |
| 545 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 546 | /* |
| 547 | * Determine the end of the callstack |
| 548 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 549 | char *callstack_end = strstr(callstack, "\n\nThread"); |
| 550 | |
| 551 | if (callstack_end == NULL) { |
| 552 | LOGMSG(l_FATAL, "Could not find callstack end in crash report %s", description); |
| 553 | } |
| 554 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 555 | /* |
| 556 | * Make sure it's NULL-terminated |
| 557 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 558 | *callstack_end = '\0'; |
| 559 | |
| 560 | /* |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 561 | * |
| 562 | * For each line, we only take the last three nibbles from the |
| 563 | * address. |
| 564 | * |
| 565 | * Sample output: |
| 566 | * |
| 567 | * 0 libsystem_kernel.dylib 0x00007fff80514d46 __kill + 10 1 |
| 568 | * libsystem_c.dylib 0x00007fff85731ec0 __abort + 193 2 |
| 569 | * libsystem_c.dylib 0x00007fff85732d17 __stack_chk_fail + 195 3 |
| 570 | * stack_buffer_overflow64-stripped 0x000000010339def5 0x10339d000 + |
| 571 | * 3829 4 ??? 0x4141414141414141 0 + 4702111234474983745 |
| 572 | * |
| 573 | * 0 libsystem_kernel.dylib 0x00007fff80514d46 __kill + 10 1 |
| 574 | * libsystem_c.dylib 0x00007fff85731ec0 __abort + 193 2 |
| 575 | * libsystem_c.dylib 0x00007fff85732d17 __stack_chk_fail + 195 3 |
| 576 | * stack_buffer_overflow64 0x0000000108f41ef5 main + 133 4 ??? |
| 577 | * 0x4141414141414141 0 + 4702111234474983745 |
| 578 | * |
| 579 | * 0 libsystem_kernel.dylib 0x940023ba __kill + 10 1 |
| 580 | * libsystem_kernel.dylib 0x940014bc kill$UNIX2003 + 32 2 |
| 581 | * libsystem_c.dylib 0x926f362e __abort + 246 3 libsystem_c.dylib |
| 582 | * 0x926c2b60 __chk_fail + 49 4 libsystem_c.dylib 0x926c2bf9 |
| 583 | * __memset_chk + 53 5 stack_buffer_overflow32-stripped 0x00093ee5 |
| 584 | * 0x93000 + 3813 6 libdyld.dylib 0x978c6725 start + 1 |
| 585 | * |
| 586 | * 0 libsystem_kernel.dylib 0x940023ba __kill + 10 1 |
| 587 | * libsystem_kernel.dylib 0x940014bc kill$UNIX2003 + 32 2 |
| 588 | * libsystem_c.dylib 0x926f362e __abort + 246 3 libsystem_c.dylib |
| 589 | * 0x926c2b60 __chk_fail + 49 4 libsystem_c.dylib 0x926c2bf9 |
| 590 | * __memset_chk + 53 5 stack_buffer_overflow32 0x0003cee5 main + 117 6 |
| 591 | * libdyld.dylib 0x978c6725 start + 1 |
| 592 | * |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 593 | */ |
| 594 | |
| 595 | uint64_t hash = 0; |
| 596 | char *pos = callstack_start; |
| 597 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 598 | /* |
| 599 | * Go through each line until we run out of lines |
| 600 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 601 | while (strstr(pos, "\t") != NULL) { |
| 602 | /* |
| 603 | * Format: dylib spaces tab address space symbol space plus space offset |
| 604 | * Scroll pos forward to the last three nibbles of the address. |
| 605 | */ |
| 606 | if ((pos = strstr(pos, "\t")) == NULL) |
| 607 | break; |
| 608 | if ((pos = strstr(pos, " ")) == NULL) |
| 609 | break; |
| 610 | pos = pos - 3; |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 611 | /* |
| 612 | * Hash the last three nibbles |
| 613 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 614 | hash ^= util_hash(pos, 3); |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 615 | /* |
| 616 | * Scroll pos one forward to skip the current tab |
| 617 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 618 | pos++; |
| 619 | } |
| 620 | |
groebert@google.com | 20e368f | 2015-02-13 14:19:25 +0000 | [diff] [blame] | 621 | LOGMSG(l_DEBUG, "Callstack hash %u", hash); |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 622 | |
| 623 | [_crashReport release]; |
| 624 | [pool drain]; |
| 625 | |
| 626 | return hash; |
| 627 | } |
| 628 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 629 | kern_return_t |
| 630 | catch_mach_exception_raise(mach_port_t exception_port, |
| 631 | mach_port_t thread, |
| 632 | mach_port_t task, |
| 633 | exception_type_t exception, |
| 634 | mach_exception_data_t code, mach_msg_type_number_t codeCnt) |
groebert@google.com | c659c0e | 2015-02-13 11:17:00 +0000 | [diff] [blame] | 635 | { |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 636 | LOGMSG(l_FATAL, "This function should never get called"); |
| 637 | return KERN_SUCCESS; |
| 638 | } |
| 639 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 640 | kern_return_t |
| 641 | catch_mach_exception_raise_state(mach_port_t exception_port, |
| 642 | exception_type_t exception, |
| 643 | const mach_exception_data_t code, |
| 644 | mach_msg_type_number_t codeCnt, |
| 645 | int *flavor, |
| 646 | const thread_state_t old_state, |
| 647 | mach_msg_type_number_t old_stateCnt, |
| 648 | thread_state_t new_state, mach_msg_type_number_t * new_stateCnt) |
groebert@google.com | c659c0e | 2015-02-13 11:17:00 +0000 | [diff] [blame] | 649 | { |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 650 | LOGMSG(l_FATAL, "This function should never get called"); |
| 651 | return KERN_SUCCESS; |
| 652 | } |
| 653 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 654 | kern_return_t catch_mach_exception_raise_state_identity( __attribute__ ((unused)) |
| 655 | exception_port_t exception_port, |
| 656 | thread_port_t thread, |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 657 | task_port_t task, |
| 658 | exception_type_t exception, |
| 659 | mach_exception_data_t code, |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 660 | mach_msg_type_number_t |
| 661 | code_count, int *flavor, |
| 662 | thread_state_t in_state, |
| 663 | mach_msg_type_number_t |
| 664 | in_state_count, |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 665 | thread_state_t out_state, |
groebert@google.com | c659c0e | 2015-02-13 11:17:00 +0000 | [diff] [blame] | 666 | mach_msg_type_number_t * out_state_count) |
| 667 | { |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 668 | if (exception != EXC_CRASH) { |
| 669 | LOGMSG(l_FATAL, "Got non EXC_CRASH! This should not happen."); |
| 670 | } |
| 671 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 672 | /* |
| 673 | * We will save our results to the honggfuzz_t global |
| 674 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 675 | pid_t pid; |
| 676 | pid_for_task(task, &pid); |
| 677 | LOGMSG(l_DEBUG, "Crash of pid %d", pid); |
| 678 | |
groebert@google.com | 20e368f | 2015-02-13 14:19:25 +0000 | [diff] [blame] | 679 | fuzzer_t *fuzzer = &g_fuzzer_crash_information[pid]; |
robert.swiecki@gmail.com | c6d2875 | 2014-01-08 12:02:37 +0000 | [diff] [blame] | 680 | |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 681 | /* |
| 682 | * Get program counter. |
| 683 | * Cast to void* in order to silence the alignment warnings |
| 684 | */ |
| 685 | |
| 686 | x86_thread_state_t *platform_in_state = ((x86_thread_state_t *) (void *)in_state); |
| 687 | |
| 688 | if (x86_THREAD_STATE32 == platform_in_state->tsh.flavor) { |
groebert@google.com | 20e368f | 2015-02-13 14:19:25 +0000 | [diff] [blame] | 689 | fuzzer->pc = platform_in_state->uts.ts32.__eip; |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 690 | } else { |
groebert@google.com | 20e368f | 2015-02-13 14:19:25 +0000 | [diff] [blame] | 691 | fuzzer->pc = platform_in_state->uts.ts64.__rip; |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 692 | } |
| 693 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 694 | /* |
| 695 | * Get the exception type |
| 696 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 697 | |
| 698 | exception_type_t exception_type = ((code[0] >> 20) & 0x0F); |
| 699 | |
| 700 | if (exception_type == 0) { |
| 701 | exception_type = EXC_CRASH; |
| 702 | } |
| 703 | |
groebert@google.com | 20e368f | 2015-02-13 14:19:25 +0000 | [diff] [blame] | 704 | fuzzer->exception = exception_type; |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 705 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 706 | /* |
| 707 | * Get the access address. |
| 708 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 709 | |
| 710 | mach_exception_data_type_t exception_data[2]; |
| 711 | memcpy(exception_data, code, sizeof(exception_data)); |
| 712 | exception_data[0] = (code[0] & ~(0x00000000FFF00000)); |
| 713 | exception_data[1] = code[1]; |
| 714 | |
| 715 | mach_exception_data_type_t access_address = exception_data[1]; |
groebert@google.com | 20e368f | 2015-02-13 14:19:25 +0000 | [diff] [blame] | 716 | fuzzer->access = (uint64_t) access_address; |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 717 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 718 | /* |
| 719 | * Get a hash of the callstack |
| 720 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 721 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 722 | uint64_t hash = hash_callstack(thread, task, exception, code, code_count, flavor, |
| 723 | in_state, in_state_count); |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 724 | |
groebert@google.com | 20e368f | 2015-02-13 14:19:25 +0000 | [diff] [blame] | 725 | fuzzer->backtrace = hash; |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 726 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 727 | /* |
| 728 | * Cleanup |
| 729 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 730 | |
| 731 | if (mach_port_deallocate(mach_task_self(), task) != KERN_SUCCESS) { |
| 732 | LOGMSG(l_WARN, "Exception Handler: Could not deallocate task"); |
| 733 | } |
| 734 | |
| 735 | if (mach_port_deallocate(mach_task_self(), thread) != KERN_SUCCESS) { |
| 736 | LOGMSG(l_WARN, "Exception Handler: Could not deallocate thread"); |
| 737 | } |
| 738 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame^] | 739 | return KERN_SUCCESS; // KERN_SUCCESS indicates that this should |
| 740 | // not be forwarded to other crash |
| 741 | // handlers |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 742 | } |