FFmpeg
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 
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;
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;
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  AVInputFormat *fmt = NULL;
107 #ifdef FFMPEG_DEMUXER
108 #define DEMUXER_SYMBOL0(DEMUXER) ff_##DEMUXER##_demuxer
109 #define DEMUXER_SYMBOL(DEMUXER) DEMUXER_SYMBOL0(DEMUXER)
110  extern AVInputFormat DEMUXER_SYMBOL(FFMPEG_DEMUXER);
111  fmt = &DEMUXER_SYMBOL(FFMPEG_DEMUXER);
112 #endif
113 
114  if (!c) {
116  c=1;
117  }
118 
119  if (!avfmt)
120  error("Failed avformat_alloc_context()");
121 
122  if (IO_FLAT) {
123  seekable = 1;
124  io_buffer_size = size;
125  } else if (size > 2048) {
126  int flags;
127  char extension[64];
128 
129  GetByteContext gbc;
130  memcpy (filename, data + size - 1024, 1024);
131  bytestream2_init(&gbc, data + size - 2048, 1024);
132  size -= 2048;
133 
134  io_buffer_size = bytestream2_get_le32(&gbc) & 0xFFFFFFF;
135  flags = bytestream2_get_byte(&gbc);
136  seekable = flags & 1;
137  filesize = bytestream2_get_le64(&gbc) & 0x7FFFFFFFFFFFFFFF;
138 
139  if ((flags & 2) && strlen(filename) < sizeof(filename) / 2) {
140  const AVInputFormat *avif = NULL;
141  void *avif_iter = NULL;
142  int avif_count = 0;
143  while ((avif = av_demuxer_iterate(&avif_iter))) {
144  if (avif->extensions)
145  avif_count ++;
146  }
147  avif_count = bytestream2_get_le32(&gbc) % avif_count;
148 
149  avif_iter = NULL;
150  while ((avif = av_demuxer_iterate(&avif_iter))) {
151  if (avif->extensions)
152  if (!avif_count--)
153  break;
154  }
155  av_strlcpy(extension, avif->extensions, sizeof(extension));
156  if (strchr(extension, ','))
157  *strchr(extension, ',') = 0;
158  av_strlcatf(filename, sizeof(filename), ".%s", extension);
159  }
160  }
161  io_buffer = av_malloc(io_buffer_size);
162  if (!io_buffer)
163  error("Failed to allocate io_buffer");
164 
165  opaque.filesize = filesize;
166  opaque.pos = 0;
167  opaque.fuzz = data;
168  opaque.fuzz_size= size;
169  fuzzed_pb = avio_alloc_context(io_buffer, io_buffer_size, 0, &opaque,
170  io_read, NULL, seekable ? io_seek : NULL);
171  if (!fuzzed_pb)
172  error("avio_alloc_context failed");
173 
174  avfmt->pb = fuzzed_pb;
175 
176  ret = avformat_open_input(&avfmt, filename, fmt, NULL);
177  if (ret < 0) {
178  av_freep(&fuzzed_pb->buffer);
179  av_freep(&fuzzed_pb);
180  avformat_free_context(avfmt);
181  return 0;
182  }
183 
184  ret = avformat_find_stream_info(avfmt, NULL);
185 
186  av_init_packet(&pkt);
187 
188  //TODO, test seeking
189 
190  for(it = 0; it < maxiteration; it++) {
191  ret = av_read_frame(avfmt, &pkt);
192  if (ret < 0)
193  break;
194  av_packet_unref(&pkt);
195  }
196 end:
197  av_freep(&fuzzed_pb->buffer);
198  av_freep(&fuzzed_pb);
199  avformat_close_input(&avfmt);
200 
201  return 0;
202 }
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
void av_log_set_level(int level)
Set the log level.
Definition: log.c:440
const uint32_t maxiteration
unsigned char * buffer
Start of the buffer.
Definition: avio.h:226
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:500
static AVPacket pkt
static int io_read(void *opaque, uint8_t *buf, int buf_size)
Format I/O context.
Definition: avformat.h:1355
uint8_t
#define av_malloc(s)
#define AV_LOG_PANIC
Something went really wrong and we will crash now.
Definition: log.h:181
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
static const uint64_t FUZZ_TAG
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:211
s EdgeDetect Foobar g libavfilter vf_edgedetect c libavfilter vf_foobar c edit libavfilter and add an entry for foobar following the pattern of the other filters edit libavfilter allfilters and add an entry for foobar following the pattern of the other filters configure make j< whatever > ffmpeg ffmpeg i you should get a foobar png with Lena edge detected That s it
#define AVERROR_EOF
End of file.
Definition: error.h:55
ptrdiff_t size
Definition: opengl_enc.c:100
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, 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:138
simple assert() macros that are a bit more flexible than ISO C assert().
#define FFMAX(a, b)
Definition: common.h:94
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:83
#define FFMIN(a, b)
Definition: common.h:96
int64_t filesize
const AVInputFormat * av_demuxer_iterate(void **opaque)
Iterate over all registered demuxers.
Definition: allformats.c:550
uint8_t * fuzz
Libavcodec external API header.
AVIOContext * pb
I/O context.
Definition: avformat.h:1397
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:606
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:101
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4378
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1710
#define flags(name, subs,...)
Definition: cbs_av1.c:560
const char * extensions
If extensions are defined, then no probe is done.
Definition: avformat.h:673
Main libavformat public API header.
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:3545
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:35
#define AVSEEK_SIZE
ORing this as the "whence" parameter to a seek function causes it to return the filesize without seek...
Definition: avio.h:531
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:4421
static void error(const char *err)
#define av_freep(p)
This structure stores compressed data.
Definition: packet.h:340
static int64_t io_seek(void *opaque, int64_t offset, int whence)