Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
FuzzEncoders.cpp
Go to the documentation of this file.
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"
17#include "src/base/SkRandom.h"
18#include "src/core/SkOSFile.h"
19
20#include <vector>
21
22// These values were picked arbitrarily to hopefully limit the size of the
23// serialized SkPixmaps.
24constexpr int MAX_WIDTH = 512;
25constexpr int MAX_HEIGHT = 512;
26
28 SkBitmap bm;
29 uint32_t w, h;
30 fuzz->nextRange(&w, 1, MAX_WIDTH);
31 fuzz->nextRange(&h, 1, MAX_HEIGHT);
33 return bm;
34 }
35 uint32_t n = w * h;
36 fuzz->nextN((SkPMColor*)bm.getPixels(), n);
37 return bm;
38}
39
40DEF_FUZZ(PNGEncoder, fuzz) {
41 auto bm = make_fuzzed_bitmap(fuzz);
42
43 auto opts = SkPngEncoder::Options{};
44 fuzz->nextRange(&opts.fZLibLevel, 0, 9);
45
47 SkPngEncoder::Encode(&dest, bm.pixmap(), opts);
48}
49
50DEF_FUZZ(JPEGEncoder, fuzz) {
51 auto bm = make_fuzzed_bitmap(fuzz);
52
53 auto opts = SkJpegEncoder::Options{};
54 fuzz->nextRange(&opts.fQuality, 0, 100);
55
57 (void)SkJpegEncoder::Encode(&dest, bm.pixmap(), opts);
58}
59
60DEF_FUZZ(WEBPEncoder, fuzz) {
61 auto bm = make_fuzzed_bitmap(fuzz);
62
63 auto opts = SkWebpEncoder::Options{};
64 fuzz->nextRange(&opts.fQuality, 0.0f, 100.0f);
65 bool lossy;
66 fuzz->next(&lossy);
67 if (lossy) {
69 } else {
70 opts.fCompression = SkWebpEncoder::Compression::kLossless;
71 }
72
74 (void)SkWebpEncoder::Encode(&dest, bm.pixmap(), opts);
75}
76
77// Not a real fuzz endpoint, but a helper to take in real, good images
78// and dump out a corpus for this fuzzer.
79DEF_FUZZ(_MakeEncoderCorpus, fuzz) {
80 sk_sp<SkData> bytes = SkData::MakeWithoutCopy(fuzz->fData, fuzz->fSize);
81 SkDebugf("bytes %zu\n", bytes->size());
82 auto img = SkImages::DeferredFromEncodedData(bytes);
83 if (nullptr == img.get()) {
84 SkDebugf("invalid image, could not decode\n");
85 return;
86 }
87 if (img->width() > MAX_WIDTH || img->height() > MAX_HEIGHT) {
88 SkDebugf("Too big (%d x %d)\n", img->width(), img->height());
89 return;
90 }
91 std::vector<int32_t> dstPixels;
92 int rowBytes = img->width() * 4;
93 dstPixels.resize(img->height() * rowBytes);
94 SkPixmap pm(SkImageInfo::MakeN32Premul(img->width(), img->height()),
95 &dstPixels.front(), rowBytes);
96 if (!img->readPixels(nullptr, pm, 0, 0)) {
97 SkDebugf("Could not read pixmap\n");
98 return;
99 }
100
101 SkString s("./encoded_corpus/enc_");
102 static SkRandom rand;
103 s.appendU32(rand.nextU());
104 auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
105 if (!file) {
106 SkDebugf("Can't initialize file\n");
107 return;
108 }
109 auto total = pm.info().bytesPerPixel() * pm.width() * pm.height();
110 SkDebugf("Writing %d (%d x %d) bytes\n", total, pm.width(), pm.height());
111 // Write out the size in two bytes since that's what the fuzzer will
112 // read first.
113 uint32_t w = pm.width();
114 sk_fwrite(&w, sizeof(uint32_t), file);
115 uint32_t h = pm.height();
116 sk_fwrite(&h, sizeof(uint32_t), file);
117 sk_fwrite(pm.addr(), total, file);
118 sk_fclose(file);
119}
constexpr int MAX_WIDTH
static SkBitmap make_fuzzed_bitmap(Fuzz *fuzz)
constexpr int MAX_HEIGHT
#define DEF_FUZZ(name, f)
Definition Fuzz.h:156
uint32_t SkPMColor
Definition SkColor.h:205
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
FILE * sk_fopen(const char path[], SkFILE_Flags)
void sk_fclose(FILE *)
size_t sk_fwrite(const void *buffer, size_t byteCount, FILE *)
@ kWrite_SkFILE_Flag
Definition SkOSFile.h:21
Definition Fuzz.h:24
void nextRange(T *, Min, Max)
Definition Fuzz.h:119
void nextN(T *ptr, int n)
Definition Fuzz.h:144
void * getPixels() const
Definition SkBitmap.h:283
bool tryAllocPixels(const SkImageInfo &info, size_t rowBytes)
Definition SkBitmap.cpp:271
static sk_sp< SkData > MakeWithoutCopy(const void *data, size_t length)
Definition SkData.h:116
int width() const
Definition SkPixmap.h:160
const SkImageInfo & info() const
Definition SkPixmap.h:135
const void * addr() const
Definition SkPixmap.h:153
int height() const
Definition SkPixmap.h:166
uint32_t nextU()
Definition SkRandom.h:42
struct MyStruct s
SK_API sk_sp< SkImage > DeferredFromEncodedData(sk_sp< SkData > encoded, std::optional< SkAlphaType > alphaType=std::nullopt)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
SkScalar w
SkScalar h
static SkImageInfo MakeN32Premul(int width, int height)
int bytesPerPixel() const