target_dem_fuzzer.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #include "config.h"
  19. #include "libavutil/avassert.h"
  20. #include "libavutil/avstring.h"
  21. #include "libavutil/mem.h"
  22. #include "libavcodec/avcodec.h"
  23. #include "libavcodec/bytestream.h"
  24. #include "libavformat/avformat.h"
  25. #include "libavformat/demux.h"
  26. typedef struct IOContext {
  27. int64_t pos;
  28. int64_t filesize;
  29. uint8_t *fuzz;
  30. int fuzz_size;
  31. } IOContext;
  32. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
  33. int64_t interrupt_counter;
  34. static int interrupt_cb(void *ctx)
  35. {
  36. interrupt_counter --;
  37. return interrupt_counter < 0;
  38. }
  39. static void error(const char *err)
  40. {
  41. fprintf(stderr, "%s", err);
  42. exit(1);
  43. }
  44. static int io_read(void *opaque, uint8_t *buf, int buf_size)
  45. {
  46. IOContext *c = opaque;
  47. int size = FFMIN(buf_size, c->fuzz_size);
  48. if (!c->fuzz_size) {
  49. c->filesize = FFMIN(c->pos, c->filesize);
  50. return AVERROR_EOF;
  51. }
  52. if (c->pos > INT64_MAX - size)
  53. return AVERROR(EIO);
  54. memcpy(buf, c->fuzz, size);
  55. c->fuzz += size;
  56. c->fuzz_size -= size;
  57. c->pos += size;
  58. c->filesize = FFMAX(c->filesize, c->pos);
  59. return size;
  60. }
  61. static int64_t io_seek(void *opaque, int64_t offset, int whence)
  62. {
  63. IOContext *c = opaque;
  64. if (whence == SEEK_CUR) {
  65. if (offset > INT64_MAX - c->pos)
  66. return -1;
  67. offset += c->pos;
  68. } else if (whence == SEEK_END) {
  69. if (offset > INT64_MAX - c->filesize)
  70. return -1;
  71. offset += c->filesize;
  72. } else if (whence == AVSEEK_SIZE) {
  73. return c->filesize;
  74. }
  75. if (offset < 0 || offset > c->filesize)
  76. return -1;
  77. if (IO_FLAT) {
  78. c->fuzz += offset - c->pos;
  79. c->fuzz_size -= offset - c->pos;
  80. }
  81. c->pos = offset;
  82. return 0;
  83. }
  84. // Ensure we don't loop forever
  85. const uint32_t maxiteration = 8096;
  86. const int maxblocks= 50000;
  87. static const uint64_t FUZZ_TAG = 0x4741542D5A5A5546ULL;
  88. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  89. const uint64_t fuzz_tag = FUZZ_TAG;
  90. uint32_t it = 0;
  91. AVFormatContext *avfmt = avformat_alloc_context();
  92. AVPacket *pkt;
  93. char filename[1025] = {0};
  94. AVIOContext *fuzzed_pb = NULL;
  95. uint8_t *io_buffer;
  96. int io_buffer_size = 32768;
  97. int64_t filesize = size;
  98. IOContext opaque;
  99. static int c;
  100. int seekable = 0;
  101. int ret;
  102. const AVInputFormat *fmt = NULL;
  103. #ifdef FFMPEG_DEMUXER
  104. #define DEMUXER_SYMBOL0(DEMUXER) ff_##DEMUXER##_demuxer
  105. #define DEMUXER_SYMBOL(DEMUXER) DEMUXER_SYMBOL0(DEMUXER)
  106. extern const FFInputFormat DEMUXER_SYMBOL(FFMPEG_DEMUXER);
  107. fmt = &DEMUXER_SYMBOL(FFMPEG_DEMUXER).p;
  108. #endif
  109. if (!c) {
  110. av_log_set_level(AV_LOG_PANIC);
  111. c=1;
  112. }
  113. if (!avfmt)
  114. error("Failed avformat_alloc_context()");
  115. if (IO_FLAT) {
  116. seekable = 1;
  117. io_buffer_size = size;
  118. } else if (size > 2048) {
  119. int flags;
  120. char extension[64];
  121. GetByteContext gbc;
  122. memcpy (filename, data + size - 1024, 1024);
  123. bytestream2_init(&gbc, data + size - 2048, 1024);
  124. size -= 2048;
  125. io_buffer_size = bytestream2_get_le32(&gbc) & 0xFFFFFFF;
  126. flags = bytestream2_get_byte(&gbc);
  127. seekable = flags & 1;
  128. filesize = bytestream2_get_le64(&gbc) & 0x7FFFFFFFFFFFFFFF;
  129. if ((flags & 2) && strlen(filename) < sizeof(filename) / 2) {
  130. const AVInputFormat *avif = NULL;
  131. void *avif_iter = NULL;
  132. int avif_count = 0;
  133. while ((avif = av_demuxer_iterate(&avif_iter))) {
  134. if (avif->extensions)
  135. avif_count ++;
  136. }
  137. avif_count = bytestream2_get_le32(&gbc) % avif_count;
  138. avif_iter = NULL;
  139. while ((avif = av_demuxer_iterate(&avif_iter))) {
  140. if (avif->extensions)
  141. if (!avif_count--)
  142. break;
  143. }
  144. av_strlcpy(extension, avif->extensions, sizeof(extension));
  145. if (strchr(extension, ','))
  146. *strchr(extension, ',') = 0;
  147. av_strlcatf(filename, sizeof(filename), ".%s", extension);
  148. }
  149. interrupt_counter = bytestream2_get_le32(&gbc);
  150. avfmt->interrupt_callback.callback = interrupt_cb;
  151. }
  152. // HLS uses a loop with sleep, we thus must breakout or we timeout
  153. if (fmt && !strcmp(fmt->name, "hls"))
  154. interrupt_counter &= 31;
  155. if (!io_buffer_size || size / io_buffer_size > maxblocks)
  156. io_buffer_size = size;
  157. pkt = av_packet_alloc();
  158. if (!pkt)
  159. error("Failed to allocate pkt");
  160. io_buffer = av_malloc(io_buffer_size);
  161. if (!io_buffer)
  162. error("Failed to allocate io_buffer");
  163. opaque.filesize = filesize;
  164. opaque.pos = 0;
  165. opaque.fuzz = data;
  166. opaque.fuzz_size= size;
  167. fuzzed_pb = avio_alloc_context(io_buffer, io_buffer_size, 0, &opaque,
  168. io_read, NULL, seekable ? io_seek : NULL);
  169. if (!fuzzed_pb)
  170. error("avio_alloc_context failed");
  171. avfmt->pb = fuzzed_pb;
  172. ret = avformat_open_input(&avfmt, filename, fmt, NULL);
  173. if (ret < 0) {
  174. goto fail;
  175. }
  176. ret = avformat_find_stream_info(avfmt, NULL);
  177. //TODO, test seeking
  178. for(it = 0; it < maxiteration; it++) {
  179. ret = av_read_frame(avfmt, pkt);
  180. if (ret < 0)
  181. break;
  182. av_packet_unref(pkt);
  183. }
  184. fail:
  185. av_packet_free(&pkt);
  186. av_freep(&fuzzed_pb->buffer);
  187. avio_context_free(&fuzzed_pb);
  188. avformat_close_input(&avfmt);
  189. return 0;
  190. }