robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | |
| 3 | honggfuzz - fuzzing routines |
| 4 | ----------------------------------------- |
| 5 | |
| 6 | Author: Robert Swiecki <swiecki@google.com> |
groebert@google.com | b857deb | 2010-10-21 19:09:29 +0000 | [diff] [blame] | 7 | Felix Gröbert <groebert@google.com> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 8 | |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 9 | Copyright 2010-2015 by Google Inc. All Rights Reserved. |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 10 | |
| 11 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 | you may not use this file except in compliance with the License. |
| 13 | You may obtain a copy of the License at |
| 14 | |
| 15 | http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | |
| 17 | Unless required by applicable law or agreed to in writing, software |
| 18 | distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | See the License for the specific language governing permissions and |
| 21 | limitations under the License. |
| 22 | |
| 23 | */ |
| 24 | |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 25 | #include "common.h" |
| 26 | #include "fuzz.h" |
| 27 | |
| 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 30 | #include <pthread.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 31 | #include <signal.h> |
| 32 | #include <stddef.h> |
| 33 | #include <stdint.h> |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 37 | #include <sys/mman.h> |
| 38 | #include <sys/param.h> |
| 39 | #include <sys/stat.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 40 | #include <sys/time.h> |
| 41 | #include <sys/types.h> |
| 42 | #include <sys/wait.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 43 | #include <time.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 44 | #include <unistd.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 45 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 46 | #include "log.h" |
| 47 | #include "arch.h" |
| 48 | #include "util.h" |
| 49 | #include "files.h" |
| 50 | |
| 51 | static void fuzz_mangleContent(honggfuzz_t * hfuzz, uint8_t * buf, off_t fileSz) |
| 52 | { |
robert.swiecki | cccbf0c | 2011-01-28 11:58:04 +0000 | [diff] [blame] | 53 | /* |
| 54 | * Just copy the file if "-r 0" |
| 55 | */ |
robert.swiecki | ba51263 | 2011-01-28 11:57:26 +0000 | [diff] [blame] | 56 | if (hfuzz->flipRate == 0.0) { |
| 57 | return; |
| 58 | } |
| 59 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 60 | uint64_t changesCnt = fileSz * hfuzz->flipRate; |
| 61 | |
| 62 | if (hfuzz->flipMode == 'b') { |
| 63 | changesCnt *= 8UL; |
| 64 | } |
| 65 | |
| 66 | changesCnt = util_rndGet(1, changesCnt); |
| 67 | |
| 68 | for (uint64_t x = 0; x < changesCnt; x++) { |
| 69 | off_t pos = util_rndGet(0, fileSz - 1); |
| 70 | |
| 71 | if (hfuzz->flipMode == 'b') { |
| 72 | buf[pos] ^= (1 << util_rndGet(0, 7)); |
| 73 | } else { |
| 74 | buf[pos] = (uint8_t) util_rndGet(0, 255); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static void fuzz_getFileName(honggfuzz_t * hfuzz, char *fileName) |
| 80 | { |
robert.swiecki | ba51263 | 2011-01-28 11:57:26 +0000 | [diff] [blame] | 81 | struct timeval tv; |
| 82 | gettimeofday(&tv, NULL); |
| 83 | |
| 84 | snprintf(fileName, PATH_MAX, ".honggfuzz.%d.%lu.%lu.%lu.%s", (int)getpid(), |
| 85 | (unsigned long int)tv.tv_sec, (unsigned long int)tv.tv_usec, |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 86 | (unsigned long int)util_rndGet(0, 1 << 30), hfuzz->fileExtn); |
| 87 | |
| 88 | return; |
| 89 | } |
| 90 | |
robert.swiecki@gmail.com | d7aed31 | 2015-02-03 21:26:37 +0000 | [diff] [blame] | 91 | static void fuzz_appendOrTrunc(int fd, off_t fileSz) |
| 92 | { |
| 93 | const int chance_one_in_x = 10; |
| 94 | if (util_rndGet(1, chance_one_in_x) != 1) { |
| 95 | return; |
| 96 | } |
| 97 | |
robert.swiecki@gmail.com | 37f194e | 2015-02-03 21:35:07 +0000 | [diff] [blame] | 98 | off_t maxSz = 2 * fileSz; |
| 99 | if (fileSz > (1024 * 1024 * 20)) { |
| 100 | maxSz = fileSz + (fileSz / 4); |
| 101 | } |
| 102 | if (fileSz > (1024 * 1024 * 100)) { |
| 103 | maxSz = fileSz; |
| 104 | } |
| 105 | |
| 106 | off_t newSz = util_rndGet(1, maxSz); |
robert.swiecki@gmail.com | d7aed31 | 2015-02-03 21:26:37 +0000 | [diff] [blame] | 107 | if (ftruncate(fd, newSz) == -1) { |
| 108 | LOGMSG_P(l_WARN, "Couldn't truncate file from '%ld' to '%ld' bytes", (long)fileSz, |
| 109 | (long)newSz); |
| 110 | return; |
| 111 | } |
| 112 | } |
| 113 | |
groebert@google.com | 1c7e3b0 | 2013-06-19 09:27:38 +0000 | [diff] [blame] | 114 | static bool fuzz_prepareFile(honggfuzz_t * hfuzz, char *fileName, int rnd_index) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 115 | { |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 116 | off_t fileSz; |
| 117 | int srcfd; |
| 118 | |
| 119 | uint8_t *buf = files_mapFileToRead(hfuzz->files[rnd_index], &fileSz, &srcfd); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 120 | if (buf == NULL) { |
robert.swiecki | 56b8801 | 2010-12-31 14:04:13 +0000 | [diff] [blame] | 121 | LOGMSG(l_ERROR, "Couldn't open and map '%s' in R/O mode", hfuzz->files[rnd_index]); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | |
| 125 | LOGMSG(l_DEBUG, "Mmaped '%s' in R/O mode, size: %d", hfuzz->files[rnd_index], fileSz); |
| 126 | |
| 127 | int dstfd = open(fileName, O_CREAT | O_EXCL | O_RDWR, 0644); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 128 | if (dstfd == -1) { |
robert.swiecki | 3d505e2 | 2010-10-14 01:17:17 +0000 | [diff] [blame] | 129 | LOGMSG_P(l_ERROR, "Couldn't create a temporary file '%s' in the current directory", |
| 130 | fileName); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 131 | munmap(buf, fileSz); |
| 132 | close(srcfd); |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | fuzz_mangleContent(hfuzz, buf, fileSz); |
| 137 | |
| 138 | if (!files_writeToFd(dstfd, buf, fileSz)) { |
| 139 | munmap(buf, fileSz); |
| 140 | close(srcfd); |
| 141 | close(dstfd); |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | munmap(buf, fileSz); |
robert.swiecki@gmail.com | d7aed31 | 2015-02-03 21:26:37 +0000 | [diff] [blame] | 146 | |
| 147 | fuzz_appendOrTrunc(dstfd, fileSz); |
| 148 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 149 | close(srcfd); |
| 150 | close(dstfd); |
| 151 | return true; |
| 152 | } |
| 153 | |
groebert@google.com | 1c7e3b0 | 2013-06-19 09:27:38 +0000 | [diff] [blame] | 154 | static bool fuzz_prepareFileExternally(honggfuzz_t * hfuzz, char *fileName, int rnd_index) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 155 | { |
robert.swiecki | 3d505e2 | 2010-10-14 01:17:17 +0000 | [diff] [blame] | 156 | off_t fileSz; |
| 157 | int srcfd; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 158 | |
robert.swiecki | 3d505e2 | 2010-10-14 01:17:17 +0000 | [diff] [blame] | 159 | int dstfd = open(fileName, O_CREAT | O_EXCL | O_RDWR, 0644); |
| 160 | if (dstfd == -1) { |
| 161 | LOGMSG_P(l_ERROR, "Couldn't create a temporary file '%s' in the current directory", |
| 162 | fileName); |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 163 | return false; |
| 164 | } |
| 165 | |
| 166 | LOGMSG(l_DEBUG, "Created '%f' as an input file", fileName); |
| 167 | |
| 168 | if (hfuzz->inputFile) { |
| 169 | uint8_t *buf = files_mapFileToRead(hfuzz->files[rnd_index], &fileSz, &srcfd); |
| 170 | if (buf == NULL) { |
| 171 | LOGMSG(l_ERROR, "Couldn't open and map '%s' in R/O mode", hfuzz->files[rnd_index]); |
| 172 | close(dstfd); |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | LOGMSG(l_DEBUG, "Mmaped '%s' in R/O mode, size: %d", hfuzz->files[rnd_index], fileSz); |
| 177 | |
| 178 | bool ret = files_writeToFd(dstfd, buf, fileSz); |
robert.swiecki | 3d505e2 | 2010-10-14 01:17:17 +0000 | [diff] [blame] | 179 | munmap(buf, fileSz); |
| 180 | close(srcfd); |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 181 | |
| 182 | if (!ret) { |
| 183 | close(dstfd); |
| 184 | return false; |
| 185 | } |
robert.swiecki | 3d505e2 | 2010-10-14 01:17:17 +0000 | [diff] [blame] | 186 | } |
| 187 | |
robert.swiecki | 3d505e2 | 2010-10-14 01:17:17 +0000 | [diff] [blame] | 188 | close(dstfd); |
| 189 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 190 | pid_t pid = fork(); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 191 | if (pid == -1) { |
| 192 | LOGMSG_P(l_ERROR, "Couldn't fork"); |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | if (!pid) { |
| 197 | /* |
| 198 | * child does the external file modifications |
| 199 | */ |
robert.swiecki | 3d505e2 | 2010-10-14 01:17:17 +0000 | [diff] [blame] | 200 | execl(hfuzz->externalCommand, hfuzz->externalCommand, fileName, NULL); |
groebert@google.com | b857deb | 2010-10-21 19:09:29 +0000 | [diff] [blame] | 201 | LOGMSG_P(l_FATAL, "Couldn't execute '%s %s'", hfuzz->externalCommand, fileName); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 202 | return false; |
| 203 | } else { |
| 204 | /* |
| 205 | * parent waits until child is done fuzzing the input file |
| 206 | */ |
| 207 | |
| 208 | int childStatus; |
| 209 | pid_t terminatedPid; |
| 210 | do { |
| 211 | terminatedPid = wait(&childStatus); |
| 212 | } while (terminatedPid != pid); |
| 213 | |
| 214 | if (WIFEXITED(childStatus)) { |
| 215 | LOGMSG(l_DEBUG, "External command exited with status %d", WEXITSTATUS(childStatus)); |
| 216 | return true; |
| 217 | } else if (WIFSIGNALED(childStatus)) { |
robert.swiecki | 3d505e2 | 2010-10-14 01:17:17 +0000 | [diff] [blame] | 218 | LOGMSG(l_ERROR, "External command terminated with signal %d", WTERMSIG(childStatus)); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 219 | return false; |
| 220 | } |
robert.swiecki | 3d505e2 | 2010-10-14 01:17:17 +0000 | [diff] [blame] | 221 | LOGMSG(l_FATAL, "External command terminated abnormally, status: %d", childStatus); |
| 222 | return false; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | abort(); /* NOTREACHED */ |
| 226 | } |
| 227 | |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 228 | static void *fuzz_threadCreate(void *arg) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 229 | { |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 230 | honggfuzz_t *hfuzz = (honggfuzz_t *) arg; |
| 231 | fuzzer_t fuzzer = { |
robert.swiecki@gmail.com | 1f98a16 | 2015-02-11 15:09:22 +0000 | [diff] [blame] | 232 | .pid = hfuzz->pid, |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 233 | .timeStarted = time(NULL), |
| 234 | .pc = 0ULL, |
| 235 | .backtrace = 0ULL, |
| 236 | .access = 0ULL, |
| 237 | .exception = 0, |
| 238 | }; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 239 | |
groebert@google.com | 1c7e3b0 | 2013-06-19 09:27:38 +0000 | [diff] [blame] | 240 | int rnd_index = util_rndGet(0, hfuzz->fileCnt - 1); |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 241 | strncpy(fuzzer.origFileName, files_basename(hfuzz->files[rnd_index]), PATH_MAX); |
| 242 | fuzz_getFileName(hfuzz, fuzzer.fileName); |
groebert@google.com | 1c7e3b0 | 2013-06-19 09:27:38 +0000 | [diff] [blame] | 243 | |
robert.swiecki@gmail.com | 65cfa1c | 2015-02-11 15:59:28 +0000 | [diff] [blame] | 244 | /* Maybe we're fuzzing an external process */ |
robert.swiecki@gmail.com | 1f98a16 | 2015-02-11 15:09:22 +0000 | [diff] [blame] | 245 | if (fuzzer.pid == 0) { |
robert.swiecki@gmail.com | 65cfa1c | 2015-02-11 15:59:28 +0000 | [diff] [blame] | 246 | fuzzer.pid = fork(); |
robert.swiecki@gmail.com | 1f98a16 | 2015-02-11 15:09:22 +0000 | [diff] [blame] | 247 | } |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 248 | |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 249 | if (fuzzer.pid == -1) { |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 250 | LOGMSG_P(l_FATAL, "Couldn't fork"); |
| 251 | exit(EXIT_FAILURE); |
| 252 | } |
| 253 | |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 254 | if (!fuzzer.pid) { |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 255 | /* |
| 256 | * We've forked, other pid's might have the same rnd seeds now, |
| 257 | * reinitialize it |
| 258 | */ |
| 259 | util_rndInit(); |
| 260 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 261 | if (hfuzz->externalCommand != NULL) { |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 262 | if (!fuzz_prepareFileExternally(hfuzz, fuzzer.fileName, rnd_index)) { |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 263 | exit(EXIT_FAILURE); |
| 264 | } |
| 265 | } else { |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 266 | if (!fuzz_prepareFile(hfuzz, fuzzer.fileName, rnd_index)) { |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 267 | exit(EXIT_FAILURE); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Ok, kill the parent |
| 273 | */ |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 274 | if (!arch_launchChild(hfuzz, fuzzer.fileName)) { |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 275 | LOGMSG(l_DEBUG, "Error launching child process, killing parent"); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 276 | kill(getppid(), SIGTERM); |
| 277 | exit(EXIT_FAILURE); |
| 278 | } |
| 279 | } |
| 280 | |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 281 | LOGMSG(l_INFO, "Launched new process, pid: %d, (%d/%d)", fuzzer.pid, |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 282 | hfuzz->threadsCnt, hfuzz->threadsMax); |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 283 | |
| 284 | arch_reapChild(hfuzz, &fuzzer); |
| 285 | unlink(fuzzer.fileName); |
| 286 | |
robert.swiecki@gmail.com | e763539 | 2015-02-11 16:17:49 +0000 | [diff] [blame^] | 287 | while (pthread_mutex_lock(&hfuzz->mutex)) ; |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 288 | hfuzz->threadsCnt--; |
robert.swiecki@gmail.com | e763539 | 2015-02-11 16:17:49 +0000 | [diff] [blame^] | 289 | while (pthread_mutex_unlock(&hfuzz->mutex)) ; |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 290 | |
| 291 | return NULL; |
| 292 | } |
| 293 | |
| 294 | static void fuzz_runNext(honggfuzz_t * hfuzz) |
| 295 | { |
| 296 | pthread_attr_t attr; |
| 297 | pthread_t t; |
| 298 | |
| 299 | pthread_attr_init(&attr); |
| 300 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 301 | |
robert.swiecki@gmail.com | e763539 | 2015-02-11 16:17:49 +0000 | [diff] [blame^] | 302 | while (pthread_mutex_lock(&hfuzz->mutex)) ; |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 303 | hfuzz->threadsCnt++; |
robert.swiecki@gmail.com | e763539 | 2015-02-11 16:17:49 +0000 | [diff] [blame^] | 304 | while (pthread_mutex_unlock(&hfuzz->mutex)) ; |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 305 | |
| 306 | if (pthread_create(&t, &attr, fuzz_threadCreate, (void *)hfuzz) < 0) { |
| 307 | LOGMSG_P(l_FATAL, "Couldn't create a new thread"); |
| 308 | exit(EXIT_FAILURE); |
| 309 | } |
| 310 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 311 | return; |
| 312 | } |
| 313 | |
robert.swiecki@gmail.com | 65cfa1c | 2015-02-11 15:59:28 +0000 | [diff] [blame] | 314 | static void fuzz_waitForAll(honggfuzz_t * hfuzz) |
| 315 | { |
robert.swiecki@gmail.com | e763539 | 2015-02-11 16:17:49 +0000 | [diff] [blame^] | 316 | for (;;) { |
| 317 | while (pthread_mutex_lock(&hfuzz->mutex)) ; |
| 318 | int num = hfuzz->threadsCnt; |
| 319 | while (pthread_mutex_unlock(&hfuzz->mutex)) ; |
| 320 | |
| 321 | if (num == 0) { |
| 322 | return; |
| 323 | } |
robert.swiecki@gmail.com | 65cfa1c | 2015-02-11 15:59:28 +0000 | [diff] [blame] | 324 | usleep(10000); |
| 325 | } |
| 326 | } |
| 327 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 328 | void fuzz_main(honggfuzz_t * hfuzz) |
| 329 | { |
robert.swiecki@gmail.com | e763539 | 2015-02-11 16:17:49 +0000 | [diff] [blame^] | 330 | if (pthread_mutex_init(&hfuzz->mutex, NULL) != 0) { |
| 331 | LOGMSG(l_FATAL, "Couldn't initialize mutex"); |
| 332 | exit(EXIT_FAILURE); |
| 333 | } |
| 334 | |
robert.swiecki@gmail.com | ef829fa | 2011-06-22 13:51:57 +0000 | [diff] [blame] | 335 | if (!arch_prepareParent(hfuzz)) { |
| 336 | LOGMSG(l_FATAL, "Couldn't prepare parent for fuzzing"); |
| 337 | exit(EXIT_FAILURE); |
| 338 | } |
| 339 | |
robert.swiecki@gmail.com | 65cfa1c | 2015-02-11 15:59:28 +0000 | [diff] [blame] | 340 | if (hfuzz->pid) { |
| 341 | fuzz_runNext(hfuzz); |
| 342 | fuzz_waitForAll(hfuzz); |
| 343 | exit(EXIT_SUCCESS); |
| 344 | } |
| 345 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 346 | for (;;) { |
robert.swiecki@gmail.com | e763539 | 2015-02-11 16:17:49 +0000 | [diff] [blame^] | 347 | while (pthread_mutex_lock(&hfuzz->mutex)) ; |
| 348 | int num = hfuzz->threadsCnt; |
| 349 | while (pthread_mutex_unlock(&hfuzz->mutex)) ; |
| 350 | |
| 351 | while (num < hfuzz->threadsMax) { |
robert.swiecki@gmail.com | 65cfa1c | 2015-02-11 15:59:28 +0000 | [diff] [blame] | 352 | /* We just want a limited number of mutations */ |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 353 | if (hfuzz->mutationsMax && (hfuzz->mutationsCnt >= hfuzz->mutationsMax)) { |
robert.swiecki@gmail.com | 65cfa1c | 2015-02-11 15:59:28 +0000 | [diff] [blame] | 354 | fuzz_waitForAll(hfuzz); |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 355 | LOGMSG(l_INFO, "Finished fuzzing %ld times.", hfuzz->mutationsMax); |
| 356 | exit(EXIT_SUCCESS); |
groebert@google.com | 8e2f44a | 2013-03-15 13:54:18 +0000 | [diff] [blame] | 357 | } |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 358 | |
robert.swiecki@gmail.com | 65cfa1c | 2015-02-11 15:59:28 +0000 | [diff] [blame] | 359 | hfuzz->mutationsCnt++; |
robert.swiecki@gmail.com | 882900b | 2015-02-11 13:56:22 +0000 | [diff] [blame] | 360 | fuzz_runNext(hfuzz); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 361 | } |
robert.swiecki@gmail.com | 65cfa1c | 2015-02-11 15:59:28 +0000 | [diff] [blame] | 362 | usleep(10000); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 363 | } |
| 364 | } |