| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Generational ClusterFuzz fuzzer. It generates IPC messages using | 6 """Generational ClusterFuzz fuzzer. It generates IPC messages using |
| 7 GenerateTraits. Support of GenerateTraits for different types will be gradually | 7 GenerateTraits. Support of GenerateTraits for different types will be gradually |
| 8 added. | 8 added. |
| 9 """ | 9 """ |
| 10 | 10 |
| 11 import argparse | |
| 12 import os | 11 import os |
| 13 import random | 12 import random |
| 14 import string | |
| 15 import subprocess | 13 import subprocess |
| 16 import sys | 14 import sys |
| 17 import tempfile | 15 import utils |
| 18 import time | |
| 19 | 16 |
| 20 # Number of IPC messages per ipcdump | 17 IPC_GENERATE_APPLICATION = 'ipc_fuzzer_generate' |
| 21 NUM_IPC_MESSAGES = 1500 | 18 IPC_REPLAY_APPLICATION = 'ipc_fuzzer_replay' |
| 19 MAX_IPC_MESSAGES_PER_TESTCASE = 1500 |
| 22 | 20 |
| 23 def platform(): | |
| 24 if sys.platform.startswith('win'): | |
| 25 return 'WINDOWS' | |
| 26 if sys.platform.startswith('linux'): | |
| 27 return 'LINUX' | |
| 28 if sys.platform == 'darwin': | |
| 29 return 'MAC' | |
| 30 | |
| 31 assert False, 'Unknown platform' | |
| 32 | |
| 33 def random_id(size=16, chars=string.ascii_lowercase): | |
| 34 return ''.join(random.choice(chars) for x in range(size)) | |
| 35 | |
| 36 def random_ipcdump_path(ipcdump_dir): | |
| 37 return os.path.join(ipcdump_dir, 'fuzz-' + random_id() + '.ipcdump') | |
| 38 | 21 |
| 39 class GenerationalFuzzer: | 22 class GenerationalFuzzer: |
| 40 def parse_cf_args(self): | 23 def parse_arguments(self): |
| 41 parser = argparse.ArgumentParser() | 24 self.args = utils.parse_arguments() |
| 42 parser.add_argument('--input_dir') | |
| 43 parser.add_argument('--output_dir') | |
| 44 parser.add_argument('--no_of_files', type=int) | |
| 45 self.args = args = parser.parse_args(); | |
| 46 if not args.input_dir or not args.output_dir or not args.no_of_files: | |
| 47 parser.print_help() | |
| 48 sys.exit(1) | |
| 49 | 25 |
| 50 def get_paths(self): | 26 def set_application_paths(self): |
| 51 app_path_key = 'APP_PATH' | 27 chrome_application_path = utils.get_application_path() |
| 52 self.generate_binary = 'ipc_fuzzer_generate' | 28 chrome_application_directory = os.path.dirname(chrome_application_path) |
| 53 self.util_binary = 'ipc_message_util' | 29 self.ipc_generate_binary = utils.application_name_for_platform( |
| 54 if platform() == 'WINDOWS': | 30 IPC_GENERATE_APPLICATION) |
| 55 self.generate_binary += '.exe' | 31 self.ipc_replay_binary = utils.application_name_for_platform( |
| 56 self.util_binary += '.exe' | 32 IPC_REPLAY_APPLICATION) |
| 33 self.ipc_generate_binary_path = os.path.join( |
| 34 chrome_application_directory, self.ipc_generate_binary) |
| 35 self.ipc_replay_binary_path = os.path.join( |
| 36 chrome_application_directory, self.ipc_replay_binary) |
| 57 | 37 |
| 58 if app_path_key not in os.environ: | 38 def generate_ipcdump_testcase(self): |
| 59 sys.exit('Env var %s should be set to chrome path' % app_path_key) | 39 ipcdump_testcase_path = ( |
| 60 chrome_path = os.environ[app_path_key] | 40 utils.random_ipcdump_testcase_path(self.args.output_dir)) |
| 61 out_dir = os.path.dirname(chrome_path) | 41 num_ipc_messages = random.randint(1, MAX_IPC_MESSAGES_PER_TESTCASE) |
| 62 self.util_path = os.path.join(out_dir, self.util_binary) | 42 count_option = '--count=%d' % num_ipc_messages |
| 63 self.generate_path = os.path.join(out_dir, self.generate_binary) | |
| 64 | 43 |
| 65 def generate_ipcdump(self): | 44 cmd = [self.ipc_generate_binary_path, count_option, ipcdump_testcase_path] |
| 66 generated_ipcdump = random_ipcdump_path(self.args.output_dir) | 45 |
| 67 cmd = [self.generate_path, | |
| 68 '--count=' + str(NUM_IPC_MESSAGES), | |
| 69 generated_ipcdump] | |
| 70 if subprocess.call(cmd): | 46 if subprocess.call(cmd): |
| 71 sys.exit('%s failed' % self.generate_binary) | 47 sys.exit('%s failed.' % self.ipc_generate_binary) |
| 48 |
| 49 utils.create_flags_file(ipcdump_testcase_path, self.ipc_replay_binary_path) |
| 72 | 50 |
| 73 def main(self): | 51 def main(self): |
| 74 self.parse_cf_args() | 52 self.parse_arguments() |
| 75 self.get_paths() | 53 self.set_application_paths() |
| 76 for i in xrange(self.args.no_of_files): | 54 for _ in xrange(self.args.no_of_files): |
| 77 self.generate_ipcdump() | 55 self.generate_ipcdump_testcase() |
| 56 |
| 78 return 0 | 57 return 0 |
| 79 | 58 |
| 80 if __name__ == "__main__": | 59 if __name__ == "__main__": |
| 81 fuzzer = GenerationalFuzzer() | 60 fuzzer = GenerationalFuzzer() |
| 82 sys.exit(fuzzer.main()) | 61 sys.exit(fuzzer.main()) |
| OLD | NEW |