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
4 # found in the LICENSE file.
6 """Utility functions used by Generational and Mutational ClusterFuzz
16 APP_PATH_KEY
= 'APP_PATH'
17 FLAGS_PREFIX
= 'flags-'
19 IPC_FUZZER_APPLICATION
= 'ipc_fuzzer'
20 IPC_REPLAY_APPLICATION
= 'ipc_fuzzer_replay'
21 IPCDUMP_EXTENSION
= '.ipcdump'
25 '--ppapi-plugin-launcher',
26 '--renderer-cmd-prefix',
27 '--utility-cmd-prefix',
30 def application_name_for_platform(application_name
):
31 """Return application name for current platform."""
32 if platform() == 'WINDOWS':
33 return application_name
+ '.exe'
34 return application_name
36 def create_flags_file(ipcdump_testcase_path
, ipc_replay_application_path
):
37 """Create a flags file to add launch prefix to application command line."""
38 random_launch_prefix
= random
.choice(LAUNCH_PREFIXES
)
39 file_content
= '%s=%s' % (random_launch_prefix
, ipc_replay_application_path
)
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
)
46 def create_temp_file():
47 """Create a temporary file."""
48 temp_file
= tempfile
.NamedTemporaryFile(delete
=False)
52 def get_fuzzer_application_name():
53 """Get the application name for the fuzzer binary."""
54 return application_name_for_platform(IPC_FUZZER_APPLICATION
)
56 def get_replay_application_name():
57 """Get the application name for the replay binary."""
58 return application_name_for_platform(IPC_REPLAY_APPLICATION
)
60 def parse_arguments():
61 """Parse fuzzer arguments."""
62 parser
= argparse
.ArgumentParser()
63 parser
.add_argument('--input_dir')
64 parser
.add_argument('--output_dir')
65 parser
.add_argument('--no_of_files', type=int)
66 args
= parser
.parse_args();
67 if (not args
.input_dir
or
68 not args
.output_dir
or
69 not args
.no_of_files
):
75 def random_id(size
=16, chars
=string
.ascii_lowercase
):
76 """Return a random id string, default 16 characters long."""
77 return ''.join(random
.choice(chars
) for _
in range(size
))
79 def random_ipcdump_testcase_path(ipcdump_directory
):
80 """Return a random ipc testcase path."""
83 '%s%s%s' % (FUZZ_PREFIX
, random_id(), IPCDUMP_EXTENSION
))
86 """Return running platform."""
87 if sys
.platform
.startswith('win'):
89 if sys
.platform
.startswith('linux'):
91 if sys
.platform
== 'darwin':
94 assert False, 'Unknown platform'
96 def get_application_path():
97 """Return chrome application path."""
98 if APP_PATH_KEY
not in os
.environ
:
100 'Environment variable %s should be set to chrome path.' % APP_PATH_KEY
)
102 return os
.environ
[APP_PATH_KEY
]