# Global Metrics

path: .metrics.cognitive.sum
old: 22.0
new: 39.0
path: .metrics.cognitive.average
old: 5.5
new: 2.0526315789473686
path: .metrics.loc.ploc
old: 90.0
new: 176.0
path: .metrics.loc.blank
old: 39.0
new: 31.0
path: .metrics.loc.sloc
old: 152.0
new: 221.0
path: .metrics.loc.cloc
old: 23.0
new: 14.0
path: .metrics.loc.lloc
old: 43.0
new: 97.0
path: .metrics.mi.mi_sei
old: 20.840429171043553
new: -18.51255262664836
path: .metrics.mi.mi_visual_studio
old: 26.86100084569862
new: 12.381867375868552
path: .metrics.mi.mi_original
old: 45.93231144614464
new: 21.17299321273522
path: .metrics.nom.functions
old: 4.0
new: 19.0
path: .metrics.nom.total
old: 4.0
new: 19.0
path: .metrics.cyclomatic.average
old: 3.8
new: 3.409090909090909
path: .metrics.cyclomatic.sum
old: 19.0
new: 75.0
path: .metrics.nexits.sum
old: 9.0
new: 19.0
path: .metrics.nexits.average
old: 2.25
new: 1.0
path: .metrics.halstead.level
old: 0.04623655913978495
new: 0.02930622009569378
path: .metrics.halstead.length
old: 332.0
new: 784.0
path: .metrics.halstead.estimated_program_length
old: 276.3489344608441
new: 1224.8168406297746
path: .metrics.halstead.vocabulary
old: 55.0
new: 180.0
path: .metrics.halstead.bugs
old: 0.39963831968888464
new: 1.1415871836762497
path: .metrics.halstead.difficulty
old: 21.627906976744185
new: 34.12244897959184
path: .metrics.halstead.purity_ratio
old: 0.8323763086170002
new: 1.5622663783543045
path: .metrics.halstead.N2
old: 155.0
new: 304.0
path: .metrics.halstead.time
old: 2306.2695415347207
new: 11134.558557389526
path: .metrics.halstead.n1
old: 12.0
new: 33.0
path: .metrics.halstead.volume
old: 1919.4114248901872
new: 5873.612827522465
path: .metrics.halstead.effort
old: 41512.851747624976
new: 200422.05403301143
path: .metrics.halstead.N1
old: 177.0
new: 480.0
path: .metrics.halstead.n2
old: 43.0
new: 147.0
path: .metrics.nargs.sum
old: 13.0
new: 15.0
path: .metrics.nargs.average
old: 3.25
new: 0.7894736842105263

# Spaces Data

## Minimal test - lines (29, 219)

path: .spaces[0].metrics.mi.mi_original
old: 107.86968486097982
new: 23.827954787582684
path: .spaces[0].metrics.mi.mi_sei
old: 80.1258471379871
new: -25.89650525556116
path: .spaces[0].metrics.mi.mi_visual_studio
old: 63.08168705320457
new: 13.934476483966485
path: .spaces[0].metrics.nexits.sum
old: 1.0
new: 19.0
path: .spaces[0].metrics.nargs.sum
old: 1.0
new: 15.0
path: .spaces[0].metrics.nargs.average
old: 1.0
new: 0.7894736842105263
path: .spaces[0].metrics.halstead.vocabulary
old: 15.0
new: 175.0
path: .spaces[0].metrics.halstead.difficulty
old: 5.6875
new: 34.74295774647887
path: .spaces[0].metrics.halstead.N2
old: 13.0
new: 299.0
path: .spaces[0].metrics.halstead.time
old: 30.861722586838127
new: 11203.626160311078
path: .spaces[0].metrics.halstead.N1
old: 12.0
new: 480.0
path: .spaces[0].metrics.halstead.length
old: 25.0
new: 779.0
path: .spaces[0].metrics.halstead.volume
old: 97.67226489021296
new: 5804.493456117384
path: .spaces[0].metrics.halstead.bugs
old: 0.02252546979282472
new: 1.1463031556986043
path: .spaces[0].metrics.halstead.estimated_program_length
old: 43.65148445440323
new: 1181.7290969084938
path: .spaces[0].metrics.halstead.effort
old: 555.5110065630863
new: 201665.2708855994
path: .spaces[0].metrics.halstead.n1
old: 7.0
new: 33.0
path: .spaces[0].metrics.halstead.level
old: 0.17582417582417584
new: 0.028782811391507044
path: .spaces[0].metrics.halstead.purity_ratio
old: 1.746059378176129
new: 1.5169821526424825
path: .spaces[0].metrics.halstead.n2
old: 8.0
new: 142.0
path: .spaces[0].metrics.cognitive.average
old: 1.0
new: 2.0526315789473686
path: .spaces[0].metrics.cognitive.sum
old: 1.0
new: 39.0
path: .spaces[0].metrics.nom.total
old: 1.0
new: 19.0
path: .spaces[0].metrics.nom.functions
old: 1.0
new: 19.0
path: .spaces[0].metrics.cyclomatic.average
old: 2.0
new: 3.5238095238095237
path: .spaces[0].metrics.cyclomatic.sum
old: 2.0
new: 74.0
path: .spaces[0].metrics.loc.lloc
old: 3.0
new: 97.0
path: .spaces[0].metrics.loc.ploc
old: 8.0
new: 159.0
path: .spaces[0].metrics.loc.sloc
old: 11.0
new: 191.0
path: .spaces[0].metrics.loc.cloc
old: 0.0
new: 2.0
path: .spaces[0].metrics.loc.blank
old: 3.0
new: 30.0

### Code

    namespace fuzzer {

    static const FuzzingOptions* HandlerOpt = nullptr;

    static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo) {
      switch (ExceptionInfo->ExceptionRecord->ExceptionCode) {
        case EXCEPTION_ACCESS_VIOLATION:
        case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
        case EXCEPTION_STACK_OVERFLOW:
          if (HandlerOpt->HandleSegv)
            Fuzzer::StaticCrashSignalCallback();
          break;
        case EXCEPTION_DATATYPE_MISALIGNMENT:
        case EXCEPTION_IN_PAGE_ERROR:
          if (HandlerOpt->HandleBus)
            Fuzzer::StaticCrashSignalCallback();
          break;
        case EXCEPTION_ILLEGAL_INSTRUCTION:
        case EXCEPTION_PRIV_INSTRUCTION:
          if (HandlerOpt->HandleIll)
            Fuzzer::StaticCrashSignalCallback();
          break;
        case EXCEPTION_FLT_DENORMAL_OPERAND:
        case EXCEPTION_FLT_DIVIDE_BY_ZERO:
        case EXCEPTION_FLT_INEXACT_RESULT:
        case EXCEPTION_FLT_INVALID_OPERATION:
        case EXCEPTION_FLT_OVERFLOW:
        case EXCEPTION_FLT_STACK_CHECK:
        case EXCEPTION_FLT_UNDERFLOW:
        case EXCEPTION_INT_DIVIDE_BY_ZERO:
        case EXCEPTION_INT_OVERFLOW:
          if (HandlerOpt->HandleFpe)
            Fuzzer::StaticCrashSignalCallback();
          break;
        // TODO: handle (Options.HandleXfsz)
      }
      return EXCEPTION_CONTINUE_SEARCH;
    }

    BOOL WINAPI CtrlHandler(DWORD dwCtrlType) {
      switch (dwCtrlType) {
        case CTRL_C_EVENT:
          if (HandlerOpt->HandleInt)
            Fuzzer::StaticInterruptCallback();
          return TRUE;
        case CTRL_BREAK_EVENT:
          if (HandlerOpt->HandleTerm)
            Fuzzer::StaticInterruptCallback();
          return TRUE;
      }
      return FALSE;
    }

    void CALLBACK AlarmHandler(PVOID, BOOLEAN) {
      Fuzzer::StaticAlarmCallback();
    }

    class TimerQ {
      HANDLE TimerQueue;
     public:
      TimerQ() : TimerQueue(NULL) {}
      ~TimerQ() {
        if (TimerQueue)
          DeleteTimerQueueEx(TimerQueue, NULL);
      }
      void SetTimer(int Seconds) {
        if (!TimerQueue) {
          TimerQueue = CreateTimerQueue();
          if (!TimerQueue) {
            Printf("libFuzzer: CreateTimerQueue failed.\n");
            exit(1);
          }
        }
        HANDLE Timer;
        if (!CreateTimerQueueTimer(&Timer, TimerQueue, AlarmHandler, NULL,
            Seconds*1000, Seconds*1000, 0)) {
          Printf("libFuzzer: CreateTimerQueueTimer failed.\n");
          exit(1);
        }
      }
    };

    static TimerQ Timer;

    static void CrashHandler(int) { Fuzzer::StaticCrashSignalCallback(); }

    void SetSignalHandler(const FuzzingOptions& Options) {
      HandlerOpt = &Options;

      if (Options.UnitTimeoutSec > 0)
        Timer.SetTimer(Options.UnitTimeoutSec / 2 + 1);

      if (Options.HandleInt || Options.HandleTerm)
        if (!SetConsoleCtrlHandler(CtrlHandler, TRUE)) {
          DWORD LastError = GetLastError();
          Printf("libFuzzer: SetConsoleCtrlHandler failed (Error code: %lu).\n",
            LastError);
          exit(1);
        }

      if (Options.HandleSegv || Options.HandleBus || Options.HandleIll ||
          Options.HandleFpe)
        SetUnhandledExceptionFilter(ExceptionHandler);

      if (Options.HandleAbrt)
        if (SIG_ERR == signal(SIGABRT, CrashHandler)) {
          Printf("libFuzzer: signal failed with %d\n", errno);
          exit(1);
        }
    }

    void SleepSeconds(int Seconds) { Sleep(Seconds * 1000); }

    unsigned long GetPid() { return GetCurrentProcessId(); }

    size_t GetPeakRSSMb() {
      PROCESS_MEMORY_COUNTERS info;
      if (!GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info)))
        return 0;
      return info.PeakWorkingSetSize >> 20;
    }

    FILE *OpenProcessPipe(const char *Command, const char *Mode) {
      return _popen(Command, Mode);
    }

    int CloseProcessPipe(FILE *F) {
      return _pclose(F);
    }

    int ExecuteCommand(const Command &Cmd) {
      std::string CmdLine = Cmd.toString();
      return system(CmdLine.c_str());
    }

    bool ExecuteCommand(const Command &Cmd, std::string *CmdOutput) {
      FILE *Pipe = _popen(Cmd.toString().c_str(), "r");
      if (!Pipe)
        return false;

      if (CmdOutput) {
        char TmpBuffer[128];
        while (fgets(TmpBuffer, sizeof(TmpBuffer), Pipe))
          CmdOutput->append(TmpBuffer);
      }
      return _pclose(Pipe) == 0;
    }

    const void *SearchMemory(const void *Data, size_t DataLen, const void *Patt,
                             size_t PattLen) {
      // TODO: make this implementation more efficient.
      const char *Cdata = (const char *)Data;
      const char *Cpatt = (const char *)Patt;

      if (!Data || !Patt || DataLen == 0 || PattLen == 0 || DataLen < PattLen)
        return NULL;

      if (PattLen == 1)
        return memchr(Data, *Cpatt, DataLen);

      const char *End = Cdata + DataLen - PattLen + 1;

      for (const char *It = Cdata; It < End; ++It)
        if (It[0] == Cpatt[0] && memcmp(It, Cpatt, PattLen) == 0)
          return It;

      return NULL;
    }

    std::string DisassembleCmd(const std::string &FileName) {
      Vector command_vector;
      command_vector.push_back("dumpbin /summary > nul");
      if (ExecuteCommand(Command(command_vector)) == 0)
        return "dumpbin /disasm " + FileName;
      Printf("libFuzzer: couldn't find tool to disassemble (dumpbin)\n");
      exit(1);
    }

    std::string SearchRegexCmd(const std::string &Regex) {
      return "findstr /r \"" + Regex + "\"";
    }

    void DiscardOutput(int Fd) {
      FILE* Temp = fopen("nul", "w");
      if (!Temp)
        return;
      _dup2(_fileno(Temp), Fd);
      fclose(Temp);
    }

    } // namespace fuzzer