utils.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Copyright 2014 The Chromium 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. """Utility functions used by Generational and Mutational ClusterFuzz
  5. fuzzers."""
  6. import argparse
  7. import os
  8. import random
  9. import string
  10. import sys
  11. import tempfile
  12. BUILD_DIR_KEY = 'BUILD_DIR'
  13. FLAGS_PREFIX = 'flags-'
  14. FUZZ_PREFIX = 'fuzz-'
  15. IPC_FUZZER_APPLICATION = 'ipc_fuzzer'
  16. IPC_REPLAY_APPLICATION = 'ipc_fuzzer_replay'
  17. IPCDUMP_EXTENSION = '.ipcdump'
  18. UNCOMMON_PREFIX_CHANCE = 10 # 1 in 10
  19. COMMON_LAUNCH_PREFIXES = [
  20. '--renderer-cmd-prefix',
  21. ]
  22. UNCOMMON_LAUNCH_PREFIXES = [
  23. '--plugin-launcher',
  24. '--ppapi-plugin-launcher',
  25. '--utility-cmd-prefix',
  26. ]
  27. def application_name_for_platform(application_name):
  28. """Return application name for current platform."""
  29. if platform() == 'WINDOWS':
  30. return application_name + '.exe'
  31. return application_name
  32. def create_flags_file(ipcdump_testcase_path):
  33. """Create a flags file to add launch prefix to application command line."""
  34. prefixes = (UNCOMMON_LAUNCH_PREFIXES if
  35. random.randint(1, UNCOMMON_PREFIX_CHANCE) == 1 else
  36. COMMON_LAUNCH_PREFIXES)
  37. random_launch_prefix = random.choice(prefixes)
  38. application_name = application_name_for_platform(IPC_REPLAY_APPLICATION)
  39. file_content = '%s=%%APP_DIR%%%s%s' % (random_launch_prefix, os.path.sep,
  40. application_name)
  41. flags_file_path = ipcdump_testcase_path.replace(FUZZ_PREFIX, FLAGS_PREFIX)
  42. file_handle = open(flags_file_path, 'w')
  43. file_handle.write(file_content)
  44. file_handle.close()
  45. def create_temp_file():
  46. """Create a temporary file."""
  47. temp_file = tempfile.NamedTemporaryFile(delete=False)
  48. temp_file.close()
  49. return temp_file.name
  50. def get_fuzzer_application_name():
  51. """Get the application name for the fuzzer binary."""
  52. return application_name_for_platform(IPC_FUZZER_APPLICATION)
  53. def get_replay_application_name():
  54. """Get the application name for the replay binary."""
  55. return application_name_for_platform(IPC_REPLAY_APPLICATION)
  56. def parse_arguments():
  57. """Parse fuzzer arguments."""
  58. parser = argparse.ArgumentParser()
  59. parser.add_argument('--input_dir')
  60. parser.add_argument('--output_dir')
  61. parser.add_argument('--no_of_files', type=int)
  62. args = parser.parse_args()
  63. if (not args.input_dir or not args.output_dir or not args.no_of_files):
  64. parser.print_help()
  65. sys.exit(1)
  66. return args
  67. def random_id(size=16, chars=string.ascii_lowercase):
  68. """Return a random id string, default 16 characters long."""
  69. return ''.join(random.choice(chars) for _ in range(size))
  70. def random_ipcdump_testcase_path(ipcdump_directory):
  71. """Return a random ipc testcase path."""
  72. return os.path.join(ipcdump_directory,
  73. '%s%s%s' % (FUZZ_PREFIX, random_id(), IPCDUMP_EXTENSION))
  74. def platform():
  75. """Return running platform."""
  76. if sys.platform.startswith('win'):
  77. return 'WINDOWS'
  78. if sys.platform.startswith('linux'):
  79. return 'LINUX'
  80. if sys.platform == 'darwin':
  81. return 'MAC'
  82. assert False, 'Unknown platform'
  83. def get_application_path(application_name):
  84. """Return chrome application path."""
  85. if BUILD_DIR_KEY not in os.environ:
  86. sys.exit('Environment variable %s should be set to chrome directory.' %
  87. BUILD_DIR_KEY)
  88. for root, _, files in os.walk(os.environ[BUILD_DIR_KEY]):
  89. for filename in files:
  90. if filename == application_name:
  91. return os.path.join(root, application_name)
  92. sys.exit('Application %s was not found in chrome directory.' %
  93. application_name)