gen_fuzzer_config.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) 2015 The Chromium Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. """Generate or update an existing config (.options file) for libfuzzer test.
  7. Invoked by GN from fuzzer_test.gni.
  8. """
  9. import argparse
  10. import os
  11. import sys
  12. if sys.version_info.major == 2:
  13. from ConfigParser import ConfigParser
  14. else:
  15. from configparser import ConfigParser
  16. def AddSectionOptions(config, section_name, options):
  17. """Add |options| to the |section_name| section of |config|.
  18. Throws an
  19. assertion error if any option in |options| does not have exactly two
  20. elements.
  21. """
  22. if not options:
  23. return
  24. config.add_section(section_name)
  25. for option_and_value in options:
  26. assert len(option_and_value) == 2, (
  27. '%s is not an option, value pair' % option_and_value)
  28. config.set(section_name, *option_and_value)
  29. def main():
  30. parser = argparse.ArgumentParser(description='Generate fuzzer config.')
  31. parser.add_argument('--config', required=True)
  32. parser.add_argument('--dict')
  33. parser.add_argument('--libfuzzer_options', nargs='+', default=[])
  34. parser.add_argument('--asan_options', nargs='+', default=[])
  35. parser.add_argument('--msan_options', nargs='+', default=[])
  36. parser.add_argument('--ubsan_options', nargs='+', default=[])
  37. parser.add_argument('--grammar_options', nargs='+', default=[])
  38. parser.add_argument(
  39. '--environment_variables',
  40. nargs='+',
  41. default=[],
  42. choices=['AFL_DRIVER_DONT_DEFER=1'])
  43. args = parser.parse_args()
  44. # Script shouldn't be invoked without any arguments, but just in case.
  45. if not (args.dict or args.libfuzzer_options or args.environment_variables or
  46. args.asan_options or args.msan_options or args.ubsan_options or
  47. args.grammar_options):
  48. return
  49. config = ConfigParser()
  50. libfuzzer_options = []
  51. if args.dict:
  52. libfuzzer_options.append(('dict', os.path.basename(args.dict)))
  53. libfuzzer_options.extend(
  54. option.split('=') for option in args.libfuzzer_options)
  55. AddSectionOptions(config, 'libfuzzer', libfuzzer_options)
  56. AddSectionOptions(config, 'asan',
  57. [option.split('=') for option in args.asan_options])
  58. AddSectionOptions(config, 'msan',
  59. [option.split('=') for option in args.msan_options])
  60. AddSectionOptions(config, 'ubsan',
  61. [option.split('=') for option in args.ubsan_options])
  62. AddSectionOptions(config, 'grammar',
  63. [option.split('=') for option in args.grammar_options])
  64. AddSectionOptions(
  65. config, 'env',
  66. [option.split('=') for option in args.environment_variables])
  67. # Generate .options file.
  68. config_path = args.config
  69. with open(config_path, 'w') as options_file:
  70. options_file.write(
  71. '# This is an automatically generated config for ClusterFuzz.\n')
  72. config.write(options_file)
  73. if __name__ == '__main__':
  74. main()