You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
5.7KB

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