target_dem_fuzzer.c 4.2 KB

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