Coverage Report

Created: 2026-10-10 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/tests/fuzzer/enc_fuzzer.cc
Line
Count
Source
1
// Copyright 2024 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 <cstddef>
18
#include <cstdint>
19
#include <cstdlib>
20
#include <iostream>
21
#include <limits>
22
#include <memory>
23
#include <string>
24
#include <string_view>
25
26
#include "gtest/gtest.h"
27
#include "imageio/image_dec.h"
28
#include "src/dsp/cpu.h"
29
#include "tests/fuzzer/fuzz_utils.h"
30
#include "webp/decode.h"
31
#include "webp/encode.h"
32
#include "webp/types.h"
33
34
namespace {
35
36
const VP8CPUInfo default_VP8GetCPUInfo = fuzz_utils::VP8GetCPUInfo;
37
38
void EncTestImpl(WebPPicture& pic, uint32_t optimization_index, bool use_argb,
39
                 WebPConfig config,
40
12.1k
                 const fuzz_utils::CropOrScaleParams& crop_or_scale_params) {
41
12.1k
  fuzz_utils::SetOptimization(default_VP8GetCPUInfo, optimization_index);
42
43
  // Crop and scale.
44
12.1k
  if (!CropOrScale(&pic, crop_or_scale_params)) {
45
0
    const WebPEncodingError error_code = pic.error_code;
46
0
    if (error_code == VP8_ENC_ERROR_OUT_OF_MEMORY) return;
47
0
    std::cerr << "CropOrScale failed. Error code: " << error_code << "\n";
48
0
    std::abort();
49
0
  }
50
51
  // Skip the cruncher except on small images, it's likely to timeout.
52
12.1k
  if (config.lossless && config.quality == 100. && config.method == 6 &&
53
2.36k
      pic.width * pic.height >= 10000) {
54
36
    config.lossless = 0;
55
36
  }
56
57
  // Encode.
58
12.1k
  WebPMemoryWriter memory_writer;
59
12.1k
  WebPMemoryWriterInit(&memory_writer);
60
12.1k
  pic.writer = WebPMemoryWrite;
61
12.1k
  pic.custom_ptr = &memory_writer;
62
12.1k
  if (!WebPEncode(&config, &pic)) {
63
0
    const WebPEncodingError error_code = pic.error_code;
64
0
    WebPMemoryWriterClear(&memory_writer);
65
0
    if (error_code == VP8_ENC_ERROR_OUT_OF_MEMORY) return;
66
0
    std::cerr << "WebPEncode failed. Error code: " << error_code << "\n";
67
0
    std::abort();
68
0
  }
69
70
  // Try decoding the result.
71
12.1k
  int w, h;
72
12.1k
  const uint8_t* const out_data = memory_writer.mem;
73
12.1k
  const size_t out_size = memory_writer.size;
74
12.1k
  uint8_t* const rgba = WebPDecodeBGRA(out_data, out_size, &w, &h);
75
12.1k
  if (rgba == nullptr || w != pic.width || h != pic.height) {
76
0
    std::cerr << "WebPDecodeBGRA failed.\n";
77
0
    WebPFree(rgba);
78
0
    WebPMemoryWriterClear(&memory_writer);
79
0
    std::abort();
80
0
  }
81
82
  // Compare the results if exact encoding.
83
12.1k
  if (pic.use_argb && config.lossless && config.near_lossless == 100) {
84
748
    const uint32_t* src1 = (const uint32_t*)rgba;
85
748
    const uint32_t* src2 = pic.argb;
86
22.7k
    for (int y = 0; y < h; ++y, src1 += w, src2 += pic.argb_stride) {
87
1.35M
      for (int x = 0; x < w; ++x) {
88
1.32M
        uint32_t v1 = src1[x], v2 = src2[x];
89
1.32M
        if (!config.exact) {
90
408k
          if ((v1 & 0xff000000u) == 0 || (v2 & 0xff000000u) == 0) {
91
            // Only keep alpha for comparison of fully transparent area.
92
453
            v1 &= 0xff000000u;
93
453
            v2 &= 0xff000000u;
94
453
          }
95
408k
        }
96
1.32M
        if (v1 != v2) {
97
0
          std::cerr << "Lossless compression failed pixel-exactness.\n";
98
0
          WebPFree(rgba);
99
0
          WebPMemoryWriterClear(&memory_writer);
100
0
          std::abort();
101
0
        }
102
1.32M
      }
103
22.0k
    }
104
748
  }
105
106
12.1k
  WebPFree(rgba);
107
12.1k
  WebPMemoryWriterClear(&memory_writer);
108
12.1k
  WebPPictureFree(&pic);
109
12.1k
}
110
111
// For open source compatibility, maybe_unused is necessary.
112
[[maybe_unused]] void EncFileTest(
113
    std::string_view file, uint32_t optimization_index, bool use_argb,
114
    WebPConfig config,
115
0
    const fuzz_utils::CropOrScaleParams& crop_or_scale_params) {
116
0
  // Init the source picture.
117
0
  WebPPicture pic;
118
0
  if (!WebPPictureInit(&pic)) {
119
0
    std::cerr << "WebPPictureInit failed.\n";
120
0
    std::abort();
121
0
  }
122
0
  pic.use_argb = use_argb;
123
0
124
0
  const uint8_t* const file_data =
125
0
      reinterpret_cast<const uint8_t*>(file.data());
126
0
  if (fuzz_utils::IsImageTooBig(file_data, file.size())) return;
127
0
  WebPImageReader reader = WebPGuessImageReader(file_data, file.size());
128
0
  if (!reader(file_data, file.size(), &pic, 1, NULL)) return;
129
0
130
0
  EncTestImpl(pic, optimization_index, use_argb, config, crop_or_scale_params);
131
0
}
132
133
void EncArbitraryTest(
134
    fuzz_utils::WebPPictureCpp pic_cpp, uint32_t optimization_index,
135
    bool use_argb, WebPConfig config,
136
12.1k
    const fuzz_utils::CropOrScaleParams& crop_or_scale_params) {
137
12.1k
  WebPPicture& pic = pic_cpp.ref();
138
139
12.1k
  EncTestImpl(pic, optimization_index, use_argb, config, crop_or_scale_params);
140
12.1k
}
141
142
}  // namespace
143
144
FUZZ_TEST(Enc, EncArbitraryTest)
145
    .WithDomains(fuzz_utils::ArbitraryWebPPicture(),
146
                 /*optimization_index=*/
147
                 fuzztest::InRange<uint32_t>(0,
148
                                             fuzz_utils::kMaxOptimizationIndex),
149
                 /*use_argb=*/fuzztest::Arbitrary<bool>(),
150
                 fuzz_utils::ArbitraryWebPConfig(),
151
                 fuzz_utils::ArbitraryCropOrScaleParams());
152
153
// NaN compares false with everything, so it used to pass the range checks of
154
// WebPValidateConfig() and reach float to int casts.
155
0
TEST(Enc, ValidateConfigRejectsNaN) {
156
0
  const float kNaN = std::numeric_limits<float>::quiet_NaN();
157
0
  WebPConfig config;
158
0
  ASSERT_TRUE(WebPConfigInit(&config));
159
0
  EXPECT_TRUE(WebPValidateConfig(&config));
160
0
  config.quality = kNaN;
161
0
  EXPECT_FALSE(WebPValidateConfig(&config));
162
0
  ASSERT_TRUE(WebPConfigInit(&config));
163
0
  config.target_PSNR = kNaN;
164
0
  EXPECT_FALSE(WebPValidateConfig(&config));
165
166
0
  for (const int lossless : {0, 1}) {
167
0
    ASSERT_TRUE(WebPConfigInit(&config));
168
0
    config.lossless = lossless;
169
0
    config.quality = kNaN;
170
0
    WebPPicture pic;
171
0
    ASSERT_TRUE(WebPPictureInit(&pic));
172
0
    pic.use_argb = lossless;
173
0
    pic.width = 32;
174
0
    pic.height = 32;
175
0
    ASSERT_TRUE(WebPPictureAlloc(&pic));
176
0
    WebPMemoryWriter writer;
177
0
    WebPMemoryWriterInit(&writer);
178
0
    pic.writer = WebPMemoryWrite;
179
0
    pic.custom_ptr = &writer;
180
0
    EXPECT_FALSE(WebPEncode(&config, &pic));
181
0
    EXPECT_EQ(pic.error_code, VP8_ENC_ERROR_INVALID_CONFIGURATION);
182
0
    WebPMemoryWriterClear(&writer);
183
0
    WebPPictureFree(&pic);
184
0
  }
185
0
}