1#!/bin/bash -eux
2# Copyright 2016 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# Disable UBSan vptr since several targets built with -fno-rtti.
19export CFLAGS="$CFLAGS -fno-sanitize=vptr"
20export CXXFLAGS="$CXXFLAGS -fno-sanitize=vptr"
21
22# Build dependencies.
23export FFMPEG_DEPS_PATH=$SRC/ffmpeg_deps
24mkdir -p $FFMPEG_DEPS_PATH
25
26export PATH="$FFMPEG_DEPS_PATH/bin:$PATH"
27export LD_LIBRARY_PATH="$FFMPEG_DEPS_PATH/lib"
28
29cd $SRC
30bzip2 -f -d alsa-lib-*
31tar xf alsa-lib-*
32cd alsa-lib-*
33./configure --prefix="$FFMPEG_DEPS_PATH" --enable-static --disable-shared
34make clean
35make -j$(nproc) all
36make install
37
38cd $SRC/fdk-aac
39autoreconf -fiv
40CXXFLAGS="$CXXFLAGS -fno-sanitize=shift-base,signed-integer-overflow" \
41./configure --prefix="$FFMPEG_DEPS_PATH" --disable-shared
42make clean
43make -j$(nproc) all
44make install
45
46cd $SRC/libXext
47./autogen.sh
48./configure --prefix="$FFMPEG_DEPS_PATH" --enable-static
49make clean
50make -j$(nproc)
51make install
52
53cd $SRC/libXfixes
54./autogen.sh
55./configure --prefix="$FFMPEG_DEPS_PATH" --enable-static
56make clean
57make -j$(nproc)
58make install
59
60cd $SRC/libva
61./autogen.sh
62./configure --prefix="$FFMPEG_DEPS_PATH" --enable-static --disable-shared
63make clean
64make -j$(nproc) all
65make install
66
67cd $SRC/libvdpau
68./autogen.sh
69./configure --prefix="$FFMPEG_DEPS_PATH" --enable-static --disable-shared
70make clean
71make -j$(nproc) all
72make install
73
74cd $SRC/libvpx
75LDFLAGS="$CXXFLAGS" ./configure --prefix="$FFMPEG_DEPS_PATH" \
76    --disable-examples --disable-unit-tests \
77    --size-limit=12288x12288 \
78    --extra-cflags="-DVPX_MAX_ALLOCABLE_MEMORY=1073741824"
79make clean
80make -j$(nproc) all
81make install
82
83cd $SRC/ogg
84./autogen.sh
85./configure --prefix="$FFMPEG_DEPS_PATH" --enable-static --disable-crc
86make clean
87make -j$(nproc)
88make install
89
90cd $SRC/opus
91./autogen.sh
92./configure --prefix="$FFMPEG_DEPS_PATH" --enable-static
93make clean
94make -j$(nproc) all
95make install
96
97cd $SRC/theora
98# theora requires ogg, need to pass its location to the "configure" script.
99CFLAGS="$CFLAGS -fPIC" LDFLAGS="-L$FFMPEG_DEPS_PATH/lib/" \
100    CPPFLAGS="$CXXFLAGS -I$FFMPEG_DEPS_PATH/include/" \
101    LD_LIBRARY_PATH="$FFMPEG_DEPS_PATH/lib/" \
102    ./autogen.sh
103./configure --with-ogg="$FFMPEG_DEPS_PATH" --prefix="$FFMPEG_DEPS_PATH" \
104    --enable-static --disable-examples
105make clean
106make -j$(nproc)
107make install
108
109cd $SRC/vorbis
110./autogen.sh
111./configure --prefix="$FFMPEG_DEPS_PATH" --enable-static
112make clean
113make -j$(nproc)
114make install
115
116# Remove shared libraries to avoid accidental linking against them.
117rm $FFMPEG_DEPS_PATH/lib/*.so
118rm $FFMPEG_DEPS_PATH/lib/*.so.*
119
120# Build ffmpeg.
121cd $SRC/ffmpeg
122PKG_CONFIG_PATH="$FFMPEG_DEPS_PATH/lib/pkgconfig" ./configure \
123    --cc=$CC --cxx=$CXX --ld="$CXX $CXXFLAGS -std=c++11" \
124    --extra-cflags="-I$FFMPEG_DEPS_PATH/include" \
125    --extra-ldflags="-L$FFMPEG_DEPS_PATH/lib" \
126    --prefix="$FFMPEG_DEPS_PATH" \
127    --pkg-config-flags="--static" \
128    --enable-ossfuzz \
129    --libfuzzer=$LIB_FUZZING_ENGINE \
130    --optflags=-O1 \
131    --enable-gpl \
132    --enable-libass \
133    --enable-libfdk-aac \
134    --enable-libfreetype \
135    --enable-libopus \
136    --enable-libtheora \
137    --enable-libvorbis \
138    --enable-libvpx \
139    --enable-nonfree \
140    --disable-muxers \
141    --disable-protocols \
142    --disable-demuxer=rtp,rtsp,sdp \
143    --disable-devices \
144    --disable-shared
145make clean
146make -j$(nproc) install
147
148# Download test sampes, will be used as seed corpus.
149# DISABLED.
150# TODO: implement a better way to maintain a minimized seed corpora
151# for all targets. As of 2017-05-04 now the combined size of corpora
152# is too big for ClusterFuzz (over 10Gb compressed data).
153export TEST_SAMPLES_PATH=$SRC/ffmpeg/fate-suite/
154make fate-rsync SAMPLES=$TEST_SAMPLES_PATH
155
156# Build the fuzzers.
157cd $SRC/ffmpeg
158
159FUZZ_TARGET_SOURCE=$SRC/ffmpeg/tools/target_dec_fuzzer.c
160
161export TEMP_VAR_CODEC="AV_CODEC_ID_H264"
162export TEMP_VAR_CODEC_TYPE="VIDEO"
163
164CONDITIONALS=`grep 'BSF 1$' config.h | sed 's/#define CONFIG_\(.*\)_BSF 1/\1/'`
165for c in $CONDITIONALS ; do
166  fuzzer_name=ffmpeg_BSF_${c}_fuzzer
167  symbol=`echo $c | sed "s/.*/\L\0/"`
168  echo -en "[libfuzzer]\nmax_len = 1000000\n" > $OUT/${fuzzer_name}.options
169  make tools/target_bsf_${symbol}_fuzzer
170  mv tools/target_bsf_${symbol}_fuzzer $OUT/${fuzzer_name}
171done
172
173# Build fuzzers for decoders.
174CONDITIONALS=`grep 'DECODER 1$' config.h | sed 's/#define CONFIG_\(.*\)_DECODER 1/\1/'`
175for c in $CONDITIONALS ; do
176  fuzzer_name=ffmpeg_AV_CODEC_ID_${c}_fuzzer
177  symbol=`echo $c | sed "s/.*/\L\0/"`
178  echo -en "[libfuzzer]\nmax_len = 1000000\n" > $OUT/${fuzzer_name}.options
179  make tools/target_dec_${symbol}_fuzzer
180  mv tools/target_dec_${symbol}_fuzzer $OUT/${fuzzer_name}
181done
182
183# Build fuzzer for demuxer
184fuzzer_name=ffmpeg_DEMUXER_fuzzer
185echo -en "[libfuzzer]\nmax_len = 1000000\n" > $OUT/${fuzzer_name}.options
186make tools/target_dem_fuzzer
187mv tools/target_dem_fuzzer $OUT/${fuzzer_name}
188
189# We do not need raw reference files for the muxer
190rm `find fate-suite -name '*.s16'`
191rm `find fate-suite -name '*.dec'`
192rm `find fate-suite -name '*.pcm'`
193
194zip -r $OUT/${fuzzer_name}_seed_corpus.zip fate-suite
195
196# Build fuzzer for demuxer fed at IO level
197fuzzer_name=ffmpeg_IO_DEMUXER_fuzzer
198make tools/target_io_dem_fuzzer
199mv tools/target_io_dem_fuzzer $OUT/${fuzzer_name}
200
201#Build fuzzers for individual demuxers
202PKG_CONFIG_PATH="$FFMPEG_DEPS_PATH/lib/pkgconfig" ./configure \
203    --cc=$CC --cxx=$CXX --ld="$CXX $CXXFLAGS -std=c++11" \
204    --extra-cflags="-I$FFMPEG_DEPS_PATH/include" \
205    --extra-ldflags="-L$FFMPEG_DEPS_PATH/lib" \
206    --prefix="$FFMPEG_DEPS_PATH" \
207    --pkg-config-flags="--static" \
208    --enable-ossfuzz \
209    --libfuzzer=$LIB_FUZZING_ENGINE \
210    --optflags=-O1 \
211    --enable-gpl \
212    --disable-muxers \
213    --disable-protocols \
214    --disable-devices \
215    --disable-shared \
216    --disable-encoders \
217    --disable-filters \
218    --disable-muxers  \
219    --disable-parsers  \
220    --disable-decoders  \
221    --disable-hwaccels  \
222    --disable-bsfs  \
223    --disable-vaapi  \
224    --disable-vdpau    \
225    --disable-crystalhd  \
226    --disable-v4l2_m2m  \
227    --disable-cuda_llvm  \
228    --enable-demuxers \
229    --disable-demuxer=rtp,rtsp,sdp \
230
231CONDITIONALS=`grep 'DEMUXER 1$' config.h | sed 's/#define CONFIG_\(.*\)_DEMUXER 1/\1/'`
232for c in $CONDITIONALS ; do
233  fuzzer_name=ffmpeg_dem_${c}_fuzzer
234  symbol=`echo $c | sed "s/.*/\L\0/"`
235  make tools/target_dem_${symbol}_fuzzer
236  mv tools/target_dem_${symbol}_fuzzer $OUT/${fuzzer_name}
237done
238
239# Find relevant corpus in test samples and archive them for every fuzzer.
240#cd $SRC
241#python group_seed_corpus.py $TEST_SAMPLES_PATH $OUT/
242