xref: /aosp_15_r20/external/webp/tests/fuzzer/advanced_api_fuzzer.c (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
1*b2055c35SXin Li // Copyright 2018 Google Inc.
2*b2055c35SXin Li //
3*b2055c35SXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*b2055c35SXin Li // you may not use this file except in compliance with the License.
5*b2055c35SXin Li // You may obtain a copy of the License at
6*b2055c35SXin Li //
7*b2055c35SXin Li //      http://www.apache.org/licenses/LICENSE-2.0
8*b2055c35SXin Li //
9*b2055c35SXin Li // Unless required by applicable law or agreed to in writing, software
10*b2055c35SXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*b2055c35SXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*b2055c35SXin Li // See the License for the specific language governing permissions and
13*b2055c35SXin Li // limitations under the License.
14*b2055c35SXin Li //
15*b2055c35SXin Li ////////////////////////////////////////////////////////////////////////////////
16*b2055c35SXin Li 
17*b2055c35SXin Li #include <stdint.h>
18*b2055c35SXin Li #include <string.h>
19*b2055c35SXin Li 
20*b2055c35SXin Li #include "./fuzz_utils.h"
21*b2055c35SXin Li #include "src/utils/rescaler_utils.h"
22*b2055c35SXin Li #include "src/webp/decode.h"
23*b2055c35SXin Li 
LLVMFuzzerTestOneInput(const uint8_t * const data,size_t size)24*b2055c35SXin Li int LLVMFuzzerTestOneInput(const uint8_t* const data, size_t size) {
25*b2055c35SXin Li   WebPDecoderConfig config;
26*b2055c35SXin Li   if (!WebPInitDecoderConfig(&config)) return 0;
27*b2055c35SXin Li   if (WebPGetFeatures(data, size, &config.input) != VP8_STATUS_OK) return 0;
28*b2055c35SXin Li   if ((size_t)config.input.width * config.input.height > kFuzzPxLimit) return 0;
29*b2055c35SXin Li 
30*b2055c35SXin Li   // Using two independent criteria ensures that all combinations of options
31*b2055c35SXin Li   // can reach each path at the decoding stage, with meaningful differences.
32*b2055c35SXin Li 
33*b2055c35SXin Li   const uint8_t value = FuzzHash(data, size);
34*b2055c35SXin Li   const float factor = value / 255.f;  // 0-1
35*b2055c35SXin Li 
36*b2055c35SXin Li   config.options.flip = value & 1;
37*b2055c35SXin Li   config.options.bypass_filtering = value & 2;
38*b2055c35SXin Li   config.options.no_fancy_upsampling = value & 4;
39*b2055c35SXin Li   config.options.use_threads = value & 8;
40*b2055c35SXin Li   if (size & 1) {
41*b2055c35SXin Li     config.options.use_cropping = 1;
42*b2055c35SXin Li     config.options.crop_width = (int)(config.input.width * (1 - factor));
43*b2055c35SXin Li     config.options.crop_height = (int)(config.input.height * (1 - factor));
44*b2055c35SXin Li     config.options.crop_left = config.input.width - config.options.crop_width;
45*b2055c35SXin Li     config.options.crop_top = config.input.height - config.options.crop_height;
46*b2055c35SXin Li   }
47*b2055c35SXin Li   if (size & 2) {
48*b2055c35SXin Li     int strength = (int)(factor * 100);
49*b2055c35SXin Li     config.options.dithering_strength = strength;
50*b2055c35SXin Li     config.options.alpha_dithering_strength = 100 - strength;
51*b2055c35SXin Li   }
52*b2055c35SXin Li   if (size & 4) {
53*b2055c35SXin Li     config.options.use_scaling = 1;
54*b2055c35SXin Li     config.options.scaled_width = (int)(config.input.width * factor * 2);
55*b2055c35SXin Li     config.options.scaled_height = (int)(config.input.height * factor * 2);
56*b2055c35SXin Li   }
57*b2055c35SXin Li 
58*b2055c35SXin Li #if defined(WEBP_REDUCE_CSP)
59*b2055c35SXin Li   config.output.colorspace = (value & 1)
60*b2055c35SXin Li                                  ? ((value & 2) ? MODE_RGBA : MODE_BGRA)
61*b2055c35SXin Li                                  : ((value & 2) ? MODE_rgbA : MODE_bgrA);
62*b2055c35SXin Li #else
63*b2055c35SXin Li   config.output.colorspace = (WEBP_CSP_MODE)(value % MODE_LAST);
64*b2055c35SXin Li #endif  // WEBP_REDUCE_CSP
65*b2055c35SXin Li 
66*b2055c35SXin Li   for (int i = 0; i < 2; ++i) {
67*b2055c35SXin Li     if (i == 1) {
68*b2055c35SXin Li       // Use the bitstream data to generate extreme ranges for the options. An
69*b2055c35SXin Li       // alternative approach would be to use a custom corpus containing webp
70*b2055c35SXin Li       // files prepended with sizeof(config.options) zeroes to allow the fuzzer
71*b2055c35SXin Li       // to modify these independently.
72*b2055c35SXin Li       const int data_offset = 50;
73*b2055c35SXin Li       if (data_offset + sizeof(config.options) >= size) break;
74*b2055c35SXin Li       memcpy(&config.options, data + data_offset, sizeof(config.options));
75*b2055c35SXin Li 
76*b2055c35SXin Li       // Skip easily avoidable out-of-memory fuzzing errors.
77*b2055c35SXin Li       if (config.options.use_scaling) {
78*b2055c35SXin Li         int scaled_width = config.options.scaled_width;
79*b2055c35SXin Li         int scaled_height = config.options.scaled_height;
80*b2055c35SXin Li         if (WebPRescalerGetScaledDimensions(config.input.width,
81*b2055c35SXin Li                                             config.input.height, &scaled_width,
82*b2055c35SXin Li                                             &scaled_height)) {
83*b2055c35SXin Li           size_t fuzz_px_limit = kFuzzPxLimit;
84*b2055c35SXin Li           if (scaled_width != config.input.width ||
85*b2055c35SXin Li               scaled_height != config.input.height) {
86*b2055c35SXin Li             // Using the WebPRescalerImport internally can significantly slow
87*b2055c35SXin Li             // down the execution. Avoid timeouts due to that.
88*b2055c35SXin Li             fuzz_px_limit /= 2;
89*b2055c35SXin Li           }
90*b2055c35SXin Li           // A big output canvas can lead to out-of-memory and timeout issues,
91*b2055c35SXin Li           // but a big internal working buffer can too. Also, rescaling from a
92*b2055c35SXin Li           // very wide input image to a very tall canvas can be as slow as
93*b2055c35SXin Li           // decoding a huge number of pixels. Avoid timeouts due to these.
94*b2055c35SXin Li           const uint64_t max_num_operations =
95*b2055c35SXin Li               (uint64_t)Max(scaled_width, config.input.width) *
96*b2055c35SXin Li               Max(scaled_height, config.input.height);
97*b2055c35SXin Li           if (max_num_operations > fuzz_px_limit) {
98*b2055c35SXin Li             break;
99*b2055c35SXin Li           }
100*b2055c35SXin Li         }
101*b2055c35SXin Li       }
102*b2055c35SXin Li     }
103*b2055c35SXin Li     if (size % 3) {
104*b2055c35SXin Li       // Decodes incrementally in chunks of increasing size.
105*b2055c35SXin Li       WebPIDecoder* idec = WebPIDecode(NULL, 0, &config);
106*b2055c35SXin Li       if (!idec) return 0;
107*b2055c35SXin Li       VP8StatusCode status;
108*b2055c35SXin Li       if (size & 8) {
109*b2055c35SXin Li         size_t available_size = value + 1;
110*b2055c35SXin Li         while (1) {
111*b2055c35SXin Li           if (available_size > size) available_size = size;
112*b2055c35SXin Li           status = WebPIUpdate(idec, data, available_size);
113*b2055c35SXin Li           if (status != VP8_STATUS_SUSPENDED || available_size == size) break;
114*b2055c35SXin Li           available_size *= 2;
115*b2055c35SXin Li         }
116*b2055c35SXin Li       } else {
117*b2055c35SXin Li         // WebPIAppend expects new data and its size with each call.
118*b2055c35SXin Li         // Implemented here by simply advancing the pointer into data.
119*b2055c35SXin Li         const uint8_t* new_data = data;
120*b2055c35SXin Li         size_t new_size = value + 1;
121*b2055c35SXin Li         while (1) {
122*b2055c35SXin Li           if (new_data + new_size > data + size) {
123*b2055c35SXin Li             new_size = data + size - new_data;
124*b2055c35SXin Li           }
125*b2055c35SXin Li           status = WebPIAppend(idec, new_data, new_size);
126*b2055c35SXin Li           if (status != VP8_STATUS_SUSPENDED || new_size == 0) break;
127*b2055c35SXin Li           new_data += new_size;
128*b2055c35SXin Li           new_size *= 2;
129*b2055c35SXin Li         }
130*b2055c35SXin Li       }
131*b2055c35SXin Li       WebPIDelete(idec);
132*b2055c35SXin Li     } else {
133*b2055c35SXin Li       (void)WebPDecode(data, size, &config);
134*b2055c35SXin Li     }
135*b2055c35SXin Li 
136*b2055c35SXin Li     WebPFreeDecBuffer(&config.output);
137*b2055c35SXin Li   }
138*b2055c35SXin Li   return 0;
139*b2055c35SXin Li }
140