Coverage Report

Created: 2025-12-14 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/tests/fuzzer/advanced_api_fuzzer.cc
Line
Count
Source
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 <cstring>
21
#include <string_view>
22
23
#include "./fuzz_utils.h"
24
#include "src/dec/webpi_dec.h"
25
#include "src/utils/rescaler_utils.h"
26
#include "src/webp/decode.h"
27
28
namespace {
29
30
void AdvancedApiTest(std::string_view blob, uint8_t factor_u8, int colorspace,
31
                     bool incremental,
32
7.88k
                     const fuzz_utils::WebPDecoderOptionsCpp& decoder_options) {
33
7.88k
  WebPDecoderConfig config;
34
7.88k
  if (!WebPInitDecoderConfig(&config)) return;
35
7.88k
  const uint8_t* const data = reinterpret_cast<const uint8_t*>(blob.data());
36
7.88k
  const size_t size = blob.size();
37
7.88k
  if (WebPGetFeatures(data, size, &config.input) != VP8_STATUS_OK) return;
38
7.80k
  if ((size_t)config.input.width * config.input.height >
39
7.80k
      fuzz_utils::kFuzzPxLimit) {
40
15
    return;
41
15
  }
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
7.79k
  const uint8_t value = fuzz_utils::FuzzHash(data, size);
47
7.79k
  const float factor = factor_u8 / 255.f;  // 0-1
48
49
7.79k
  std::memcpy(&config.options, &decoder_options, sizeof(decoder_options));
50
7.79k
  if (config.options.use_cropping) {
51
4.23k
    config.options.crop_width = (int)(config.input.width * (1 - factor));
52
4.23k
    config.options.crop_height = (int)(config.input.height * (1 - factor));
53
4.23k
    config.options.crop_left = config.input.width - config.options.crop_width;
54
4.23k
    config.options.crop_top = config.input.height - config.options.crop_height;
55
4.23k
  }
56
7.79k
  if (config.options.use_scaling) {
57
3.59k
    config.options.scaled_width = (int)(config.input.width * factor * 2);
58
3.59k
    config.options.scaled_height = (int)(config.input.height * factor * 2);
59
3.59k
  }
60
7.79k
  config.output.colorspace = static_cast<WEBP_CSP_MODE>(colorspace);
61
62
16.6k
  for (int i = 0; i < 2; ++i) {
63
15.5k
    if (i == 1) {
64
      // Use the bitstream data to generate extreme ranges for the options. An
65
      // alternative approach would be to use a custom corpus containing webp
66
      // files prepended with sizeof(config.options) zeroes to allow the fuzzer
67
      // to modify these independently.
68
7.79k
      const int data_offset = 50;
69
7.79k
      if (data_offset + sizeof(config.options) >= size) break;
70
1.25k
      memcpy(&config.options, data + data_offset, sizeof(config.options));
71
72
      // Skip easily avoidable out-of-memory fuzzing errors.
73
1.25k
      if (config.options.use_scaling) {
74
684
        int input_width = config.input.width;
75
684
        int input_height = config.input.height;
76
684
        if (config.options.use_cropping) {
77
544
          const int cw = config.options.crop_width;
78
544
          const int ch = config.options.crop_height;
79
544
          const int x = config.options.crop_left & ~1;
80
544
          const int y = config.options.crop_top & ~1;
81
544
          if (WebPCheckCropDimensions(input_width, input_height, x, y, cw,
82
544
                                      ch)) {
83
2
            input_width = cw;
84
2
            input_height = ch;
85
2
          }
86
544
        }
87
88
684
        int scaled_width = config.options.scaled_width;
89
684
        int scaled_height = config.options.scaled_height;
90
684
        if (WebPRescalerGetScaledDimensions(input_width, input_height,
91
684
                                            &scaled_width, &scaled_height)) {
92
252
          size_t fuzz_px_limit = fuzz_utils::kFuzzPxLimit;
93
252
          if (scaled_width != config.input.width ||
94
250
              scaled_height != config.input.height) {
95
            // Using the WebPRescalerImport internally can significantly slow
96
            // down the execution. Avoid timeouts due to that.
97
250
            fuzz_px_limit /= 2;
98
250
          }
99
          // A big output canvas can lead to out-of-memory and timeout issues,
100
          // but a big internal working buffer can too. Also, rescaling from a
101
          // very wide input image to a very tall canvas can be as slow as
102
          // decoding a huge number of pixels. Avoid timeouts due to these.
103
252
          const uint64_t max_num_operations =
104
252
              (uint64_t)std::max(scaled_width, config.input.width) *
105
252
              std::max(scaled_height, config.input.height);
106
252
          if (max_num_operations > fuzz_px_limit) {
107
166
            break;
108
166
          }
109
252
        }
110
684
      }
111
1.25k
    }
112
8.88k
    if (incremental) {
113
      // Decodes incrementally in chunks of increasing size.
114
4.42k
      WebPIDecoder* idec = WebPIDecode(NULL, 0, &config);
115
4.42k
      if (!idec) return;
116
4.42k
      VP8StatusCode status;
117
4.42k
      if (size & 8) {
118
2.52k
        size_t available_size = value + 1;
119
3.74k
        while (1) {
120
3.74k
          if (available_size > size) available_size = size;
121
3.74k
          status = WebPIUpdate(idec, data, available_size);
122
3.74k
          if (status != VP8_STATUS_SUSPENDED || available_size == size) break;
123
1.22k
          available_size *= 2;
124
1.22k
        }
125
2.52k
      } else {
126
        // WebPIAppend expects new data and its size with each call.
127
        // Implemented here by simply advancing the pointer into data.
128
1.90k
        const uint8_t* new_data = data;
129
1.90k
        size_t new_size = value + 1;
130
5.03k
        while (1) {
131
5.03k
          if (new_data + new_size > data + size) {
132
2.81k
            new_size = data + size - new_data;
133
2.81k
          }
134
5.03k
          status = WebPIAppend(idec, new_data, new_size);
135
5.03k
          if (status != VP8_STATUS_SUSPENDED || new_size == 0) break;
136
3.13k
          new_data += new_size;
137
3.13k
          new_size *= 2;
138
3.13k
        }
139
1.90k
      }
140
4.42k
      WebPIDelete(idec);
141
4.45k
    } else {
142
4.45k
      (void)WebPDecode(data, size, &config);
143
4.45k
    }
144
145
8.88k
    WebPFreeDecBuffer(&config.output);
146
8.88k
  }
147
7.79k
}
148
149
}  // namespace
150
151
FUZZ_TEST(AdvancedApi, AdvancedApiTest)
152
    .WithDomains(fuzztest::String().WithMaxSize(fuzz_utils::kMaxWebPFileSize +
153
                                                1),
154
                 /*factor_u8=*/fuzztest::Arbitrary<uint8_t>(),
155
#if defined(WEBP_REDUCE_CSP)
156
                 fuzztest::ElementOf<int>({static_cast<int>(MODE_RGBA),
157
                                           static_cast<int>(MODE_BGRA),
158
                                           static_cast<int>(MODE_rgbA),
159
                                           static_cast<int>(MODE_bgrA)}),
160
#else
161
                 fuzztest::InRange<int>(0, static_cast<int>(MODE_LAST) - 1),
162
#endif
163
                 /*incremental=*/fuzztest::Arbitrary<bool>(),
164
                 fuzz_utils::ArbitraryValidWebPDecoderOptions());