# Global Metrics

path: .metrics.halstead.length
old: 6394.0
new: 4529.0
path: .metrics.halstead.time
old: 381696.8135084341
new: 170925.91058461904
path: .metrics.halstead.purity_ratio
old: 0.24374226793980855
new: 1.0736741858233372
path: .metrics.halstead.effort
old: 6870542.643151813
new: 3076666.390523142
path: .metrics.halstead.n2
old: 195.0
new: 516.0
path: .metrics.halstead.difficulty
old: 138.92307692307693
new: 74.49612403100775
path: .metrics.halstead.estimated_program_length
old: 1558.4880612071358
new: 4862.670387593894
path: .metrics.halstead.level
old: 0.007198228128460686
new: 0.013423517169614985
path: .metrics.halstead.N2
old: 3010.0
new: 1922.0
path: .metrics.halstead.bugs
old: 12.046829717140318
new: 7.05124303089276
path: .metrics.halstead.n1
old: 18.0
new: 40.0
path: .metrics.halstead.vocabulary
old: 213.0
new: 556.0
path: .metrics.halstead.N1
old: 3384.0
new: 2607.0
path: .metrics.halstead.volume
old: 49455.73331172401
new: 41299.68411836476
path: .metrics.nexits.average
old: 0.21052631578947367
new: 1.3571428571428572
path: .metrics.nexits.sum
old: 8.0
new: 38.0
path: .metrics.cognitive.average
old: 0.47368421052631576
new: 7.035714285714286
path: .metrics.cognitive.sum
old: 18.0
new: 197.0
path: .metrics.nom.total
old: 38.0
new: 28.0
path: .metrics.nom.closures
old: 0.0
new: 2.0
path: .metrics.nom.functions
old: 38.0
new: 26.0
path: .metrics.nargs.average
old: 9.894736842105264
new: 1.6071428571428572
path: .metrics.nargs.sum
old: 376.0
new: 45.0
path: .metrics.cyclomatic.sum
old: 49.0
new: 184.0
path: .metrics.cyclomatic.average
old: 1.2564102564102564
new: 5.935483870967742
path: .metrics.loc.sloc
old: 761.0
new: 871.0
path: .metrics.loc.ploc
old: 677.0
new: 757.0
path: .metrics.loc.lloc
old: 364.0
new: 433.0
path: .metrics.loc.blank
old: 40.0
new: 77.0
path: .metrics.loc.cloc
old: 44.0
new: 37.0
path: .metrics.mi.mi_sei
old: -58.222624347973685
new: -93.57870445104209
path: .metrics.mi.mi_original
old: -3.956993411354461
new: -36.256972703372014

# Spaces Data

## Minimal test - lines (45, 871)

path: .spaces[2].metrics.nexits.sum
old: 1.0
new: 38.0
path: .spaces[2].metrics.nexits.average
old: 1.0
new: 1.4615384615384617
path: .spaces[2].metrics.cognitive.average
old: 0.0
new: 7.538461538461538
path: .spaces[2].metrics.cognitive.sum
old: 0.0
new: 196.0
path: .spaces[2].metrics.cyclomatic.average
old: 1.0
new: 6.428571428571429
path: .spaces[2].metrics.cyclomatic.sum
old: 1.0
new: 180.0
path: .spaces[2].metrics.nom.total
old: 1.0
new: 26.0
path: .spaces[2].metrics.nom.closures
old: 0.0
new: 2.0
path: .spaces[2].metrics.nom.functions
old: 1.0
new: 24.0
path: .spaces[2].metrics.mi.mi_sei
old: 42.41262335674293
new: -94.01469894865588
path: .spaces[2].metrics.mi.mi_visual_studio
old: 47.83593349453531
new: 0.0
path: .spaces[2].metrics.mi.mi_original
old: 81.79944627565538
new: -34.438572104199864
path: .spaces[2].metrics.nargs.average
old: 13.0
new: 1.7307692307692308
path: .spaces[2].metrics.nargs.sum
old: 13.0
new: 45.0
path: .spaces[2].metrics.loc.sloc
old: 26.0
new: 827.0
path: .spaces[2].metrics.loc.lloc
old: 16.0
new: 433.0
path: .spaces[2].metrics.loc.cloc
old: 0.0
new: 24.0
path: .spaces[2].metrics.loc.ploc
old: 24.0
new: 727.0
path: .spaces[2].metrics.loc.blank
old: 2.0
new: 76.0
path: .spaces[2].metrics.halstead.vocabulary
old: 34.0
new: 540.0
path: .spaces[2].metrics.halstead.length
old: 207.0
new: 4499.0
path: .spaces[2].metrics.halstead.time
old: 788.7451649434784
new: 172693.41596725243
path: .spaces[2].metrics.halstead.n1
old: 7.0
new: 40.0
path: .spaces[2].metrics.halstead.n2
old: 27.0
new: 500.0
path: .spaces[2].metrics.halstead.purity_ratio
old: 0.7151374251826902
new: 1.043736222744285
path: .spaces[2].metrics.halstead.N2
old: 104.0
new: 1903.0
path: .spaces[2].metrics.halstead.volume
old: 1053.10480813882
new: 40836.59337113169
path: .spaces[2].metrics.halstead.bugs
old: 0.19544214571917265
new: 7.0997698200935
path: .spaces[2].metrics.halstead.N1
old: 103.0
new: 2596.0
path: .spaces[2].metrics.halstead.difficulty
old: 13.48148148148148
new: 76.12
path: .spaces[2].metrics.halstead.estimated_program_length
old: 148.03344701281688
new: 4695.769266126538
path: .spaces[2].metrics.halstead.level
old: 0.07417582417582418
new: 0.013137151865475564
path: .spaces[2].metrics.halstead.effort
old: 14197.412968982611
new: 3108481.4874105444

### Code

    namespace fuzzer {

    // Program arguments.
    struct FlagDescription {
      const char *Name;
      const char *Description;
      int   Default;
      int   *IntFlag;
      const char **StrFlag;
      unsigned int *UIntFlag;
    };

    struct {
    #define FUZZER_DEPRECATED_FLAG(Name)
    #define FUZZER_FLAG_INT(Name, Default, Description) int Name;
    #define FUZZER_FLAG_UNSIGNED(Name, Default, Description) unsigned int Name;
    #define FUZZER_FLAG_STRING(Name, Description) const char *Name;
    #include "FuzzerFlags.def"
    #undef FUZZER_DEPRECATED_FLAG
    #undef FUZZER_FLAG_INT
    #undef FUZZER_FLAG_UNSIGNED
    #undef FUZZER_FLAG_STRING
    } Flags;

    static const FlagDescription FlagDescriptions [] {
    #define FUZZER_DEPRECATED_FLAG(Name)                                           \
      {#Name, "Deprecated; don't use", 0, nullptr, nullptr, nullptr},
    #define FUZZER_FLAG_INT(Name, Default, Description)                            \
      {#Name, Description, Default, &Flags.Name, nullptr, nullptr},
    #define FUZZER_FLAG_UNSIGNED(Name, Default, Description)                       \
      {#Name,   Description, static_cast(Default),                            \
       nullptr, nullptr, &Flags.Name},
    #define FUZZER_FLAG_STRING(Name, Description)                                  \
      {#Name, Description, 0, nullptr, &Flags.Name, nullptr},
    #include "FuzzerFlags.def"
    #undef FUZZER_DEPRECATED_FLAG
    #undef FUZZER_FLAG_INT
    #undef FUZZER_FLAG_UNSIGNED
    #undef FUZZER_FLAG_STRING
    };

    static const size_t kNumFlags =
        sizeof(FlagDescriptions) / sizeof(FlagDescriptions[0]);

    static Vector *Inputs;
    static std::string *ProgName;

    static void PrintHelp() {
      Printf("Usage:\n");
      auto Prog = ProgName->c_str();
      Printf("\nTo run fuzzing pass 0 or more directories.\n");
      Printf("%s [-flag1=val1 [-flag2=val2 ...] ] [dir1 [dir2 ...] ]\n", Prog);

      Printf("\nTo run individual tests without fuzzing pass 1 or more files:\n");
      Printf("%s [-flag1=val1 [-flag2=val2 ...] ] file1 [file2 ...]\n", Prog);

      Printf("\nFlags: (strictly in form -flag=value)\n");
      size_t MaxFlagLen = 0;
      for (size_t F = 0; F < kNumFlags; F++)
        MaxFlagLen = std::max(strlen(FlagDescriptions[F].Name), MaxFlagLen);

      for (size_t F = 0; F < kNumFlags; F++) {
        const auto &D = FlagDescriptions[F];
        if (strstr(D.Description, "internal flag") == D.Description) continue;
        Printf(" %s", D.Name);
        for (size_t i = 0, n = MaxFlagLen - strlen(D.Name); i < n; i++)
          Printf(" ");
        Printf("\t");
        Printf("%d\t%s\n", D.Default, D.Description);
      }
      Printf("\nFlags starting with '--' will be ignored and "
                "will be passed verbatim to subprocesses.\n");
    }

    static const char *FlagValue(const char *Param, const char *Name) {
      size_t Len = strlen(Name);
      if (Param[0] == '-' && strstr(Param + 1, Name) == Param + 1 &&
          Param[Len + 1] == '=')
          return &Param[Len + 2];
      return nullptr;
    }

    // Avoid calling stol as it triggers a bug in clang/glibc build.
    static long MyStol(const char *Str) {
      long Res = 0;
      long Sign = 1;
      if (*Str == '-') {
        Str++;
        Sign = -1;
      }
      for (size_t i = 0; Str[i]; i++) {
        char Ch = Str[i];
        if (Ch < '0' || Ch > '9')
          return Res;
        Res = Res * 10 + (Ch - '0');
      }
      return Res * Sign;
    }

    static bool ParseOneFlag(const char *Param) {
      if (Param[0] != '-') return false;
      if (Param[1] == '-') {
        static bool PrintedWarning = false;
        if (!PrintedWarning) {
          PrintedWarning = true;
          Printf("INFO: libFuzzer ignores flags that start with '--'\n");
        }
        for (size_t F = 0; F < kNumFlags; F++)
          if (FlagValue(Param + 1, FlagDescriptions[F].Name))
            Printf("WARNING: did you mean '%s' (single dash)?\n", Param + 1);
        return true;
      }
      for (size_t F = 0; F < kNumFlags; F++) {
        const char *Name = FlagDescriptions[F].Name;
        const char *Str = FlagValue(Param, Name);
        if (Str)  {
          if (FlagDescriptions[F].IntFlag) {
            int Val = MyStol(Str);
            *FlagDescriptions[F].IntFlag = Val;
            if (Flags.verbosity >= 2)
              Printf("Flag: %s %d\n", Name, Val);
            return true;
          } else if (FlagDescriptions[F].UIntFlag) {
            unsigned int Val = std::stoul(Str);
            *FlagDescriptions[F].UIntFlag = Val;
            if (Flags.verbosity >= 2)
              Printf("Flag: %s %u\n", Name, Val);
            return true;
          } else if (FlagDescriptions[F].StrFlag) {
            *FlagDescriptions[F].StrFlag = Str;
            if (Flags.verbosity >= 2)
              Printf("Flag: %s %s\n", Name, Str);
            return true;
          } else {  // Deprecated flag.
            Printf("Flag: %s: deprecated, don't use\n", Name);
            return true;
          }
        }
      }
      Printf("\n\nWARNING: unrecognized flag '%s'; "
             "use -help=1 to list all flags\n\n", Param);
      return true;
    }

    // We don't use any library to minimize dependencies.
    static void ParseFlags(const Vector &Args,
                           const ExternalFunctions *EF) {
      for (size_t F = 0; F < kNumFlags; F++) {
        if (FlagDescriptions[F].IntFlag)
          *FlagDescriptions[F].IntFlag = FlagDescriptions[F].Default;
        if (FlagDescriptions[F].UIntFlag)
          *FlagDescriptions[F].UIntFlag =
              static_cast(FlagDescriptions[F].Default);
        if (FlagDescriptions[F].StrFlag)
          *FlagDescriptions[F].StrFlag = nullptr;
      }

      // Disable len_control by default, if LLVMFuzzerCustomMutator is used.
      if (EF->LLVMFuzzerCustomMutator) {
        Flags.len_control = 0;
        Printf("INFO: found LLVMFuzzerCustomMutator (%p). "
               "Disabling -len_control by default.\n", EF->LLVMFuzzerCustomMutator);
      }

      Inputs = new Vector;
      for (size_t A = 1; A < Args.size(); A++) {
        if (ParseOneFlag(Args[A].c_str())) {
          if (Flags.ignore_remaining_args)
            break;
          continue;
        }
        Inputs->push_back(Args[A]);
      }
    }

    static std::mutex Mu;

    static void PulseThread() {
      while (true) {
        SleepSeconds(600);
        std::lock_guard Lock(Mu);
        Printf("pulse...\n");
      }
    }

    static void WorkerThread(const Command &BaseCmd, std::atomic *Counter,
                             unsigned NumJobs, std::atomic *HasErrors) {
      while (true) {
        unsigned C = (*Counter)++;
        if (C >= NumJobs) break;
        std::string Log = "fuzz-" + std::to_string(C) + ".log";
        Command Cmd(BaseCmd);
        Cmd.setOutputFile(Log);
        Cmd.combineOutAndErr();
        if (Flags.verbosity) {
          std::string CommandLine = Cmd.toString();
          Printf("%s\n", CommandLine.c_str());
        }
        int ExitCode = ExecuteCommand(Cmd);
        if (ExitCode != 0)
          *HasErrors = true;
        std::lock_guard Lock(Mu);
        Printf("================== Job %u exited with exit code %d ============\n",
               C, ExitCode);
        fuzzer::CopyFileToErr(Log);
      }
    }

    std::string CloneArgsWithoutX(const Vector &Args,
                                  const char *X1, const char *X2) {
      std::string Cmd;
      for (auto &S : Args) {
        if (FlagValue(S.c_str(), X1) || FlagValue(S.c_str(), X2))
          continue;
        Cmd += S + " ";
      }
      return Cmd;
    }

    static int RunInMultipleProcesses(const Vector &Args,
                                      unsigned NumWorkers, unsigned NumJobs) {
      std::atomic Counter(0);
      std::atomic HasErrors(false);
      Command Cmd(Args);
      Cmd.removeFlag("jobs");
      Cmd.removeFlag("workers");
      Vector V;
      std::thread Pulse(PulseThread);
      Pulse.detach();
      for (unsigned i = 0; i < NumWorkers; i++)
        V.push_back(std::thread(WorkerThread, std::ref(Cmd), &Counter, NumJobs, &HasErrors));
      for (auto &T : V)
        T.join();
      return HasErrors ? 1 : 0;
    }

    static void RssThread(Fuzzer *F, size_t RssLimitMb) {
      while (true) {
        SleepSeconds(1);
        size_t Peak = GetPeakRSSMb();
        if (Peak > RssLimitMb)
          F->RssLimitCallback();
      }
    }

    static void StartRssThread(Fuzzer *F, size_t RssLimitMb) {
      if (!RssLimitMb)
        return;
      std::thread T(RssThread, F, RssLimitMb);
      T.detach();
    }

    int RunOneTest(Fuzzer *F, const char *InputFilePath, size_t MaxLen) {
      Unit U = FileToVector(InputFilePath);
      if (MaxLen && MaxLen < U.size())
        U.resize(MaxLen);
      F->ExecuteCallback(U.data(), U.size());
      F->TryDetectingAMemoryLeak(U.data(), U.size(), true);
      return 0;
    }

    static bool AllInputsAreFiles() {
      if (Inputs->empty()) return false;
      for (auto &Path : *Inputs)
        if (!IsFile(Path))
          return false;
      return true;
    }

    static std::string GetDedupTokenFromCmdOutput(const std::string &S) {
      auto Beg = S.find("DEDUP_TOKEN:");
      if (Beg == std::string::npos)
        return "";
      auto End = S.find('\n', Beg);
      if (End == std::string::npos)
        return "";
      return S.substr(Beg, End - Beg);
    }

    int CleanseCrashInput(const Vector &Args,
                           const FuzzingOptions &Options) {
      if (Inputs->size() != 1 || !Flags.exact_artifact_path) {
        Printf("ERROR: -cleanse_crash should be given one input file and"
              " -exact_artifact_path\n");
        exit(1);
      }
      std::string InputFilePath = Inputs->at(0);
      std::string OutputFilePath = Flags.exact_artifact_path;
      Command Cmd(Args);
      Cmd.removeFlag("cleanse_crash");

      assert(Cmd.hasArgument(InputFilePath));
      Cmd.removeArgument(InputFilePath);

      auto TmpFilePath = TempPath("CleanseCrashInput", ".repro");
      Cmd.addArgument(TmpFilePath);
      Cmd.setOutputFile(getDevNull());
      Cmd.combineOutAndErr();

      std::string CurrentFilePath = InputFilePath;
      auto U = FileToVector(CurrentFilePath);
      size_t Size = U.size();

      const Vector ReplacementBytes = {' ', 0xff};
      for (int NumAttempts = 0; NumAttempts < 5; NumAttempts++) {
        bool Changed = false;
        for (size_t Idx = 0; Idx < Size; Idx++) {
          Printf("CLEANSE[%d]: Trying to replace byte %zd of %zd\n", NumAttempts,
                 Idx, Size);
          uint8_t OriginalByte = U[Idx];
          if (ReplacementBytes.end() != std::find(ReplacementBytes.begin(),
                                                  ReplacementBytes.end(),
                                                  OriginalByte))
            continue;
          for (auto NewByte : ReplacementBytes) {
            U[Idx] = NewByte;
            WriteToFile(U, TmpFilePath);
            auto ExitCode = ExecuteCommand(Cmd);
            RemoveFile(TmpFilePath);
            if (!ExitCode) {
              U[Idx] = OriginalByte;
            } else {
              Changed = true;
              Printf("CLEANSE: Replaced byte %zd with 0x%x\n", Idx, NewByte);
              WriteToFile(U, OutputFilePath);
              break;
            }
          }
        }
        if (!Changed) break;
      }
      return 0;
    }

    int MinimizeCrashInput(const Vector &Args,
                           const FuzzingOptions &Options) {
      if (Inputs->size() != 1) {
        Printf("ERROR: -minimize_crash should be given one input file\n");
        exit(1);
      }
      std::string InputFilePath = Inputs->at(0);
      Command BaseCmd(Args);
      BaseCmd.removeFlag("minimize_crash");
      BaseCmd.removeFlag("exact_artifact_path");
      assert(BaseCmd.hasArgument(InputFilePath));
      BaseCmd.removeArgument(InputFilePath);
      if (Flags.runs <= 0 && Flags.max_total_time == 0) {
        Printf("INFO: you need to specify -runs=N or "
               "-max_total_time=N with -minimize_crash=1\n"
               "INFO: defaulting to -max_total_time=600\n");
        BaseCmd.addFlag("max_total_time", "600");
      }

      BaseCmd.combineOutAndErr();

      std::string CurrentFilePath = InputFilePath;
      while (true) {
        Unit U = FileToVector(CurrentFilePath);
        Printf("CRASH_MIN: minimizing crash input: '%s' (%zd bytes)\n",
               CurrentFilePath.c_str(), U.size());

        Command Cmd(BaseCmd);
        Cmd.addArgument(CurrentFilePath);

        Printf("CRASH_MIN: executing: %s\n", Cmd.toString().c_str());
        std::string CmdOutput;
        bool Success = ExecuteCommand(Cmd, &CmdOutput);
        if (Success) {
          Printf("ERROR: the input %s did not crash\n", CurrentFilePath.c_str());
          exit(1);
        }
        Printf("CRASH_MIN: '%s' (%zd bytes) caused a crash. Will try to minimize "
               "it further\n",
               CurrentFilePath.c_str(), U.size());
        auto DedupToken1 = GetDedupTokenFromCmdOutput(CmdOutput);
        if (!DedupToken1.empty())
          Printf("CRASH_MIN: DedupToken1: %s\n", DedupToken1.c_str());

        std::string ArtifactPath =
            Flags.exact_artifact_path
                ? Flags.exact_artifact_path
                : Options.ArtifactPrefix + "minimized-from-" + Hash(U);
        Cmd.addFlag("minimize_crash_internal_step", "1");
        Cmd.addFlag("exact_artifact_path", ArtifactPath);
        Printf("CRASH_MIN: executing: %s\n", Cmd.toString().c_str());
        CmdOutput.clear();
        Success = ExecuteCommand(Cmd, &CmdOutput);
        Printf("%s", CmdOutput.c_str());
        if (Success) {
          if (Flags.exact_artifact_path) {
            CurrentFilePath = Flags.exact_artifact_path;
            WriteToFile(U, CurrentFilePath);
          }
          Printf("CRASH_MIN: failed to minimize beyond %s (%d bytes), exiting\n",
                 CurrentFilePath.c_str(), U.size());
          break;
        }
        auto DedupToken2 = GetDedupTokenFromCmdOutput(CmdOutput);
        if (!DedupToken2.empty())
          Printf("CRASH_MIN: DedupToken2: %s\n", DedupToken2.c_str());

        if (DedupToken1 != DedupToken2) {
          if (Flags.exact_artifact_path) {
            CurrentFilePath = Flags.exact_artifact_path;
            WriteToFile(U, CurrentFilePath);
          }
          Printf("CRASH_MIN: mismatch in dedup tokens"
                 " (looks like a different bug). Won't minimize further\n");
          break;
        }

        CurrentFilePath = ArtifactPath;
        Printf("*********************************\n");
      }
      return 0;
    }

    int MinimizeCrashInputInternalStep(Fuzzer *F, InputCorpus *Corpus) {
      assert(Inputs->size() == 1);
      std::string InputFilePath = Inputs->at(0);
      Unit U = FileToVector(InputFilePath);
      Printf("INFO: Starting MinimizeCrashInputInternalStep: %zd\n", U.size());
      if (U.size() < 2) {
        Printf("INFO: The input is small enough, exiting\n");
        exit(0);
      }
      F->SetMaxInputLen(U.size());
      F->SetMaxMutationLen(U.size() - 1);
      F->MinimizeCrashLoop(U);
      Printf("INFO: Done MinimizeCrashInputInternalStep, no crashes found\n");
      exit(0);
      return 0;
    }

    void Merge(Fuzzer *F, FuzzingOptions &Options, const Vector &Args,
               const Vector &Corpora, const char *CFPathOrNull) {
      if (Corpora.size() < 2) {
        Printf("INFO: Merge requires two or more corpus dirs\n");
        exit(0);
      }

      Vector OldCorpus, NewCorpus;
      GetSizedFilesFromDir(Corpora[0], &OldCorpus);
      for (size_t i = 1; i < Corpora.size(); i++)
        GetSizedFilesFromDir(Corpora[i], &NewCorpus);
      std::sort(OldCorpus.begin(), OldCorpus.end());
      std::sort(NewCorpus.begin(), NewCorpus.end());

      std::string CFPath = CFPathOrNull ? CFPathOrNull : TempPath("Merge", ".txt");
      Vector NewFiles;
      Set NewFeatures, NewCov;
      CrashResistantMerge(Args, OldCorpus, NewCorpus, &NewFiles, {}, &NewFeatures,
                          {}, &NewCov, CFPath, true);
      for (auto &Path : NewFiles)
        F->WriteToOutputCorpus(FileToVector(Path, Options.MaxLen));
      // We are done, delete the control file if it was a temporary one.
      if (!Flags.merge_control_file)
        RemoveFile(CFPath);

      exit(0);
    }

    int AnalyzeDictionary(Fuzzer *F, const Vector& Dict,
                          UnitVector& Corpus) {
      Printf("Started dictionary minimization (up to %d tests)\n",
             Dict.size() * Corpus.size() * 2);

      // Scores and usage count for each dictionary unit.
      Vector Scores(Dict.size());
      Vector Usages(Dict.size());

      Vector InitialFeatures;
      Vector ModifiedFeatures;
      for (auto &C : Corpus) {
        // Get coverage for the testcase without modifications.
        F->ExecuteCallback(C.data(), C.size());
        InitialFeatures.clear();
        TPC.CollectFeatures([&](size_t Feature) {
          InitialFeatures.push_back(Feature);
        });

        for (size_t i = 0; i < Dict.size(); ++i) {
          Vector Data = C;
          auto StartPos = std::search(Data.begin(), Data.end(),
                                      Dict[i].begin(), Dict[i].end());
          // Skip dictionary unit, if the testcase does not contain it.
          if (StartPos == Data.end())
            continue;

          ++Usages[i];
          while (StartPos != Data.end()) {
            // Replace all occurrences of dictionary unit in the testcase.
            auto EndPos = StartPos + Dict[i].size();
            for (auto It = StartPos; It != EndPos; ++It)
              *It ^= 0xFF;

            StartPos = std::search(EndPos, Data.end(),
                                   Dict[i].begin(), Dict[i].end());
          }

          // Get coverage for testcase with masked occurrences of dictionary unit.
          F->ExecuteCallback(Data.data(), Data.size());
          ModifiedFeatures.clear();
          TPC.CollectFeatures([&](size_t Feature) {
            ModifiedFeatures.push_back(Feature);
          });

          if (InitialFeatures == ModifiedFeatures)
            --Scores[i];
          else
            Scores[i] += 2;
        }
      }

      Printf("###### Useless dictionary elements. ######\n");
      for (size_t i = 0; i < Dict.size(); ++i) {
        // Dictionary units with positive score are treated as useful ones.
        if (Scores[i] > 0)
           continue;

        Printf("\"");
        PrintASCII(Dict[i].data(), Dict[i].size(), "\"");
        Printf(" # Score: %d, Used: %d\n", Scores[i], Usages[i]);
      }
      Printf("###### End of useless dictionary elements. ######\n");
      return 0;
    }

    Vector ParseSeedInuts(const char *seed_inputs) {
      // Parse -seed_inputs=file1,file2,... or -seed_inputs=@seed_inputs_file
      Vector Files;
      if (!seed_inputs) return Files;
      std::string SeedInputs;
      if (Flags.seed_inputs[0] == '@')
        SeedInputs = FileToString(Flags.seed_inputs + 1); // File contains list.
      else
        SeedInputs = Flags.seed_inputs; // seed_inputs contains the list.
      if (SeedInputs.empty()) {
        Printf("seed_inputs is empty or @file does not exist.\n");
        exit(1);
      }
      // Parse SeedInputs.
      size_t comma_pos = 0;
      while ((comma_pos = SeedInputs.find_last_of(',')) != std::string::npos) {
        Files.push_back(SeedInputs.substr(comma_pos + 1));
        SeedInputs = SeedInputs.substr(0, comma_pos);
      }
      Files.push_back(SeedInputs);
      return Files;
    }

    static Vector ReadCorpora(const Vector &CorpusDirs,
        const Vector &ExtraSeedFiles) {
      Vector SizedFiles;
      size_t LastNumFiles = 0;
      for (auto &Dir : CorpusDirs) {
        GetSizedFilesFromDir(Dir, &SizedFiles);
        Printf("INFO: % 8zd files found in %s\n", SizedFiles.size() - LastNumFiles,
               Dir.c_str());
        LastNumFiles = SizedFiles.size();
      }
      for (auto &File : ExtraSeedFiles)
        if (auto Size = FileSize(File))
          SizedFiles.push_back({File, Size});
      return SizedFiles;
    }

    int FuzzerDriver(int *argc, char ***argv, UserCallback Callback) {
      using namespace fuzzer;
      assert(argc && argv && "Argument pointers cannot be nullptr");
      std::string Argv0((*argv)[0]);
      if (!EF)
        EF = new ExternalFunctions();
      if (EF->LLVMFuzzerInitialize)
        EF->LLVMFuzzerInitialize(argc, argv);
      if (EF->__msan_scoped_disable_interceptor_checks)
        EF->__msan_scoped_disable_interceptor_checks();
      const Vector Args(*argv, *argv + *argc);
      assert(!Args.empty());
      ProgName = new std::string(Args[0]);
      if (Argv0 != *ProgName) {
        Printf("ERROR: argv[0] has been modified in LLVMFuzzerInitialize\n");
        exit(1);
      }
      ParseFlags(Args, EF);
      if (Flags.help) {
        PrintHelp();
        return 0;
      }

      if (Flags.close_fd_mask & 2)
        DupAndCloseStderr();
      if (Flags.close_fd_mask & 1)
        CloseStdout();

      if (Flags.jobs > 0 && Flags.workers == 0) {
        Flags.workers = std::min(NumberOfCpuCores() / 2, Flags.jobs);
        if (Flags.workers > 1)
          Printf("Running %u workers\n", Flags.workers);
      }

      if (Flags.workers > 0 && Flags.jobs > 0)
        return RunInMultipleProcesses(Args, Flags.workers, Flags.jobs);

      FuzzingOptions Options;
      Options.Verbosity = Flags.verbosity;
      Options.MaxLen = Flags.max_len;
      Options.LenControl = Flags.len_control;
      Options.UnitTimeoutSec = Flags.timeout;
      Options.ErrorExitCode = Flags.error_exitcode;
      Options.TimeoutExitCode = Flags.timeout_exitcode;
      Options.IgnoreTimeouts = Flags.ignore_timeouts;
      Options.IgnoreOOMs = Flags.ignore_ooms;
      Options.IgnoreCrashes = Flags.ignore_crashes;
      Options.MaxTotalTimeSec = Flags.max_total_time;
      Options.DoCrossOver = Flags.cross_over;
      Options.MutateDepth = Flags.mutate_depth;
      Options.ReduceDepth = Flags.reduce_depth;
      Options.UseCounters = Flags.use_counters;
      Options.UseMemmem = Flags.use_memmem;
      Options.UseCmp = Flags.use_cmp;
      Options.UseValueProfile = Flags.use_value_profile;
      Options.Shrink = Flags.shrink;
      Options.ReduceInputs = Flags.reduce_inputs;
      Options.ShuffleAtStartUp = Flags.shuffle;
      Options.PreferSmall = Flags.prefer_small;
      Options.ReloadIntervalSec = Flags.reload;
      Options.OnlyASCII = Flags.only_ascii;
      Options.DetectLeaks = Flags.detect_leaks;
      Options.PurgeAllocatorIntervalSec = Flags.purge_allocator_interval;
      Options.TraceMalloc = Flags.trace_malloc;
      Options.RssLimitMb = Flags.rss_limit_mb;
      Options.MallocLimitMb = Flags.malloc_limit_mb;
      if (!Options.MallocLimitMb)
        Options.MallocLimitMb = Options.RssLimitMb;
      if (Flags.runs >= 0)
        Options.MaxNumberOfRuns = Flags.runs;
      if (!Inputs->empty() && !Flags.minimize_crash_internal_step)
        Options.OutputCorpus = (*Inputs)[0];
      Options.ReportSlowUnits = Flags.report_slow_units;
      if (Flags.artifact_prefix)
        Options.ArtifactPrefix = Flags.artifact_prefix;
      if (Flags.exact_artifact_path)
        Options.ExactArtifactPath = Flags.exact_artifact_path;
      Vector Dictionary;
      if (Flags.dict)
        if (!ParseDictionaryFile(FileToString(Flags.dict), &Dictionary))
          return 1;
      if (Flags.verbosity > 0 && !Dictionary.empty())
        Printf("Dictionary: %zd entries\n", Dictionary.size());
      bool RunIndividualFiles = AllInputsAreFiles();
      Options.SaveArtifacts =
          !RunIndividualFiles || Flags.minimize_crash_internal_step;
      Options.PrintNewCovPcs = Flags.print_pcs;
      Options.PrintNewCovFuncs = Flags.print_funcs;
      Options.PrintFinalStats = Flags.print_final_stats;
      Options.PrintCorpusStats = Flags.print_corpus_stats;
      Options.PrintCoverage = Flags.print_coverage;
      if (Flags.exit_on_src_pos)
        Options.ExitOnSrcPos = Flags.exit_on_src_pos;
      if (Flags.exit_on_item)
        Options.ExitOnItem = Flags.exit_on_item;
      if (Flags.focus_function)
        Options.FocusFunction = Flags.focus_function;
      if (Flags.data_flow_trace)
        Options.DataFlowTrace = Flags.data_flow_trace;
      if (Flags.features_dir)
        Options.FeaturesDir = Flags.features_dir;
      if (Flags.collect_data_flow)
        Options.CollectDataFlow = Flags.collect_data_flow;
      if (Flags.stop_file)
        Options.StopFile = Flags.stop_file;
      Options.Entropic = Flags.entropic;
      Options.EntropicFeatureFrequencyThreshold =
          (size_t)Flags.entropic_feature_frequency_threshold;
      Options.EntropicNumberOfRarestFeatures =
          (size_t)Flags.entropic_number_of_rarest_features;
      if (Options.Entropic) {
        if (!Options.FocusFunction.empty()) {
          Printf("ERROR: The parameters `--entropic` and `--focus_function` cannot "
                 "be used together.\n");
          exit(1);
        }
        Printf("INFO: Running with entropic power schedule (0x%X, %d).\n",
               Options.EntropicFeatureFrequencyThreshold,
               Options.EntropicNumberOfRarestFeatures);
      }
      struct EntropicOptions Entropic;
      Entropic.Enabled = Options.Entropic;
      Entropic.FeatureFrequencyThreshold =
          Options.EntropicFeatureFrequencyThreshold;
      Entropic.NumberOfRarestFeatures = Options.EntropicNumberOfRarestFeatures;

      unsigned Seed = Flags.seed;
      // Initialize Seed.
      if (Seed == 0)
        Seed =
            std::chrono::system_clock::now().time_since_epoch().count() + GetPid();
      if (Flags.verbosity)
        Printf("INFO: Seed: %u\n", Seed);

      if (Flags.collect_data_flow && !Flags.fork && !Flags.merge) {
        if (RunIndividualFiles)
          return CollectDataFlow(Flags.collect_data_flow, Flags.data_flow_trace,
                            ReadCorpora({}, *Inputs));
        else
          return CollectDataFlow(Flags.collect_data_flow, Flags.data_flow_trace,
                            ReadCorpora(*Inputs, {}));
      }

      Random Rand(Seed);
      auto *MD = new MutationDispatcher(Rand, Options);
      auto *Corpus = new InputCorpus(Options.OutputCorpus, Entropic);
      auto *F = new Fuzzer(Callback, *Corpus, *MD, Options);

      for (auto &U: Dictionary)
        if (U.size() <= Word::GetMaxSize())
          MD->AddWordToManualDictionary(Word(U.data(), U.size()));

          // Threads are only supported by Chrome. Don't use them with emscripten
          // for now.
    #if !LIBFUZZER_EMSCRIPTEN
      StartRssThread(F, Flags.rss_limit_mb);
    #endif // LIBFUZZER_EMSCRIPTEN

      Options.HandleAbrt = Flags.handle_abrt;
      Options.HandleBus = Flags.handle_bus;
      Options.HandleFpe = Flags.handle_fpe;
      Options.HandleIll = Flags.handle_ill;
      Options.HandleInt = Flags.handle_int;
      Options.HandleSegv = Flags.handle_segv;
      Options.HandleTerm = Flags.handle_term;
      Options.HandleXfsz = Flags.handle_xfsz;
      Options.HandleUsr1 = Flags.handle_usr1;
      Options.HandleUsr2 = Flags.handle_usr2;
      SetSignalHandler(Options);

      std::atexit(Fuzzer::StaticExitCallback);

      if (Flags.minimize_crash)
        return MinimizeCrashInput(Args, Options);

      if (Flags.minimize_crash_internal_step)
        return MinimizeCrashInputInternalStep(F, Corpus);

      if (Flags.cleanse_crash)
        return CleanseCrashInput(Args, Options);

      if (RunIndividualFiles) {
        Options.SaveArtifacts = false;
        int Runs = std::max(1, Flags.runs);
        Printf("%s: Running %zd inputs %d time(s) each.\n", ProgName->c_str(),
               Inputs->size(), Runs);
        for (auto &Path : *Inputs) {
          auto StartTime = system_clock::now();
          Printf("Running: %s\n", Path.c_str());
          for (int Iter = 0; Iter < Runs; Iter++)
            RunOneTest(F, Path.c_str(), Options.MaxLen);
          auto StopTime = system_clock::now();
          auto MS = duration_cast(StopTime - StartTime).count();
          Printf("Executed %s in %zd ms\n", Path.c_str(), (long)MS);
        }
        Printf("***\n"
               "*** NOTE: fuzzing was not performed, you have only\n"
               "***       executed the target code on a fixed set of inputs.\n"
               "***\n");
        F->PrintFinalStats();
        exit(0);
      }

      if (Flags.fork)
        FuzzWithFork(F->GetMD().GetRand(), Options, Args, *Inputs, Flags.fork);

      if (Flags.merge)
        Merge(F, Options, Args, *Inputs, Flags.merge_control_file);

      if (Flags.merge_inner) {
        const size_t kDefaultMaxMergeLen = 1 << 20;
        if (Options.MaxLen == 0)
          F->SetMaxInputLen(kDefaultMaxMergeLen);
        assert(Flags.merge_control_file);
        F->CrashResistantMergeInternalStep(Flags.merge_control_file);
        exit(0);
      }

      if (Flags.analyze_dict) {
        size_t MaxLen = INT_MAX;  // Large max length.
        UnitVector InitialCorpus;
        for (auto &Inp : *Inputs) {
          Printf("Loading corpus dir: %s\n", Inp.c_str());
          ReadDirToVectorOfUnits(Inp.c_str(), &InitialCorpus, nullptr,
                                 MaxLen, /*ExitOnError=*/false);
        }

        if (Dictionary.empty() || Inputs->empty()) {
          Printf("ERROR: can't analyze dict without dict and corpus provided\n");
          return 1;
        }
        if (AnalyzeDictionary(F, Dictionary, InitialCorpus)) {
          Printf("Dictionary analysis failed\n");
          exit(1);
        }
        Printf("Dictionary analysis succeeded\n");
        exit(0);
      }

      auto CorporaFiles = ReadCorpora(*Inputs, ParseSeedInuts(Flags.seed_inputs));
      F->Loop(CorporaFiles);

      if (Flags.verbosity)
        Printf("Done %zd runs in %zd second(s)\n", F->getTotalNumberOfRuns(),
               F->secondsSinceProcessStartUp());
      F->PrintFinalStats();

      exit(0);  // Don't let F destroy itself.
    }

    extern "C" ATTRIBUTE_INTERFACE int
    LLVMFuzzerRunDriver(int *argc, char ***argv,
                        int (*UserCb)(const uint8_t *Data, size_t Size)) {
      return FuzzerDriver(argc, argv, UserCb);
    }

    // Storage for global ExternalFunctions object.
    ExternalFunctions *EF = nullptr;

    }  // namespace fuzzer

## Minimal test - lines (35, 35)

path: .spaces[0].metrics.mi.mi_original
old: 98.67820366598298
new: 159.95690398326485
path: .spaces[0].metrics.mi.mi_sei
old: 66.76352294013648
new: 155.17000000000002
path: .spaces[0].metrics.mi.mi_visual_studio
old: 57.70655185145203
new: 93.54204911302038
path: .spaces[0].metrics.halstead.purity_ratio
old: 1.0212231961439342
new: 1.188721875540867
path: .spaces[0].metrics.halstead.effort
old: 4255.464758157069
new: 12.0
path: .spaces[0].metrics.halstead.time
old: 236.41470878650384
new: 0.6666666666666666
path: .spaces[0].metrics.halstead.N1
old: 55.0
new: 3.0
path: .spaces[0].metrics.halstead.estimated_program_length
old: 99.0586500259616
new: 4.754887502163468
path: .spaces[0].metrics.halstead.N2
old: 42.0
new: 1.0
path: .spaces[0].metrics.halstead.volume
old: 455.9426526596859
new: 8.0
path: .spaces[0].metrics.halstead.n1
old: 8.0
new: 3.0
path: .spaces[0].metrics.halstead.n2
old: 18.0
new: 1.0
path: .spaces[0].metrics.halstead.bugs
old: 0.08753399383893205
new: 0.0017471609294725976
path: .spaces[0].metrics.halstead.difficulty
old: 9.333333333333334
new: 1.5
path: .spaces[0].metrics.halstead.vocabulary
old: 26.0
new: 4.0
path: .spaces[0].metrics.halstead.level
old: 0.10714285714285714
new: 0.6666666666666666
path: .spaces[0].metrics.halstead.length
old: 97.0
new: 4.0
path: .spaces[0].metrics.loc.lloc
old: 6.0
new: 0.0
path: .spaces[0].metrics.loc.ploc
old: 12.0
new: 1.0
path: .spaces[0].metrics.loc.sloc
old: 12.0
new: 1.0
path: .spaces[0].metrics.nargs.sum
old: 7.0
new: 0.0
path: .spaces[0].metrics.nargs.average
old: 7.0
new: 0.0

### Code

    extern "C" void __libfuzzer_is_present() {}

## Minimal test - lines (42, 42)

path: .spaces[1].metrics.nexits.average
old: 1.0
new: 0.0
path: .spaces[1].metrics.nexits.sum
old: 1.0
new: 0.0
path: .spaces[1].metrics.halstead.difficulty
old: 4.3125
new: 1.5
path: .spaces[1].metrics.halstead.volume
old: 187.29612798276648
new: 16.253496664211536
path: .spaces[1].metrics.halstead.effort
old: 807.7145519256804
new: 24.380244996317305
path: .spaces[1].metrics.halstead.vocabulary
old: 22.0
new: 5.0
path: .spaces[1].metrics.halstead.N1
old: 19.0
new: 5.0
path: .spaces[1].metrics.halstead.level
old: 0.2318840579710145
new: 0.6666666666666666
path: .spaces[1].metrics.halstead.N2
old: 23.0
new: 2.0
path: .spaces[1].metrics.halstead.n1
old: 6.0
new: 3.0
path: .spaces[1].metrics.halstead.n2
old: 16.0
new: 2.0
path: .spaces[1].metrics.halstead.bugs
old: 0.028910172530378215
new: 0.002802662411680566
path: .spaces[1].metrics.halstead.purity_ratio
old: 1.893089881055403
new: 0.9649839288804954
path: .spaces[1].metrics.halstead.length
old: 42.0
new: 7.0
path: .spaces[1].metrics.halstead.time
old: 44.8730306625378
new: 1.3544580553509613
path: .spaces[1].metrics.halstead.estimated_program_length
old: 79.50977500432694
new: 6.754887502163468
path: .spaces[1].metrics.nargs.sum
old: 1.0
new: 0.0
path: .spaces[1].metrics.nargs.average
old: 1.0
new: 0.0
path: .spaces[1].metrics.loc.ploc
old: 6.0
new: 1.0
path: .spaces[1].metrics.loc.sloc
old: 6.0
new: 1.0
path: .spaces[1].metrics.loc.lloc
old: 1.0
new: 0.0
path: .spaces[1].metrics.mi.mi_original
old: 114.53350372908623
new: 156.27079806218674
path: .spaces[1].metrics.mi.mi_sei
old: 89.63788571298204
new: 149.85207326746917
path: .spaces[1].metrics.mi.mi_visual_studio
old: 66.97865715151242
new: 91.3864316153139

### Code

    extern "C" __attribute__((used)) void __libfuzzer_is_present() {}