target_dem_fuzzer.c 5.7 KB

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