Exemple #1
0
int main(int argc, char** argv) {

  s32 opt;
  u8  mem_limit_given = 0, timeout_given = 0, qemu_mode = 0;
  u32 tcnt;
  char** use_argv;

  doc_path = access(DOC_PATH, F_OK) ? "docs" : DOC_PATH;

  while ((opt = getopt(argc,argv,"+o:m:t:A:eqZQ")) > 0)

    switch (opt) {

      case 'o':

        if (out_file) FATAL("Multiple -o options not supported");
        out_file = optarg;
        break;

      case 'm': {

          u8 suffix = 'M';

          if (mem_limit_given) FATAL("Multiple -m options not supported");
          mem_limit_given = 1;

          if (!strcmp(optarg, "none")) {

            mem_limit = 0;
            break;

          }

          if (sscanf(optarg, "%llu%c", &mem_limit, &suffix) < 1 ||
              optarg[0] == '-') FATAL("Bad syntax used for -m");

          switch (suffix) {

            case 'T': mem_limit *= 1024 * 1024; break;
            case 'G': mem_limit *= 1024; break;
            case 'k': mem_limit /= 1024; break;
            case 'M': break;

            default:  FATAL("Unsupported suffix or bad syntax for -m");

          }

          if (mem_limit < 5) FATAL("Dangerously low value of -m");

          if (sizeof(rlim_t) == 4 && mem_limit > 2000)
            FATAL("Value of -m out of range on 32-bit systems");

        }

        break;

      case 't':

        if (timeout_given) FATAL("Multiple -t options not supported");
        timeout_given = 1;

        if (strcmp(optarg, "none")) {
          exec_tmout = atoi(optarg);

          if (exec_tmout < 20 || optarg[0] == '-')
            FATAL("Dangerously low value of -t");

        }

        break;

      case 'e':

        if (edges_only) FATAL("Multiple -e options not supported");
        edges_only = 1;
        break;

      case 'q':

        if (quiet_mode) FATAL("Multiple -q options not supported");
        quiet_mode = 1;
        break;

      case 'Z':

        /* This is an undocumented option to write data in the syntax expected
           by afl-cmin. Nobody else should have any use for this. */

        cmin_mode  = 1;
        quiet_mode = 1;
        break;

      case 'A':

        /* Another afl-cmin specific feature. */
        at_file = optarg;
        break;

      case 'Q':

        if (qemu_mode) FATAL("Multiple -Q options not supported");
        if (!mem_limit_given) mem_limit = MEM_LIMIT_QEMU;

        qemu_mode = 1;
        break;

      default:

        usage(argv[0]);

    }

  if (optind == argc || !out_file) usage(argv[0]);

  setup_shm();
  setup_signal_handlers();

  set_up_environment();

  find_binary(argv[optind]);

  if (!quiet_mode) {
    show_banner();
    ACTF("Executing '%s'...\n", target_path);
  }

  detect_file_args(argv + optind);

  if (qemu_mode)
    use_argv = get_qemu_argv(argv[0], argv + optind, argc - optind);
  else
    use_argv = argv + optind;

  run_target(use_argv);

  tcnt = write_results();

  if (!quiet_mode) {

    if (!tcnt) FATAL("No instrumentation detected" cRST);
    OKF("Captured %u tuples in '%s'." cRST, tcnt, out_file);

  }

  exit(child_crashed * 2 + child_timed_out);

}
int main(int argc, char** argv) {

#ifdef HAVE_AFFINITY

    u32 cpu_cnt = sysconf(_SC_NPROCESSORS_ONLN),
        idle_cpus = 0, maybe_cpus = 0, i;

    SAYF(cCYA "afl-gotcpu " cBRI VERSION cRST " by <*****@*****.**>\n");

    ACTF("Measuring per-core preemption rate (this will take %0.02f sec)...",
         ((double)CTEST_CORE_TRG_MS) * cpu_cnt / 1000);

    for (i = 0; i < cpu_cnt; i++) {

        cpu_set_t c;
        u32 util_perc;

        CPU_ZERO(&c);
        CPU_SET(i, &c);

        if (sched_setaffinity(0, sizeof(c), &c))
            PFATAL("sched_setaffinity failed");

        util_perc = measure_preemption(CTEST_CORE_TRG_MS);

        if (util_perc < 105) {

            SAYF("    Core #%u: " cLGN "AVAILABLE\n" cRST, i);
            maybe_cpus++;
            idle_cpus++;

        } else if (util_perc < 130) {

            SAYF("    Core #%u: " cYEL "CAUTION " cRST "(%u%%)\n", i, util_perc);
            maybe_cpus++;

        } else {

            SAYF("    Core #%u: " cLRD "OVERBOOKED " cRST "(%u%%)\n" cRST, i,
                 util_perc);

        }

    }

    SAYF(cGRA "\n>>> ");

    if (idle_cpus) {

        if (maybe_cpus == idle_cpus) {

            SAYF(cLGN "PASS: "******"You can run more processes on %u core%s.",
                 idle_cpus, idle_cpus > 1 ? "s" : "");

        } else {

            SAYF(cLGN "PASS: "******"You can run more processes on %u to %u core%s.",
                 idle_cpus, maybe_cpus, maybe_cpus > 1 ? "s" : "");

        }

        SAYF(cGRA " <<<" cRST "\n\n");
        return 0;

    }

    if (maybe_cpus) {

        SAYF(cYEL "CAUTION: " cRST "You may still have %u core%s available.",
             maybe_cpus, maybe_cpus > 1 ? "s" : "");
        SAYF(cGRA " <<<" cRST "\n\n");
        return 1;

    }

    SAYF(cLRD "FAIL: " cRST "All cores are overbooked.");
    SAYF(cGRA " <<<" cRST "\n\n");
    return 2;

#else

    u32 util_perc;

    SAYF(cCYA "afl-gotcpu " cBRI VERSION cRST " by <*****@*****.**>\n");

    /* Run a busy loop for CTEST_TARGET_MS. */

    ACTF("Measuring gross preemption rate (this will take %0.02f sec)...",
         ((double)CTEST_TARGET_MS) / 1000);

    util_perc = measure_preemption(CTEST_TARGET_MS);

    /* Deliver the final verdict. */

    SAYF(cGRA "\n>>> ");

    if (util_perc < 105) {

        SAYF(cLGN "PASS: "******"You can probably run additional processes.");

    } else if (util_perc < 130) {

        SAYF(cYEL "CAUTION: " cRST "Your CPU may be somewhat overbooked (%u%%).",
             util_perc);

    } else {

        SAYF(cLRD "FAIL: " cRST "Your CPU is overbooked (%u%%).", util_perc);

    }

    SAYF(cGRA " <<<" cRST "\n\n");

    return (util_perc > 105) + (util_perc > 130);

#endif /* ^HAVE_AFFINITY */

}
int main(int argc, char** argv) {

  static volatile u32 v1, v2;

  s32 loop_repeats = 0, util_perc;
  u64 st_t, en_t, st_c, en_c, real_delta, slice_delta;

  SAYF(cCYA "afl-gotcpu " cBRI VERSION cRST " (" __DATE__ " " __TIME__
       ") by <*****@*****.**>\n");

  /* Run a busy loop for CTEST_TARGET_MS. */

  ACTF("Measuring preemption rate (this will take %0.02f sec)...",
       ((double)CTEST_TARGET_MS) / 1000);

  st_t = get_cur_time_us();
  st_c = get_cpu_usage_us();

repeat_loop:

  v1 = CTEST_BUSY_CYCLES;

  while (v1--) v2++;
  sched_yield();

  en_t = get_cur_time_us();

  if (en_t - st_t < CTEST_TARGET_MS * 1000) {
    loop_repeats++;
    goto repeat_loop;
  }

  /* Let's see what percentage of this time we actually had a chance to
     run, and how much time was spent in the penalty box. */

  en_c = get_cpu_usage_us();

  real_delta  = (en_t - st_t) / 1000;
  slice_delta = (en_c - st_c) / 1000;

  OKF("Busy loop hit %u times, real = %llu ms, slice = %llu ms.",
      loop_repeats, real_delta, slice_delta);

  util_perc = real_delta * 100 / slice_delta;

  /* Deliver the final verdict. */

  SAYF(cGRA "\n>>> ");

  if (util_perc < 105) {

    SAYF(cLGN "PASS: "******"You can probably run additional processes.");

  } else if (util_perc < 130) {

    SAYF(cYEL "CAUTION: " cRST "Your CPU may be somewhat overbooked (%u%%).",
         util_perc);

  } else {

    SAYF(cLRD "FAIL: " cRST "Your CPU is overbooked (%u%%).", util_perc);

  }

  SAYF(cGRA " <<<" cRST "\n\n");

  return (util_perc > 105) + (util_perc > 130);

}