Coverage Report

Created: 2025-07-01 06:35

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