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.

196 lines
5.3KB

  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. if (!c) {
  92. av_register_all();
  93. avcodec_register_all();
  94. av_log_set_level(AV_LOG_PANIC);
  95. c=1;
  96. }
  97. if (!avfmt)
  98. error("Failed avformat_alloc_context()");
  99. if (IO_FLAT) {
  100. seekable = 1;
  101. io_buffer_size = size;
  102. } else if (size > 2048) {
  103. int flags;
  104. char extension[64];
  105. GetByteContext gbc;
  106. memcpy (filename, data + size - 1024, 1024);
  107. bytestream2_init(&gbc, data + size - 2048, 1024);
  108. size -= 2048;
  109. io_buffer_size = bytestream2_get_le32(&gbc) & 0xFFFFFFF;
  110. flags = bytestream2_get_byte(&gbc);
  111. seekable = flags & 1;
  112. filesize = bytestream2_get_le64(&gbc) & 0x7FFFFFFFFFFFFFFF;
  113. if ((flags & 2) && strlen(filename) < sizeof(filename) / 2) {
  114. AVInputFormat *avif = NULL;
  115. int avif_count = 0;
  116. while ((avif = av_iformat_next(avif))) {
  117. if (avif->extensions)
  118. avif_count ++;
  119. }
  120. avif_count = bytestream2_get_le32(&gbc) % avif_count;
  121. while ((avif = av_iformat_next(avif))) {
  122. if (avif->extensions)
  123. if (!avif_count--)
  124. break;
  125. }
  126. av_strlcpy(extension, avif->extensions, sizeof(extension));
  127. if (strchr(extension, ','))
  128. *strchr(extension, ',') = 0;
  129. av_strlcatf(filename, sizeof(filename), ".%s", extension);
  130. }
  131. }
  132. io_buffer = av_malloc(io_buffer_size);
  133. if (!io_buffer)
  134. error("Failed to allocate io_buffer");
  135. opaque.filesize = filesize;
  136. opaque.pos = 0;
  137. opaque.fuzz = data;
  138. opaque.fuzz_size= size;
  139. fuzzed_pb = avio_alloc_context(io_buffer, io_buffer_size, 0, &opaque,
  140. io_read, NULL, seekable ? io_seek : NULL);
  141. if (!fuzzed_pb)
  142. error("avio_alloc_context failed");
  143. avfmt->pb = fuzzed_pb;
  144. ret = avformat_open_input(&avfmt, filename, NULL, NULL);
  145. if (ret < 0) {
  146. av_freep(&fuzzed_pb->buffer);
  147. av_freep(&fuzzed_pb);
  148. avformat_free_context(avfmt);
  149. return 0;
  150. }
  151. ret = avformat_find_stream_info(avfmt, NULL);
  152. av_init_packet(&pkt);
  153. //TODO, test seeking
  154. for(it = 0; it < maxiteration; it++) {
  155. ret = av_read_frame(avfmt, &pkt);
  156. if (ret < 0)
  157. break;
  158. av_packet_unref(&pkt);
  159. }
  160. end:
  161. av_freep(&fuzzed_pb->buffer);
  162. av_freep(&fuzzed_pb);
  163. avformat_close_input(&avfmt);
  164. return 0;
  165. }