Global Metrics

path: .metrics.loc.blank
old: 3.0
new: 9.0

path: .metrics.loc.ploc
old: 19.0
new: 122.0

path: .metrics.loc.cloc
old: 5.0
new: 39.0

path: .metrics.loc.sloc
old: 27.0
new: 170.0

path: .metrics.loc.lloc
old: 8.0
new: 58.0

path: .metrics.cognitive.average
old: 2.5
new: 14.0

path: .metrics.cognitive.sum
old: 5.0
new: 28.0

path: .metrics.halstead.time
old: 352.94117647058823
new: 8262.957815985288

path: .metrics.halstead.level
old: 0.06296296296296296
new: 0.02791116446578631

path: .metrics.halstead.n2
old: 17.0
new: 93.0

path: .metrics.halstead.volume
old: 400.0
new: 4151.317942364757

path: .metrics.halstead.bugs
old: 0.1143392400450876
new: 0.9357304718722952

path: .metrics.halstead.difficulty
old: 15.882352941176473
new: 35.82795698924731

path: .metrics.halstead.estimated_program_length
old: 128.09022723538357
new: 742.7477072506598

path: .metrics.halstead.purity_ratio
old: 1.6011278404422946
new: 1.2379128454177664

path: .metrics.halstead.effort
old: 6352.941176470588
new: 148733.24068773518

path: .metrics.halstead.length
old: 80.0
new: 600.0

path: .metrics.halstead.vocabulary
old: 32.0
new: 121.0

path: .metrics.halstead.N1
old: 44.0
new: 362.0

path: .metrics.halstead.N2
old: 36.0
new: 238.0

path: .metrics.halstead.n1
old: 15.0
new: 28.0

path: .metrics.cyclomatic.average
old: 2.3333333333333335
new: 3.5714285714285716

path: .metrics.cyclomatic.sum
old: 7.0
new: 25.0

path: .metrics.mi.mi_sei
old: 78.3312604316101
new: 16.506004364252384

path: .metrics.mi.mi_visual_studio
old: 49.61510358232068
new: 22.647908418434056

path: .metrics.mi.mi_original
old: 84.84182712576836
new: 38.72792339552224

path: .metrics.nexits.average
old: 0.0
new: 3.0

path: .metrics.nexits.sum
old: 0.0
new: 6.0

Spaces Data

Minimal test - lines (25, 168)

path: .spaces[0].metrics.cyclomatic.sum
old: 4.0
new: 24.0

path: .spaces[0].metrics.mi.mi_visual_studio
old: 59.92303512740923
new: 24.432888493698933

path: .spaces[0].metrics.mi.mi_sei
old: 72.53706564439678
new: 18.10040971353298

path: .spaces[0].metrics.mi.mi_original
old: 102.4683900678698
new: 41.78023932422518

path: .spaces[0].metrics.halstead.N2
old: 24.0
new: 232.0

path: .spaces[0].metrics.halstead.volume
old: 252.61501174663377
new: 4046.208787125267

path: .spaces[0].metrics.halstead.vocabulary
old: 29.0
new: 116.0

path: .spaces[0].metrics.halstead.difficulty
old: 12.857142857142858
new: 36.90909090909091

path: .spaces[0].metrics.halstead.N1
old: 28.0
new: 358.0

path: .spaces[0].metrics.halstead.length
old: 52.0
new: 590.0

path: .spaces[0].metrics.halstead.level
old: 0.07777777777777778
new: 0.027093596059113306

path: .spaces[0].metrics.halstead.bugs
old: 0.0731051257185307
new: 0.9382815360617724

path: .spaces[0].metrics.halstead.n1
old: 15.0
new: 28.0

path: .spaces[0].metrics.halstead.effort
old: 3247.907293885291
new: 149341.88796116895

path: .spaces[0].metrics.halstead.n2
old: 14.0
new: 88.0

path: .spaces[0].metrics.halstead.purity_ratio
old: 2.152044766210274
new: 1.1915863055215172

path: .spaces[0].metrics.halstead.time
old: 180.4392941047384
new: 8296.771553398276

path: .spaces[0].metrics.halstead.estimated_program_length
old: 111.90632784293425
new: 703.0359202576951

path: .spaces[0].metrics.loc.blank
old: 0.0
new: 8.0

path: .spaces[0].metrics.loc.cloc
old: 0.0
new: 27.0

path: .spaces[0].metrics.loc.lloc
old: 5.0
new: 58.0

path: .spaces[0].metrics.loc.ploc
old: 11.0
new: 109.0

path: .spaces[0].metrics.loc.sloc
old: 11.0
new: 144.0

path: .spaces[0].metrics.nom.functions
old: 1.0
new: 2.0

path: .spaces[0].metrics.nom.total
old: 1.0
new: 2.0

path: .spaces[0].metrics.cognitive.average
old: 4.0
new: 14.0

path: .spaces[0].metrics.cognitive.sum
old: 4.0
new: 28.0

path: .spaces[0].metrics.nargs.average
old: 2.0
new: 1.0

path: .spaces[0].metrics.nexits.average
old: 0.0
new: 3.0

path: .spaces[0].metrics.nexits.sum
old: 0.0
new: 6.0

Code

namespace fuzzer {

static std::mutex SignalMutex;
// Global variables used to keep track of how signal handling should be
// restored. They should **not** be accessed without holding `SignalMutex`.
static int ActiveThreadCount = 0;
static struct sigaction OldSigIntAction;
static struct sigaction OldSigQuitAction;
static sigset_t OldBlockedSignalsSet;

// This is a reimplementation of Libc's `system()`. On Darwin the Libc
// implementation contains a mutex which prevents it from being used
// concurrently. This implementation **can** be used concurrently. It sets the
// signal handlers when the first thread enters and restores them when the last
// thread finishes execution of the function and ensures this is not racey by
// using a mutex.
int ExecuteCommand(const Command &Cmd) {
  std::string CmdLine = Cmd.toString();
  posix_spawnattr_t SpawnAttributes;
  if (posix_spawnattr_init(&SpawnAttributes))
    return -1;
  // Block and ignore signals of the current process when the first thread
  // enters.
  {
    std::lock_guard Lock(SignalMutex);
    if (ActiveThreadCount == 0) {
      static struct sigaction IgnoreSignalAction;
      sigset_t BlockedSignalsSet;
      memset(&IgnoreSignalAction, 0, sizeof(IgnoreSignalAction));
      IgnoreSignalAction.sa_handler = SIG_IGN;

      if (sigaction(SIGINT, &IgnoreSignalAction, &OldSigIntAction) == -1) {
        Printf("Failed to ignore SIGINT\n");
        (void)posix_spawnattr_destroy(&SpawnAttributes);
        return -1;
      }
      if (sigaction(SIGQUIT, &IgnoreSignalAction, &OldSigQuitAction) == -1) {
        Printf("Failed to ignore SIGQUIT\n");
        // Try our best to restore the signal handlers.
        (void)sigaction(SIGINT, &OldSigIntAction, NULL);
        (void)posix_spawnattr_destroy(&SpawnAttributes);
        return -1;
      }

      (void)sigemptyset(&BlockedSignalsSet);
      (void)sigaddset(&BlockedSignalsSet, SIGCHLD);
      if (sigprocmask(SIG_BLOCK, &BlockedSignalsSet, &OldBlockedSignalsSet) ==
          -1) {
        Printf("Failed to block SIGCHLD\n");
        // Try our best to restore the signal handlers.
        (void)sigaction(SIGQUIT, &OldSigQuitAction, NULL);
        (void)sigaction(SIGINT, &OldSigIntAction, NULL);
        (void)posix_spawnattr_destroy(&SpawnAttributes);
        return -1;
      }
    }
    ++ActiveThreadCount;
  }

  // NOTE: Do not introduce any new `return` statements past this
  // point. It is important that `ActiveThreadCount` always be decremented
  // when leaving this function.

  // Make sure the child process uses the default handlers for the
  // following signals rather than inheriting what the parent has.
  sigset_t DefaultSigSet;
  (void)sigemptyset(&DefaultSigSet);
  (void)sigaddset(&DefaultSigSet, SIGQUIT);
  (void)sigaddset(&DefaultSigSet, SIGINT);
  (void)posix_spawnattr_setsigdefault(&SpawnAttributes, &DefaultSigSet);
  // Make sure the child process doesn't block SIGCHLD
  (void)posix_spawnattr_setsigmask(&SpawnAttributes, &OldBlockedSignalsSet);
  short SpawnFlags = POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK;
  (void)posix_spawnattr_setflags(&SpawnAttributes, SpawnFlags);

  pid_t Pid;
  char **Environ = environ; // Read from global
  const char *CommandCStr = CmdLine.c_str();
  char *const Argv[] = {
    strdup("sh"),
    strdup("-c"),
    strdup(CommandCStr),
    NULL
  };
  int ErrorCode = 0, ProcessStatus = 0;
  // FIXME: We probably shouldn't hardcode the shell path.
  ErrorCode = posix_spawn(&Pid, "/bin/sh", NULL, &SpawnAttributes,
                          Argv, Environ);
  (void)posix_spawnattr_destroy(&SpawnAttributes);
  if (!ErrorCode) {
    pid_t SavedPid = Pid;
    do {
      // Repeat until call completes uninterrupted.
      Pid = waitpid(SavedPid, &ProcessStatus, /*options=*/0);
    } while (Pid == -1 && errno == EINTR);
    if (Pid == -1) {
      // Fail for some other reason.
      ProcessStatus = -1;
    }
  } else if (ErrorCode == ENOMEM || ErrorCode == EAGAIN) {
    // Fork failure.
    ProcessStatus = -1;
  } else {
    // Shell execution failure.
    ProcessStatus = W_EXITCODE(127, 0);
  }
  for (unsigned i = 0, n = sizeof(Argv) / sizeof(Argv[0]); i < n; ++i)
    free(Argv[i]);

  // Restore the signal handlers of the current process when the last thread
  // using this function finishes.
  {
    std::lock_guard Lock(SignalMutex);
    --ActiveThreadCount;
    if (ActiveThreadCount == 0) {
      bool FailedRestore = false;
      if (sigaction(SIGINT, &OldSigIntAction, NULL) == -1) {
        Printf("Failed to restore SIGINT handling\n");
        FailedRestore = true;
      }
      if (sigaction(SIGQUIT, &OldSigQuitAction, NULL) == -1) {
        Printf("Failed to restore SIGQUIT handling\n");
        FailedRestore = true;
      }
      if (sigprocmask(SIG_BLOCK, &OldBlockedSignalsSet, NULL) == -1) {
        Printf("Failed to unblock SIGCHLD\n");
        FailedRestore = true;
      }
      if (FailedRestore)
        ProcessStatus = -1;
    }
  }
  return ProcessStatus;
}

void DiscardOutput(int Fd) {
  FILE* Temp = fopen("/dev/null", "w");
  if (!Temp)
    return;
  dup2(fileno(Temp), Fd);
  fclose(Temp);
}

} // namespace fuzzer