• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 the V8 project authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import json
6import os
7import random
8
9THIS_DIR = os.path.dirname(os.path.abspath(__file__))
10
11# List of configuration experiments for correctness fuzzing.
12# List of <probability>, <1st config name>, <2nd config name>, <2nd d8>.
13# Probabilities must add up to 100.
14with open(os.path.join(THIS_DIR, 'v8_fuzz_experiments.json')) as f:
15  FOOZZIE_EXPERIMENTS = json.load(f)
16
17# Additional flag experiments. List of tuples like
18# (<likelihood to use flags in [0,1)>, <flag>).
19with open(os.path.join(THIS_DIR, 'v8_fuzz_flags.json')) as f:
20  ADDITIONAL_FLAGS = json.load(f)
21
22
23class Config(object):
24  def __init__(self, name, rng=None):
25    """
26    Args:
27      name: Name of the used fuzzer.
28      rng: Random number generator for generating experiments.
29      random_seed: Random-seed used for d8 throughout one fuzz session.
30    """
31    self.name = name
32    self.rng = rng or random.Random()
33
34  def choose_foozzie_flags(self, foozzie_experiments=None, additional_flags=None):
35    """Randomly chooses a configuration from FOOZZIE_EXPERIMENTS.
36
37    Args:
38      foozzie_experiments: Override experiment config for testing.
39      additional_flags: Override additional flags for testing.
40
41    Returns: List of flags to pass to v8_foozzie.py fuzz harness.
42    """
43    foozzie_experiments = foozzie_experiments or FOOZZIE_EXPERIMENTS
44    additional_flags = additional_flags or ADDITIONAL_FLAGS
45
46    # Add additional flags to second config based on experiment percentages.
47    extra_flags = []
48    for p, flags in additional_flags:
49      if self.rng.random() < p:
50        for flag in flags.split():
51          extra_flags.append('--second-config-extra-flags=%s' % flag)
52
53    # Calculate flags determining the experiment.
54    acc = 0
55    threshold = self.rng.random() * 100
56    for prob, first_config, second_config, second_d8 in foozzie_experiments:
57      acc += prob
58      if acc > threshold:
59        return [
60          '--first-config=' + first_config,
61          '--second-config=' + second_config,
62          '--second-d8=' + second_d8,
63        ] + extra_flags
64    assert False
65