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/mman.h> |
| 40 | #include <sys/param.h> |
| 41 | #include <sys/stat.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 42 | #include <sys/time.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/wait.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 45 | #include <time.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 46 | #include <unistd.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 47 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 48 | #include "arch.h" |
Jagger | 0764ad7 | 2015-09-06 01:11:08 +0200 | [diff] [blame] | 49 | #include "display.h" |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 50 | #include "files.h" |
robert.swiecki@gmail.com | e7190b9 | 2015-02-14 23:05:42 +0000 | [diff] [blame] | 51 | #include "log.h" |
robert.swiecki@gmail.com | 36700b5 | 2015-02-22 05:03:16 +0000 | [diff] [blame] | 52 | #include "mangle.h" |
robert.swiecki@gmail.com | e7190b9 | 2015-02-14 23:05:42 +0000 | [diff] [blame] | 53 | #include "report.h" |
| 54 | #include "util.h" |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 55 | |
robert.swiecki@gmail.com | cf4e8d0 | 2015-04-23 15:53:39 +0000 | [diff] [blame] | 56 | static int fuzz_sigReceived = 0; |
robert.swiecki@gmail.com | 9c77bb1 | 2015-04-23 15:35:28 +0000 | [diff] [blame] | 57 | |
Jagger | 6113c43 | 2015-09-24 05:00:28 +0200 | [diff] [blame] | 58 | static pthread_t fuzz_mainThread; |
| 59 | |
Anestis Bechtsoudis | 1dd1923 | 2015-12-30 13:11:06 +0200 | [diff] [blame] | 60 | static inline bool fuzz_isPerfCntsSet(honggfuzz_t * hfuzz) |
Anestis Bechtsoudis | 914787a | 2015-12-26 18:24:03 +0200 | [diff] [blame] | 61 | { |
Anestis Bechtsoudis | f2a5bc6 | 2015-12-26 18:41:48 +0200 | [diff] [blame] | 62 | if (hfuzz->hwCnts.cpuInstrCnt > 0ULL || hfuzz->hwCnts.cpuBranchCnt > 0ULL |
Jagger | 39bd2b0 | 2016-02-04 01:16:15 +0100 | [diff] [blame] | 63 | || hfuzz->hwCnts.cpuBtsBlockCnt > 0ULL || hfuzz->hwCnts.cpuBtsEdgeCnt > 0ULL |
Anestis Bechtsoudis | f2a5bc6 | 2015-12-26 18:41:48 +0200 | [diff] [blame] | 64 | || hfuzz->hwCnts.customCnt > 0ULL) { |
Anestis Bechtsoudis | 914787a | 2015-12-26 18:24:03 +0200 | [diff] [blame] | 65 | return true; |
| 66 | } else { |
| 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | |
Anestis Bechtsoudis | 1dd1923 | 2015-12-30 13:11:06 +0200 | [diff] [blame] | 71 | static inline void fuzz_resetFeedbackCnts(honggfuzz_t * hfuzz) |
| 72 | { |
| 73 | /* HW perf counters */ |
| 74 | __sync_fetch_and_and(&hfuzz->hwCnts.cpuInstrCnt, 0UL); |
| 75 | __sync_fetch_and_and(&hfuzz->hwCnts.cpuBranchCnt, 0UL); |
Jagger | 39bd2b0 | 2016-02-04 01:16:15 +0100 | [diff] [blame] | 76 | __sync_fetch_and_and(&hfuzz->hwCnts.cpuBtsBlockCnt, 0UL); |
| 77 | __sync_fetch_and_and(&hfuzz->hwCnts.cpuBtsEdgeCnt, 0UL); |
Anestis Bechtsoudis | 1dd1923 | 2015-12-30 13:11:06 +0200 | [diff] [blame] | 78 | __sync_fetch_and_and(&hfuzz->hwCnts.customCnt, 0UL); |
| 79 | |
| 80 | /* Sanitizer coverage counter */ |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 81 | __sync_fetch_and_and(&hfuzz->sanCovCnts.hitBBCnt, 0UL); |
| 82 | __sync_fetch_and_and(&hfuzz->sanCovCnts.totalBBCnt, 0UL); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 83 | __sync_fetch_and_and(&hfuzz->sanCovCnts.dsoCnt, 0UL); |
| 84 | __sync_fetch_and_and(&hfuzz->sanCovCnts.iDsoCnt, 0UL); |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 85 | __sync_fetch_and_and(&hfuzz->sanCovCnts.newBBCnt, 0UL); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 86 | __sync_fetch_and_and(&hfuzz->sanCovCnts.crashesCnt, 0UL); |
Anestis Bechtsoudis | b78cf60 | 2016-01-07 13:10:50 +0200 | [diff] [blame] | 87 | |
Robert Swiecki | 23ec02a | 2016-01-19 18:47:45 +0100 | [diff] [blame] | 88 | /* |
Anestis Bechtsoudis | 1fd10c7 | 2016-01-07 12:38:45 +0200 | [diff] [blame] | 89 | * For performance reasons Trie & Bitmap methods are not exposed in arch.h |
| 90 | * Thus maintain a status flag to destroy runtime data internally at sancov.c |
| 91 | * when dynFile input seed is replaced. |
| 92 | */ |
| 93 | hfuzz->clearCovMetadata = true; |
Anestis Bechtsoudis | 1dd1923 | 2015-12-30 13:11:06 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Robert Swiecki | 6e08d46 | 2015-09-08 17:23:03 +0200 | [diff] [blame] | 96 | static void fuzz_sigHandler(int sig) |
robert.swiecki@gmail.com | 9c77bb1 | 2015-04-23 15:35:28 +0000 | [diff] [blame] | 97 | { |
Robert Swiecki | 81c6a0d | 2015-09-08 15:43:20 +0200 | [diff] [blame] | 98 | /* We should not terminate upon SIGALRM delivery */ |
| 99 | if (sig == SIGALRM) { |
| 100 | return; |
| 101 | } |
| 102 | |
robert.swiecki@gmail.com | cf4e8d0 | 2015-04-23 15:53:39 +0000 | [diff] [blame] | 103 | fuzz_sigReceived = sig; |
robert.swiecki@gmail.com | 9c77bb1 | 2015-04-23 15:35:28 +0000 | [diff] [blame] | 104 | } |
| 105 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 106 | static void fuzz_getFileName(honggfuzz_t * hfuzz, char *fileName) |
| 107 | { |
robert.swiecki | ba51263 | 2011-01-28 11:57:26 +0000 | [diff] [blame] | 108 | struct timeval tv; |
| 109 | gettimeofday(&tv, NULL); |
| 110 | |
Anestis Bechtsoudis | d968053 | 2015-09-06 17:37:05 +0300 | [diff] [blame] | 111 | snprintf(fileName, PATH_MAX, "%s/.honggfuzz.%d.%lu.%llx.%s", hfuzz->workDir, (int)getpid(), |
robert.swiecki@gmail.com | 6f31991 | 2015-02-28 05:01:37 +0000 | [diff] [blame] | 112 | (unsigned long int)tv.tv_sec, (unsigned long long int)util_rndGet(0, 1ULL << 62), |
robert.swiecki@gmail.com | bb5d264 | 2015-02-25 20:00:00 +0000 | [diff] [blame] | 113 | hfuzz->fileExtn); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 114 | } |
| 115 | |
robert.swiecki@gmail.com | 624233e | 2015-02-18 10:26:05 +0000 | [diff] [blame] | 116 | static bool fuzz_prepareFileDynamically(honggfuzz_t * hfuzz, fuzzer_t * fuzzer, int rnd_index) |
robert.swiecki@gmail.com | 6d6f756 | 2015-02-17 22:18:51 +0000 | [diff] [blame] | 117 | { |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 118 | { |
| 119 | MX_LOCK(&hfuzz->dynamicFile_mutex); |
| 120 | defer(MX_UNLOCK(&hfuzz->dynamicFile_mutex)); |
robert.swiecki@gmail.com | 41d8e05 | 2015-02-19 01:10:41 +0000 | [diff] [blame] | 121 | |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 122 | /* If max dynamicFile iterations counter, pick new seed file when working with input file corpus */ |
| 123 | if (hfuzz->inputFile && |
| 124 | __sync_fetch_and_add(&hfuzz->dynFileIterExpire, 0UL) >= _HF_MAX_DYNFILE_ITER) { |
| 125 | size_t fileSz = files_readFileToBufMax(hfuzz->files[rnd_index], hfuzz->dynamicFileBest, |
| 126 | hfuzz->maxFileSz); |
| 127 | if (fileSz == 0) { |
| 128 | MX_UNLOCK(&hfuzz->dynamicFile_mutex); |
| 129 | LOG_E("Couldn't read '%s'", hfuzz->files[rnd_index]); |
| 130 | return false; |
| 131 | } |
| 132 | hfuzz->dynamicFileBestSz = fileSz; |
| 133 | |
| 134 | /* Reset counter since new seed pick */ |
| 135 | __sync_fetch_and_and(&hfuzz->dynFileIterExpire, 0UL); |
| 136 | fuzz_resetFeedbackCnts(hfuzz); |
| 137 | |
| 138 | /* |
| 139 | * In order to have accurate comparison base for coverage, first iteration |
| 140 | * of a new seed is executed without mangling. Also workersBlock_mutex mutex |
| 141 | * is maintain until execution is finished to ensure that other threads will |
| 142 | * work against the same coverage data vs. original seed. |
| 143 | */ |
| 144 | hfuzz->isDynFileLocked = true; |
| 145 | } else if (hfuzz->inputFile == NULL && (fuzz_isPerfCntsSet(hfuzz) == false)) { |
| 146 | /* |
| 147 | * When working with an empty input file corpus (allowed if perf feedback enabled for Linux archs), |
| 148 | * first iteration is executed without mangling. First iteration need to be executed by one thread |
| 149 | * blocking other workers from continuing until finished. |
| 150 | */ |
| 151 | hfuzz->isDynFileLocked = true; |
robert.swiecki@gmail.com | 624233e | 2015-02-18 10:26:05 +0000 | [diff] [blame] | 152 | } |
Anestis Bechtsoudis | 02b99be | 2015-12-27 11:53:01 +0200 | [diff] [blame] | 153 | |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 154 | if (hfuzz->dynamicFileBestSz > hfuzz->maxFileSz) { |
| 155 | LOG_F("Current BEST file Sz > maxFileSz (%zu > %zu)", hfuzz->dynamicFileBestSz, |
| 156 | hfuzz->maxFileSz); |
| 157 | } |
Anestis Bechtsoudis | ac05480 | 2016-01-07 23:48:06 +0200 | [diff] [blame] | 158 | |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 159 | fuzzer->dynamicFileSz = hfuzz->dynamicFileBestSz; |
| 160 | memcpy(fuzzer->dynamicFile, hfuzz->dynamicFileBest, hfuzz->dynamicFileBestSz); |
robert.swiecki@gmail.com | 624233e | 2015-02-18 10:26:05 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Robert Swiecki | 23ec02a | 2016-01-19 18:47:45 +0100 | [diff] [blame] | 163 | /* |
| 164 | * true isDynFileLocked indicates first run for a new seed, so skip mangling |
Anestis Bechtsoudis | 6ef24e4 | 2016-01-13 13:05:53 +0200 | [diff] [blame] | 165 | * without unlocking threads block mutex. |
| 166 | */ |
Anestis Bechtsoudis | ac05480 | 2016-01-07 23:48:06 +0200 | [diff] [blame] | 167 | MX_LOCK(&hfuzz->workersBlock_mutex); |
Anestis Bechtsoudis | a1f8a03 | 2016-01-14 16:45:30 +0200 | [diff] [blame] | 168 | if (hfuzz->isDynFileLocked) { |
Anestis Bechtsoudis | ac05480 | 2016-01-07 23:48:06 +0200 | [diff] [blame] | 169 | goto skipMangling; |
| 170 | } |
| 171 | MX_UNLOCK(&hfuzz->workersBlock_mutex); |
| 172 | |
Robert Swiecki | 23ec02a | 2016-01-19 18:47:45 +0100 | [diff] [blame] | 173 | /* |
Anestis Bechtsoudis | 25262b3 | 2015-11-07 22:39:47 +0200 | [diff] [blame] | 174 | * if flip rate is 0.0, early abort file mangling. This will leave perf counters |
| 175 | * with values equal to dry runs against input corpus. |
| 176 | */ |
Anestis Bechtsoudis | 4e35c67 | 2015-12-28 15:00:42 +0200 | [diff] [blame] | 177 | if (hfuzz->flipRate == 0.0L) { |
Anestis Bechtsoudis | 25262b3 | 2015-11-07 22:39:47 +0200 | [diff] [blame] | 178 | goto skipMangling; |
| 179 | } |
Anestis Bechtsoudis | ac05480 | 2016-01-07 23:48:06 +0200 | [diff] [blame] | 180 | |
Anestis Bechtsoudis | 6ef24e4 | 2016-01-13 13:05:53 +0200 | [diff] [blame] | 181 | mangle_Resize(hfuzz, fuzzer->dynamicFile, &fuzzer->dynamicFileSz); |
| 182 | mangle_mangleContent(hfuzz, fuzzer->dynamicFile, fuzzer->dynamicFileSz); |
robert.swiecki@gmail.com | 6d6f756 | 2015-02-17 22:18:51 +0000 | [diff] [blame] | 183 | |
Anestis Bechtsoudis | 25262b3 | 2015-11-07 22:39:47 +0200 | [diff] [blame] | 184 | skipMangling: |
robert.swiecki@gmail.com | dc8403e | 2015-03-01 01:33:00 +0000 | [diff] [blame] | 185 | if (files_writeBufToFile |
robert.swiecki@gmail.com | 62e34ae | 2015-03-05 03:39:32 +0000 | [diff] [blame] | 186 | (fuzzer->fileName, fuzzer->dynamicFile, fuzzer->dynamicFileSz, |
| 187 | O_WRONLY | O_CREAT | O_EXCL | O_TRUNC) == false) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 188 | 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] | 189 | return false; |
| 190 | } |
robert.swiecki@gmail.com | 6d6f756 | 2015-02-17 22:18:51 +0000 | [diff] [blame] | 191 | |
robert.swiecki@gmail.com | 6d6f756 | 2015-02-17 22:18:51 +0000 | [diff] [blame] | 192 | return true; |
robert.swiecki@gmail.com | d7aed31 | 2015-02-03 21:26:37 +0000 | [diff] [blame] | 193 | } |
| 194 | |
robert.swiecki@gmail.com | 4a7a9d8 | 2015-03-01 01:25:16 +0000 | [diff] [blame] | 195 | 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] | 196 | { |
robert.swiecki@gmail.com | 4a7a9d8 | 2015-03-01 01:25:16 +0000 | [diff] [blame] | 197 | size_t fileSz = |
| 198 | files_readFileToBufMax(hfuzz->files[rnd_index], fuzzer->dynamicFile, hfuzz->maxFileSz); |
| 199 | if (fileSz == 0UL) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 200 | LOG_E("Couldn't read contents of '%s'", hfuzz->files[rnd_index]); |
robert.swiecki@gmail.com | bb5d264 | 2015-02-25 20:00:00 +0000 | [diff] [blame] | 201 | return false; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Anestis Bechtsoudis | 25262b3 | 2015-11-07 22:39:47 +0200 | [diff] [blame] | 204 | /* If flip rate is 0.0, early abort file mangling */ |
| 205 | if (hfuzz->flipRate != 0.0L) { |
| 206 | mangle_Resize(hfuzz, fuzzer->dynamicFile, &fileSz); |
| 207 | mangle_mangleContent(hfuzz, fuzzer->dynamicFile, fileSz); |
| 208 | } |
robert.swiecki@gmail.com | c070b94 | 2015-02-25 18:29:19 +0000 | [diff] [blame] | 209 | |
robert.swiecki@gmail.com | dc8403e | 2015-03-01 01:33:00 +0000 | [diff] [blame] | 210 | if (files_writeBufToFile |
robert.swiecki@gmail.com | 62e34ae | 2015-03-05 03:39:32 +0000 | [diff] [blame] | 211 | (fuzzer->fileName, fuzzer->dynamicFile, fileSz, O_WRONLY | O_CREAT | O_EXCL) == false) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 212 | 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] | 213 | return false; |
| 214 | } |
| 215 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 216 | return true; |
| 217 | } |
| 218 | |
robert.swiecki@gmail.com | 2085120 | 2015-03-01 01:48:15 +0000 | [diff] [blame] | 219 | static bool fuzz_prepareFileExternally(honggfuzz_t * hfuzz, fuzzer_t * fuzzer, int rnd_index) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 220 | { |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame^] | 221 | { |
| 222 | int dstfd = open(fuzzer->fileName, O_CREAT | O_EXCL | O_RDWR, 0644); |
| 223 | if (dstfd == -1) { |
| 224 | PLOG_E("Couldn't create a temporary file '%s'", fuzzer->fileName); |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 225 | return false; |
| 226 | } |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame^] | 227 | defer(close(dstfd)); |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 228 | |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame^] | 229 | LOG_D("Created '%s' as an input file", fuzzer->fileName); |
| 230 | |
| 231 | if (hfuzz->inputFile) { |
| 232 | size_t fileSz = files_readFileToBufMax(hfuzz->files[rnd_index], fuzzer->dynamicFile, |
| 233 | hfuzz->maxFileSz); |
| 234 | if (fileSz == 0UL) { |
| 235 | LOG_E("Couldn't read '%s'", hfuzz->files[rnd_index]); |
| 236 | unlink(fuzzer->fileName); |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | if (files_writeToFd(dstfd, fuzzer->dynamicFile, fileSz) == false) { |
| 241 | unlink(fuzzer->fileName); |
| 242 | return false; |
| 243 | } |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 244 | } |
robert.swiecki | 3d505e2 | 2010-10-14 01:17:17 +0000 | [diff] [blame] | 245 | |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame^] | 246 | } |
robert.swiecki | 3d505e2 | 2010-10-14 01:17:17 +0000 | [diff] [blame] | 247 | |
Anestis Bechtsoudis | 3541b47 | 2015-10-08 09:52:44 -0700 | [diff] [blame] | 248 | pid_t pid = arch_fork(hfuzz); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 249 | if (pid == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 250 | PLOG_E("Couldn't fork"); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 251 | return false; |
| 252 | } |
| 253 | |
| 254 | if (!pid) { |
| 255 | /* |
robert.swiecki@gmail.com | cdf18f9 | 2015-02-11 22:22:18 +0000 | [diff] [blame] | 256 | * child performs the external file modifications |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 257 | */ |
robert.swiecki@gmail.com | 2085120 | 2015-03-01 01:48:15 +0000 | [diff] [blame] | 258 | execl(hfuzz->externalCommand, hfuzz->externalCommand, fuzzer->fileName, NULL); |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 259 | PLOG_F("Couldn't execute '%s %s'", hfuzz->externalCommand, fuzzer->fileName); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 260 | return false; |
robert.swiecki@gmail.com | 757ee19 | 2015-02-13 16:54:02 +0000 | [diff] [blame] | 261 | } |
robert.swiecki@gmail.com | 8a9df0e | 2015-02-13 17:08:06 +0000 | [diff] [blame] | 262 | |
robert.swiecki@gmail.com | 757ee19 | 2015-02-13 16:54:02 +0000 | [diff] [blame] | 263 | /* |
| 264 | * parent waits until child is done fuzzing the input file |
| 265 | */ |
robert.swiecki@gmail.com | 757ee19 | 2015-02-13 16:54:02 +0000 | [diff] [blame] | 266 | int childStatus; |
| 267 | int flags = 0; |
robert.swiecki@gmail.com | a229118 | 2015-02-13 16:56:27 +0000 | [diff] [blame] | 268 | #if defined(__WNOTHREAD) |
| 269 | flags |= __WNOTHREAD; |
robert.swiecki@gmail.com | 8a9df0e | 2015-02-13 17:08:06 +0000 | [diff] [blame] | 270 | #endif /* defined(__WNOTHREAD) */ |
robert.swiecki@gmail.com | 757ee19 | 2015-02-13 16:54:02 +0000 | [diff] [blame] | 271 | while (wait4(pid, &childStatus, flags, NULL) != pid) ; |
robert.swiecki@gmail.com | 757ee19 | 2015-02-13 16:54:02 +0000 | [diff] [blame] | 272 | if (WIFEXITED(childStatus)) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 273 | LOG_D("External command exited with status %d", WEXITSTATUS(childStatus)); |
robert.swiecki@gmail.com | 757ee19 | 2015-02-13 16:54:02 +0000 | [diff] [blame] | 274 | return true; |
| 275 | } |
| 276 | if (WIFSIGNALED(childStatus)) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 277 | LOG_E("External command terminated with signal %d", WTERMSIG(childStatus)); |
robert.swiecki | 3d505e2 | 2010-10-14 01:17:17 +0000 | [diff] [blame] | 278 | return false; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 279 | } |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 280 | LOG_F("External command terminated abnormally, status: %d", childStatus); |
robert.swiecki@gmail.com | 757ee19 | 2015-02-13 16:54:02 +0000 | [diff] [blame] | 281 | return false; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 282 | |
| 283 | abort(); /* NOTREACHED */ |
| 284 | } |
| 285 | |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 286 | static bool fuzz_runVerifier(honggfuzz_t * hfuzz, fuzzer_t * crashedFuzzer) |
| 287 | { |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 288 | int crashFd = -1; |
| 289 | uint8_t *crashBuf = NULL; |
| 290 | off_t crashFileSz = 0; |
| 291 | |
| 292 | crashBuf = files_mapFile(crashedFuzzer->crashFileName, &crashFileSz, &crashFd, false); |
| 293 | if (crashBuf == NULL) { |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 294 | 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^] | 295 | return false; |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 296 | } |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame^] | 297 | defer(munmap(crashBuf, crashFileSz)); |
| 298 | defer(close(crashFd)); |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 299 | |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 300 | LOG_I("Launching verifier for %" PRIx64 " hash", crashedFuzzer->backtrace); |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 301 | for (int i = 0; i < _HF_VERIFIER_ITER; i++) { |
| 302 | fuzzer_t vFuzzer = { |
| 303 | .pid = 0, |
| 304 | .timeStartedMillis = util_timeNowMillis(), |
Anestis Bechtsoudis | ecb0a66 | 2015-09-27 18:19:46 +0300 | [diff] [blame] | 305 | .crashFileName = {0}, |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 306 | .pc = 0ULL, |
| 307 | .backtrace = 0ULL, |
| 308 | .access = 0ULL, |
| 309 | .exception = 0, |
| 310 | .dynamicFileSz = 0, |
| 311 | .dynamicFile = NULL, |
| 312 | .hwCnts = { |
| 313 | .cpuInstrCnt = 0ULL, |
| 314 | .cpuBranchCnt = 0ULL, |
Jagger | 39bd2b0 | 2016-02-04 01:16:15 +0100 | [diff] [blame] | 315 | .cpuBtsBlockCnt = 0ULL, |
| 316 | .cpuBtsEdgeCnt = 0ULL, |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 317 | .customCnt = 0ULL, |
| 318 | }, |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 319 | .sanCovCnts = { |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 320 | .hitBBCnt = 0ULL, |
| 321 | .totalBBCnt = 0ULL, |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 322 | .dsoCnt = 0ULL, |
| 323 | .iDsoCnt = 0ULL, |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 324 | .newBBCnt = 0ULL, |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 325 | .crashesCnt = 0ULL, |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 326 | }, |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 327 | .report = {'\0'}, |
Anestis Bechtsoudis | 6b9e83d | 2015-10-02 11:10:50 -0700 | [diff] [blame] | 328 | .mainWorker = false |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 329 | }; |
| 330 | |
| 331 | fuzz_getFileName(hfuzz, vFuzzer.fileName); |
| 332 | if (files_writeBufToFile |
| 333 | (vFuzzer.fileName, crashBuf, crashFileSz, O_WRONLY | O_CREAT | O_EXCL) == false) { |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 334 | LOG_E("Couldn't write buffer to file '%s'", vFuzzer.fileName); |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame^] | 335 | return false; |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 336 | } |
| 337 | |
Anestis Bechtsoudis | 3541b47 | 2015-10-08 09:52:44 -0700 | [diff] [blame] | 338 | vFuzzer.pid = arch_fork(hfuzz); |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 339 | if (vFuzzer.pid == -1) { |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 340 | PLOG_F("Couldn't fork"); |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 341 | return false; |
| 342 | } |
| 343 | |
| 344 | if (!vFuzzer.pid) { |
| 345 | if (!arch_launchChild(hfuzz, crashedFuzzer->crashFileName)) { |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 346 | LOG_E("Error launching verifier child process"); |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame^] | 347 | return false; |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | arch_reapChild(hfuzz, &vFuzzer); |
| 352 | unlink(vFuzzer.fileName); |
| 353 | |
Anestis Bechtsoudis | ecb0a66 | 2015-09-27 18:19:46 +0300 | [diff] [blame] | 354 | /* If stack hash doesn't match skip name tag and exit */ |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 355 | if (crashedFuzzer->backtrace != vFuzzer.backtrace) { |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 356 | LOG_D("Verifier stack hash mismatch"); |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame^] | 357 | return false; |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
| 361 | /* Workspace is inherited, just append a extra suffix */ |
| 362 | char verFile[PATH_MAX] = { 0 }; |
| 363 | snprintf(verFile, sizeof(verFile), "%s.verified", crashedFuzzer->crashFileName); |
Anestis Bechtsoudis | ecb0a66 | 2015-09-27 18:19:46 +0300 | [diff] [blame] | 364 | |
Anestis Bechtsoudis | d86e660 | 2015-11-07 18:34:06 +0200 | [diff] [blame] | 365 | /* Copy file with new suffix & remove original copy */ |
| 366 | bool dstFileExists = false; |
| 367 | if (files_copyFile(crashedFuzzer->crashFileName, verFile, &dstFileExists)) { |
| 368 | LOG_I("Successfully verified, saving as (%s)", verFile); |
| 369 | __sync_fetch_and_add(&hfuzz->verifiedCrashesCnt, 1UL); |
| 370 | unlink(crashedFuzzer->crashFileName); |
| 371 | } else { |
| 372 | if (dstFileExists) { |
| 373 | LOG_I("It seems that '%s' already exists, skipping", verFile); |
| 374 | } else { |
| 375 | LOG_E("Couldn't copy '%s' to '%s'", crashedFuzzer->crashFileName, verFile); |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame^] | 376 | return false; |
Anestis Bechtsoudis | d86e660 | 2015-11-07 18:34:06 +0200 | [diff] [blame] | 377 | } |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 378 | } |
Anestis Bechtsoudis | ecb0a66 | 2015-09-27 18:19:46 +0300 | [diff] [blame] | 379 | |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame^] | 380 | return true; |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 381 | } |
| 382 | |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 383 | static void fuzz_perfFeedback(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
| 384 | { |
| 385 | LOG_D |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 386 | ("File size (New/Best): %zu/%zu, Perf feedback (instr,branch,block,block-edge,custom): Best: [%" |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 387 | PRIu64 ",%" PRIu64 ",%" PRIu64 ",%" PRIu64 ",%" PRIu64 "] / New: [%" PRIu64 ",%" PRIu64 |
| 388 | ",%" PRIu64 ",%" PRIu64 ",%" PRIu64 "]", fuzzer->dynamicFileSz, |
| 389 | hfuzz->dynamicFileBestSz, hfuzz->hwCnts.cpuInstrCnt, hfuzz->hwCnts.cpuBranchCnt, |
Jagger | 39bd2b0 | 2016-02-04 01:16:15 +0100 | [diff] [blame] | 390 | hfuzz->hwCnts.cpuBtsBlockCnt, hfuzz->hwCnts.cpuBtsEdgeCnt, hfuzz->hwCnts.customCnt, |
| 391 | fuzzer->hwCnts.cpuInstrCnt, fuzzer->hwCnts.cpuBranchCnt, fuzzer->hwCnts.cpuBtsBlockCnt, |
| 392 | fuzzer->hwCnts.cpuBtsEdgeCnt, fuzzer->hwCnts.customCnt); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 393 | |
| 394 | MX_LOCK(&hfuzz->dynamicFile_mutex); |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 395 | defer(MX_UNLOCK(&hfuzz->dynamicFile_mutex)); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 396 | |
| 397 | int64_t diff0 = hfuzz->hwCnts.cpuInstrCnt - fuzzer->hwCnts.cpuInstrCnt; |
| 398 | int64_t diff1 = hfuzz->hwCnts.cpuBranchCnt - fuzzer->hwCnts.cpuBranchCnt; |
Jagger | 39bd2b0 | 2016-02-04 01:16:15 +0100 | [diff] [blame] | 399 | int64_t diff2 = hfuzz->hwCnts.cpuBtsBlockCnt - fuzzer->hwCnts.cpuBtsBlockCnt; |
| 400 | int64_t diff3 = hfuzz->hwCnts.cpuBtsEdgeCnt - fuzzer->hwCnts.cpuBtsEdgeCnt; |
Jagger | d7c6ae6 | 2016-02-05 03:24:10 +0100 | [diff] [blame] | 401 | int64_t diff4 = hfuzz->hwCnts.cpuIptBlockCnt - fuzzer->hwCnts.cpuIptBlockCnt; |
Jagger | 0d2727a | 2016-02-08 02:08:42 +0100 | [diff] [blame] | 402 | int64_t diff5 = hfuzz->hwCnts.customCnt - fuzzer->hwCnts.customCnt; |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 403 | |
Jagger | 0d2727a | 2016-02-08 02:08:42 +0100 | [diff] [blame] | 404 | if (diff0 < 0 || diff1 < 0 || diff2 < 0 || diff3 < 0 || diff4 < 0 || diff5 < 0) { |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 405 | |
Jagger | 39bd2b0 | 2016-02-04 01:16:15 +0100 | [diff] [blame] | 406 | LOG_I("New: (Size New,Old): %zu,%zu, Perf (Cur,New): " |
Jagger | 0d2727a | 2016-02-08 02:08:42 +0100 | [diff] [blame] | 407 | "%" PRIu64 "/%" PRIu64 "/%" PRIu64 "/%" PRIu64 "/%" PRIu64 "/%" PRIu64 |
| 408 | ",%" PRIu64 "/%" PRIu64 "/%" PRIu64 "/%" PRIu64 "/%" PRIu64 "/%" PRIu64, |
Jagger | 39bd2b0 | 2016-02-04 01:16:15 +0100 | [diff] [blame] | 409 | fuzzer->dynamicFileSz, hfuzz->dynamicFileBestSz, |
| 410 | hfuzz->hwCnts.cpuInstrCnt, hfuzz->hwCnts.cpuBranchCnt, |
| 411 | hfuzz->hwCnts.cpuBtsBlockCnt, hfuzz->hwCnts.cpuBtsEdgeCnt, |
Jagger | 0d2727a | 2016-02-08 02:08:42 +0100 | [diff] [blame] | 412 | hfuzz->hwCnts.cpuIptBlockCnt, hfuzz->hwCnts.customCnt, |
Jagger | 39bd2b0 | 2016-02-04 01:16:15 +0100 | [diff] [blame] | 413 | fuzzer->hwCnts.cpuInstrCnt, fuzzer->hwCnts.cpuBranchCnt, |
| 414 | fuzzer->hwCnts.cpuBtsBlockCnt, fuzzer->hwCnts.cpuBtsEdgeCnt, |
Jagger | 0d2727a | 2016-02-08 02:08:42 +0100 | [diff] [blame] | 415 | fuzzer->hwCnts.cpuIptBlockCnt, fuzzer->hwCnts.customCnt); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 416 | |
| 417 | memcpy(hfuzz->dynamicFileBest, fuzzer->dynamicFile, fuzzer->dynamicFileSz); |
| 418 | |
| 419 | hfuzz->dynamicFileBestSz = fuzzer->dynamicFileSz; |
| 420 | hfuzz->hwCnts.cpuInstrCnt = fuzzer->hwCnts.cpuInstrCnt; |
| 421 | hfuzz->hwCnts.cpuBranchCnt = fuzzer->hwCnts.cpuBranchCnt; |
Jagger | 39bd2b0 | 2016-02-04 01:16:15 +0100 | [diff] [blame] | 422 | hfuzz->hwCnts.cpuBtsBlockCnt = fuzzer->hwCnts.cpuBtsBlockCnt; |
| 423 | hfuzz->hwCnts.cpuBtsEdgeCnt = fuzzer->hwCnts.cpuBtsEdgeCnt; |
Jagger | d7c6ae6 | 2016-02-05 03:24:10 +0100 | [diff] [blame] | 424 | hfuzz->hwCnts.cpuIptBlockCnt = fuzzer->hwCnts.cpuIptBlockCnt; |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 425 | hfuzz->hwCnts.customCnt = fuzzer->hwCnts.customCnt; |
| 426 | |
Anestis Bechtsoudis | e84d236 | 2015-12-27 12:43:08 +0200 | [diff] [blame] | 427 | /* Reset counter if better coverage achieved */ |
| 428 | __sync_fetch_and_and(&hfuzz->dynFileIterExpire, 0UL); |
| 429 | |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 430 | char currentBest[PATH_MAX], currentBestTmp[PATH_MAX]; |
| 431 | snprintf(currentBest, PATH_MAX, "%s/CURRENT_BEST", hfuzz->workDir); |
| 432 | snprintf(currentBestTmp, PATH_MAX, "%s/.tmp.CURRENT_BEST", hfuzz->workDir); |
| 433 | |
| 434 | if (files_writeBufToFile |
| 435 | (currentBestTmp, fuzzer->dynamicFile, fuzzer->dynamicFileSz, |
| 436 | O_WRONLY | O_CREAT | O_TRUNC)) { |
| 437 | rename(currentBestTmp, currentBest); |
| 438 | } |
| 439 | } |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | static void fuzz_sanCovFeedback(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
| 443 | { |
| 444 | LOG_D |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 445 | ("File size (Best/New): %zu/%zu, SanCov feedback (bb,dso): Best: [%" PRIu64 |
| 446 | ",%" PRIu64 "] / New: [%" PRIu64 ",%" PRIu64 "], newBBs:%" PRIu64, |
| 447 | hfuzz->dynamicFileBestSz, fuzzer->dynamicFileSz, hfuzz->sanCovCnts.hitBBCnt, |
| 448 | hfuzz->sanCovCnts.iDsoCnt, fuzzer->sanCovCnts.hitBBCnt, fuzzer->sanCovCnts.iDsoCnt, |
| 449 | fuzzer->sanCovCnts.newBBCnt); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 450 | |
| 451 | MX_LOCK(&hfuzz->dynamicFile_mutex); |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 452 | defer(MX_UNLOCK(&hfuzz->dynamicFile_mutex)); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 453 | |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 454 | /* abs diff of total BBs between global counter for chosen seed & current run */ |
| 455 | uint64_t totalBBsDiff; |
| 456 | if (hfuzz->sanCovCnts.hitBBCnt > fuzzer->sanCovCnts.hitBBCnt) { |
| 457 | totalBBsDiff = hfuzz->sanCovCnts.hitBBCnt - fuzzer->sanCovCnts.hitBBCnt; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 458 | } else { |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 459 | totalBBsDiff = fuzzer->sanCovCnts.hitBBCnt - hfuzz->sanCovCnts.hitBBCnt; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 460 | } |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 461 | |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 462 | /* |
| 463 | * Keep mutated seed if: |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 464 | * a) Newly discovered (not met before) BBs && total hit BBs not significantly dropped |
| 465 | * b) More instrumented code accessed (BB hit counter bigger) |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 466 | * c) More instrumented DSOs loaded |
Robert Swiecki | 23ec02a | 2016-01-19 18:47:45 +0100 | [diff] [blame] | 467 | * |
Anestis Bechtsoudis | b78cf60 | 2016-01-07 13:10:50 +0200 | [diff] [blame] | 468 | * TODO: (a) method can significantly assist to further improvements in interesting areas |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 469 | * discovery if combined with seeds pool/queue support. If a runtime queue is maintained |
| 470 | * more interesting seeds can be saved between runs instead of instantly discarded |
| 471 | * based on current absolute elitism (only one mutated seed is promoted). |
| 472 | */ |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 473 | if ((fuzzer->sanCovCnts.newBBCnt > 0 && fuzzer->sanCovCnts.newBBCnt >= totalBBsDiff) || |
| 474 | hfuzz->sanCovCnts.hitBBCnt < fuzzer->sanCovCnts.hitBBCnt || |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 475 | hfuzz->sanCovCnts.iDsoCnt < fuzzer->sanCovCnts.iDsoCnt) { |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 476 | LOG_I("SanCov Update: file size (Cur,New): %zu,%zu, newBBs:%" PRIu64 |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 477 | ", counters (Cur,New): %" PRIu64 "/%" PRIu64 ",%" PRIu64 "/%" PRIu64, |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 478 | hfuzz->dynamicFileBestSz, fuzzer->dynamicFileSz, fuzzer->sanCovCnts.newBBCnt, |
| 479 | hfuzz->sanCovCnts.hitBBCnt, hfuzz->sanCovCnts.iDsoCnt, fuzzer->sanCovCnts.hitBBCnt, |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 480 | fuzzer->sanCovCnts.iDsoCnt); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 481 | |
| 482 | memcpy(hfuzz->dynamicFileBest, fuzzer->dynamicFile, fuzzer->dynamicFileSz); |
| 483 | |
Anestis Bechtsoudis | 1fc7cd4 | 2015-12-26 17:54:15 +0200 | [diff] [blame] | 484 | hfuzz->dynamicFileBestSz = fuzzer->dynamicFileSz; |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 485 | hfuzz->sanCovCnts.hitBBCnt = fuzzer->sanCovCnts.hitBBCnt; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 486 | hfuzz->sanCovCnts.dsoCnt = fuzzer->sanCovCnts.dsoCnt; |
| 487 | hfuzz->sanCovCnts.iDsoCnt = fuzzer->sanCovCnts.iDsoCnt; |
| 488 | hfuzz->sanCovCnts.crashesCnt += fuzzer->sanCovCnts.crashesCnt; |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 489 | hfuzz->sanCovCnts.newBBCnt += fuzzer->sanCovCnts.newBBCnt; |
Anestis Bechtsoudis | b78cf60 | 2016-01-07 13:10:50 +0200 | [diff] [blame] | 490 | |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 491 | if (hfuzz->sanCovCnts.totalBBCnt < fuzzer->sanCovCnts.totalBBCnt) { |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 492 | /* 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] | 493 | hfuzz->sanCovCnts.totalBBCnt = fuzzer->sanCovCnts.totalBBCnt; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 494 | } |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 495 | |
Anestis Bechtsoudis | e84d236 | 2015-12-27 12:43:08 +0200 | [diff] [blame] | 496 | /* Reset counter if better coverage achieved */ |
| 497 | __sync_fetch_and_and(&hfuzz->dynFileIterExpire, 0UL); |
| 498 | |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 499 | char currentBest[PATH_MAX], currentBestTmp[PATH_MAX]; |
| 500 | snprintf(currentBest, PATH_MAX, "%s/CURRENT_BEST", hfuzz->workDir); |
| 501 | snprintf(currentBestTmp, PATH_MAX, "%s/.tmp.CURRENT_BEST", hfuzz->workDir); |
| 502 | |
| 503 | if (files_writeBufToFile |
| 504 | (currentBestTmp, fuzzer->dynamicFile, fuzzer->dynamicFileSz, |
| 505 | O_WRONLY | O_CREAT | O_TRUNC)) { |
| 506 | rename(currentBestTmp, currentBest); |
| 507 | } |
| 508 | } |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 509 | } |
| 510 | |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 511 | static void fuzz_fuzzLoop(honggfuzz_t * hfuzz) |
| 512 | { |
| 513 | fuzzer_t fuzzer = { |
| 514 | .pid = 0, |
| 515 | .timeStartedMillis = util_timeNowMillis(), |
Anestis Bechtsoudis | ecb0a66 | 2015-09-27 18:19:46 +0300 | [diff] [blame] | 516 | .crashFileName = {0}, |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 517 | .pc = 0ULL, |
| 518 | .backtrace = 0ULL, |
| 519 | .access = 0ULL, |
| 520 | .exception = 0, |
| 521 | .dynamicFileSz = 0, |
| 522 | .dynamicFile = malloc(hfuzz->maxFileSz), |
Jagger | b409ee1 | 2015-09-09 02:02:32 +0200 | [diff] [blame] | 523 | .hwCnts = { |
| 524 | .cpuInstrCnt = 0ULL, |
| 525 | .cpuBranchCnt = 0ULL, |
Jagger | 39bd2b0 | 2016-02-04 01:16:15 +0100 | [diff] [blame] | 526 | .cpuBtsBlockCnt = 0ULL, |
| 527 | .cpuBtsEdgeCnt = 0ULL, |
Jagger | b409ee1 | 2015-09-09 02:02:32 +0200 | [diff] [blame] | 528 | .customCnt = 0ULL, |
| 529 | }, |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 530 | .sanCovCnts = { |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 531 | .hitBBCnt = 0ULL, |
| 532 | .totalBBCnt = 0ULL, |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 533 | .dsoCnt = 0ULL, |
| 534 | .iDsoCnt = 0ULL, |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 535 | .newBBCnt = 0ULL, |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 536 | .crashesCnt = 0ULL, |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 537 | }, |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 538 | .report = {'\0'}, |
Anestis Bechtsoudis | a1f8a03 | 2016-01-14 16:45:30 +0200 | [diff] [blame] | 539 | .mainWorker = true |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 540 | }; |
| 541 | if (fuzzer.dynamicFile == NULL) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 542 | LOG_F("malloc(%zu) failed", hfuzz->maxFileSz); |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 543 | } |
Robert Swiecki | fc35c5e | 2016-03-09 17:18:48 +0100 | [diff] [blame] | 544 | defer(free(fuzzer.dynamicFile)); |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 545 | |
Anestis Bechtsoudis | 2013f25 | 2015-11-07 22:03:31 +0200 | [diff] [blame] | 546 | size_t rnd_index = util_rndGet(0, hfuzz->fileCnt - 1); |
Anestis Bechtsoudis | 46ea10e | 2015-11-07 18:16:25 +0200 | [diff] [blame] | 547 | |
| 548 | /* If dry run mode, pick the next file and not a random one */ |
| 549 | if (hfuzz->flipRate == 0.0L && hfuzz->useVerifier) { |
| 550 | rnd_index = __sync_fetch_and_add(&hfuzz->lastCheckedFileIndex, 1UL); |
| 551 | } |
| 552 | |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 553 | strncpy(fuzzer.origFileName, files_basename(hfuzz->files[rnd_index]), PATH_MAX); |
| 554 | fuzz_getFileName(hfuzz, fuzzer.fileName); |
| 555 | |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 556 | if (hfuzz->dynFileMethod != _HF_DYNFILE_NONE || hfuzz->useSanCov) { |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 557 | if (!fuzz_prepareFileDynamically(hfuzz, &fuzzer, rnd_index)) { |
| 558 | exit(EXIT_FAILURE); |
| 559 | } |
| 560 | } else if (hfuzz->externalCommand != NULL) { |
| 561 | if (!fuzz_prepareFileExternally(hfuzz, &fuzzer, rnd_index)) { |
| 562 | exit(EXIT_FAILURE); |
| 563 | } |
| 564 | } else { |
| 565 | if (!fuzz_prepareFile(hfuzz, &fuzzer, rnd_index)) { |
| 566 | exit(EXIT_FAILURE); |
| 567 | } |
| 568 | } |
| 569 | |
Robert Swiecki | 1df61a9 | 2015-10-08 17:26:06 +0200 | [diff] [blame] | 570 | fuzzer.pid = arch_fork(hfuzz); |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 571 | if (fuzzer.pid == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 572 | PLOG_F("Couldn't fork"); |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 573 | exit(EXIT_FAILURE); |
| 574 | } |
| 575 | |
| 576 | if (!fuzzer.pid) { |
| 577 | /* |
| 578 | * Ok, kill the parent if this fails |
| 579 | */ |
| 580 | if (!arch_launchChild(hfuzz, fuzzer.fileName)) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 581 | LOG_E("Error launching child process, killing parent"); |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 582 | exit(EXIT_FAILURE); |
| 583 | } |
| 584 | } |
| 585 | |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 586 | LOG_D("Launched new process, pid: %d, (concurrency: %zd)", fuzzer.pid, hfuzz->threadsMax); |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 587 | |
| 588 | arch_reapChild(hfuzz, &fuzzer); |
| 589 | unlink(fuzzer.fileName); |
| 590 | |
| 591 | if (hfuzz->dynFileMethod != _HF_DYNFILE_NONE) { |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 592 | fuzz_perfFeedback(hfuzz, &fuzzer); |
| 593 | } else if (hfuzz->useSanCov) { |
| 594 | fuzz_sanCovFeedback(hfuzz, &fuzzer); |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Robert Swiecki | 23ec02a | 2016-01-19 18:47:45 +0100 | [diff] [blame] | 597 | /* |
Anestis Bechtsoudis | ac05480 | 2016-01-07 23:48:06 +0200 | [diff] [blame] | 598 | * If worker picked first iteration of new seed for dynFile, unlock the mutex |
| 599 | * so other threads can continue. |
| 600 | */ |
Anestis Bechtsoudis | a1f8a03 | 2016-01-14 16:45:30 +0200 | [diff] [blame] | 601 | if (hfuzz->isDynFileLocked) { |
| 602 | hfuzz->isDynFileLocked = false; |
Anestis Bechtsoudis | ac05480 | 2016-01-07 23:48:06 +0200 | [diff] [blame] | 603 | MX_UNLOCK(&hfuzz->workersBlock_mutex); |
| 604 | } |
| 605 | |
Anestis Bechtsoudis | c45db7f | 2015-11-30 16:44:58 +0200 | [diff] [blame] | 606 | if (hfuzz->useVerifier && (fuzzer.crashFileName[0] != 0) && fuzzer.backtrace) { |
Anestis Bechtsoudis | 3085f22 | 2015-09-27 18:35:26 +0300 | [diff] [blame] | 607 | if (!fuzz_runVerifier(hfuzz, &fuzzer)) { |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 608 | LOG_I("Failed to verify %s", fuzzer.crashFileName); |
Anestis Bechtsoudis | 3085f22 | 2015-09-27 18:35:26 +0300 | [diff] [blame] | 609 | } |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 610 | } |
| 611 | |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 612 | report_Report(hfuzz, fuzzer.report); |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 613 | } |
| 614 | |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 615 | static void *fuzz_threadNew(void *arg) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 616 | { |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 617 | honggfuzz_t *hfuzz = (honggfuzz_t *) arg; |
Jagger | ea39a8f | 2015-09-05 00:57:22 +0200 | [diff] [blame] | 618 | for (;;) { |
Anestis Bechtsoudis | 02b99be | 2015-12-27 11:53:01 +0200 | [diff] [blame] | 619 | /* Dynamic file iteration counter for same seed */ |
| 620 | __sync_fetch_and_add(&hfuzz->dynFileIterExpire, 1UL); |
| 621 | |
Anestis Bechtsoudis | 46ea10e | 2015-11-07 18:16:25 +0200 | [diff] [blame] | 622 | /* Check if dry run mode with verifier enabled */ |
| 623 | if (hfuzz->flipRate == 0.0L && hfuzz->useVerifier) { |
| 624 | if (__sync_fetch_and_add(&hfuzz->mutationsCnt, 1UL) >= hfuzz->fileCnt) { |
| 625 | __sync_fetch_and_add(&hfuzz->threadsFinished, 1UL); |
| 626 | // All files checked, weak-up the main process |
| 627 | pthread_kill(fuzz_mainThread, SIGALRM); |
| 628 | return NULL; |
| 629 | } |
| 630 | } |
| 631 | /* Check for max iterations limit if set */ |
| 632 | else if ((__sync_fetch_and_add(&hfuzz->mutationsCnt, 1UL) >= hfuzz->mutationsMax) |
| 633 | && hfuzz->mutationsMax) { |
Jagger | 6aa6e71 | 2015-09-06 20:52:04 +0200 | [diff] [blame] | 634 | __sync_fetch_and_add(&hfuzz->threadsFinished, 1UL); |
Robert Swiecki | 81c6a0d | 2015-09-08 15:43:20 +0200 | [diff] [blame] | 635 | // Wake-up the main process |
Jagger | 6113c43 | 2015-09-24 05:00:28 +0200 | [diff] [blame] | 636 | pthread_kill(fuzz_mainThread, SIGALRM); |
Jagger | ea39a8f | 2015-09-05 00:57:22 +0200 | [diff] [blame] | 637 | return NULL; |
| 638 | } |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 639 | |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 640 | fuzz_fuzzLoop(hfuzz); |
robert.swiecki@gmail.com | d4dd4df | 2015-02-18 00:50:12 +0000 | [diff] [blame] | 641 | } |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 642 | } |
| 643 | |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 644 | static void fuzz_runThread(honggfuzz_t * hfuzz, void *(*thread) (void *)) |
| 645 | { |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 646 | pthread_attr_t attr; |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 647 | |
robert.swiecki@gmail.com | 6f5c239 | 2015-02-16 18:13:09 +0000 | [diff] [blame] | 648 | pthread_attr_init(&attr); |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 649 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
robert.swiecki@gmail.com | 441089a | 2015-02-23 13:14:07 +0000 | [diff] [blame] | 650 | pthread_attr_setstacksize(&attr, _HF_PTHREAD_STACKSIZE); |
robert.swiecki@gmail.com | 011981f | 2015-02-17 19:06:44 +0000 | [diff] [blame] | 651 | pthread_attr_setguardsize(&attr, (size_t) sysconf(_SC_PAGESIZE)); |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 652 | |
robert.swiecki@gmail.com | 01b6dd4 | 2015-02-16 18:11:28 +0000 | [diff] [blame] | 653 | pthread_t t; |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 654 | if (pthread_create(&t, &attr, thread, (void *)hfuzz) < 0) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 655 | PLOG_F("Couldn't create a new thread"); |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | return; |
| 659 | } |
| 660 | |
Jagger | 0764ad7 | 2015-09-06 01:11:08 +0200 | [diff] [blame] | 661 | bool fuzz_setupTimer(void) |
| 662 | { |
| 663 | struct itimerval it = { |
Robert Swiecki | 6e08d46 | 2015-09-08 17:23:03 +0200 | [diff] [blame] | 664 | .it_value = {.tv_sec = 0,.tv_usec = 1}, |
Jagger | 0764ad7 | 2015-09-06 01:11:08 +0200 | [diff] [blame] | 665 | .it_interval = {.tv_sec = 1,.tv_usec = 0}, |
| 666 | }; |
| 667 | if (setitimer(ITIMER_REAL, &it, NULL) == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 668 | PLOG_E("setitimer(ITIMER_REAL)"); |
Jagger | 0764ad7 | 2015-09-06 01:11:08 +0200 | [diff] [blame] | 669 | return false; |
| 670 | } |
Jagger | 0764ad7 | 2015-09-06 01:11:08 +0200 | [diff] [blame] | 671 | return true; |
| 672 | } |
| 673 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 674 | void fuzz_main(honggfuzz_t * hfuzz) |
| 675 | { |
Jagger | 6113c43 | 2015-09-24 05:00:28 +0200 | [diff] [blame] | 676 | fuzz_mainThread = pthread_self(); |
| 677 | |
robert.swiecki@gmail.com | 9c77bb1 | 2015-04-23 15:35:28 +0000 | [diff] [blame] | 678 | struct sigaction sa = { |
Robert Swiecki | 6e08d46 | 2015-09-08 17:23:03 +0200 | [diff] [blame] | 679 | .sa_handler = fuzz_sigHandler, |
| 680 | .sa_flags = 0, |
robert.swiecki@gmail.com | 9c77bb1 | 2015-04-23 15:35:28 +0000 | [diff] [blame] | 681 | }; |
| 682 | sigemptyset(&sa.sa_mask); |
| 683 | if (sigaction(SIGTERM, &sa, NULL) == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 684 | PLOG_F("sigaction(SIGTERM) failed"); |
robert.swiecki@gmail.com | 9c77bb1 | 2015-04-23 15:35:28 +0000 | [diff] [blame] | 685 | } |
| 686 | if (sigaction(SIGINT, &sa, NULL) == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 687 | PLOG_F("sigaction(SIGINT) failed"); |
robert.swiecki@gmail.com | 9c77bb1 | 2015-04-23 15:35:28 +0000 | [diff] [blame] | 688 | } |
| 689 | if (sigaction(SIGQUIT, &sa, NULL) == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 690 | PLOG_F("sigaction(SIGQUIT) failed"); |
robert.swiecki@gmail.com | 9c77bb1 | 2015-04-23 15:35:28 +0000 | [diff] [blame] | 691 | } |
Robert Swiecki | 81c6a0d | 2015-09-08 15:43:20 +0200 | [diff] [blame] | 692 | if (sigaction(SIGALRM, &sa, NULL) == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 693 | PLOG_F("sigaction(SIGALRM) failed"); |
Robert Swiecki | 81c6a0d | 2015-09-08 15:43:20 +0200 | [diff] [blame] | 694 | } |
Jagger | 0764ad7 | 2015-09-06 01:11:08 +0200 | [diff] [blame] | 695 | if (fuzz_setupTimer() == false) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 696 | LOG_F("fuzz_setupTimer()"); |
Jagger | 0764ad7 | 2015-09-06 01:11:08 +0200 | [diff] [blame] | 697 | } |
robert.swiecki@gmail.com | e468320 | 2015-04-02 00:10:52 +0000 | [diff] [blame] | 698 | |
robert.swiecki@gmail.com | 956276a | 2015-04-16 16:51:52 +0000 | [diff] [blame] | 699 | if (!arch_archInit(hfuzz)) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 700 | LOG_F("Couldn't prepare arch for fuzzing"); |
robert.swiecki@gmail.com | ef829fa | 2011-06-22 13:51:57 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Jagger | ea39a8f | 2015-09-05 00:57:22 +0200 | [diff] [blame] | 703 | for (size_t i = 0; i < hfuzz->threadsMax; i++) { |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 704 | fuzz_runThread(hfuzz, fuzz_threadNew); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 705 | } |
robert.swiecki@gmail.com | 9c77bb1 | 2015-04-23 15:35:28 +0000 | [diff] [blame] | 706 | |
Jagger | ea39a8f | 2015-09-05 00:57:22 +0200 | [diff] [blame] | 707 | for (;;) { |
Jagger | 7acbf2e | 2015-09-06 20:02:32 +0200 | [diff] [blame] | 708 | if (hfuzz->useScreen) { |
| 709 | display_display(hfuzz); |
| 710 | } |
Jagger | ea39a8f | 2015-09-05 00:57:22 +0200 | [diff] [blame] | 711 | if (fuzz_sigReceived > 0) { |
| 712 | break; |
| 713 | } |
Jagger | 6aa6e71 | 2015-09-06 20:52:04 +0200 | [diff] [blame] | 714 | if (__sync_fetch_and_add(&hfuzz->threadsFinished, 0UL) >= hfuzz->threadsMax) { |
Jagger | ea39a8f | 2015-09-05 00:57:22 +0200 | [diff] [blame] | 715 | break; |
| 716 | } |
Robert Swiecki | 6e08d46 | 2015-09-08 17:23:03 +0200 | [diff] [blame] | 717 | pause(); |
Jagger | ea39a8f | 2015-09-05 00:57:22 +0200 | [diff] [blame] | 718 | } |
| 719 | |
robert.swiecki@gmail.com | cf4e8d0 | 2015-04-23 15:53:39 +0000 | [diff] [blame] | 720 | if (fuzz_sigReceived > 0) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 721 | LOG_I("Signal %d (%s) received, terminating", fuzz_sigReceived, |
| 722 | strsignal(fuzz_sigReceived)); |
robert.swiecki@gmail.com | cf4e8d0 | 2015-04-23 15:53:39 +0000 | [diff] [blame] | 723 | } |
Jagger | 3e1f54a | 2015-09-06 21:15:52 +0200 | [diff] [blame] | 724 | |
Anestis Bechtsoudis | 61b5ab1 | 2016-01-08 16:07:02 +0200 | [diff] [blame] | 725 | /* Clean-up global buffers */ |
Anestis Bechtsoudis | 8d19100 | 2015-09-10 17:36:10 +0300 | [diff] [blame] | 726 | free(hfuzz->files); |
| 727 | free(hfuzz->dynamicFileBest); |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 728 | if (hfuzz->dictionary) { |
Anestis Bechtsoudis | 70f2fbd | 2016-01-14 13:15:19 +0200 | [diff] [blame] | 729 | for (size_t i = 0; i < hfuzz->dictionaryCnt; i++) { |
| 730 | free(hfuzz->dictionary[i]); |
| 731 | } |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 732 | free(hfuzz->dictionary); |
| 733 | } |
| 734 | if (hfuzz->blacklist) { |
| 735 | free(hfuzz->blacklist); |
| 736 | } |
Anestis Bechtsoudis | 61b5ab1 | 2016-01-08 16:07:02 +0200 | [diff] [blame] | 737 | if (hfuzz->sanOpts.asanOpts) { |
| 738 | free(hfuzz->sanOpts.asanOpts); |
| 739 | } |
| 740 | if (hfuzz->sanOpts.ubsanOpts) { |
| 741 | free(hfuzz->sanOpts.ubsanOpts); |
| 742 | } |
| 743 | if (hfuzz->sanOpts.msanOpts) { |
| 744 | free(hfuzz->sanOpts.msanOpts); |
| 745 | } |
Anestis Bechtsoudis | 7c88d7a | 2016-02-09 17:55:38 +0200 | [diff] [blame] | 746 | if (hfuzz->pidCmd) { |
| 747 | free(hfuzz->pidCmd); |
| 748 | } |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 749 | } |