xref: /aosp_15_r20/external/webp/tests/fuzzer/fuzz_utils.h (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
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 #ifndef WEBP_TESTS_FUZZER_FUZZ_UTILS_H_
18 #define WEBP_TESTS_FUZZER_FUZZ_UTILS_H_
19 
20 #include <stdint.h>
21 #include <stdlib.h>
22 
23 #include "./img_alpha.h"
24 #include "./img_grid.h"
25 #include "./img_peak.h"
26 #include "src/dsp/dsp.h"
27 #include "src/webp/encode.h"
28 
29 //------------------------------------------------------------------------------
30 // Arbitrary limits to prevent OOM, timeout, or slow execution.
31 
32 // The decoded image size, and for animations additionally the canvas size.
33 // Enabling some sanitizers slow down runtime significantly.
34 // Use a very low threshold in this case to avoid timeouts.
35 #if defined(__SANITIZE_ADDRESS__)  // GCC
36 static const size_t kFuzzPxLimit = 1024 * 1024 / 10;
37 #elif !defined(__has_feature)  // Clang
38 static const size_t kFuzzPxLimit = 1024 * 1024;
39 #elif __has_feature(address_sanitizer) || __has_feature(memory_sanitizer)
40 static const size_t kFuzzPxLimit = 1024 * 1024 / 18;
41 #else
42 static const size_t kFuzzPxLimit = 1024 * 1024;
43 #endif
44 
45 // Demuxed or decoded animation frames.
46 static const int kFuzzFrameLimit = 3;
47 
48 // Reads and sums (up to) 128 spread-out bytes.
FuzzHash(const uint8_t * const data,size_t size)49 static WEBP_INLINE uint8_t FuzzHash(const uint8_t* const data, size_t size) {
50   uint8_t value = 0;
51   size_t incr = size / 128;
52   if (!incr) incr = 1;
53   for (size_t i = 0; i < size; i += incr) value += data[i];
54   return value;
55 }
56 
57 //------------------------------------------------------------------------------
58 // Extract an integer in [0, max_value].
59 
Extract(uint32_t max_value,const uint8_t data[],size_t size,uint32_t * const bit_pos)60 static WEBP_INLINE uint32_t Extract(uint32_t max_value,
61                                     const uint8_t data[], size_t size,
62                                     uint32_t* const bit_pos) {
63   uint32_t v = 0;
64   uint32_t range = 1;
65   while (*bit_pos < 8 * size && range <= max_value) {
66     const uint8_t mask = 1u << (*bit_pos & 7);
67     v = (v << 1) | !!(data[*bit_pos >> 3] & mask);
68     range <<= 1;
69     ++*bit_pos;
70   }
71   return v % (max_value + 1);
72 }
73 
74 //------------------------------------------------------------------------------
75 // Some functions to override VP8GetCPUInfo and disable some optimizations.
76 
77 #ifdef __cplusplus
78 extern "C" VP8CPUInfo VP8GetCPUInfo;
79 #else
80 extern VP8CPUInfo VP8GetCPUInfo;
81 #endif
82 static VP8CPUInfo GetCPUInfo;
83 
GetCPUInfoNoSSE41(CPUFeature feature)84 static WEBP_INLINE int GetCPUInfoNoSSE41(CPUFeature feature) {
85   if (feature == kSSE4_1 || feature == kAVX) return 0;
86   return GetCPUInfo(feature);
87 }
88 
GetCPUInfoNoAVX(CPUFeature feature)89 static WEBP_INLINE int GetCPUInfoNoAVX(CPUFeature feature) {
90   if (feature == kAVX) return 0;
91   return GetCPUInfo(feature);
92 }
93 
GetCPUInfoForceSlowSSSE3(CPUFeature feature)94 static WEBP_INLINE int GetCPUInfoForceSlowSSSE3(CPUFeature feature) {
95   if (feature == kSlowSSSE3 && GetCPUInfo(kSSE3)) {
96     return 1;  // we have SSE3 -> force SlowSSSE3
97   }
98   return GetCPUInfo(feature);
99 }
100 
GetCPUInfoOnlyC(CPUFeature feature)101 static WEBP_INLINE int GetCPUInfoOnlyC(CPUFeature feature) {
102   (void)feature;
103   return 0;
104 }
105 
ExtractAndDisableOptimizations(VP8CPUInfo default_VP8GetCPUInfo,const uint8_t data[],size_t size,uint32_t * const bit_pos)106 static WEBP_INLINE void ExtractAndDisableOptimizations(
107     VP8CPUInfo default_VP8GetCPUInfo, const uint8_t data[], size_t size,
108     uint32_t* const bit_pos) {
109   GetCPUInfo = default_VP8GetCPUInfo;
110   const VP8CPUInfo kVP8CPUInfos[5] = {GetCPUInfoOnlyC, GetCPUInfoForceSlowSSSE3,
111                                       GetCPUInfoNoSSE41, GetCPUInfoNoAVX,
112                                       GetCPUInfo};
113   int VP8GetCPUInfo_index = Extract(4, data, size, bit_pos);
114   VP8GetCPUInfo = kVP8CPUInfos[VP8GetCPUInfo_index];
115 }
116 
117 //------------------------------------------------------------------------------
118 
ExtractWebPConfig(WebPConfig * const config,const uint8_t data[],size_t size,uint32_t * const bit_pos)119 static WEBP_INLINE int ExtractWebPConfig(WebPConfig* const config,
120                                          const uint8_t data[], size_t size,
121                                          uint32_t* const bit_pos) {
122   if (config == NULL || !WebPConfigInit(config)) return 0;
123   config->lossless = Extract(1, data, size, bit_pos);
124   config->quality = Extract(100, data, size, bit_pos);
125   config->method = Extract(6, data, size, bit_pos);
126   config->image_hint =
127       (WebPImageHint)Extract(WEBP_HINT_LAST - 1, data, size, bit_pos);
128   config->segments = 1 + Extract(3, data, size, bit_pos);
129   config->sns_strength = Extract(100, data, size, bit_pos);
130   config->filter_strength = Extract(100, data, size, bit_pos);
131   config->filter_sharpness = Extract(7, data, size, bit_pos);
132   config->filter_type = Extract(1, data, size, bit_pos);
133   config->autofilter = Extract(1, data, size, bit_pos);
134   config->alpha_compression = Extract(1, data, size, bit_pos);
135   config->alpha_filtering = Extract(2, data, size, bit_pos);
136   config->alpha_quality = Extract(100, data, size, bit_pos);
137   config->pass = 1 + Extract(9, data, size, bit_pos);
138   config->show_compressed = 1;
139   config->preprocessing = Extract(2, data, size, bit_pos);
140   config->partitions = Extract(3, data, size, bit_pos);
141   config->partition_limit = 10 * Extract(10, data, size, bit_pos);
142   config->emulate_jpeg_size = Extract(1, data, size, bit_pos);
143   config->thread_level = Extract(1, data, size, bit_pos);
144   config->low_memory = Extract(1, data, size, bit_pos);
145   config->near_lossless = 20 * Extract(5, data, size, bit_pos);
146   config->exact = Extract(1, data, size, bit_pos);
147   config->use_delta_palette = Extract(1, data, size, bit_pos);
148   config->use_sharp_yuv = Extract(1, data, size, bit_pos);
149   return WebPValidateConfig(config);
150 }
151 
152 //------------------------------------------------------------------------------
153 
ExtractSourcePicture(WebPPicture * const pic,const uint8_t data[],size_t size,uint32_t * const bit_pos)154 static WEBP_INLINE int ExtractSourcePicture(WebPPicture* const pic,
155                                             const uint8_t data[], size_t size,
156                                             uint32_t* const bit_pos) {
157   if (pic == NULL) return 0;
158 
159   // Pick a source picture.
160   const uint8_t* kImagesData[] = {
161       kImgAlphaData,
162       kImgGridData,
163       kImgPeakData
164   };
165   const int kImagesWidth[] = {
166       kImgAlphaWidth,
167       kImgGridWidth,
168       kImgPeakWidth
169   };
170   const int kImagesHeight[] = {
171       kImgAlphaHeight,
172       kImgGridHeight,
173       kImgPeakHeight
174   };
175   const size_t kNbImages = sizeof(kImagesData) / sizeof(kImagesData[0]);
176   const size_t image_index = Extract(kNbImages - 1, data, size, bit_pos);
177   const uint8_t* const image_data = kImagesData[image_index];
178   pic->width = kImagesWidth[image_index];
179   pic->height = kImagesHeight[image_index];
180   pic->argb_stride = pic->width * 4 * sizeof(uint8_t);
181 
182   // Read the bytes.
183   return WebPPictureImportRGBA(pic, image_data, pic->argb_stride);
184 }
185 
186 //------------------------------------------------------------------------------
187 
Max(int a,int b)188 static WEBP_INLINE int Max(int a, int b) { return ((a < b) ? b : a); }
189 
ExtractAndCropOrScale(WebPPicture * const pic,const uint8_t data[],size_t size,uint32_t * const bit_pos)190 static WEBP_INLINE int ExtractAndCropOrScale(WebPPicture* const pic,
191                                              const uint8_t data[], size_t size,
192                                              uint32_t* const bit_pos) {
193   if (pic == NULL) return 0;
194 #if !defined(WEBP_REDUCE_SIZE)
195   const int alter_input = Extract(1, data, size, bit_pos);
196   const int crop_or_scale = Extract(1, data, size, bit_pos);
197   const int width_ratio = 1 + Extract(7, data, size, bit_pos);
198   const int height_ratio = 1 + Extract(7, data, size, bit_pos);
199   if (alter_input) {
200     if (crop_or_scale) {
201       const uint32_t left_ratio = 1 + Extract(7, data, size, bit_pos);
202       const uint32_t top_ratio = 1 + Extract(7, data, size, bit_pos);
203       const int cropped_width = Max(1, pic->width / width_ratio);
204       const int cropped_height = Max(1, pic->height / height_ratio);
205       const int cropped_left = (pic->width - cropped_width) / left_ratio;
206       const int cropped_top = (pic->height - cropped_height) / top_ratio;
207       return WebPPictureCrop(pic, cropped_left, cropped_top, cropped_width,
208                              cropped_height);
209     } else {
210       const int scaled_width = 1 + (pic->width * width_ratio) / 8;
211       const int scaled_height = 1 + (pic->height * height_ratio) / 8;
212       return WebPPictureRescale(pic, scaled_width, scaled_height);
213     }
214   }
215 #else   // defined(WEBP_REDUCE_SIZE)
216   (void)data;
217   (void)size;
218   (void)bit_pos;
219 #endif  // !defined(WEBP_REDUCE_SIZE)
220   return 1;
221 }
222 
223 #endif  // WEBP_TESTS_FUZZER_FUZZ_UTILS_H_
224