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.

203 lines
5.5KB

  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. static const uint64_t FUZZ_TAG = 0x4741542D5A5A5546ULL;
  77. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  78. const uint64_t fuzz_tag = FUZZ_TAG;
  79. uint32_t it = 0;
  80. AVFormatContext *avfmt = avformat_alloc_context();
  81. AVPacket pkt;
  82. char filename[1025] = {0};
  83. AVIOContext *fuzzed_pb = NULL;
  84. uint8_t *io_buffer;
  85. int io_buffer_size = 32768;
  86. int64_t filesize = size;
  87. IOContext opaque;
  88. static int c;
  89. int seekable = 0;
  90. int ret;
  91. AVInputFormat *fmt = NULL;
  92. if (!c) {
  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. av_register_all();
  100. avcodec_register_all();
  101. av_log_set_level(AV_LOG_PANIC);
  102. c=1;
  103. }
  104. if (!avfmt)
  105. error("Failed avformat_alloc_context()");
  106. if (IO_FLAT) {
  107. seekable = 1;
  108. io_buffer_size = size;
  109. } else if (size > 2048) {
  110. int flags;
  111. char extension[64];
  112. GetByteContext gbc;
  113. memcpy (filename, data + size - 1024, 1024);
  114. bytestream2_init(&gbc, data + size - 2048, 1024);
  115. size -= 2048;
  116. io_buffer_size = bytestream2_get_le32(&gbc) & 0xFFFFFFF;
  117. flags = bytestream2_get_byte(&gbc);
  118. seekable = flags & 1;
  119. filesize = bytestream2_get_le64(&gbc) & 0x7FFFFFFFFFFFFFFF;
  120. if ((flags & 2) && strlen(filename) < sizeof(filename) / 2) {
  121. AVInputFormat *avif = NULL;
  122. int avif_count = 0;
  123. while ((avif = av_iformat_next(avif))) {
  124. if (avif->extensions)
  125. avif_count ++;
  126. }
  127. avif_count = bytestream2_get_le32(&gbc) % avif_count;
  128. while ((avif = av_iformat_next(avif))) {
  129. if (avif->extensions)
  130. if (!avif_count--)
  131. break;
  132. }
  133. av_strlcpy(extension, avif->extensions, sizeof(extension));
  134. if (strchr(extension, ','))
  135. *strchr(extension, ',') = 0;
  136. av_strlcatf(filename, sizeof(filename), ".%s", extension);
  137. }
  138. }
  139. io_buffer = av_malloc(io_buffer_size);
  140. if (!io_buffer)
  141. error("Failed to allocate io_buffer");
  142. opaque.filesize = filesize;
  143. opaque.pos = 0;
  144. opaque.fuzz = data;
  145. opaque.fuzz_size= size;
  146. fuzzed_pb = avio_alloc_context(io_buffer, io_buffer_size, 0, &opaque,
  147. io_read, NULL, seekable ? io_seek : NULL);
  148. if (!fuzzed_pb)
  149. error("avio_alloc_context failed");
  150. avfmt->pb = fuzzed_pb;
  151. ret = avformat_open_input(&avfmt, filename, fmt, NULL);
  152. if (ret < 0) {
  153. av_freep(&fuzzed_pb->buffer);
  154. av_freep(&fuzzed_pb);
  155. avformat_free_context(avfmt);
  156. return 0;
  157. }
  158. ret = avformat_find_stream_info(avfmt, NULL);
  159. av_init_packet(&pkt);
  160. //TODO, test seeking
  161. for(it = 0; it < maxiteration; it++) {
  162. ret = av_read_frame(avfmt, &pkt);
  163. if (ret < 0)
  164. break;
  165. av_packet_unref(&pkt);
  166. }
  167. end:
  168. av_freep(&fuzzed_pb->buffer);
  169. av_freep(&fuzzed_pb);
  170. avformat_close_input(&avfmt);
  171. return 0;
  172. }