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 static void error(const char *err)
39 fprintf(stderr, "%s", err);
43 static int io_read(void *opaque, uint8_t *buf, int buf_size)
45 IOContext *c = opaque;
46 int size = FFMIN(buf_size, c->fuzz_size);
49 c->filesize = FFMIN(c->pos, c->filesize);
52 if (c->pos > INT64_MAX - size)
55 memcpy(buf, c->fuzz, size);
59 c->filesize = FFMAX(c->filesize, c->pos);
64 static int64_t io_seek(void *opaque, int64_t offset, int whence)
66 IOContext *c = opaque;
68 if (whence == SEEK_CUR) {
69 if (offset > INT64_MAX - c->pos)
72 } else if (whence == SEEK_END) {
73 if (offset > INT64_MAX - c->filesize)
75 offset += c->filesize;
76 } else if (whence == AVSEEK_SIZE) {
79 if (offset < 0 || offset > c->filesize)
82 c->fuzz += offset - c->pos;
83 c->fuzz_size -= offset - c->pos;
89 // Ensure we don't loop forever
90 const uint32_t maxiteration = 8096;
91 const int maxblocks= 100000;
93 static const uint64_t FUZZ_TAG = 0x4741542D5A5A5546ULL;
95 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
96 const uint64_t fuzz_tag = FUZZ_TAG;
98 AVFormatContext *avfmt = avformat_alloc_context();
100 char filename[1025] = {0};
101 AVIOContext *fuzzed_pb = NULL;
103 int io_buffer_size = 32768;
104 int64_t filesize = size;
109 AVInputFormat *fmt = NULL;
110 #ifdef FFMPEG_DEMUXER
111 #define DEMUXER_SYMBOL0(DEMUXER) ff_##DEMUXER##_demuxer
112 #define DEMUXER_SYMBOL(DEMUXER) DEMUXER_SYMBOL0(DEMUXER)
113 extern AVInputFormat DEMUXER_SYMBOL(FFMPEG_DEMUXER);
114 fmt = &DEMUXER_SYMBOL(FFMPEG_DEMUXER);
118 av_log_set_level(AV_LOG_PANIC);
123 error("Failed avformat_alloc_context()");
127 io_buffer_size = size;
128 } else if (size > 2048) {
133 memcpy (filename, data + size - 1024, 1024);
134 bytestream2_init(&gbc, data + size - 2048, 1024);
137 io_buffer_size = bytestream2_get_le32(&gbc) & 0xFFFFFFF;
138 flags = bytestream2_get_byte(&gbc);
139 seekable = flags & 1;
140 filesize = bytestream2_get_le64(&gbc) & 0x7FFFFFFFFFFFFFFF;
142 if ((flags & 2) && strlen(filename) < sizeof(filename) / 2) {
143 const AVInputFormat *avif = NULL;
144 void *avif_iter = NULL;
146 while ((avif = av_demuxer_iterate(&avif_iter))) {
147 if (avif->extensions)
150 avif_count = bytestream2_get_le32(&gbc) % avif_count;
153 while ((avif = av_demuxer_iterate(&avif_iter))) {
154 if (avif->extensions)
158 av_strlcpy(extension, avif->extensions, sizeof(extension));
159 if (strchr(extension, ','))
160 *strchr(extension, ',') = 0;
161 av_strlcatf(filename, sizeof(filename), ".%s", extension);
165 if (!io_buffer_size || size / io_buffer_size > maxblocks)
166 io_buffer_size = size;
168 io_buffer = av_malloc(io_buffer_size);
170 error("Failed to allocate io_buffer");
172 opaque.filesize = filesize;
175 opaque.fuzz_size= size;
176 fuzzed_pb = avio_alloc_context(io_buffer, io_buffer_size, 0, &opaque,
177 io_read, NULL, seekable ? io_seek : NULL);
179 error("avio_alloc_context failed");
181 avfmt->pb = fuzzed_pb;
183 ret = avformat_open_input(&avfmt, filename, fmt, NULL);
185 av_freep(&fuzzed_pb->buffer);
186 av_freep(&fuzzed_pb);
187 avformat_free_context(avfmt);
191 ret = avformat_find_stream_info(avfmt, NULL);
193 av_init_packet(&pkt);
197 for(it = 0; it < maxiteration; it++) {
198 ret = av_read_frame(avfmt, &pkt);
201 av_packet_unref(&pkt);
204 av_freep(&fuzzed_pb->buffer);
205 avio_context_free(&fuzzed_pb);
206 avformat_close_input(&avfmt);