target_dem_fuzzer.c 6.1 KB

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