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 "fuzz.h" |
| 27 | |
| 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
robert.swiecki@gmail.com | 90e9911 | 2015-02-15 02:05:14 +0000 | [diff] [blame] | 30 | #include <inttypes.h> |
Robert Swiecki | 2af83ec | 2017-06-05 23:54:22 +0200 | [diff] [blame] | 31 | #include <libgen.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 | 10eeb0a | 2017-09-28 15:42:52 +0200 | [diff] [blame] | 39 | #include <sys/mman.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 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> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 44 | #include <time.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 45 | #include <unistd.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 46 | |
Robert Swiecki | d0fa62c | 2017-09-28 18:11:05 +0200 | [diff] [blame] | 47 | #include "arch.h" |
| 48 | #include "honggfuzz.h" |
| 49 | #include "input.h" |
Robert Swiecki | 10eeb0a | 2017-09-28 15:42:52 +0200 | [diff] [blame] | 50 | #include "libcommon/common.h" |
Robert Swiecki | 241a741 | 2017-05-24 01:53:15 +0200 | [diff] [blame] | 51 | #include "libcommon/files.h" |
| 52 | #include "libcommon/log.h" |
Robert Swiecki | 241a741 | 2017-05-24 01:53:15 +0200 | [diff] [blame] | 53 | #include "libcommon/util.h" |
robert.swiecki@gmail.com | 36700b5 | 2015-02-22 05:03:16 +0000 | [diff] [blame] | 54 | #include "mangle.h" |
robert.swiecki@gmail.com | e7190b9 | 2015-02-14 23:05:42 +0000 | [diff] [blame] | 55 | #include "report.h" |
Robert Swiecki | ec7b845 | 2017-06-01 13:25:56 +0200 | [diff] [blame] | 56 | #include "sancov.h" |
| 57 | #include "sanitizers.h" |
Robert Swiecki | 2d01690 | 2017-05-24 02:12:30 +0200 | [diff] [blame] | 58 | #include "subproc.h" |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 59 | |
Jagger | 6113c43 | 2015-09-24 05:00:28 +0200 | [diff] [blame] | 60 | static pthread_t fuzz_mainThread; |
| 61 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 62 | static void fuzz_getFileName(honggfuzz_t* hfuzz, run_t* run) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 63 | { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 64 | snprintf(run->fileName, PATH_MAX, "%s/honggfuzz.input.%" PRIu32 ".%s.%s", hfuzz->workDir, |
| 65 | run->fuzzNo, basename(hfuzz->cmdline[0]), hfuzz->fileExtn); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 68 | static bool fuzz_prepareFileDynamically(honggfuzz_t* hfuzz, run_t* run) |
robert.swiecki@gmail.com | 6d6f756 | 2015-02-17 22:18:51 +0000 | [diff] [blame] | 69 | { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 70 | run->origFileName = "[DYNAMIC]"; |
Robert Swiecki | a08ab1c | 2016-03-14 18:29:21 +0100 | [diff] [blame] | 71 | |
| 72 | { |
Robert Swiecki | bf8f8cc | 2017-11-09 00:42:50 +0100 | [diff] [blame] | 73 | MX_SCOPED_RWLOCK_READ(&hfuzz->dynfileq_mutex); |
Robert Swiecki | 4b0390a | 2016-03-14 18:44:05 +0100 | [diff] [blame] | 74 | |
Jagger | b070b81 | 2016-03-15 05:31:30 +0100 | [diff] [blame] | 75 | if (hfuzz->dynfileqCnt == 0) { |
Jagger | e449651 | 2016-03-16 01:55:46 +0100 | [diff] [blame] | 76 | LOG_F("The dynamic file corpus is empty. Apparently, the initial fuzzing of the " |
| 77 | "provided file corpus (-f) has not produced any follow-up files with positive " |
| 78 | "coverage and/or CPU counters"); |
Jagger | b070b81 | 2016-03-15 05:31:30 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 81 | if (run->dynfileqCurrent == NULL) { |
| 82 | run->dynfileqCurrent = TAILQ_FIRST(&hfuzz->dynfileq); |
Robert Swiecki | 3db3429 | 2017-11-09 14:13:21 +0100 | [diff] [blame] | 83 | } else { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 84 | if (run->dynfileqCurrent == TAILQ_LAST(&hfuzz->dynfileq, dyns_t)) { |
| 85 | run->dynfileqCurrent = TAILQ_FIRST(&hfuzz->dynfileq); |
Robert Swiecki | 3db3429 | 2017-11-09 14:13:21 +0100 | [diff] [blame] | 86 | } else { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 87 | run->dynfileqCurrent = TAILQ_NEXT(run->dynfileqCurrent, pointers); |
Robert Swiecki | 3db3429 | 2017-11-09 14:13:21 +0100 | [diff] [blame] | 88 | } |
Robert Swiecki | 3bfc33c | 2016-03-14 18:12:41 +0100 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 92 | memcpy(run->dynamicFile, run->dynfileqCurrent->data, run->dynfileqCurrent->size); |
| 93 | run->dynamicFileSz = run->dynfileqCurrent->size; |
Robert Swiecki | 3bfc33c | 2016-03-14 18:12:41 +0100 | [diff] [blame] | 94 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 95 | mangle_mangleContent(hfuzz, run); |
robert.swiecki@gmail.com | 6d6f756 | 2015-02-17 22:18:51 +0000 | [diff] [blame] | 96 | |
Robert Swiecki | d0fa62c | 2017-09-28 18:11:05 +0200 | [diff] [blame] | 97 | if (hfuzz->persistent == false |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 98 | && files_writeBufToFile(run->fileName, run->dynamicFile, run->dynamicFileSz, |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 99 | O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC) |
| 100 | == false) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 101 | LOG_E("Couldn't write buffer to file '%s'", run->fileName); |
robert.swiecki@gmail.com | 3b6c629 | 2015-02-26 11:48:46 +0000 | [diff] [blame] | 102 | return false; |
| 103 | } |
robert.swiecki@gmail.com | 6d6f756 | 2015-02-17 22:18:51 +0000 | [diff] [blame] | 104 | |
robert.swiecki@gmail.com | 6d6f756 | 2015-02-17 22:18:51 +0000 | [diff] [blame] | 105 | return true; |
robert.swiecki@gmail.com | d7aed31 | 2015-02-03 21:26:37 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 108 | static bool fuzz_prepareFile(honggfuzz_t* hfuzz, run_t* run, bool rewind) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 109 | { |
Robert Swiecki | 92a3136 | 2017-02-24 16:21:40 +0100 | [diff] [blame] | 110 | char fname[PATH_MAX]; |
Robert Swiecki | 10eeb0a | 2017-09-28 15:42:52 +0200 | [diff] [blame] | 111 | if (input_getNext(hfuzz, fname, rewind) == false) { |
Robert Swiecki | 92a3136 | 2017-02-24 16:21:40 +0100 | [diff] [blame] | 112 | return false; |
| 113 | } |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 114 | run->origFileName = files_basename(fname); |
Robert Swiecki | e8f8e8d | 2016-10-03 23:51:32 +0200 | [diff] [blame] | 115 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 116 | ssize_t fileSz = files_readFileToBufMax(fname, run->dynamicFile, hfuzz->maxFileSz); |
Jagger | 1aa94d7 | 2016-04-02 02:37:35 +0200 | [diff] [blame] | 117 | if (fileSz < 0) { |
Robert Swiecki | 92a3136 | 2017-02-24 16:21:40 +0100 | [diff] [blame] | 118 | LOG_E("Couldn't read contents of '%s'", fname); |
robert.swiecki@gmail.com | bb5d264 | 2015-02-25 20:00:00 +0000 | [diff] [blame] | 119 | return false; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 120 | } |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 121 | run->dynamicFileSz = fileSz; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 122 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 123 | mangle_mangleContent(hfuzz, run); |
robert.swiecki@gmail.com | c070b94 | 2015-02-25 18:29:19 +0000 | [diff] [blame] | 124 | |
Robert Swiecki | d0fa62c | 2017-09-28 18:11:05 +0200 | [diff] [blame] | 125 | if (hfuzz->persistent == false |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 126 | && files_writeBufToFile(run->fileName, run->dynamicFile, run->dynamicFileSz, |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 127 | O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC) |
| 128 | == false) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 129 | LOG_E("Couldn't write buffer to file '%s'", run->fileName); |
robert.swiecki@gmail.com | e768052 | 2015-02-22 22:22:37 +0000 | [diff] [blame] | 130 | return false; |
| 131 | } |
| 132 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 136 | static bool fuzz_prepareFileExternally(honggfuzz_t* hfuzz, run_t* run) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 137 | { |
Robert Swiecki | c1deec8 | 2017-04-27 02:42:12 +0200 | [diff] [blame] | 138 | char fname[PATH_MAX]; |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 139 | if (input_getNext(hfuzz, fname, true /* rewind */)) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 140 | run->origFileName = files_basename(fname); |
| 141 | if (files_copyFile(fname, run->fileName, NULL, false /* try_link */) == false) { |
| 142 | LOG_E("files_copyFile('%s', '%s')", fname, run->fileName); |
Robert Swiecki | c1deec8 | 2017-04-27 02:42:12 +0200 | [diff] [blame] | 143 | return false; |
| 144 | } |
Robert Swiecki | 87ca211 | 2017-04-27 12:03:53 +0200 | [diff] [blame] | 145 | } else { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 146 | run->origFileName = "[EXTERNAL]"; |
| 147 | int dstfd = open(run->fileName, O_CREAT | O_TRUNC | O_RDWR | O_CLOEXEC, 0644); |
Robert Swiecki | 87ca211 | 2017-04-27 12:03:53 +0200 | [diff] [blame] | 148 | if (dstfd == -1) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 149 | PLOG_E("Couldn't create a temporary file '%s'", run->fileName); |
Robert Swiecki | 87ca211 | 2017-04-27 12:03:53 +0200 | [diff] [blame] | 150 | return false; |
| 151 | } |
| 152 | close(dstfd); |
Robert Swiecki | c1deec8 | 2017-04-27 02:42:12 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 155 | LOG_D("Created '%s' as an input file", run->fileName); |
robert.swiecki | 3d505e2 | 2010-10-14 01:17:17 +0000 | [diff] [blame] | 156 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 157 | const char* const argv[] = { hfuzz->externalCommand, run->fileName, NULL }; |
| 158 | if (subproc_System(hfuzz, run, argv) != 0) { |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 159 | LOG_E("Subprocess '%s' returned abnormally", hfuzz->externalCommand); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 160 | return false; |
| 161 | } |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 162 | LOG_D("Subporcess '%s' finished with success", hfuzz->externalCommand); |
Jagger | 1d74338 | 2016-04-02 02:09:41 +0200 | [diff] [blame] | 163 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 164 | ssize_t rsz = files_readFileToBufMax(run->fileName, run->dynamicFile, hfuzz->maxFileSz); |
Jagger | 1aa94d7 | 2016-04-02 02:37:35 +0200 | [diff] [blame] | 165 | if (rsz < 0) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 166 | LOG_W("Couldn't read back '%s' to the buffer", run->fileName); |
Jagger | 1d74338 | 2016-04-02 02:09:41 +0200 | [diff] [blame] | 167 | return false; |
| 168 | } |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 169 | run->dynamicFileSz = rsz; |
Jagger | 1d74338 | 2016-04-02 02:09:41 +0200 | [diff] [blame] | 170 | |
| 171 | if (hfuzz->persistent) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 172 | unlink(run->fileName); |
Jagger | 1d74338 | 2016-04-02 02:09:41 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | return true; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 178 | static bool fuzz_postProcessFile(honggfuzz_t* hfuzz, run_t* run) |
Robert Swiecki | ee266ac | 2016-10-03 02:25:59 +0200 | [diff] [blame] | 179 | { |
Robert Swiecki | cc88f93 | 2017-07-07 00:13:00 +0200 | [diff] [blame] | 180 | if (hfuzz->persistent) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 181 | if (files_writeBufToFile(run->fileName, run->dynamicFile, run->dynamicFileSz, |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 182 | O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC) |
Robert Swiecki | d0fa62c | 2017-09-28 18:11:05 +0200 | [diff] [blame] | 183 | == false) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 184 | LOG_E("Couldn't write file to '%s'", run->fileName); |
Robert Swiecki | cc88f93 | 2017-07-07 00:13:00 +0200 | [diff] [blame] | 185 | return false; |
| 186 | } |
Robert Swiecki | ee266ac | 2016-10-03 02:25:59 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 189 | const char* const argv[] = { hfuzz->postExternalCommand, run->fileName, NULL }; |
| 190 | if (subproc_System(hfuzz, run, argv) != 0) { |
Robert Swiecki | ee266ac | 2016-10-03 02:25:59 +0200 | [diff] [blame] | 191 | LOG_E("Subprocess '%s' returned abnormally", hfuzz->postExternalCommand); |
| 192 | return false; |
| 193 | } |
| 194 | LOG_D("Subporcess '%s' finished with success", hfuzz->externalCommand); |
| 195 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 196 | ssize_t rsz = files_readFileToBufMax(run->fileName, run->dynamicFile, hfuzz->maxFileSz); |
Robert Swiecki | ee266ac | 2016-10-03 02:25:59 +0200 | [diff] [blame] | 197 | if (rsz < 0) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 198 | LOG_W("Couldn't read back '%s' to the buffer", run->fileName); |
Robert Swiecki | ee266ac | 2016-10-03 02:25:59 +0200 | [diff] [blame] | 199 | return false; |
| 200 | } |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 201 | run->dynamicFileSz = rsz; |
Robert Swiecki | ee266ac | 2016-10-03 02:25:59 +0200 | [diff] [blame] | 202 | |
| 203 | return true; |
| 204 | } |
| 205 | |
Robert Swiecki | 0b56611 | 2017-10-17 17:39:07 +0200 | [diff] [blame] | 206 | static fuzzState_t fuzz_getState(honggfuzz_t* hfuzz) { return ATOMIC_GET(hfuzz->state); } |
Robert Swiecki | a7841da | 2017-02-24 17:27:06 +0100 | [diff] [blame] | 207 | |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 208 | static void fuzz_setState(honggfuzz_t* hfuzz, fuzzState_t state) |
Robert Swiecki | a7841da | 2017-02-24 17:27:06 +0100 | [diff] [blame] | 209 | { |
Robert Swiecki | 3915a81 | 2017-02-25 02:07:56 +0100 | [diff] [blame] | 210 | /* All threads must indicate willingness to switch to _HF_STATE_DYNAMIC_MAIN */ |
| 211 | if (state == _HF_STATE_DYNAMIC_MAIN) { |
| 212 | static size_t cnt = 0; |
| 213 | ATOMIC_PRE_INC(cnt); |
| 214 | while (ATOMIC_GET(cnt) < hfuzz->threadsMax) { |
Robert Swiecki | 1a99473 | 2017-05-31 18:32:21 +0200 | [diff] [blame] | 215 | if (ATOMIC_GET(hfuzz->terminating) == true) { |
| 216 | return; |
| 217 | } |
Robert Swiecki | 3915a81 | 2017-02-25 02:07:56 +0100 | [diff] [blame] | 218 | sleep(1); |
| 219 | } |
| 220 | } |
| 221 | |
Robert Swiecki | a7841da | 2017-02-24 17:27:06 +0100 | [diff] [blame] | 222 | static pthread_mutex_t state_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 223 | MX_SCOPED_LOCK(&state_mutex); |
| 224 | |
| 225 | if (hfuzz->state == state) { |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | switch (state) { |
| 230 | case _HF_STATE_DYNAMIC_PRE: |
| 231 | LOG_I("Entering phase 1/2: Dry Run"); |
| 232 | break; |
| 233 | case _HF_STATE_DYNAMIC_MAIN: |
| 234 | LOG_I("Entering phase 2/2: Main"); |
| 235 | break; |
| 236 | case _HF_STATE_STATIC: |
| 237 | LOG_I("Entering phase: Static"); |
| 238 | break; |
| 239 | default: |
| 240 | LOG_I("Entering unknown phase: %d", state); |
| 241 | break; |
| 242 | } |
| 243 | |
| 244 | ATOMIC_SET(hfuzz->state, state); |
| 245 | } |
| 246 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 247 | static bool fuzz_runVerifier(honggfuzz_t* hfuzz, run_t* crashedFuzzer) |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 248 | { |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 249 | int crashFd = -1; |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 250 | uint8_t* crashBuf = NULL; |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 251 | off_t crashFileSz = 0; |
| 252 | |
| 253 | crashBuf = files_mapFile(crashedFuzzer->crashFileName, &crashFileSz, &crashFd, false); |
| 254 | if (crashBuf == NULL) { |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 255 | 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] | 256 | return false; |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 257 | } |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 258 | defer |
| 259 | { |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 260 | munmap(crashBuf, crashFileSz); |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 261 | close(crashFd); |
| 262 | }; |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 263 | |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 264 | LOG_I("Launching verifier for %" PRIx64 " hash", crashedFuzzer->backtrace); |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 265 | for (int i = 0; i < _HF_VERIFIER_ITER; i++) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 266 | run_t vFuzzer = { |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 267 | .pid = 0, |
Anestis Bechtsoudis | 92b9704 | 2016-04-27 12:34:19 +0300 | [diff] [blame] | 268 | .persistentPid = 0, |
Robert Swiecki | a7841da | 2017-02-24 17:27:06 +0100 | [diff] [blame] | 269 | .state = fuzz_getState(hfuzz), |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 270 | .timeStartedMillis = util_timeNowMillis(), |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 271 | .crashFileName = { 0 }, |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 272 | .pc = 0ULL, |
| 273 | .backtrace = 0ULL, |
| 274 | .access = 0ULL, |
| 275 | .exception = 0, |
Robert Swiecki | bf8f8cc | 2017-11-09 00:42:50 +0100 | [diff] [blame] | 276 | .dynfileqCurrent = NULL, |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 277 | .dynamicFileSz = 0, |
| 278 | .dynamicFile = NULL, |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 279 | .sanCovCnts = { |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 280 | .hitBBCnt = 0ULL, |
| 281 | .totalBBCnt = 0ULL, |
| 282 | .dsoCnt = 0ULL, |
| 283 | .iDsoCnt = 0ULL, |
| 284 | .newBBCnt = 0ULL, |
| 285 | .crashesCnt = 0ULL, |
| 286 | }, |
| 287 | .report = { '\0' }, |
Jagger | b2c1fad | 2016-03-17 23:37:10 +0100 | [diff] [blame] | 288 | .mainWorker = false, |
Jagger | fa3544a | 2016-08-30 02:55:55 +0200 | [diff] [blame] | 289 | .fuzzNo = crashedFuzzer->fuzzNo, |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 290 | .persistentSock = -1, |
Robert Swiecki | 013bc9c | 2016-12-12 17:31:06 +0100 | [diff] [blame] | 291 | .tmOutSignaled = false, |
Jagger | b2c1fad | 2016-03-17 23:37:10 +0100 | [diff] [blame] | 292 | |
Jagger | 2381ef4 | 2016-03-20 23:32:05 +0100 | [diff] [blame] | 293 | .linux = { |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 294 | .hwCnts = { |
| 295 | .cpuInstrCnt = 0ULL, |
| 296 | .cpuBranchCnt = 0ULL, |
| 297 | .bbCnt = 0ULL, |
| 298 | .newBBCnt = 0ULL, |
| 299 | .softCntPc = 0ULL, |
| 300 | .softCntEdge = 0ULL, |
| 301 | .softCntCmp = 0ULL, |
| 302 | }, |
| 303 | .attachedPid = 0, |
| 304 | }, |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 305 | }; |
| 306 | |
Anestis Bechtsoudis | 92b9704 | 2016-04-27 12:34:19 +0300 | [diff] [blame] | 307 | if (arch_archThreadInit(hfuzz, &vFuzzer) == false) { |
| 308 | LOG_F("Could not initialize the thread"); |
| 309 | } |
| 310 | |
Robert Swiecki | eaf2732 | 2017-06-01 21:49:26 +0200 | [diff] [blame] | 311 | fuzz_getFileName(hfuzz, &vFuzzer); |
Robert Swiecki | 0b56611 | 2017-10-17 17:39:07 +0200 | [diff] [blame] | 312 | if (files_writeBufToFile( |
| 313 | vFuzzer.fileName, crashBuf, crashFileSz, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC) |
Robert Swiecki | d0fa62c | 2017-09-28 18:11:05 +0200 | [diff] [blame] | 314 | == false) { |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 315 | LOG_E("Couldn't write buffer to file '%s'", vFuzzer.fileName); |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame] | 316 | return false; |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 317 | } |
| 318 | |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 319 | if (subproc_Run(hfuzz, &vFuzzer) == false) { |
| 320 | LOG_F("subproc_Run()"); |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 321 | } |
Robert Swiecki | a8b8cb8 | 2016-08-17 18:27:08 +0200 | [diff] [blame] | 322 | |
Anestis Bechtsoudis | 2b2a63b | 2016-12-28 15:34:40 +0200 | [diff] [blame] | 323 | /* Delete intermediate files generated from verifier */ |
| 324 | unlink(vFuzzer.fileName); |
| 325 | |
Anestis Bechtsoudis | ecb0a66 | 2015-09-27 18:19:46 +0300 | [diff] [blame] | 326 | /* If stack hash doesn't match skip name tag and exit */ |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 327 | if (crashedFuzzer->backtrace != vFuzzer.backtrace) { |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 328 | LOG_D("Verifier stack hash mismatch"); |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame] | 329 | return false; |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | /* Workspace is inherited, just append a extra suffix */ |
| 334 | char verFile[PATH_MAX] = { 0 }; |
| 335 | snprintf(verFile, sizeof(verFile), "%s.verified", crashedFuzzer->crashFileName); |
Anestis Bechtsoudis | ecb0a66 | 2015-09-27 18:19:46 +0300 | [diff] [blame] | 336 | |
Anestis Bechtsoudis | d86e660 | 2015-11-07 18:34:06 +0200 | [diff] [blame] | 337 | /* Copy file with new suffix & remove original copy */ |
| 338 | bool dstFileExists = false; |
Robert Swiecki | 0b56611 | 2017-10-17 17:39:07 +0200 | [diff] [blame] | 339 | if (files_copyFile( |
| 340 | crashedFuzzer->crashFileName, verFile, &dstFileExists, true /* try_link */)) { |
Anestis Bechtsoudis | d86e660 | 2015-11-07 18:34:06 +0200 | [diff] [blame] | 341 | LOG_I("Successfully verified, saving as (%s)", verFile); |
Jagger | d34417d | 2016-03-16 01:26:54 +0100 | [diff] [blame] | 342 | ATOMIC_POST_INC(hfuzz->verifiedCrashesCnt); |
Anestis Bechtsoudis | d86e660 | 2015-11-07 18:34:06 +0200 | [diff] [blame] | 343 | unlink(crashedFuzzer->crashFileName); |
| 344 | } else { |
| 345 | if (dstFileExists) { |
| 346 | LOG_I("It seems that '%s' already exists, skipping", verFile); |
| 347 | } else { |
| 348 | LOG_E("Couldn't copy '%s' to '%s'", crashedFuzzer->crashFileName, verFile); |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame] | 349 | return false; |
Anestis Bechtsoudis | d86e660 | 2015-11-07 18:34:06 +0200 | [diff] [blame] | 350 | } |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 351 | } |
Anestis Bechtsoudis | ecb0a66 | 2015-09-27 18:19:46 +0300 | [diff] [blame] | 352 | |
Robert Swiecki | e48811e | 2016-03-09 18:31:19 +0100 | [diff] [blame] | 353 | return true; |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 354 | } |
| 355 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 356 | static void fuzz_addFileToFileQ(honggfuzz_t* hfuzz, run_t* run) |
Robert Swiecki | f3534bb | 2016-03-14 18:55:10 +0100 | [diff] [blame] | 357 | { |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 358 | struct dynfile_t* dynfile = (struct dynfile_t*)util_Malloc(sizeof(struct dynfile_t)); |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 359 | dynfile->size = run->dynamicFileSz; |
| 360 | dynfile->data = (uint8_t*)util_Malloc(run->dynamicFileSz); |
| 361 | memcpy(dynfile->data, run->dynamicFile, run->dynamicFileSz); |
Robert Swiecki | 37498fd | 2017-03-12 21:12:54 +0100 | [diff] [blame] | 362 | |
Robert Swiecki | bf8f8cc | 2017-11-09 00:42:50 +0100 | [diff] [blame] | 363 | MX_SCOPED_RWLOCK_WRITE(&hfuzz->dynfileq_mutex); |
Robert Swiecki | b0e261a | 2017-11-09 01:30:54 +0100 | [diff] [blame] | 364 | TAILQ_INSERT_TAIL(&hfuzz->dynfileq, dynfile, pointers); |
Robert Swiecki | f3534bb | 2016-03-14 18:55:10 +0100 | [diff] [blame] | 365 | hfuzz->dynfileqCnt++; |
| 366 | |
Jagger | 3c7e7ce | 2016-09-25 16:05:19 +0200 | [diff] [blame] | 367 | /* No need to add new coverage if we are supposed to append new coverage-inducing inputs only */ |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 368 | if (run->state == _HF_STATE_DYNAMIC_PRE && hfuzz->covDir == NULL) { |
Jagger | 1b2d482 | 2016-09-25 16:19:45 +0200 | [diff] [blame] | 369 | 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] | 370 | return; |
| 371 | } |
| 372 | |
Robert Swiecki | f3534bb | 2016-03-14 18:55:10 +0100 | [diff] [blame] | 373 | char fname[PATH_MAX]; |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 374 | uint64_t crc64f = util_CRC64(run->dynamicFile, run->dynamicFileSz); |
| 375 | uint64_t crc64r = util_CRC64Rev(run->dynamicFile, run->dynamicFileSz); |
Robert Swiecki | fd02b69 | 2017-02-22 02:33:27 +0100 | [diff] [blame] | 376 | snprintf(fname, sizeof(fname), "%s/%016" PRIx64 "%016" PRIx64 ".%08" PRIx32 ".honggfuzz.cov", |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 377 | hfuzz->covDir ? hfuzz->covDir : hfuzz->inputDir, crc64f, crc64r, |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 378 | (uint32_t)run->dynamicFileSz); |
Jagger | 3c7e7ce | 2016-09-25 16:05:19 +0200 | [diff] [blame] | 379 | |
Robert Swiecki | 22f6bef | 2017-01-31 01:45:26 +0100 | [diff] [blame] | 380 | if (access(fname, R_OK) == 0) { |
| 381 | LOG_D("File '%s' already exists in the corpus directory", fname); |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | LOG_D("Adding file '%s' to the corpus directory", fname); |
| 386 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 387 | if (files_writeBufToFile(fname, run->dynamicFile, run->dynamicFileSz, |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 388 | O_WRONLY | O_CREAT | O_EXCL | O_TRUNC | O_CLOEXEC) |
Jagger | 33cce5d | 2016-04-16 20:10:36 +0200 | [diff] [blame] | 389 | == false) { |
Robert Swiecki | f3534bb | 2016-03-14 18:55:10 +0100 | [diff] [blame] | 390 | LOG_W("Couldn't write buffer to file '%s'", fname); |
| 391 | } |
| 392 | } |
| 393 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 394 | static void fuzz_perfFeedback(honggfuzz_t* hfuzz, run_t* run) |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 395 | { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 396 | if (hfuzz->skipFeedbackOnTimeout && run->tmOutSignaled) { |
Robert Swiecki | 53ec9e4 | 2017-02-15 20:34:27 +0100 | [diff] [blame] | 397 | return; |
| 398 | } |
| 399 | |
Robert Swiecki | 0b56611 | 2017-10-17 17:39:07 +0200 | [diff] [blame] | 400 | LOG_D("New file size: %zu, Perf feedback new/cur (instr,branch): %" PRIu64 "/%" PRIu64 |
| 401 | "/%" PRIu64 "/%" PRIu64 ", BBcnt new/total: %" PRIu64 "/%" PRIu64, |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 402 | run->dynamicFileSz, run->linux.hwCnts.cpuInstrCnt, hfuzz->linux.hwCnts.cpuInstrCnt, |
| 403 | run->linux.hwCnts.cpuBranchCnt, hfuzz->linux.hwCnts.cpuBranchCnt, |
| 404 | run->linux.hwCnts.newBBCnt, hfuzz->linux.hwCnts.bbCnt); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 405 | |
Robert Swiecki | 37498fd | 2017-03-12 21:12:54 +0100 | [diff] [blame] | 406 | MX_SCOPED_LOCK(&hfuzz->feedback_mutex); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 407 | |
Jagger | 34789a7 | 2016-09-08 00:36:09 +0200 | [diff] [blame] | 408 | uint64_t softCntPc = 0UL; |
Robert Swiecki | b27c895 | 2017-05-23 23:38:04 +0200 | [diff] [blame] | 409 | uint64_t softCntEdge = 0UL; |
Jagger | 34789a7 | 2016-09-08 00:36:09 +0200 | [diff] [blame] | 410 | uint64_t softCntCmp = 0UL; |
Jagger | 251d019 | 2016-08-24 00:54:04 +0200 | [diff] [blame] | 411 | if (hfuzz->bbFd != -1) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 412 | softCntPc = ATOMIC_GET(hfuzz->feedback->pidFeedbackPc[run->fuzzNo]); |
| 413 | ATOMIC_CLEAR(hfuzz->feedback->pidFeedbackPc[run->fuzzNo]); |
| 414 | softCntEdge = ATOMIC_GET(hfuzz->feedback->pidFeedbackEdge[run->fuzzNo]); |
| 415 | ATOMIC_CLEAR(hfuzz->feedback->pidFeedbackEdge[run->fuzzNo]); |
| 416 | softCntCmp = ATOMIC_GET(hfuzz->feedback->pidFeedbackCmp[run->fuzzNo]); |
| 417 | ATOMIC_CLEAR(hfuzz->feedback->pidFeedbackCmp[run->fuzzNo]); |
Jagger | 251d019 | 2016-08-24 00:54:04 +0200 | [diff] [blame] | 418 | } |
Jagger | b01aaae | 2016-08-20 03:35:38 +0200 | [diff] [blame] | 419 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 420 | int64_t diff0 = hfuzz->linux.hwCnts.cpuInstrCnt - run->linux.hwCnts.cpuInstrCnt; |
| 421 | int64_t diff1 = hfuzz->linux.hwCnts.cpuBranchCnt - run->linux.hwCnts.cpuBranchCnt; |
Jagger | 302c2ea | 2016-09-07 03:54:43 +0200 | [diff] [blame] | 422 | |
Jagger | 8d0002c | 2016-08-17 03:44:23 +0200 | [diff] [blame] | 423 | /* |
| 424 | * Coverage is the primary counter, the rest is secondary, and taken into consideration only |
| 425 | * if the coverage counter has not been changed |
| 426 | */ |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 427 | if (run->linux.hwCnts.newBBCnt > 0 || softCntPc > 0 || softCntEdge > 0 || softCntCmp > 0 |
Robert Swiecki | b27c895 | 2017-05-23 23:38:04 +0200 | [diff] [blame] | 428 | || diff0 < 0 || diff1 < 0) { |
Robert Swiecki | 92ec8d2 | 2016-11-21 01:10:18 +0100 | [diff] [blame] | 429 | |
| 430 | if (diff0 < 0) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 431 | hfuzz->linux.hwCnts.cpuInstrCnt = run->linux.hwCnts.cpuInstrCnt; |
Robert Swiecki | 92ec8d2 | 2016-11-21 01:10:18 +0100 | [diff] [blame] | 432 | } |
| 433 | if (diff1 < 0) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 434 | hfuzz->linux.hwCnts.cpuBranchCnt = run->linux.hwCnts.cpuBranchCnt; |
Robert Swiecki | 92ec8d2 | 2016-11-21 01:10:18 +0100 | [diff] [blame] | 435 | } |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 436 | hfuzz->linux.hwCnts.bbCnt += run->linux.hwCnts.newBBCnt; |
Jagger | 34789a7 | 2016-09-08 00:36:09 +0200 | [diff] [blame] | 437 | hfuzz->linux.hwCnts.softCntPc += softCntPc; |
Robert Swiecki | b27c895 | 2017-05-23 23:38:04 +0200 | [diff] [blame] | 438 | hfuzz->linux.hwCnts.softCntEdge += softCntEdge; |
Jagger | 34789a7 | 2016-09-08 00:36:09 +0200 | [diff] [blame] | 439 | hfuzz->linux.hwCnts.softCntCmp += softCntCmp; |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 440 | |
Robert Swiecki | 0b56611 | 2017-10-17 17:39:07 +0200 | [diff] [blame] | 441 | LOG_I("Size:%zu (i,b,edg,ip,hw,cmp): %" PRIu64 "/%" PRIu64 "/%" PRIu64 "/%" PRIu64 |
| 442 | "/%" PRIu64 "/%" PRIu64 ", Tot:%" PRIu64 "/%" PRIu64 "/%" PRIu64 "/%" PRIu64 |
| 443 | "/%" PRIu64 "/%" PRIu64, |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 444 | run->dynamicFileSz, run->linux.hwCnts.cpuInstrCnt, run->linux.hwCnts.cpuBranchCnt, |
| 445 | softCntEdge, softCntPc, run->linux.hwCnts.newBBCnt, softCntCmp, |
| 446 | hfuzz->linux.hwCnts.cpuInstrCnt, hfuzz->linux.hwCnts.cpuBranchCnt, |
| 447 | hfuzz->linux.hwCnts.softCntEdge, hfuzz->linux.hwCnts.softCntPc, |
| 448 | hfuzz->linux.hwCnts.bbCnt, hfuzz->linux.hwCnts.softCntCmp); |
Jagger | 395df02 | 2016-08-21 01:13:25 +0200 | [diff] [blame] | 449 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 450 | fuzz_addFileToFileQ(hfuzz, run); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 451 | } |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 454 | static void fuzz_sanCovFeedback(honggfuzz_t* hfuzz, run_t* run) |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 455 | { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 456 | if (hfuzz->skipFeedbackOnTimeout && run->tmOutSignaled) { |
Robert Swiecki | 53ec9e4 | 2017-02-15 20:34:27 +0100 | [diff] [blame] | 457 | return; |
| 458 | } |
| 459 | |
Robert Swiecki | 0b56611 | 2017-10-17 17:39:07 +0200 | [diff] [blame] | 460 | LOG_D("File size (Best/New): %zu, SanCov feedback (bb,dso): Best: [%" PRIu64 ",%" PRIu64 |
| 461 | "] / New: [%" PRIu64 ",%" PRIu64 "], newBBs:%" PRIu64, |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 462 | run->dynamicFileSz, hfuzz->sanCovCnts.hitBBCnt, hfuzz->sanCovCnts.iDsoCnt, |
| 463 | run->sanCovCnts.hitBBCnt, run->sanCovCnts.iDsoCnt, run->sanCovCnts.newBBCnt); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 464 | |
Robert Swiecki | 37498fd | 2017-03-12 21:12:54 +0100 | [diff] [blame] | 465 | MX_SCOPED_LOCK(&hfuzz->feedback_mutex); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 466 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 467 | int64_t diff0 = hfuzz->linux.hwCnts.cpuInstrCnt - run->linux.hwCnts.cpuInstrCnt; |
| 468 | int64_t diff1 = hfuzz->linux.hwCnts.cpuBranchCnt - run->linux.hwCnts.cpuBranchCnt; |
Jagger | d573837 | 2016-08-17 20:12:15 +0200 | [diff] [blame] | 469 | |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 470 | /* |
| 471 | * Keep mutated seed if: |
Robert Swiecki | 142f941 | 2016-03-14 19:22:01 +0100 | [diff] [blame] | 472 | * a) Newly discovered (not met before) BBs |
| 473 | * b) More instrumented DSOs loaded |
Robert Swiecki | 23ec02a | 2016-01-19 18:47:45 +0100 | [diff] [blame] | 474 | * |
Anestis Bechtsoudis | b78cf60 | 2016-01-07 13:10:50 +0200 | [diff] [blame] | 475 | * TODO: (a) method can significantly assist to further improvements in interesting areas |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 476 | * discovery if combined with seeds pool/queue support. If a runtime queue is maintained |
| 477 | * more interesting seeds can be saved between runs instead of instantly discarded |
| 478 | * based on current absolute elitism (only one mutated seed is promoted). |
| 479 | */ |
Jagger | d573837 | 2016-08-17 20:12:15 +0200 | [diff] [blame] | 480 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 481 | bool newCov |
| 482 | = (run->sanCovCnts.newBBCnt > 0 || hfuzz->sanCovCnts.iDsoCnt < run->sanCovCnts.iDsoCnt); |
Jagger | d573837 | 2016-08-17 20:12:15 +0200 | [diff] [blame] | 483 | |
Robert Swiecki | d158aac | 2016-11-01 23:14:12 +0100 | [diff] [blame] | 484 | if (newCov || (diff0 < 0 || diff1 < 0)) { |
Robert Swiecki | 0b56611 | 2017-10-17 17:39:07 +0200 | [diff] [blame] | 485 | LOG_I("SanCov Update: fsize:%zu, newBBs:%" PRIu64 ", (Cur,New): %" PRIu64 "/%" PRIu64 |
| 486 | ",%" PRIu64 "/%" PRIu64, |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 487 | run->dynamicFileSz, run->sanCovCnts.newBBCnt, hfuzz->sanCovCnts.hitBBCnt, |
| 488 | hfuzz->sanCovCnts.iDsoCnt, run->sanCovCnts.hitBBCnt, run->sanCovCnts.iDsoCnt); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 489 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 490 | hfuzz->sanCovCnts.hitBBCnt += run->sanCovCnts.newBBCnt; |
| 491 | hfuzz->sanCovCnts.dsoCnt = run->sanCovCnts.dsoCnt; |
| 492 | hfuzz->sanCovCnts.iDsoCnt = run->sanCovCnts.iDsoCnt; |
| 493 | hfuzz->sanCovCnts.crashesCnt += run->sanCovCnts.crashesCnt; |
| 494 | hfuzz->sanCovCnts.newBBCnt = run->sanCovCnts.newBBCnt; |
Anestis Bechtsoudis | b78cf60 | 2016-01-07 13:10:50 +0200 | [diff] [blame] | 495 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 496 | if (hfuzz->sanCovCnts.totalBBCnt < run->sanCovCnts.totalBBCnt) { |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 497 | /* Keep only the max value (for dlopen cases) to measure total target coverage */ |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 498 | hfuzz->sanCovCnts.totalBBCnt = run->sanCovCnts.totalBBCnt; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 499 | } |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 500 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 501 | hfuzz->linux.hwCnts.cpuInstrCnt = run->linux.hwCnts.cpuInstrCnt; |
| 502 | hfuzz->linux.hwCnts.cpuBranchCnt = run->linux.hwCnts.cpuBranchCnt; |
Jagger | d573837 | 2016-08-17 20:12:15 +0200 | [diff] [blame] | 503 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 504 | fuzz_addFileToFileQ(hfuzz, run); |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 505 | } |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 506 | } |
| 507 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 508 | static void fuzz_fuzzLoop(honggfuzz_t* hfuzz, run_t* run) |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 509 | { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 510 | run->pid = 0; |
| 511 | run->timeStartedMillis = util_timeNowMillis(); |
| 512 | run->state = fuzz_getState(hfuzz); |
| 513 | run->crashFileName[0] = '\0'; |
| 514 | run->pc = 0ULL; |
| 515 | run->backtrace = 0ULL; |
| 516 | run->access = 0ULL; |
| 517 | run->exception = 0; |
| 518 | run->report[0] = '\0'; |
| 519 | run->mainWorker = true; |
| 520 | run->origFileName = "DYNAMIC"; |
| 521 | run->mutationsPerRun = hfuzz->mutationsPerRun; |
| 522 | run->dynamicFileSz = 0; |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 523 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 524 | run->sanCovCnts.hitBBCnt = 0ULL; |
| 525 | run->sanCovCnts.totalBBCnt = 0ULL; |
| 526 | run->sanCovCnts.dsoCnt = 0ULL; |
| 527 | run->sanCovCnts.newBBCnt = 0ULL; |
| 528 | run->sanCovCnts.crashesCnt = 0ULL; |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 529 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 530 | run->linux.hwCnts.cpuInstrCnt = 0ULL; |
| 531 | run->linux.hwCnts.cpuBranchCnt = 0ULL; |
| 532 | run->linux.hwCnts.bbCnt = 0ULL; |
| 533 | run->linux.hwCnts.newBBCnt = 0ULL; |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 534 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 535 | if (run->state == _HF_STATE_DYNAMIC_PRE) { |
| 536 | run->mutationsPerRun = 0U; |
| 537 | if (fuzz_prepareFile(hfuzz, run, false /* rewind */) == false) { |
Robert Swiecki | cc88f93 | 2017-07-07 00:13:00 +0200 | [diff] [blame] | 538 | fuzz_setState(hfuzz, _HF_STATE_DYNAMIC_MAIN); |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 539 | run->state = fuzz_getState(hfuzz); |
Robert Swiecki | ee266ac | 2016-10-03 02:25:59 +0200 | [diff] [blame] | 540 | } |
Robert Swiecki | 92a3136 | 2017-02-24 16:21:40 +0100 | [diff] [blame] | 541 | } |
Robert Swiecki | 1a99473 | 2017-05-31 18:32:21 +0200 | [diff] [blame] | 542 | |
| 543 | if (ATOMIC_GET(hfuzz->terminating) == true) { |
| 544 | return; |
| 545 | } |
| 546 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 547 | if (run->state == _HF_STATE_DYNAMIC_MAIN) { |
Robert Swiecki | cc88f93 | 2017-07-07 00:13:00 +0200 | [diff] [blame] | 548 | if (hfuzz->externalCommand) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 549 | if (!fuzz_prepareFileExternally(hfuzz, run)) { |
Robert Swiecki | cc88f93 | 2017-07-07 00:13:00 +0200 | [diff] [blame] | 550 | LOG_F("fuzz_prepareFileExternally() failed"); |
| 551 | } |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 552 | } else if (!fuzz_prepareFileDynamically(hfuzz, run)) { |
Jagger | 1d8e0c6 | 2016-04-02 02:22:40 +0200 | [diff] [blame] | 553 | LOG_F("fuzz_prepareFileDynamically() failed"); |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 554 | } |
Robert Swiecki | cc88f93 | 2017-07-07 00:13:00 +0200 | [diff] [blame] | 555 | |
| 556 | if (hfuzz->postExternalCommand) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 557 | if (!fuzz_postProcessFile(hfuzz, run)) { |
Robert Swiecki | cc88f93 | 2017-07-07 00:13:00 +0200 | [diff] [blame] | 558 | LOG_F("fuzz_postProcessFile() failed"); |
| 559 | } |
| 560 | } |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 561 | } |
Robert Swiecki | cc88f93 | 2017-07-07 00:13:00 +0200 | [diff] [blame] | 562 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 563 | if (run->state == _HF_STATE_STATIC) { |
Robert Swiecki | 92a3136 | 2017-02-24 16:21:40 +0100 | [diff] [blame] | 564 | if (hfuzz->externalCommand) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 565 | if (!fuzz_prepareFileExternally(hfuzz, run)) { |
Robert Swiecki | 92a3136 | 2017-02-24 16:21:40 +0100 | [diff] [blame] | 566 | LOG_F("fuzz_prepareFileExternally() failed"); |
| 567 | } |
| 568 | } else { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 569 | if (!fuzz_prepareFile(hfuzz, run, true /* rewind */)) { |
Robert Swiecki | 92a3136 | 2017-02-24 16:21:40 +0100 | [diff] [blame] | 570 | LOG_F("fuzz_prepareFile() failed"); |
| 571 | } |
| 572 | } |
Robert Swiecki | ab2b8c9 | 2017-07-07 12:55:43 +0200 | [diff] [blame] | 573 | |
Robert Swiecki | 92a3136 | 2017-02-24 16:21:40 +0100 | [diff] [blame] | 574 | if (hfuzz->postExternalCommand != NULL) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 575 | if (!fuzz_postProcessFile(hfuzz, run)) { |
Robert Swiecki | 92a3136 | 2017-02-24 16:21:40 +0100 | [diff] [blame] | 576 | LOG_F("fuzz_postProcessFile() failed"); |
| 577 | } |
Robert Swiecki | ee266ac | 2016-10-03 02:25:59 +0200 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 581 | if (subproc_Run(hfuzz, run) == false) { |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 582 | LOG_F("subproc_Run()"); |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 583 | } |
| 584 | |
Jagger | a34b302 | 2016-04-02 02:20:40 +0200 | [diff] [blame] | 585 | if (hfuzz->persistent == false) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 586 | unlink(run->fileName); |
Jagger | a34b302 | 2016-04-02 02:20:40 +0200 | [diff] [blame] | 587 | } |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 588 | |
Robert Swiecki | 53ec9e4 | 2017-02-15 20:34:27 +0100 | [diff] [blame] | 589 | if (hfuzz->dynFileMethod != _HF_DYNFILE_NONE) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 590 | fuzz_perfFeedback(hfuzz, run); |
Robert Swiecki | 53ec9e4 | 2017-02-15 20:34:27 +0100 | [diff] [blame] | 591 | } |
| 592 | if (hfuzz->useSanCov) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 593 | fuzz_sanCovFeedback(hfuzz, run); |
Robert Swiecki | 0f937af | 2016-03-30 18:19:16 +0200 | [diff] [blame] | 594 | } |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 595 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 596 | if (hfuzz->useVerifier && (run->crashFileName[0] != 0) && run->backtrace) { |
| 597 | if (!fuzz_runVerifier(hfuzz, run)) { |
| 598 | LOG_I("Failed to verify %s", run->crashFileName); |
Anestis Bechtsoudis | 3085f22 | 2015-09-27 18:35:26 +0300 | [diff] [blame] | 599 | } |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 600 | } |
| 601 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 602 | report_Report(hfuzz, run->report); |
Jagger | 190f0dc | 2015-09-05 16:41:22 +0200 | [diff] [blame] | 603 | } |
| 604 | |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 605 | static void* fuzz_threadNew(void* arg) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 606 | { |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 607 | honggfuzz_t* hfuzz = (honggfuzz_t*)arg; |
Robert Swiecki | 0ec9811 | 2017-02-03 02:08:14 +0100 | [diff] [blame] | 608 | unsigned int fuzzNo = ATOMIC_POST_INC(hfuzz->threadsActiveCnt); |
| 609 | LOG_I("Launched new fuzzing thread, no. #%" PRId32, fuzzNo); |
Anestis Bechtsoudis | 02b99be | 2015-12-27 11:53:01 +0200 | [diff] [blame] | 610 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 611 | run_t run = { |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 612 | .pid = 0, |
| 613 | .persistentPid = 0, |
Robert Swiecki | bf8f8cc | 2017-11-09 00:42:50 +0100 | [diff] [blame] | 614 | .dynfileqCurrent = NULL, |
Robert Swiecki | 147d467 | 2017-01-29 02:49:06 +0100 | [diff] [blame] | 615 | .dynamicFile = util_Calloc(hfuzz->maxFileSz), |
Jagger | fa3544a | 2016-08-30 02:55:55 +0200 | [diff] [blame] | 616 | .fuzzNo = fuzzNo, |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 617 | .persistentSock = -1, |
Robert Swiecki | 013bc9c | 2016-12-12 17:31:06 +0100 | [diff] [blame] | 618 | .tmOutSignaled = false, |
Robert Swiecki | 47f5de1 | 2017-03-28 03:25:55 +0200 | [diff] [blame] | 619 | .fileName = "[UNSET]", |
Robert Swiecki | 12800cd | 2016-03-31 15:38:10 +0200 | [diff] [blame] | 620 | |
Robert Swiecki | b692f28 | 2016-08-25 16:21:08 +0200 | [diff] [blame] | 621 | .linux.attachedPid = 0, |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 622 | }; |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 623 | defer { free(run.dynamicFile); }; |
| 624 | fuzz_getFileName(hfuzz, &run); |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 625 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 626 | if (arch_archThreadInit(hfuzz, &run) == false) { |
Robert Swiecki | 0f937af | 2016-03-30 18:19:16 +0200 | [diff] [blame] | 627 | LOG_F("Could not initialize the thread"); |
| 628 | } |
| 629 | |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 630 | for (;;) { |
Anestis Bechtsoudis | 46ea10e | 2015-11-07 18:16:25 +0200 | [diff] [blame] | 631 | /* Check if dry run mode with verifier enabled */ |
Robert Swiecki | 10e9356 | 2017-11-04 00:57:47 +0100 | [diff] [blame] | 632 | if (hfuzz->mutationsPerRun == 0U && hfuzz->useVerifier) { |
Jagger | d34417d | 2016-03-16 01:26:54 +0100 | [diff] [blame] | 633 | if (ATOMIC_POST_INC(hfuzz->mutationsCnt) >= hfuzz->fileCnt) { |
| 634 | ATOMIC_POST_INC(hfuzz->threadsFinished); |
Robert Swiecki | 069b48f | 2017-05-31 01:00:08 +0200 | [diff] [blame] | 635 | break; |
Anestis Bechtsoudis | 46ea10e | 2015-11-07 18:16:25 +0200 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | /* Check for max iterations limit if set */ |
Jagger | d34417d | 2016-03-16 01:26:54 +0100 | [diff] [blame] | 639 | else if ((ATOMIC_POST_INC(hfuzz->mutationsCnt) >= hfuzz->mutationsMax) |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 640 | && hfuzz->mutationsMax) { |
Jagger | d34417d | 2016-03-16 01:26:54 +0100 | [diff] [blame] | 641 | ATOMIC_POST_INC(hfuzz->threadsFinished); |
Robert Swiecki | 069b48f | 2017-05-31 01:00:08 +0200 | [diff] [blame] | 642 | break; |
Robert Swiecki | 8d01b01 | 2017-02-19 15:48:11 +0100 | [diff] [blame] | 643 | } |
| 644 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 645 | fuzz_fuzzLoop(hfuzz, &run); |
Robert Swiecki | 069b48f | 2017-05-31 01:00:08 +0200 | [diff] [blame] | 646 | |
| 647 | if (ATOMIC_GET(hfuzz->terminating) == true) { |
| 648 | break; |
| 649 | } |
| 650 | |
| 651 | if (hfuzz->exitUponCrash && ATOMIC_GET(hfuzz->crashesCnt) > 0) { |
| 652 | LOG_I("Seen a crash. Terminating all fuzzing threads"); |
| 653 | ATOMIC_SET(hfuzz->terminating, true); |
| 654 | break; |
| 655 | } |
robert.swiecki@gmail.com | d4dd4df | 2015-02-18 00:50:12 +0000 | [diff] [blame] | 656 | } |
Robert Swiecki | 069b48f | 2017-05-31 01:00:08 +0200 | [diff] [blame] | 657 | |
| 658 | LOG_I("Terminating thread no. #%" PRId32, fuzzNo); |
| 659 | ATOMIC_POST_INC(hfuzz->threadsFinished); |
Robert Swiecki | aa62395 | 2017-06-01 02:50:41 +0200 | [diff] [blame] | 660 | pthread_kill(fuzz_mainThread, SIGALRM); |
Robert Swiecki | 069b48f | 2017-05-31 01:00:08 +0200 | [diff] [blame] | 661 | return NULL; |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 664 | static void fuzz_runThread(honggfuzz_t* hfuzz, pthread_t* thread, void* (*thread_func)(void*)) |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 665 | { |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 666 | pthread_attr_t attr; |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 667 | |
robert.swiecki@gmail.com | 6f5c239 | 2015-02-16 18:13:09 +0000 | [diff] [blame] | 668 | pthread_attr_init(&attr); |
Robert Swiecki | 33fb284 | 2017-02-19 05:39:50 +0100 | [diff] [blame] | 669 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
robert.swiecki@gmail.com | 441089a | 2015-02-23 13:14:07 +0000 | [diff] [blame] | 670 | pthread_attr_setstacksize(&attr, _HF_PTHREAD_STACKSIZE); |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 671 | pthread_attr_setguardsize(&attr, (size_t)sysconf(_SC_PAGESIZE)); |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 672 | |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 673 | if (pthread_create(thread, &attr, thread_func, (void*)hfuzz) < 0) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 674 | PLOG_F("Couldn't create a new thread"); |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Robert Swiecki | 33fb284 | 2017-02-19 05:39:50 +0100 | [diff] [blame] | 677 | pthread_attr_destroy(&attr); |
| 678 | |
robert.swiecki@gmail.com | c844314 | 2015-02-13 13:46:40 +0000 | [diff] [blame] | 679 | return; |
| 680 | } |
| 681 | |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 682 | void fuzz_threadsStart(honggfuzz_t* hfuzz, pthread_t* threads) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 683 | { |
Jagger | 6113c43 | 2015-09-24 05:00:28 +0200 | [diff] [blame] | 684 | fuzz_mainThread = pthread_self(); |
| 685 | |
robert.swiecki@gmail.com | 956276a | 2015-04-16 16:51:52 +0000 | [diff] [blame] | 686 | if (!arch_archInit(hfuzz)) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 687 | LOG_F("Couldn't prepare arch for fuzzing"); |
robert.swiecki@gmail.com | ef829fa | 2011-06-22 13:51:57 +0000 | [diff] [blame] | 688 | } |
Anestis Bechtsoudis | e5f09f8 | 2016-12-27 16:06:05 +0200 | [diff] [blame] | 689 | if (!sanitizers_Init(hfuzz)) { |
| 690 | LOG_F("Couldn't prepare sanitizer options"); |
| 691 | } |
Jagger | 0026560 | 2016-03-10 02:36:27 +0100 | [diff] [blame] | 692 | if (!sancov_Init(hfuzz)) { |
| 693 | LOG_F("Couldn't prepare sancov options"); |
| 694 | } |
robert.swiecki@gmail.com | ef829fa | 2011-06-22 13:51:57 +0000 | [diff] [blame] | 695 | |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 696 | if (hfuzz->useSanCov || hfuzz->dynFileMethod != _HF_DYNFILE_NONE) { |
Jagger | ebe1929 | 2016-03-17 00:11:44 +0100 | [diff] [blame] | 697 | fuzz_setState(hfuzz, _HF_STATE_DYNAMIC_PRE); |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 698 | } else { |
Jagger | ebe1929 | 2016-03-17 00:11:44 +0100 | [diff] [blame] | 699 | fuzz_setState(hfuzz, _HF_STATE_STATIC); |
Robert Swiecki | a96d78d | 2016-03-14 16:50:50 +0100 | [diff] [blame] | 700 | } |
| 701 | |
Jagger | ea39a8f | 2015-09-05 00:57:22 +0200 | [diff] [blame] | 702 | for (size_t i = 0; i < hfuzz->threadsMax; i++) { |
Robert Swiecki | 33fb284 | 2017-02-19 05:39:50 +0100 | [diff] [blame] | 703 | fuzz_runThread(hfuzz, &threads[i], fuzz_threadNew); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 704 | } |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 705 | } |
Robert Swiecki | 33fb284 | 2017-02-19 05:39:50 +0100 | [diff] [blame] | 706 | |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 707 | void fuzz_threadsStop(honggfuzz_t* hfuzz, pthread_t* threads) |
Robert Swiecki | 33fb284 | 2017-02-19 05:39:50 +0100 | [diff] [blame] | 708 | { |
| 709 | for (size_t i = 0; i < hfuzz->threadsMax; i++) { |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 710 | void* retval; |
Robert Swiecki | 33fb284 | 2017-02-19 05:39:50 +0100 | [diff] [blame] | 711 | if (pthread_join(threads[i], &retval) != 0) { |
| 712 | PLOG_F("Couldn't pthread_join() thread: %zu", i); |
| 713 | } |
| 714 | } |
| 715 | LOG_I("All threads done"); |
| 716 | } |