Debian Codegraph @

fuzz/FuzzEncoders.cpp

raw
1 /*
2 * Copyright 2018 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "fuzz/Fuzz.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkImage.h"
11 #include "include/core/SkImageInfo.h"
12 #include "include/core/SkPixmap.h"
13 #include "include/encode/SkJpegEncoder.h"
14 #include "include/encode/SkWebpEncoder.h"
15 #include "src/base/SkRandom.h"
16 #include "src/core/SkOSFile.h"
17
18 #if defined(SK_CODEC_ENCODES_PNG_WITH_LIBPNG)
19 #include "include/encode/SkPngEncoder.h"
20 #endif
21 #if defined(SK_CODEC_ENCODES_PNG_WITH_RUST)
22 #include "include/encode/SkPngRustEncoder.h"
23 #endif
24
25 #include <vector>
26
27 // These values were picked arbitrarily to hopefully limit the size of the
28 // serialized SkPixmaps.
29 constexpr int MAX_WIDTH = 512;
30 constexpr int MAX_HEIGHT = 512;
31
32 static SkBitmap make_fuzzed_bitmap(Fuzz* fuzz) {
33 SkBitmap bm;
34 uint32_t w, h;
35 fuzz->nextRange(&w, 1, MAX_WIDTH);
36 fuzz->nextRange(&h, 1, MAX_HEIGHT);
37 if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(w, h))) {
38 return bm;
39 }
40 uint32_t n = w * h;
41 fuzz->nextN((SkPMColor*)bm.getPixels(), n);
42 return bm;
43 }
44
45 #if defined(SK_CODEC_ENCODES_PNG_WITH_LIBPNG)
46 DEF_FUZZ(PNGEncoder, fuzz) {
47 auto bm = make_fuzzed_bitmap(fuzz);
48
49 auto opts = SkPngEncoder::Options{};
50 fuzz->nextRange(&opts.fZLibLevel, 0, 9);
51
52 std::ignore = SkPngEncoder::Encode(bm.pixmap(), opts);
53 }
54 #endif
55
56 #if defined(SK_CODEC_ENCODES_PNG_WITH_RUST)
57 DEF_FUZZ(PNGRustEncoder, fuzz) {
58 auto bm = make_fuzzed_bitmap(fuzz);
59
60 auto opts = SkPngRustEncoder::Options{};
61 int compression = 0;
62 fuzz->nextRange(&compression, 0, 3);
63 switch (compression) {
64 case 0:
65 opts.fCompressionLevel = SkPngRustEncoder::CompressionLevel::kLow;
66 break;
67 case 1:
68 opts.fCompressionLevel = SkPngRustEncoder::CompressionLevel::kMedium;
69 break;
70 case 2:
71 opts.fCompressionLevel = SkPngRustEncoder::CompressionLevel::kHigh;
72 break;
73 }
74
75 std::ignore = SkPngRustEncoder::Encode(bm.pixmap(), opts);
76 }
77 #endif
78
79 DEF_FUZZ(JPEGEncoder, fuzz) {
80 auto bm = make_fuzzed_bitmap(fuzz);
81
82 auto opts = SkJpegEncoder::Options{};
83 fuzz->nextRange(&opts.fQuality, 0, 100);
84
85 std::ignore = SkJpegEncoder::Encode(bm.pixmap(), opts);
86 }
87
88 DEF_FUZZ(WEBPEncoder, fuzz) {
89 auto bm = make_fuzzed_bitmap(fuzz);
90
91 auto opts = SkWebpEncoder::Options{};
92 fuzz->nextRange(&opts.fQuality, 0.0f, 100.0f);
93 bool lossy;
94 fuzz->next(&lossy);
95 if (lossy) {
96 opts.fCompression = SkWebpEncoder::Compression::kLossy;
97 } else {
98 opts.fCompression = SkWebpEncoder::Compression::kLossless;
99 }
100
101 std::ignore = SkWebpEncoder::Encode(bm.pixmap(), opts);
102 }
103
104 // Not a real fuzz endpoint, but a helper to take in real, good images
105 // and dump out a corpus for this fuzzer.
106 DEF_FUZZ(_MakeEncoderCorpus, fuzz) {
107 sk_sp<SkData> bytes = SkData::MakeWithoutCopy(fuzz->fData, fuzz->fSize);
108 SkDebugf("bytes %zu\n", bytes->size());
109 auto img = SkImages::DeferredFromEncodedData(bytes);
110 if (nullptr == img.get()) {
111 SkDebugf("invalid image, could not decode\n");
112 return;
113 }
114 if (img->width() > MAX_WIDTH || img->height() > MAX_HEIGHT) {
115 SkDebugf("Too big (%d x %d)\n", img->width(), img->height());
116 return;
117 }
118 std::vector<int32_t> dstPixels;
119 int rowBytes = img->width() * 4;
120 dstPixels.resize(img->height() * rowBytes);
121 SkPixmap pm(SkImageInfo::MakeN32Premul(img->width(), img->height()),
122 &dstPixels.front(), rowBytes);
123 if (!img->readPixels(nullptr, pm, 0, 0)) {
124 SkDebugf("Could not read pixmap\n");
125 return;
126 }
127
128 SkString s("./encoded_corpus/enc_");
129 static SkRandom rand;
130 s.appendU32(rand.nextU());
131 auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
132 if (!file) {
133 SkDebugf("Can't initialize file\n");
134 return;
135 }
136 auto total = pm.info().bytesPerPixel() * pm.width() * pm.height();
137 SkDebugf("Writing %d (%d x %d) bytes\n", total, pm.width(), pm.height());
138 // Write out the size in two bytes since that's what the fuzzer will
139 // read first.
140 uint32_t w = pm.width();
141 sk_fwrite(&w, sizeof(uint32_t), file);
142 uint32_t h = pm.height();
143 sk_fwrite(&h, sizeof(uint32_t), file);
144 sk_fwrite(pm.addr(), total, file);
145 sk_fclose(file);
146 }