George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 1 | //===- FuzzerDriver.cpp - FuzzerDriver function and flags -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // FuzzerDriver and flag parsing. |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
Matt Morehouse | 04304d1 | 2017-12-04 19:25:59 | [diff] [blame] | 12 | #include "FuzzerCommand.h" |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 13 | #include "FuzzerCorpus.h" |
| 14 | #include "FuzzerIO.h" |
| 15 | #include "FuzzerInterface.h" |
| 16 | #include "FuzzerInternal.h" |
| 17 | #include "FuzzerMutate.h" |
| 18 | #include "FuzzerRandom.h" |
| 19 | #include "FuzzerShmem.h" |
| 20 | #include "FuzzerTracePC.h" |
| 21 | #include <algorithm> |
| 22 | #include <atomic> |
| 23 | #include <chrono> |
| 24 | #include <cstdlib> |
| 25 | #include <cstring> |
| 26 | #include <mutex> |
| 27 | #include <string> |
| 28 | #include <thread> |
| 29 | |
| 30 | // This function should be present in the libFuzzer so that the client |
| 31 | // binary can test for its existence. |
Jonathan Metzman | b795c31 | 2019-01-17 16:36:05 | [diff] [blame^] | 32 | #if LIBFUZZER_MSVC |
| 33 | extern "C" void __libfuzzer_is_present() {} |
| 34 | #pragma comment(linker, "/include:__libfuzzer_is_present") |
| 35 | #else |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 36 | extern "C" __attribute__((used)) void __libfuzzer_is_present() {} |
Jonathan Metzman | b795c31 | 2019-01-17 16:36:05 | [diff] [blame^] | 37 | #endif // LIBFUZZER_MSVC |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 38 | |
| 39 | namespace fuzzer { |
| 40 | |
| 41 | // Program arguments. |
| 42 | struct FlagDescription { |
| 43 | const char *Name; |
| 44 | const char *Description; |
| 45 | int Default; |
| 46 | int *IntFlag; |
| 47 | const char **StrFlag; |
| 48 | unsigned int *UIntFlag; |
| 49 | }; |
| 50 | |
| 51 | struct { |
| 52 | #define FUZZER_DEPRECATED_FLAG(Name) |
| 53 | #define FUZZER_FLAG_INT(Name, Default, Description) int Name; |
| 54 | #define FUZZER_FLAG_UNSIGNED(Name, Default, Description) unsigned int Name; |
| 55 | #define FUZZER_FLAG_STRING(Name, Description) const char *Name; |
| 56 | #include "FuzzerFlags.def" |
| 57 | #undef FUZZER_DEPRECATED_FLAG |
| 58 | #undef FUZZER_FLAG_INT |
| 59 | #undef FUZZER_FLAG_UNSIGNED |
| 60 | #undef FUZZER_FLAG_STRING |
| 61 | } Flags; |
| 62 | |
| 63 | static const FlagDescription FlagDescriptions [] { |
| 64 | #define FUZZER_DEPRECATED_FLAG(Name) \ |
| 65 | {#Name, "Deprecated; don't use", 0, nullptr, nullptr, nullptr}, |
| 66 | #define FUZZER_FLAG_INT(Name, Default, Description) \ |
| 67 | {#Name, Description, Default, &Flags.Name, nullptr, nullptr}, |
| 68 | #define FUZZER_FLAG_UNSIGNED(Name, Default, Description) \ |
| 69 | {#Name, Description, static_cast<int>(Default), \ |
| 70 | nullptr, nullptr, &Flags.Name}, |
| 71 | #define FUZZER_FLAG_STRING(Name, Description) \ |
| 72 | {#Name, Description, 0, nullptr, &Flags.Name, nullptr}, |
| 73 | #include "FuzzerFlags.def" |
| 74 | #undef FUZZER_DEPRECATED_FLAG |
| 75 | #undef FUZZER_FLAG_INT |
| 76 | #undef FUZZER_FLAG_UNSIGNED |
| 77 | #undef FUZZER_FLAG_STRING |
| 78 | }; |
| 79 | |
| 80 | static const size_t kNumFlags = |
| 81 | sizeof(FlagDescriptions) / sizeof(FlagDescriptions[0]); |
| 82 | |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 83 | static Vector<std::string> *Inputs; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 84 | static std::string *ProgName; |
| 85 | |
| 86 | static void PrintHelp() { |
| 87 | Printf("Usage:\n"); |
| 88 | auto Prog = ProgName->c_str(); |
| 89 | Printf("\nTo run fuzzing pass 0 or more directories.\n"); |
| 90 | Printf("%s [-flag1=val1 [-flag2=val2 ...] ] [dir1 [dir2 ...] ]\n", Prog); |
| 91 | |
| 92 | Printf("\nTo run individual tests without fuzzing pass 1 or more files:\n"); |
| 93 | Printf("%s [-flag1=val1 [-flag2=val2 ...] ] file1 [file2 ...]\n", Prog); |
| 94 | |
| 95 | Printf("\nFlags: (strictly in form -flag=value)\n"); |
| 96 | size_t MaxFlagLen = 0; |
| 97 | for (size_t F = 0; F < kNumFlags; F++) |
| 98 | MaxFlagLen = std::max(strlen(FlagDescriptions[F].Name), MaxFlagLen); |
| 99 | |
| 100 | for (size_t F = 0; F < kNumFlags; F++) { |
| 101 | const auto &D = FlagDescriptions[F]; |
| 102 | if (strstr(D.Description, "internal flag") == D.Description) continue; |
| 103 | Printf(" %s", D.Name); |
| 104 | for (size_t i = 0, n = MaxFlagLen - strlen(D.Name); i < n; i++) |
| 105 | Printf(" "); |
| 106 | Printf("\t"); |
| 107 | Printf("%d\t%s\n", D.Default, D.Description); |
| 108 | } |
| 109 | Printf("\nFlags starting with '--' will be ignored and " |
Max Moroz | 1d369a5 | 2018-07-16 15:15:34 | [diff] [blame] | 110 | "will be passed verbatim to subprocesses.\n"); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static const char *FlagValue(const char *Param, const char *Name) { |
| 114 | size_t Len = strlen(Name); |
| 115 | if (Param[0] == '-' && strstr(Param + 1, Name) == Param + 1 && |
| 116 | Param[Len + 1] == '=') |
Max Moroz | 1d369a5 | 2018-07-16 15:15:34 | [diff] [blame] | 117 | return &Param[Len + 2]; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 118 | return nullptr; |
| 119 | } |
| 120 | |
| 121 | // Avoid calling stol as it triggers a bug in clang/glibc build. |
| 122 | static long MyStol(const char *Str) { |
| 123 | long Res = 0; |
| 124 | long Sign = 1; |
| 125 | if (*Str == '-') { |
| 126 | Str++; |
| 127 | Sign = -1; |
| 128 | } |
| 129 | for (size_t i = 0; Str[i]; i++) { |
| 130 | char Ch = Str[i]; |
| 131 | if (Ch < '0' || Ch > '9') |
| 132 | return Res; |
| 133 | Res = Res * 10 + (Ch - '0'); |
| 134 | } |
| 135 | return Res * Sign; |
| 136 | } |
| 137 | |
| 138 | static bool ParseOneFlag(const char *Param) { |
| 139 | if (Param[0] != '-') return false; |
| 140 | if (Param[1] == '-') { |
| 141 | static bool PrintedWarning = false; |
| 142 | if (!PrintedWarning) { |
| 143 | PrintedWarning = true; |
| 144 | Printf("INFO: libFuzzer ignores flags that start with '--'\n"); |
| 145 | } |
| 146 | for (size_t F = 0; F < kNumFlags; F++) |
| 147 | if (FlagValue(Param + 1, FlagDescriptions[F].Name)) |
| 148 | Printf("WARNING: did you mean '%s' (single dash)?\n", Param + 1); |
| 149 | return true; |
| 150 | } |
| 151 | for (size_t F = 0; F < kNumFlags; F++) { |
| 152 | const char *Name = FlagDescriptions[F].Name; |
| 153 | const char *Str = FlagValue(Param, Name); |
| 154 | if (Str) { |
| 155 | if (FlagDescriptions[F].IntFlag) { |
| 156 | int Val = MyStol(Str); |
| 157 | *FlagDescriptions[F].IntFlag = Val; |
| 158 | if (Flags.verbosity >= 2) |
| 159 | Printf("Flag: %s %d\n", Name, Val); |
| 160 | return true; |
| 161 | } else if (FlagDescriptions[F].UIntFlag) { |
| 162 | unsigned int Val = std::stoul(Str); |
| 163 | *FlagDescriptions[F].UIntFlag = Val; |
| 164 | if (Flags.verbosity >= 2) |
| 165 | Printf("Flag: %s %u\n", Name, Val); |
| 166 | return true; |
| 167 | } else if (FlagDescriptions[F].StrFlag) { |
| 168 | *FlagDescriptions[F].StrFlag = Str; |
| 169 | if (Flags.verbosity >= 2) |
| 170 | Printf("Flag: %s %s\n", Name, Str); |
| 171 | return true; |
| 172 | } else { // Deprecated flag. |
| 173 | Printf("Flag: %s: deprecated, don't use\n", Name); |
| 174 | return true; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | Printf("\n\nWARNING: unrecognized flag '%s'; " |
| 179 | "use -help=1 to list all flags\n\n", Param); |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | // We don't use any library to minimize dependencies. |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 184 | static void ParseFlags(const Vector<std::string> &Args) { |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 185 | for (size_t F = 0; F < kNumFlags; F++) { |
| 186 | if (FlagDescriptions[F].IntFlag) |
| 187 | *FlagDescriptions[F].IntFlag = FlagDescriptions[F].Default; |
| 188 | if (FlagDescriptions[F].UIntFlag) |
| 189 | *FlagDescriptions[F].UIntFlag = |
| 190 | static_cast<unsigned int>(FlagDescriptions[F].Default); |
| 191 | if (FlagDescriptions[F].StrFlag) |
| 192 | *FlagDescriptions[F].StrFlag = nullptr; |
| 193 | } |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 194 | Inputs = new Vector<std::string>; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 195 | for (size_t A = 1; A < Args.size(); A++) { |
| 196 | if (ParseOneFlag(Args[A].c_str())) { |
| 197 | if (Flags.ignore_remaining_args) |
| 198 | break; |
| 199 | continue; |
| 200 | } |
| 201 | Inputs->push_back(Args[A]); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | static std::mutex Mu; |
| 206 | |
| 207 | static void PulseThread() { |
| 208 | while (true) { |
| 209 | SleepSeconds(600); |
| 210 | std::lock_guard<std::mutex> Lock(Mu); |
| 211 | Printf("pulse...\n"); |
| 212 | } |
| 213 | } |
| 214 | |
Matt Morehouse | 04304d1 | 2017-12-04 19:25:59 | [diff] [blame] | 215 | static void WorkerThread(const Command &BaseCmd, std::atomic<unsigned> *Counter, |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 216 | unsigned NumJobs, std::atomic<bool> *HasErrors) { |
| 217 | while (true) { |
| 218 | unsigned C = (*Counter)++; |
| 219 | if (C >= NumJobs) break; |
| 220 | std::string Log = "fuzz-" + std::to_string(C) + ".log"; |
Matt Morehouse | 04304d1 | 2017-12-04 19:25:59 | [diff] [blame] | 221 | Command Cmd(BaseCmd); |
| 222 | Cmd.setOutputFile(Log); |
| 223 | Cmd.combineOutAndErr(); |
| 224 | if (Flags.verbosity) { |
| 225 | std::string CommandLine = Cmd.toString(); |
Kostya Serebryany | 7ac58ee | 2017-12-06 22:12:24 | [diff] [blame] | 226 | Printf("%s\n", CommandLine.c_str()); |
Matt Morehouse | 04304d1 | 2017-12-04 19:25:59 | [diff] [blame] | 227 | } |
| 228 | int ExitCode = ExecuteCommand(Cmd); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 229 | if (ExitCode != 0) |
| 230 | *HasErrors = true; |
| 231 | std::lock_guard<std::mutex> Lock(Mu); |
| 232 | Printf("================== Job %u exited with exit code %d ============\n", |
| 233 | C, ExitCode); |
| 234 | fuzzer::CopyFileToErr(Log); |
| 235 | } |
| 236 | } |
| 237 | |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 238 | std::string CloneArgsWithoutX(const Vector<std::string> &Args, |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 239 | const char *X1, const char *X2) { |
| 240 | std::string Cmd; |
| 241 | for (auto &S : Args) { |
| 242 | if (FlagValue(S.c_str(), X1) || FlagValue(S.c_str(), X2)) |
| 243 | continue; |
| 244 | Cmd += S + " "; |
| 245 | } |
| 246 | return Cmd; |
| 247 | } |
| 248 | |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 249 | static int RunInMultipleProcesses(const Vector<std::string> &Args, |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 250 | unsigned NumWorkers, unsigned NumJobs) { |
| 251 | std::atomic<unsigned> Counter(0); |
| 252 | std::atomic<bool> HasErrors(false); |
Matt Morehouse | 04304d1 | 2017-12-04 19:25:59 | [diff] [blame] | 253 | Command Cmd(Args); |
| 254 | Cmd.removeFlag("jobs"); |
| 255 | Cmd.removeFlag("workers"); |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 256 | Vector<std::thread> V; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 257 | std::thread Pulse(PulseThread); |
| 258 | Pulse.detach(); |
| 259 | for (unsigned i = 0; i < NumWorkers; i++) |
Matt Morehouse | 04304d1 | 2017-12-04 19:25:59 | [diff] [blame] | 260 | V.push_back(std::thread(WorkerThread, std::ref(Cmd), &Counter, NumJobs, &HasErrors)); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 261 | for (auto &T : V) |
| 262 | T.join(); |
| 263 | return HasErrors ? 1 : 0; |
| 264 | } |
| 265 | |
| 266 | static void RssThread(Fuzzer *F, size_t RssLimitMb) { |
| 267 | while (true) { |
| 268 | SleepSeconds(1); |
| 269 | size_t Peak = GetPeakRSSMb(); |
| 270 | if (Peak > RssLimitMb) |
| 271 | F->RssLimitCallback(); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | static void StartRssThread(Fuzzer *F, size_t RssLimitMb) { |
| 276 | if (!RssLimitMb) return; |
| 277 | std::thread T(RssThread, F, RssLimitMb); |
| 278 | T.detach(); |
| 279 | } |
| 280 | |
| 281 | int RunOneTest(Fuzzer *F, const char *InputFilePath, size_t MaxLen) { |
| 282 | Unit U = FileToVector(InputFilePath); |
| 283 | if (MaxLen && MaxLen < U.size()) |
| 284 | U.resize(MaxLen); |
| 285 | F->ExecuteCallback(U.data(), U.size()); |
| 286 | F->TryDetectingAMemoryLeak(U.data(), U.size(), true); |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static bool AllInputsAreFiles() { |
| 291 | if (Inputs->empty()) return false; |
| 292 | for (auto &Path : *Inputs) |
| 293 | if (!IsFile(Path)) |
| 294 | return false; |
| 295 | return true; |
| 296 | } |
| 297 | |
| 298 | static std::string GetDedupTokenFromFile(const std::string &Path) { |
| 299 | auto S = FileToString(Path); |
| 300 | auto Beg = S.find("DEDUP_TOKEN:"); |
| 301 | if (Beg == std::string::npos) |
| 302 | return ""; |
| 303 | auto End = S.find('\n', Beg); |
| 304 | if (End == std::string::npos) |
| 305 | return ""; |
| 306 | return S.substr(Beg, End - Beg); |
| 307 | } |
| 308 | |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 309 | int CleanseCrashInput(const Vector<std::string> &Args, |
Max Moroz | 1d369a5 | 2018-07-16 15:15:34 | [diff] [blame] | 310 | const FuzzingOptions &Options) { |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 311 | if (Inputs->size() != 1 || !Flags.exact_artifact_path) { |
| 312 | Printf("ERROR: -cleanse_crash should be given one input file and" |
Max Moroz | 1d369a5 | 2018-07-16 15:15:34 | [diff] [blame] | 313 | " -exact_artifact_path\n"); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 314 | exit(1); |
| 315 | } |
| 316 | std::string InputFilePath = Inputs->at(0); |
| 317 | std::string OutputFilePath = Flags.exact_artifact_path; |
Matt Morehouse | 04304d1 | 2017-12-04 19:25:59 | [diff] [blame] | 318 | Command Cmd(Args); |
| 319 | Cmd.removeFlag("cleanse_crash"); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 320 | |
Matt Morehouse | 04304d1 | 2017-12-04 19:25:59 | [diff] [blame] | 321 | assert(Cmd.hasArgument(InputFilePath)); |
| 322 | Cmd.removeArgument(InputFilePath); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 323 | |
| 324 | auto LogFilePath = DirPlusFile( |
| 325 | TmpDir(), "libFuzzerTemp." + std::to_string(GetPid()) + ".txt"); |
| 326 | auto TmpFilePath = DirPlusFile( |
| 327 | TmpDir(), "libFuzzerTemp." + std::to_string(GetPid()) + ".repro"); |
Matt Morehouse | 04304d1 | 2017-12-04 19:25:59 | [diff] [blame] | 328 | Cmd.addArgument(TmpFilePath); |
| 329 | Cmd.setOutputFile(LogFilePath); |
| 330 | Cmd.combineOutAndErr(); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 331 | |
| 332 | std::string CurrentFilePath = InputFilePath; |
| 333 | auto U = FileToVector(CurrentFilePath); |
| 334 | size_t Size = U.size(); |
| 335 | |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 336 | const Vector<uint8_t> ReplacementBytes = {' ', 0xff}; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 337 | for (int NumAttempts = 0; NumAttempts < 5; NumAttempts++) { |
| 338 | bool Changed = false; |
| 339 | for (size_t Idx = 0; Idx < Size; Idx++) { |
| 340 | Printf("CLEANSE[%d]: Trying to replace byte %zd of %zd\n", NumAttempts, |
| 341 | Idx, Size); |
| 342 | uint8_t OriginalByte = U[Idx]; |
| 343 | if (ReplacementBytes.end() != std::find(ReplacementBytes.begin(), |
| 344 | ReplacementBytes.end(), |
| 345 | OriginalByte)) |
| 346 | continue; |
| 347 | for (auto NewByte : ReplacementBytes) { |
| 348 | U[Idx] = NewByte; |
| 349 | WriteToFile(U, TmpFilePath); |
| 350 | auto ExitCode = ExecuteCommand(Cmd); |
| 351 | RemoveFile(TmpFilePath); |
| 352 | if (!ExitCode) { |
| 353 | U[Idx] = OriginalByte; |
| 354 | } else { |
| 355 | Changed = true; |
| 356 | Printf("CLEANSE: Replaced byte %zd with 0x%x\n", Idx, NewByte); |
| 357 | WriteToFile(U, OutputFilePath); |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | if (!Changed) break; |
| 363 | } |
| 364 | RemoveFile(LogFilePath); |
| 365 | return 0; |
| 366 | } |
| 367 | |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 368 | int MinimizeCrashInput(const Vector<std::string> &Args, |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 369 | const FuzzingOptions &Options) { |
| 370 | if (Inputs->size() != 1) { |
| 371 | Printf("ERROR: -minimize_crash should be given one input file\n"); |
| 372 | exit(1); |
| 373 | } |
| 374 | std::string InputFilePath = Inputs->at(0); |
Matt Morehouse | 04304d1 | 2017-12-04 19:25:59 | [diff] [blame] | 375 | Command BaseCmd(Args); |
| 376 | BaseCmd.removeFlag("minimize_crash"); |
| 377 | BaseCmd.removeFlag("exact_artifact_path"); |
| 378 | assert(BaseCmd.hasArgument(InputFilePath)); |
| 379 | BaseCmd.removeArgument(InputFilePath); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 380 | if (Flags.runs <= 0 && Flags.max_total_time == 0) { |
| 381 | Printf("INFO: you need to specify -runs=N or " |
| 382 | "-max_total_time=N with -minimize_crash=1\n" |
| 383 | "INFO: defaulting to -max_total_time=600\n"); |
Matt Morehouse | 04304d1 | 2017-12-04 19:25:59 | [diff] [blame] | 384 | BaseCmd.addFlag("max_total_time", "600"); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | auto LogFilePath = DirPlusFile( |
| 388 | TmpDir(), "libFuzzerTemp." + std::to_string(GetPid()) + ".txt"); |
Matt Morehouse | 04304d1 | 2017-12-04 19:25:59 | [diff] [blame] | 389 | BaseCmd.setOutputFile(LogFilePath); |
| 390 | BaseCmd.combineOutAndErr(); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 391 | |
| 392 | std::string CurrentFilePath = InputFilePath; |
| 393 | while (true) { |
| 394 | Unit U = FileToVector(CurrentFilePath); |
| 395 | Printf("CRASH_MIN: minimizing crash input: '%s' (%zd bytes)\n", |
| 396 | CurrentFilePath.c_str(), U.size()); |
| 397 | |
Matt Morehouse | 04304d1 | 2017-12-04 19:25:59 | [diff] [blame] | 398 | Command Cmd(BaseCmd); |
| 399 | Cmd.addArgument(CurrentFilePath); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 400 | |
Matt Morehouse | 04304d1 | 2017-12-04 19:25:59 | [diff] [blame] | 401 | std::string CommandLine = Cmd.toString(); |
| 402 | Printf("CRASH_MIN: executing: %s\n", CommandLine.c_str()); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 403 | int ExitCode = ExecuteCommand(Cmd); |
| 404 | if (ExitCode == 0) { |
| 405 | Printf("ERROR: the input %s did not crash\n", CurrentFilePath.c_str()); |
| 406 | exit(1); |
| 407 | } |
| 408 | Printf("CRASH_MIN: '%s' (%zd bytes) caused a crash. Will try to minimize " |
| 409 | "it further\n", |
| 410 | CurrentFilePath.c_str(), U.size()); |
| 411 | auto DedupToken1 = GetDedupTokenFromFile(LogFilePath); |
| 412 | if (!DedupToken1.empty()) |
| 413 | Printf("CRASH_MIN: DedupToken1: %s\n", DedupToken1.c_str()); |
| 414 | |
| 415 | std::string ArtifactPath = |
| 416 | Flags.exact_artifact_path |
| 417 | ? Flags.exact_artifact_path |
| 418 | : Options.ArtifactPrefix + "minimized-from-" + Hash(U); |
Matt Morehouse | 04304d1 | 2017-12-04 19:25:59 | [diff] [blame] | 419 | Cmd.addFlag("minimize_crash_internal_step", "1"); |
| 420 | Cmd.addFlag("exact_artifact_path", ArtifactPath); |
| 421 | CommandLine = Cmd.toString(); |
| 422 | Printf("CRASH_MIN: executing: %s\n", CommandLine.c_str()); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 423 | ExitCode = ExecuteCommand(Cmd); |
| 424 | CopyFileToErr(LogFilePath); |
| 425 | if (ExitCode == 0) { |
| 426 | if (Flags.exact_artifact_path) { |
| 427 | CurrentFilePath = Flags.exact_artifact_path; |
| 428 | WriteToFile(U, CurrentFilePath); |
| 429 | } |
| 430 | Printf("CRASH_MIN: failed to minimize beyond %s (%d bytes), exiting\n", |
| 431 | CurrentFilePath.c_str(), U.size()); |
| 432 | break; |
| 433 | } |
| 434 | auto DedupToken2 = GetDedupTokenFromFile(LogFilePath); |
| 435 | if (!DedupToken2.empty()) |
| 436 | Printf("CRASH_MIN: DedupToken2: %s\n", DedupToken2.c_str()); |
| 437 | |
| 438 | if (DedupToken1 != DedupToken2) { |
| 439 | if (Flags.exact_artifact_path) { |
| 440 | CurrentFilePath = Flags.exact_artifact_path; |
| 441 | WriteToFile(U, CurrentFilePath); |
| 442 | } |
| 443 | Printf("CRASH_MIN: mismatch in dedup tokens" |
| 444 | " (looks like a different bug). Won't minimize further\n"); |
| 445 | break; |
| 446 | } |
| 447 | |
| 448 | CurrentFilePath = ArtifactPath; |
| 449 | Printf("*********************************\n"); |
| 450 | } |
| 451 | RemoveFile(LogFilePath); |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | int MinimizeCrashInputInternalStep(Fuzzer *F, InputCorpus *Corpus) { |
| 456 | assert(Inputs->size() == 1); |
| 457 | std::string InputFilePath = Inputs->at(0); |
| 458 | Unit U = FileToVector(InputFilePath); |
| 459 | Printf("INFO: Starting MinimizeCrashInputInternalStep: %zd\n", U.size()); |
| 460 | if (U.size() < 2) { |
| 461 | Printf("INFO: The input is small enough, exiting\n"); |
| 462 | exit(0); |
| 463 | } |
| 464 | F->SetMaxInputLen(U.size()); |
| 465 | F->SetMaxMutationLen(U.size() - 1); |
| 466 | F->MinimizeCrashLoop(U); |
| 467 | Printf("INFO: Done MinimizeCrashInputInternalStep, no crashes found\n"); |
| 468 | exit(0); |
| 469 | return 0; |
| 470 | } |
| 471 | |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 472 | int AnalyzeDictionary(Fuzzer *F, const Vector<Unit>& Dict, |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 473 | UnitVector& Corpus) { |
| 474 | Printf("Started dictionary minimization (up to %d tests)\n", |
| 475 | Dict.size() * Corpus.size() * 2); |
| 476 | |
| 477 | // Scores and usage count for each dictionary unit. |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 478 | Vector<int> Scores(Dict.size()); |
| 479 | Vector<int> Usages(Dict.size()); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 480 | |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 481 | Vector<size_t> InitialFeatures; |
| 482 | Vector<size_t> ModifiedFeatures; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 483 | for (auto &C : Corpus) { |
| 484 | // Get coverage for the testcase without modifications. |
| 485 | F->ExecuteCallback(C.data(), C.size()); |
| 486 | InitialFeatures.clear(); |
Kostya Serebryany | bcd7849 | 2017-09-15 22:10:36 | [diff] [blame] | 487 | TPC.CollectFeatures([&](size_t Feature) { |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 488 | InitialFeatures.push_back(Feature); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 489 | }); |
| 490 | |
| 491 | for (size_t i = 0; i < Dict.size(); ++i) { |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 492 | Vector<uint8_t> Data = C; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 493 | auto StartPos = std::search(Data.begin(), Data.end(), |
| 494 | Dict[i].begin(), Dict[i].end()); |
| 495 | // Skip dictionary unit, if the testcase does not contain it. |
| 496 | if (StartPos == Data.end()) |
| 497 | continue; |
| 498 | |
| 499 | ++Usages[i]; |
| 500 | while (StartPos != Data.end()) { |
| 501 | // Replace all occurrences of dictionary unit in the testcase. |
| 502 | auto EndPos = StartPos + Dict[i].size(); |
| 503 | for (auto It = StartPos; It != EndPos; ++It) |
| 504 | *It ^= 0xFF; |
| 505 | |
| 506 | StartPos = std::search(EndPos, Data.end(), |
| 507 | Dict[i].begin(), Dict[i].end()); |
| 508 | } |
| 509 | |
| 510 | // Get coverage for testcase with masked occurrences of dictionary unit. |
| 511 | F->ExecuteCallback(Data.data(), Data.size()); |
| 512 | ModifiedFeatures.clear(); |
Kostya Serebryany | bcd7849 | 2017-09-15 22:10:36 | [diff] [blame] | 513 | TPC.CollectFeatures([&](size_t Feature) { |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 514 | ModifiedFeatures.push_back(Feature); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 515 | }); |
| 516 | |
| 517 | if (InitialFeatures == ModifiedFeatures) |
| 518 | --Scores[i]; |
| 519 | else |
| 520 | Scores[i] += 2; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | Printf("###### Useless dictionary elements. ######\n"); |
| 525 | for (size_t i = 0; i < Dict.size(); ++i) { |
| 526 | // Dictionary units with positive score are treated as useful ones. |
| 527 | if (Scores[i] > 0) |
Max Moroz | 1d369a5 | 2018-07-16 15:15:34 | [diff] [blame] | 528 | continue; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 529 | |
| 530 | Printf("\""); |
| 531 | PrintASCII(Dict[i].data(), Dict[i].size(), "\""); |
| 532 | Printf(" # Score: %d, Used: %d\n", Scores[i], Usages[i]); |
| 533 | } |
| 534 | Printf("###### End of useless dictionary elements. ######\n"); |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | int FuzzerDriver(int *argc, char ***argv, UserCallback Callback) { |
| 539 | using namespace fuzzer; |
| 540 | assert(argc && argv && "Argument pointers cannot be nullptr"); |
| 541 | std::string Argv0((*argv)[0]); |
| 542 | EF = new ExternalFunctions(); |
| 543 | if (EF->LLVMFuzzerInitialize) |
| 544 | EF->LLVMFuzzerInitialize(argc, argv); |
Matt Morehouse | a34c65e | 2018-07-09 23:51:08 | [diff] [blame] | 545 | if (EF->__msan_scoped_disable_interceptor_checks) |
| 546 | EF->__msan_scoped_disable_interceptor_checks(); |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 547 | const Vector<std::string> Args(*argv, *argv + *argc); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 548 | assert(!Args.empty()); |
| 549 | ProgName = new std::string(Args[0]); |
| 550 | if (Argv0 != *ProgName) { |
| 551 | Printf("ERROR: argv[0] has been modified in LLVMFuzzerInitialize\n"); |
| 552 | exit(1); |
| 553 | } |
| 554 | ParseFlags(Args); |
| 555 | if (Flags.help) { |
| 556 | PrintHelp(); |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | if (Flags.close_fd_mask & 2) |
| 561 | DupAndCloseStderr(); |
| 562 | if (Flags.close_fd_mask & 1) |
| 563 | CloseStdout(); |
| 564 | |
| 565 | if (Flags.jobs > 0 && Flags.workers == 0) { |
| 566 | Flags.workers = std::min(NumberOfCpuCores() / 2, Flags.jobs); |
| 567 | if (Flags.workers > 1) |
| 568 | Printf("Running %u workers\n", Flags.workers); |
| 569 | } |
| 570 | |
| 571 | if (Flags.workers > 0 && Flags.jobs > 0) |
| 572 | return RunInMultipleProcesses(Args, Flags.workers, Flags.jobs); |
| 573 | |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 574 | FuzzingOptions Options; |
| 575 | Options.Verbosity = Flags.verbosity; |
| 576 | Options.MaxLen = Flags.max_len; |
Matt Morehouse | 36c89b3 | 2018-02-13 20:52:15 | [diff] [blame] | 577 | Options.LenControl = Flags.len_control; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 578 | Options.UnitTimeoutSec = Flags.timeout; |
| 579 | Options.ErrorExitCode = Flags.error_exitcode; |
| 580 | Options.TimeoutExitCode = Flags.timeout_exitcode; |
| 581 | Options.MaxTotalTimeSec = Flags.max_total_time; |
| 582 | Options.DoCrossOver = Flags.cross_over; |
| 583 | Options.MutateDepth = Flags.mutate_depth; |
Kostya Serebryany | ad05ee0 | 2017-12-01 19:18:38 | [diff] [blame] | 584 | Options.ReduceDepth = Flags.reduce_depth; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 585 | Options.UseCounters = Flags.use_counters; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 586 | Options.UseMemmem = Flags.use_memmem; |
| 587 | Options.UseCmp = Flags.use_cmp; |
| 588 | Options.UseValueProfile = Flags.use_value_profile; |
| 589 | Options.Shrink = Flags.shrink; |
| 590 | Options.ReduceInputs = Flags.reduce_inputs; |
| 591 | Options.ShuffleAtStartUp = Flags.shuffle; |
| 592 | Options.PreferSmall = Flags.prefer_small; |
| 593 | Options.ReloadIntervalSec = Flags.reload; |
| 594 | Options.OnlyASCII = Flags.only_ascii; |
| 595 | Options.DetectLeaks = Flags.detect_leaks; |
Alex Shlyapnikov | 6f1c26f | 2017-10-23 22:04:30 | [diff] [blame] | 596 | Options.PurgeAllocatorIntervalSec = Flags.purge_allocator_interval; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 597 | Options.TraceMalloc = Flags.trace_malloc; |
| 598 | Options.RssLimitMb = Flags.rss_limit_mb; |
Kostya Serebryany | de9bafb | 2017-12-01 22:12:04 | [diff] [blame] | 599 | Options.MallocLimitMb = Flags.malloc_limit_mb; |
| 600 | if (!Options.MallocLimitMb) |
| 601 | Options.MallocLimitMb = Options.RssLimitMb; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 602 | if (Flags.runs >= 0) |
| 603 | Options.MaxNumberOfRuns = Flags.runs; |
| 604 | if (!Inputs->empty() && !Flags.minimize_crash_internal_step) |
| 605 | Options.OutputCorpus = (*Inputs)[0]; |
| 606 | Options.ReportSlowUnits = Flags.report_slow_units; |
| 607 | if (Flags.artifact_prefix) |
| 608 | Options.ArtifactPrefix = Flags.artifact_prefix; |
| 609 | if (Flags.exact_artifact_path) |
| 610 | Options.ExactArtifactPath = Flags.exact_artifact_path; |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 611 | Vector<Unit> Dictionary; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 612 | if (Flags.dict) |
| 613 | if (!ParseDictionaryFile(FileToString(Flags.dict), &Dictionary)) |
| 614 | return 1; |
| 615 | if (Flags.verbosity > 0 && !Dictionary.empty()) |
| 616 | Printf("Dictionary: %zd entries\n", Dictionary.size()); |
| 617 | bool DoPlainRun = AllInputsAreFiles(); |
| 618 | Options.SaveArtifacts = |
| 619 | !DoPlainRun || Flags.minimize_crash_internal_step; |
| 620 | Options.PrintNewCovPcs = Flags.print_pcs; |
Kostya Serebryany | 2eef816 | 2017-08-25 20:09:25 | [diff] [blame] | 621 | Options.PrintNewCovFuncs = Flags.print_funcs; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 622 | Options.PrintFinalStats = Flags.print_final_stats; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 623 | Options.PrintCorpusStats = Flags.print_corpus_stats; |
| 624 | Options.PrintCoverage = Flags.print_coverage; |
Kostya Serebryany | 69c2b71 | 2018-05-21 19:47:00 | [diff] [blame] | 625 | Options.DumpCoverage = Flags.dump_coverage; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 626 | if (Flags.exit_on_src_pos) |
| 627 | Options.ExitOnSrcPos = Flags.exit_on_src_pos; |
| 628 | if (Flags.exit_on_item) |
| 629 | Options.ExitOnItem = Flags.exit_on_item; |
Kostya Serebryany | e9c6f06 | 2018-05-16 23:26:37 | [diff] [blame] | 630 | if (Flags.focus_function) |
| 631 | Options.FocusFunction = Flags.focus_function; |
Kostya Serebryany | 1fd005f | 2018-06-06 01:23:29 | [diff] [blame] | 632 | if (Flags.data_flow_trace) |
| 633 | Options.DataFlowTrace = Flags.data_flow_trace; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 634 | |
| 635 | unsigned Seed = Flags.seed; |
| 636 | // Initialize Seed. |
| 637 | if (Seed == 0) |
| 638 | Seed = |
| 639 | std::chrono::system_clock::now().time_since_epoch().count() + GetPid(); |
| 640 | if (Flags.verbosity) |
| 641 | Printf("INFO: Seed: %u\n", Seed); |
| 642 | |
| 643 | Random Rand(Seed); |
| 644 | auto *MD = new MutationDispatcher(Rand, Options); |
| 645 | auto *Corpus = new InputCorpus(Options.OutputCorpus); |
| 646 | auto *F = new Fuzzer(Callback, *Corpus, *MD, Options); |
| 647 | |
| 648 | for (auto &U: Dictionary) |
| 649 | if (U.size() <= Word::GetMaxSize()) |
| 650 | MD->AddWordToManualDictionary(Word(U.data(), U.size())); |
| 651 | |
| 652 | StartRssThread(F, Flags.rss_limit_mb); |
| 653 | |
| 654 | Options.HandleAbrt = Flags.handle_abrt; |
| 655 | Options.HandleBus = Flags.handle_bus; |
| 656 | Options.HandleFpe = Flags.handle_fpe; |
| 657 | Options.HandleIll = Flags.handle_ill; |
| 658 | Options.HandleInt = Flags.handle_int; |
| 659 | Options.HandleSegv = Flags.handle_segv; |
| 660 | Options.HandleTerm = Flags.handle_term; |
| 661 | Options.HandleXfsz = Flags.handle_xfsz; |
Kostya Serebryany | a2ca2dc | 2017-11-09 20:30:19 | [diff] [blame] | 662 | Options.HandleUsr1 = Flags.handle_usr1; |
| 663 | Options.HandleUsr2 = Flags.handle_usr2; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 664 | SetSignalHandler(Options); |
| 665 | |
| 666 | std::atexit(Fuzzer::StaticExitCallback); |
| 667 | |
| 668 | if (Flags.minimize_crash) |
| 669 | return MinimizeCrashInput(Args, Options); |
| 670 | |
| 671 | if (Flags.minimize_crash_internal_step) |
| 672 | return MinimizeCrashInputInternalStep(F, Corpus); |
| 673 | |
| 674 | if (Flags.cleanse_crash) |
| 675 | return CleanseCrashInput(Args, Options); |
| 676 | |
Kostya Serebryany | 2f7edae | 2018-05-15 01:15:47 | [diff] [blame] | 677 | #if 0 // deprecated, to be removed. |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 678 | if (auto Name = Flags.run_equivalence_server) { |
| 679 | SMR.Destroy(Name); |
| 680 | if (!SMR.Create(Name)) { |
| 681 | Printf("ERROR: can't create shared memory region\n"); |
| 682 | return 1; |
| 683 | } |
| 684 | Printf("INFO: EQUIVALENCE SERVER UP\n"); |
| 685 | while (true) { |
| 686 | SMR.WaitClient(); |
| 687 | size_t Size = SMR.ReadByteArraySize(); |
| 688 | SMR.WriteByteArray(nullptr, 0); |
| 689 | const Unit tmp(SMR.GetByteArray(), SMR.GetByteArray() + Size); |
| 690 | F->ExecuteCallback(tmp.data(), tmp.size()); |
| 691 | SMR.PostServer(); |
| 692 | } |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | if (auto Name = Flags.use_equivalence_server) { |
| 697 | if (!SMR.Open(Name)) { |
| 698 | Printf("ERROR: can't open shared memory region\n"); |
| 699 | return 1; |
| 700 | } |
| 701 | Printf("INFO: EQUIVALENCE CLIENT UP\n"); |
| 702 | } |
Kostya Serebryany | 2f7edae | 2018-05-15 01:15:47 | [diff] [blame] | 703 | #endif |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 704 | |
| 705 | if (DoPlainRun) { |
| 706 | Options.SaveArtifacts = false; |
| 707 | int Runs = std::max(1, Flags.runs); |
| 708 | Printf("%s: Running %zd inputs %d time(s) each.\n", ProgName->c_str(), |
| 709 | Inputs->size(), Runs); |
| 710 | for (auto &Path : *Inputs) { |
| 711 | auto StartTime = system_clock::now(); |
| 712 | Printf("Running: %s\n", Path.c_str()); |
| 713 | for (int Iter = 0; Iter < Runs; Iter++) |
| 714 | RunOneTest(F, Path.c_str(), Options.MaxLen); |
| 715 | auto StopTime = system_clock::now(); |
| 716 | auto MS = duration_cast<milliseconds>(StopTime - StartTime).count(); |
| 717 | Printf("Executed %s in %zd ms\n", Path.c_str(), (long)MS); |
| 718 | } |
| 719 | Printf("***\n" |
| 720 | "*** NOTE: fuzzing was not performed, you have only\n" |
| 721 | "*** executed the target code on a fixed set of inputs.\n" |
| 722 | "***\n"); |
| 723 | F->PrintFinalStats(); |
| 724 | exit(0); |
| 725 | } |
| 726 | |
| 727 | if (Flags.merge) { |
Kostya Serebryany | 68fdef1 | 2017-11-09 01:05:29 | [diff] [blame] | 728 | F->CrashResistantMerge(Args, *Inputs, |
| 729 | Flags.load_coverage_summary, |
| 730 | Flags.save_coverage_summary, |
| 731 | Flags.merge_control_file); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 732 | exit(0); |
| 733 | } |
| 734 | |
Kostya Serebryany | 68fdef1 | 2017-11-09 01:05:29 | [diff] [blame] | 735 | if (Flags.merge_inner) { |
| 736 | const size_t kDefaultMaxMergeLen = 1 << 20; |
| 737 | if (Options.MaxLen == 0) |
| 738 | F->SetMaxInputLen(kDefaultMaxMergeLen); |
| 739 | assert(Flags.merge_control_file); |
| 740 | F->CrashResistantMergeInternalStep(Flags.merge_control_file); |
| 741 | exit(0); |
| 742 | } |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 743 | |
| 744 | if (Flags.analyze_dict) { |
Kostya Serebryany | 3a8e3c8 | 2017-08-29 02:05:01 | [diff] [blame] | 745 | size_t MaxLen = INT_MAX; // Large max length. |
| 746 | UnitVector InitialCorpus; |
| 747 | for (auto &Inp : *Inputs) { |
| 748 | Printf("Loading corpus dir: %s\n", Inp.c_str()); |
| 749 | ReadDirToVectorOfUnits(Inp.c_str(), &InitialCorpus, nullptr, |
| 750 | MaxLen, /*ExitOnError=*/false); |
| 751 | } |
| 752 | |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 753 | if (Dictionary.empty() || Inputs->empty()) { |
| 754 | Printf("ERROR: can't analyze dict without dict and corpus provided\n"); |
| 755 | return 1; |
| 756 | } |
| 757 | if (AnalyzeDictionary(F, Dictionary, InitialCorpus)) { |
| 758 | Printf("Dictionary analysis failed\n"); |
| 759 | exit(1); |
| 760 | } |
Sylvestre Ledru | d9a8b6a | 2018-03-13 14:35:10 | [diff] [blame] | 761 | Printf("Dictionary analysis succeeded\n"); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 762 | exit(0); |
| 763 | } |
| 764 | |
Kostya Serebryany | 3a8e3c8 | 2017-08-29 02:05:01 | [diff] [blame] | 765 | F->Loop(*Inputs); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 766 | |
| 767 | if (Flags.verbosity) |
| 768 | Printf("Done %zd runs in %zd second(s)\n", F->getTotalNumberOfRuns(), |
| 769 | F->secondsSinceProcessStartUp()); |
| 770 | F->PrintFinalStats(); |
| 771 | |
| 772 | exit(0); // Don't let F destroy itself. |
| 773 | } |
| 774 | |
| 775 | // Storage for global ExternalFunctions object. |
| 776 | ExternalFunctions *EF = nullptr; |
| 777 | |
| 778 | } // namespace fuzzer |