1#!/bin/bash -eu
2# Copyright 2020 Google Inc.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16################################################################################
17
18for PROJECT in ${SRC}/*;
19do
20  PROJECT=$(basename ${PROJECT})
21
22  # A libyal project should have an ossfuzz directory and a synclibs.sh script.
23  if ! test -d ${SRC}/${PROJECT}/ossfuzz || ! test -x ${SRC}/${PROJECT}/synclibs.sh;
24  then
25    continue
26  fi
27  cd ${SRC}/${PROJECT}
28
29  # Prepare the project source for build.
30  ./synclibs.sh
31  ./autogen.sh
32  ./configure --enable-shared=no
33
34  # Build the project and fuzzer binaries.
35  make -j$(nproc) LIB_FUZZING_ENGINE=${LIB_FUZZING_ENGINE}
36
37  # Download the test data if supported.
38  if test -x ./synctestdata.sh;
39  then
40    ./synctestdata.sh
41  fi
42
43  # Copy the fuzzer binaries and test data to the output directory.
44  for FUZZ_TARGET in $(cd ossfuzz && find . -executable -type f);
45  do
46    FUZZ_TARGET=$(basename ${FUZZ_TARGET})
47
48    # Prefix the fuzzer binaries with the project name.
49    cp ossfuzz/${FUZZ_TARGET} ${OUT}/${PROJECT}_${FUZZ_TARGET}
50
51    # Download the test data if supported.
52    LIBYAL_TYPE_NAME=${FUZZ_TARGET/_fuzzer/};
53
54    if test -f tests/data/${LIBYAL_TYPE_NAME/}.1;
55    then
56      (cd tests/data && zip ${OUT}/${PROJECT}_${FUZZ_TARGET}_seed_corpus.zip ${LIBYAL_TYPE_NAME}.*)
57
58    elif test -d tests/input/public;
59    then
60      (cd tests/input/public && zip ${OUT}/${PROJECT}_${FUZZ_TARGET}_seed_corpus.zip *)
61
62    else
63      echo "Missing test data for seed corpus."
64      exit 1
65    fi
66  done
67done
68