# Global Metrics

path: .metrics.halstead.time
old: 28582.799138609385
new: 2120.9593555499846
path: .metrics.halstead.length
old: 1219.0
new: 318.0
path: .metrics.halstead.estimated_program_length
old: 1160.256826310223
new: 325.06567602884894
path: .metrics.halstead.N1
old: 702.0
new: 200.0
path: .metrics.halstead.bugs
old: 2.1402454721165713
new: 0.37793343325152173
path: .metrics.halstead.level
old: 0.01759530791788856
new: 0.049788135593220345
path: .metrics.halstead.n1
old: 31.0
new: 16.0
path: .metrics.halstead.n2
old: 141.0
new: 47.0
path: .metrics.halstead.purity_ratio
old: 0.9518103579247112
new: 1.022219107008959
path: .metrics.halstead.N2
old: 517.0
new: 118.0
path: .metrics.halstead.volume
old: 9052.616735981856
new: 1900.7750156729735
path: .metrics.halstead.vocabulary
old: 172.0
new: 63.0
path: .metrics.halstead.effort
old: 514490.3844949689
new: 38177.26839989972
path: .metrics.halstead.difficulty
old: 56.833333333333336
new: 20.08510638297872
path: .metrics.nom.total
old: 4.0
new: 5.0
path: .metrics.nom.functions
old: 4.0
new: 5.0
path: .metrics.nexits.sum
old: 6.0
new: 13.0
path: .metrics.nexits.average
old: 1.5
new: 2.6
path: .metrics.cyclomatic.sum
old: 72.0
new: 21.0
path: .metrics.cyclomatic.average
old: 14.4
new: 2.625
path: .metrics.cognitive.average
old: 39.25
new: 3.0
path: .metrics.cognitive.sum
old: 157.0
new: 15.0
path: .metrics.mi.mi_original
old: 12.778449015902694
new: 49.08477075347464
path: .metrics.mi.mi_visual_studio
old: 7.472777202282278
new: 28.70454430027757
path: .metrics.mi.mi_sei
old: -31.509030194441845
new: 23.09624114720862
path: .metrics.nargs.sum
old: 8.0
new: 0.0
path: .metrics.nargs.average
old: 2.0
new: 0.0
path: .metrics.loc.cloc
old: 20.0
new: 15.0
path: .metrics.loc.sloc
old: 337.0
new: 122.0
path: .metrics.loc.ploc
old: 251.0
new: 87.0
path: .metrics.loc.lloc
old: 136.0
new: 37.0
path: .metrics.loc.blank
old: 66.0
new: 20.0

# Spaces Data

## Minimal test - lines (20, 122)

path: .spaces[0].metrics.cyclomatic.average
old: 22.0
new: 2.857142857142857
path: .spaces[0].metrics.cyclomatic.sum
old: 22.0
new: 20.0
path: .spaces[0].metrics.halstead.N2
old: 170.0
new: 107.0
path: .spaces[0].metrics.halstead.bugs
old: 0.5538393369293081
new: 0.3813918466300638
path: .spaces[0].metrics.halstead.estimated_program_length
old: 519.5956831277142
new: 270.13068653562766
path: .spaces[0].metrics.halstead.effort
old: 67726.33587295479
new: 38702.497282231234
path: .spaces[0].metrics.halstead.length
old: 384.0
new: 305.0
path: .spaces[0].metrics.halstead.N1
old: 214.0
new: 198.0
path: .spaces[0].metrics.halstead.time
old: 3762.5742151641552
new: 2150.138737901735
path: .spaces[0].metrics.halstead.purity_ratio
old: 1.3531137581450892
new: 0.8856743820840252
path: .spaces[0].metrics.halstead.vocabulary
old: 91.0
new: 55.0
path: .spaces[0].metrics.halstead.n2
old: 69.0
new: 39.0
path: .spaces[0].metrics.halstead.level
old: 0.03689839572192513
new: 0.0455607476635514
path: .spaces[0].metrics.halstead.difficulty
old: 27.10144927536232
new: 21.94871794871795
path: .spaces[0].metrics.halstead.volume
old: 2498.9931418362994
new: 1763.3147126250212
path: .spaces[0].metrics.halstead.n1
old: 22.0
new: 16.0
path: .spaces[0].metrics.cognitive.sum
old: 35.0
new: 15.0
path: .spaces[0].metrics.cognitive.average
old: 35.0
new: 3.0
path: .spaces[0].metrics.nargs.average
old: 1.0
new: 0.0
path: .spaces[0].metrics.nargs.sum
old: 1.0
new: 0.0
path: .spaces[0].metrics.nexits.average
old: 3.0
new: 2.6
path: .spaces[0].metrics.nexits.sum
old: 3.0
new: 13.0
path: .spaces[0].metrics.nom.total
old: 1.0
new: 5.0
path: .spaces[0].metrics.nom.functions
old: 1.0
new: 5.0
path: .spaces[0].metrics.mi.mi_visual_studio
old: 29.15958933205539
new: 30.671138523123645
path: .spaces[0].metrics.mi.mi_original
old: 49.86289775781472
new: 52.447646874541434
path: .spaces[0].metrics.mi.mi_sei
old: 9.08532483708878
new: 24.106555185043916
path: .spaces[0].metrics.loc.ploc
old: 80.0
new: 78.0
path: .spaces[0].metrics.loc.cloc
old: 2.0
new: 9.0
path: .spaces[0].metrics.loc.lloc
old: 49.0
new: 37.0
path: .spaces[0].metrics.loc.sloc
old: 105.0
new: 103.0
path: .spaces[0].metrics.loc.blank
old: 23.0
new: 16.0

### Code

    namespace mozilla {
    namespace ipc {

    using namespace fuzzing;

    /* static */
    bool SharedMemoryFuzzer::IsLoggingEnabled() {
      static bool sInitialized = false;
      static bool sIsLoggingEnabled = false;

      if (!sInitialized) {
        sIsLoggingEnabled = !!PR_GetEnv("SHMEM_FUZZER_ENABLE_LOGGING");
        sInitialized = true;
      }
      return sIsLoggingEnabled;
    }

    /* static */
    bool SharedMemoryFuzzer::IsEnabled() {
      static bool sInitialized = false;
      static bool sIsFuzzerEnabled = false;

      if (!sInitialized) {
        sIsFuzzerEnabled = !!PR_GetEnv("SHMEM_FUZZER_ENABLE");
      }
      return sIsFuzzerEnabled;
    }

    /* static */
    uint64_t SharedMemoryFuzzer::MutationProbability() {
      static uint64_t sPropValue = SHMEM_FUZZER_DEFAULT_MUTATION_PROBABILITY;
      static bool sInitialized = false;

      if (sInitialized) {
        return sPropValue;
      }
      sInitialized = true;

      const char* probability = PR_GetEnv("SHMEM_FUZZER_MUTATION_PROBABILITY");
      if (probability) {
        long n = std::strtol(probability, nullptr, 10);
        if (n != 0) {
          sPropValue = n;
          return sPropValue;
        }
      }
      return sPropValue;
    }

    /* static */
    uint64_t SharedMemoryFuzzer::MutationFactor() {
      static uint64_t sPropValue = SHMEM_FUZZER_DEFAULT_MUTATION_FACTOR;
      static bool sInitialized = false;

      if (sInitialized) {
        return sPropValue;
      }
      sInitialized = true;

      const char* factor = PR_GetEnv("SHMEM_FUZZER_MUTATION_FACTOR");
      if (factor) {
        long n = strtol(factor, nullptr, 10);
        if (n != 0) {
          sPropValue = n;
          return sPropValue;
        }
      }
      return sPropValue;
    }

    /* static */
    void* SharedMemoryFuzzer::MutateSharedMemory(void* aMemory, size_t aSize) {
      if (!IsEnabled()) {
        return aMemory;
      }

      if (aSize == 0) {
        /* Shmem opened from foreign handle. */
        SHMEM_FUZZER_LOG("shmem is of size 0.");
        return aMemory;
      }

      if (!aMemory) {
        /* Memory space is not mapped. */
        SHMEM_FUZZER_LOG("shmem memory space is not mapped.");
        return aMemory;
      }

      // The likelihood when a value gets fuzzed of this object.
      if (!FuzzingTraits::Sometimes(MutationProbability())) {
        return aMemory;
      }

      const size_t max = FuzzingTraits::Frequency(aSize, MutationFactor());
      SHMEM_FUZZER_LOG("shmem of size: %zu / mutations: %zu", aSize, max);
      for (size_t i = 0; i < max; i++) {
        FuzzingMutate::ChangeBit((uint8_t*)aMemory, aSize);
      }
      return aMemory;
    }

    }  // namespace ipc
    }  // namespace mozilla