Debian Codegraph @

tests/fuzzer/oss-fuzz/build.sh

raw
1 #!/bin/bash
2 # Copyright 2018 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
18 # This script is meant to be run by the oss-fuzz infrastructure from the script
19 # https://github.com/google/oss-fuzz/blob/master/projects/libwebp/build.sh
20 # It builds the different fuzz targets.
21 # Only the libfuzzer engine is supported.
22
23 # To test changes to this file:
24 # - make changes and commit to your REPO
25 # - run:
26 # git clone --depth=1 [email protected]:google/oss-fuzz.git
27 # cd oss-fuzz
28 # - modify projects/libwebp/Dockerfile to point to your REPO
29 # - run:
30 # python3 infra/helper.py build_image libwebp
31 # # enter 'y' and wait for everything to be downloaded
32 # - run:
33 # python3 infra/helper.py build_fuzzers --sanitizer address libwebp
34 # # wait for the tests to be built
35 # And then run the fuzzer locally, for example:
36 # python3 infra/helper.py run_fuzzer libwebp \
37 # --sanitizer address \
38 # [email protected]
39
40 set -eu
41
42 EXTRA_CMAKE_FLAGS=""
43 export CXXFLAGS="${CXXFLAGS} -DFUZZTEST_COMPATIBILITY_MODE"
44 EXTRA_CMAKE_FLAGS="-DFUZZTEST_COMPATIBILITY_MODE=libfuzzer"
45
46 # limit allocation size to reduce spurious OOMs
47 WEBP_CFLAGS="$CFLAGS -DWEBP_MAX_IMAGE_SIZE=838860800" # 800MiB
48
49 export CFLAGS="$WEBP_CFLAGS"
50 cmake -S . -B build -DWEBP_BUILD_FUZZTEST=ON ${EXTRA_CMAKE_FLAGS}
51 cd build && make -j$(nproc) && cd ..
52
53 find $SRC/libwebp-test-data -type f -size -32k -iname "*.webp" \
54 -exec zip -qju fuzz_seed_corpus.zip "{}" \;
55
56 # The following is taken from https://github.com/google/oss-fuzz/blob/31ac7244748ea7390015455fb034b1f4eda039d9/infra/base-images/base-builder/compile_fuzztests.sh#L59
57 # Iterate the fuzz binaries and list each fuzz entrypoint in the binary. For
58 # each entrypoint create a wrapper script that calls into the binaries the
59 # given entrypoint as argument.
60 # The scripts will be named:
61 # {binary_name}@{fuzztest_entrypoint}
62 FUZZ_TEST_BINARIES_OUT_PATHS=$(find ./build/tests/fuzzer/ -executable -type f)
63 echo "Fuzz binaries: $FUZZ_TEST_BINARIES_OUT_PATHS"
64 for fuzz_main_file in $FUZZ_TEST_BINARIES_OUT_PATHS; do
65 FUZZ_TESTS=$($fuzz_main_file --list_fuzz_tests | cut -d ' ' -f 4)
66 cp -f ${fuzz_main_file} $OUT/
67 fuzz_basename=$(basename $fuzz_main_file)
68 chmod -x $OUT/$fuzz_basename
69 for fuzz_entrypoint in $FUZZ_TESTS; do
70 TARGET_FUZZER="${fuzz_basename}@$fuzz_entrypoint"
71 # Write executer script
72 cat << EOF > $OUT/$TARGET_FUZZER
73 #!/bin/sh
74 # LLVMFuzzerTestOneInput for fuzzer detection.
75 this_dir=\$(dirname "\$0")
76 export TEST_DATA_DIRS=\$this_dir/corpus
77 chmod +x \$this_dir/$fuzz_basename
78 \$this_dir/$fuzz_basename --fuzz=$fuzz_entrypoint -- \$@
79 chmod -x \$this_dir/$fuzz_basename
80 EOF
81 chmod +x $OUT/$TARGET_FUZZER
82 done
83 # Copy data.
84 cp fuzz_seed_corpus.zip $OUT/${fuzz_basename}_seed_corpus.zip
85 cp tests/fuzzer/fuzz.dict $OUT/${fuzz_basename}.dict
86 done