target_enc_fuzzer.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2024 Michael Niedermayer <michael-ffmpeg@niedermayer.cc>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Based on target_dec_fuzzer
  21. */
  22. #include "config.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/cpu.h"
  26. #include "libavutil/imgutils.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/mem.h"
  29. #include "libavcodec/avcodec.h"
  30. #include "libavcodec/bytestream.h"
  31. #include "libavcodec/codec_internal.h"
  32. #include "libavformat/avformat.h"
  33. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
  34. extern const FFCodec * codec_list[];
  35. static void error(const char *err)
  36. {
  37. fprintf(stderr, "%s", err);
  38. exit(1);
  39. }
  40. static const FFCodec *c = NULL;
  41. // Ensure we don't loop forever
  42. const uint32_t maxiteration = 8096;
  43. static int encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt)
  44. {
  45. int ret;
  46. ret = avcodec_send_frame(enc_ctx, frame);
  47. if (ret < 0)
  48. return ret;
  49. while (ret >= 0) {
  50. ret = avcodec_receive_packet(enc_ctx, pkt);
  51. if (ret == AVERROR(EAGAIN)) {
  52. return 0;
  53. } else if (ret < 0) {
  54. return ret;
  55. }
  56. av_packet_unref(pkt);
  57. }
  58. av_assert0(0);
  59. }
  60. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  61. uint64_t maxpixels_per_frame = 512 * 512;
  62. uint64_t maxpixels;
  63. const uint8_t *end = data + size;
  64. uint32_t it = 0;
  65. uint64_t nb_samples = 0;
  66. AVDictionary *opts = NULL;
  67. uint64_t ec_pixels = 0;
  68. if (!c) {
  69. #define ENCODER_SYMBOL0(CODEC) ff_##CODEC##_encoder
  70. #define ENCODER_SYMBOL(CODEC) ENCODER_SYMBOL0(CODEC)
  71. extern FFCodec ENCODER_SYMBOL(FFMPEG_ENCODER);
  72. codec_list[0] = &ENCODER_SYMBOL(FFMPEG_ENCODER);
  73. c = &ENCODER_SYMBOL(FFMPEG_ENCODER);
  74. av_log_set_level(AV_LOG_PANIC);
  75. }
  76. if (c->p.type != AVMEDIA_TYPE_VIDEO)
  77. return 0;
  78. maxpixels = maxpixels_per_frame * maxiteration;
  79. switch (c->p.id) {
  80. case AV_CODEC_ID_A64_MULTI: maxpixels /= 65536; break;
  81. case AV_CODEC_ID_A64_MULTI5: maxpixels /= 65536; break;
  82. }
  83. maxpixels_per_frame = FFMIN(maxpixels_per_frame , maxpixels);
  84. AVCodecContext* ctx = avcodec_alloc_context3(&c->p);
  85. if (!ctx)
  86. error("Failed memory allocation");
  87. if (ctx->max_pixels == 0 || ctx->max_pixels > maxpixels_per_frame)
  88. ctx->max_pixels = maxpixels_per_frame; //To reduce false positive OOM and hangs
  89. ctx->pix_fmt = AV_PIX_FMT_YUV420P;
  90. if (size > 1024) {
  91. GetByteContext gbc;
  92. int flags;
  93. int64_t flags64;
  94. size -= 1024;
  95. bytestream2_init(&gbc, data + size, 1024);
  96. ctx->width = bytestream2_get_le32(&gbc) & 0xFFFF;
  97. ctx->height = bytestream2_get_le32(&gbc) & 0xFFFF;
  98. ctx->bit_rate = bytestream2_get_le64(&gbc);
  99. ctx->gop_size = bytestream2_get_le32(&gbc) & 0x7FFFFFFF;
  100. ctx->max_b_frames = bytestream2_get_le32(&gbc) & 0x7FFFFFFF;
  101. ctx->time_base.num = bytestream2_get_le32(&gbc) & 0x7FFFFFFF;
  102. ctx->time_base.den = bytestream2_get_le32(&gbc) & 0x7FFFFFFF;
  103. ctx->framerate.num = bytestream2_get_le32(&gbc) & 0x7FFFFFFF;
  104. ctx->framerate.den = bytestream2_get_le32(&gbc) & 0x7FFFFFFF;
  105. flags = bytestream2_get_byte(&gbc);
  106. if (flags & 2)
  107. ctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
  108. if (flags & 0x40)
  109. av_force_cpu_flags(0);
  110. flags64 = bytestream2_get_le64(&gbc);
  111. if (c->p.pix_fmts) {
  112. int npixfmts = 0;
  113. while (c->p.pix_fmts[npixfmts++] != AV_PIX_FMT_NONE)
  114. ;
  115. ctx->pix_fmt = c->p.pix_fmts[bytestream2_get_byte(&gbc) % npixfmts];
  116. }
  117. switch (c->p.id) {
  118. case AV_CODEC_ID_FFV1:{
  119. int coder = bytestream2_get_byte(&gbc)&3;
  120. if (coder == 3) coder = -2;
  121. av_dict_set_int(&opts, "coder", coder, 0);
  122. av_dict_set_int(&opts, "context", bytestream2_get_byte(&gbc)&1, 0);
  123. av_dict_set_int(&opts, "slicecrc", bytestream2_get_byte(&gbc)&1, 0);
  124. break;}
  125. }
  126. }
  127. if (ctx->width == 0 || av_image_check_size(ctx->width, ctx->height, 0, ctx))
  128. ctx->width = ctx->height = 64;
  129. int res = avcodec_open2(ctx, &c->p, &opts);
  130. if (res < 0) {
  131. avcodec_free_context(&ctx);
  132. av_dict_free(&opts);
  133. return 0; // Failure of avcodec_open2() does not imply that a issue was found
  134. }
  135. AVFrame *frame = av_frame_alloc();
  136. AVPacket *avpkt = av_packet_alloc();
  137. if (!frame || !avpkt)
  138. error("Failed memory allocation");
  139. frame->format = ctx->pix_fmt;
  140. frame->width = ctx->width;
  141. frame->height = ctx->height;
  142. while (data < end && it < maxiteration) {
  143. ec_pixels += (ctx->width + 32LL) * (ctx->height + 32LL);
  144. if (ec_pixels > maxpixels)
  145. goto maximums_reached;
  146. res = av_frame_get_buffer(frame, 0);
  147. if (res < 0)
  148. error("Failed av_frame_get_buffer");
  149. for (int i=0; i<FF_ARRAY_ELEMS(frame->buf); i++) {
  150. if (frame->buf[i]) {
  151. int buf_size = FFMIN(end-data, frame->buf[i]->size);
  152. memcpy(frame->buf[i]->data, data, buf_size);
  153. memset(frame->buf[i]->data + buf_size, 0, frame->buf[i]->size - buf_size);
  154. data += buf_size;
  155. }
  156. }
  157. frame->pts = nb_samples;
  158. res = encode(ctx, frame, avpkt);
  159. if (res < 0)
  160. break;
  161. it++;
  162. for (int i=0; i<FF_ARRAY_ELEMS(frame->buf); i++)
  163. av_buffer_unref(&frame->buf[i]);
  164. av_packet_unref(avpkt);
  165. }
  166. maximums_reached:
  167. encode(ctx, NULL, avpkt);
  168. av_packet_unref(avpkt);
  169. // fprintf(stderr, "frames encoded: %"PRId64", iterations: %d\n", nb_samples , it);
  170. av_frame_free(&frame);
  171. avcodec_free_context(&ctx);
  172. av_packet_free(&avpkt);
  173. av_dict_free(&opts);
  174. return 0;
  175. }