target_dem_fuzzer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "libavcodec/avcodec.h"
  21. #include "libavcodec/bytestream.h"
  22. #include "libavformat/avformat.h"
  23. typedef struct IOContext {
  24. int64_t pos;
  25. int64_t filesize;
  26. uint8_t *fuzz;
  27. int fuzz_size;
  28. } IOContext;
  29. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
  30. static void error(const char *err)
  31. {
  32. fprintf(stderr, "%s", err);
  33. exit(1);
  34. }
  35. static int io_read(void *opaque, uint8_t *buf, int buf_size)
  36. {
  37. IOContext *c = opaque;
  38. int size = FFMIN(buf_size, c->fuzz_size);
  39. if (!c->fuzz_size) {
  40. c->filesize = FFMIN(c->pos, c->filesize);
  41. return AVERROR_EOF;
  42. }
  43. memcpy(buf, c->fuzz, size);
  44. c->fuzz += size;
  45. c->fuzz_size -= size;
  46. c->pos += size;
  47. c->filesize = FFMAX(c->filesize, c->pos);
  48. return size;
  49. }
  50. static int64_t io_seek(void *opaque, int64_t offset, int whence)
  51. {
  52. IOContext *c = opaque;
  53. if (whence == SEEK_CUR) {
  54. if (offset > INT64_MAX - c->pos)
  55. return -1;
  56. offset += c->pos;
  57. } else if (whence == SEEK_END) {
  58. if (offset > INT64_MAX - c->filesize)
  59. return -1;
  60. offset += c->filesize;
  61. } else if (whence == AVSEEK_SIZE) {
  62. return c->filesize;
  63. }
  64. if (offset < 0 || offset > c->filesize)
  65. return -1;
  66. c->pos = offset;
  67. return 0;
  68. }
  69. // Ensure we don't loop forever
  70. const uint32_t maxiteration = 8096;
  71. static const uint64_t FUZZ_TAG = 0x4741542D5A5A5546ULL;
  72. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  73. const uint64_t fuzz_tag = FUZZ_TAG;
  74. uint32_t it = 0;
  75. AVFormatContext *avfmt = avformat_alloc_context();
  76. AVPacket pkt;
  77. char filename[1025] = {0};
  78. AVIOContext *fuzzed_pb = NULL;
  79. uint8_t *io_buffer;
  80. int io_buffer_size = 32768;
  81. int64_t filesize = size;
  82. IOContext opaque;
  83. static int c;
  84. int seekable = 0;
  85. int ret;
  86. if (!c) {
  87. av_register_all();
  88. avcodec_register_all();
  89. av_log_set_level(AV_LOG_PANIC);
  90. c=1;
  91. }
  92. if (!avfmt)
  93. error("Failed avformat_alloc_context()");
  94. if (size > 2048) {
  95. GetByteContext gbc;
  96. memcpy (filename, data + size - 1024, 1024);
  97. bytestream2_init(&gbc, data + size - 2048, 1024);
  98. size -= 2048;
  99. io_buffer_size = bytestream2_get_le32(&gbc) & 0xFFFFFFF;
  100. seekable = bytestream2_get_byte(&gbc) & 1;
  101. filesize = bytestream2_get_le64(&gbc) & 0x7FFFFFFFFFFFFFFF;
  102. }
  103. io_buffer = av_malloc(io_buffer_size);
  104. if (!io_buffer)
  105. error("Failed to allocate io_buffer");
  106. opaque.filesize = filesize;
  107. opaque.pos = 0;
  108. opaque.fuzz = data;
  109. opaque.fuzz_size= size;
  110. fuzzed_pb = avio_alloc_context(io_buffer, io_buffer_size, 0, &opaque,
  111. io_read, NULL, seekable ? io_seek : NULL);
  112. if (!fuzzed_pb)
  113. error("avio_alloc_context failed");
  114. avfmt->pb = fuzzed_pb;
  115. ret = avformat_open_input(&avfmt, filename, NULL, NULL);
  116. if (ret < 0) {
  117. av_freep(&fuzzed_pb->buffer);
  118. av_freep(&fuzzed_pb);
  119. avformat_free_context(avfmt);
  120. return 0;
  121. }
  122. ret = avformat_find_stream_info(avfmt, NULL);
  123. av_init_packet(&pkt);
  124. //TODO, test seeking
  125. for(it = 0; it < maxiteration; it++) {
  126. ret = av_read_frame(avfmt, &pkt);
  127. if (ret < 0)
  128. break;
  129. av_packet_unref(&pkt);
  130. }
  131. end:
  132. av_freep(&fuzzed_pb->buffer);
  133. av_freep(&fuzzed_pb);
  134. avformat_close_input(&avfmt);
  135. return 0;
  136. }