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
20 #include "libavutil/avassert.h"
21 #include "libavutil/avstring.h"
23 #include "libavcodec/avcodec.h"
24 #include "libavcodec/bytestream.h"
25 #include "libavformat/avformat.h"
28 typedef struct IOContext {
35 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
37 int64_t interrupt_counter;
38 static int interrupt_cb(void *ctx)
41 return interrupt_counter < 0;
44 static void error(const char *err)
46 fprintf(stderr, "%s", err);
50 static int io_read(void *opaque, uint8_t *buf, int buf_size)
52 IOContext *c = opaque;
53 int size = FFMIN(buf_size, c->fuzz_size);
56 c->filesize = FFMIN(c->pos, c->filesize);
59 if (c->pos > INT64_MAX - size)
62 memcpy(buf, c->fuzz, size);
66 c->filesize = FFMAX(c->filesize, c->pos);
71 static int64_t io_seek(void *opaque, int64_t offset, int whence)
73 IOContext *c = opaque;
75 if (whence == SEEK_CUR) {
76 if (offset > INT64_MAX - c->pos)
79 } else if (whence == SEEK_END) {
80 if (offset > INT64_MAX - c->filesize)
82 offset += c->filesize;
83 } else if (whence == AVSEEK_SIZE) {
86 if (offset < 0 || offset > c->filesize)
89 c->fuzz += offset - c->pos;
90 c->fuzz_size -= offset - c->pos;
96 // Ensure we don't loop forever
97 const uint32_t maxiteration = 8096;
98 const int maxblocks= 50000;
100 static const uint64_t FUZZ_TAG = 0x4741542D5A5A5546ULL;
102 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
103 const uint64_t fuzz_tag = FUZZ_TAG;
105 AVFormatContext *avfmt = avformat_alloc_context();
107 char filename[1025] = {0};
108 AVIOContext *fuzzed_pb = NULL;
110 int io_buffer_size = 32768;
111 int64_t filesize = size;
116 AVInputFormat *fmt = NULL;
117 #ifdef FFMPEG_DEMUXER
118 #define DEMUXER_SYMBOL0(DEMUXER) ff_##DEMUXER##_demuxer
119 #define DEMUXER_SYMBOL(DEMUXER) DEMUXER_SYMBOL0(DEMUXER)
120 extern AVInputFormat DEMUXER_SYMBOL(FFMPEG_DEMUXER);
121 fmt = &DEMUXER_SYMBOL(FFMPEG_DEMUXER);
125 av_log_set_level(AV_LOG_PANIC);
130 error("Failed avformat_alloc_context()");
134 io_buffer_size = size;
135 } else if (size > 2048) {
140 memcpy (filename, data + size - 1024, 1024);
141 bytestream2_init(&gbc, data + size - 2048, 1024);
144 io_buffer_size = bytestream2_get_le32(&gbc) & 0xFFFFFFF;
145 flags = bytestream2_get_byte(&gbc);
146 seekable = flags & 1;
147 filesize = bytestream2_get_le64(&gbc) & 0x7FFFFFFFFFFFFFFF;
149 if ((flags & 2) && strlen(filename) < sizeof(filename) / 2) {
150 const AVInputFormat *avif = NULL;
151 void *avif_iter = NULL;
153 while ((avif = av_demuxer_iterate(&avif_iter))) {
154 if (avif->extensions)
157 avif_count = bytestream2_get_le32(&gbc) % avif_count;
160 while ((avif = av_demuxer_iterate(&avif_iter))) {
161 if (avif->extensions)
165 av_strlcpy(extension, avif->extensions, sizeof(extension));
166 if (strchr(extension, ','))
167 *strchr(extension, ',') = 0;
168 av_strlcatf(filename, sizeof(filename), ".%s", extension);
171 interrupt_counter = bytestream2_get_le32(&gbc);
172 avfmt->interrupt_callback.callback = interrupt_cb;
175 // HLS uses a loop with sleep, we thus must breakout or we timeout
176 if (fmt && !strcmp(fmt->name, "hls"))
177 interrupt_counter &= 31;
179 if (!io_buffer_size || size / io_buffer_size > maxblocks)
180 io_buffer_size = size;
182 pkt = av_packet_alloc();
184 error("Failed to allocate pkt");
186 io_buffer = av_malloc(io_buffer_size);
188 error("Failed to allocate io_buffer");
190 opaque.filesize = filesize;
193 opaque.fuzz_size= size;
194 fuzzed_pb = avio_alloc_context(io_buffer, io_buffer_size, 0, &opaque,
195 io_read, NULL, seekable ? io_seek : NULL);
197 error("avio_alloc_context failed");
199 avfmt->pb = fuzzed_pb;
201 ret = avformat_open_input(&avfmt, filename, fmt, NULL);
206 ret = avformat_find_stream_info(avfmt, NULL);
210 for(it = 0; it < maxiteration; it++) {
211 ret = av_read_frame(avfmt, pkt);
214 av_packet_unref(pkt);
218 av_packet_free(&pkt);
219 av_freep(&fuzzed_pb->buffer);
220 avio_context_free(&fuzzed_pb);
221 avformat_close_input(&avfmt);