Re: [FFmpeg-devel] [PATCH] tools: add target_enc_fuzzer.c

2024-04-21 Thread Michael Niedermayer
On Fri, Apr 19, 2024 at 10:48:05PM -0300, James Almer wrote:
> On 4/19/2024 10:10 PM, Michael Niedermayer wrote:
> > Sponsored-by: Sovereign Tech Fund
> > Signed-off-by: Michael Niedermayer 
> > ---
> >   Makefile  |   3 +
> >   tools/Makefile|   3 +
> >   tools/target_enc_fuzzer.c | 213 ++
> >   3 files changed, 219 insertions(+)
> >   create mode 100644 tools/target_enc_fuzzer.c
> > 
> > diff --git a/Makefile b/Makefile
> > index b309dbc4db9..de727cbe00e 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -52,6 +52,9 @@ $(TOOLS): %$(EXESUF): %.o
> >   target_dec_%_fuzzer$(EXESUF): target_dec_%_fuzzer.o $(FF_DEP_LIBS)
> > $(LD) $(LDFLAGS) $(LDEXEFLAGS) $(LD_O) $^ $(ELIBS) $(FF_EXTRALIBS) 
> > $(LIBFUZZER_PATH)
> > +target_enc_%_fuzzer$(EXESUF): target_enc_%_fuzzer.o $(FF_DEP_LIBS)
> > +   $(LD) $(LDFLAGS) $(LDEXEFLAGS) $(LD_O) $^ $(ELIBS) $(FF_EXTRALIBS) 
> > $(LIBFUZZER_PATH)
> > +
> >   tools/target_bsf_%_fuzzer$(EXESUF): tools/target_bsf_%_fuzzer.o 
> > $(FF_DEP_LIBS)
> > $(LD) $(LDFLAGS) $(LDEXEFLAGS) $(LD_O) $^ $(ELIBS) $(FF_EXTRALIBS) 
> > $(LIBFUZZER_PATH)
> > diff --git a/tools/Makefile b/tools/Makefile
> > index 72e8e709a8d..2a11fa0ae62 100644
> > --- a/tools/Makefile
> > +++ b/tools/Makefile
> > @@ -5,6 +5,9 @@ TOOLS-$(CONFIG_ZLIB) += cws2fws
> >   tools/target_dec_%_fuzzer.o: tools/target_dec_fuzzer.c
> > $(COMPILE_C) -DFFMPEG_DECODER=$*
> > +tools/target_enc_%_fuzzer.o: tools/target_enc_fuzzer.c
> > +   $(COMPILE_C) -DFFMPEG_ENCODER=$*
> > +
> >   tools/target_bsf_%_fuzzer.o: tools/target_bsf_fuzzer.c
> > $(COMPILE_C) -DFFMPEG_BSF=$*
> > diff --git a/tools/target_enc_fuzzer.c b/tools/target_enc_fuzzer.c
> > new file mode 100644
> > index 000..bc9f98c1443
> > --- /dev/null
> > +++ b/tools/target_enc_fuzzer.c
> > @@ -0,0 +1,213 @@
> > +/*
> > + * Copyright (c) 2024 Michael Niedermayer 
> > + *
> > + * This file is part of FFmpeg.
> > + *
> > + * FFmpeg is free software; you can redistribute it and/or
> > + * modify it under the terms of the GNU Lesser General Public
> > + * License as published by the Free Software Foundation; either
> > + * version 2.1 of the License, or (at your option) any later version.
> > + *
> > + * FFmpeg is distributed in the hope that it will be useful,
> > + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> > + * Lesser General Public License for more details.
> > + *
> > + * You should have received a copy of the GNU Lesser General Public
> > + * License along with FFmpeg; if not, write to the Free Software
> > + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
> > 02110-1301 USA
> > + *
> > + * Based on target_dec_fuzzer
> > + */
> > +
> > +#include "config.h"
> > +#include "libavutil/avassert.h"
> > +#include "libavutil/avstring.h"
> > +#include "libavutil/cpu.h"
> > +#include "libavutil/imgutils.h"
> > +#include "libavutil/intreadwrite.h"
> > +#include "libavutil/mem.h"
> > +
> > +#include "libavcodec/avcodec.h"
> > +#include "libavcodec/bytestream.h"
> > +#include "libavcodec/codec_internal.h"
> > +#include "libavformat/avformat.h"
> > +
> > +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
> > +
> > +extern const FFCodec * codec_list[];
> > +
> > +static void error(const char *err)
> > +{
> > +fprintf(stderr, "%s", err);
> > +exit(1);
> > +}
> > +
> > +static const FFCodec *c = NULL;
> > +static const FFCodec *AVCodecInitialize(enum AVCodecID codec_id)
> > +{
> > +const AVCodec *res;
> > +
> > +res = avcodec_find_decoder(codec_id);
> > +if (!res)
> > +error("Failed to find decoder");
> > +return ffcodec(res);
> > +}
> > +
> > +// Ensure we don't loop forever
> > +const uint32_t maxiteration = 8096;
> > +
> > +
> > +static int encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt)
> > +{
> > +int ret;
> > +
> > +ret = avcodec_send_frame(enc_ctx, frame);
> > +if (ret < 0)
> > +return ret;
> > +
> > +while (ret >= 0) {
> > +ret = avcodec_receive_packet(enc_ctx, pkt);
> > +if (ret == AVERROR(EAGAIN)) {
> > +return 0;
> > +} else if (ret < 0) {
> > +return ret;
> > +}
> > +
> > +av_packet_unref(pkt);
> > +}
> > +av_assert0(0);
> > +}
> > +
> > +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
> > +uint64_t maxpixels_per_frame = 512 * 512;
> > +uint64_t maxpixels;
> > +
> > +uint64_t maxsamples;
> > +const uint8_t *end = data + size;
> > +uint32_t it = 0;
> > +uint64_t nb_samples = 0;
> > +AVDictionary *opts = NULL;
> > +
> > +if (!c) {
> > +#ifdef FFMPEG_ENCODER
> > +#define ENCODER_SYMBOL0(CODEC) ff_##CODEC##_encoder
> > +#define ENCODER_SYMBOL(CODEC) ENCODER_SYMBOL0(CODEC)
> > +extern FFCodec ENCODER_SYMBOL(FFMPEG_ENCODER);
> > +codec_list[0] = 

Re: [FFmpeg-devel] [PATCH] tools: add target_enc_fuzzer.c

2024-04-21 Thread Michael Niedermayer
On Sat, Apr 20, 2024 at 09:50:19AM +0200, Stefano Sabatini wrote:
> On date Saturday 2024-04-20 03:10:37 +0200, Michael Niedermayer wrote:
[...]
> > +static const FFCodec *AVCodecInitialize(enum AVCodecID codec_id)
> 
> nit: snake_case, also the function is used once and the code can be
> embedded in the code

This is from the decoder fuzzer, it seems not needed at all so i droped it

thx

[...]
-- 
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

The worst form of inequality is to try to make unequal things equal.
-- Aristotle


signature.asc
Description: PGP signature
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] tools: add target_enc_fuzzer.c

2024-04-20 Thread Stefano Sabatini
On date Saturday 2024-04-20 03:10:37 +0200, Michael Niedermayer wrote:
> Sponsored-by: Sovereign Tech Fund
> Signed-off-by: Michael Niedermayer 
> ---
>  Makefile  |   3 +
>  tools/Makefile|   3 +
>  tools/target_enc_fuzzer.c | 213 ++
>  3 files changed, 219 insertions(+)
>  create mode 100644 tools/target_enc_fuzzer.c
> 
> diff --git a/Makefile b/Makefile
> index b309dbc4db9..de727cbe00e 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -52,6 +52,9 @@ $(TOOLS): %$(EXESUF): %.o
>  target_dec_%_fuzzer$(EXESUF): target_dec_%_fuzzer.o $(FF_DEP_LIBS)
>   $(LD) $(LDFLAGS) $(LDEXEFLAGS) $(LD_O) $^ $(ELIBS) $(FF_EXTRALIBS) 
> $(LIBFUZZER_PATH)
>  
> +target_enc_%_fuzzer$(EXESUF): target_enc_%_fuzzer.o $(FF_DEP_LIBS)
> + $(LD) $(LDFLAGS) $(LDEXEFLAGS) $(LD_O) $^ $(ELIBS) $(FF_EXTRALIBS) 
> $(LIBFUZZER_PATH)
> +
>  tools/target_bsf_%_fuzzer$(EXESUF): tools/target_bsf_%_fuzzer.o 
> $(FF_DEP_LIBS)
>   $(LD) $(LDFLAGS) $(LDEXEFLAGS) $(LD_O) $^ $(ELIBS) $(FF_EXTRALIBS) 
> $(LIBFUZZER_PATH)
>  
> diff --git a/tools/Makefile b/tools/Makefile
> index 72e8e709a8d..2a11fa0ae62 100644
> --- a/tools/Makefile
> +++ b/tools/Makefile
> @@ -5,6 +5,9 @@ TOOLS-$(CONFIG_ZLIB) += cws2fws
>  tools/target_dec_%_fuzzer.o: tools/target_dec_fuzzer.c
>   $(COMPILE_C) -DFFMPEG_DECODER=$*
>  
> +tools/target_enc_%_fuzzer.o: tools/target_enc_fuzzer.c
> + $(COMPILE_C) -DFFMPEG_ENCODER=$*
> +
>  tools/target_bsf_%_fuzzer.o: tools/target_bsf_fuzzer.c
>   $(COMPILE_C) -DFFMPEG_BSF=$*
>  
> diff --git a/tools/target_enc_fuzzer.c b/tools/target_enc_fuzzer.c
> new file mode 100644
> index 000..bc9f98c1443
> --- /dev/null
> +++ b/tools/target_enc_fuzzer.c
> @@ -0,0 +1,213 @@
> +/*
> + * Copyright (c) 2024 Michael Niedermayer 
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 
> USA
> + *
> + * Based on target_dec_fuzzer
> + */
> +
> +#include "config.h"
> +#include "libavutil/avassert.h"
> +#include "libavutil/avstring.h"
> +#include "libavutil/cpu.h"
> +#include "libavutil/imgutils.h"
> +#include "libavutil/intreadwrite.h"
> +#include "libavutil/mem.h"
> +
> +#include "libavcodec/avcodec.h"
> +#include "libavcodec/bytestream.h"
> +#include "libavcodec/codec_internal.h"
> +#include "libavformat/avformat.h"
> +
> +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
> +
> +extern const FFCodec * codec_list[];
> +
> +static void error(const char *err)
> +{
> +fprintf(stderr, "%s", err);
> +exit(1);
> +}
> +
> +static const FFCodec *c = NULL;

> +static const FFCodec *AVCodecInitialize(enum AVCodecID codec_id)

nit: snake_case, also the function is used once and the code can be
embedded in the code

[...]
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] tools: add target_enc_fuzzer.c

2024-04-19 Thread James Almer

On 4/19/2024 10:10 PM, Michael Niedermayer wrote:

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Michael Niedermayer 
---
  Makefile  |   3 +
  tools/Makefile|   3 +
  tools/target_enc_fuzzer.c | 213 ++
  3 files changed, 219 insertions(+)
  create mode 100644 tools/target_enc_fuzzer.c

diff --git a/Makefile b/Makefile
index b309dbc4db9..de727cbe00e 100644
--- a/Makefile
+++ b/Makefile
@@ -52,6 +52,9 @@ $(TOOLS): %$(EXESUF): %.o
  target_dec_%_fuzzer$(EXESUF): target_dec_%_fuzzer.o $(FF_DEP_LIBS)
$(LD) $(LDFLAGS) $(LDEXEFLAGS) $(LD_O) $^ $(ELIBS) $(FF_EXTRALIBS) 
$(LIBFUZZER_PATH)
  
+target_enc_%_fuzzer$(EXESUF): target_enc_%_fuzzer.o $(FF_DEP_LIBS)

+   $(LD) $(LDFLAGS) $(LDEXEFLAGS) $(LD_O) $^ $(ELIBS) $(FF_EXTRALIBS) 
$(LIBFUZZER_PATH)
+
  tools/target_bsf_%_fuzzer$(EXESUF): tools/target_bsf_%_fuzzer.o $(FF_DEP_LIBS)
$(LD) $(LDFLAGS) $(LDEXEFLAGS) $(LD_O) $^ $(ELIBS) $(FF_EXTRALIBS) 
$(LIBFUZZER_PATH)
  
diff --git a/tools/Makefile b/tools/Makefile

index 72e8e709a8d..2a11fa0ae62 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -5,6 +5,9 @@ TOOLS-$(CONFIG_ZLIB) += cws2fws
  tools/target_dec_%_fuzzer.o: tools/target_dec_fuzzer.c
$(COMPILE_C) -DFFMPEG_DECODER=$*
  
+tools/target_enc_%_fuzzer.o: tools/target_enc_fuzzer.c

+   $(COMPILE_C) -DFFMPEG_ENCODER=$*
+
  tools/target_bsf_%_fuzzer.o: tools/target_bsf_fuzzer.c
$(COMPILE_C) -DFFMPEG_BSF=$*
  
diff --git a/tools/target_enc_fuzzer.c b/tools/target_enc_fuzzer.c

new file mode 100644
index 000..bc9f98c1443
--- /dev/null
+++ b/tools/target_enc_fuzzer.c
@@ -0,0 +1,213 @@
+/*
+ * Copyright (c) 2024 Michael Niedermayer 
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Based on target_dec_fuzzer
+ */
+
+#include "config.h"
+#include "libavutil/avassert.h"
+#include "libavutil/avstring.h"
+#include "libavutil/cpu.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
+
+#include "libavcodec/avcodec.h"
+#include "libavcodec/bytestream.h"
+#include "libavcodec/codec_internal.h"
+#include "libavformat/avformat.h"
+
+int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
+
+extern const FFCodec * codec_list[];
+
+static void error(const char *err)
+{
+fprintf(stderr, "%s", err);
+exit(1);
+}
+
+static const FFCodec *c = NULL;
+static const FFCodec *AVCodecInitialize(enum AVCodecID codec_id)
+{
+const AVCodec *res;
+
+res = avcodec_find_decoder(codec_id);
+if (!res)
+error("Failed to find decoder");
+return ffcodec(res);
+}
+
+// Ensure we don't loop forever
+const uint32_t maxiteration = 8096;
+
+
+static int encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt)
+{
+int ret;
+
+ret = avcodec_send_frame(enc_ctx, frame);
+if (ret < 0)
+return ret;
+
+while (ret >= 0) {
+ret = avcodec_receive_packet(enc_ctx, pkt);
+if (ret == AVERROR(EAGAIN)) {
+return 0;
+} else if (ret < 0) {
+return ret;
+}
+
+av_packet_unref(pkt);
+}
+av_assert0(0);
+}
+
+int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
+uint64_t maxpixels_per_frame = 512 * 512;
+uint64_t maxpixels;
+
+uint64_t maxsamples;
+const uint8_t *end = data + size;
+uint32_t it = 0;
+uint64_t nb_samples = 0;
+AVDictionary *opts = NULL;
+
+if (!c) {
+#ifdef FFMPEG_ENCODER
+#define ENCODER_SYMBOL0(CODEC) ff_##CODEC##_encoder
+#define ENCODER_SYMBOL(CODEC) ENCODER_SYMBOL0(CODEC)
+extern FFCodec ENCODER_SYMBOL(FFMPEG_ENCODER);
+codec_list[0] = _SYMBOL(FFMPEG_ENCODER);
+
+c = _SYMBOL(FFMPEG_ENCODER);
+#else
+c = AVCodecInitialize(FFMPEG_CODEC);  // Done once.
+#endif
+av_log_set_level(AV_LOG_PANIC);
+}
+
+av_assert0(c->p.type == AVMEDIA_TYPE_VIDEO);
+
+maxpixels = maxpixels_per_frame * maxiteration;
+
+maxpixels_per_frame  = FFMIN(maxpixels_per_frame , maxpixels);
+
+AVCodecContext* ctx = avcodec_alloc_context3(>p);
+if (!ctx)
+error("Failed memory allocation");
+
+if (ctx->max_pixels == 0 || ctx->max_pixels > maxpixels_per_frame)
+ctx->max_pixels = maxpixels_per_frame; //To