]> git.sesse.net Git - ffmpeg/blob - tools/target_dem_fuzzer.c
avcodec/av1dec: parse dimensions from the sequence header in extradata
[ffmpeg] / tools / target_dem_fuzzer.c
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20 #include "libavutil/avassert.h"
21 #include "libavutil/avstring.h"
22
23 #include "libavcodec/avcodec.h"
24 #include "libavcodec/bytestream.h"
25 #include "libavformat/avformat.h"
26
27
28 typedef struct IOContext {
29     int64_t pos;
30     int64_t filesize;
31     uint8_t *fuzz;
32     int fuzz_size;
33 } IOContext;
34
35 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
36
37 static void error(const char *err)
38 {
39     fprintf(stderr, "%s", err);
40     exit(1);
41 }
42
43 static int io_read(void *opaque, uint8_t *buf, int buf_size)
44 {
45     IOContext *c = opaque;
46     int size = FFMIN(buf_size, c->fuzz_size);
47
48     if (!c->fuzz_size) {
49         c->filesize = FFMIN(c->pos, c->filesize);
50         return AVERROR_EOF;
51     }
52
53     memcpy(buf, c->fuzz, size);
54     c->fuzz      += size;
55     c->fuzz_size -= size;
56     c->pos       += size;
57     c->filesize   = FFMAX(c->filesize, c->pos);
58
59     return size;
60 }
61
62 static int64_t io_seek(void *opaque, int64_t offset, int whence)
63 {
64     IOContext *c = opaque;
65
66     if (whence == SEEK_CUR) {
67         if (offset > INT64_MAX - c->pos)
68             return -1;
69         offset += c->pos;
70     } else if (whence == SEEK_END) {
71         if (offset > INT64_MAX - c->filesize)
72             return -1;
73         offset += c->filesize;
74     } else if (whence == AVSEEK_SIZE) {
75         return c->filesize;
76     }
77     if (offset < 0 || offset > c->filesize)
78         return -1;
79     if (IO_FLAT) {
80         c->fuzz      += offset - c->pos;
81         c->fuzz_size -= offset - c->pos;
82     }
83     c->pos = offset;
84     return 0;
85 }
86
87 // Ensure we don't loop forever
88 const uint32_t maxiteration = 8096;
89
90 static const uint64_t FUZZ_TAG = 0x4741542D5A5A5546ULL;
91
92 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
93     const uint64_t fuzz_tag = FUZZ_TAG;
94     uint32_t it = 0;
95     AVFormatContext *avfmt = avformat_alloc_context();
96     AVPacket pkt;
97     char filename[1025] = {0};
98     AVIOContext *fuzzed_pb = NULL;
99     uint8_t *io_buffer;
100     int io_buffer_size = 32768;
101     int64_t filesize   = size;
102     IOContext opaque;
103     static int c;
104     int seekable = 0;
105     int ret;
106
107     if (!c) {
108         av_register_all();
109         avcodec_register_all();
110         av_log_set_level(AV_LOG_PANIC);
111         c=1;
112     }
113
114     if (!avfmt)
115         error("Failed avformat_alloc_context()");
116
117     if (IO_FLAT) {
118         seekable = 1;
119         io_buffer_size = size;
120     } else if (size > 2048) {
121         int flags;
122         char extension[64];
123
124         GetByteContext gbc;
125         memcpy (filename, data + size - 1024, 1024);
126         bytestream2_init(&gbc, data + size - 2048, 1024);
127         size -= 2048;
128
129         io_buffer_size = bytestream2_get_le32(&gbc) & 0xFFFFFFF;
130         flags          = bytestream2_get_byte(&gbc);
131         seekable       = flags & 1;
132         filesize       = bytestream2_get_le64(&gbc) & 0x7FFFFFFFFFFFFFFF;
133
134         if ((flags & 2) && strlen(filename) < sizeof(filename) / 2) {
135             AVInputFormat *avif = NULL;
136             int avif_count = 0;
137             while ((avif = av_iformat_next(avif))) {
138                 if (avif->extensions)
139                     avif_count ++;
140             }
141             avif_count =  bytestream2_get_le32(&gbc) % avif_count;
142
143             while ((avif = av_iformat_next(avif))) {
144                 if (avif->extensions)
145                     if (!avif_count--)
146                         break;
147             }
148             av_strlcpy(extension, avif->extensions, sizeof(extension));
149             if (strchr(extension, ','))
150                 *strchr(extension, ',') = 0;
151             av_strlcatf(filename, sizeof(filename), ".%s", extension);
152         }
153     }
154     io_buffer = av_malloc(io_buffer_size);
155     if (!io_buffer)
156         error("Failed to allocate io_buffer");
157
158     opaque.filesize = filesize;
159     opaque.pos      = 0;
160     opaque.fuzz     = data;
161     opaque.fuzz_size= size;
162     fuzzed_pb = avio_alloc_context(io_buffer, io_buffer_size, 0, &opaque,
163                                    io_read, NULL, seekable ? io_seek : NULL);
164     if (!fuzzed_pb)
165         error("avio_alloc_context failed");
166
167     avfmt->pb = fuzzed_pb;
168
169     ret = avformat_open_input(&avfmt, filename, NULL, NULL);
170     if (ret < 0) {
171         av_freep(&fuzzed_pb->buffer);
172         av_freep(&fuzzed_pb);
173         avformat_free_context(avfmt);
174         return 0;
175     }
176
177     ret = avformat_find_stream_info(avfmt, NULL);
178
179     av_init_packet(&pkt);
180
181     //TODO, test seeking
182
183     for(it = 0; it < maxiteration; it++) {
184         ret = av_read_frame(avfmt, &pkt);
185         if (ret < 0)
186             break;
187         av_packet_unref(&pkt);
188     }
189 end:
190     av_freep(&fuzzed_pb->buffer);
191     av_freep(&fuzzed_pb);
192     avformat_close_input(&avfmt);
193
194     return 0;
195 }