# Global Metrics

path: .metrics.nexits.sum
old: 0.0
new: 44.0
path: .metrics.nexits.average
old: null
new: 0.8301886792452831
path: .metrics.nargs.average
old: null
new: 1.7169811320754718
path: .metrics.nargs.sum
old: 0.0
new: 91.0
path: .metrics.loc.sloc
old: 105.0
new: 657.0
path: .metrics.loc.blank
old: 15.0
new: 47.0
path: .metrics.loc.ploc
old: 37.0
new: 542.0
path: .metrics.loc.lloc
old: 0.0
new: 230.0
path: .metrics.loc.cloc
old: 53.0
new: 68.0
path: .metrics.mi.mi_original
old: 60.69081962451981
new: -22.46960578659268
path: .metrics.mi.mi_sei
old: 56.636213642086126
new: -68.63819498777183
path: .metrics.mi.mi_visual_studio
old: 35.49170738276012
new: 0.0
path: .metrics.cyclomatic.sum
old: 2.0
new: 153.0
path: .metrics.cyclomatic.average
old: 1.0
new: 3.0
path: .metrics.nom.closures
old: 0.0
new: 4.0
path: .metrics.nom.total
old: 0.0
new: 53.0
path: .metrics.nom.functions
old: 0.0
new: 49.0
path: .metrics.cognitive.sum
old: 0.0
new: 141.0
path: .metrics.cognitive.average
old: null
new: 2.660377358490566
path: .metrics.halstead.N1
old: 74.0
new: 2027.0
path: .metrics.halstead.effort
old: 4140.606665902355
new: 2590778.281189028
path: .metrics.halstead.n1
old: 8.0
new: 43.0
path: .metrics.halstead.N2
old: 59.0
new: 1269.0
path: .metrics.halstead.volume
old: 754.4325704822088
new: 27632.689347994474
path: .metrics.halstead.n2
old: 43.0
new: 291.0
path: .metrics.halstead.time
old: 230.03370366124196
new: 143932.1267327238
path: .metrics.halstead.vocabulary
old: 51.0
new: 334.0
path: .metrics.halstead.difficulty
old: 5.488372093023256
new: 93.75773195876288
path: .metrics.halstead.length
old: 133.0
new: 3296.0
path: .metrics.halstead.level
old: 0.1822033898305085
new: 0.01066578701412942
path: .metrics.halstead.bugs
old: 0.08595175127750572
new: 6.287802765461047
path: .metrics.halstead.purity_ratio
old: 1.934807401896167
new: 0.7934247904243024
path: .metrics.halstead.estimated_program_length
old: 257.3293844521902
new: 2615.1281092385007

# Spaces Data

## Minimal test - lines (30, 411)

path: .spaces[0].metrics.nom.closures
old: 0.0
new: 4.0
path: .spaces[0].metrics.nom.total
old: 0.0
new: 27.0
path: .spaces[0].metrics.nom.functions
old: 0.0
new: 23.0
path: .spaces[0].metrics.halstead.difficulty
old: 5.538461538461538
new: 79.875
path: .spaces[0].metrics.halstead.level
old: 0.18055555555555555
new: 0.012519561815336464
path: .spaces[0].metrics.halstead.n2
old: 39.0
new: 224.0
path: .spaces[0].metrics.halstead.purity_ratio
old: 1.797895988559591
new: 0.931756997310194
path: .spaces[0].metrics.halstead.effort
old: 3937.776219773931
new: 1364041.251215594
path: .spaces[0].metrics.halstead.bugs
old: 0.08312138415560434
new: 4.099810168146815
path: .spaces[0].metrics.halstead.time
old: 218.7653455429962
new: 75780.06951197743
path: .spaces[0].metrics.halstead.volume
old: 710.9873730147376
new: 17077.19876326252
path: .spaces[0].metrics.halstead.n1
old: 8.0
new: 42.0
path: .spaces[0].metrics.halstead.length
old: 128.0
new: 2120.0
path: .spaces[0].metrics.halstead.vocabulary
old: 47.0
new: 266.0
path: .spaces[0].metrics.halstead.estimated_program_length
old: 230.1306865356277
new: 1975.3248342976112
path: .spaces[0].metrics.halstead.N1
old: 74.0
new: 1268.0
path: .spaces[0].metrics.halstead.N2
old: 54.0
new: 852.0
path: .spaces[0].metrics.loc.sloc
old: 86.0
new: 382.0
path: .spaces[0].metrics.loc.cloc
old: 46.0
new: 31.0
path: .spaces[0].metrics.loc.ploc
old: 29.0
new: 323.0
path: .spaces[0].metrics.loc.blank
old: 11.0
new: 28.0
path: .spaces[0].metrics.loc.lloc
old: 0.0
new: 157.0
path: .spaces[0].metrics.mi.mi_visual_studio
old: 37.697642991693385
new: 0.0
path: .spaces[0].metrics.mi.mi_original
old: 64.46296951579569
new: -0.14241098412408348
path: .spaces[0].metrics.mi.mi_sei
old: 62.68607784445133
new: -43.85850001964986
path: .spaces[0].metrics.cyclomatic.average
old: 1.0
new: 4.375
path: .spaces[0].metrics.cyclomatic.sum
old: 1.0
new: 105.0
path: .spaces[0].metrics.nargs.sum
old: 0.0
new: 25.0
path: .spaces[0].metrics.nargs.average
old: null
new: 0.925925925925926
path: .spaces[0].metrics.nexits.average
old: null
new: 0.9629629629629628
path: .spaces[0].metrics.nexits.sum
old: 0.0
new: 26.0
path: .spaces[0].metrics.cognitive.average
old: null
new: 4.333333333333333
path: .spaces[0].metrics.cognitive.sum
old: 0.0
new: 117.0

### Code

    namespace fuzzer {

    TracePC TPC;

    size_t TracePC::GetTotalPCCoverage() {
      return ObservedPCs.size();
    }

    void TracePC::HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop) {
      if (Start == Stop) return;
      if (NumModules &&
          Modules[NumModules - 1].Start() == Start)
        return;
      assert(NumModules <
             sizeof(Modules) / sizeof(Modules[0]));
      auto &M = Modules[NumModules++];
      uint8_t *AlignedStart = RoundUpByPage(Start);
      uint8_t *AlignedStop  = RoundDownByPage(Stop);
      size_t NumFullPages = AlignedStop > AlignedStart ?
                            (AlignedStop - AlignedStart) / PageSize() : 0;
      bool NeedFirst = Start < AlignedStart || !NumFullPages;
      bool NeedLast  = Stop > AlignedStop && AlignedStop >= AlignedStart;
      M.NumRegions = NumFullPages + NeedFirst + NeedLast;;
      assert(M.NumRegions > 0);
      M.Regions = new Module::Region[M.NumRegions];
      assert(M.Regions);
      size_t R = 0;
      if (NeedFirst)
        M.Regions[R++] = {Start, std::min(Stop, AlignedStart), true, false};
      for (uint8_t *P = AlignedStart; P < AlignedStop; P += PageSize())
        M.Regions[R++] = {P, P + PageSize(), true, true};
      if (NeedLast)
        M.Regions[R++] = {AlignedStop, Stop, true, false};
      assert(R == M.NumRegions);
      assert(M.Size() == (size_t)(Stop - Start));
      assert(M.Stop() == Stop);
      assert(M.Start() == Start);
      NumInline8bitCounters += M.Size();
    }

    void TracePC::HandlePCsInit(const uintptr_t *Start, const uintptr_t *Stop) {
      const PCTableEntry *B = reinterpret_cast(Start);
      const PCTableEntry *E = reinterpret_cast(Stop);
      if (NumPCTables && ModulePCTable[NumPCTables - 1].Start == B) return;
      assert(NumPCTables < sizeof(ModulePCTable) / sizeof(ModulePCTable[0]));
      ModulePCTable[NumPCTables++] = {B, E};
      NumPCsInPCTables += E - B;
    }

    void TracePC::PrintModuleInfo() {
      if (NumModules) {
        Printf("INFO: Loaded %zd modules   (%zd inline 8-bit counters): ",
               NumModules, NumInline8bitCounters);
        for (size_t i = 0; i < NumModules; i++)
          Printf("%zd [%p, %p), ", Modules[i].Size(), Modules[i].Start(),
                 Modules[i].Stop());
        Printf("\n");
      }
      if (NumPCTables) {
        Printf("INFO: Loaded %zd PC tables (%zd PCs): ", NumPCTables,
               NumPCsInPCTables);
        for (size_t i = 0; i < NumPCTables; i++) {
          Printf("%zd [%p,%p), ", ModulePCTable[i].Stop - ModulePCTable[i].Start,
                 ModulePCTable[i].Start, ModulePCTable[i].Stop);
        }
        Printf("\n");

        if (NumInline8bitCounters && NumInline8bitCounters != NumPCsInPCTables) {
          Printf("ERROR: The size of coverage PC tables does not match the\n"
                 "number of instrumented PCs. This might be a compiler bug,\n"
                 "please contact the libFuzzer developers.\n"
                 "Also check https://bugs.llvm.org/show_bug.cgi?id=34636\n"
                 "for possible workarounds (tl;dr: don't use the old GNU ld)\n");
          _Exit(1);
        }
      }
      if (size_t NumExtraCounters = ExtraCountersEnd() - ExtraCountersBegin())
        Printf("INFO: %zd Extra Counters\n", NumExtraCounters);
    }

    ATTRIBUTE_NO_SANITIZE_ALL
    void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
      const uintptr_t kBits = 12;
      const uintptr_t kMask = (1 << kBits) - 1;
      uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits);
      ValueProfileMap.AddValueModPrime(Idx);
    }

    /// \return the address of the previous instruction.
    /// Note: the logic is copied from `sanitizer_common/sanitizer_stacktrace.h`
    inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) {
    #if defined(__arm__)
      // T32 (Thumb) branch instructions might be 16 or 32 bit long,
      // so we return (pc-2) in that case in order to be safe.
      // For A32 mode we return (pc-4) because all instructions are 32 bit long.
      return (PC - 3) & (~1);
    #elif defined(__powerpc__) || defined(__powerpc64__) || defined(__aarch64__)
      // PCs are always 4 byte aligned.
      return PC - 4;
    #elif defined(__sparc__) || defined(__mips__)
      return PC - 8;
    #else
      return PC - 1;
    #endif
    }

    /// \return the address of the next instruction.
    /// Note: the logic is copied from `sanitizer_common/sanitizer_stacktrace.cpp`
    ALWAYS_INLINE uintptr_t TracePC::GetNextInstructionPc(uintptr_t PC) {
    #if defined(__mips__)
      return PC + 8;
    #elif defined(__powerpc__) || defined(__sparc__) || defined(__arm__) || \
        defined(__aarch64__)
      return PC + 4;
    #else
      return PC + 1;
    #endif
    }

    void TracePC::UpdateObservedPCs() {
      Vector CoveredFuncs;
      auto ObservePC = [&](const PCTableEntry *TE) {
        if (ObservedPCs.insert(TE).second && DoPrintNewPCs) {
          PrintPC("\tNEW_PC: %p %F %L", "\tNEW_PC: %p",
                  GetNextInstructionPc(TE->PC));
          Printf("\n");
        }
      };

      auto Observe = [&](const PCTableEntry *TE) {
        if (PcIsFuncEntry(TE))
          if (++ObservedFuncs[TE->PC] == 1 && NumPrintNewFuncs)
            CoveredFuncs.push_back(TE->PC);
        ObservePC(TE);
      };

      if (NumPCsInPCTables) {
        if (NumInline8bitCounters == NumPCsInPCTables) {
          for (size_t i = 0; i < NumModules; i++) {
            auto &M = Modules[i];
            assert(M.Size() ==
                   (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start));
            for (size_t r = 0; r < M.NumRegions; r++) {
              auto &R = M.Regions[r];
              if (!R.Enabled) continue;
              for (uint8_t *P = R.Start; P < R.Stop; P++)
                if (*P)
                  Observe(&ModulePCTable[i].Start[M.Idx(P)]);
            }
          }
        }
      }

      for (size_t i = 0, N = Min(CoveredFuncs.size(), NumPrintNewFuncs); i < N;
           i++) {
        Printf("\tNEW_FUNC[%zd/%zd]: ", i + 1, CoveredFuncs.size());
        PrintPC("%p %F %L", "%p", GetNextInstructionPc(CoveredFuncs[i]));
        Printf("\n");
      }
    }

    uintptr_t TracePC::PCTableEntryIdx(const PCTableEntry *TE) {
      size_t TotalTEs = 0;
      for (size_t i = 0; i < NumPCTables; i++) {
        auto &M = ModulePCTable[i];
        if (TE >= M.Start && TE < M.Stop)
          return TotalTEs + TE - M.Start;
        TotalTEs += M.Stop - M.Start;
      }
      assert(0);
      return 0;
    }

    const TracePC::PCTableEntry *TracePC::PCTableEntryByIdx(uintptr_t Idx) {
      for (size_t i = 0; i < NumPCTables; i++) {
        auto &M = ModulePCTable[i];
        size_t Size = M.Stop - M.Start;
        if (Idx < Size) return &M.Start[Idx];
        Idx -= Size;
      }
      return nullptr;
    }

    static std::string GetModuleName(uintptr_t PC) {
      char ModulePathRaw[4096] = "";  // What's PATH_MAX in portable C++?
      void *OffsetRaw = nullptr;
      if (!EF->__sanitizer_get_module_and_offset_for_pc(
          reinterpret_cast(PC), ModulePathRaw,
          sizeof(ModulePathRaw), &OffsetRaw))
        return "";
      return ModulePathRaw;
    }

    template
    void TracePC::IterateCoveredFunctions(CallBack CB) {
      for (size_t i = 0; i < NumPCTables; i++) {
        auto &M = ModulePCTable[i];
        assert(M.Start < M.Stop);
        auto ModuleName = GetModuleName(M.Start->PC);
        for (auto NextFE = M.Start; NextFE < M.Stop; ) {
          auto FE = NextFE;
          assert(PcIsFuncEntry(FE) && "Not a function entry point");
          do {
            NextFE++;
          } while (NextFE < M.Stop && !(PcIsFuncEntry(NextFE)));
          CB(FE, NextFE, ObservedFuncs[FE->PC]);
        }
      }
    }

    void TracePC::SetFocusFunction(const std::string &FuncName) {
      // This function should be called once.
      assert(!FocusFunctionCounterPtr);
      // "auto" is not a valid function name. If this function is called with "auto"
      // that means the auto focus functionality failed.
      if (FuncName.empty() || FuncName == "auto")
        return;
      for (size_t M = 0; M < NumModules; M++) {
        auto &PCTE = ModulePCTable[M];
        size_t N = PCTE.Stop - PCTE.Start;
        for (size_t I = 0; I < N; I++) {
          if (!(PcIsFuncEntry(&PCTE.Start[I]))) continue;  // not a function entry.
          auto Name = DescribePC("%F", GetNextInstructionPc(PCTE.Start[I].PC));
          if (Name[0] == 'i' && Name[1] == 'n' && Name[2] == ' ')
            Name = Name.substr(3, std::string::npos);
          if (FuncName != Name) continue;
          Printf("INFO: Focus function is set to '%s'\n", Name.c_str());
          FocusFunctionCounterPtr = Modules[M].Start() + I;
          return;
        }
      }

      Printf("ERROR: Failed to set focus function. Make sure the function name is "
             "valid (%s) and symbolization is enabled.\n", FuncName.c_str());
      exit(1);
    }

    bool TracePC::ObservedFocusFunction() {
      return FocusFunctionCounterPtr && *FocusFunctionCounterPtr;
    }

    void TracePC::PrintCoverage() {
      if (!EF->__sanitizer_symbolize_pc ||
          !EF->__sanitizer_get_module_and_offset_for_pc) {
        Printf("INFO: __sanitizer_symbolize_pc or "
               "__sanitizer_get_module_and_offset_for_pc is not available,"
               " not printing coverage\n");
        return;
      }
      Printf("COVERAGE:\n");
      auto CoveredFunctionCallback = [&](const PCTableEntry *First,
                                         const PCTableEntry *Last,
                                         uintptr_t Counter) {
        assert(First < Last);
        auto VisualizePC = GetNextInstructionPc(First->PC);
        std::string FileStr = DescribePC("%s", VisualizePC);
        if (!IsInterestingCoverageFile(FileStr))
          return;
        std::string FunctionStr = DescribePC("%F", VisualizePC);
        if (FunctionStr.find("in ") == 0)
          FunctionStr = FunctionStr.substr(3);
        std::string LineStr = DescribePC("%l", VisualizePC);
        size_t NumEdges = Last - First;
        Vector UncoveredPCs;
        for (auto TE = First; TE < Last; TE++)
          if (!ObservedPCs.count(TE))
            UncoveredPCs.push_back(TE->PC);
        Printf("%sCOVERED_FUNC: hits: %zd", Counter ? "" : "UN", Counter);
        Printf(" edges: %zd/%zd", NumEdges - UncoveredPCs.size(), NumEdges);
        Printf(" %s %s:%s\n", FunctionStr.c_str(), FileStr.c_str(),
               LineStr.c_str());
        if (Counter)
          for (auto PC : UncoveredPCs)
            Printf("  UNCOVERED_PC: %s\n",
                   DescribePC("%s:%l", GetNextInstructionPc(PC)).c_str());
      };

      IterateCoveredFunctions(CoveredFunctionCallback);
    }

    // Value profile.
    // We keep track of various values that affect control flow.
    // These values are inserted into a bit-set-based hash map.
    // Every new bit in the map is treated as a new coverage.
    //
    // For memcmp/strcmp/etc the interesting value is the length of the common
    // prefix of the parameters.
    // For cmp instructions the interesting value is a XOR of the parameters.
    // The interesting value is mixed up with the PC and is then added to the map.

    ATTRIBUTE_NO_SANITIZE_ALL
    void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
                                    size_t n, bool StopAtZero) {
      if (!n) return;
      size_t Len = std::min(n, Word::GetMaxSize());
      const uint8_t *A1 = reinterpret_cast(s1);
      const uint8_t *A2 = reinterpret_cast(s2);
      uint8_t B1[Word::kMaxSize];
      uint8_t B2[Word::kMaxSize];
      // Copy the data into locals in this non-msan-instrumented function
      // to avoid msan complaining further.
      size_t Hash = 0;  // Compute some simple hash of both strings.
      for (size_t i = 0; i < Len; i++) {
        B1[i] = A1[i];
        B2[i] = A2[i];
        size_t T = B1[i];
        Hash ^= (T << 8) | B2[i];
      }
      size_t I = 0;
      uint8_t HammingDistance = 0;
      for (; I < Len; I++) {
        if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0)) {
          HammingDistance = Popcountll(B1[I] ^ B2[I]);
          break;
        }
      }
      size_t PC = reinterpret_cast(caller_pc);
      size_t Idx = (PC & 4095) | (I << 12);
      Idx += HammingDistance;
      ValueProfileMap.AddValue(Idx);
      TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len));
    }

    template
    ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE
    ATTRIBUTE_NO_SANITIZE_ALL
    void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) {
      uint64_t ArgXor = Arg1 ^ Arg2;
      if (sizeof(T) == 4)
          TORC4.Insert(ArgXor, Arg1, Arg2);
      else if (sizeof(T) == 8)
          TORC8.Insert(ArgXor, Arg1, Arg2);
      uint64_t HammingDistance = Popcountll(ArgXor);  // [0,64]
      uint64_t AbsoluteDistance = (Arg1 == Arg2 ? 0 : Clzll(Arg1 - Arg2) + 1);
      ValueProfileMap.AddValue(PC * 128 + HammingDistance);
      ValueProfileMap.AddValue(PC * 128 + 64 + AbsoluteDistance);
    }

    static size_t InternalStrnlen(const char *S, size_t MaxLen) {
      size_t Len = 0;
      for (; Len < MaxLen && S[Len]; Len++) {}
      return Len;
    }

    // Finds min of (strlen(S1), strlen(S2)).
    // Needed bacause one of these strings may actually be non-zero terminated.
    static size_t InternalStrnlen2(const char *S1, const char *S2) {
      size_t Len = 0;
      for (; S1[Len] && S2[Len]; Len++)  {}
      return Len;
    }

    void TracePC::ClearInlineCounters() {
      IterateCounterRegions([](const Module::Region &R){
        if (R.Enabled)
          memset(R.Start, 0, R.Stop - R.Start);
      });
    }

    ATTRIBUTE_NO_SANITIZE_ALL
    void TracePC::RecordInitialStack() {
      int stack;
      __sancov_lowest_stack = InitialStack = reinterpret_cast(&stack);
    }

    uintptr_t TracePC::GetMaxStackOffset() const {
      return InitialStack - __sancov_lowest_stack;  // Stack grows down
    }

    void WarnAboutDeprecatedInstrumentation(const char *flag) {
      // Use RawPrint because Printf cannot be used on Windows before OutputFile is
      // initialized.
      RawPrint(flag);
      RawPrint(
          " is no longer supported by libFuzzer.\n"
          "Please either migrate to a compiler that supports -fsanitize=fuzzer\n"
          "or use an older version of libFuzzer\n");
      exit(1);
    }

    } // namespace fuzzer