Ticket #11692: target_sws_fuzzer1519.c

File target_sws_fuzzer1519.c, 7.2 KB (added by flyfish101, 12 months ago)

fuzz driver

Line 
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
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 "libavutil/opt.h"
30
31#include "libavcodec/bytestream.h"
32
33#include "libswscale/swscale.h"
34
35
36int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
37
38static void error(const char *err)
39{
40 fprintf(stderr, "%s", err);
41 exit(1);
42}
43
44static int alloc_plane(uint8_t *data[AV_VIDEO_MAX_PLANES], int stride[AV_VIDEO_MAX_PLANES], int w, int h, int format, int *hshift, int *vshift)
45{
46 size_t size[AV_VIDEO_MAX_PLANES];
47 ptrdiff_t ptrdiff_stride[AV_VIDEO_MAX_PLANES];
48 int ret = av_image_fill_linesizes(stride, format, w);
49 if (ret < 0)
50 return -1;
51
52 av_assert0(AV_VIDEO_MAX_PLANES == 4); // Some of the libavutil API has 4 hardcoded so this has undefined behaviour if its not 4
53
54 av_pix_fmt_get_chroma_sub_sample(format, hshift, vshift);
55
56 for(int p=0; p<AV_VIDEO_MAX_PLANES; p++) {
57 stride[p] =
58 ptrdiff_stride[p] = FFALIGN(stride[p], 32);
59 }
60 ret = av_image_fill_plane_sizes(size, format, h, ptrdiff_stride);
61 if (ret < 0)
62 return ret;
63
64 for(int p=0; p<AV_VIDEO_MAX_PLANES; p++) {
65 if (size[p]) {
66 data[p] = av_mallocz(size[p] + 32);
67 if (!data[p])
68 return -1;
69 } else
70 data[p] = NULL;
71 }
72 return 0;
73}
74
75static void free_plane(uint8_t *data[AV_VIDEO_MAX_PLANES])
76{
77 for(int p=0; p<AV_VIDEO_MAX_PLANES; p++)
78 av_freep(&data[p]);
79}
80
81static void mapres(unsigned *r0, unsigned *r1) {
82 double d = (double)(*r0*10ll - 9ll*UINT32_MAX) / UINT32_MAX;
83 double a = exp(d) * 16384 / exp(1) ;
84 int ai = (int)round(a);
85 uint64_t maxb = 16384 / ai;
86 *r0 = ai;
87 *r1 = 1 + (*r1 * maxb) / UINT32_MAX;
88}
89
90int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
91 int srcW= 48, srcH = 48;
92 int dstW= 48, dstH = 48;
93 int srcHShift, srcVShift;
94 int dstHShift, dstVShift;
95 unsigned flags = 1;
96 int srcStride[AV_VIDEO_MAX_PLANES] = {0};
97 int dstStride[AV_VIDEO_MAX_PLANES] = {0};
98 int ret;
99 const uint8_t *end = data + size;
100 enum AVPixelFormat srcFormat = AV_PIX_FMT_YUV420P;
101 enum AVPixelFormat dstFormat = AV_PIX_FMT_YUV420P;
102 uint8_t *src[AV_VIDEO_MAX_PLANES] = { 0 };
103 uint8_t *dst[AV_VIDEO_MAX_PLANES] = { 0 };
104 struct SwsContext *sws = NULL;
105 const AVPixFmtDescriptor *desc_src, *desc_dst;
106 const char *alphablend_mode = "none"; // Default value
107
108 if (size > 129) { // Increased from 128 to 129 to accommodate alphablend byte
109 GetByteContext gbc;
110 int64_t flags64;
111
112 size -= 129; // Increased from 128 to 129
113 bytestream2_init(&gbc, data + size, 129); // Increased from 128 to 129
114 srcW = bytestream2_get_le32(&gbc);
115 srcH = bytestream2_get_le32(&gbc);
116 dstW = bytestream2_get_le32(&gbc);
117 dstH = bytestream2_get_le32(&gbc);
118
119 mapres(&srcW, &srcH);
120 mapres(&dstW, &dstH);
121
122 flags = bytestream2_get_le32(&gbc);
123
124 unsigned mask = flags & (SWS_POINT |
125 SWS_AREA |
126 SWS_BILINEAR |
127 SWS_FAST_BILINEAR |
128 SWS_BICUBIC |
129 SWS_X |
130 SWS_GAUSS |
131 SWS_LANCZOS |
132 SWS_SINC |
133 SWS_SPLINE |
134 SWS_BICUBLIN);
135 mask &= flags;
136 if (mask && (mask & (mask -1)))
137 return 0; // multiple scalers are set, not possible
138
139 srcFormat = bytestream2_get_le32(&gbc) % AV_PIX_FMT_NB;
140 dstFormat = bytestream2_get_le32(&gbc) % AV_PIX_FMT_NB;
141
142 flags64 = bytestream2_get_le64(&gbc);
143 if (flags64 & 0x10)
144 av_force_cpu_flags(0);
145
146 // Read alphablend mode from input data
147 if (bytestream2_get_bytes_left(&gbc) > 0) {
148 int blend_choice = bytestream2_get_byte(&gbc) % 3;
149 switch (blend_choice) {
150 case 0:
151 alphablend_mode = "none";
152 break;
153 case 1:
154 alphablend_mode = "uniform_color";
155 break;
156 case 2:
157 alphablend_mode = "checkerboard";
158 break;
159 default:
160 alphablend_mode = "none";
161 break;
162 }
163 }
164
165 if (av_image_check_size(srcW, srcH, srcFormat, NULL) < 0)
166 srcW = srcH = 23;
167 if (av_image_check_size(dstW, dstH, dstFormat, NULL) < 0)
168 dstW = dstH = 23;
169 }
170
171 desc_src = av_pix_fmt_desc_get(srcFormat);
172 desc_dst = av_pix_fmt_desc_get(dstFormat);
173
174 fprintf(stderr, "%d x %d %s -> %d x %d %s (alphablend: %s)\n", srcW, srcH, desc_src->name, dstW, dstH, desc_dst->name, alphablend_mode);
175
176 ret = alloc_plane(src, srcStride, srcW, srcH, srcFormat, &srcHShift, &srcVShift);
177 if (ret < 0)
178 goto end;
179
180 ret = alloc_plane(dst, dstStride, dstW, dstH, dstFormat, &dstHShift, &dstVShift);
181 if (ret < 0)
182 goto end;
183
184
185 for(int p=0; p<AV_VIDEO_MAX_PLANES; p++) {
186 int psize = srcStride[p] * AV_CEIL_RSHIFT(srcH, (p == 1 || p == 2) ? srcVShift : 0);
187 if (psize > size)
188 psize = size;
189 if (psize) {
190 memcpy(src[p], data, psize);
191 data += psize;
192 size -= psize;
193 }
194 }
195
196 sws = sws_alloc_context();
197 if (!sws)
198 error("Failed sws allocation");
199
200 av_opt_set_int(sws, "sws_flags", flags, 0);
201 av_opt_set_int(sws, "srcw", srcW, 0);
202 av_opt_set_int(sws, "srch", srcH, 0);
203 av_opt_set_int(sws, "dstw", dstW, 0);
204 av_opt_set_int(sws, "dsth", dstH, 0);
205 av_opt_set_int(sws, "src_format", srcFormat, 0);
206 av_opt_set_int(sws, "dst_format", dstFormat, 0);
207 // Use the alphablend mode determined from input data
208 av_opt_set(sws, "alphablend", alphablend_mode, 0);
209
210 ret = sws_init_context(sws, NULL, NULL);
211 if (ret < 0)
212 goto end;
213
214 //TODO Slices
215 sws_scale(sws, (const uint8_t * const*)src, srcStride, 0, srcH, dst, dstStride);
216
217end:
218 sws_freeContext(sws);
219
220 free_plane(src);
221 free_plane(dst);
222
223 return 0;
224}