target_bsf_fuzzer.c 5.8 KB

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