blob: 8ee4187250b8abd90666ecee359095461adcd009 [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 - the main file
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 Swieckic8c32db2015-10-09 18:06:22 +020026#include <inttypes.h>
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000027#include <getopt.h>
Jagger08816fd2016-03-11 01:32:38 +010028#include <signal.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000029#include <stdio.h>
30#include <stdlib.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000031#include <string.h>
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000032#include <time.h>
33#include <unistd.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000034
35#include "common.h"
Robert Swieckic8c32db2015-10-09 18:06:22 +020036#include "cmdline.h"
Jagger08816fd2016-03-11 01:32:38 +010037#include "display.h"
robert.swiecki3bb518c2010-10-14 00:48:24 +000038#include "log.h"
39#include "files.h"
40#include "fuzz.h"
41#include "util.h"
42
Jagger08816fd2016-03-11 01:32:38 +010043static int sigReceived = 0;
44
Robert Swieckic6a48b12016-03-11 17:41:57 +010045/*
46 * CygWin/MinGW incorrectly copies stack during fork(), so we need to keep some
47 * structures in the data section
48 */
49honggfuzz_t hfuzz;
Robert Swieckic6a48b12016-03-11 17:41:57 +010050
Jagger08816fd2016-03-11 01:32:38 +010051void sigHandler(int sig)
52{
53 /* We should not terminate upon SIGALRM delivery */
54 if (sig == SIGALRM) {
55 return;
56 }
57
58 sigReceived = sig;
59}
60
61static void setupTimer(void)
62{
63 struct itimerval it = {
Robert Swiecki37778e02016-03-15 15:45:28 +010064 .it_value = {.tv_sec = 1,.tv_usec = 0},
Robert Swiecki05783bb2016-07-26 15:43:48 +020065 .it_interval = {.tv_sec = 1,.tv_usec = 0},
Jagger08816fd2016-03-11 01:32:38 +010066 };
67 if (setitimer(ITIMER_REAL, &it, NULL) == -1) {
68 PLOG_F("setitimer(ITIMER_REAL)");
69 }
70}
71
72static void setupSignalsPreThr(void)
73{
Jaggerfbd4ae12016-09-02 02:26:02 +020074 /* Block signals which should be handled or blocked in the main thread */
Jagger33cce5d2016-04-16 20:10:36 +020075 sigset_t ss;
Jagger090de222016-04-16 20:05:13 +020076 sigemptyset(&ss);
Jagger08816fd2016-03-11 01:32:38 +010077 sigaddset(&ss, SIGTERM);
78 sigaddset(&ss, SIGINT);
79 sigaddset(&ss, SIGQUIT);
Jagger33cce5d2016-04-16 20:10:36 +020080 sigaddset(&ss, SIGALRM);
Jaggerac75f262016-08-20 12:38:20 +020081 sigaddset(&ss, SIGPIPE);
Jaggerfbd4ae12016-09-02 02:26:02 +020082 sigaddset(&ss, SIGIO);
Jagger08816fd2016-03-11 01:32:38 +010083 if (sigprocmask(SIG_BLOCK, &ss, NULL) != 0) {
84 PLOG_F("pthread_sigmask(SIG_BLOCK)");
85 }
Jagger848c4fc2016-03-11 01:34:53 +010086}
Jagger08816fd2016-03-11 01:32:38 +010087
Jagger848c4fc2016-03-11 01:34:53 +010088static void setupSignalsPostThr(void)
89{
Jagger08816fd2016-03-11 01:32:38 +010090 struct sigaction sa = {
91 .sa_handler = sigHandler,
92 .sa_flags = 0,
93 };
94 sigemptyset(&sa.sa_mask);
95 if (sigaction(SIGTERM, &sa, NULL) == -1) {
96 PLOG_F("sigaction(SIGTERM) failed");
97 }
98 if (sigaction(SIGINT, &sa, NULL) == -1) {
99 PLOG_F("sigaction(SIGINT) failed");
100 }
101 if (sigaction(SIGQUIT, &sa, NULL) == -1) {
102 PLOG_F("sigaction(SIGQUIT) failed");
103 }
Jagger33cce5d2016-04-16 20:10:36 +0200104 if (sigaction(SIGALRM, &sa, NULL) == -1) {
105 PLOG_F("sigaction(SIGQUIT) failed");
106 }
Jagger08816fd2016-03-11 01:32:38 +0100107 /* Unblock signals which should be handled by the main thread */
108 sigset_t ss;
109 sigemptyset(&ss);
110 sigaddset(&ss, SIGTERM);
111 sigaddset(&ss, SIGINT);
112 sigaddset(&ss, SIGQUIT);
Jagger3846dfc2016-04-16 20:06:16 +0200113 sigaddset(&ss, SIGALRM);
Jagger08816fd2016-03-11 01:32:38 +0100114 if (sigprocmask(SIG_UNBLOCK, &ss, NULL) != 0) {
Jagger33cce5d2016-04-16 20:10:36 +0200115 PLOG_F("pthread_sigmask(SIG_UNBLOCK)");
Jagger08816fd2016-03-11 01:32:38 +0100116 }
117}
118
robert.swiecki3bb518c2010-10-14 00:48:24 +0000119int main(int argc, char **argv)
120{
Robert Swieckic6a48b12016-03-11 17:41:57 +0100121 /*
122 * Work around CygWin/MinGW
123 */
Robert Swiecki37778e02016-03-15 15:45:28 +0100124 char **myargs = (char **)util_Malloc(sizeof(char *) * (argc + 1));
Anestis Bechtsoudisa4198732016-04-27 12:27:39 +0300125 defer {
126 free(myargs);
127 };
Jagger1ebc6dc2016-03-12 01:39:09 +0100128
129 int i;
130 for (i = 0U; i < argc; i++) {
Robert Swieckic6a48b12016-03-11 17:41:57 +0100131 myargs[i] = argv[i];
132 }
133 myargs[i] = NULL;
134
135 if (cmdlineParse(argc, myargs, &hfuzz) == false) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200136 LOG_F("Parsing of the cmd-line arguments failed");
robert.swiecki3bb518c2010-10-14 00:48:24 +0000137 }
138
robert.swiecki3bb518c2010-10-14 00:48:24 +0000139 if (!files_init(&hfuzz)) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200140 LOG_F("Couldn't load input files");
robert.swiecki3bb518c2010-10-14 00:48:24 +0000141 exit(EXIT_FAILURE);
142 }
143
robert.swiecki@gmail.com4f1124f2015-04-21 17:12:22 +0000144 if (hfuzz.dictionaryFile && (files_parseDictionary(&hfuzz) == false)) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200145 LOG_F("Couldn't parse dictionary file ('%s')", hfuzz.dictionaryFile);
robert.swiecki@gmail.com4f1124f2015-04-21 17:12:22 +0000146 }
147
Anestis Bechtsoudisd59af692015-09-21 15:15:05 +0300148 if (hfuzz.blacklistFile && (files_parseBlacklist(&hfuzz) == false)) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200149 LOG_F("Couldn't parse stackhash blacklist file ('%s')", hfuzz.blacklistFile);
Anestis Bechtsoudisd59af692015-09-21 15:15:05 +0300150 }
Anestis Bechtsoudis00be1892016-10-31 09:29:44 +0200151#define hfuzzl hfuzz.linux
Anestis Bechtsoudis3dce6ac2016-10-31 08:51:14 +0200152 if (hfuzzl.symsBlFile &&
153 ((hfuzzl.symsBlCnt = files_parseSymbolFilter(hfuzzl.symsBlFile, &hfuzzl.symsBl)) == 0)) {
154 LOG_F("Couldn't parse symbols blacklist file ('%s')", hfuzzl.symsBlFile);
Anestis Bechtsoudisba68b382016-10-29 20:44:15 +0300155 }
156
Anestis Bechtsoudis3dce6ac2016-10-31 08:51:14 +0200157 if (hfuzzl.symsWlFile &&
158 ((hfuzzl.symsWlCnt = files_parseSymbolFilter(hfuzzl.symsWlFile, &hfuzzl.symsWl)) == 0)) {
159 LOG_F("Couldn't parse symbols whitelist file ('%s')", hfuzzl.symsWlFile);
Anestis Bechtsoudisba68b382016-10-29 20:44:15 +0300160 }
Anestis Bechtsoudis00be1892016-10-31 09:29:44 +0200161
Robert Swieckie4d63d42016-11-01 00:40:51 +0100162 if (hfuzz.dynFileMethod != _HF_DYNFILE_NONE) {
163 hfuzz.feedback = files_mapSharedMem(sizeof(feedback_t), &hfuzz.bbFd, hfuzz.workDir);
164 if (hfuzz.feedback == MAP_FAILED) {
165 LOG_F("files_mapSharedMem(sz=%zu, dir='%s') failed", sizeof(feedback_t), hfuzz.workDir);
166 }
167 }
168
robert.swiecki3bb518c2010-10-14 00:48:24 +0000169 /*
170 * So far so good
171 */
Robert Swiecki37778e02016-03-15 15:45:28 +0100172 setupSignalsPreThr();
Jagger08816fd2016-03-11 01:32:38 +0100173 fuzz_threads(&hfuzz);
174 setupSignalsPostThr();
robert.swiecki3bb518c2010-10-14 00:48:24 +0000175
Robert Swiecki37778e02016-03-15 15:45:28 +0100176 setupTimer();
Jagger08816fd2016-03-11 01:32:38 +0100177 for (;;) {
178 if (hfuzz.useScreen) {
179 display_display(&hfuzz);
180 }
181 if (sigReceived > 0) {
182 break;
183 }
Jaggerd34417d2016-03-16 01:26:54 +0100184 if (ATOMIC_GET(hfuzz.threadsFinished) >= hfuzz.threadsMax) {
Jagger08816fd2016-03-11 01:32:38 +0100185 break;
186 }
187 pause();
188 }
189
190 if (sigReceived > 0) {
191 LOG_I("Signal %d (%s) received, terminating", sigReceived, strsignal(sigReceived));
Jagger4f97d772016-07-20 20:55:51 +0200192 return EXIT_SUCCESS;
Jagger08816fd2016-03-11 01:32:38 +0100193 }
194
195 /* Clean-up global buffers */
Jagger08816fd2016-03-11 01:32:38 +0100196 if (hfuzz.blacklist) {
197 free(hfuzz.blacklist);
198 }
Anestis Bechtsoudisba68b382016-10-29 20:44:15 +0300199 if (hfuzz.linux.symsBl) {
200 free(hfuzz.linux.symsBl);
201 }
202 if (hfuzz.linux.symsWl) {
203 free(hfuzz.linux.symsWl);
204 }
Jagger08816fd2016-03-11 01:32:38 +0100205 if (hfuzz.sanOpts.asanOpts) {
206 free(hfuzz.sanOpts.asanOpts);
207 }
208 if (hfuzz.sanOpts.ubsanOpts) {
209 free(hfuzz.sanOpts.ubsanOpts);
210 }
211 if (hfuzz.sanOpts.msanOpts) {
212 free(hfuzz.sanOpts.msanOpts);
213 }
Jagger247c3b42016-03-21 23:24:05 +0100214 if (hfuzz.linux.pidCmd) {
215 free(hfuzz.linux.pidCmd);
Jagger08816fd2016-03-11 01:32:38 +0100216 }
217
Robert Swiecki37778e02016-03-15 15:45:28 +0100218 return EXIT_SUCCESS;
robert.swiecki3bb518c2010-10-14 00:48:24 +0000219}