/src/libwebp/tests/fuzzer/fuzz_utils.cc
Line | Count | Source |
1 | | // Copyright 2024 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | // |
15 | | //////////////////////////////////////////////////////////////////////////////// |
16 | | |
17 | | #include "./fuzz_utils.h" |
18 | | |
19 | | #include <algorithm> |
20 | | #include <cassert> |
21 | | #include <cctype> |
22 | | #include <cstddef> |
23 | | #include <cstdint> |
24 | | #include <cstdlib> |
25 | | #include <cstring> |
26 | | #include <filesystem> // NOLINT |
27 | | #include <fstream> |
28 | | #include <initializer_list> |
29 | | #include <iostream> |
30 | | #include <memory> |
31 | | #include <string> |
32 | | #include <string_view> |
33 | | #include <tuple> |
34 | | #include <utility> |
35 | | #include <vector> |
36 | | |
37 | | #include "./img_alpha.h" |
38 | | #include "./img_grid.h" |
39 | | #include "./img_peak.h" |
40 | | #include "imageio/jpegdec.h" |
41 | | #include "imageio/pngdec.h" |
42 | | #include "imageio/pnmdec.h" |
43 | | #include "imageio/tiffdec.h" |
44 | | #include "src/dsp/cpu.h" |
45 | | #include "webp/decode.h" |
46 | | #include "webp/encode.h" |
47 | | #include "webp/types.h" |
48 | | |
49 | | namespace fuzz_utils { |
50 | | |
51 | 27.4k | WebPPicture GetSourcePicture(int image_index, bool use_argb) { |
52 | 27.4k | WebPPicture pic; |
53 | 27.4k | if (!WebPPictureInit(&pic)) std::abort(); |
54 | 27.4k | pic.use_argb = use_argb; |
55 | | |
56 | | // Pick a source picture. |
57 | 27.4k | const int kImagesWidth[] = {kImgAlphaWidth, kImgGridWidth, kImgPeakWidth}; |
58 | 27.4k | const int kImagesHeight[] = {kImgAlphaHeight, kImgGridHeight, kImgPeakHeight}; |
59 | 27.4k | const uint8_t* const image_data = kImagesData[image_index]; |
60 | 27.4k | pic.width = kImagesWidth[image_index]; |
61 | 27.4k | pic.height = kImagesHeight[image_index]; |
62 | 27.4k | pic.argb_stride = pic.width * 4 * sizeof(uint8_t); |
63 | | |
64 | | // Read the bytes. |
65 | 27.4k | if (!WebPPictureImportRGBA(&pic, image_data, pic.argb_stride)) std::abort(); |
66 | 27.4k | return pic; |
67 | 27.4k | } |
68 | | |
69 | | //------------------------------------------------------------------------------ |
70 | | |
71 | 124k | int CropOrScale(WebPPicture* const pic, const CropOrScaleParams& params) { |
72 | 124k | if (pic == NULL) return 0; |
73 | 124k | #if !defined(WEBP_REDUCE_SIZE) |
74 | 124k | if (params.alter_input) { |
75 | 55.1k | if (params.crop_or_scale) { |
76 | 13.2k | const int cropped_width = std::max(1, pic->width / params.width_ratio); |
77 | 13.2k | const int cropped_height = std::max(1, pic->height / params.height_ratio); |
78 | 13.2k | const int cropped_left = (pic->width - cropped_width) / params.left_ratio; |
79 | 13.2k | const int cropped_top = (pic->height - cropped_height) / params.top_ratio; |
80 | 13.2k | return WebPPictureCrop(pic, cropped_left, cropped_top, cropped_width, |
81 | 13.2k | cropped_height); |
82 | 41.9k | } else { |
83 | 41.9k | const int scaled_width = 1 + (pic->width * params.width_ratio) / 8; |
84 | 41.9k | const int scaled_height = 1 + (pic->height * params.height_ratio) / 8; |
85 | 41.9k | return WebPPictureRescale(pic, scaled_width, scaled_height); |
86 | 41.9k | } |
87 | 55.1k | } |
88 | | #else // defined(WEBP_REDUCE_SIZE) |
89 | | (void)pic; |
90 | | (void)params; |
91 | | #endif // !defined(WEBP_REDUCE_SIZE) |
92 | 69.4k | return 1; |
93 | 124k | } |
94 | | |
95 | | extern "C" VP8CPUInfo VP8GetCPUInfo; |
96 | | static VP8CPUInfo GetCPUInfo; |
97 | | |
98 | 205k | static WEBP_INLINE int GetCPUInfoNoSSE41(CPUFeature feature) { |
99 | 205k | if (feature == kSSE4_1 || feature == kAVX || feature == kAVX2) return 0; |
100 | 119k | return GetCPUInfo(feature); |
101 | 205k | } |
102 | | |
103 | 166k | static WEBP_INLINE int GetCPUInfoNoAVX(CPUFeature feature) { |
104 | 166k | if (feature == kAVX || feature == kAVX2) return 0; |
105 | 147k | return GetCPUInfo(feature); |
106 | 166k | } |
107 | | |
108 | 206k | static WEBP_INLINE int GetCPUInfoForceSlowSSSE3(CPUFeature feature) { |
109 | 206k | if (feature == kSlowSSSE3 && GetCPUInfo(kSSE3)) { |
110 | 6.23k | return 1; // we have SSE3 -> force SlowSSSE3 |
111 | 6.23k | } |
112 | 199k | return GetCPUInfo(feature); |
113 | 206k | } |
114 | | |
115 | 298k | static WEBP_INLINE int GetCPUInfoOnlyC(CPUFeature feature) { |
116 | 298k | (void)feature; |
117 | 298k | return 0; |
118 | 298k | } |
119 | | |
120 | 226k | void SetOptimization(VP8CPUInfo default_VP8GetCPUInfo, uint32_t index) { |
121 | 226k | assert(index <= kMaxOptimizationIndex); |
122 | 226k | GetCPUInfo = default_VP8GetCPUInfo; |
123 | 226k | const VP8CPUInfo kVP8CPUInfos[kMaxOptimizationIndex + 1] = { |
124 | 226k | GetCPUInfoOnlyC, GetCPUInfoForceSlowSSSE3, GetCPUInfoNoSSE41, |
125 | 226k | GetCPUInfoNoAVX, GetCPUInfo}; |
126 | 226k | VP8GetCPUInfo = kVP8CPUInfos[index]; |
127 | 226k | } |
128 | | |
129 | | //------------------------------------------------------------------------------ |
130 | | |
131 | | std::vector<std::string> GetDictionaryFromFiles( |
132 | 0 | const std::vector<std::string_view>& file_paths) { |
133 | 0 | std::vector<std::string> lines; |
134 | 0 | for (std::string_view file_path : file_paths) { |
135 | 0 | std::ifstream dictfile{std::string(file_path)}; |
136 | 0 | if (!dictfile.is_open()) { |
137 | 0 | std::cerr << file_path << " cannot be opened\n"; |
138 | 0 | std::abort(); |
139 | 0 | } |
140 | 0 | for (std::string line; std::getline(dictfile, line);) { |
141 | 0 | if (line.empty() || line[0] == '#') { |
142 | 0 | continue; |
143 | 0 | } |
144 | 0 | lines.push_back(line); |
145 | 0 | } |
146 | 0 | } |
147 | 0 | return lines; |
148 | 0 | } |
149 | | |
150 | | namespace { |
151 | | |
152 | 0 | bool ShouldIncludeSeedFile(std::string_view path, FuzzSeedFormat format) { |
153 | 0 | const size_t slash = path.find_last_of("/\\"); |
154 | 0 | const std::string_view filename = |
155 | 0 | (slash == std::string_view::npos) ? path : path.substr(slash + 1); |
156 | 0 | const size_t dot = filename.rfind('.'); |
157 | 0 | const std::string_view ext = (dot == std::string_view::npos || dot == 0) |
158 | 0 | ? std::string_view() |
159 | 0 | : filename.substr(dot); |
160 | 0 | if (ext.empty()) return true; |
161 | 0 | std::string lower_ext(ext); |
162 | 0 | for (char& c : lower_ext) { |
163 | 0 | c = static_cast<char>(std::tolower(static_cast<unsigned char>(c))); |
164 | 0 | } |
165 | 0 | switch (format) { |
166 | 0 | case FuzzSeedFormat::kWebPOnly: |
167 | 0 | return lower_ext == ".webp"; |
168 | 0 | case FuzzSeedFormat::kWebPAndGif: |
169 | 0 | return lower_ext == ".webp" || lower_ext == ".gif"; |
170 | 0 | case FuzzSeedFormat::kImageIo: |
171 | 0 | for (std::string_view allowed : |
172 | 0 | {".webp", ".png", ".jpg", ".jpeg", ".ppm", ".pgm", ".pbm", ".pam", |
173 | 0 | ".pnm", ".tif", ".tiff", ".bin"}) { |
174 | 0 | if (lower_ext == allowed) return true; |
175 | 0 | } |
176 | 0 | return false; |
177 | 0 | } |
178 | 0 | return false; |
179 | 0 | } |
180 | | |
181 | | } // namespace |
182 | | |
183 | | std::vector<std::string> ReadFilesFromDirectory(std::string_view dir, |
184 | 41 | FuzzSeedFormat format) { |
185 | 41 | if (!std::filesystem::is_directory(dir)) return {}; |
186 | 0 | std::vector<std::tuple<std::string>> tuples = |
187 | 0 | fuzztest::ReadFilesFromDirectory(dir, [format](std::string_view path) { |
188 | 0 | return ShouldIncludeSeedFile(path, format); |
189 | 0 | }); |
190 | 0 | std::vector<std::string> strings(tuples.size()); |
191 | 0 | for (size_t i = 0; i < tuples.size(); ++i) { |
192 | 0 | using std::swap; |
193 | 0 | swap(std::get<0>(tuples[i]), strings[i]); |
194 | 0 | } |
195 | 0 | return strings; |
196 | 41 | } |
197 | | |
198 | | #if !defined(WEBP_HAS_CUSTOM_FUZZ_SEEDS) |
199 | 35 | std::vector<std::string> DefaultDictionary(FuzzSeedFormat /*format*/) { |
200 | 70 | for (std::string_view path : {"tests/fuzzer/fuzz.dict", "fuzz.dict"}) { |
201 | 70 | if (std::filesystem::is_regular_file(path)) { |
202 | 0 | return GetDictionaryFromFiles({path}); |
203 | 0 | } |
204 | 70 | } |
205 | 35 | return {}; |
206 | 35 | } |
207 | | |
208 | 41 | std::vector<std::string> DefaultSeeds(FuzzSeedFormat format) { |
209 | 41 | std::vector<std::string> seeds; |
210 | 41 | if (const char* const dir = std::getenv("TEST_DATA_DIRS"); |
211 | 41 | dir != nullptr && *dir != '\0') { |
212 | 41 | seeds = ReadFilesFromDirectory(dir, format); |
213 | 41 | } |
214 | 41 | if (seeds.empty()) seeds.push_back(""); |
215 | 41 | return seeds; |
216 | 41 | } |
217 | | #endif // !defined(WEBP_HAS_CUSTOM_FUZZ_SEEDS) |
218 | | |
219 | | //------------------------------------------------------------------------------ |
220 | | |
221 | 32.2k | bool IsImageTooBig(const uint8_t* data, size_t size) { |
222 | 32.2k | int width = 0, height = 0; |
223 | 32.2k | if (ReadJpegDimensions(data, size, &width, &height) || |
224 | 32.2k | WebPGetInfo(data, size, &width, &height) || |
225 | 9.09k | ReadPngDimensions(data, size, &width, &height) || |
226 | 9.09k | ReadPnmDimensions(data, size, &width, &height) || |
227 | 24.1k | ReadTiffDimensions(data, size, &width, &height)) { |
228 | 24.1k | if (width <= 0 || height <= 0) return false; |
229 | | // Look at the number of 8x8px blocks rather than the overall pixel count |
230 | | // when comparing to memory and duration thresholds. |
231 | 24.1k | const uint64_t ceiled_width = ((uint64_t)width + 7) / 8 * 8; |
232 | 24.1k | const uint64_t ceiled_height = ((uint64_t)height + 7) / 8 * 8; |
233 | | // Threshold to avoid out-of-memory and timeout issues. |
234 | | // The threshold is arbitrary but below the fuzzer limit of 2 GB. |
235 | | // The value cannot be 2 GB because of the added memory by MSAN. |
236 | 24.1k | if (ceiled_width > kFuzzPxLimit || ceiled_height > kFuzzPxLimit || |
237 | 23.9k | ceiled_width * ceiled_height > kFuzzPxLimit) { |
238 | 418 | return true; |
239 | 418 | } |
240 | 24.1k | } |
241 | 31.7k | return false; |
242 | 32.2k | } |
243 | | |
244 | 33.9k | bool CompareDecBuffers(const WebPDecBuffer& a, const WebPDecBuffer& b) { |
245 | 33.9k | if (a.colorspace != b.colorspace || a.width != b.width || |
246 | 33.9k | a.height != b.height) { |
247 | 0 | return false; |
248 | 0 | } |
249 | 33.9k | if (WebPIsRGBMode(a.colorspace)) { |
250 | 30.1k | return a.u.RGBA.size == b.u.RGBA.size && |
251 | 30.1k | std::memcmp(a.u.RGBA.rgba, b.u.RGBA.rgba, a.u.RGBA.size) == 0; |
252 | 30.1k | } |
253 | 3.76k | if (a.u.YUVA.y_size != b.u.YUVA.y_size || |
254 | 3.76k | a.u.YUVA.u_size != b.u.YUVA.u_size || |
255 | 3.76k | a.u.YUVA.v_size != b.u.YUVA.v_size || |
256 | 3.76k | std::memcmp(a.u.YUVA.y, b.u.YUVA.y, a.u.YUVA.y_size) != 0 || |
257 | 3.76k | std::memcmp(a.u.YUVA.u, b.u.YUVA.u, a.u.YUVA.u_size) != 0 || |
258 | 3.76k | std::memcmp(a.u.YUVA.v, b.u.YUVA.v, a.u.YUVA.v_size) != 0) { |
259 | 0 | return false; |
260 | 0 | } |
261 | 3.76k | if (a.u.YUVA.a != nullptr || b.u.YUVA.a != nullptr) { |
262 | 2.57k | return a.u.YUVA.a != nullptr && b.u.YUVA.a != nullptr && |
263 | 2.57k | a.u.YUVA.a_size == b.u.YUVA.a_size && |
264 | 2.57k | std::memcmp(a.u.YUVA.a, b.u.YUVA.a, a.u.YUVA.a_size) == 0; |
265 | 2.57k | } |
266 | 1.19k | return true; |
267 | 3.76k | } |
268 | | |
269 | | namespace { |
270 | | |
271 | | bool CropOrScaleAndEncodeOne(const CropOrScaleParams& crop_or_scale_params, |
272 | | WebPConfig& config, WebPPicture& pic, |
273 | 111k | WebPMemoryWriter& memory_writer) { |
274 | | // Crop and scale. |
275 | 111k | if (!CropOrScale(&pic, crop_or_scale_params)) { |
276 | 0 | const WebPEncodingError error_code = pic.error_code; |
277 | 0 | if (error_code == VP8_ENC_ERROR_OUT_OF_MEMORY) return false; |
278 | 0 | std::cerr << "CropOrScale failed. Error code: " << error_code << "\n"; |
279 | 0 | std::abort(); |
280 | 0 | } |
281 | | |
282 | | // Skip slow settings on big images, it's likely to timeout. |
283 | 111k | if (pic.width * pic.height > 32 * 32) { |
284 | 18.8k | if (config.lossless) { |
285 | 8.29k | if (config.quality > 99.0f && config.method >= 5) { |
286 | 392 | config.quality = 99.0f; |
287 | 392 | config.method = 5; |
288 | 392 | } |
289 | 10.5k | } else { |
290 | 10.5k | if (config.quality > 99.0f && config.method == 6) { |
291 | 248 | config.quality = 99.0f; |
292 | 248 | } |
293 | 10.5k | } |
294 | 18.8k | if (config.alpha_quality == 100 && config.method == 6) { |
295 | 186 | config.alpha_quality = 99; |
296 | 186 | } |
297 | 18.8k | } |
298 | | |
299 | | // Encode. |
300 | 111k | pic.writer = WebPMemoryWrite; |
301 | 111k | pic.custom_ptr = &memory_writer; |
302 | 111k | if (!WebPEncode(&config, &pic)) { |
303 | 0 | const WebPEncodingError error_code = pic.error_code; |
304 | 0 | if (error_code == VP8_ENC_ERROR_OUT_OF_MEMORY || |
305 | 0 | error_code == VP8_ENC_ERROR_BAD_WRITE) { |
306 | 0 | return false; |
307 | 0 | } |
308 | 0 | std::cerr << "WebPEncode failed. Error code: " << error_code << "\n"; |
309 | 0 | std::abort(); |
310 | 0 | } |
311 | 111k | return true; |
312 | 111k | } |
313 | | |
314 | | } // namespace |
315 | | |
316 | | bool CropOrScaleAndEncode(const CropOrScaleParams& crop_or_scale_params, |
317 | | WebPConfig& config, WebPPicture& pic, |
318 | | VP8CPUInfo default_VP8GetCPUInfo, |
319 | | uint32_t optimization_index, |
320 | 55.5k | WebPMemoryWriter& memory_writer) { |
321 | 55.5k | WebPPicture pic_ref; |
322 | 55.5k | if (!WebPPictureInit(&pic_ref)) { |
323 | 0 | std::cerr << "WebPPictureInit failed.\n"; |
324 | 0 | std::abort(); |
325 | 0 | } |
326 | 55.5k | std::unique_ptr<WebPPicture, UniquePtrDeleter> pic_ref_owner(&pic_ref); |
327 | 55.5k | if (!WebPPictureCopy(&pic, &pic_ref)) return false; |
328 | 55.5k | WebPConfig config_ref = config; |
329 | | |
330 | 55.5k | SetOptimization(default_VP8GetCPUInfo, optimization_index); |
331 | 55.5k | if (!CropOrScaleAndEncodeOne(crop_or_scale_params, config, pic, |
332 | 55.5k | memory_writer)) { |
333 | 0 | return false; |
334 | 0 | } |
335 | | |
336 | | // Verify that encoding produces the exact same bitstream independently of the |
337 | | // optimization level. |
338 | 55.5k | const uint32_t ref_optimization_index = |
339 | 55.5k | (optimization_index == kMaxOptimizationIndex) ? 0 : kMaxOptimizationIndex; |
340 | 55.5k | SetOptimization(default_VP8GetCPUInfo, ref_optimization_index); |
341 | 55.5k | WebPMemoryWriter ref_memory_writer; |
342 | 55.5k | WebPMemoryWriterInit(&ref_memory_writer); |
343 | 55.5k | std::unique_ptr<WebPMemoryWriter, UniquePtrDeleter> ref_memory_writer_owner( |
344 | 55.5k | &ref_memory_writer); |
345 | 55.5k | if (!CropOrScaleAndEncodeOne(crop_or_scale_params, config_ref, pic_ref, |
346 | 55.5k | ref_memory_writer)) { |
347 | 0 | return false; |
348 | 0 | } |
349 | 55.5k | if (memory_writer.size != ref_memory_writer.size || |
350 | 55.5k | std::memcmp(memory_writer.mem, ref_memory_writer.mem, |
351 | 55.5k | memory_writer.size) != 0) { |
352 | 0 | std::cerr << "Encoded bitstream mismatch between optimizations " |
353 | 0 | << optimization_index << " and " << ref_optimization_index |
354 | 0 | << ".\n"; |
355 | 0 | std::abort(); |
356 | 0 | } |
357 | 55.5k | SetOptimization(default_VP8GetCPUInfo, optimization_index); |
358 | 55.5k | return true; |
359 | 55.5k | } |
360 | | |
361 | | void CompareDecodeOptimizations(VP8CPUInfo default_VP8GetCPUInfo, |
362 | | uint32_t optimization_index, |
363 | | const uint8_t* data, size_t size, |
364 | 17.0k | const WebPDecoderConfig& config) { |
365 | 17.0k | const uint32_t ref_optimization_index = |
366 | 17.0k | (optimization_index == kMaxOptimizationIndex) ? 0 : kMaxOptimizationIndex; |
367 | 17.0k | SetOptimization(default_VP8GetCPUInfo, ref_optimization_index); |
368 | 17.0k | WebPDecoderConfig ref_config; |
369 | 17.0k | std::unique_ptr<WebPDecoderConfig, UniquePtrDeleter> ref_config_owner( |
370 | 17.0k | &ref_config); |
371 | 17.0k | if (!WebPInitDecoderConfig(&ref_config)) { |
372 | 0 | std::cerr << "WebPInitDecoderConfig failed.\n"; |
373 | 0 | std::abort(); |
374 | 0 | } |
375 | 17.0k | ref_config.output.colorspace = config.output.colorspace; |
376 | 17.0k | ref_config.options = config.options; |
377 | 17.0k | const VP8StatusCode ref_status = WebPDecode(data, size, &ref_config); |
378 | 17.0k | SetOptimization(default_VP8GetCPUInfo, optimization_index); |
379 | 17.0k | if (ref_status != VP8_STATUS_OK && ref_status != VP8_STATUS_OUT_OF_MEMORY && |
380 | 0 | ref_status != VP8_STATUS_USER_ABORT) { |
381 | 0 | std::cerr << "WebPDecode failed. status: " << ref_status << ".\n"; |
382 | 0 | std::abort(); |
383 | 0 | } |
384 | 17.0k | if (ref_status == VP8_STATUS_OK && |
385 | 17.0k | !CompareDecBuffers(config.output, ref_config.output)) { |
386 | 0 | std::cerr << "Decoded output mismatch between optimizations " |
387 | 0 | << optimization_index << " and " << ref_optimization_index |
388 | 0 | << ".\n"; |
389 | 0 | std::abort(); |
390 | 0 | } |
391 | 17.0k | } |
392 | | |
393 | | } // namespace fuzz_utils |