Coverage Report

Created: 2026-01-13 06:17

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 "webp/decode.h"
27
28
namespace {
29
30
void AdvancedApiTest(std::string_view blob, uint8_t factor_u8, int colorspace,
31
                     bool incremental,
32
8.43k
                     const fuzz_utils::WebPDecoderOptionsCpp& decoder_options) {
33
8.43k
  WebPDecoderConfig config;
34
8.43k
  if (!WebPInitDecoderConfig(&config)) return;
35
8.43k
  const uint8_t* const data = reinterpret_cast<const uint8_t*>(blob.data());
36
8.43k
  const size_t size = blob.size();
37
8.43k
  if (WebPGetFeatures(data, size, &config.input) != VP8_STATUS_OK) return;
38
8.35k
  if ((size_t)config.input.width * config.input.height >
39
8.35k
      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.33k
  const uint8_t value = fuzz_utils::FuzzHash(data, size);
47
8.33k
  const float factor = factor_u8 / 255.f;  // 0-1
48
49
8.33k
  std::memcpy(&config.options, &decoder_options, sizeof(decoder_options));
50
8.33k
  if (config.options.use_cropping) {
51
4.33k
    config.options.crop_width = (int)(config.input.width * (1 - factor));
52
4.33k
    config.options.crop_height = (int)(config.input.height * (1 - factor));
53
4.33k
    config.options.crop_left = config.input.width - config.options.crop_width;
54
4.33k
    config.options.crop_top = config.input.height - config.options.crop_height;
55
4.33k
  }
56
8.33k
  if (config.options.use_scaling) {
57
3.95k
    config.options.scaled_width = (int)(config.input.width * factor * 2);
58
3.95k
    config.options.scaled_height = (int)(config.input.height * factor * 2);
59
3.95k
  }
60
8.33k
  config.output.colorspace = static_cast<WEBP_CSP_MODE>(colorspace);
61
62
17.8k
  for (int i = 0; i < 2; ++i) {
63
16.6k
    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
8.33k
      const int data_offset = 50;
69
8.33k
      if (data_offset + sizeof(config.options) >= size) break;
70
1.37k
      memcpy(&config.options, data + data_offset, sizeof(config.options));
71
72
      // Skip easily avoidable out-of-memory fuzzing errors.
73
1.37k
      if (config.options.use_scaling) {
74
770
        int input_width = config.input.width;
75
770
        int input_height = config.input.height;
76
770
        if (config.options.use_cropping) {
77
617
          const int cw = config.options.crop_width;
78
617
          const int ch = config.options.crop_height;
79
617
          const int x = config.options.crop_left & ~1;
80
617
          const int y = config.options.crop_top & ~1;
81
617
          if (WebPCheckCropDimensions(input_width, input_height, x, y, cw,
82
617
                                      ch)) {
83
3
            input_width = cw;
84
3
            input_height = ch;
85
3
          }
86
617
        }
87
88
770
        int scaled_width = config.options.scaled_width;
89
770
        int scaled_height = config.options.scaled_height;
90
770
        if (WebPRescalerGetScaledDimensions(input_width, input_height,
91
770
                                            &scaled_width, &scaled_height)) {
92
293
          size_t fuzz_px_limit = fuzz_utils::kFuzzPxLimit;
93
293
          if (scaled_width != config.input.width ||
94
291
              scaled_height != config.input.height) {
95
            // Using the WebPRescalerImport internally can significantly slow
96
            // down the execution. Avoid timeouts due to that.
97
291
            fuzz_px_limit /= 2;
98
291
          }
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
293
          const uint64_t max_num_operations =
104
293
              (uint64_t)std::max(scaled_width, config.input.width) *
105
293
              std::max(scaled_height, config.input.height);
106
293
          if (max_num_operations > fuzz_px_limit) {
107
198
            break;
108
198
          }
109
293
        }
110
770
      }
111
1.37k
    }
112
9.51k
    if (incremental) {
113
      // Decodes incrementally in chunks of increasing size.
114
4.65k
      WebPIDecoder* idec = WebPIDecode(NULL, 0, &config);
115
4.65k
      if (!idec) return;
116
4.65k
      VP8StatusCode status;
117
4.65k
      if (size & 8) {
118
2.73k
        size_t available_size = value + 1;
119
4.05k
        while (1) {
120
4.05k
          if (available_size > size) available_size = size;
121
4.05k
          status = WebPIUpdate(idec, data, available_size);
122
4.05k
          if (status != VP8_STATUS_SUSPENDED || available_size == size) break;
123
1.31k
          available_size *= 2;
124
1.31k
        }
125
2.73k
      } else {
126
        // WebPIAppend expects new data and its size with each call.
127
        // Implemented here by simply advancing the pointer into data.
128
1.91k
        const uint8_t* new_data = data;
129
1.91k
        size_t new_size = value + 1;
130
5.57k
        while (1) {
131
5.57k
          if (new_data + new_size > data + size) {
132
2.73k
            new_size = data + size - new_data;
133
2.73k
          }
134
5.57k
          status = WebPIAppend(idec, new_data, new_size);
135
5.57k
          if (status != VP8_STATUS_SUSPENDED || new_size == 0) break;
136
3.65k
          new_data += new_size;
137
3.65k
          new_size *= 2;
138
3.65k
        }
139
1.91k
      }
140
4.65k
      WebPIDelete(idec);
141
4.86k
    } else {
142
4.86k
      (void)WebPDecode(data, size, &config);
143
4.86k
    }
144
145
9.51k
    WebPFreeDecBuffer(&config.output);
146
9.51k
  }
147
8.33k
}
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());