• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright 2016 The Chromium Authors
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6"""Archive corpus file into zip and generate .d depfile.
7
8Invoked by GN from fuzzer_test.gni.
9"""
10
11from __future__ import print_function
12import argparse
13import os
14import sys
15import warnings
16import zipfile
17
18SEED_CORPUS_LIMIT_MB = 100
19
20
21def main():
22  parser = argparse.ArgumentParser(description='Generate fuzzer config.')
23  parser.add_argument('corpus_directories',
24                      metavar='corpus_dir',
25                      type=str,
26                      nargs='+')
27  parser.add_argument('--output',
28                      metavar='output_archive_name.zip',
29                      required=True)
30
31  args = parser.parse_args()
32  corpus_files = []
33  seed_corpus_path = args.output
34
35  for directory in args.corpus_directories:
36    if not os.path.exists(directory):
37      raise Exception('The given seed_corpus directory (%s) does not exist.' %
38                      directory)
39    for (dirpath, _, filenames) in os.walk(directory):
40      for filename in filenames:
41        full_filename = os.path.join(dirpath, filename)
42        corpus_files.append(full_filename)
43
44  with zipfile.ZipFile(seed_corpus_path, 'w') as z:
45    # Turn warnings into errors to interrupt the build: crbug.com/653920.
46    with warnings.catch_warnings():
47      warnings.simplefilter('error')
48      for i, corpus_file in enumerate(corpus_files):
49        # To avoid duplication of filenames inside the archive, use numbers.
50        arcname = '%016d' % i
51        z.write(corpus_file, arcname)
52
53  if os.path.getsize(seed_corpus_path) > SEED_CORPUS_LIMIT_MB * 1024 * 1024:
54    print('Seed corpus %s exceeds maximum allowed size (%d MB).' %
55          (seed_corpus_path, SEED_CORPUS_LIMIT_MB))
56    sys.exit(-1)
57
58
59if __name__ == '__main__':
60  main()
61