blob: 693ab8a1dcf9ba28455f5939d75612159d8423d2 [file] [log] [blame]
robert.swiecki3bb518c2010-10-14 00:48:24 +00001/*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +00002 *
robert.swiecki@gmail.com90e99112015-02-15 02:05:14 +00003 * honggfuzz - fuzzing routines
4 * -----------------------------------------
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +00005 *
robert.swiecki@gmail.com8531f692015-02-17 12:25:36 +00006 * Author:
7 * Robert Swiecki <swiecki@google.com>
8 * Felix Gröbert <groebert@google.com>
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +00009 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000010 * Copyright 2010-2015 by Google Inc. All Rights Reserved.
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +000011 *
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.com772b33d2015-02-14 20:35:00 +000014 * a copy of the License at
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +000015 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000016 * http://www.apache.org/licenses/LICENSE-2.0
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +000017 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000018 * 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.com3b630b42015-02-16 10:53:53 +000023 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000024 */
robert.swiecki3bb518c2010-10-14 00:48:24 +000025
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000026#include "common.h"
27#include "fuzz.h"
28
29#include <errno.h>
30#include <fcntl.h>
robert.swiecki@gmail.com90e99112015-02-15 02:05:14 +000031#include <inttypes.h>
robert.swiecki@gmail.com882900b2015-02-11 13:56:22 +000032#include <pthread.h>
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000033#include <signal.h>
34#include <stddef.h>
35#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000039#include <sys/param.h>
Robert Swiecki1e1b3532016-03-10 18:43:28 +010040#include <sys/resource.h>
Jagger08816fd2016-03-11 01:32:38 +010041#include <sys/mman.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000042#include <sys/stat.h>
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000043#include <sys/time.h>
44#include <sys/types.h>
45#include <sys/wait.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000046#include <time.h>
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000047#include <unistd.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000048
robert.swiecki3bb518c2010-10-14 00:48:24 +000049#include "arch.h"
robert.swiecki3bb518c2010-10-14 00:48:24 +000050#include "files.h"
robert.swiecki@gmail.come7190b92015-02-14 23:05:42 +000051#include "log.h"
robert.swiecki@gmail.com36700b52015-02-22 05:03:16 +000052#include "mangle.h"
robert.swiecki@gmail.come7190b92015-02-14 23:05:42 +000053#include "report.h"
Jagger00265602016-03-10 02:36:27 +010054#include "sancov.h"
robert.swiecki@gmail.come7190b92015-02-14 23:05:42 +000055#include "util.h"
robert.swiecki3bb518c2010-10-14 00:48:24 +000056
Jagger1e05f742016-03-10 21:36:27 +010057extern char **environ;
58
Jagger6113c432015-09-24 05:00:28 +020059static pthread_t fuzz_mainThread;
60
robert.swiecki3bb518c2010-10-14 00:48:24 +000061static void fuzz_getFileName(honggfuzz_t * hfuzz, char *fileName)
62{
robert.swieckiba512632011-01-28 11:57:26 +000063 struct timeval tv;
64 gettimeofday(&tv, NULL);
65
Anestis Bechtsoudisd9680532015-09-06 17:37:05 +030066 snprintf(fileName, PATH_MAX, "%s/.honggfuzz.%d.%lu.%llx.%s", hfuzz->workDir, (int)getpid(),
robert.swiecki@gmail.com6f319912015-02-28 05:01:37 +000067 (unsigned long int)tv.tv_sec, (unsigned long long int)util_rndGet(0, 1ULL << 62),
robert.swiecki@gmail.combb5d2642015-02-25 20:00:00 +000068 hfuzz->fileExtn);
robert.swiecki3bb518c2010-10-14 00:48:24 +000069}
70
Robert Swiecki1e1b3532016-03-10 18:43:28 +010071static bool fuzz_prepareExecve(honggfuzz_t * hfuzz, const char *fileName)
72{
73 /*
74 * Set timeout (prof), real timeout (2*prof), and rlimit_cpu (2*prof)
75 */
Robert Swiecki4038c9b2016-03-31 18:58:52 +020076 if (hfuzz->persistent == false && hfuzz->tmOut) {
Robert Swiecki1e1b3532016-03-10 18:43:28 +010077 struct itimerval it;
78
79 /*
80 * The hfuzz->tmOut is real CPU usage time...
81 */
82 it.it_value.tv_sec = hfuzz->tmOut;
83 it.it_value.tv_usec = 0;
84 it.it_interval.tv_sec = 0;
85 it.it_interval.tv_usec = 0;
86 if (setitimer(ITIMER_PROF, &it, NULL) == -1) {
Robert Swieckic6a48b12016-03-11 17:41:57 +010087 PLOG_D("Couldn't set the ITIMER_PROF timer");
Robert Swiecki1e1b3532016-03-10 18:43:28 +010088 }
89
90 /*
91 * ...so, if a process sleeps, this one should
92 * trigger a signal...
93 */
94 it.it_value.tv_sec = hfuzz->tmOut;
95 it.it_value.tv_usec = 0;
96 it.it_interval.tv_sec = 0;
97 it.it_interval.tv_usec = 0;
98 if (setitimer(ITIMER_REAL, &it, NULL) == -1) {
99 PLOG_E("Couldn't set the ITIMER_REAL timer");
100 return false;
101 }
102
103 /*
104 * ..if a process sleeps and catches SIGPROF/SIGALRM
105 * rlimits won't help either. However, arch_checkTimeLimit
106 * will send a SIGKILL at tmOut + 2 seconds. That should
107 * do it :)
108 */
109 struct rlimit rl;
110
111 rl.rlim_cur = hfuzz->tmOut + 1;
112 rl.rlim_max = hfuzz->tmOut + 1;
113 if (setrlimit(RLIMIT_CPU, &rl) == -1) {
Robert Swieckic6a48b12016-03-11 17:41:57 +0100114 PLOG_D("Couldn't enforce the RLIMIT_CPU resource limit");
Robert Swiecki1e1b3532016-03-10 18:43:28 +0100115 }
116 }
117
118 /*
119 * The address space limit. If big enough - roughly the size of RAM used
120 */
121 if (hfuzz->asLimit) {
Jagger06a4bbc2016-03-10 21:19:23 +0100122 struct rlimit rl = {
Robert Swiecki1e1b3532016-03-10 18:43:28 +0100123 .rlim_cur = hfuzz->asLimit * 1024ULL * 1024ULL,
124 .rlim_max = hfuzz->asLimit * 1024ULL * 1024ULL,
125 };
Jagger06a4bbc2016-03-10 21:19:23 +0100126 if (setrlimit(RLIMIT_AS, &rl) == -1) {
Robert Swiecki1e1b3532016-03-10 18:43:28 +0100127 PLOG_D("Couldn't enforce the RLIMIT_AS resource limit, ignoring");
128 }
129 }
130
131 if (hfuzz->nullifyStdio) {
132 util_nullifyStdio();
133 }
134
135 if (hfuzz->fuzzStdin) {
136 /*
137 * Uglyyyyyy ;)
138 */
139 if (!util_redirectStdin(fileName)) {
140 return false;
141 }
142 }
143
Jagger80041fe2016-03-10 21:32:35 +0100144 if (hfuzz->clearEnv) {
Jagger1e05f742016-03-10 21:36:27 +0100145 environ = NULL;
Jagger80041fe2016-03-10 21:32:35 +0100146 }
Robert Swiecki1e1b3532016-03-10 18:43:28 +0100147 if (sancov_prepareExecve(hfuzz) == false) {
148 LOG_E("sancov_prepareExecve() failed");
149 return false;
150 }
151 for (size_t i = 0; i < ARRAYSIZE(hfuzz->envs) && hfuzz->envs[i]; i++) {
152 putenv(hfuzz->envs[i]);
153 }
154 setsid();
155
156 return true;
157}
158
Robert Swieckia96d78d2016-03-14 16:50:50 +0100159static bool fuzz_prepareFileDynamically(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
robert.swiecki@gmail.com6d6f7562015-02-17 22:18:51 +0000160{
Robert Swiecki3bfc33c2016-03-14 18:12:41 +0100161 struct dynfile_t *dynfile;
Robert Swieckia08ab1c2016-03-14 18:29:21 +0100162
163 {
Robert Swiecki76ecd5e2016-03-16 14:57:03 +0100164 MX_SCOPED_LOCK(&hfuzz->dynfileq_mutex);
Robert Swiecki4b0390a2016-03-14 18:44:05 +0100165
Jaggerb070b812016-03-15 05:31:30 +0100166 if (hfuzz->dynfileqCnt == 0) {
Jaggere4496512016-03-16 01:55:46 +0100167 LOG_F("The dynamic file corpus is empty. Apparently, the initial fuzzing of the "
168 "provided file corpus (-f) has not produced any follow-up files with positive "
169 "coverage and/or CPU counters");
Jaggerb070b812016-03-15 05:31:30 +0100170 }
171
Robert Swiecki4b0390a2016-03-14 18:44:05 +0100172 size_t i = 0U;
Robert Swieckia08ab1c2016-03-14 18:29:21 +0100173 size_t dynFilePos = util_rndGet(0, hfuzz->dynfileqCnt - 1);
174 TAILQ_FOREACH(dynfile, &hfuzz->dynfileq, pointers) {
175 if (i++ == dynFilePos) {
176 break;
177 }
Robert Swiecki3bfc33c2016-03-14 18:12:41 +0100178 }
179 }
180
181 memcpy(fuzzer->dynamicFile, dynfile->data, dynfile->size);
Robert Swieckia08ab1c2016-03-14 18:29:21 +0100182 fuzzer->dynamicFileSz = dynfile->size;
Robert Swiecki3bfc33c2016-03-14 18:12:41 +0100183
Anestis Bechtsoudis6ef24e42016-01-13 13:05:53 +0200184 mangle_Resize(hfuzz, fuzzer->dynamicFile, &fuzzer->dynamicFileSz);
Robert Swieckia96d78d2016-03-14 16:50:50 +0100185 mangle_mangleContent(hfuzz, fuzzer);
robert.swiecki@gmail.com6d6f7562015-02-17 22:18:51 +0000186
Jagger1d743382016-04-02 02:09:41 +0200187 if (hfuzz->persistent == false && files_writeBufToFile
robert.swiecki@gmail.com62e34ae2015-03-05 03:39:32 +0000188 (fuzzer->fileName, fuzzer->dynamicFile, fuzzer->dynamicFileSz,
Jagger5a3c4c32016-04-16 19:27:47 +0200189 O_WRONLY | O_CREAT | O_EXCL | O_TRUNC | O_CLOEXEC) == false) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200190 LOG_E("Couldn't write buffer to file '%s'", fuzzer->fileName);
robert.swiecki@gmail.com3b6c6292015-02-26 11:48:46 +0000191 return false;
192 }
robert.swiecki@gmail.com6d6f7562015-02-17 22:18:51 +0000193
robert.swiecki@gmail.com6d6f7562015-02-17 22:18:51 +0000194 return true;
robert.swiecki@gmail.comd7aed312015-02-03 21:26:37 +0000195}
196
robert.swiecki@gmail.com4a7a9d82015-03-01 01:25:16 +0000197static bool fuzz_prepareFile(honggfuzz_t * hfuzz, fuzzer_t * fuzzer, int rnd_index)
robert.swiecki3bb518c2010-10-14 00:48:24 +0000198{
Jagger1aa94d72016-04-02 02:37:35 +0200199 ssize_t fileSz =
robert.swiecki@gmail.com4a7a9d82015-03-01 01:25:16 +0000200 files_readFileToBufMax(hfuzz->files[rnd_index], fuzzer->dynamicFile, hfuzz->maxFileSz);
Jagger1aa94d72016-04-02 02:37:35 +0200201 if (fileSz < 0) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200202 LOG_E("Couldn't read contents of '%s'", hfuzz->files[rnd_index]);
robert.swiecki@gmail.combb5d2642015-02-25 20:00:00 +0000203 return false;
robert.swiecki3bb518c2010-10-14 00:48:24 +0000204 }
Robert Swieckia96d78d2016-03-14 16:50:50 +0100205 fuzzer->dynamicFileSz = fileSz;
robert.swiecki3bb518c2010-10-14 00:48:24 +0000206
Anestis Bechtsoudis25262b32015-11-07 22:39:47 +0200207 /* If flip rate is 0.0, early abort file mangling */
Robert Swieckia96d78d2016-03-14 16:50:50 +0100208 if (fuzzer->flipRate != 0.0L) {
209 mangle_Resize(hfuzz, fuzzer->dynamicFile, &fuzzer->dynamicFileSz);
210 mangle_mangleContent(hfuzz, fuzzer);
Anestis Bechtsoudis25262b32015-11-07 22:39:47 +0200211 }
robert.swiecki@gmail.comc070b942015-02-25 18:29:19 +0000212
Jagger1d743382016-04-02 02:09:41 +0200213 if (hfuzz->persistent == false && files_writeBufToFile
Robert Swieckia96d78d2016-03-14 16:50:50 +0100214 (fuzzer->fileName, fuzzer->dynamicFile, fuzzer->dynamicFileSz,
Jagger5a3c4c32016-04-16 19:27:47 +0200215 O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC) == false) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200216 LOG_E("Couldn't write buffer to file '%s'", fuzzer->fileName);
robert.swiecki@gmail.come7680522015-02-22 22:22:37 +0000217 return false;
218 }
219
robert.swiecki3bb518c2010-10-14 00:48:24 +0000220 return true;
221}
222
Jaggera34b3022016-04-02 02:20:40 +0200223static bool fuzz_prepareFileExternally(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
robert.swiecki3bb518c2010-10-14 00:48:24 +0000224{
Jagger5a3c4c32016-04-16 19:27:47 +0200225 int dstfd = open(fuzzer->fileName, O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, 0644);
Jaggera34b3022016-04-02 02:20:40 +0200226 if (dstfd == -1) {
227 PLOG_E("Couldn't create a temporary file '%s'", fuzzer->fileName);
228 return false;
Robert Swieckie48811e2016-03-09 18:31:19 +0100229 }
Jaggera34b3022016-04-02 02:20:40 +0200230 close(dstfd);
231
232 LOG_D("Created '%s' as an input file", fuzzer->fileName);
robert.swiecki3d505e22010-10-14 01:17:17 +0000233
Robert Swiecki12800cd2016-03-31 15:38:10 +0200234 pid_t pid = fork();
robert.swiecki3bb518c2010-10-14 00:48:24 +0000235 if (pid == -1) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200236 PLOG_E("Couldn't fork");
robert.swiecki3bb518c2010-10-14 00:48:24 +0000237 return false;
238 }
239
240 if (!pid) {
241 /*
robert.swiecki@gmail.comcdf18f92015-02-11 22:22:18 +0000242 * child performs the external file modifications
robert.swiecki3bb518c2010-10-14 00:48:24 +0000243 */
robert.swiecki@gmail.com20851202015-03-01 01:48:15 +0000244 execl(hfuzz->externalCommand, hfuzz->externalCommand, fuzzer->fileName, NULL);
Robert Swieckic8c32db2015-10-09 18:06:22 +0200245 PLOG_F("Couldn't execute '%s %s'", hfuzz->externalCommand, fuzzer->fileName);
robert.swiecki3bb518c2010-10-14 00:48:24 +0000246 return false;
robert.swiecki@gmail.com757ee192015-02-13 16:54:02 +0000247 }
robert.swiecki@gmail.com8a9df0e2015-02-13 17:08:06 +0000248
robert.swiecki@gmail.com757ee192015-02-13 16:54:02 +0000249 /*
250 * parent waits until child is done fuzzing the input file
251 */
robert.swiecki@gmail.com757ee192015-02-13 16:54:02 +0000252 int childStatus;
253 int flags = 0;
robert.swiecki@gmail.coma2291182015-02-13 16:56:27 +0000254#if defined(__WNOTHREAD)
255 flags |= __WNOTHREAD;
robert.swiecki@gmail.com8a9df0e2015-02-13 17:08:06 +0000256#endif /* defined(__WNOTHREAD) */
robert.swiecki@gmail.com757ee192015-02-13 16:54:02 +0000257 while (wait4(pid, &childStatus, flags, NULL) != pid) ;
robert.swiecki@gmail.com757ee192015-02-13 16:54:02 +0000258 if (WIFSIGNALED(childStatus)) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200259 LOG_E("External command terminated with signal %d", WTERMSIG(childStatus));
robert.swiecki3d505e22010-10-14 01:17:17 +0000260 return false;
robert.swiecki3bb518c2010-10-14 00:48:24 +0000261 }
Jagger1d743382016-04-02 02:09:41 +0200262 if (!WIFEXITED(childStatus)) {
263 LOG_F("External command terminated abnormally, status: %d", childStatus);
264 return false;
265 }
266 LOG_D("External command exited with status %d", WEXITSTATUS(childStatus));
267
268 ssize_t rsz = files_readFileToBufMax(fuzzer->fileName, fuzzer->dynamicFile, hfuzz->maxFileSz);
Jagger1aa94d72016-04-02 02:37:35 +0200269 if (rsz < 0) {
Jagger1d743382016-04-02 02:09:41 +0200270 LOG_W("Couldn't read back '%s' to the buffer", fuzzer->fileName);
271 return false;
272 }
273 fuzzer->dynamicFileSz = rsz;
274
275 if (hfuzz->persistent) {
276 unlink(fuzzer->fileName);
277 }
278
279 return true;
robert.swiecki3bb518c2010-10-14 00:48:24 +0000280}
281
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300282static bool fuzz_runVerifier(honggfuzz_t * hfuzz, fuzzer_t * crashedFuzzer)
283{
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300284 int crashFd = -1;
285 uint8_t *crashBuf = NULL;
286 off_t crashFileSz = 0;
287
288 crashBuf = files_mapFile(crashedFuzzer->crashFileName, &crashFileSz, &crashFd, false);
289 if (crashBuf == NULL) {
Anestis Bechtsoudis0cde66f2015-10-11 19:37:11 -0700290 LOG_E("Couldn't open and map '%s' in R/O mode", crashedFuzzer->crashFileName);
Robert Swieckie48811e2016-03-09 18:31:19 +0100291 return false;
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300292 }
Jagger4fe18692016-04-22 23:15:07 +0200293 defer {
294 munmap(crashBuf, crashFileSz);
Jagger4fe18692016-04-22 23:15:07 +0200295 close(crashFd);
296 };
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300297
Anestis Bechtsoudis0cde66f2015-10-11 19:37:11 -0700298 LOG_I("Launching verifier for %" PRIx64 " hash", crashedFuzzer->backtrace);
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300299 for (int i = 0; i < _HF_VERIFIER_ITER; i++) {
300 fuzzer_t vFuzzer = {
301 .pid = 0,
Anestis Bechtsoudis92b97042016-04-27 12:34:19 +0300302 .persistentPid = 0,
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300303 .timeStartedMillis = util_timeNowMillis(),
Anestis Bechtsoudisecb0a662015-09-27 18:19:46 +0300304 .crashFileName = {0},
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300305 .pc = 0ULL,
306 .backtrace = 0ULL,
307 .access = 0ULL,
308 .exception = 0,
309 .dynamicFileSz = 0,
310 .dynamicFile = NULL,
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200311 .sanCovCnts = {
Anestis Bechtsoudis56e360f2016-01-11 14:29:17 +0200312 .hitBBCnt = 0ULL,
313 .totalBBCnt = 0ULL,
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200314 .dsoCnt = 0ULL,
315 .iDsoCnt = 0ULL,
Anestis Bechtsoudis56e360f2016-01-11 14:29:17 +0200316 .newBBCnt = 0ULL,
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200317 .crashesCnt = 0ULL,
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200318 },
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300319 .report = {'\0'},
Jaggerb2c1fad2016-03-17 23:37:10 +0100320 .mainWorker = false,
321
Jagger2381ef42016-03-20 23:32:05 +0100322 .linux = {
323 .hwCnts = {
324 .cpuInstrCnt = 0ULL,
325 .cpuBranchCnt = 0ULL,
326 .customCnt = 0ULL,
327 .bbCnt = 0ULL,
328 .newBBCnt = 0ULL,
329 },
330 .perfMmapBuf = NULL,
331 .perfMmapAux = NULL,
Anestis Bechtsoudis92b97042016-04-27 12:34:19 +0300332#if defined(_HF_ARCH_LINUX)
333 .timerId = (timer_t) 0,
334#endif // defined(_HF_ARCH_LINUX)
335 .attachedPid = 0,
336 .persistentSock = -1,
Jagger2381ef42016-03-20 23:32:05 +0100337 },
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300338 };
339
Anestis Bechtsoudis92b97042016-04-27 12:34:19 +0300340 if (arch_archThreadInit(hfuzz, &vFuzzer) == false) {
341 LOG_F("Could not initialize the thread");
342 }
343
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300344 fuzz_getFileName(hfuzz, vFuzzer.fileName);
345 if (files_writeBufToFile
Jagger33cce5d2016-04-16 20:10:36 +0200346 (vFuzzer.fileName, crashBuf, crashFileSz,
347 O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC) == false) {
Anestis Bechtsoudis0cde66f2015-10-11 19:37:11 -0700348 LOG_E("Couldn't write buffer to file '%s'", vFuzzer.fileName);
Robert Swieckie48811e2016-03-09 18:31:19 +0100349 return false;
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300350 }
351
Robert Swiecki12800cd2016-03-31 15:38:10 +0200352 vFuzzer.pid = arch_fork(hfuzz, &vFuzzer);
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300353 if (vFuzzer.pid == -1) {
Anestis Bechtsoudis0cde66f2015-10-11 19:37:11 -0700354 PLOG_F("Couldn't fork");
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300355 return false;
356 }
357
358 if (!vFuzzer.pid) {
Robert Swiecki1e1b3532016-03-10 18:43:28 +0100359 if (fuzz_prepareExecve(hfuzz, crashedFuzzer->crashFileName) == false) {
360 LOG_E("fuzz_prepareExecve() failed");
Jagger2b97dc92016-03-10 04:28:18 +0100361 return false;
362 }
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300363 if (!arch_launchChild(hfuzz, crashedFuzzer->crashFileName)) {
Anestis Bechtsoudis0cde66f2015-10-11 19:37:11 -0700364 LOG_E("Error launching verifier child process");
Robert Swieckie48811e2016-03-09 18:31:19 +0100365 return false;
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300366 }
367 }
368
369 arch_reapChild(hfuzz, &vFuzzer);
370 unlink(vFuzzer.fileName);
371
Anestis Bechtsoudisecb0a662015-09-27 18:19:46 +0300372 /* If stack hash doesn't match skip name tag and exit */
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300373 if (crashedFuzzer->backtrace != vFuzzer.backtrace) {
Anestis Bechtsoudis0cde66f2015-10-11 19:37:11 -0700374 LOG_D("Verifier stack hash mismatch");
Robert Swieckie48811e2016-03-09 18:31:19 +0100375 return false;
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300376 }
377 }
378
379 /* Workspace is inherited, just append a extra suffix */
380 char verFile[PATH_MAX] = { 0 };
381 snprintf(verFile, sizeof(verFile), "%s.verified", crashedFuzzer->crashFileName);
Anestis Bechtsoudisecb0a662015-09-27 18:19:46 +0300382
Anestis Bechtsoudisd86e6602015-11-07 18:34:06 +0200383 /* Copy file with new suffix & remove original copy */
384 bool dstFileExists = false;
385 if (files_copyFile(crashedFuzzer->crashFileName, verFile, &dstFileExists)) {
386 LOG_I("Successfully verified, saving as (%s)", verFile);
Jaggerd34417d2016-03-16 01:26:54 +0100387 ATOMIC_POST_INC(hfuzz->verifiedCrashesCnt);
Anestis Bechtsoudisd86e6602015-11-07 18:34:06 +0200388 unlink(crashedFuzzer->crashFileName);
389 } else {
390 if (dstFileExists) {
391 LOG_I("It seems that '%s' already exists, skipping", verFile);
392 } else {
393 LOG_E("Couldn't copy '%s' to '%s'", crashedFuzzer->crashFileName, verFile);
Robert Swieckie48811e2016-03-09 18:31:19 +0100394 return false;
Anestis Bechtsoudisd86e6602015-11-07 18:34:06 +0200395 }
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300396 }
Anestis Bechtsoudisecb0a662015-09-27 18:19:46 +0300397
Robert Swieckie48811e2016-03-09 18:31:19 +0100398 return true;
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300399}
400
Robert Swieckia4a68052016-03-17 16:51:01 +0100401static void fuzz_addFileToFileQLocked(honggfuzz_t * hfuzz, uint8_t * data, size_t size,
402 uint64_t cov)
Robert Swieckif3534bb2016-03-14 18:55:10 +0100403{
Robert Swiecki7084e652016-03-14 19:47:00 +0100404 struct dynfile_t *dynfile = (struct dynfile_t *)util_Malloc(sizeof(struct dynfile_t));
Robert Swieckif3534bb2016-03-14 18:55:10 +0100405 dynfile->size = size;
Robert Swiecki7084e652016-03-14 19:47:00 +0100406 dynfile->data = (uint8_t *) util_Malloc(size);
Robert Swieckif3534bb2016-03-14 18:55:10 +0100407 memcpy(dynfile->data, data, size);
408 TAILQ_INSERT_TAIL(&hfuzz->dynfileq, dynfile, pointers);
409 hfuzz->dynfileqCnt++;
410
411 char fname[PATH_MAX];
Robert Swieckia4a68052016-03-17 16:51:01 +0100412 snprintf(fname, sizeof(fname),
Robert Swiecki4fc3d572016-03-17 16:55:07 +0100413 "%s/COV.RANK.%06" PRIu64 ".PID.%d.COVBB.%07" PRIu64 ".TIME.%" PRIu64 ".RND.%" PRIx64,
414 hfuzz->workDir, (uint64_t) 999999ULL - cov, getpid(), cov, (uint64_t) time(NULL),
415 util_rndGet(0, 0xFFFFFFFFFFFF));
Jagger33cce5d2016-04-16 20:10:36 +0200416 if (files_writeBufToFile(fname, data, size, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC | O_CLOEXEC)
417 == false) {
Robert Swieckif3534bb2016-03-14 18:55:10 +0100418 LOG_W("Couldn't write buffer to file '%s'", fname);
419 }
420}
421
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200422static void fuzz_perfFeedback(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
423{
424 LOG_D
Robert Swiecki3bfc33c2016-03-14 18:12:41 +0100425 ("New file size: %zu, Perf feedback new/cur (instr,branch): %" PRIu64 "/%" PRIu64 ",%"
426 PRIu64 "/%" PRIu64 ", BBcnt new/total: %" PRIu64 "/%" PRIu64, fuzzer->dynamicFileSz,
Jagger247c3b42016-03-21 23:24:05 +0100427 fuzzer->linux.hwCnts.cpuInstrCnt, hfuzz->linux.hwCnts.cpuInstrCnt,
428 fuzzer->linux.hwCnts.cpuBranchCnt, hfuzz->linux.hwCnts.cpuBranchCnt,
429 fuzzer->linux.hwCnts.newBBCnt, hfuzz->linux.hwCnts.bbCnt);
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200430
Robert Swiecki76ecd5e2016-03-16 14:57:03 +0100431 MX_SCOPED_LOCK(&hfuzz->dynfileq_mutex);
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200432
Jagger247c3b42016-03-21 23:24:05 +0100433 int64_t diff0 = hfuzz->linux.hwCnts.cpuInstrCnt - fuzzer->linux.hwCnts.cpuInstrCnt;
434 int64_t diff1 = hfuzz->linux.hwCnts.cpuBranchCnt - fuzzer->linux.hwCnts.cpuBranchCnt;
435 int64_t diff2 = hfuzz->linux.hwCnts.customCnt - fuzzer->linux.hwCnts.customCnt;
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200436
Jagger2381ef42016-03-20 23:32:05 +0100437 if (diff0 < 0 || diff1 < 0 || diff2 < 0 || fuzzer->linux.hwCnts.newBBCnt > 0) {
Robert Swieckif3534bb2016-03-14 18:55:10 +0100438 LOG_I
Robert Swiecki3bfc33c2016-03-14 18:12:41 +0100439 ("New file size: %zu, Perf feedback new/cur (instr,branch): %" PRIu64 "/%" PRIu64 ",%"
440 PRIu64 "/%" PRIu64 ", BBcnt new/total: %" PRIu64 "/%" PRIu64, fuzzer->dynamicFileSz,
Jagger247c3b42016-03-21 23:24:05 +0100441 fuzzer->linux.hwCnts.cpuInstrCnt, hfuzz->linux.hwCnts.cpuInstrCnt,
442 fuzzer->linux.hwCnts.cpuBranchCnt, hfuzz->linux.hwCnts.cpuBranchCnt,
443 fuzzer->linux.hwCnts.newBBCnt, hfuzz->linux.hwCnts.bbCnt);
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200444
Jagger247c3b42016-03-21 23:24:05 +0100445 hfuzz->linux.hwCnts.cpuInstrCnt = fuzzer->linux.hwCnts.cpuInstrCnt;
446 hfuzz->linux.hwCnts.cpuBranchCnt = fuzzer->linux.hwCnts.cpuBranchCnt;
447 hfuzz->linux.hwCnts.customCnt = fuzzer->linux.hwCnts.customCnt;
448 hfuzz->linux.hwCnts.bbCnt += fuzzer->linux.hwCnts.newBBCnt;
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200449
Robert Swieckia4a68052016-03-17 16:51:01 +0100450 fuzz_addFileToFileQLocked(hfuzz, fuzzer->dynamicFile, fuzzer->dynamicFileSz,
Jagger2381ef42016-03-20 23:32:05 +0100451 fuzzer->linux.hwCnts.newBBCnt);
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200452 }
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200453}
454
455static void fuzz_sanCovFeedback(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
456{
457 LOG_D
Robert Swiecki3bfc33c2016-03-14 18:12:41 +0100458 ("File size (Best/New): %zu, SanCov feedback (bb,dso): Best: [%" PRIu64
Anestis Bechtsoudis56e360f2016-01-11 14:29:17 +0200459 ",%" PRIu64 "] / New: [%" PRIu64 ",%" PRIu64 "], newBBs:%" PRIu64,
Robert Swiecki3bfc33c2016-03-14 18:12:41 +0100460 fuzzer->dynamicFileSz, hfuzz->sanCovCnts.hitBBCnt,
Anestis Bechtsoudis56e360f2016-01-11 14:29:17 +0200461 hfuzz->sanCovCnts.iDsoCnt, fuzzer->sanCovCnts.hitBBCnt, fuzzer->sanCovCnts.iDsoCnt,
462 fuzzer->sanCovCnts.newBBCnt);
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200463
Robert Swiecki76ecd5e2016-03-16 14:57:03 +0100464 MX_SCOPED_LOCK(&hfuzz->dynfileq_mutex);
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200465
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200466 /*
467 * Keep mutated seed if:
Robert Swiecki142f9412016-03-14 19:22:01 +0100468 * a) Newly discovered (not met before) BBs
469 * b) More instrumented DSOs loaded
Robert Swiecki23ec02a2016-01-19 18:47:45 +0100470 *
Anestis Bechtsoudisb78cf602016-01-07 13:10:50 +0200471 * TODO: (a) method can significantly assist to further improvements in interesting areas
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200472 * discovery if combined with seeds pool/queue support. If a runtime queue is maintained
473 * more interesting seeds can be saved between runs instead of instantly discarded
474 * based on current absolute elitism (only one mutated seed is promoted).
475 */
Robert Swiecki142f9412016-03-14 19:22:01 +0100476 if (fuzzer->sanCovCnts.newBBCnt > 0 || hfuzz->sanCovCnts.iDsoCnt < fuzzer->sanCovCnts.iDsoCnt) {
Robert Swiecki3bfc33c2016-03-14 18:12:41 +0100477 LOG_I("SanCov Update: file size (Cur): %zu, newBBs:%" PRIu64
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200478 ", counters (Cur,New): %" PRIu64 "/%" PRIu64 ",%" PRIu64 "/%" PRIu64,
Robert Swiecki3bfc33c2016-03-14 18:12:41 +0100479 fuzzer->dynamicFileSz, fuzzer->sanCovCnts.newBBCnt,
Anestis Bechtsoudis56e360f2016-01-11 14:29:17 +0200480 hfuzz->sanCovCnts.hitBBCnt, hfuzz->sanCovCnts.iDsoCnt, fuzzer->sanCovCnts.hitBBCnt,
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200481 fuzzer->sanCovCnts.iDsoCnt);
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200482
Robert Swiecki142f9412016-03-14 19:22:01 +0100483 hfuzz->sanCovCnts.hitBBCnt += fuzzer->sanCovCnts.newBBCnt;
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200484 hfuzz->sanCovCnts.dsoCnt = fuzzer->sanCovCnts.dsoCnt;
485 hfuzz->sanCovCnts.iDsoCnt = fuzzer->sanCovCnts.iDsoCnt;
486 hfuzz->sanCovCnts.crashesCnt += fuzzer->sanCovCnts.crashesCnt;
Robert Swiecki142f9412016-03-14 19:22:01 +0100487 hfuzz->sanCovCnts.newBBCnt = fuzzer->sanCovCnts.newBBCnt;
Anestis Bechtsoudisb78cf602016-01-07 13:10:50 +0200488
Anestis Bechtsoudis56e360f2016-01-11 14:29:17 +0200489 if (hfuzz->sanCovCnts.totalBBCnt < fuzzer->sanCovCnts.totalBBCnt) {
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200490 /* Keep only the max value (for dlopen cases) to measure total target coverage */
Anestis Bechtsoudis56e360f2016-01-11 14:29:17 +0200491 hfuzz->sanCovCnts.totalBBCnt = fuzzer->sanCovCnts.totalBBCnt;
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200492 }
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200493
Robert Swieckia4a68052016-03-17 16:51:01 +0100494 fuzz_addFileToFileQLocked(hfuzz, fuzzer->dynamicFile, fuzzer->dynamicFileSz,
495 fuzzer->sanCovCnts.hitBBCnt);
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200496 }
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200497}
498
Robert Swiecki6d01e822016-03-15 20:21:46 +0100499static fuzzState_t fuzz_getState(honggfuzz_t * hfuzz)
500{
Jaggerd34417d2016-03-16 01:26:54 +0100501 return ATOMIC_GET(hfuzz->state);
Robert Swiecki6d01e822016-03-15 20:21:46 +0100502}
503
504static void fuzz_setState(honggfuzz_t * hfuzz, fuzzState_t state)
505{
Jaggerd34417d2016-03-16 01:26:54 +0100506 ATOMIC_SET(hfuzz->state, state);
Robert Swiecki6d01e822016-03-15 20:21:46 +0100507}
508
Robert Swieckidecf14b2016-03-31 15:09:28 +0200509static void fuzz_fuzzLoop(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
Jagger190f0dc2015-09-05 16:41:22 +0200510{
Robert Swieckidecf14b2016-03-31 15:09:28 +0200511 fuzzer->pid = 0;
512 fuzzer->timeStartedMillis = util_timeNowMillis();
513 fuzzer->crashFileName[0] = '\0';
514 fuzzer->pc = 0ULL;
515 fuzzer->backtrace = 0ULL;
516 fuzzer->access = 0ULL;
517 fuzzer->exception = 0;
518 fuzzer->report[0] = '\0';
519 fuzzer->mainWorker = true;
520 fuzzer->origFileName = "DYNAMIC";
521 fuzzer->flipRate = hfuzz->origFlipRate;
522 fuzzer->dynamicFileSz = 0;
Robert Swieckia96d78d2016-03-14 16:50:50 +0100523
Robert Swieckidecf14b2016-03-31 15:09:28 +0200524 fuzzer->sanCovCnts.hitBBCnt = 0ULL;
525 fuzzer->sanCovCnts.totalBBCnt = 0ULL;
526 fuzzer->sanCovCnts.dsoCnt = 0ULL;
527 fuzzer->sanCovCnts.newBBCnt = 0ULL;
528 fuzzer->sanCovCnts.crashesCnt = 0ULL;
Robert Swieckia96d78d2016-03-14 16:50:50 +0100529
Robert Swieckidecf14b2016-03-31 15:09:28 +0200530 fuzzer->linux.hwCnts.cpuInstrCnt = 0ULL;
531 fuzzer->linux.hwCnts.cpuBranchCnt = 0ULL;
532 fuzzer->linux.hwCnts.customCnt = 0ULL;
533 fuzzer->linux.hwCnts.bbCnt = 0ULL;
534 fuzzer->linux.hwCnts.newBBCnt = 0ULL;
535 fuzzer->linux.perfMmapBuf = NULL;
536 fuzzer->linux.perfMmapAux = NULL;
Jagger190f0dc2015-09-05 16:41:22 +0200537
Anestis Bechtsoudis2013f252015-11-07 22:03:31 +0200538 size_t rnd_index = util_rndGet(0, hfuzz->fileCnt - 1);
Anestis Bechtsoudis46ea10e2015-11-07 18:16:25 +0200539
540 /* If dry run mode, pick the next file and not a random one */
Robert Swieckidecf14b2016-03-31 15:09:28 +0200541 if (fuzzer->flipRate == 0.0L && hfuzz->useVerifier) {
Jaggerd34417d2016-03-16 01:26:54 +0100542 rnd_index = ATOMIC_POST_INC(hfuzz->lastFileIndex);
Anestis Bechtsoudis46ea10e2015-11-07 18:16:25 +0200543 }
Robert Swiecki129afe82016-03-15 20:03:20 +0100544
Robert Swiecki6d01e822016-03-15 20:21:46 +0100545 if (fuzz_getState(hfuzz) == _HF_STATE_DYNAMIC_PRE) {
Jaggerd34417d2016-03-16 01:26:54 +0100546 rnd_index = ATOMIC_POST_INC(hfuzz->lastFileIndex);
Robert Swieckia96d78d2016-03-14 16:50:50 +0100547 if (rnd_index >= hfuzz->fileCnt) {
Jagger33fd9522016-03-15 22:13:57 +0100548 /*
549 * The waiting logic (for the DYNAMIC_PRE phase to finish) should be based on cond-waits
550 * or mutexes, but it'd complicate code too much
551 */
Robert Swiecki6d01e822016-03-15 20:21:46 +0100552 while (fuzz_getState(hfuzz) == _HF_STATE_DYNAMIC_PRE) {
553 sleep(1);
Robert Swiecki129afe82016-03-15 20:03:20 +0100554 }
Robert Swieckia96d78d2016-03-14 16:50:50 +0100555 }
556 }
Anestis Bechtsoudis46ea10e2015-11-07 18:16:25 +0200557
Jagger33fd9522016-03-15 22:13:57 +0100558 fuzzState_t state = fuzz_getState(hfuzz);
Robert Swieckidecf14b2016-03-31 15:09:28 +0200559 if (state != _HF_STATE_DYNAMIC_MAIN) {
560 fuzzer->origFileName = files_basename(hfuzz->files[rnd_index]);
Robert Swieckia08ab1c2016-03-14 18:29:21 +0100561 }
Robert Swiecki6d01e822016-03-15 20:21:46 +0100562
Robert Swieckidecf14b2016-03-31 15:09:28 +0200563 fuzz_getFileName(hfuzz, fuzzer->fileName);
Jagger190f0dc2015-09-05 16:41:22 +0200564
Jagger30384ec2016-04-02 02:30:32 +0200565 if (hfuzz->externalCommand) {
566 if (!fuzz_prepareFileExternally(hfuzz, fuzzer)) {
567 LOG_F("fuzz_prepareFileExternally() failed");
568 }
569 } else if (state == _HF_STATE_DYNAMIC_MAIN) {
Robert Swieckidecf14b2016-03-31 15:09:28 +0200570 if (!fuzz_prepareFileDynamically(hfuzz, fuzzer)) {
Jagger1d8e0c62016-04-02 02:22:40 +0200571 LOG_F("fuzz_prepareFileDynamically() failed");
Robert Swieckia96d78d2016-03-14 16:50:50 +0100572 }
Robert Swiecki6d01e822016-03-15 20:21:46 +0100573 } else if (state == _HF_STATE_DYNAMIC_PRE) {
Robert Swieckidecf14b2016-03-31 15:09:28 +0200574 fuzzer->flipRate = 0.0f;
575 if (!fuzz_prepareFile(hfuzz, fuzzer, rnd_index)) {
Jagger1d8e0c62016-04-02 02:22:40 +0200576 LOG_F("fuzz_prepareFile() failed");
Jagger190f0dc2015-09-05 16:41:22 +0200577 }
Jagger190f0dc2015-09-05 16:41:22 +0200578 } else {
Robert Swieckidecf14b2016-03-31 15:09:28 +0200579 if (!fuzz_prepareFile(hfuzz, fuzzer, rnd_index)) {
Jagger1d8e0c62016-04-02 02:22:40 +0200580 LOG_F("fuzz_prepareFile() failed");
Jagger190f0dc2015-09-05 16:41:22 +0200581 }
582 }
583
Robert Swieckidecf14b2016-03-31 15:09:28 +0200584 fuzzer->pid = fuzzer->persistentPid;
585 if (fuzzer->pid == 0) {
Robert Swiecki12800cd2016-03-31 15:38:10 +0200586 fuzzer->pid = arch_fork(hfuzz, fuzzer);
Robert Swieckidecf14b2016-03-31 15:09:28 +0200587 if (fuzzer->pid == -1) {
Robert Swiecki0f937af2016-03-30 18:19:16 +0200588 PLOG_F("Couldn't fork");
Jaggerbb83e7c2016-03-10 04:33:40 +0100589 }
Robert Swiecki0f937af2016-03-30 18:19:16 +0200590
Robert Swieckidecf14b2016-03-31 15:09:28 +0200591 if (!fuzzer->pid) {
592 if (!fuzz_prepareExecve(hfuzz, fuzzer->fileName)) {
Robert Swiecki0f937af2016-03-30 18:19:16 +0200593 LOG_E("fuzz_prepareExecve() failed");
594 exit(EXIT_FAILURE);
595 }
Robert Swieckidecf14b2016-03-31 15:09:28 +0200596 if (!arch_launchChild(hfuzz, fuzzer->fileName)) {
Robert Swiecki0f937af2016-03-30 18:19:16 +0200597 LOG_E("Error launching child process");
598 exit(EXIT_FAILURE);
599 }
600 }
Robert Swiecki1ae5eab2016-03-31 15:17:46 +0200601
602 if (hfuzz->persistent) {
Robert Swiecki0e673882016-03-31 18:50:29 +0200603 LOG_I("Persistent mode: Launched new persistent PID: %d", (int)fuzzer->pid);
Robert Swiecki12800cd2016-03-31 15:38:10 +0200604 fuzzer->persistentPid = fuzzer->pid;
Robert Swiecki1ae5eab2016-03-31 15:17:46 +0200605 }
Jagger190f0dc2015-09-05 16:41:22 +0200606 }
607
Robert Swieckidecf14b2016-03-31 15:09:28 +0200608 LOG_D("Launched new process, pid: %d, (concurrency: %zd)", fuzzer->pid, hfuzz->threadsMax);
Jagger190f0dc2015-09-05 16:41:22 +0200609
Robert Swieckidecf14b2016-03-31 15:09:28 +0200610 arch_reapChild(hfuzz, fuzzer);
Jaggera34b3022016-04-02 02:20:40 +0200611 if (hfuzz->persistent == false) {
612 unlink(fuzzer->fileName);
613 }
Jagger190f0dc2015-09-05 16:41:22 +0200614
615 if (hfuzz->dynFileMethod != _HF_DYNFILE_NONE) {
Robert Swieckidecf14b2016-03-31 15:09:28 +0200616 fuzz_perfFeedback(hfuzz, fuzzer);
Jaggerabb6ae02016-03-18 01:55:51 +0100617 }
618 if (hfuzz->useSanCov) {
Robert Swieckidecf14b2016-03-31 15:09:28 +0200619 fuzz_sanCovFeedback(hfuzz, fuzzer);
Robert Swiecki0f937af2016-03-30 18:19:16 +0200620 }
Jagger190f0dc2015-09-05 16:41:22 +0200621
Robert Swieckidecf14b2016-03-31 15:09:28 +0200622 if (hfuzz->useVerifier && (fuzzer->crashFileName[0] != 0) && fuzzer->backtrace) {
623 if (!fuzz_runVerifier(hfuzz, fuzzer)) {
624 LOG_I("Failed to verify %s", fuzzer->crashFileName);
Anestis Bechtsoudis3085f222015-09-27 18:35:26 +0300625 }
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300626 }
627
Robert Swieckidecf14b2016-03-31 15:09:28 +0200628 report_Report(hfuzz, fuzzer->report);
Robert Swiecki05354ca2016-03-15 19:10:23 +0100629
Jaggerd34417d2016-03-16 01:26:54 +0100630 if (state == _HF_STATE_DYNAMIC_PRE && ATOMIC_PRE_INC(hfuzz->doneFileIndex) >= hfuzz->fileCnt) {
Robert Swiecki6d01e822016-03-15 20:21:46 +0100631 fuzz_setState(hfuzz, _HF_STATE_DYNAMIC_MAIN);
Robert Swiecki05354ca2016-03-15 19:10:23 +0100632 }
Jagger190f0dc2015-09-05 16:41:22 +0200633}
634
robert.swiecki@gmail.comc8443142015-02-13 13:46:40 +0000635static void *fuzz_threadNew(void *arg)
robert.swiecki3bb518c2010-10-14 00:48:24 +0000636{
Robert Swiecki0e673882016-03-31 18:50:29 +0200637 LOG_I("Launched new fuzzing thread");
638
robert.swiecki@gmail.com882900b2015-02-11 13:56:22 +0000639 honggfuzz_t *hfuzz = (honggfuzz_t *) arg;
Anestis Bechtsoudis02b99be2015-12-27 11:53:01 +0200640
Robert Swieckidecf14b2016-03-31 15:09:28 +0200641 fuzzer_t fuzzer = {
642 .pid = 0,
643 .persistentPid = 0,
644 .dynamicFile = util_Malloc(hfuzz->maxFileSz),
Robert Swiecki12800cd2016-03-31 15:38:10 +0200645
Robert Swiecki9f9968a2016-04-01 14:39:51 +0200646#if defined(_HF_ARCH_LINUX)
Robert Swieckie33c90b2016-03-31 17:32:56 +0200647 .linux.timerId = (timer_t) 0,
Robert Swiecki9f9968a2016-04-01 14:39:51 +0200648#endif // defined(_HF_ARCH_LINUX)
649 .linux.attachedPid = 0,.linux.persistentSock = -1,
Robert Swieckidecf14b2016-03-31 15:09:28 +0200650 };
Jagger4fe18692016-04-22 23:15:07 +0200651 defer {
652 free(fuzzer.dynamicFile);
653 };
Robert Swieckidecf14b2016-03-31 15:09:28 +0200654
655 if (arch_archThreadInit(hfuzz, &fuzzer) == false) {
Robert Swiecki0f937af2016-03-30 18:19:16 +0200656 LOG_F("Could not initialize the thread");
657 }
658
Robert Swieckia96d78d2016-03-14 16:50:50 +0100659 for (;;) {
Anestis Bechtsoudis46ea10e2015-11-07 18:16:25 +0200660 /* Check if dry run mode with verifier enabled */
Robert Swieckia96d78d2016-03-14 16:50:50 +0100661 if (hfuzz->origFlipRate == 0.0L && hfuzz->useVerifier) {
Jaggerd34417d2016-03-16 01:26:54 +0100662 if (ATOMIC_POST_INC(hfuzz->mutationsCnt) >= hfuzz->fileCnt) {
663 ATOMIC_POST_INC(hfuzz->threadsFinished);
Anestis Bechtsoudis46ea10e2015-11-07 18:16:25 +0200664 // All files checked, weak-up the main process
665 pthread_kill(fuzz_mainThread, SIGALRM);
666 return NULL;
667 }
668 }
669 /* Check for max iterations limit if set */
Jaggerd34417d2016-03-16 01:26:54 +0100670 else if ((ATOMIC_POST_INC(hfuzz->mutationsCnt) >= hfuzz->mutationsMax)
Anestis Bechtsoudis46ea10e2015-11-07 18:16:25 +0200671 && hfuzz->mutationsMax) {
Jaggerd34417d2016-03-16 01:26:54 +0100672 ATOMIC_POST_INC(hfuzz->threadsFinished);
Robert Swiecki81c6a0d2015-09-08 15:43:20 +0200673 // Wake-up the main process
Jagger6113c432015-09-24 05:00:28 +0200674 pthread_kill(fuzz_mainThread, SIGALRM);
Jaggerea39a8f2015-09-05 00:57:22 +0200675 return NULL;
676 }
robert.swiecki3bb518c2010-10-14 00:48:24 +0000677
Robert Swieckidecf14b2016-03-31 15:09:28 +0200678 fuzz_fuzzLoop(hfuzz, &fuzzer);
robert.swiecki@gmail.comd4dd4df2015-02-18 00:50:12 +0000679 }
robert.swiecki@gmail.com882900b2015-02-11 13:56:22 +0000680}
681
robert.swiecki@gmail.comc8443142015-02-13 13:46:40 +0000682static void fuzz_runThread(honggfuzz_t * hfuzz, void *(*thread) (void *))
683{
robert.swiecki@gmail.comc8443142015-02-13 13:46:40 +0000684 pthread_attr_t attr;
robert.swiecki@gmail.comc8443142015-02-13 13:46:40 +0000685
robert.swiecki@gmail.com6f5c2392015-02-16 18:13:09 +0000686 pthread_attr_init(&attr);
robert.swiecki@gmail.comc8443142015-02-13 13:46:40 +0000687 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
robert.swiecki@gmail.com441089a2015-02-23 13:14:07 +0000688 pthread_attr_setstacksize(&attr, _HF_PTHREAD_STACKSIZE);
robert.swiecki@gmail.com011981f2015-02-17 19:06:44 +0000689 pthread_attr_setguardsize(&attr, (size_t) sysconf(_SC_PAGESIZE));
robert.swiecki@gmail.comc8443142015-02-13 13:46:40 +0000690
robert.swiecki@gmail.com01b6dd42015-02-16 18:11:28 +0000691 pthread_t t;
robert.swiecki@gmail.comc8443142015-02-13 13:46:40 +0000692 if (pthread_create(&t, &attr, thread, (void *)hfuzz) < 0) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200693 PLOG_F("Couldn't create a new thread");
robert.swiecki@gmail.comc8443142015-02-13 13:46:40 +0000694 }
695
696 return;
697}
698
Jagger08816fd2016-03-11 01:32:38 +0100699void fuzz_threads(honggfuzz_t * hfuzz)
robert.swiecki3bb518c2010-10-14 00:48:24 +0000700{
Jagger6113c432015-09-24 05:00:28 +0200701 fuzz_mainThread = pthread_self();
702
robert.swiecki@gmail.com956276a2015-04-16 16:51:52 +0000703 if (!arch_archInit(hfuzz)) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200704 LOG_F("Couldn't prepare arch for fuzzing");
robert.swiecki@gmail.comef829fa2011-06-22 13:51:57 +0000705 }
Jagger00265602016-03-10 02:36:27 +0100706 if (!sancov_Init(hfuzz)) {
707 LOG_F("Couldn't prepare sancov options");
708 }
robert.swiecki@gmail.comef829fa2011-06-22 13:51:57 +0000709
Robert Swieckia96d78d2016-03-14 16:50:50 +0100710 if (hfuzz->useSanCov || hfuzz->dynFileMethod != _HF_DYNFILE_NONE) {
Jaggerebe19292016-03-17 00:11:44 +0100711 fuzz_setState(hfuzz, _HF_STATE_DYNAMIC_PRE);
Robert Swieckia96d78d2016-03-14 16:50:50 +0100712 } else {
Jaggerebe19292016-03-17 00:11:44 +0100713 fuzz_setState(hfuzz, _HF_STATE_STATIC);
Robert Swieckia96d78d2016-03-14 16:50:50 +0100714 }
715
Jaggerea39a8f2015-09-05 00:57:22 +0200716 for (size_t i = 0; i < hfuzz->threadsMax; i++) {
robert.swiecki@gmail.comc8443142015-02-13 13:46:40 +0000717 fuzz_runThread(hfuzz, fuzz_threadNew);
robert.swiecki3bb518c2010-10-14 00:48:24 +0000718 }
robert.swiecki3bb518c2010-10-14 00:48:24 +0000719}