Lines Matching +full:auto +full:- +full:string +full:- +full:detection
1 //===- FuzzerDriver.cpp - FuzzerDriver function and flags -----------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 //===----------------------------------------------------------------------===//
28 #include <string>
89 static std::vector<std::string> *Inputs;
90 static std::string *ProgName;
94 auto Prog = ProgName->c_str();
96 Printf("%s [-flag1=val1 [-flag2=val2 ...] ] [dir1 [dir2 ...] ]\n", Prog);
99 Printf("%s [-flag1=val1 [-flag2=val2 ...] ] file1 [file2 ...]\n", Prog);
101 Printf("\nFlags: (strictly in form -flag=value)\n");
107 const auto &D = FlagDescriptions[F];
110 for (size_t i = 0, n = MaxFlagLen - strlen(D.Name); i < n; i++)
115 Printf("\nFlags starting with '--' will be ignored and "
121 if (Param[0] == '-' && strstr(Param + 1, Name) == Param + 1 &&
131 if (*Str == '-') {
133 Sign = -1;
139 Res = Res * 10 + (Ch - '0');
145 if (Param[0] != '-') return false;
146 if (Param[1] == '-') {
150 Printf("INFO: libFuzzer ignores flags that start with '--'\n");
162 auto Val = MyStol(Str);
168 auto Val = std::stoul(Str);
185 "use -help=1 to list all flags\n\n", Param);
190 static void ParseFlags(const std::vector<std::string> &Args,
203 if (EF->LLVMFuzzerCustomMutator) {
206 "Disabling -len_control by default.\n", EF->LLVMFuzzerCustomMutator);
209 Inputs = new std::vector<std::string>;
216 Inputs->push_back(Args[A]);
236 std::string Log = "fuzz-" + std::to_string(C) + ".log";
241 std::string CommandLine = Cmd.toString();
254 static void ValidateDirectoryExists(const std::string &Path,
257 Printf("ERROR: Provided directory path is an empty string\n");
276 std::string CloneArgsWithoutX(const std::vector<std::string> &Args,
278 std::string Cmd;
279 for (auto &S : Args) {
287 static int RunInMultipleProcesses(const std::vector<std::string> &Args,
303 for (auto &T : V)
313 F->RssLimitCallback();
328 F->ExecuteCallback(U.data(), U.size());
330 // Leak detection is not needed when collecting full coverage data.
331 F->TPCUpdateObservedPCs();
333 F->TryDetectingAMemoryLeak(U.data(), U.size(), true);
339 if (Inputs->empty()) return false;
340 for (auto &Path : *Inputs)
346 static std::string GetDedupTokenFromCmdOutput(const std::string &S) {
347 auto Beg = S.find("DEDUP_TOKEN:");
348 if (Beg == std::string::npos)
350 auto End = S.find('\n', Beg);
351 if (End == std::string::npos)
353 return S.substr(Beg, End - Beg);
356 int CleanseCrashInput(const std::vector<std::string> &Args,
358 if (Inputs->size() != 1 || !Flags.exact_artifact_path) {
359 Printf("ERROR: -cleanse_crash should be given one input file and"
360 " -exact_artifact_path\n");
363 std::string InputFilePath = Inputs->at(0);
364 std::string OutputFilePath = Flags.exact_artifact_path;
371 auto TmpFilePath = TempPath("CleanseCrashInput", ".repro");
376 std::string CurrentFilePath = InputFilePath;
377 auto U = FileToVector(CurrentFilePath);
391 for (auto NewByte : ReplacementBytes) {
394 auto ExitCode = ExecuteCommand(Cmd);
411 int MinimizeCrashInput(const std::vector<std::string> &Args,
413 if (Inputs->size() != 1) {
414 Printf("ERROR: -minimize_crash should be given one input file\n");
417 std::string InputFilePath = Inputs->at(0);
424 Printf("INFO: you need to specify -runs=N or "
425 "-max_total_time=N with -minimize_crash=1\n"
426 "INFO: defaulting to -max_total_time=600\n");
432 std::string CurrentFilePath = InputFilePath;
442 std::string CmdOutput;
451 auto DedupToken1 = GetDedupTokenFromCmdOutput(CmdOutput);
455 std::string ArtifactPath =
458 : Options.ArtifactPrefix + "minimized-from-" + Hash(U);
474 auto DedupToken2 = GetDedupTokenFromCmdOutput(CmdOutput);
495 assert(Inputs->size() == 1);
496 std::string InputFilePath = Inputs->at(0);
503 F->SetMaxInputLen(U.size());
504 F->SetMaxMutationLen(U.size() - 1);
505 F->MinimizeCrashLoop(U);
511 const std::vector<std::string> &Args,
512 const std::vector<std::string> &Corpora, const char *CFPathOrNull) {
525 std::string CFPath = CFPathOrNull ? CFPathOrNull : TempPath("Merge", ".txt");
526 std::vector<std::string> NewFiles;
530 for (auto &Path : NewFiles)
531 F->WriteToOutputCorpus(FileToVector(Path, Options.MaxLen));
550 for (auto &C : Corpus) {
552 F->ExecuteCallback(C.data(), C.size());
560 auto StartPos = std::search(Data.begin(), Data.end(),
569 auto EndPos = StartPos + Dict[i].size();
570 for (auto It = StartPos; It != EndPos; ++It)
578 F->ExecuteCallback(Data.data(), Data.size());
585 --Scores[i];
605 std::vector<std::string> ParseSeedInuts(const char *seed_inputs) {
606 // Parse -seed_inputs=file1,file2,... or -seed_inputs=@seed_inputs_file
607 std::vector<std::string> Files;
609 std::string SeedInputs;
620 while ((comma_pos = SeedInputs.find_last_of(',')) != std::string::npos) {
629 ReadCorpora(const std::vector<std::string> &CorpusDirs,
630 const std::vector<std::string> &ExtraSeedFiles) {
633 for (auto &Dir : CorpusDirs) {
635 Printf("INFO: % 8zd files found in %s\n", SizedFiles.size() - LastNumFiles,
639 for (auto &File : ExtraSeedFiles)
640 if (auto Size = FileSize(File))
648 std::string Argv0((*argv)[0]);
650 if (EF->LLVMFuzzerInitialize)
651 EF->LLVMFuzzerInitialize(argc, argv);
652 if (EF->__msan_scoped_disable_interceptor_checks)
653 EF->__msan_scoped_disable_interceptor_checks();
654 const std::vector<std::string> Args(*argv, *argv + *argc);
656 ProgName = new std::string(Args[0]);
716 if (!Inputs->empty() && !Flags.minimize_crash_internal_step) {
719 std::string OutputCorpusDir = (*Inputs)[0];
732 std::string ArtifactPathDir = Options.ArtifactPrefix;
733 if (!IsSeparator(ArtifactPathDir[ArtifactPathDir.length() - 1])) {
814 auto *MD = new MutationDispatcher(Rand, Options);
815 auto *Corpus = new InputCorpus(Options.OutputCorpus, Entropic);
816 auto *F = new Fuzzer(Callback, *Corpus, *MD, Options);
818 for (auto &U: Dictionary)
820 MD->AddWordToManualDictionary(Word(U.data(), U.size()));
857 Printf("%s: Running %zd inputs %d time(s) each.\n", ProgName->c_str(),
858 Inputs->size(), Runs);
859 for (auto &Path : *Inputs) {
860 auto StartTime = system_clock::now();
864 auto StopTime = system_clock::now();
865 auto MS = duration_cast<milliseconds>(StopTime - StartTime).count();
872 F->PrintFinalStats();
878 FuzzWithFork(F->GetMD().GetRand(), Options, Args, *Inputs, Flags.fork);
886 F->SetMaxInputLen(kDefaultMaxMergeLen);
888 F->CrashResistantMergeInternalStep(Flags.merge_control_file,
896 for (auto &Inp : *Inputs) {
902 if (Dictionary.empty() || Inputs->empty()) {
914 auto CorporaFiles = ReadCorpora(*Inputs, ParseSeedInuts(Flags.seed_inputs));
915 F->Loop(CorporaFiles);
918 Printf("Done %zd runs in %zd second(s)\n", F->getTotalNumberOfRuns(),
919 F->secondsSinceProcessStartUp());
920 F->PrintFinalStats();