• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3# Copyright 2021 Google LLC
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8# This tool updates the OSS-Fuzz corpus using Google Cloud's 'gsutil' tool.
9
10# You will need to be given access to the Google Storage fuzzer repo (at
11# gs://skia-fuzzer/oss-fuzz/) by the Skia Infra team.
12
13# You will also need to set up credentials for gsutil on your machine by running:
14#   gcloud auth login
15
16import os
17import subprocess
18import tempfile
19import zipfile
20
21# Locate this script in the file system.
22startDir = os.path.dirname(os.path.abspath(__file__))
23fileNum = 1
24
25# Prepare two scratch zip files, one for the input data as-is and another with 256-byte padding.
26with tempfile.NamedTemporaryFile(suffix='primary.zip', delete=False, mode='w') as pathToPrimaryZip:
27    with tempfile.NamedTemporaryFile(suffix='pad.zip', delete=False, mode='w') as pathToPaddedZip:
28        with zipfile.ZipFile(pathToPrimaryZip.name, 'w', zipfile.ZIP_DEFLATED) as primaryArchive:
29            with zipfile.ZipFile(pathToPaddedZip.name, 'w', zipfile.ZIP_DEFLATED) as paddedArchive:
30                # Iterate over every file in this directory and use it to assemble our corpus.
31                for root, dirs, files in os.walk(startDir):
32                    for file in files:
33                        # Exclude files that won't be useful fuzzer inputs.
34                        if (not file.startswith('.')        # Hidden
35                            and not file.endswith('.py')    # Python
36                            and not file.endswith('.test')  # ES2 conformance script
37                            and not file.endswith('.txt')): # Text
38                            # Prepend a number to each output filename to guarantee uniqueness.
39                            pathInZip = '%d_%s' % (fileNum, file)
40                            fileNum += 1
41                            with open('%s/%s' % (root, file), 'r') as skslFile:
42                                # Read the SkSL text as input.
43                                inputSkSL = skslFile.read()
44                                # In the primary archive, write the input SkSL as-is.
45                                primaryArchive.writestr(pathInZip, inputSkSL)
46                                # In the padded archive, write the input SkSL with 256 bonus bytes.
47                                paddedSkSL = inputSkSL + ("/" * 256)
48                                paddedArchive.writestr(pathInZip, paddedSkSL)
49
50    try:
51        # Upload both zip files to cloud storage.
52        output = subprocess.check_output(
53                ['gsutil', 'cp', pathToPrimaryZip.name,
54                 'gs://skia-fuzzer/oss-fuzz/sksl_seed_corpus.zip'],
55                 stderr=subprocess.STDOUT)
56
57        output = subprocess.check_output(
58                ['gsutil', 'cp', pathToPaddedZip.name,
59                 'gs://skia-fuzzer/oss-fuzz/sksl_with_256_padding_seed_corpus.zip'],
60                stderr=subprocess.STDOUT)
61
62        # Make the uploaded files world-readable.
63        output = subprocess.check_output(
64                ['gsutil', 'acl', 'ch', '-u', 'AllUsers:R',
65                 'gs://skia-fuzzer/oss-fuzz/sksl_seed_corpus.zip'],
66                stderr=subprocess.STDOUT)
67
68        output = subprocess.check_output(
69                ['gsutil', 'acl', 'ch', '-u', 'AllUsers:R',
70                 'gs://skia-fuzzer/oss-fuzz/sksl_with_256_padding_seed_corpus.zip'],
71                stderr=subprocess.STDOUT)
72
73    except subprocess.CalledProcessError as err:
74        # Report the error.
75        print("### Unable to upload fuzzer corpus to Google Cloud:")
76        print("    " + "\n    ".join(err.output.splitlines()))
77        print("\nPlease read the notes at the top of update_fuzzer.py for next steps.\n")
78        sys.exit(err.returncode)
79