/src/libwebp/tests/fuzzer/enc_fuzzer.cc
Line | Count | Source |
1 | | // Copyright 2024 Google Inc. |
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 <cstddef> |
18 | | #include <cstdint> |
19 | | #include <cstdlib> |
20 | | #include <iostream> |
21 | | #include <limits> |
22 | | #include <memory> |
23 | | #include <string> |
24 | | #include <string_view> |
25 | | |
26 | | #include "gtest/gtest.h" |
27 | | #include "imageio/image_dec.h" |
28 | | #include "src/dsp/cpu.h" |
29 | | #include "tests/fuzzer/fuzz_utils.h" |
30 | | #include "webp/decode.h" |
31 | | #include "webp/encode.h" |
32 | | #include "webp/types.h" |
33 | | |
34 | | namespace { |
35 | | |
36 | | const VP8CPUInfo default_VP8GetCPUInfo = fuzz_utils::VP8GetCPUInfo; |
37 | | |
38 | | void EncTestImpl(WebPPicture& pic, uint32_t optimization_index, bool use_argb, |
39 | | const WebPConfig& config, |
40 | 12.7k | const fuzz_utils::CropOrScaleParams& crop_or_scale_params) { |
41 | 12.7k | fuzz_utils::SetOptimization(default_VP8GetCPUInfo, optimization_index); |
42 | | |
43 | | // Crop and scale. |
44 | 12.7k | if (!CropOrScale(&pic, crop_or_scale_params)) { |
45 | 0 | const WebPEncodingError error_code = pic.error_code; |
46 | 0 | if (error_code == VP8_ENC_ERROR_OUT_OF_MEMORY) return; |
47 | 0 | std::cerr << "CropOrScale failed. Error code: " << error_code << "\n"; |
48 | 0 | std::abort(); |
49 | 0 | } |
50 | | |
51 | | // Skip slow settings on big images, it's likely to timeout. |
52 | 12.7k | if (pic.width * pic.height > 32 * 32) { |
53 | 1.87k | if (config.quality > 99.0f && |
54 | 62 | ((config.lossless && config.method >= 5) || config.method == 6)) { |
55 | 5 | return; |
56 | 5 | } |
57 | 1.87k | if (config.alpha_quality == 100 && config.method == 6) { |
58 | 6 | return; |
59 | 6 | } |
60 | 1.87k | } |
61 | | |
62 | | // Encode. |
63 | 12.7k | WebPMemoryWriter memory_writer; |
64 | 12.7k | WebPMemoryWriterInit(&memory_writer); |
65 | 12.7k | pic.writer = WebPMemoryWrite; |
66 | 12.7k | pic.custom_ptr = &memory_writer; |
67 | 12.7k | if (!WebPEncode(&config, &pic)) { |
68 | 0 | const WebPEncodingError error_code = pic.error_code; |
69 | 0 | WebPMemoryWriterClear(&memory_writer); |
70 | 0 | if (error_code == VP8_ENC_ERROR_OUT_OF_MEMORY) return; |
71 | 0 | std::cerr << "WebPEncode failed. Error code: " << error_code << "\n"; |
72 | 0 | std::abort(); |
73 | 0 | } |
74 | | |
75 | | // Try decoding the result. |
76 | 12.7k | int w, h; |
77 | 12.7k | const uint8_t* const out_data = memory_writer.mem; |
78 | 12.7k | const size_t out_size = memory_writer.size; |
79 | 12.7k | uint8_t* const rgba = WebPDecodeBGRA(out_data, out_size, &w, &h); |
80 | 12.7k | if (rgba == nullptr || w != pic.width || h != pic.height) { |
81 | 0 | std::cerr << "WebPDecodeBGRA failed.\n"; |
82 | 0 | WebPFree(rgba); |
83 | 0 | WebPMemoryWriterClear(&memory_writer); |
84 | 0 | std::abort(); |
85 | 0 | } |
86 | | |
87 | | // Compare the results if exact encoding. |
88 | 12.7k | if (pic.use_argb && config.lossless && config.near_lossless == 100) { |
89 | 814 | const uint32_t* src1 = (const uint32_t*)rgba; |
90 | 814 | const uint32_t* src2 = pic.argb; |
91 | 30.3k | for (int y = 0; y < h; ++y, src1 += w, src2 += pic.argb_stride) { |
92 | 3.06M | for (int x = 0; x < w; ++x) { |
93 | 3.03M | uint32_t v1 = src1[x], v2 = src2[x]; |
94 | 3.03M | if (!config.exact) { |
95 | 1.70M | if ((v1 & 0xff000000u) == 0 || (v2 & 0xff000000u) == 0) { |
96 | | // Only keep alpha for comparison of fully transparent area. |
97 | 527 | v1 &= 0xff000000u; |
98 | 527 | v2 &= 0xff000000u; |
99 | 527 | } |
100 | 1.70M | } |
101 | 3.03M | if (v1 != v2) { |
102 | 0 | std::cerr << "Lossless compression failed pixel-exactness.\n"; |
103 | 0 | WebPFree(rgba); |
104 | 0 | WebPMemoryWriterClear(&memory_writer); |
105 | 0 | std::abort(); |
106 | 0 | } |
107 | 3.03M | } |
108 | 29.5k | } |
109 | 814 | } |
110 | | |
111 | 12.7k | WebPFree(rgba); |
112 | 12.7k | WebPMemoryWriterClear(&memory_writer); |
113 | 12.7k | WebPPictureFree(&pic); |
114 | 12.7k | } |
115 | | |
116 | | // For open source compatibility, maybe_unused is necessary. |
117 | | [[maybe_unused]] void EncFileTest( |
118 | | std::string_view file, uint32_t optimization_index, bool use_argb, |
119 | | WebPConfig config, |
120 | 0 | const fuzz_utils::CropOrScaleParams& crop_or_scale_params) { |
121 | 0 | // Init the source picture. |
122 | 0 | WebPPicture pic; |
123 | 0 | if (!WebPPictureInit(&pic)) { |
124 | 0 | std::cerr << "WebPPictureInit failed.\n"; |
125 | 0 | std::abort(); |
126 | 0 | } |
127 | 0 | pic.use_argb = use_argb; |
128 | 0 |
|
129 | 0 | const uint8_t* const file_data = |
130 | 0 | reinterpret_cast<const uint8_t*>(file.data()); |
131 | 0 | if (fuzz_utils::IsImageTooBig(file_data, file.size())) return; |
132 | 0 | WebPImageReader reader = WebPGuessImageReader(file_data, file.size()); |
133 | 0 | if (!reader(file_data, file.size(), &pic, 1, NULL)) return; |
134 | 0 |
|
135 | 0 | EncTestImpl(pic, optimization_index, use_argb, config, crop_or_scale_params); |
136 | 0 | } |
137 | | |
138 | | void EncArbitraryTest( |
139 | | fuzz_utils::WebPPictureCpp pic_cpp, uint32_t optimization_index, |
140 | | bool use_argb, const WebPConfig& config, |
141 | 12.7k | const fuzz_utils::CropOrScaleParams& crop_or_scale_params) { |
142 | 12.7k | WebPPicture& pic = pic_cpp.ref(); |
143 | | |
144 | 12.7k | EncTestImpl(pic, optimization_index, use_argb, config, crop_or_scale_params); |
145 | 12.7k | } |
146 | | |
147 | | } // namespace |
148 | | |
149 | | FUZZ_TEST(Enc, EncArbitraryTest) |
150 | | .WithDomains(fuzz_utils::ArbitraryWebPPicture(), |
151 | | /*optimization_index=*/ |
152 | | fuzztest::InRange<uint32_t>(0, |
153 | | fuzz_utils::kMaxOptimizationIndex), |
154 | | /*use_argb=*/fuzztest::Arbitrary<bool>(), |
155 | | fuzz_utils::ArbitraryWebPConfig(), |
156 | | fuzz_utils::ArbitraryCropOrScaleParams()); |
157 | | |
158 | | // NaN compares false with everything, so it used to pass the range checks of |
159 | | // WebPValidateConfig() and reach float to int casts. |
160 | 0 | TEST(Enc, ValidateConfigRejectsNaN) { |
161 | 0 | const float kNaN = std::numeric_limits<float>::quiet_NaN(); |
162 | 0 | WebPConfig config; |
163 | 0 | ASSERT_TRUE(WebPConfigInit(&config)); |
164 | 0 | EXPECT_TRUE(WebPValidateConfig(&config)); |
165 | 0 | config.quality = kNaN; |
166 | 0 | EXPECT_FALSE(WebPValidateConfig(&config)); |
167 | 0 | ASSERT_TRUE(WebPConfigInit(&config)); |
168 | 0 | config.target_PSNR = kNaN; |
169 | 0 | EXPECT_FALSE(WebPValidateConfig(&config)); |
170 | |
|
171 | 0 | for (const int lossless : {0, 1}) { |
172 | 0 | ASSERT_TRUE(WebPConfigInit(&config)); |
173 | 0 | config.lossless = lossless; |
174 | 0 | config.quality = kNaN; |
175 | 0 | WebPPicture pic; |
176 | 0 | ASSERT_TRUE(WebPPictureInit(&pic)); |
177 | 0 | pic.use_argb = lossless; |
178 | 0 | pic.width = 32; |
179 | 0 | pic.height = 32; |
180 | 0 | ASSERT_TRUE(WebPPictureAlloc(&pic)); |
181 | 0 | WebPMemoryWriter writer; |
182 | 0 | WebPMemoryWriterInit(&writer); |
183 | 0 | pic.writer = WebPMemoryWrite; |
184 | 0 | pic.custom_ptr = &writer; |
185 | 0 | EXPECT_FALSE(WebPEncode(&config, &pic)); |
186 | 0 | EXPECT_EQ(pic.error_code, VP8_ENC_ERROR_INVALID_CONFIGURATION); |
187 | 0 | WebPMemoryWriterClear(&writer); |
188 | 0 | WebPPictureFree(&pic); |
189 | 0 | } |
190 | 0 | } |