|
| 1 | +# Shared helper for libFuzzer corpus builder scripts. |
| 2 | +# |
| 3 | +# Usage from a fuzz target-specific builder: |
| 4 | +# |
| 5 | +# root="$(readlink -f -- "$1")" |
| 6 | +# output_corpus="$2" |
| 7 | +# source "$root/etc/fuzz-corpus-builder.sh" |
| 8 | +# build_fuzz_corpus "$root" "$output_corpus" "gix-pack" "index_file" |
| 9 | +# |
| 10 | +# The target-specific script is invoked by Google OSS Fuzz and |
| 11 | +# their build script at https://github.com/google/oss-fuzz/blob/master/projects/gitoxide/build.sh |
| 12 | +# with the repository root and the output zip path. |
| 13 | +# This helper adds all files from: |
| 14 | +# |
| 15 | +# <crate>/fuzz/corpus/<target>/* |
| 16 | +# |
| 17 | +# If present, it also adds files from: |
| 18 | +# |
| 19 | +# <crate>/fuzz/artifacts/<target>/* |
| 20 | +# |
| 21 | +# Corpus files and artifact files are zipped with `-j`, matching the flat corpus |
| 22 | +# archive layout it expects. Empty input groups are skipped so artifact-only |
| 23 | +# targets can still produce a corpus archive. Artifacts are added in a separate |
| 24 | +# zip invocation so a duplicate basename updates the archive instead of making |
| 25 | +# zip fail with "cannot repeat names in zip file". |
| 26 | +build_fuzz_corpus() { |
| 27 | + local root="$1" |
| 28 | + local output_corpus="$2" |
| 29 | + local crate="$3" |
| 30 | + local target="$4" |
| 31 | + local fuzz_dir |
| 32 | + local corpus_dir |
| 33 | + local artifacts_dir |
| 34 | + |
| 35 | + fuzz_dir="$(readlink -f -- "$root/$crate/fuzz")" |
| 36 | + corpus_dir="$fuzz_dir/corpus/$target" |
| 37 | + artifacts_dir="$fuzz_dir/artifacts/$target" |
| 38 | + |
| 39 | + echo "$root" |
| 40 | + echo "$corpus_dir" |
| 41 | + echo "$artifacts_dir" |
| 42 | + |
| 43 | + shopt -s nullglob |
| 44 | + |
| 45 | + local corpus_files=("$corpus_dir"/*) |
| 46 | + if ((${#corpus_files[@]})); then |
| 47 | + zip -j "$output_corpus" "${corpus_files[@]}" |
| 48 | + fi |
| 49 | + |
| 50 | + if [[ -d "$artifacts_dir" ]]; then |
| 51 | + local artifact_files=("$artifacts_dir"/*) |
| 52 | + if ((${#artifact_files[@]})); then |
| 53 | + # A second zip invocation updates duplicate basenames instead of aborting. |
| 54 | + zip -j "$output_corpus" "${artifact_files[@]}" |
| 55 | + fi |
| 56 | + fi |
| 57 | +} |
0 commit comments