blob: 2925d4fc45301cb455e3f186d8d43c13f1257952 [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},
Jagger08816fd2016-03-11 01:32:38 +010065 .it_interval = {.tv_sec = 1,.tv_usec = 0},
66 };
67 if (setitimer(ITIMER_REAL, &it, NULL) == -1) {
68 PLOG_F("setitimer(ITIMER_REAL)");
69 }
70}
71
72static void setupSignalsPreThr(void)
73{
Jagger090de222016-04-16 20:05:13 +020074 /* Block signals which should be handled by 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);
Jagger08816fd2016-03-11 01:32:38 +010081 if (sigprocmask(SIG_BLOCK, &ss, NULL) != 0) {
82 PLOG_F("pthread_sigmask(SIG_BLOCK)");
83 }
Jagger848c4fc2016-03-11 01:34:53 +010084}
Jagger08816fd2016-03-11 01:32:38 +010085
Jagger848c4fc2016-03-11 01:34:53 +010086static void setupSignalsPostThr(void)
87{
Jagger08816fd2016-03-11 01:32:38 +010088 struct sigaction sa = {
89 .sa_handler = sigHandler,
90 .sa_flags = 0,
91 };
92 sigemptyset(&sa.sa_mask);
93 if (sigaction(SIGTERM, &sa, NULL) == -1) {
94 PLOG_F("sigaction(SIGTERM) failed");
95 }
96 if (sigaction(SIGINT, &sa, NULL) == -1) {
97 PLOG_F("sigaction(SIGINT) failed");
98 }
99 if (sigaction(SIGQUIT, &sa, NULL) == -1) {
100 PLOG_F("sigaction(SIGQUIT) failed");
101 }
Jagger33cce5d2016-04-16 20:10:36 +0200102 if (sigaction(SIGALRM, &sa, NULL) == -1) {
103 PLOG_F("sigaction(SIGQUIT) failed");
104 }
Jagger08816fd2016-03-11 01:32:38 +0100105 /* Unblock signals which should be handled by the main thread */
106 sigset_t ss;
107 sigemptyset(&ss);
108 sigaddset(&ss, SIGTERM);
109 sigaddset(&ss, SIGINT);
110 sigaddset(&ss, SIGQUIT);
Jagger3846dfc2016-04-16 20:06:16 +0200111 sigaddset(&ss, SIGALRM);
Jagger08816fd2016-03-11 01:32:38 +0100112 if (sigprocmask(SIG_UNBLOCK, &ss, NULL) != 0) {
Jagger33cce5d2016-04-16 20:10:36 +0200113 PLOG_F("pthread_sigmask(SIG_UNBLOCK)");
Jagger08816fd2016-03-11 01:32:38 +0100114 }
115}
116
robert.swiecki3bb518c2010-10-14 00:48:24 +0000117int main(int argc, char **argv)
118{
Robert Swieckic6a48b12016-03-11 17:41:57 +0100119 /*
120 * Work around CygWin/MinGW
121 */
Robert Swiecki37778e02016-03-15 15:45:28 +0100122 char **myargs = (char **)util_Malloc(sizeof(char *) * (argc + 1));
Anestis Bechtsoudisa4198732016-04-27 12:27:39 +0300123 defer {
124 free(myargs);
125 };
Jagger1ebc6dc2016-03-12 01:39:09 +0100126
127 int i;
128 for (i = 0U; i < argc; i++) {
Robert Swieckic6a48b12016-03-11 17:41:57 +0100129 myargs[i] = argv[i];
130 }
131 myargs[i] = NULL;
132
133 if (cmdlineParse(argc, myargs, &hfuzz) == false) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200134 LOG_F("Parsing of the cmd-line arguments failed");
robert.swiecki3bb518c2010-10-14 00:48:24 +0000135 }
136
robert.swiecki3bb518c2010-10-14 00:48:24 +0000137 if (!files_init(&hfuzz)) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200138 LOG_F("Couldn't load input files");
robert.swiecki3bb518c2010-10-14 00:48:24 +0000139 exit(EXIT_FAILURE);
140 }
141
robert.swiecki@gmail.com4f1124f2015-04-21 17:12:22 +0000142 if (hfuzz.dictionaryFile && (files_parseDictionary(&hfuzz) == false)) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200143 LOG_F("Couldn't parse dictionary file ('%s')", hfuzz.dictionaryFile);
robert.swiecki@gmail.com4f1124f2015-04-21 17:12:22 +0000144 }
145
Anestis Bechtsoudisd59af692015-09-21 15:15:05 +0300146 if (hfuzz.blacklistFile && (files_parseBlacklist(&hfuzz) == false)) {
Robert Swieckic8c32db2015-10-09 18:06:22 +0200147 LOG_F("Couldn't parse stackhash blacklist file ('%s')", hfuzz.blacklistFile);
Anestis Bechtsoudisd59af692015-09-21 15:15:05 +0300148 }
149
robert.swiecki3bb518c2010-10-14 00:48:24 +0000150 /*
151 * So far so good
152 */
Robert Swiecki37778e02016-03-15 15:45:28 +0100153 setupSignalsPreThr();
Jagger08816fd2016-03-11 01:32:38 +0100154 fuzz_threads(&hfuzz);
155 setupSignalsPostThr();
robert.swiecki3bb518c2010-10-14 00:48:24 +0000156
Robert Swiecki37778e02016-03-15 15:45:28 +0100157 setupTimer();
Jagger08816fd2016-03-11 01:32:38 +0100158 for (;;) {
159 if (hfuzz.useScreen) {
160 display_display(&hfuzz);
161 }
162 if (sigReceived > 0) {
163 break;
164 }
Jaggerd34417d2016-03-16 01:26:54 +0100165 if (ATOMIC_GET(hfuzz.threadsFinished) >= hfuzz.threadsMax) {
Jagger08816fd2016-03-11 01:32:38 +0100166 break;
167 }
168 pause();
169 }
170
171 if (sigReceived > 0) {
172 LOG_I("Signal %d (%s) received, terminating", sigReceived, strsignal(sigReceived));
173 }
174
175 /* Clean-up global buffers */
176 free(hfuzz.files);
Jagger08816fd2016-03-11 01:32:38 +0100177 if (hfuzz.dictionary) {
178 for (size_t i = 0; i < hfuzz.dictionaryCnt; i++) {
179 free(hfuzz.dictionary[i]);
180 }
181 free(hfuzz.dictionary);
182 }
183 if (hfuzz.blacklist) {
184 free(hfuzz.blacklist);
185 }
186 if (hfuzz.sanOpts.asanOpts) {
187 free(hfuzz.sanOpts.asanOpts);
188 }
189 if (hfuzz.sanOpts.ubsanOpts) {
190 free(hfuzz.sanOpts.ubsanOpts);
191 }
192 if (hfuzz.sanOpts.msanOpts) {
193 free(hfuzz.sanOpts.msanOpts);
194 }
Jagger247c3b42016-03-21 23:24:05 +0100195 if (hfuzz.linux.pidCmd) {
196 free(hfuzz.linux.pidCmd);
Jagger08816fd2016-03-11 01:32:38 +0100197 }
198
Robert Swiecki37778e02016-03-15 15:45:28 +0100199 return EXIT_SUCCESS;
robert.swiecki3bb518c2010-10-14 00:48:24 +0000200}