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 | 90e9911 | 2015-02-15 02:05:14 +0000 | [diff] [blame] | 3 | * honggfuzz - fuzzing routines |
| 4 | * ----------------------------------------- |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 5 | * |
robert.swiecki@gmail.com | 8531f69 | 2015-02-17 12:25:36 +0000 | [diff] [blame] | 6 | * Author: |
| 7 | * Robert Swiecki <swiecki@google.com> |
| 8 | * Felix Gröbert <groebert@google.com> |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 9 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 10 | * Copyright 2010-2015 by Google Inc. All Rights Reserved. |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 11 | * |
| 12 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 13 | * 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] | 14 | * a copy of the License at |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 15 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 16 | * http://www.apache.org/licenses/LICENSE-2.0 |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 17 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 18 | * Unless required by applicable law or agreed to in writing, software |
| 19 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 21 | * implied. See the License for the specific language governing |
| 22 | * permissions and limitations under the License. |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 23 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 24 | */ |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 25 | |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 26 | #include "common.h" |
| 27 | #include "fuzz.h" |
| 28 | |
| 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
robert.swiecki@gmail.com | 90e9911 | 2015-02-15 02:05:14 +0000 | [diff] [blame] | 31 | #include <inttypes.h> |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 32 | #include <pthread.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 33 | #include <signal.h> |
| 34 | #include <stddef.h> |
| 35 | #include <stdint.h> |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 39 | #include <sys/param.h> |
| 40 | #include <sys/stat.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 41 | #include <sys/time.h> |
| 42 | #include <sys/types.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 43 | #include <time.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 44 | #include <unistd.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 45 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 46 | #include "arch.h" |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 47 | #include "files.h" |
robert.swiecki@gmail.com | e7190b9 | 2015-02-14 23:05:42 +0000 | [diff] [blame] | 48 | #include "log.h" |
robert.swiecki@gmail.com | 36700b5 | 2015-02-22 05:03:16 +0000 | [diff] [blame] | 49 | #include "mangle.h" |
robert.swiecki@gmail.com | e7190b9 | 2015-02-14 23:05:42 +0000 | [diff] [blame] | 50 | #include "report.h" |
Jagger | 0026560 | 2016-03-10 02:36:27 +0100 | [diff] [blame] | 51 | #include "sancov.h" |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 52 | #include "subproc.h" |
robert.swiecki@gmail.com | e7190b9 | 2015-02-14 23:05:42 +0000 | [diff] [blame] | 53 | #include "util.h" |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 54 | |
Jagger | 6113c43 | 2015-09-24 05:00:28 +0200 | [diff] [blame] | 55 | static pthread_t fuzz_mainThread; |
| 56 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 57 | static void fuzz_getFileName(honggfuzz_t * hfuzz, char *fileName) |
| 58 | { |
robert.swiecki | ba51263 | 2011-01-28 11:57:26 +0000 | [diff] [blame] | 59 | struct timeval tv; |
| 60 | gettimeofday(&tv, NULL); |
| 61 | |
Jagger | c44a89a | 2016-09-25 16:42:44 +0200 | [diff] [blame] | 62 | snprintf(fileName, PATH_MAX, "%s/.honggfuzz.%d.%lu.%" PRIx64 ".%s", hfuzz->workDir, |
| 63 | (int)getpid(), (unsigned long int)tv.tv_sec, util_rnd64(), hfuzz->fileExtn); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 66 | static bool fuzz_prepareFileDynamically(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
robert.swiecki@gmail.com | 6d6f756 | 2015-02-17 22:18:51 +0000 | [diff] [blame] | 67 | { |
Robert Swiecki | 3bfc33c | 2016-03-14 18:12:41 +0100 | [diff] [blame] | 68 | struct dynfile_t *dynfile; |
Robert Swiecki | a08ab1c | 2016-03-14 18:29:21 +0100 | [diff] [blame] | 69 | |
| 70 | { |
Robert Swiecki | 76ecd5e | 2016-03-16 14:57:03 +0100 | [diff] [blame] | 71 | MX_SCOPED_LOCK(&hfuzz->dynfileq_mutex); |
Robert Swiecki | 4b0390a | 2016-03-14 18:44:05 +0100 | [diff] [blame] | 72 | |
Jagger | b070b81 | 2016-03-15 05:31:30 +0100 | [diff] [blame] | 73 | if (hfuzz->dynfileqCnt == 0) { |
Jagger | e449651 | 2016-03-16 01:55:46 +0100 | [diff] [blame] | 74 | LOG_F("The dynamic file corpus is empty. Apparently, the initial fuzzing of the " |
| 75 | "provided file corpus (-f) has not produced any follow-up files with positive " |
| 76 | "coverage and/or CPU counters"); |
Jagger | b070b81 | 2016-03-15 05:31:30 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Robert Swiecki | 4b0390a | 2016-03-14 18:44:05 +0100 | [diff] [blame] | 79 | size_t i = 0U; |
Robert Swiecki | a08ab1c | 2016-03-14 18:29:21 +0100 | [diff] [blame] | 80 | size_t dynFilePos = util_rndGet(0, hfuzz->dynfileqCnt - 1); |
| 81 | TAILQ_FOREACH(dynfile, &hfuzz->dynfileq, pointers) { |
| 82 | if (i++ == dynFilePos) { |
| 83 | break; |
| 84 | } |
Robert Swiecki | 3bfc33c | 2016-03-14 18:12:41 +0100 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | memcpy(fuzzer->dynamicFile, dynfile->data, dynfile->size); |
Robert Swiecki | a08ab1c | 2016-03-14 18:29:21 +0100 | [diff] [blame] | 89 | fuzzer->dynamicFileSz = dynfile->size; |
Robert Swiecki | 3bfc33c | 2016-03-14 18:12:41 +0100 | [diff] [blame] | 90 | |
Anestis Bechtsoudis | 6ef24e4 | 2016-01-13 13:05:53 +0200 | [diff] [blame] | 91 | mangle_Resize(hfuzz, fuzzer->dynamicFile, &fuzzer->dynamicFileSz); |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 92 | mangle_mangleContent(hfuzz, fuzzer); |
robert.swiecki@gmail.com | 6d6f756 | 2015-02-17 22:18:51 +0000 | [diff] [blame] | 93 | |
Jagger | 1d74338 | 2016-04-02 02:09:41 +0200 | [diff] [blame] | 94 | if (hfuzz->persistent == false && files_writeBufToFile |
robert.swiecki@gmail.com | 62e34ae | 2015-03-05 03:39:32 +0000 | [diff] [blame] | 95 | (fuzzer->fileName, fuzzer->dynamicFile, fuzzer->dynamicFileSz, |
Jagger | 5a3c4c3 | 2016-04-16 19:27:47 +0200 | [diff] [blame] | 96 | O_WRONLY | O_CREAT | O_EXCL | O_TRUNC | O_CLOEXEC) == false) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 97 | LOG_E("Couldn't write buffer to file '%s'", fuzzer->fileName); |
robert.swiecki@gmail.com | 3b6c629 | 2015-02-26 11:48:46 +0000 | [diff] [blame] | 98 | return false; |
| 99 | } |
robert.swiecki@gmail.com | 6d6f756 | 2015-02-17 22:18:51 +0000 | [diff] [blame] | 100 | |
robert.swiecki@gmail.com | 6d6f756 | 2015-02-17 22:18:51 +0000 | [diff] [blame] | 101 | return true; |
robert.swiecki@gmail.com | d7aed31 | 2015-02-03 21:26:37 +0000 | [diff] [blame] | 102 | } |
| 103 | |
robert.swiecki@gmail.com | 4a7a9d8 | 2015-03-01 01:25:16 +0000 | [diff] [blame] | 104 | static bool fuzz_prepareFile(honggfuzz_t * hfuzz, fuzzer_t * fuzzer, int rnd_index) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 105 | { |
Robert Swiecki | e8f8e8d | 2016-10-03 23:51:32 +0200 | [diff] [blame] | 106 | struct paths_t *file = files_getFileFromFileq(hfuzz, rnd_index); |
| 107 | |
| 108 | ssize_t fileSz = files_readFileToBufMax(file->path, fuzzer->dynamicFile, hfuzz->maxFileSz); |
Jagger | 1aa94d7 | 2016-04-02 02:37:35 +0200 | [diff] [blame] | 109 | if (fileSz < 0) { |
Robert Swiecki | e8f8e8d | 2016-10-03 23:51:32 +0200 | [diff] [blame] | 110 | LOG_E("Couldn't read contents of '%s'", file->path); |
robert.swiecki@gmail.com | bb5d264 | 2015-02-25 20:00:00 +0000 | [diff] [blame] | 111 | return false; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 112 | } |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 113 | fuzzer->dynamicFileSz = fileSz; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 114 | |
Anestis Bechtsoudis | 25262b3 | 2015-11-07 22:39:47 +0200 | [diff] [blame] | 115 | /* If flip rate is 0.0, early abort file mangling */ |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 116 | if (fuzzer->flipRate != 0.0L) { |
| 117 | mangle_Resize(hfuzz, fuzzer->dynamicFile, &fuzzer->dynamicFileSz); |
| 118 | mangle_mangleContent(hfuzz, fuzzer); |
Anestis Bechtsoudis | 25262b3 | 2015-11-07 22:39:47 +0200 | [diff] [blame] | 119 | } |
robert.swiecki@gmail.com | c070b94 | 2015-02-25 18:29:19 +0000 | [diff] [blame] | 120 | |
Jagger | 1d74338 | 2016-04-02 02:09:41 +0200 | [diff] [blame] | 121 | if (hfuzz->persistent == false && files_writeBufToFile |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 122 | (fuzzer->fileName, fuzzer->dynamicFile, fuzzer->dynamicFileSz, |
Jagger | 5a3c4c3 | 2016-04-16 19:27:47 +0200 | [diff] [blame] | 123 | O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC) == false) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 124 | LOG_E("Couldn't write buffer to file '%s'", fuzzer->fileName); |
robert.swiecki@gmail.com | e768052 | 2015-02-22 22:22:37 +0000 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 128 | return true; |
| 129 | } |
| 130 | |
Jagger | a34b302 | 2016-04-02 02:20:40 +0200 | [diff] [blame] | 131 | static bool fuzz_prepareFileExternally(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 132 | { |
Jagger | 5a3c4c3 | 2016-04-16 19:27:47 +0200 | [diff] [blame] | 133 | int dstfd = open(fuzzer->fileName, O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, 0644); |
Jagger | a34b302 | 2016-04-02 02:20:40 +0200 | [diff] [blame] | 134 | if (dstfd == -1) { |
| 135 | PLOG_E("Couldn't create a temporary file '%s'", fuzzer->fileName); |
| 136 | return false; |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame] | 137 | } |
Jagger | a34b302 | 2016-04-02 02:20:40 +0200 | [diff] [blame] | 138 | close(dstfd); |
| 139 | |
| 140 | LOG_D("Created '%s' as an input file", fuzzer->fileName); |
robert.swiecki | 3d505e2 | 2010-10-14 01:17:17 +0000 | [diff] [blame] | 141 | |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 142 | const char *const argv[] = { hfuzz->externalCommand, fuzzer->fileName, NULL }; |
| 143 | if (subproc_System(argv) != 0) { |
| 144 | LOG_E("Subprocess '%s' returned abnormally", hfuzz->externalCommand); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 145 | return false; |
| 146 | } |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 147 | LOG_D("Subporcess '%s' finished with success", hfuzz->externalCommand); |
Jagger | 1d74338 | 2016-04-02 02:09:41 +0200 | [diff] [blame] | 148 | |
| 149 | ssize_t rsz = files_readFileToBufMax(fuzzer->fileName, fuzzer->dynamicFile, hfuzz->maxFileSz); |
Jagger | 1aa94d7 | 2016-04-02 02:37:35 +0200 | [diff] [blame] | 150 | if (rsz < 0) { |
Jagger | 1d74338 | 2016-04-02 02:09:41 +0200 | [diff] [blame] | 151 | LOG_W("Couldn't read back '%s' to the buffer", fuzzer->fileName); |
| 152 | return false; |
| 153 | } |
| 154 | fuzzer->dynamicFileSz = rsz; |
| 155 | |
| 156 | if (hfuzz->persistent) { |
| 157 | unlink(fuzzer->fileName); |
| 158 | } |
| 159 | |
| 160 | return true; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Robert Swiecki | ee266ac | 2016-10-03 02:25:59 +0200 | [diff] [blame] | 163 | static bool fuzz_postProcessFile(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
| 164 | { |
| 165 | if (hfuzz->persistent == true) { |
| 166 | LOG_F("Postprocessing file in persistent mode not yet supported"); |
| 167 | } |
| 168 | |
| 169 | const char *const argv[] = { hfuzz->postExternalCommand, fuzzer->fileName, NULL }; |
| 170 | if (subproc_System(argv) != 0) { |
| 171 | LOG_E("Subprocess '%s' returned abnormally", hfuzz->postExternalCommand); |
| 172 | return false; |
| 173 | } |
| 174 | LOG_D("Subporcess '%s' finished with success", hfuzz->externalCommand); |
| 175 | |
| 176 | ssize_t rsz = files_readFileToBufMax(fuzzer->fileName, fuzzer->dynamicFile, hfuzz->maxFileSz); |
| 177 | if (rsz < 0) { |
| 178 | LOG_W("Couldn't read back '%s' to the buffer", fuzzer->fileName); |
| 179 | return false; |
| 180 | } |
| 181 | fuzzer->dynamicFileSz = rsz; |
| 182 | |
| 183 | return true; |
| 184 | } |
| 185 | |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 186 | static bool fuzz_runVerifier(honggfuzz_t * hfuzz, fuzzer_t * crashedFuzzer) |
| 187 | { |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 188 | int crashFd = -1; |
| 189 | uint8_t *crashBuf = NULL; |
| 190 | off_t crashFileSz = 0; |
| 191 | |
| 192 | crashBuf = files_mapFile(crashedFuzzer->crashFileName, &crashFileSz, &crashFd, false); |
| 193 | if (crashBuf == NULL) { |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 194 | LOG_E("Couldn't open and map '%s' in R/O mode", crashedFuzzer->crashFileName); |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame] | 195 | return false; |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 196 | } |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 197 | defer { |
| 198 | munmap(crashBuf, crashFileSz); |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 199 | close(crashFd); |
| 200 | }; |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 201 | |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 202 | LOG_I("Launching verifier for %" PRIx64 " hash", crashedFuzzer->backtrace); |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 203 | for (int i = 0; i < _HF_VERIFIER_ITER; i++) { |
| 204 | fuzzer_t vFuzzer = { |
| 205 | .pid = 0, |
Anestis Bechtsoudis | 92b9704 | 2016-04-27 12:34:19 +0300 | [diff] [blame] | 206 | .persistentPid = 0, |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 207 | .timeStartedMillis = util_timeNowMillis(), |
Anestis Bechtsoudis | ecb0a66 | 2015-09-27 18:19:46 +0300 | [diff] [blame] | 208 | .crashFileName = {0}, |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 209 | .pc = 0ULL, |
| 210 | .backtrace = 0ULL, |
| 211 | .access = 0ULL, |
| 212 | .exception = 0, |
| 213 | .dynamicFileSz = 0, |
| 214 | .dynamicFile = NULL, |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 215 | .sanCovCnts = { |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 216 | .hitBBCnt = 0ULL, |
| 217 | .totalBBCnt = 0ULL, |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 218 | .dsoCnt = 0ULL, |
| 219 | .iDsoCnt = 0ULL, |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 220 | .newBBCnt = 0ULL, |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 221 | .crashesCnt = 0ULL, |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 222 | }, |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 223 | .report = {'\0'}, |
Jagger | b2c1fad | 2016-03-17 23:37:10 +0100 | [diff] [blame] | 224 | .mainWorker = false, |
Jagger | fa3544a | 2016-08-30 02:55:55 +0200 | [diff] [blame] | 225 | .fuzzNo = crashedFuzzer->fuzzNo, |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 226 | .persistentSock = -1, |
Jagger | b2c1fad | 2016-03-17 23:37:10 +0100 | [diff] [blame] | 227 | |
Jagger | 2381ef4 | 2016-03-20 23:32:05 +0100 | [diff] [blame] | 228 | .linux = { |
| 229 | .hwCnts = { |
| 230 | .cpuInstrCnt = 0ULL, |
| 231 | .cpuBranchCnt = 0ULL, |
Jagger | 2381ef4 | 2016-03-20 23:32:05 +0100 | [diff] [blame] | 232 | .bbCnt = 0ULL, |
| 233 | .newBBCnt = 0ULL, |
Jagger | 34789a7 | 2016-09-08 00:36:09 +0200 | [diff] [blame] | 234 | .softCntPc = 0ULL, |
| 235 | .softCntCmp = 0ULL, |
Jagger | 2381ef4 | 2016-03-20 23:32:05 +0100 | [diff] [blame] | 236 | }, |
| 237 | .perfMmapBuf = NULL, |
| 238 | .perfMmapAux = NULL, |
Anestis Bechtsoudis | 92b9704 | 2016-04-27 12:34:19 +0300 | [diff] [blame] | 239 | .attachedPid = 0, |
Jagger | 2381ef4 | 2016-03-20 23:32:05 +0100 | [diff] [blame] | 240 | }, |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 241 | }; |
| 242 | |
Anestis Bechtsoudis | 92b9704 | 2016-04-27 12:34:19 +0300 | [diff] [blame] | 243 | if (arch_archThreadInit(hfuzz, &vFuzzer) == false) { |
| 244 | LOG_F("Could not initialize the thread"); |
| 245 | } |
| 246 | |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 247 | fuzz_getFileName(hfuzz, vFuzzer.fileName); |
| 248 | if (files_writeBufToFile |
Jagger | 33cce5d | 2016-04-16 20:10:36 +0200 | [diff] [blame] | 249 | (vFuzzer.fileName, crashBuf, crashFileSz, |
| 250 | O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC) == false) { |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 251 | LOG_E("Couldn't write buffer to file '%s'", vFuzzer.fileName); |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame] | 252 | return false; |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 253 | } |
| 254 | |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame^] | 255 | if (subproc_Run(hfuzz, &vFuzzer) == false) { |
| 256 | LOG_F("subproc_Run()"); |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 257 | } |
Robert Swiecki | a8b8cb8 | 2016-08-17 18:27:08 +0200 | [diff] [blame] | 258 | |
Anestis Bechtsoudis | ecb0a66 | 2015-09-27 18:19:46 +0300 | [diff] [blame] | 259 | /* If stack hash doesn't match skip name tag and exit */ |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 260 | if (crashedFuzzer->backtrace != vFuzzer.backtrace) { |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 261 | LOG_D("Verifier stack hash mismatch"); |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame] | 262 | return false; |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | /* Workspace is inherited, just append a extra suffix */ |
| 267 | char verFile[PATH_MAX] = { 0 }; |
| 268 | snprintf(verFile, sizeof(verFile), "%s.verified", crashedFuzzer->crashFileName); |
Anestis Bechtsoudis | ecb0a66 | 2015-09-27 18:19:46 +0300 | [diff] [blame] | 269 | |
Anestis Bechtsoudis | d86e660 | 2015-11-07 18:34:06 +0200 | [diff] [blame] | 270 | /* Copy file with new suffix & remove original copy */ |
| 271 | bool dstFileExists = false; |
| 272 | if (files_copyFile(crashedFuzzer->crashFileName, verFile, &dstFileExists)) { |
| 273 | LOG_I("Successfully verified, saving as (%s)", verFile); |
Jagger | d34417d | 2016-03-16 01:26:54 +0100 | [diff] [blame] | 274 | ATOMIC_POST_INC(hfuzz->verifiedCrashesCnt); |
Anestis Bechtsoudis | d86e660 | 2015-11-07 18:34:06 +0200 | [diff] [blame] | 275 | unlink(crashedFuzzer->crashFileName); |
| 276 | } else { |
| 277 | if (dstFileExists) { |
| 278 | LOG_I("It seems that '%s' already exists, skipping", verFile); |
| 279 | } else { |
| 280 | LOG_E("Couldn't copy '%s' to '%s'", crashedFuzzer->crashFileName, verFile); |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame] | 281 | return false; |
Anestis Bechtsoudis | d86e660 | 2015-11-07 18:34:06 +0200 | [diff] [blame] | 282 | } |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 283 | } |
Anestis Bechtsoudis | ecb0a66 | 2015-09-27 18:19:46 +0300 | [diff] [blame] | 284 | |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame] | 285 | return true; |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 286 | } |
| 287 | |
Jagger | 3c7e7ce | 2016-09-25 16:05:19 +0200 | [diff] [blame] | 288 | static fuzzState_t fuzz_getState(honggfuzz_t * hfuzz) |
| 289 | { |
| 290 | return ATOMIC_GET(hfuzz->state); |
| 291 | } |
| 292 | |
| 293 | static void fuzz_setState(honggfuzz_t * hfuzz, fuzzState_t state) |
| 294 | { |
| 295 | ATOMIC_SET(hfuzz->state, state); |
| 296 | } |
| 297 | |
Robert Swiecki | d258ff8 | 2016-08-29 17:54:20 +0200 | [diff] [blame] | 298 | static void fuzz_addFileToFileQLocked(honggfuzz_t * hfuzz, uint8_t * data, size_t size) |
Robert Swiecki | f3534bb | 2016-03-14 18:55:10 +0100 | [diff] [blame] | 299 | { |
Robert Swiecki | 7084e65 | 2016-03-14 19:47:00 +0100 | [diff] [blame] | 300 | struct dynfile_t *dynfile = (struct dynfile_t *)util_Malloc(sizeof(struct dynfile_t)); |
Robert Swiecki | f3534bb | 2016-03-14 18:55:10 +0100 | [diff] [blame] | 301 | dynfile->size = size; |
Robert Swiecki | 7084e65 | 2016-03-14 19:47:00 +0100 | [diff] [blame] | 302 | dynfile->data = (uint8_t *) util_Malloc(size); |
Robert Swiecki | f3534bb | 2016-03-14 18:55:10 +0100 | [diff] [blame] | 303 | memcpy(dynfile->data, data, size); |
| 304 | TAILQ_INSERT_TAIL(&hfuzz->dynfileq, dynfile, pointers); |
| 305 | hfuzz->dynfileqCnt++; |
| 306 | |
Jagger | 3c7e7ce | 2016-09-25 16:05:19 +0200 | [diff] [blame] | 307 | /* No need to add new coverage if we are supposed to append new coverage-inducing inputs only */ |
Jagger | 1b2d482 | 2016-09-25 16:19:45 +0200 | [diff] [blame] | 308 | if (fuzz_getState(hfuzz) == _HF_STATE_DYNAMIC_PRE && hfuzz->covDir == NULL) { |
| 309 | LOG_D("New coverage found, but we're in the initial coverage assessment state. Skipping"); |
Jagger | 3c7e7ce | 2016-09-25 16:05:19 +0200 | [diff] [blame] | 310 | return; |
| 311 | } |
| 312 | |
Jagger | b30b481 | 2016-09-25 16:34:15 +0200 | [diff] [blame] | 313 | char tmstr[512]; |
Jagger | 3968ba3 | 2016-09-25 16:44:31 +0200 | [diff] [blame] | 314 | util_getLocalTime("%Y%m%d.%H%M%S", tmstr, sizeof(tmstr), time(NULL)); |
Robert Swiecki | f3534bb | 2016-03-14 18:55:10 +0100 | [diff] [blame] | 315 | char fname[PATH_MAX]; |
Jagger | 1b2d482 | 2016-09-25 16:19:45 +0200 | [diff] [blame] | 316 | if (hfuzz->covDir == NULL) { |
Jagger | b30b481 | 2016-09-25 16:34:15 +0200 | [diff] [blame] | 317 | snprintf(fname, sizeof(fname), "%s/TIME.%s.ITER.%" PRIu64 ".RND.%" PRIx64 ".HONGGFUZZ.COV", |
Jagger | c44a89a | 2016-09-25 16:42:44 +0200 | [diff] [blame] | 318 | hfuzz->inputDir, tmstr, (uint64_t) ATOMIC_GET(hfuzz->mutationsCnt), util_rnd64()); |
Jagger | 3c7e7ce | 2016-09-25 16:05:19 +0200 | [diff] [blame] | 319 | } else { |
Jagger | b30b481 | 2016-09-25 16:34:15 +0200 | [diff] [blame] | 320 | snprintf(fname, sizeof(fname), "%s/TIME.%s.ITER.%" PRIu64 ".RND.%" PRIx64 ".HONGGFUZZ.COV", |
Jagger | c44a89a | 2016-09-25 16:42:44 +0200 | [diff] [blame] | 321 | hfuzz->covDir, tmstr, (uint64_t) ATOMIC_GET(hfuzz->mutationsCnt), util_rnd64()); |
Jagger | 3c7e7ce | 2016-09-25 16:05:19 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Jagger | 33cce5d | 2016-04-16 20:10:36 +0200 | [diff] [blame] | 324 | if (files_writeBufToFile(fname, data, size, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC | O_CLOEXEC) |
| 325 | == false) { |
Robert Swiecki | f3534bb | 2016-03-14 18:55:10 +0100 | [diff] [blame] | 326 | LOG_W("Couldn't write buffer to file '%s'", fname); |
| 327 | } |
| 328 | } |
| 329 | |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 330 | static void fuzz_perfFeedback(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
| 331 | { |
| 332 | LOG_D |
Jagger | 395df02 | 2016-08-21 01:13:25 +0200 | [diff] [blame] | 333 | ("New file size: %zu, Perf feedback new/cur (instr,branch): %" PRIu64 "/%" PRIu64 "/%" |
Robert Swiecki | 3bfc33c | 2016-03-14 18:12:41 +0100 | [diff] [blame] | 334 | PRIu64 "/%" PRIu64 ", BBcnt new/total: %" PRIu64 "/%" PRIu64, fuzzer->dynamicFileSz, |
Jagger | 247c3b4 | 2016-03-21 23:24:05 +0100 | [diff] [blame] | 335 | fuzzer->linux.hwCnts.cpuInstrCnt, hfuzz->linux.hwCnts.cpuInstrCnt, |
| 336 | fuzzer->linux.hwCnts.cpuBranchCnt, hfuzz->linux.hwCnts.cpuBranchCnt, |
| 337 | fuzzer->linux.hwCnts.newBBCnt, hfuzz->linux.hwCnts.bbCnt); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 338 | |
Robert Swiecki | 76ecd5e | 2016-03-16 14:57:03 +0100 | [diff] [blame] | 339 | MX_SCOPED_LOCK(&hfuzz->dynfileq_mutex); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 340 | |
Jagger | 34789a7 | 2016-09-08 00:36:09 +0200 | [diff] [blame] | 341 | uint64_t softCntPc = 0UL; |
| 342 | uint64_t softCntCmp = 0UL; |
Jagger | 251d019 | 2016-08-24 00:54:04 +0200 | [diff] [blame] | 343 | if (hfuzz->bbFd != -1) { |
Jagger | 34789a7 | 2016-09-08 00:36:09 +0200 | [diff] [blame] | 344 | softCntPc = ATOMIC_GET(hfuzz->feedback->pidFeedbackPc[fuzzer->fuzzNo]); |
| 345 | ATOMIC_CLEAR(hfuzz->feedback->pidFeedbackPc[fuzzer->fuzzNo]); |
| 346 | softCntCmp = ATOMIC_GET(hfuzz->feedback->pidFeedbackCmp[fuzzer->fuzzNo]); |
| 347 | ATOMIC_CLEAR(hfuzz->feedback->pidFeedbackCmp[fuzzer->fuzzNo]); |
Jagger | 251d019 | 2016-08-24 00:54:04 +0200 | [diff] [blame] | 348 | } |
Jagger | b01aaae | 2016-08-20 03:35:38 +0200 | [diff] [blame] | 349 | |
Jagger | 302c2ea | 2016-09-07 03:54:43 +0200 | [diff] [blame] | 350 | int64_t diff0 = hfuzz->linux.hwCnts.cpuInstrCnt - fuzzer->linux.hwCnts.cpuInstrCnt; |
| 351 | int64_t diff1 = hfuzz->linux.hwCnts.cpuBranchCnt - fuzzer->linux.hwCnts.cpuBranchCnt; |
Jagger | 302c2ea | 2016-09-07 03:54:43 +0200 | [diff] [blame] | 352 | |
Jagger | 8d0002c | 2016-08-17 03:44:23 +0200 | [diff] [blame] | 353 | /* |
| 354 | * Coverage is the primary counter, the rest is secondary, and taken into consideration only |
| 355 | * if the coverage counter has not been changed |
| 356 | */ |
Jagger | 34789a7 | 2016-09-08 00:36:09 +0200 | [diff] [blame] | 357 | if (fuzzer->linux.hwCnts.newBBCnt > 0 || softCntPc > 0 || softCntCmp > 0 || diff0 < 0 |
Robert Swiecki | d158aac | 2016-11-01 23:14:12 +0100 | [diff] [blame] | 358 | || diff1 < 0) { |
Jagger | 247c3b4 | 2016-03-21 23:24:05 +0100 | [diff] [blame] | 359 | hfuzz->linux.hwCnts.cpuInstrCnt = fuzzer->linux.hwCnts.cpuInstrCnt; |
| 360 | hfuzz->linux.hwCnts.cpuBranchCnt = fuzzer->linux.hwCnts.cpuBranchCnt; |
Jagger | 247c3b4 | 2016-03-21 23:24:05 +0100 | [diff] [blame] | 361 | hfuzz->linux.hwCnts.bbCnt += fuzzer->linux.hwCnts.newBBCnt; |
Jagger | 34789a7 | 2016-09-08 00:36:09 +0200 | [diff] [blame] | 362 | hfuzz->linux.hwCnts.softCntPc += softCntPc; |
| 363 | hfuzz->linux.hwCnts.softCntCmp += softCntCmp; |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 364 | |
Jagger | 395df02 | 2016-08-21 01:13:25 +0200 | [diff] [blame] | 365 | LOG_I |
Robert Swiecki | d158aac | 2016-11-01 23:14:12 +0100 | [diff] [blame] | 366 | ("New: %zu B., (instr,branch,soft,soft-cmp,perf): %" PRIu64 "/%" |
| 367 | PRIu64 "/%" PRIu64 "/%" PRIu64 "/%" PRIu64 ", Total: %" PRIu64 |
Jagger | 91c687c | 2016-09-07 03:51:21 +0200 | [diff] [blame] | 368 | "/%" PRIu64 "/%" PRIu64 "/%" PRIu64 "/%" PRIu64, fuzzer->dynamicFileSz, |
Jagger | 34789a7 | 2016-09-08 00:36:09 +0200 | [diff] [blame] | 369 | fuzzer->linux.hwCnts.cpuInstrCnt, fuzzer->linux.hwCnts.cpuBranchCnt, softCntPc, |
Robert Swiecki | d158aac | 2016-11-01 23:14:12 +0100 | [diff] [blame] | 370 | softCntCmp, fuzzer->linux.hwCnts.newBBCnt, |
Jagger | 91c687c | 2016-09-07 03:51:21 +0200 | [diff] [blame] | 371 | hfuzz->linux.hwCnts.cpuInstrCnt, hfuzz->linux.hwCnts.cpuBranchCnt, |
Jagger | 34789a7 | 2016-09-08 00:36:09 +0200 | [diff] [blame] | 372 | hfuzz->linux.hwCnts.softCntPc, hfuzz->linux.hwCnts.softCntCmp, |
Robert Swiecki | d158aac | 2016-11-01 23:14:12 +0100 | [diff] [blame] | 373 | hfuzz->linux.hwCnts.bbCnt); |
Jagger | 395df02 | 2016-08-21 01:13:25 +0200 | [diff] [blame] | 374 | |
Robert Swiecki | d258ff8 | 2016-08-29 17:54:20 +0200 | [diff] [blame] | 375 | fuzz_addFileToFileQLocked(hfuzz, fuzzer->dynamicFile, fuzzer->dynamicFileSz); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 376 | } |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | static void fuzz_sanCovFeedback(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
| 380 | { |
| 381 | LOG_D |
Robert Swiecki | 3bfc33c | 2016-03-14 18:12:41 +0100 | [diff] [blame] | 382 | ("File size (Best/New): %zu, SanCov feedback (bb,dso): Best: [%" PRIu64 |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 383 | ",%" PRIu64 "] / New: [%" PRIu64 ",%" PRIu64 "], newBBs:%" PRIu64, |
Robert Swiecki | 3bfc33c | 2016-03-14 18:12:41 +0100 | [diff] [blame] | 384 | fuzzer->dynamicFileSz, hfuzz->sanCovCnts.hitBBCnt, |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 385 | hfuzz->sanCovCnts.iDsoCnt, fuzzer->sanCovCnts.hitBBCnt, fuzzer->sanCovCnts.iDsoCnt, |
| 386 | fuzzer->sanCovCnts.newBBCnt); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 387 | |
Robert Swiecki | 76ecd5e | 2016-03-16 14:57:03 +0100 | [diff] [blame] | 388 | MX_SCOPED_LOCK(&hfuzz->dynfileq_mutex); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 389 | |
Jagger | d573837 | 2016-08-17 20:12:15 +0200 | [diff] [blame] | 390 | int64_t diff0 = hfuzz->linux.hwCnts.cpuInstrCnt - fuzzer->linux.hwCnts.cpuInstrCnt; |
| 391 | int64_t diff1 = hfuzz->linux.hwCnts.cpuBranchCnt - fuzzer->linux.hwCnts.cpuBranchCnt; |
Jagger | d573837 | 2016-08-17 20:12:15 +0200 | [diff] [blame] | 392 | |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 393 | /* |
| 394 | * Keep mutated seed if: |
Robert Swiecki | 142f941 | 2016-03-14 19:22:01 +0100 | [diff] [blame] | 395 | * a) Newly discovered (not met before) BBs |
| 396 | * b) More instrumented DSOs loaded |
Robert Swiecki | 23ec02a | 2016-01-19 18:47:45 +0100 | [diff] [blame] | 397 | * |
Anestis Bechtsoudis | b78cf60 | 2016-01-07 13:10:50 +0200 | [diff] [blame] | 398 | * TODO: (a) method can significantly assist to further improvements in interesting areas |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 399 | * discovery if combined with seeds pool/queue support. If a runtime queue is maintained |
| 400 | * more interesting seeds can be saved between runs instead of instantly discarded |
| 401 | * based on current absolute elitism (only one mutated seed is promoted). |
| 402 | */ |
Jagger | d573837 | 2016-08-17 20:12:15 +0200 | [diff] [blame] | 403 | |
| 404 | bool newCov = (fuzzer->sanCovCnts.newBBCnt > 0 |
| 405 | || hfuzz->sanCovCnts.iDsoCnt < fuzzer->sanCovCnts.iDsoCnt); |
| 406 | |
Robert Swiecki | d158aac | 2016-11-01 23:14:12 +0100 | [diff] [blame] | 407 | if (newCov || (diff0 < 0 || diff1 < 0)) { |
Robert Swiecki | 3bfc33c | 2016-03-14 18:12:41 +0100 | [diff] [blame] | 408 | LOG_I("SanCov Update: file size (Cur): %zu, newBBs:%" PRIu64 |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 409 | ", counters (Cur,New): %" PRIu64 "/%" PRIu64 ",%" PRIu64 "/%" PRIu64, |
Robert Swiecki | 3bfc33c | 2016-03-14 18:12:41 +0100 | [diff] [blame] | 410 | fuzzer->dynamicFileSz, fuzzer->sanCovCnts.newBBCnt, |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 411 | hfuzz->sanCovCnts.hitBBCnt, hfuzz->sanCovCnts.iDsoCnt, fuzzer->sanCovCnts.hitBBCnt, |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 412 | fuzzer->sanCovCnts.iDsoCnt); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 413 | |
Robert Swiecki | 142f941 | 2016-03-14 19:22:01 +0100 | [diff] [blame] | 414 | hfuzz->sanCovCnts.hitBBCnt += fuzzer->sanCovCnts.newBBCnt; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 415 | hfuzz->sanCovCnts.dsoCnt = fuzzer->sanCovCnts.dsoCnt; |
| 416 | hfuzz->sanCovCnts.iDsoCnt = fuzzer->sanCovCnts.iDsoCnt; |
| 417 | hfuzz->sanCovCnts.crashesCnt += fuzzer->sanCovCnts.crashesCnt; |
Robert Swiecki | 142f941 | 2016-03-14 19:22:01 +0100 | [diff] [blame] | 418 | hfuzz->sanCovCnts.newBBCnt = fuzzer->sanCovCnts.newBBCnt; |
Anestis Bechtsoudis | b78cf60 | 2016-01-07 13:10:50 +0200 | [diff] [blame] | 419 | |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 420 | if (hfuzz->sanCovCnts.totalBBCnt < fuzzer->sanCovCnts.totalBBCnt) { |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 421 | /* Keep only the max value (for dlopen cases) to measure total target coverage */ |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 422 | hfuzz->sanCovCnts.totalBBCnt = fuzzer->sanCovCnts.totalBBCnt; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 423 | } |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 424 | |
Jagger | d573837 | 2016-08-17 20:12:15 +0200 | [diff] [blame] | 425 | hfuzz->linux.hwCnts.cpuInstrCnt = fuzzer->linux.hwCnts.cpuInstrCnt; |
| 426 | hfuzz->linux.hwCnts.cpuBranchCnt = fuzzer->linux.hwCnts.cpuBranchCnt; |
Jagger | d573837 | 2016-08-17 20:12:15 +0200 | [diff] [blame] | 427 | |
Robert Swiecki | d258ff8 | 2016-08-29 17:54:20 +0200 | [diff] [blame] | 428 | fuzz_addFileToFileQLocked(hfuzz, fuzzer->dynamicFile, fuzzer->dynamicFileSz); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 429 | } |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 430 | } |
| 431 | |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 432 | static void fuzz_fuzzLoop(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 433 | { |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 434 | fuzzer->pid = 0; |
| 435 | fuzzer->timeStartedMillis = util_timeNowMillis(); |
| 436 | fuzzer->crashFileName[0] = '\0'; |
| 437 | fuzzer->pc = 0ULL; |
| 438 | fuzzer->backtrace = 0ULL; |
| 439 | fuzzer->access = 0ULL; |
| 440 | fuzzer->exception = 0; |
| 441 | fuzzer->report[0] = '\0'; |
| 442 | fuzzer->mainWorker = true; |
| 443 | fuzzer->origFileName = "DYNAMIC"; |
| 444 | fuzzer->flipRate = hfuzz->origFlipRate; |
| 445 | fuzzer->dynamicFileSz = 0; |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 446 | |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 447 | fuzzer->sanCovCnts.hitBBCnt = 0ULL; |
| 448 | fuzzer->sanCovCnts.totalBBCnt = 0ULL; |
| 449 | fuzzer->sanCovCnts.dsoCnt = 0ULL; |
| 450 | fuzzer->sanCovCnts.newBBCnt = 0ULL; |
| 451 | fuzzer->sanCovCnts.crashesCnt = 0ULL; |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 452 | |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 453 | fuzzer->linux.hwCnts.cpuInstrCnt = 0ULL; |
| 454 | fuzzer->linux.hwCnts.cpuBranchCnt = 0ULL; |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 455 | fuzzer->linux.hwCnts.bbCnt = 0ULL; |
| 456 | fuzzer->linux.hwCnts.newBBCnt = 0ULL; |
| 457 | fuzzer->linux.perfMmapBuf = NULL; |
| 458 | fuzzer->linux.perfMmapAux = NULL; |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 459 | |
Anestis Bechtsoudis | 2013f25 | 2015-11-07 22:03:31 +0200 | [diff] [blame] | 460 | size_t rnd_index = util_rndGet(0, hfuzz->fileCnt - 1); |
Anestis Bechtsoudis | 46ea10e | 2015-11-07 18:16:25 +0200 | [diff] [blame] | 461 | |
| 462 | /* If dry run mode, pick the next file and not a random one */ |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 463 | if (fuzzer->flipRate == 0.0L && hfuzz->useVerifier) { |
Jagger | d34417d | 2016-03-16 01:26:54 +0100 | [diff] [blame] | 464 | rnd_index = ATOMIC_POST_INC(hfuzz->lastFileIndex); |
Anestis Bechtsoudis | 46ea10e | 2015-11-07 18:16:25 +0200 | [diff] [blame] | 465 | } |
Robert Swiecki | 129afe8 | 2016-03-15 20:03:20 +0100 | [diff] [blame] | 466 | |
Robert Swiecki | 6d01e82 | 2016-03-15 20:21:46 +0100 | [diff] [blame] | 467 | if (fuzz_getState(hfuzz) == _HF_STATE_DYNAMIC_PRE) { |
Jagger | d34417d | 2016-03-16 01:26:54 +0100 | [diff] [blame] | 468 | rnd_index = ATOMIC_POST_INC(hfuzz->lastFileIndex); |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 469 | if (rnd_index >= hfuzz->fileCnt) { |
Jagger | 33fd952 | 2016-03-15 22:13:57 +0100 | [diff] [blame] | 470 | /* |
| 471 | * The waiting logic (for the DYNAMIC_PRE phase to finish) should be based on cond-waits |
| 472 | * or mutexes, but it'd complicate code too much |
| 473 | */ |
Robert Swiecki | 6d01e82 | 2016-03-15 20:21:46 +0100 | [diff] [blame] | 474 | while (fuzz_getState(hfuzz) == _HF_STATE_DYNAMIC_PRE) { |
| 475 | sleep(1); |
Robert Swiecki | 129afe8 | 2016-03-15 20:03:20 +0100 | [diff] [blame] | 476 | } |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 477 | } |
| 478 | } |
Anestis Bechtsoudis | 46ea10e | 2015-11-07 18:16:25 +0200 | [diff] [blame] | 479 | |
Jagger | 33fd952 | 2016-03-15 22:13:57 +0100 | [diff] [blame] | 480 | fuzzState_t state = fuzz_getState(hfuzz); |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 481 | if (state != _HF_STATE_DYNAMIC_MAIN) { |
Robert Swiecki | e8f8e8d | 2016-10-03 23:51:32 +0200 | [diff] [blame] | 482 | fuzzer->origFileName = files_basename(files_getFileFromFileq(hfuzz, rnd_index)->path); |
Robert Swiecki | a08ab1c | 2016-03-14 18:29:21 +0100 | [diff] [blame] | 483 | } |
Robert Swiecki | 6d01e82 | 2016-03-15 20:21:46 +0100 | [diff] [blame] | 484 | |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 485 | fuzz_getFileName(hfuzz, fuzzer->fileName); |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 486 | |
Robert Swiecki | ee266ac | 2016-10-03 02:25:59 +0200 | [diff] [blame] | 487 | if (state == _HF_STATE_DYNAMIC_PRE) { |
| 488 | fuzzer->flipRate = 0.0f; |
| 489 | if (!fuzz_prepareFile(hfuzz, fuzzer, rnd_index)) { |
| 490 | LOG_F("fuzz_prepareFile() failed"); |
| 491 | } |
| 492 | } else if (hfuzz->externalCommand) { |
Jagger | 30384ec | 2016-04-02 02:30:32 +0200 | [diff] [blame] | 493 | if (!fuzz_prepareFileExternally(hfuzz, fuzzer)) { |
| 494 | LOG_F("fuzz_prepareFileExternally() failed"); |
| 495 | } |
| 496 | } else if (state == _HF_STATE_DYNAMIC_MAIN) { |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 497 | if (!fuzz_prepareFileDynamically(hfuzz, fuzzer)) { |
Jagger | 1d8e0c6 | 2016-04-02 02:22:40 +0200 | [diff] [blame] | 498 | LOG_F("fuzz_prepareFileDynamically() failed"); |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 499 | } |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 500 | } else { |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 501 | if (!fuzz_prepareFile(hfuzz, fuzzer, rnd_index)) { |
Jagger | 1d8e0c6 | 2016-04-02 02:22:40 +0200 | [diff] [blame] | 502 | LOG_F("fuzz_prepareFile() failed"); |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Robert Swiecki | 0fcace2 | 2016-10-03 21:24:32 +0200 | [diff] [blame] | 506 | if (state == _HF_STATE_STATIC && hfuzz->postExternalCommand != NULL) { |
Robert Swiecki | ee266ac | 2016-10-03 02:25:59 +0200 | [diff] [blame] | 507 | if (!fuzz_postProcessFile(hfuzz, fuzzer)) { |
| 508 | LOG_F("fuzz_postProcessFile() failed"); |
| 509 | } |
| 510 | } |
| 511 | |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame^] | 512 | if (subproc_Run(hfuzz, fuzzer) == false) { |
| 513 | LOG_F("subproc_Run()"); |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 514 | } |
| 515 | |
Jagger | a34b302 | 2016-04-02 02:20:40 +0200 | [diff] [blame] | 516 | if (hfuzz->persistent == false) { |
| 517 | unlink(fuzzer->fileName); |
| 518 | } |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 519 | |
| 520 | if (hfuzz->dynFileMethod != _HF_DYNFILE_NONE) { |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 521 | fuzz_perfFeedback(hfuzz, fuzzer); |
Jagger | abb6ae0 | 2016-03-18 01:55:51 +0100 | [diff] [blame] | 522 | } |
| 523 | if (hfuzz->useSanCov) { |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 524 | fuzz_sanCovFeedback(hfuzz, fuzzer); |
Robert Swiecki | 0f937af | 2016-03-30 18:19:16 +0200 | [diff] [blame] | 525 | } |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 526 | |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 527 | if (hfuzz->useVerifier && (fuzzer->crashFileName[0] != 0) && fuzzer->backtrace) { |
| 528 | if (!fuzz_runVerifier(hfuzz, fuzzer)) { |
| 529 | LOG_I("Failed to verify %s", fuzzer->crashFileName); |
Anestis Bechtsoudis | 3085f22 | 2015-09-27 18:35:26 +0300 | [diff] [blame] | 530 | } |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 531 | } |
| 532 | |
Robert Swiecki | 216fef6 | 2016-10-20 18:32:58 +0200 | [diff] [blame] | 533 | report_Report(hfuzz, fuzzer->report); |
Robert Swiecki | 05354ca | 2016-03-15 19:10:23 +0100 | [diff] [blame] | 534 | |
Jagger | d34417d | 2016-03-16 01:26:54 +0100 | [diff] [blame] | 535 | if (state == _HF_STATE_DYNAMIC_PRE && ATOMIC_PRE_INC(hfuzz->doneFileIndex) >= hfuzz->fileCnt) { |
Robert Swiecki | 6d01e82 | 2016-03-15 20:21:46 +0100 | [diff] [blame] | 536 | fuzz_setState(hfuzz, _HF_STATE_DYNAMIC_MAIN); |
Robert Swiecki | 05354ca | 2016-03-15 19:10:23 +0100 | [diff] [blame] | 537 | } |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 538 | } |
| 539 | |
Jagger | fa3544a | 2016-08-30 02:55:55 +0200 | [diff] [blame] | 540 | static uint32_t fuzz_threadNo = 0; |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 541 | static void *fuzz_threadNew(void *arg) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 542 | { |
Jagger | fa3544a | 2016-08-30 02:55:55 +0200 | [diff] [blame] | 543 | unsigned int fuzzNo = ATOMIC_POST_INC(fuzz_threadNo); |
| 544 | |
| 545 | LOG_I("Launched new fuzzing thread, no. #%" PRId32, fuzzNo); |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 546 | |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 547 | honggfuzz_t *hfuzz = (honggfuzz_t *) arg; |
Anestis Bechtsoudis | 02b99be | 2015-12-27 11:53:01 +0200 | [diff] [blame] | 548 | |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 549 | fuzzer_t fuzzer = { |
| 550 | .pid = 0, |
| 551 | .persistentPid = 0, |
| 552 | .dynamicFile = util_Malloc(hfuzz->maxFileSz), |
Jagger | fa3544a | 2016-08-30 02:55:55 +0200 | [diff] [blame] | 553 | .fuzzNo = fuzzNo, |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 554 | .persistentSock = -1, |
Robert Swiecki | 12800cd | 2016-03-31 15:38:10 +0200 | [diff] [blame] | 555 | |
Robert Swiecki | b692f28 | 2016-08-25 16:21:08 +0200 | [diff] [blame] | 556 | .linux.attachedPid = 0, |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 557 | }; |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 558 | defer { |
| 559 | free(fuzzer.dynamicFile); |
| 560 | }; |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 561 | |
| 562 | if (arch_archThreadInit(hfuzz, &fuzzer) == false) { |
Robert Swiecki | 0f937af | 2016-03-30 18:19:16 +0200 | [diff] [blame] | 563 | LOG_F("Could not initialize the thread"); |
| 564 | } |
| 565 | |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 566 | for (;;) { |
Anestis Bechtsoudis | 46ea10e | 2015-11-07 18:16:25 +0200 | [diff] [blame] | 567 | /* Check if dry run mode with verifier enabled */ |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 568 | if (hfuzz->origFlipRate == 0.0L && hfuzz->useVerifier) { |
Jagger | d34417d | 2016-03-16 01:26:54 +0100 | [diff] [blame] | 569 | if (ATOMIC_POST_INC(hfuzz->mutationsCnt) >= hfuzz->fileCnt) { |
| 570 | ATOMIC_POST_INC(hfuzz->threadsFinished); |
Anestis Bechtsoudis | 46ea10e | 2015-11-07 18:16:25 +0200 | [diff] [blame] | 571 | // All files checked, weak-up the main process |
| 572 | pthread_kill(fuzz_mainThread, SIGALRM); |
| 573 | return NULL; |
| 574 | } |
| 575 | } |
| 576 | /* Check for max iterations limit if set */ |
Jagger | d34417d | 2016-03-16 01:26:54 +0100 | [diff] [blame] | 577 | else if ((ATOMIC_POST_INC(hfuzz->mutationsCnt) >= hfuzz->mutationsMax) |
Anestis Bechtsoudis | 46ea10e | 2015-11-07 18:16:25 +0200 | [diff] [blame] | 578 | && hfuzz->mutationsMax) { |
Jagger | d34417d | 2016-03-16 01:26:54 +0100 | [diff] [blame] | 579 | ATOMIC_POST_INC(hfuzz->threadsFinished); |
Robert Swiecki | 81c6a0d | 2015-09-08 15:43:20 +0200 | [diff] [blame] | 580 | // Wake-up the main process |
Jagger | 6113c43 | 2015-09-24 05:00:28 +0200 | [diff] [blame] | 581 | pthread_kill(fuzz_mainThread, SIGALRM); |
Jagger | ea39a8f | 2015-09-05 00:57:22 +0200 | [diff] [blame] | 582 | return NULL; |
| 583 | } |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 584 | |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 585 | fuzz_fuzzLoop(hfuzz, &fuzzer); |
robert.swiecki@gmail.com | d4dd4df | 2015-02-18 00:50:12 +0000 | [diff] [blame] | 586 | } |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 587 | } |
| 588 | |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 589 | static void fuzz_runThread(honggfuzz_t * hfuzz, void *(*thread) (void *)) |
| 590 | { |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 591 | pthread_attr_t attr; |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 592 | |
robert.swiecki@gmail.com | 6f5c239 | 2015-02-16 18:13:09 +0000 | [diff] [blame] | 593 | pthread_attr_init(&attr); |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 594 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
robert.swiecki@gmail.com | 441089a | 2015-02-23 13:14:07 +0000 | [diff] [blame] | 595 | pthread_attr_setstacksize(&attr, _HF_PTHREAD_STACKSIZE); |
robert.swiecki@gmail.com | 011981f | 2015-02-17 19:06:44 +0000 | [diff] [blame] | 596 | pthread_attr_setguardsize(&attr, (size_t) sysconf(_SC_PAGESIZE)); |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 597 | |
robert.swiecki@gmail.com | 01b6dd4 | 2015-02-16 18:11:28 +0000 | [diff] [blame] | 598 | pthread_t t; |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 599 | if (pthread_create(&t, &attr, thread, (void *)hfuzz) < 0) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 600 | PLOG_F("Couldn't create a new thread"); |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | return; |
| 604 | } |
| 605 | |
Jagger | 08816fd | 2016-03-11 01:32:38 +0100 | [diff] [blame] | 606 | void fuzz_threads(honggfuzz_t * hfuzz) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 607 | { |
Jagger | 6113c43 | 2015-09-24 05:00:28 +0200 | [diff] [blame] | 608 | fuzz_mainThread = pthread_self(); |
| 609 | |
robert.swiecki@gmail.com | 956276a | 2015-04-16 16:51:52 +0000 | [diff] [blame] | 610 | if (!arch_archInit(hfuzz)) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 611 | LOG_F("Couldn't prepare arch for fuzzing"); |
robert.swiecki@gmail.com | ef829fa | 2011-06-22 13:51:57 +0000 | [diff] [blame] | 612 | } |
Jagger | 0026560 | 2016-03-10 02:36:27 +0100 | [diff] [blame] | 613 | if (!sancov_Init(hfuzz)) { |
| 614 | LOG_F("Couldn't prepare sancov options"); |
| 615 | } |
robert.swiecki@gmail.com | ef829fa | 2011-06-22 13:51:57 +0000 | [diff] [blame] | 616 | |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 617 | if (hfuzz->useSanCov || hfuzz->dynFileMethod != _HF_DYNFILE_NONE) { |
Jagger | ebe1929 | 2016-03-17 00:11:44 +0100 | [diff] [blame] | 618 | fuzz_setState(hfuzz, _HF_STATE_DYNAMIC_PRE); |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 619 | } else { |
Jagger | ebe1929 | 2016-03-17 00:11:44 +0100 | [diff] [blame] | 620 | fuzz_setState(hfuzz, _HF_STATE_STATIC); |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 621 | } |
| 622 | |
Jagger | ea39a8f | 2015-09-05 00:57:22 +0200 | [diff] [blame] | 623 | for (size_t i = 0; i < hfuzz->threadsMax; i++) { |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 624 | fuzz_runThread(hfuzz, fuzz_threadNew); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 625 | } |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 626 | } |