Coverage Report

Created: 2026-09-04 06:18

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 <memory>
22
#include <string>
23
#include <string_view>
24
25
#include "imageio/image_dec.h"
26
#include "src/dsp/cpu.h"
27
#include "tests/fuzzer/fuzz_utils.h"
28
#include "webp/decode.h"
29
#include "webp/encode.h"
30
#include "webp/types.h"
31
32
namespace {
33
34
const VP8CPUInfo default_VP8GetCPUInfo = fuzz_utils::VP8GetCPUInfo;
35
36
void EncTestImpl(WebPPicture& pic, uint32_t optimization_index, bool use_argb,
37
                 WebPConfig config,
38
14.3k
                 const fuzz_utils::CropOrScaleParams& crop_or_scale_params) {
39
14.3k
  fuzz_utils::SetOptimization(default_VP8GetCPUInfo, optimization_index);
40
41
  // Crop and scale.
42
14.3k
  if (!CropOrScale(&pic, crop_or_scale_params)) {
43
0
    const WebPEncodingError error_code = pic.error_code;
44
0
    if (error_code == VP8_ENC_ERROR_OUT_OF_MEMORY) return;
45
0
    std::cerr << "CropOrScale failed. Error code: " << error_code << "\n";
46
0
    std::abort();
47
0
  }
48
49
  // Skip the cruncher except on small images, it's likely to timeout.
50
14.3k
  if (config.lossless && config.quality == 100. && config.method == 6 &&
51
2.73k
      pic.width * pic.height >= 10000) {
52
11
    config.lossless = 0;
53
11
  }
54
55
  // Encode.
56
14.3k
  WebPMemoryWriter memory_writer;
57
14.3k
  WebPMemoryWriterInit(&memory_writer);
58
14.3k
  pic.writer = WebPMemoryWrite;
59
14.3k
  pic.custom_ptr = &memory_writer;
60
14.3k
  if (!WebPEncode(&config, &pic)) {
61
0
    const WebPEncodingError error_code = pic.error_code;
62
0
    WebPMemoryWriterClear(&memory_writer);
63
0
    if (error_code == VP8_ENC_ERROR_OUT_OF_MEMORY) return;
64
0
    std::cerr << "WebPEncode failed. Error code: " << error_code << "\n";
65
0
    std::abort();
66
0
  }
67
68
  // Try decoding the result.
69
14.3k
  int w, h;
70
14.3k
  const uint8_t* const out_data = memory_writer.mem;
71
14.3k
  const size_t out_size = memory_writer.size;
72
14.3k
  uint8_t* const rgba = WebPDecodeBGRA(out_data, out_size, &w, &h);
73
14.3k
  if (rgba == nullptr || w != pic.width || h != pic.height) {
74
0
    std::cerr << "WebPDecodeBGRA failed.\n";
75
0
    WebPFree(rgba);
76
0
    WebPMemoryWriterClear(&memory_writer);
77
0
    std::abort();
78
0
  }
79
80
  // Compare the results if exact encoding.
81
14.3k
  if (pic.use_argb && config.lossless && config.near_lossless == 100) {
82
876
    const uint32_t* src1 = (const uint32_t*)rgba;
83
876
    const uint32_t* src2 = pic.argb;
84
25.3k
    for (int y = 0; y < h; ++y, src1 += w, src2 += pic.argb_stride) {
85
776k
      for (int x = 0; x < w; ++x) {
86
752k
        uint32_t v1 = src1[x], v2 = src2[x];
87
752k
        if (!config.exact) {
88
288k
          if ((v1 & 0xff000000u) == 0 || (v2 & 0xff000000u) == 0) {
89
            // Only keep alpha for comparison of fully transparent area.
90
23.7k
            v1 &= 0xff000000u;
91
23.7k
            v2 &= 0xff000000u;
92
23.7k
          }
93
288k
        }
94
752k
        if (v1 != v2) {
95
0
          std::cerr << "Lossless compression failed pixel-exactness.\n";
96
0
          WebPFree(rgba);
97
0
          WebPMemoryWriterClear(&memory_writer);
98
0
          std::abort();
99
0
        }
100
752k
      }
101
24.4k
    }
102
876
  }
103
104
14.3k
  WebPFree(rgba);
105
14.3k
  WebPMemoryWriterClear(&memory_writer);
106
14.3k
  WebPPictureFree(&pic);
107
14.3k
}
108
109
// For open source compatibility, maybe_unused is necessary.
110
[[maybe_unused]] void EncFileTest(
111
    std::string_view file, uint32_t optimization_index, bool use_argb,
112
    WebPConfig config,
113
0
    const fuzz_utils::CropOrScaleParams& crop_or_scale_params) {
114
0
  // Init the source picture.
115
0
  WebPPicture pic;
116
0
  if (!WebPPictureInit(&pic)) {
117
0
    std::cerr << "WebPPictureInit failed.\n";
118
0
    std::abort();
119
0
  }
120
0
  pic.use_argb = use_argb;
121
0
122
0
  const uint8_t* const file_data =
123
0
      reinterpret_cast<const uint8_t*>(file.data());
124
0
  if (fuzz_utils::IsImageTooBig(file_data, file.size())) return;
125
0
  WebPImageReader reader = WebPGuessImageReader(file_data, file.size());
126
0
  if (!reader(file_data, file.size(), &pic, 1, NULL)) return;
127
0
128
0
  EncTestImpl(pic, optimization_index, use_argb, config, crop_or_scale_params);
129
0
}
130
131
void EncArbitraryTest(
132
    fuzz_utils::WebPPictureCpp pic_cpp, uint32_t optimization_index,
133
    bool use_argb, WebPConfig config,
134
14.3k
    const fuzz_utils::CropOrScaleParams& crop_or_scale_params) {
135
14.3k
  WebPPicture& pic = pic_cpp.ref();
136
137
14.3k
  EncTestImpl(pic, optimization_index, use_argb, config, crop_or_scale_params);
138
14.3k
}
139
140
}  // namespace
141
142
FUZZ_TEST(Enc, EncArbitraryTest)
143
    .WithDomains(fuzz_utils::ArbitraryWebPPicture(),
144
                 /*optimization_index=*/
145
                 fuzztest::InRange<uint32_t>(0,
146
                                             fuzz_utils::kMaxOptimizationIndex),
147
                 /*use_argb=*/fuzztest::Arbitrary<bool>(),
148
                 fuzz_utils::ArbitraryWebPConfig(),
149
                 fuzz_utils::ArbitraryCropOrScaleParams());