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