target_bsf_fuzzer.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/imgutils.h"
  20. #include "libavcodec/avcodec.h"
  21. #include "libavcodec/bsf_internal.h"
  22. #include "libavcodec/bytestream.h"
  23. #include "libavcodec/internal.h"
  24. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
  25. static void error(const char *err)
  26. {
  27. fprintf(stderr, "%s", err);
  28. exit(1);
  29. }
  30. static AVBitStreamFilter *f = NULL;
  31. static const uint64_t FUZZ_TAG = 0x4741542D5A5A5546ULL;
  32. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  33. const uint64_t fuzz_tag = FUZZ_TAG;
  34. const uint8_t *last = data;
  35. const uint8_t *end = data + size;
  36. AVBSFContext *bsf = NULL;
  37. AVPacket in, out;
  38. uint64_t keyframes = 0;
  39. int res;
  40. if (!f) {
  41. #ifdef FFMPEG_BSF
  42. #define BSF_SYMBOL0(BSF) ff_##BSF##_bsf
  43. #define BSF_SYMBOL(BSF) BSF_SYMBOL0(BSF)
  44. extern AVBitStreamFilter BSF_SYMBOL(FFMPEG_BSF);
  45. f = &BSF_SYMBOL(FFMPEG_BSF);
  46. #else
  47. extern AVBitStreamFilter ff_null_bsf;
  48. f = &ff_null_bsf;
  49. #endif
  50. av_log_set_level(AV_LOG_PANIC);
  51. }
  52. res = av_bsf_alloc(f, &bsf);
  53. if (res < 0)
  54. error("Failed memory allocation");
  55. if (size > 1024) {
  56. GetByteContext gbc;
  57. int extradata_size;
  58. size -= 1024;
  59. bytestream2_init(&gbc, data + size, 1024);
  60. bsf->par_in->width = bytestream2_get_le32(&gbc);
  61. bsf->par_in->height = bytestream2_get_le32(&gbc);
  62. bsf->par_in->bit_rate = bytestream2_get_le64(&gbc);
  63. bsf->par_in->bits_per_coded_sample = bytestream2_get_le32(&gbc);
  64. if (f->codec_ids) {
  65. int i, id;
  66. for (i = 0; f->codec_ids[i] != AV_CODEC_ID_NONE; i++);
  67. id = f->codec_ids[bytestream2_get_byte(&gbc) % i];
  68. bsf->par_in->codec_id = id;
  69. bsf->par_in->codec_tag = bytestream2_get_le32(&gbc);
  70. }
  71. extradata_size = bytestream2_get_le32(&gbc);
  72. bsf->par_in->sample_rate = bytestream2_get_le32(&gbc);
  73. bsf->par_in->channels = (unsigned)bytestream2_get_le32(&gbc) % FF_SANE_NB_CHANNELS;
  74. bsf->par_in->block_align = bytestream2_get_le32(&gbc);
  75. keyframes = bytestream2_get_le64(&gbc);
  76. if (extradata_size < size) {
  77. bsf->par_in->extradata = av_mallocz(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  78. if (bsf->par_in->extradata) {
  79. bsf->par_in->extradata_size = extradata_size;
  80. size -= bsf->par_in->extradata_size;
  81. memcpy(bsf->par_in->extradata, data + size, bsf->par_in->extradata_size);
  82. }
  83. }
  84. if (av_image_check_size(bsf->par_in->width, bsf->par_in->height, 0, bsf))
  85. bsf->par_in->width = bsf->par_in->height = 0;
  86. }
  87. res = av_bsf_init(bsf);
  88. if (res < 0) {
  89. av_bsf_free(&bsf);
  90. return 0; // Failure of av_bsf_init() does not imply that a issue was found
  91. }
  92. av_init_packet(&in);
  93. av_init_packet(&out);
  94. out.data = NULL;
  95. out.size = 0;
  96. while (data < end) {
  97. // Search for the TAG
  98. while (data + sizeof(fuzz_tag) < end) {
  99. if (data[0] == (fuzz_tag & 0xFF) && AV_RN64(data) == fuzz_tag)
  100. break;
  101. data++;
  102. }
  103. if (data + sizeof(fuzz_tag) > end)
  104. data = end;
  105. res = av_new_packet(&in, data - last);
  106. if (res < 0)
  107. error("Failed memory allocation");
  108. memcpy(in.data, last, data - last);
  109. in.flags = (keyframes & 1) * AV_PKT_FLAG_DISCARD + (!!(keyframes & 2)) * AV_PKT_FLAG_KEY;
  110. keyframes = (keyframes >> 2) + (keyframes<<62);
  111. data += sizeof(fuzz_tag);
  112. last = data;
  113. while (in.size) {
  114. res = av_bsf_send_packet(bsf, &in);
  115. if (res < 0 && res != AVERROR(EAGAIN))
  116. break;
  117. res = av_bsf_receive_packet(bsf, &out);
  118. if (res < 0)
  119. break;
  120. av_packet_unref(&out);
  121. }
  122. av_packet_unref(&in);
  123. }
  124. res = av_bsf_send_packet(bsf, NULL);
  125. while (!res) {
  126. res = av_bsf_receive_packet(bsf, &out);
  127. if (res < 0)
  128. break;
  129. av_packet_unref(&out);
  130. }
  131. av_bsf_free(&bsf);
  132. return 0;
  133. }