|
| 1 | +#!/bin/bash -eux |
| 2 | +# Copyright 2025 Google LLC. |
| 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 | +# Prepare wrappers to make recompilation faster. |
| 19 | +if [ ! -f /usr/bin/bash.real ]; then |
| 20 | + # Only run this once. |
| 21 | + python /usr/local/bin/make_build_replayable.py |
| 22 | +fi |
| 23 | + |
| 24 | + |
| 25 | +MAGICK_COMPILER=$CXX |
| 26 | +MAGICK_COMPILER_FLAGS=$CXXFLAGS |
| 27 | +MAGICK_INCLUDE="$WORK/include/ImageMagick-7" |
| 28 | +MAGICK_SRC="$SRC/imagemagick/oss-fuzz" |
| 29 | +MAGICK_LIBS_NO_FUZZ="$WORK/lib/libMagick++-7.Q16HDRI.a $WORK/lib/libMagickWand-7.Q16HDRI.a $WORK/lib/libMagickCore-7.Q16HDRI.a $WORK/lib/libpng.a $WORK/lib/libtiff.a $WORK/lib/libheif.a $WORK/lib/libde265.a $WORK/lib/libopenjp2.a $WORK/lib/libwebp.a $WORK/lib/libwebpmux.a $WORK/lib/libwebpdemux.a $WORK/lib/libsharpyuv.a $WORK/lib/libhwy.a $WORK/lib/libbrotlicommon.a $WORK/lib/libbrotlidec.a $WORK/lib/libbrotlienc.a $WORK/lib/libjxl_threads.a $WORK/lib/libjxl_cms.a $WORK/lib/libjxl.a $WORK/lib/libturbojpeg.a $WORK/lib/libjpeg.a $WORK/lib/libfreetype.a $WORK/lib/libraw.a $WORK/lib/liblzma.a $WORK/lib/liblcms2.a $WORK/lib/libdeflate.a $WORK/lib/libz.a" |
| 30 | +MAGICK_LIBS="$LIB_FUZZING_ENGINE $MAGICK_LIBS_NO_FUZZ" |
| 31 | +MAGICK_OUTPUT=$OUT |
| 32 | +MAGICK_FAST_BUILD=0 |
| 33 | + |
| 34 | +. $MAGICK_SRC/build_dependencies.sh |
| 35 | +. $MAGICK_SRC/build_imagemagick.sh |
| 36 | + |
| 37 | +# Move on the building the fuzzers |
| 38 | +MAGICK_COMPILER_FLAGS="$MAGICK_COMPILER_FLAGS -fuse-ld=lld -DMAGICKCORE_HDRI_ENABLE=1 -DMAGICKCORE_QUANTUM_DEPTH=16" |
| 39 | + |
| 40 | +$MAGICK_COMPILER $MAGICK_COMPILER_FLAGS -std=c++11 -I$MAGICK_INCLUDE "$MAGICK_SRC/encoder_list.cc" \ |
| 41 | + -o "$MAGICK_SRC/encoder_list" $MAGICK_LIBS_NO_FUZZ |
| 42 | + |
| 43 | + |
| 44 | +# Control the target fuzzer from the command line. Hardcoded below commented |
| 45 | +# out is for testing purposes, and to illustrate the logic behind it. |
| 46 | +# TARGET_FUZZER="encoder_sgi_fuzzer" |
| 47 | +TARGET_FUZZER=$1 |
| 48 | + |
| 49 | +for f in $MAGICK_SRC/*_fuzzer.cc; do |
| 50 | + fuzzer=$(basename "$f" _fuzzer.cc) |
| 51 | + out_fuzzname=$(basename "$f" .cc) |
| 52 | + echo "Real fuzz name: ${out_fuzzname}" |
| 53 | + # encoder_fuzzer is special |
| 54 | + if [ "$fuzzer" == "encoder" ]; then |
| 55 | + continue |
| 56 | + fi |
| 57 | + if [ "$out_fuzzname" != "$TARGET_FUZZER" ]; then |
| 58 | + continue |
| 59 | + fi |
| 60 | + $MAGICK_COMPILER $MAGICK_COMPILER_FLAGS -std=c++11 -I$MAGICK_INCLUDE \ |
| 61 | + "$f" -o "$MAGICK_OUTPUT/${fuzzer}_fuzzer" $MAGICK_LIBS & |
| 62 | + echo -e "[libfuzzer]\nclose_fd_mask=3" > "$MAGICK_OUTPUT/${fuzzer}_fuzzer.options" |
| 63 | +done |
| 64 | + |
| 65 | +for item in $("$MAGICK_SRC/encoder_list"); do |
| 66 | + info=${item:1} |
| 67 | + encoder=${info%:*} |
| 68 | + initializer=${info##*:} |
| 69 | + encoder_flags="-DFUZZ_IMAGEMAGICK_ENCODER=$encoder" |
| 70 | + out_fuzzname="encoder_${encoder,,}_fuzzer" |
| 71 | + if [ "$out_fuzzname" != "$TARGET_FUZZER" ]; then |
| 72 | + continue |
| 73 | + fi |
| 74 | + |
| 75 | + if [ "$initializer" != "" ]; then |
| 76 | + encoder_flags="$encoder_flags -DFUZZ_IMAGEMAGICK_ENCODER_INITIALIZER=$initializer" |
| 77 | + fi |
| 78 | + |
| 79 | + if [ "${item:0:1}" == "+" ]; then |
| 80 | + encoder_flags="$encoder_flags -DFUZZ_IMAGEMAGICK_ENCODER_WRITE=1" |
| 81 | + fi |
| 82 | + |
| 83 | + $MAGICK_COMPILER $MAGICK_COMPILER_FLAGS -std=c++11 -I$MAGICK_INCLUDE \ |
| 84 | + "$MAGICK_SRC/encoder_fuzzer.cc" -o "$MAGICK_OUTPUT/encoder_${encoder,,}_fuzzer" \ |
| 85 | + $encoder_flags $MAGICK_LIBS & |
| 86 | + |
| 87 | + echo -e "[libfuzzer]\nclose_fd_mask=3" > "$MAGICK_OUTPUT/encoder_${encoder,,}_fuzzer.options" |
| 88 | + |
| 89 | + if [ -f "$MAGICK_SRC/dictionaries/${encoder,,}.dict" ]; then |
| 90 | + cp "$MAGICK_SRC/dictionaries/${encoder,,}.dict" "$MAGICK_OUTPUT/encoder_${encoder,,}_fuzzer.dict" |
| 91 | + fi |
| 92 | + |
| 93 | + if [ $MAGICK_FAST_BUILD -eq 1 ]; then |
| 94 | + break |
| 95 | + fi |
| 96 | +done |
| 97 | + |
| 98 | +wait |
0 commit comments