summaryrefslogtreecommitdiff
path: root/chromium/testing/libfuzzer/gen_fuzzer_config.py
blob: 5628a30f7b5d4ce2b977c81a05f209018ed9c97e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python2
#
# Copyright (c) 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Generate or update an existing config (.options file) for libfuzzer test.

Invoked by GN from fuzzer_test.gni.
"""

import argparse
import os
import sys


OPTIONS_FILE_TEMPLATE = '''# This is automatically generated fuzzer config.
[libfuzzer]
dict = %(dict)s
'''

def main():
  parser = argparse.ArgumentParser(description="Generate fuzzer config.")
  parser.add_argument('--config', required=True)
  parser.add_argument('--dict', required=True)
  parser.add_argument('--libfuzzer_options', required=False)
  args = parser.parse_args()

  config_path = args.config
  # Dict will be copied into build directory, use only its basename for config.
  dict_name = os.path.basename(args.dict)

  if not args.libfuzzer_options:
    # Generate .options file with initialized 'dict' option.
    with open(config_path, 'w') as options_file:
      options_file.write(OPTIONS_FILE_TEMPLATE % {'dict': dict_name})
    return

  # Append 'dict' option to an existing .options file.
  initial_config = open(args.libfuzzer_options).read()
  with open(config_path, 'w') as options_file:
    options_file.write(initial_config)
    options_file.write('\ndict = %s\n' % dict_name)


if __name__ == '__main__':
  main()