# Global Metrics

path: .metrics.cognitive.sum
old: 14.0
new: 28.0
path: .metrics.cognitive.average
old: 0.7
new: 9.333333333333334
path: .metrics.halstead.n1
old: 21.0
new: 28.0
path: .metrics.halstead.N2
old: 513.0
new: 160.0
path: .metrics.halstead.level
old: 0.02172096908939014
new: 0.029464285714285717
path: .metrics.halstead.vocabulary
old: 138.0
new: 94.0
path: .metrics.halstead.estimated_program_length
old: 896.0713380696122
new: 533.5359496952708
path: .metrics.halstead.difficulty
old: 46.03846153846154
new: 33.93939393939394
path: .metrics.halstead.purity_ratio
old: 0.7600265802117152
new: 1.2465793217179224
path: .metrics.halstead.effort
old: 385846.0596325435
new: 95212.35490727855
path: .metrics.halstead.length
old: 1179.0
new: 428.0
path: .metrics.halstead.n2
old: 117.0
new: 66.0
path: .metrics.halstead.N1
old: 666.0
new: 268.0
path: .metrics.halstead.volume
old: 8380.950334541461
new: 2805.364028518029
path: .metrics.halstead.bugs
old: 1.766667385522274
new: 0.6950365067360735
path: .metrics.halstead.time
old: 21435.892201807972
new: 5289.575272626586
path: .metrics.nom.functions
old: 18.0
new: 3.0
path: .metrics.nom.total
old: 20.0
new: 3.0
path: .metrics.nom.closures
old: 2.0
new: 0.0
path: .metrics.loc.sloc
old: 20.0
new: 114.0
path: .metrics.loc.blank
old: 0.0
new: 17.0
path: .metrics.loc.lloc
old: 95.0
new: 33.0
path: .metrics.loc.cloc
old: 20.0
new: 11.0
path: .metrics.loc.ploc
old: 245.0
new: 86.0
path: .metrics.nargs.average
old: 1.55
new: 3.0
path: .metrics.nargs.sum
old: 31.0
new: 9.0
path: .metrics.cyclomatic.sum
old: 32.0
new: 18.0
path: .metrics.cyclomatic.average
old: 1.6842105263157894
new: 2.5714285714285716
path: .metrics.nexits.average
old: 1.3
new: 0.0
path: .metrics.nexits.sum
old: 26.0
new: 0.0
path: .metrics.mi.mi_original
old: 68.13381088938675
new: 48.84928447854384
path: .metrics.mi.mi_visual_studio
old: 39.84433385344254
new: 28.566833028388206
path: .metrics.mi.mi_sei
old: 75.8420278220549
new: 19.74986244732724

# Spaces Data

## Minimal test - lines (16, 112)

path: .spaces[0].metrics.nargs.average
old: 0.0
new: 3.0
path: .spaces[0].metrics.nargs.sum
old: 0.0
new: 9.0
path: .spaces[0].metrics.nom.total
old: 1.0
new: 3.0
path: .spaces[0].metrics.nom.functions
old: 1.0
new: 3.0
path: .spaces[0].metrics.loc.ploc
old: 21.0
new: 79.0
path: .spaces[0].metrics.loc.cloc
old: 0.0
new: 5.0
path: .spaces[0].metrics.loc.sloc
old: 24.0
new: 97.0
path: .spaces[0].metrics.loc.blank
old: 3.0
new: 13.0
path: .spaces[0].metrics.loc.lloc
old: 11.0
new: 33.0
path: .spaces[0].metrics.cognitive.average
old: 3.0
new: 9.333333333333334
path: .spaces[0].metrics.cognitive.sum
old: 3.0
new: 28.0
path: .spaces[0].metrics.cyclomatic.average
old: 4.0
new: 2.8333333333333335
path: .spaces[0].metrics.cyclomatic.sum
old: 4.0
new: 17.0
path: .spaces[0].metrics.halstead.N1
old: 50.0
new: 268.0
path: .spaces[0].metrics.halstead.time
old: 200.2755121844115
new: 5365.964935303296
path: .spaces[0].metrics.halstead.volume
old: 352.93307042287904
new: 2732.7595078678196
path: .spaces[0].metrics.halstead.estimated_program_length
old: 91.35671671381672
new: 496.380915408949
path: .spaces[0].metrics.halstead.level
old: 0.0979020979020979
new: 0.02829313543599258
path: .spaces[0].metrics.halstead.N2
old: 26.0
new: 154.0
path: .spaces[0].metrics.halstead.purity_ratio
old: 1.2020620620239042
new: 1.176258093386135
path: .spaces[0].metrics.halstead.effort
old: 3604.959219319407
new: 96587.36883545932
path: .spaces[0].metrics.halstead.vocabulary
old: 25.0
new: 89.0
path: .spaces[0].metrics.halstead.length
old: 76.0
new: 422.0
path: .spaces[0].metrics.halstead.bugs
old: 0.07836924257330379
new: 0.7017121057695229
path: .spaces[0].metrics.halstead.n1
old: 11.0
new: 28.0
path: .spaces[0].metrics.halstead.difficulty
old: 10.214285714285714
new: 35.34426229508197
path: .spaces[0].metrics.halstead.n2
old: 14.0
new: 61.0
path: .spaces[0].metrics.mi.mi_original
old: 88.09088007723702
new: 51.831732778972295
path: .spaces[0].metrics.mi.mi_sei
old: 51.79470328057933
new: 18.0334128701176
path: .spaces[0].metrics.mi.mi_visual_studio
old: 51.51513454809182
new: 30.310954841504262
path: .spaces[0].metrics.nexits.average
old: 3.0
new: 0.0
path: .spaces[0].metrics.nexits.sum
old: 3.0
new: 0.0

### Code

    namespace mozilla {
    namespace ipc {

    class ProtocolFuzzerHelper {
     public:
      static mozilla::dom::ContentParent* CreateContentParent(
          const nsACString& aRemoteType);

      static void CompositorBridgeParentSetup();

      static void AddShmemToProtocol(IToplevelProtocol* aProtocol,
                                     Shmem::SharedMemory* aSegment, int32_t aId) {
        MOZ_ASSERT(!aProtocol->mShmemMap.Contains(aId),
                   "Don't insert with an existing ID");
        aProtocol->mShmemMap.InsertOrUpdate(aId, aSegment);
      }

      static void RemoveShmemFromProtocol(IToplevelProtocol* aProtocol,
                                          int32_t aId) {
        aProtocol->mShmemMap.Remove(aId);
      }
    };

    template
    void FuzzProtocol(T* aProtocol, const uint8_t* aData, size_t aSize,
                      const nsTArray& aIgnoredMessageTypes) {
      while (true) {
        uint32_t msg_size =
            IPC::Message::MessageSize(reinterpret_cast(aData),
                                      reinterpret_cast(aData) + aSize);
        if (msg_size == 0 || msg_size > aSize) {
          break;
        }
        IPC::Message m(reinterpret_cast(aData), msg_size);
        aSize -= msg_size;
        aData += msg_size;

        // We ignore certain message types
        if (aIgnoredMessageTypes.Contains(m.name())) {
          continue;
        }

        uint8_t num_shmems = 0;
        if (aSize) {
          num_shmems = *aData;
          aData++;
          aSize--;

          for (uint32_t i = 0; i < num_shmems; i++) {
            if (aSize < sizeof(uint16_t)) {
              break;
            }
            size_t shmem_size = *reinterpret_cast(aData);
            aData += sizeof(uint16_t);
            aSize -= sizeof(uint16_t);

            if (shmem_size > aSize) {
              break;
            }
            RefPtr segment(
                Shmem::Alloc(Shmem::PrivateIPDLCaller(), shmem_size,
                             SharedMemory::TYPE_BASIC, false));
            if (!segment) {
              break;
            }

            Shmem shmem(Shmem::PrivateIPDLCaller(), segment.get(), i + 1);
            memcpy(shmem.get(), aData, shmem_size);
            ProtocolFuzzerHelper::AddShmemToProtocol(
                aProtocol, segment.forget().take(), i + 1);

            aData += shmem_size;
            aSize -= shmem_size;
          }
        }
        // TODO: attach |m.header().num_fds| file descriptors to |m|. MVP can be
        // empty files, next implementation maybe read a length header from |data|
        // and then that many bytes.

        if (m.is_sync()) {
          UniquePtr reply;
          aProtocol->OnMessageReceived(m, *getter_Transfers(reply));
        } else {
          aProtocol->OnMessageReceived(m);
        }
        for (uint32_t i = 0; i < num_shmems; i++) {
          Shmem::SharedMemory* segment = aProtocol->LookupSharedMemory(i + 1);
          Shmem::Dealloc(Shmem::PrivateIPDLCaller(), segment);
          ProtocolFuzzerHelper::RemoveShmemFromProtocol(aProtocol, i + 1);
        }
      }
    }

    nsTArray LoadIPCMessageBlacklist(const char* aPath);

    }  // namespace ipc
    }  // namespace mozilla