Exemplo n.º 1
0
 def describe(self, name=None):
     if name is None:  # describe all options
         names = sorted(dir(self))
     else:
         opt = self._get_option(name)
         if isinstance(opt, Config):
             return opt.describe()
         names = [name]
     extras = ""
     for name in names:
         opt = self._get_option(name)
         if isinstance(opt, Config):
             extras += "%s [...]\n\n" % core.apply_color("bold", opt.name)
             continue
         value = opt.get()
         comment = ("(default)" if value == opt.default else
                    "(default: %r)" % opt.default)
         print("%s = %s %s" %
               (core.apply_color("bold", opt.name),
                core.apply_color("bright_green", repr(value)),
                core.apply_color("bright_black", comment)))
         for line in opt.doc.strip().split("\n"):
             print("    " + line)
         print()
     if extras:
         print(extras, end="")
Exemplo n.º 2
0
def start_random_attack(n_attacks=None,
                        maxfail=None,
                        skip_successful_seeds=False,
                        save_logs_to_file=False):
    if n_attacks is None:
        n_attacks = 10**10
    if maxfail is None:
        maxfail = n_attacks
    if save_logs_to_file:
        log_dir = os.path.join(os.path.dirname(__file__), "logs")
    else:
        log_dir = None

    n_tests = 0
    n_errors = 0
    try:
        for i in range(n_attacks):
            seed = random.getrandbits(63)
            is_success = try_seed(seed, skip_successful_seeds, log_dir)
            n_tests += 1
            n_errors += not is_success
            if n_errors >= maxfail:
                raise KeyboardInterrupt
            if skip_successful_seeds:
                print(core.apply_color("bold", " Seeds tried: %d" % (i + 1)),
                      end="\r")
    except KeyboardInterrupt:
        errmsg = "errors: %d" % n_errors
        if n_errors:
            errmsg = core.apply_color("bright_red", errmsg)
        print("\r" + core.apply_color("bright_cyan", "DONE.") +
              " Seeds tested: %d, %s" % (n_tests, errmsg))
Exemplo n.º 3
0
 def attack(self, frame=None, rounds=None):
     t0 = time.time()
     if rounds is None:
         rounds = int(random.expovariate(0.05) + 2)
     assert isinstance(rounds, int)
     if frame is None:
         frame = MetaFrame.random()
     print("# Launching an attack for %d rounds" % rounds)
     for i in range(rounds):
         action = random.choices(population=ATTACK_METHODS,
                                 cum_weights=ATTACK_WEIGHTS, k=1)[0]
         if action:
             action(frame)
         else:
             # Non-standard actions
             fork_and_run(frame, rounds - i)
             break
         if self._exhaustive_checks:
             frame.check()
         if time.time() - t0 > 60:
             print(">>> Stopped early, taking too long <<<")
             break
     print("\nAttack ended, checking the outcome... ", end='')
     frame.check()
     print(core.apply_color("bright_green", "PASSED"))
     t1 = time.time()
     print("Time taken = %.3fs" % (t1 - t0))
Exemplo n.º 4
0
def try_seed(seed, skip_successful_seeds, log_dir):
    utf8_env = os.environ
    utf8_env['PYTHONIOENCODING'] = 'utf-8'
    script = os.path.join(os.path.dirname(__file__), "attacker.py")
    proc = subprocess.Popen(
        [sys.executable, script, str(seed)],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        env=utf8_env)
    try:
        out, err = proc.communicate(timeout=100)
    except subprocess.TimeoutExpired:
        proc.kill()
        out, err = proc.communicate()
    rc = proc.returncode
    if rc == 0 and skip_successful_seeds:
        return True

    if rc == 0:
        status = core.apply_color("bright_green", "OK")
    elif rc > 0:
        status = core.apply_color("yellow", "FAIL")
    elif rc == -9:
        status = core.apply_color("cyan", "HANG")
    else:
        status = core.apply_color("bright_red", "ABORT")
    if rc != 0:
        status += " (%d)" % rc
    print("%-19d: %s" % (seed, status))

    if log_dir is None:
        write_to_screen(out, err)
    else:
        os.makedirs(log_dir, exist_ok=True)
        log_file = os.path.join(log_dir, "%d.txt" % seed)
        write_to_file(log_file, out, err)
    return rc == 0
Exemplo n.º 5
0
def start_random_attack(n_attacks=None, maxfail=None):
    if n_attacks is None:
        n_attacks = 10**10
    if maxfail is None:
        maxfail = n_attacks
    n_tests = 0
    n_errors = 0
    try:
        for i in range(n_attacks):
            seed = random.getrandbits(63)
            ret = try_seed(seed)
            n_tests += 1
            n_errors += not ret
            if n_errors >= maxfail:
                raise KeyboardInterrupt
            if skip_successful_seeds:
                print(core.apply_color("bold", " Seeds tried: %d" % (i + 1)),
                      end="\r")
    except KeyboardInterrupt:
        errmsg = "errors: %d" % n_errors
        if n_errors:
            errmsg = core.apply_color("bright_red", errmsg)
        print("\r" + core.apply_color("bright_cyan", "DONE.") +
              " Seeds tested: %d, %s" % (n_tests, errmsg))
Exemplo n.º 6
0
    def attack(self, frame=None, rounds=None):
        t0 = time.time()
        if rounds is None:
            rounds = int(random.expovariate(0.05) + 2)
        assert isinstance(rounds, int)
        if self._allow_forks:
            fork_probability = 1 / MethodsLibrary.n_methods()
        else:
            fork_probability = 0

        print("# Launching an attack for %d rounds" % rounds)
        context = EvaluationContext()
        if frame is not None:
            context.frame = frame

        for i in range(rounds):
            if random.random() < fork_probability:
                fork_and_run(context.frame, rounds - i)
                break

            action = MethodsLibrary.random_action(context)
            if action.skipped:
                print(f"# SKIPPED: {action.__class__.__name__}")
            elif action.raises:
                print(f"# RAISES: ", end="")
                action.log_to_console()
                msg = re.escape(action.error_message)
                with pytest.raises(action.raises, match=msg):
                    action.apply_to_dtframe()
            else:
                action.log_to_console()
                action.apply_to_dtframe()
                action.apply_to_pyframe()

            if self._exhaustive_checks:
                context.check()
            if time.time() - t0 > 60:
                print(">>> Stopped early, taking too long <<<")
                break
        print("\nAttack ended, checking the outcome... ", end='')
        context.check_all()
        print(core.apply_color("bright_green", "PASSED"))
        t1 = time.time()
        print("# Time taken = %.3fs" % (t1 - t0))
Exemplo n.º 7
0
 def attack(self, frame=None, rounds=None):
     t0 = time.time()
     if rounds is None:
         rounds = int(random.expovariate(0.05) + 2)
     assert isinstance(rounds, int)
     if frame is None:
         frame = Frame0()
     print("Launching an attack for %d rounds" % rounds)
     for _ in range(rounds):
         self.attack_frame(frame)
         if exhaustive_checks:
             frame.check()
         if time.time() - t0 > 60:
             print(">>> Stopped early, taking too long <<<")
             break
     print("\nAttack ended, checking the outcome... ", end='')
     frame.check()
     print(core.apply_color("bright_green", "PASSED"))
     t1 = time.time()
     print("Time taken = %.3fs" % (t1 - t0))
Exemplo n.º 8
0
 def debug(self, message):
     if message[0] != "[":
         message = "  " + message
     print(core.apply_color("grey", message), flush=True)