blob: 09484e3b870367d0478ab82cd73dff64676fe52a [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
Robert Swiecki8d01b012017-02-19 15:48:11 +010058 if (ATOMIC_GET(sigReceived) != 0) {
59 static const char *const sigMsg = "Repeated termination signal caugth\n";
60 if (write(STDERR_FILENO, sigMsg, strlen(sigMsg) + 1) == -1) {
61 };
62 _exit(EXIT_FAILURE);
63 }
64
65 ATOMIC_SET(sigReceived, sig);
Jagger08816fd2016-03-11 01:32:38 +010066}
67
68static void setupTimer(void)
69{
70 struct itimerval it = {
Robert Swiecki37778e02016-03-15 15:45:28 +010071 .it_value = {.tv_sec = 1,.tv_usec = 0},
Robert Swiecki05783bb2016-07-26 15:43:48 +020072 .it_interval = {.tv_sec = 1,.tv_usec = 0},
Jagger08816fd2016-03-11 01:32:38 +010073 };
74 if (setitimer(ITIMER_REAL, &it, NULL) == -1) {
75 PLOG_F("setitimer(ITIMER_REAL)");
76 }
77}
78
79static void setupSignalsPreThr(void)
80{
Jaggerfbd4ae12016-09-02 02:26:02 +020081 /* Block signals which should be handled or blocked in the main thread */
Jagger33cce5d2016-04-16 20:10:36 +020082 sigset_t ss;
Jagger090de222016-04-16 20:05:13 +020083 sigemptyset(&ss);
Jagger08816fd2016-03-11 01:32:38 +010084 sigaddset(&ss, SIGTERM);
85 sigaddset(&ss, SIGINT);
86 sigaddset(&ss, SIGQUIT);
Jagger33cce5d2016-04-16 20:10:36 +020087 sigaddset(&ss, SIGALRM);
Jaggerac75f262016-08-20 12:38:20 +020088 sigaddset(&ss, SIGPIPE);
Jaggerfbd4ae12016-09-02 02:26:02 +020089 sigaddset(&ss, SIGIO);
Jagger08816fd2016-03-11 01:32:38 +010090 if (sigprocmask(SIG_BLOCK, &ss, NULL) != 0) {
91 PLOG_F("pthread_sigmask(SIG_BLOCK)");
92 }
Jagger848c4fc2016-03-11 01:34:53 +010093}
Jagger08816fd2016-03-11 01:32:38 +010094
Jagger848c4fc2016-03-11 01:34:53 +010095static void setupSignalsPostThr(void)
96{
Jagger08816fd2016-03-11 01:32:38 +010097 struct sigaction sa = {
98 .sa_handler = sigHandler,
99 .sa_flags = 0,
100 };
101 sigemptyset(&sa.sa_mask);
102 if (sigaction(SIGTERM, &sa, NULL) == -1) {
103 PLOG_F("sigaction(SIGTERM) failed");
104 }
105 if (sigaction(SIGINT, &sa, NULL) == -1) {
106 PLOG_F("sigaction(SIGINT) failed");
107 }
108 if (sigaction(SIGQUIT, &sa, NULL) == -1) {
109 PLOG_F("sigaction(SIGQUIT) failed");
110 }
Jagger33cce5d2016-04-16 20:10:36 +0200111 if (sigaction(SIGALRM, &sa, NULL) == -1) {
112 PLOG_F("sigaction(SIGQUIT) failed");
113 }
Jagger08816fd2016-03-11 01:32:38 +0100114 /* Unblock signals which should be handled by the main thread */
115 sigset_t ss;
116 sigemptyset(&ss);
117 sigaddset(&ss, SIGTERM);
118 sigaddset(&ss, SIGINT);
119 sigaddset(&ss, SIGQUIT);
Jagger3846dfc2016-04-16 20:06:16 +0200120 sigaddset(&ss, SIGALRM);
Jagger08816fd2016-03-11 01:32:38 +0100121 if (sigprocmask(SIG_UNBLOCK, &ss, NULL) != 0) {
Jagger33cce5d2016-04-16 20:10:36 +0200122 PLOG_F("pthread_sigmask(SIG_UNBLOCK)");
Jagger08816fd2016-03-11 01:32:38 +0100123 }
124}
125
robert.swiecki3bb518c2010-10-14 00:48:24 +0000126int main(int argc, char **argv)
127{
Robert Swieckic6a48b12016-03-11 17:41:57 +0100128 /*
129 * Work around CygWin/MinGW
130 */
Robert Swiecki37778e02016-03-15 15:45:28 +0100131 char **myargs = (char **)util_Malloc(sizeof(char *) * (argc + 1));
Anestis Bechtsoudisa4198732016-04-27 12:27:39 +0300132 defer {
133 free(myargs);
134 };
Jagger1ebc6dc2016-03-12 01:39:09 +0100135
136 int i;
137 for (i = 0U; i < argc; i++) {
Robert Swieckic6a48b12016-03-11 17:41:57 +0100138 myargs[i] = argv[i];
139 }
140 myargs[i] = NULL;
141
142 if (cmdlineParse(argc, myargs, &hfuzz) == false) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200143 LOG_F("Parsing of the cmd-line arguments failed");
robert.swiecki3bb518c2010-10-14 00:48:24 +0000144 }
145
Robert Swiecki1f1ce172017-02-10 03:43:00 +0100146 if (hfuzz.useScreen) {
147 display_init();
148 }
149
robert.swiecki3bb518c2010-10-14 00:48:24 +0000150 if (!files_init(&hfuzz)) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200151 LOG_F("Couldn't load input files");
robert.swiecki3bb518c2010-10-14 00:48:24 +0000152 exit(EXIT_FAILURE);
153 }
154
robert.swiecki@gmail.com4f1124f2015-04-21 17:12:22 +0000155 if (hfuzz.dictionaryFile && (files_parseDictionary(&hfuzz) == false)) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200156 LOG_F("Couldn't parse dictionary file ('%s')", hfuzz.dictionaryFile);
robert.swiecki@gmail.com4f1124f2015-04-21 17:12:22 +0000157 }
158
Anestis Bechtsoudisd59af692015-09-21 15:15:05 +0300159 if (hfuzz.blacklistFile && (files_parseBlacklist(&hfuzz) == false)) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200160 LOG_F("Couldn't parse stackhash blacklist file ('%s')", hfuzz.blacklistFile);
Anestis Bechtsoudisd59af692015-09-21 15:15:05 +0300161 }
Anestis Bechtsoudis00be1892016-10-31 09:29:44 +0200162#define hfuzzl hfuzz.linux
Anestis Bechtsoudis3dce6ac2016-10-31 08:51:14 +0200163 if (hfuzzl.symsBlFile &&
164 ((hfuzzl.symsBlCnt = files_parseSymbolFilter(hfuzzl.symsBlFile, &hfuzzl.symsBl)) == 0)) {
165 LOG_F("Couldn't parse symbols blacklist file ('%s')", hfuzzl.symsBlFile);
Anestis Bechtsoudisba68b382016-10-29 20:44:15 +0300166 }
167
Anestis Bechtsoudis3dce6ac2016-10-31 08:51:14 +0200168 if (hfuzzl.symsWlFile &&
169 ((hfuzzl.symsWlCnt = files_parseSymbolFilter(hfuzzl.symsWlFile, &hfuzzl.symsWl)) == 0)) {
170 LOG_F("Couldn't parse symbols whitelist file ('%s')", hfuzzl.symsWlFile);
Anestis Bechtsoudisba68b382016-10-29 20:44:15 +0300171 }
Anestis Bechtsoudis00be1892016-10-31 09:29:44 +0200172
Robert Swieckie4d63d42016-11-01 00:40:51 +0100173 if (hfuzz.dynFileMethod != _HF_DYNFILE_NONE) {
174 hfuzz.feedback = files_mapSharedMem(sizeof(feedback_t), &hfuzz.bbFd, hfuzz.workDir);
175 if (hfuzz.feedback == MAP_FAILED) {
176 LOG_F("files_mapSharedMem(sz=%zu, dir='%s') failed", sizeof(feedback_t), hfuzz.workDir);
177 }
178 }
179
robert.swiecki3bb518c2010-10-14 00:48:24 +0000180 /*
181 * So far so good
182 */
Robert Swiecki33fb2842017-02-19 05:39:50 +0100183 pthread_t threads[hfuzz.threadsMax];
184
Robert Swiecki37778e02016-03-15 15:45:28 +0100185 setupSignalsPreThr();
Robert Swiecki33fb2842017-02-19 05:39:50 +0100186 fuzz_threadsStart(&hfuzz, threads);
Jagger08816fd2016-03-11 01:32:38 +0100187 setupSignalsPostThr();
robert.swiecki3bb518c2010-10-14 00:48:24 +0000188
Robert Swiecki37778e02016-03-15 15:45:28 +0100189 setupTimer();
Jagger08816fd2016-03-11 01:32:38 +0100190 for (;;) {
191 if (hfuzz.useScreen) {
192 display_display(&hfuzz);
193 }
Robert Swiecki8d01b012017-02-19 15:48:11 +0100194 if (ATOMIC_GET(sigReceived) > 0) {
Jagger08816fd2016-03-11 01:32:38 +0100195 break;
196 }
Jaggerd34417d2016-03-16 01:26:54 +0100197 if (ATOMIC_GET(hfuzz.threadsFinished) >= hfuzz.threadsMax) {
Jagger08816fd2016-03-11 01:32:38 +0100198 break;
199 }
200 pause();
201 }
202
Robert Swiecki98b10562017-02-09 23:58:13 +0100203 if (hfuzz.useScreen) {
Robert Swiecki1f1ce172017-02-10 03:43:00 +0100204 display_fini();
Robert Swiecki98b10562017-02-09 23:58:13 +0100205 }
206
Robert Swiecki8d01b012017-02-19 15:48:11 +0100207 if (ATOMIC_GET(sigReceived) > 0) {
208 LOG_I("Signal %d (%s) received, terminating", ATOMIC_GET(sigReceived),
209 strsignal(ATOMIC_GET(sigReceived)));
210 ATOMIC_SET(hfuzz.terminating, true);
Jagger08816fd2016-03-11 01:32:38 +0100211 }
212
Robert Swiecki33fb2842017-02-19 05:39:50 +0100213 fuzz_threadsStop(&hfuzz, threads);
214
Jagger08816fd2016-03-11 01:32:38 +0100215 /* Clean-up global buffers */
Jagger08816fd2016-03-11 01:32:38 +0100216 if (hfuzz.blacklist) {
217 free(hfuzz.blacklist);
218 }
Anestis Bechtsoudisba68b382016-10-29 20:44:15 +0300219 if (hfuzz.linux.symsBl) {
220 free(hfuzz.linux.symsBl);
221 }
222 if (hfuzz.linux.symsWl) {
223 free(hfuzz.linux.symsWl);
224 }
Jagger08816fd2016-03-11 01:32:38 +0100225 if (hfuzz.sanOpts.asanOpts) {
226 free(hfuzz.sanOpts.asanOpts);
227 }
228 if (hfuzz.sanOpts.ubsanOpts) {
229 free(hfuzz.sanOpts.ubsanOpts);
230 }
231 if (hfuzz.sanOpts.msanOpts) {
232 free(hfuzz.sanOpts.msanOpts);
233 }
Jagger247c3b42016-03-21 23:24:05 +0100234 if (hfuzz.linux.pidCmd) {
235 free(hfuzz.linux.pidCmd);
Jagger08816fd2016-03-11 01:32:38 +0100236 }
237
Robert Swiecki37778e02016-03-15 15:45:28 +0100238 return EXIT_SUCCESS;
robert.swiecki3bb518c2010-10-14 00:48:24 +0000239}