aizatsky | 198e302 | 2016-03-08 21:33:46 | [diff] [blame] | 1 | #!/usr/bin/python2 |
| 2 | # |
| 3 | # Copyright 2016 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 | |
| 7 | """Archive corpus file into zip and generate .d depfile. |
| 8 | |
| 9 | Invoked by GN from fuzzer_test.gni. |
| 10 | """ |
| 11 | |
| 12 | from __future__ import print_function |
| 13 | import argparse |
| 14 | import os |
| 15 | import sys |
mmoroz | 0aa3479 | 2016-12-02 10:07:59 | [diff] [blame] | 16 | import warnings |
aizatsky | 198e302 | 2016-03-08 21:33:46 | [diff] [blame] | 17 | import zipfile |
| 18 | |
Abhishek Arya | 7f66e07 | 2019-04-12 18:46:35 | [diff] [blame] | 19 | SEED_CORPUS_LIMIT_MB = 100 |
| 20 | |
aizatsky | 198e302 | 2016-03-08 21:33:46 | [diff] [blame] | 21 | |
| 22 | def main(): |
| 23 | parser = argparse.ArgumentParser(description="Generate fuzzer config.") |
mmoroz | 902ef43 | 2017-02-07 17:03:37 | [diff] [blame] | 24 | parser.add_argument('corpus_directories', metavar='corpus_dir', type=str, |
| 25 | nargs='+') |
| 26 | parser.add_argument('--output', metavar='output_archive_name.zip', |
| 27 | required=True) |
aizatsky | 198e302 | 2016-03-08 21:33:46 | [diff] [blame] | 28 | |
Jonathan Metzman | 950be74 | 2019-04-12 22:54:26 | [diff] [blame] | 29 | args = parser.parse_args() |
aizatsky | 198e302 | 2016-03-08 21:33:46 | [diff] [blame] | 30 | corpus_files = [] |
Abhishek Arya | 7f66e07 | 2019-04-12 18:46:35 | [diff] [blame] | 31 | seed_corpus_path = args.output |
mmoroz | 146a2e8 | 2016-04-28 10:32:35 | [diff] [blame] | 32 | |
mmoroz | 902ef43 | 2017-02-07 17:03:37 | [diff] [blame] | 33 | for directory in args.corpus_directories: |
Max Moroz | ebe22580 | 2017-08-08 06:52:33 | [diff] [blame] | 34 | if not os.path.exists(directory): |
| 35 | raise Exception('The given seed_corpus directory (%s) does not exist.' % |
| 36 | directory) |
mmoroz | 902ef43 | 2017-02-07 17:03:37 | [diff] [blame] | 37 | for (dirpath, _, filenames) in os.walk(directory): |
| 38 | for filename in filenames: |
| 39 | full_filename = os.path.join(dirpath, filename) |
| 40 | corpus_files.append(full_filename) |
aizatsky | 198e302 | 2016-03-08 21:33:46 | [diff] [blame] | 41 | |
Abhishek Arya | 7f66e07 | 2019-04-12 18:46:35 | [diff] [blame] | 42 | with zipfile.ZipFile(seed_corpus_path, 'w') as z: |
mmoroz | 0aa3479 | 2016-12-02 10:07:59 | [diff] [blame] | 43 | # Turn warnings into errors to interrupt the build: crbug.com/653920. |
| 44 | with warnings.catch_warnings(): |
| 45 | warnings.simplefilter("error") |
| 46 | for i, corpus_file in enumerate(corpus_files): |
| 47 | # To avoid duplication of filenames inside the archive, use numbers. |
| 48 | arcname = '%016d' % i |
| 49 | z.write(corpus_file, arcname) |
aizatsky | 198e302 | 2016-03-08 21:33:46 | [diff] [blame] | 50 | |
Abhishek Arya | 7f66e07 | 2019-04-12 18:46:35 | [diff] [blame] | 51 | if os.path.getsize(seed_corpus_path) > SEED_CORPUS_LIMIT_MB * 1024 * 1024: |
| 52 | print('Seed corpus %s exceeds maximum allowed size (%d MB).' % |
| 53 | (seed_corpus_path, SEED_CORPUS_LIMIT_MB)) |
| 54 | sys.exit(-1) |
aizatsky | 198e302 | 2016-03-08 21:33:46 | [diff] [blame] | 55 | |
| 56 | if __name__ == '__main__': |
| 57 | main() |