summaryrefslogtreecommitdiff
path: root/buildscripts/merge_corpus.sh
blob: a7e0a598c5d93ce15cc0aa18534d0f911905341a (plain)
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
#!/bin/bash
# merge_corpus.sh
#
# Merges the corpus of each libfuzzer tests

set -o verbose
set -o errexit

input="build/libfuzzer_tests.txt"
corpus_dir="corpus"

if [ ! -f $input ] || [ ! -d $corpus_dir ]; then
    echo "Missing corpus information"
    exit 0
fi

# For each test, merge in new data.
while IFS= read -r line; do
    corpus_file="$corpus_dir/corpus-${line##*/}"

    if [ -d "${corpus_file}-new" ]; then
        if [ ! -d "${corpus_file}" ]; then
            # An error in a prior run left old corpus data orphaned, reclaim it.
            mv -v "${corpus_file}-new" "${corpus_file}"
        else
            # Somehow we have multiple corpii, treat non '-new' as official.
            rm -rv "${corpus_file}-new"
        fi
    fi

    # Create a new merge from old corpus and new run.
    mkdir -v "${corpus_file}-new"
    ./"$line" "${corpus_file}-new" "$corpus_file" -merge=1
done < "$input"

# Delete old corpus.
find corpus/* -not -name '*-new' -type d -exec rm -rv {} +

# Rename new corpus to old corpus.
for f in "$corpus_dir"/*-new; do
    mv -v "$f" "${f%-new}"
done