1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
#!/usr/bin/env python3
# Copyright 2023 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Download the fuzzing corpora associated with Chromium/V8 fuzz test.
For libfuzzer and centipede, assumes that fuzzer targets are already built
and reside in the BUILD_DIR directory. For fuzzilli download corpora from
gs bucket based on arch.
* Example usage: download_fuzz_corpora.py --download-dir [DOWNLOAD_DIR]
--build-dir [BUILD_DIR] --corpora-type [CORPORA_TYPE]
"""
LIBFUZZER_CORPORA_TYPE = 'libfuzzer'
CENTIPEDE_CORPORA_TYPE = 'centipede'
FUZZILLI_CORPORA_TYPE = 'fuzzilli'
ALL_CORPORA_TYPES = [
LIBFUZZER_CORPORA_TYPE, CENTIPEDE_CORPORA_TYPE, FUZZILLI_CORPORA_TYPE
]
CORPORA_BUCKET_BASE_URL_BY_TYPE = {
LIBFUZZER_CORPORA_TYPE:
'gs://clusterfuzz-libfuzzer-backup/corpus/libfuzzer/',
CENTIPEDE_CORPORA_TYPE:
'gs://clusterfuzz-centipede-backup/corpus/centipede/',
FUZZILLI_CORPORA_TYPE: 'gs://autozilli/',
}
import argparse
import coverage_consts
import logging
from multiprocessing import cpu_count, Pool
import os
import re
import subprocess
import sys
def _gsutil(cmd, cwd):
subprocess.run(cmd, cwd=cwd)
def _get_fuzzilli_corpora(arch):
output = subprocess.check_output(
['gsutil', 'ls',
CORPORA_BUCKET_BASE_URL_BY_TYPE[FUZZILLI_CORPORA_TYPE]]).decode('utf-8')
regex = {
'x64': 'autozilli-[0-9]+\.tgz',
'x86': 'autozilli-x86-[0-9]+\.tgz',
'arm64': 'autozilli-arm64-[0-9]+\.tgz',
}[arch]
return re.findall(regex, output)
def _download_corpus(args):
target = args[0]
download_dir = args[1]
corpora_type = args[2]
url = CORPORA_BUCKET_BASE_URL_BY_TYPE[corpora_type]
if corpora_type == FUZZILLI_CORPORA_TYPE:
# For a corpora file autozilli-1.tgz, it will be downloaded to
# [DOWNLOAD_DIR]/autozilli-1/autozilli-1.tgz
corpus_dir, _ = os.path.splitext(target)
corpus_url = os.path.join(url, target)
else:
corpus_dir = target
corpus_url = os.path.join(url, target, 'latest.zip')
subprocess.run(['mkdir', corpus_dir], cwd=download_dir)
cmd = ['gsutil', 'cp', corpus_url, corpus_dir]
_gsutil(cmd, download_dir)
def _unzip_corpus(args):
target = args[0]
download_dir = args[1]
target_folder = os.path.join(download_dir, target)
subprocess.run(['unzip', 'latest.zip'], cwd=target_folder)
subprocess.run(['rm', 'latest.zip'], cwd=target_folder)
try:
# Unzipping the corpora often also contains a "regressions" folder, which
# is a subset of the total corpus, so can be ignored/removed
subprocess.run(['rm', '-rf', 'regressions'], cwd=target_folder)
except:
pass
def _unzip_fuzzilli_corpus(args):
corpus = args[0]
download_dir = args[1]
base, _ = os.path.splitext(corpus)
corpus_dir = os.path.join(download_dir, base)
subprocess.run(['tar', 'xzvf', corpus], cwd=corpus_dir)
subprocess.run(['rm', corpus], cwd=corpus_dir)
def _ParseCommandArguments():
"""Adds and parses relevant arguments for tool comands.
Returns:
A dictionary representing the arguments.
"""
arg_parser = argparse.ArgumentParser()
arg_parser.usage = __doc__
arg_parser.add_argument('--download-dir',
type=str,
required=True,
help='Directory into which corpora are downloaded.')
arg_parser.add_argument('--build-dir',
required=True,
type=str,
help='Directory where fuzzers were built.')
arg_parser.add_argument('--corpora-type',
choices=ALL_CORPORA_TYPES,
default=LIBFUZZER_CORPORA_TYPE,
help='The type of corpora to download.')
arg_parser.add_argument(
'--arch',
choices=['x64', 'x86', 'arm64'],
default='x64',
help='The cpu architecture of the target. Fuzzilli only.')
args = arg_parser.parse_args()
return args
def Main():
args = _ParseCommandArguments()
exit
if not args.download_dir:
logging.error("No download_dir given")
exit
if not os.path.isdir(args.download_dir):
logging.error("%s does not exist or is not a directory" % args.download_dir)
exit
if not args.build_dir:
logging.error("No build_dir given")
exit
if not os.path.isdir(args.build_dir):
logging.error("%s does not exist or is not a directory" % args.build_dir)
exit
if args.corpora_type == FUZZILLI_CORPORA_TYPE:
corpora_to_download = _get_fuzzilli_corpora(args.arch)
else:
corpora_to_download = []
for target in os.listdir(args.build_dir):
if target.endswith('_fuzzer'):
corpora_to_download.append(target)
print("Corpora to download: " + str(corpora_to_download))
with Pool(cpu_count()) as p:
results = p.map(_download_corpus,
[(corpus, args.download_dir, args.corpora_type)
for corpus in corpora_to_download])
unzip_func = (_unzip_fuzzilli_corpus if args.corpora_type
== FUZZILLI_CORPORA_TYPE else _unzip_corpus)
with Pool(cpu_count()) as p:
results = p.map(unzip_func, [(corpus, args.download_dir)
for corpus in corpora_to_download])
if __name__ == '__main__':
sys.exit(Main())
|