|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Utility functions for micro-experiment run.""" |
| 15 | + |
| 16 | +import random |
| 17 | +import os |
| 18 | +import tempfile |
| 19 | +import multiprocessing |
| 20 | +import zipfile |
| 21 | +from typing import List |
| 22 | + |
| 23 | +from common import experiment_utils |
| 24 | +from common import filesystem |
| 25 | +from common import logs |
| 26 | + |
| 27 | +MAX_SOURCE_CORPUS_FILES = 1 |
| 28 | +CORPUS_ELEMENT_BYTES_LIMIT = 1 * 1024 * 1024 |
| 29 | + |
| 30 | + |
| 31 | +def initialize_random_corpus_fuzzing(benchmarks: List[str], num_trials: int): |
| 32 | + """Prepare corpus for micro experiment.""" |
| 33 | + pool_args = () |
| 34 | + with multiprocessing.Pool(*pool_args) as pool: |
| 35 | + pool.starmap(prepare_benchmark_random_corpus, |
| 36 | + [(benchmark, num_trials) for benchmark in benchmarks]) |
| 37 | + logs.info('Done preparing corpus for micro experiment') |
| 38 | + |
| 39 | + |
| 40 | +# pylint: disable=too-many-locals |
| 41 | +def prepare_benchmark_random_corpus(benchmark: str, num_trials: int): |
| 42 | + """Prepare corpus for given benchmark.""" |
| 43 | + # Temporary location to park corpus files before get picked randomly. |
| 44 | + benchmark_unarchived_corpora = os.path.join( |
| 45 | + experiment_utils.get_oss_fuzz_corpora_unarchived_path(), benchmark) |
| 46 | + filesystem.create_directory(benchmark_unarchived_corpora) |
| 47 | + |
| 48 | + # Unzip oss fuzz corpus. |
| 49 | + corpus_archive_filename = f'{benchmark}.zip' |
| 50 | + oss_fuzz_corpus_archive_path = os.path.join( |
| 51 | + experiment_utils.get_oss_fuzz_corpora_filestore_path(), |
| 52 | + corpus_archive_filename) |
| 53 | + with zipfile.ZipFile(oss_fuzz_corpus_archive_path) as zip_file: |
| 54 | + idx = 0 |
| 55 | + for seed_corpus_file in zip_file.infolist(): |
| 56 | + if seed_corpus_file.filename.endswith('/'): |
| 57 | + # Ignore directories. |
| 58 | + continue |
| 59 | + # Allow callers to opt-out of unpacking large files. |
| 60 | + if seed_corpus_file.file_size > CORPUS_ELEMENT_BYTES_LIMIT: |
| 61 | + continue |
| 62 | + output_filename = f'{idx:016d}' |
| 63 | + output_file_path = os.path.join(benchmark_unarchived_corpora, |
| 64 | + output_filename) |
| 65 | + zip_file.extract(seed_corpus_file, output_file_path) |
| 66 | + idx += 1 |
| 67 | + |
| 68 | + # Path used to store and feed seed corpus for benchmark runner |
| 69 | + # each trial group will have the same seed input(s). |
| 70 | + benchmark_random_corpora = os.path.join( |
| 71 | + experiment_utils.get_random_corpora_filestore_path(), benchmark) |
| 72 | + filesystem.create_directory(benchmark_random_corpora) |
| 73 | + |
| 74 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 75 | + all_corpus_files = [] |
| 76 | + for root, _, files in os.walk(benchmark_unarchived_corpora): |
| 77 | + for filename in files: |
| 78 | + file_path = os.path.join(root, filename) |
| 79 | + all_corpus_files.append(file_path) |
| 80 | + |
| 81 | + all_corpus_files.sort() |
| 82 | + trial_group_num = 0 |
| 83 | + # All trials in the same group will start with the same |
| 84 | + # set of randomly selected seed files. |
| 85 | + while trial_group_num < num_trials: |
| 86 | + trial_group_subdir = f'trial-group-{trial_group_num}' |
| 87 | + custom_corpus_trial_dir = os.path.join(benchmark_random_corpora, |
| 88 | + trial_group_subdir) |
| 89 | + src_dir = os.path.join(tmp_dir, 'source') |
| 90 | + filesystem.recreate_directory(src_dir) |
| 91 | + |
| 92 | + source_files = random.sample(all_corpus_files, |
| 93 | + MAX_SOURCE_CORPUS_FILES) |
| 94 | + for file in source_files: |
| 95 | + filesystem.copy(file, src_dir) |
| 96 | + |
| 97 | + # Copy only the src directory. |
| 98 | + filesystem.copytree(src_dir, custom_corpus_trial_dir) |
| 99 | + trial_group_num += 1 |
| 100 | + |
| 101 | + return [] |
0 commit comments