blob: f930f91e4a9c00ebf757537fb04afc3a0dbdd8b6 [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 }
151
Jagger4aac9fe2016-08-28 17:35:48 +0200152 if (hfuzz.dynFileMethod != _HF_DYNFILE_NONE) {
Jagger5b81a502016-09-07 20:48:32 +0200153 hfuzz.feedback = files_mapSharedMem(sizeof(feedback_t), &hfuzz.bbFd, hfuzz.workDir);
Jagger4aac9fe2016-08-28 17:35:48 +0200154 if (hfuzz.feedback == MAP_FAILED) {
Jagger5b81a502016-09-07 20:48:32 +0200155 LOG_F("files_mapSharedMem(sz=%zu, dir='%s') failed", sizeof(feedback_t), hfuzz.workDir);
Jagger4aac9fe2016-08-28 17:35:48 +0200156 }
157 }
158
robert.swiecki3bb518c2010-10-14 00:48:24 +0000159 /*
160 * So far so good
161 */
Robert Swiecki37778e02016-03-15 15:45:28 +0100162 setupSignalsPreThr();
Jagger08816fd2016-03-11 01:32:38 +0100163 fuzz_threads(&hfuzz);
164 setupSignalsPostThr();
robert.swiecki3bb518c2010-10-14 00:48:24 +0000165
Robert Swiecki37778e02016-03-15 15:45:28 +0100166 setupTimer();
Jagger08816fd2016-03-11 01:32:38 +0100167 for (;;) {
168 if (hfuzz.useScreen) {
169 display_display(&hfuzz);
170 }
171 if (sigReceived > 0) {
172 break;
173 }
Jaggerd34417d2016-03-16 01:26:54 +0100174 if (ATOMIC_GET(hfuzz.threadsFinished) >= hfuzz.threadsMax) {
Jagger08816fd2016-03-11 01:32:38 +0100175 break;
176 }
177 pause();
178 }
179
180 if (sigReceived > 0) {
181 LOG_I("Signal %d (%s) received, terminating", sigReceived, strsignal(sigReceived));
Jagger4f97d772016-07-20 20:55:51 +0200182 return EXIT_SUCCESS;
Jagger08816fd2016-03-11 01:32:38 +0100183 }
184
185 /* Clean-up global buffers */
Jagger08816fd2016-03-11 01:32:38 +0100186 if (hfuzz.blacklist) {
187 free(hfuzz.blacklist);
188 }
189 if (hfuzz.sanOpts.asanOpts) {
190 free(hfuzz.sanOpts.asanOpts);
191 }
192 if (hfuzz.sanOpts.ubsanOpts) {
193 free(hfuzz.sanOpts.ubsanOpts);
194 }
195 if (hfuzz.sanOpts.msanOpts) {
196 free(hfuzz.sanOpts.msanOpts);
197 }
Jagger247c3b42016-03-21 23:24:05 +0100198 if (hfuzz.linux.pidCmd) {
199 free(hfuzz.linux.pidCmd);
Jagger08816fd2016-03-11 01:32:38 +0100200 }
201
Robert Swiecki37778e02016-03-15 15:45:28 +0100202 return EXIT_SUCCESS;
robert.swiecki3bb518c2010-10-14 00:48:24 +0000203}