/src/libwebp/tests/fuzzer/advanced_api_fuzzer.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2018 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 <algorithm> |
18 | | #include <cstddef> |
19 | | #include <cstdint> |
20 | | #include <string_view> |
21 | | |
22 | | #include "./fuzz_utils.h" |
23 | | #include "src/dec/webpi_dec.h" |
24 | | #include "src/utils/rescaler_utils.h" |
25 | | #include "src/webp/decode.h" |
26 | | |
27 | | namespace { |
28 | | |
29 | | void AdvancedApiTest(std::string_view blob, uint8_t factor_u8, bool flip, |
30 | | bool bypass_filtering, bool no_fancy_upsampling, |
31 | | bool use_threads, bool use_cropping, bool use_scaling, |
32 | 8.54k | bool use_dithering, int colorspace, bool incremental) { |
33 | 8.54k | WebPDecoderConfig config; |
34 | 8.54k | if (!WebPInitDecoderConfig(&config)) return; |
35 | 8.54k | const uint8_t* const data = reinterpret_cast<const uint8_t*>(blob.data()); |
36 | 8.54k | const size_t size = blob.size(); |
37 | 8.54k | if (WebPGetFeatures(data, size, &config.input) != VP8_STATUS_OK) return; |
38 | 8.30k | if ((size_t)config.input.width * config.input.height > |
39 | 8.30k | fuzz_utils::kFuzzPxLimit) { |
40 | 14 | return; |
41 | 14 | } |
42 | | |
43 | | // Using two independent criteria ensures that all combinations of options |
44 | | // can reach each path at the decoding stage, with meaningful differences. |
45 | | |
46 | 8.29k | const uint8_t value = fuzz_utils::FuzzHash(data, size); |
47 | 8.29k | const float factor = factor_u8 / 255.f; // 0-1 |
48 | | |
49 | 8.29k | config.options.flip = flip; |
50 | 8.29k | config.options.bypass_filtering = bypass_filtering; |
51 | 8.29k | config.options.no_fancy_upsampling = no_fancy_upsampling; |
52 | 8.29k | config.options.use_threads = use_threads; |
53 | 8.29k | if (use_cropping) { |
54 | 3.81k | config.options.use_cropping = 1; |
55 | 3.81k | config.options.crop_width = (int)(config.input.width * (1 - factor)); |
56 | 3.81k | config.options.crop_height = (int)(config.input.height * (1 - factor)); |
57 | 3.81k | config.options.crop_left = config.input.width - config.options.crop_width; |
58 | 3.81k | config.options.crop_top = config.input.height - config.options.crop_height; |
59 | 3.81k | } |
60 | 8.29k | if (use_dithering) { |
61 | 4.40k | int strength = (int)(factor * 100); |
62 | 4.40k | config.options.dithering_strength = strength; |
63 | 4.40k | config.options.alpha_dithering_strength = 100 - strength; |
64 | 4.40k | } |
65 | 8.29k | if (use_scaling) { |
66 | 3.68k | config.options.use_scaling = 1; |
67 | 3.68k | config.options.scaled_width = (int)(config.input.width * factor * 2); |
68 | 3.68k | config.options.scaled_height = (int)(config.input.height * factor * 2); |
69 | 3.68k | } |
70 | 8.29k | config.output.colorspace = static_cast<WEBP_CSP_MODE>(colorspace); |
71 | | |
72 | 17.7k | for (int i = 0; i < 2; ++i) { |
73 | 16.5k | if (i == 1) { |
74 | | // Use the bitstream data to generate extreme ranges for the options. An |
75 | | // alternative approach would be to use a custom corpus containing webp |
76 | | // files prepended with sizeof(config.options) zeroes to allow the fuzzer |
77 | | // to modify these independently. |
78 | 8.29k | const int data_offset = 50; |
79 | 8.29k | if (data_offset + sizeof(config.options) >= size) break; |
80 | 1.34k | memcpy(&config.options, data + data_offset, sizeof(config.options)); |
81 | | |
82 | | // Skip easily avoidable out-of-memory fuzzing errors. |
83 | 1.34k | if (config.options.use_scaling) { |
84 | 817 | int input_width = config.input.width; |
85 | 817 | int input_height = config.input.height; |
86 | 817 | if (config.options.use_cropping) { |
87 | 668 | const int cw = config.options.crop_width; |
88 | 668 | const int ch = config.options.crop_height; |
89 | 668 | const int x = config.options.crop_left & ~1; |
90 | 668 | const int y = config.options.crop_top & ~1; |
91 | 668 | if (WebPCheckCropDimensions(input_width, input_height, x, y, cw, |
92 | 668 | ch)) { |
93 | 4 | input_width = cw; |
94 | 4 | input_height = ch; |
95 | 4 | } |
96 | 668 | } |
97 | | |
98 | 817 | int scaled_width = config.options.scaled_width; |
99 | 817 | int scaled_height = config.options.scaled_height; |
100 | 817 | if (WebPRescalerGetScaledDimensions(input_width, input_height, |
101 | 817 | &scaled_width, &scaled_height)) { |
102 | 258 | size_t fuzz_px_limit = fuzz_utils::kFuzzPxLimit; |
103 | 258 | if (scaled_width != config.input.width || |
104 | 258 | scaled_height != config.input.height) { |
105 | | // Using the WebPRescalerImport internally can significantly slow |
106 | | // down the execution. Avoid timeouts due to that. |
107 | 255 | fuzz_px_limit /= 2; |
108 | 255 | } |
109 | | // A big output canvas can lead to out-of-memory and timeout issues, |
110 | | // but a big internal working buffer can too. Also, rescaling from a |
111 | | // very wide input image to a very tall canvas can be as slow as |
112 | | // decoding a huge number of pixels. Avoid timeouts due to these. |
113 | 258 | const uint64_t max_num_operations = |
114 | 258 | (uint64_t)std::max(scaled_width, config.input.width) * |
115 | 258 | std::max(scaled_height, config.input.height); |
116 | 258 | if (max_num_operations > fuzz_px_limit) { |
117 | 152 | break; |
118 | 152 | } |
119 | 258 | } |
120 | 817 | } |
121 | 1.34k | } |
122 | 9.48k | if (incremental) { |
123 | | // Decodes incrementally in chunks of increasing size. |
124 | 4.81k | WebPIDecoder* idec = WebPIDecode(NULL, 0, &config); |
125 | 4.81k | if (!idec) return; |
126 | 4.81k | VP8StatusCode status; |
127 | 4.81k | if (size & 8) { |
128 | 2.45k | size_t available_size = value + 1; |
129 | 4.67k | while (1) { |
130 | 4.67k | if (available_size > size) available_size = size; |
131 | 4.67k | status = WebPIUpdate(idec, data, available_size); |
132 | 4.67k | if (status != VP8_STATUS_SUSPENDED || available_size == size) break; |
133 | 2.21k | available_size *= 2; |
134 | 2.21k | } |
135 | 2.45k | } else { |
136 | | // WebPIAppend expects new data and its size with each call. |
137 | | // Implemented here by simply advancing the pointer into data. |
138 | 2.36k | const uint8_t* new_data = data; |
139 | 2.36k | size_t new_size = value + 1; |
140 | 7.37k | while (1) { |
141 | 7.37k | if (new_data + new_size > data + size) { |
142 | 3.49k | new_size = data + size - new_data; |
143 | 3.49k | } |
144 | 7.37k | status = WebPIAppend(idec, new_data, new_size); |
145 | 7.37k | if (status != VP8_STATUS_SUSPENDED || new_size == 0) break; |
146 | 5.00k | new_data += new_size; |
147 | 5.00k | new_size *= 2; |
148 | 5.00k | } |
149 | 2.36k | } |
150 | 4.81k | WebPIDelete(idec); |
151 | 4.81k | } else { |
152 | 4.66k | (void)WebPDecode(data, size, &config); |
153 | 4.66k | } |
154 | | |
155 | 9.48k | WebPFreeDecBuffer(&config.output); |
156 | 9.48k | } |
157 | 8.29k | } |
158 | | |
159 | | } // namespace |
160 | | |
161 | | FUZZ_TEST(AdvancedApi, AdvancedApiTest) |
162 | | .WithDomains(fuzztest::String().WithMaxSize(fuzz_utils::kMaxWebPFileSize + |
163 | | 1), |
164 | | /*factor_u8=*/fuzztest::Arbitrary<uint8_t>(), |
165 | | /*flip=*/fuzztest::Arbitrary<bool>(), |
166 | | /*bypass_filtering=*/fuzztest::Arbitrary<bool>(), |
167 | | /*no_fancy_upsampling=*/fuzztest::Arbitrary<bool>(), |
168 | | /*use_threads=*/fuzztest::Arbitrary<bool>(), |
169 | | /*use_cropping=*/fuzztest::Arbitrary<bool>(), |
170 | | /*use_scaling=*/fuzztest::Arbitrary<bool>(), |
171 | | /*use_dithering=*/fuzztest::Arbitrary<bool>(), |
172 | | #if defined(WEBP_REDUCE_CSP) |
173 | | fuzztest::ElementOf<int>({static_cast<int>(MODE_RGBA), |
174 | | static_cast<int>(MODE_BGRA), |
175 | | static_cast<int>(MODE_rgbA), |
176 | | static_cast<int>(MODE_bgrA)}), |
177 | | #else |
178 | | fuzztest::InRange<int>(0, static_cast<int>(MODE_LAST) - 1), |
179 | | #endif |
180 | | /*incremental=*/fuzztest::Arbitrary<bool>()); |