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