FFmpeg
Loading...
Searching...
No Matches
target_dem_fuzzer.c
Go to the documentation of this file.
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#include "libavutil/mem.h"
23
24#include "libavcodec/avcodec.h"
27#include "libavformat/demux.h"
28
29typedef struct IOContext {
32 const uint8_t *fuzz;
34} IOContext;
35
36int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
37
39static int interrupt_cb(void *ctx)
40{
42 return interrupt_counter < 0;
43}
44
45static void error(const char *err)
46{
47 fprintf(stderr, "%s", err);
48 exit(1);
49}
50
51static int io_read(void *opaque, uint8_t *buf, int buf_size)
52{
53 IOContext *c = opaque;
54 int size = FFMIN(buf_size, c->fuzz_size);
55
56 if (!c->fuzz_size) {
57 c->filesize = FFMIN(c->pos, c->filesize);
58 return AVERROR_EOF;
59 }
60 if (c->pos > INT64_MAX - size)
61 return AVERROR(EIO);
62
63 memcpy(buf, c->fuzz, size);
64 c->fuzz += size;
65 c->fuzz_size -= size;
66 c->pos += size;
67 c->filesize = FFMAX(c->filesize, c->pos);
68
69 return size;
70}
71
72static int64_t io_seek(void *opaque, int64_t offset, int whence)
73{
74 IOContext *c = opaque;
75
76 if (whence == SEEK_CUR) {
77 if (offset > INT64_MAX - c->pos)
78 return -1;
79 offset += c->pos;
80 } else if (whence == SEEK_END) {
81 if (offset > INT64_MAX - c->filesize)
82 return -1;
83 offset += c->filesize;
84 } else if (whence == AVSEEK_SIZE) {
85 return c->filesize;
86 }
87 if (offset < 0 || offset > c->filesize)
88 return -1;
89 if (IO_FLAT) {
90 c->fuzz += offset - c->pos;
91 c->fuzz_size -= offset - c->pos;
92 }
93 c->pos = offset;
94 return 0;
95}
96
97// Ensure we don't loop forever
98const uint32_t maxiteration = 8096;
99const int maxblocks= 50000;
100
101int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
102 uint32_t it = 0;
104 AVPacket *pkt;
105 char filename[1025] = {0};
106 AVIOContext *fuzzed_pb = NULL;
107 uint8_t *io_buffer;
108 int io_buffer_size = 32768;
110 IOContext opaque;
111 static int c;
112 int seekable = 0;
113 int ret;
114 const AVInputFormat *fmt = NULL;
115#ifdef FFMPEG_DEMUXER
116#define DEMUXER_SYMBOL0(DEMUXER) ff_##DEMUXER##_demuxer
117#define DEMUXER_SYMBOL(DEMUXER) DEMUXER_SYMBOL0(DEMUXER)
118 extern const FFInputFormat DEMUXER_SYMBOL(FFMPEG_DEMUXER);
119 fmt = &DEMUXER_SYMBOL(FFMPEG_DEMUXER).p;
120#endif
121
122 if (!c) {
124 c=1;
125 }
126
127 if (!avfmt)
128 error("Failed avformat_alloc_context()");
129
130 if (IO_FLAT) {
131 seekable = 1;
132 io_buffer_size = size;
133 } else if (size > 2048) {
134 int flags;
135 char extension[64];
136
137 GetByteContext gbc;
138 memcpy (filename, data + size - 1024, 1024);
139 bytestream2_init(&gbc, data + size - 2048, 1024);
140 size -= 2048;
141
142 io_buffer_size = bytestream2_get_le32(&gbc) & 0xFFFFFFF;
143 flags = bytestream2_get_byte(&gbc);
144 seekable = flags & 1;
145 filesize = bytestream2_get_le64(&gbc) & 0x7FFFFFFFFFFFFFFF;
146
147 if ((flags & 2) && strlen(filename) < sizeof(filename) / 2) {
148 const AVInputFormat *avif = NULL;
149 void *avif_iter = NULL;
150 int avif_count = 0;
151 while ((avif = av_demuxer_iterate(&avif_iter))) {
152 if (avif->extensions)
153 avif_count ++;
154 }
155 avif_count = bytestream2_get_le32(&gbc) % avif_count;
156
157 avif_iter = NULL;
158 while ((avif = av_demuxer_iterate(&avif_iter))) {
159 if (avif->extensions)
160 if (!avif_count--)
161 break;
162 }
163 av_strlcpy(extension, avif->extensions, sizeof(extension));
164 if (strchr(extension, ','))
165 *strchr(extension, ',') = 0;
166 av_strlcatf(filename, sizeof(filename), ".%s", extension);
167 }
168
169 interrupt_counter = bytestream2_get_le32(&gbc);
171 }
172
173 // HLS uses a loop with sleep, we thus must breakout or we timeout
174 if (fmt && !strcmp(fmt->name, "hls"))
175 interrupt_counter &= 31;
176
177 if (!io_buffer_size || size / io_buffer_size > maxblocks)
178 io_buffer_size = size;
179
181 if (!pkt)
182 error("Failed to allocate pkt");
183
184 io_buffer = av_malloc(io_buffer_size);
185 if (!io_buffer) {
188 return 0;
189 }
190
191 opaque.filesize = filesize;
192 opaque.pos = 0;
193 opaque.fuzz = data;
194 opaque.fuzz_size= size;
195 fuzzed_pb = avio_alloc_context(io_buffer, io_buffer_size, 0, &opaque,
196 io_read, NULL, seekable ? io_seek : NULL);
197 if (!fuzzed_pb)
198 error("avio_alloc_context failed");
199
200 avfmt->pb = fuzzed_pb;
201
202 ret = avformat_open_input(&avfmt, filename, fmt, NULL);
203 if (ret < 0) {
204 goto fail;
205 }
206
207 ret = avformat_find_stream_info(avfmt, NULL);
208
209 //TODO, test seeking
210
211 for(it = 0; it < maxiteration; it++) {
212 ret = av_read_frame(avfmt, pkt);
213 if (ret < 0)
214 break;
216 }
217
218fail:
220 av_freep(&fuzzed_pb->buffer);
221 avio_context_free(&fuzzed_pb);
222 avformat_close_input(&avfmt);
223
224 return 0;
225
226}
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
Libavcodec external API header.
Main libavformat public API header.
#define AVSEEK_SIZE
Passing this as the "whence" parameter to a seek function causes it to return the filesize without se...
Definition avio.h:468
AVIOContext * avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, const uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Allocate and initialize an AVIOContext for buffered I/O.
Definition aviobuf.c:109
void avio_context_free(AVIOContext **s)
Free the supplied IO context and everything associated with it.
Definition aviobuf.c:126
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition avstring.c:103
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int64_t filesize(AVIOContext *pb)
Definition ffmpeg_mux.c:51
#define fail
Definition test.h:479
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition options.c:165
const AVInputFormat * av_demuxer_iterate(void **opaque)
Iterate over all registered demuxers.
Definition allformats.c:618
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition avformat.c:150
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition demux.c:1588
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition demux.c:231
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition demux.c:2606
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition demux.c:377
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_PANIC
Something went really wrong and we will crash now.
Definition log.h:197
void av_log_set_level(int level)
Set the log level.
Definition log.c:476
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition avstring.c:85
unsigned offset
Definition libaomenc.c:763
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
const char data[16]
Definition mxf.c:149
#define av_malloc(s)
Definition ops_static.c:52
Format I/O context.
Definition avformat.h:1333
AVIOContext * pb
I/O context.
Definition avformat.h:1375
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition avformat.h:1618
Bytestream IO Context.
Definition avio.h:160
unsigned char * buffer
Start of the buffer.
Definition avio.h:225
int(* callback)(void *)
Definition avio.h:60
const char * name
A comma separated list of short names for the format.
Definition avformat.h:570
const char * extensions
If extensions are defined, then no probe is done.
Definition avformat.h:592
This structure stores compressed data.
Definition packet.h:580
AVInputFormat p
The public AVInputFormat.
Definition demux.h:70
const uint8_t * fuzz
#define av_freep(p)
const uint32_t maxiteration
static int64_t io_seek(void *opaque, int64_t offset, int whence)
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
static void error(const char *err)
static int interrupt_cb(void *ctx)
int64_t interrupt_counter
static int io_read(void *opaque, uint8_t *buf, int buf_size)
const int maxblocks
int size
static double c[64]