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.

189 lines
5.1KB

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