2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 /* Targeted fuzzer that targets specific codecs depending on two
23 * Get the very fresh clang, e.g. see http://libfuzzer.info#versions
24 * Get and build libFuzzer:
25 svn co http://llvm.org/svn/llvm-project/llvm/trunk/lib/Fuzzer
27 * build ffmpeg for fuzzing:
28 FLAGS="-fsanitize=address -fsanitize-coverage=trace-pc-guard,trace-cmp -g" CC="clang $FLAGS" CXX="clang++ $FLAGS" ./configure --disable-yasm
30 * build the fuzz target.
31 Choose the value of FFMPEG_CODEC (e.g. AV_CODEC_ID_DVD_SUBTITLE) and
32 choose one of FUZZ_FFMPEG_VIDEO, FUZZ_FFMPEG_AUDIO, FUZZ_FFMPEG_SUBTITLE.
33 clang -fsanitize=address -fsanitize-coverage=trace-pc-guard,trace-cmp tools/target_dec_fuzzer.c -o target_dec_fuzzer -I. -DFFMPEG_CODEC=AV_CODEC_ID_MPEG1VIDEO -DFUZZ_FFMPEG_VIDEO ../../libfuzzer/libFuzzer.a -Llibavcodec -Llibavdevice -Llibavfilter -Llibavformat -Llibavresample -Llibavutil -Llibpostproc -Llibswscale -Llibswresample -Wl,--as-needed -Wl,-z,noexecstack -Wl,--warn-common -Wl,-rpath-link=libpostproc:libswresample:libswscale:libavfilter:libavdevice:libavformat:libavcodec:libavutil:libavresample -lavdevice -lavfilter -lavformat -lavcodec -lswresample -lswscale -lavutil -ldl -lxcb -lxcb-shm -lxcb -lxcb-xfixes -lxcb -lxcb-shape -lxcb -lX11 -lasound -lm -lbz2 -lz -pthread
34 * create a corpus directory and put some samples there (empty dir is ok too):
35 mkdir CORPUS && cp some-files CORPUS
38 ./target_dec_fuzzer -max_len=100000 CORPUS
42 http://tutorial.libfuzzer.info
43 https://github.com/google/oss-fuzz
44 http://lcamtuf.coredump.cx/afl/
45 https://security.googleblog.com/2016/08/guided-in-process-fuzzing-of-chrome.html
48 #include "libavutil/avassert.h"
50 #include "libavcodec/avcodec.h"
51 #include "libavformat/avformat.h"
53 static void error(const char *err)
55 fprintf(stderr, "%s", err);
59 static AVCodec *c = NULL;
60 static AVCodec *AVCodecInitialize(enum AVCodecID codec_id)
64 av_log_set_level(AV_LOG_PANIC);
65 res = avcodec_find_decoder(codec_id);
67 error("Failed to find decoder");
71 #if defined(FUZZ_FFMPEG_VIDEO)
72 #define decode_handler avcodec_decode_video2
73 #elif defined(FUZZ_FFMPEG_AUDIO)
74 #define decode_handler avcodec_decode_audio4
75 #elif defined(FUZZ_FFMPEG_SUBTITLE)
76 static int subtitle_handler(AVCodecContext *avctx, void *frame,
77 int *got_sub_ptr, AVPacket *avpkt)
80 int ret = avcodec_decode_subtitle2(avctx, &sub, got_sub_ptr, avpkt);
81 if (ret >= 0 && *got_sub_ptr)
82 avsubtitle_free(&sub);
86 #define decode_handler subtitle_handler
88 #error "Specify encoder type" // To catch mistakes
91 // Class to handle buffer allocation and resize for each frame
92 typedef struct FuzzDataBuffer {
97 void FDBCreate(FuzzDataBuffer *FDB) {
99 FDB->data_ = av_malloc(FDB->size_);
101 error("Failed memory allocation");
104 void FDBDesroy(FuzzDataBuffer *FDB) { av_free(FDB->data_); }
106 void FDBRealloc(FuzzDataBuffer *FDB, size_t size) {
107 size_t needed = size + FF_INPUT_BUFFER_PADDING_SIZE;
108 av_assert0(needed > size);
109 if (needed > FDB->size_) {
112 FDB->data_ = av_malloc(FDB->size_);
114 error("Failed memory allocation");
118 void FDBPrepare(FuzzDataBuffer *FDB, AVPacket *dst, const uint8_t *data,
121 FDBRealloc(FDB, size);
122 memcpy(FDB->data_, data, size);
123 size_t padd = FDB->size_ - size;
124 if (padd > FF_INPUT_BUFFER_PADDING_SIZE)
125 padd = FF_INPUT_BUFFER_PADDING_SIZE;
126 memset(FDB->data_ + size, 0, padd);
128 dst->data = FDB->data_;
132 // Ensure we don't loop forever
133 const uint32_t maxiteration = 8096;
135 static const uint64_t FUZZ_TAG = 0x4741542D5A5A5546ULL;
137 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
138 const uint64_t fuzz_tag = FUZZ_TAG;
139 FuzzDataBuffer buffer;
140 const uint8_t *last = data;
141 const uint8_t *end = data + size;
145 c = AVCodecInitialize(FFMPEG_CODEC); // Done once.
147 AVCodecContext* ctx = avcodec_alloc_context3(NULL);
149 error("Failed memory allocation");
151 ctx->max_pixels = 4096 * 4096; //To reduce false positive OOM and hangs
153 int res = avcodec_open2(ctx, c, NULL);
159 AVFrame *frame = av_frame_alloc();
161 error("Failed memory allocation");
163 // Read very simple container
165 while (data < end && it < maxiteration) {
166 // Search for the TAG
167 while (data + sizeof(fuzz_tag) < end) {
168 if (data[0] == (fuzz_tag & 0xFF) && *(const uint64_t *)(data) == fuzz_tag)
172 if (data + sizeof(fuzz_tag) > end)
175 FDBPrepare(&buffer, &avpkt, last, data - last);
176 data += sizeof(fuzz_tag);
179 // Iterate through all data
180 while (avpkt.size > 0 && it++ < maxiteration) {
181 av_frame_unref(frame);
182 int ret = decode_handler(ctx, frame, &got_frame, &avpkt);
185 ctx->error_concealment = 0;
187 if (ret <= 0 || ret > avpkt.size)
194 av_init_packet(&avpkt);
200 decode_handler(ctx, frame, &got_frame, &avpkt);
201 } while (got_frame == 1 && it++ < maxiteration);
203 av_frame_free(&frame);
204 avcodec_free_context(&ctx);