v8_fuzz_config.py 2.17 KB
Newer Older
1 2 3 4
# Copyright 2018 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

5 6
import json
import os
7 8
import random

9 10
THIS_DIR = os.path.dirname(os.path.abspath(__file__))

11 12 13
# List of configuration experiments for correctness fuzzing.
# List of <probability>, <1st config name>, <2nd config name>, <2nd d8>.
# Probabilities must add up to 100.
14 15
with open(os.path.join(THIS_DIR, 'v8_fuzz_experiments.json')) as f:
  FOOZZIE_EXPERIMENTS = json.load(f)
16

17 18
# Additional flag experiments. List of tuples like
# (<likelihood to use flags in [0,1)>, <flag>).
19 20 21
with open(os.path.join(THIS_DIR, 'v8_fuzz_flags.json')) as f:
  ADDITIONAL_FLAGS = json.load(f)

22

23
class Config(object):
24
  def __init__(self, name, rng=None):
25 26 27 28 29 30
    """
    Args:
      name: Name of the used fuzzer.
      rng: Random number generator for generating experiments.
      random_seed: Random-seed used for d8 throughout one fuzz session.
    """
31 32 33
    self.name = name
    self.rng = rng or random.Random()

34
  def choose_foozzie_flags(self, foozzie_experiments=None, additional_flags=None):
35 36
    """Randomly chooses a configuration from FOOZZIE_EXPERIMENTS.

37 38 39 40
    Args:
      foozzie_experiments: Override experiment config for testing.
      additional_flags: Override additional flags for testing.

41 42
    Returns: List of flags to pass to v8_foozzie.py fuzz harness.
    """
43 44
    foozzie_experiments = foozzie_experiments or FOOZZIE_EXPERIMENTS
    additional_flags = additional_flags or ADDITIONAL_FLAGS
45 46 47

    # Add additional flags to second config based on experiment percentages.
    extra_flags = []
48
    for p, flags in additional_flags:
49
      if self.rng.random() < p:
50 51
        for flag in flags.split():
          extra_flags.append('--second-config-extra-flags=%s' % flag)
52 53

    # Calculate flags determining the experiment.
54 55
    acc = 0
    threshold = self.rng.random() * 100
56
    for prob, first_config, second_config, second_d8 in foozzie_experiments:
57 58 59 60 61 62
      acc += prob
      if acc > threshold:
        return [
          '--first-config=' + first_config,
          '--second-config=' + second_config,
          '--second-d8=' + second_d8,
63
        ] + extra_flags
64
    assert False