Flutter Engine
The Flutter Engine
Functions | Variables
FuzzEncoders.cpp File Reference
#include "fuzz/Fuzz.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPixmap.h"
#include "include/core/SkStream.h"
#include "include/encode/SkJpegEncoder.h"
#include "include/encode/SkPngEncoder.h"
#include "include/encode/SkWebpEncoder.h"
#include "src/base/SkRandom.h"
#include "src/core/SkOSFile.h"
#include <vector>

Go to the source code of this file.

Functions

static SkBitmap make_fuzzed_bitmap (Fuzz *fuzz)
 
 DEF_FUZZ (PNGEncoder, fuzz)
 
 DEF_FUZZ (JPEGEncoder, fuzz)
 
 DEF_FUZZ (WEBPEncoder, fuzz)
 
 DEF_FUZZ (_MakeEncoderCorpus, fuzz)
 

Variables

constexpr int MAX_WIDTH = 512
 
constexpr int MAX_HEIGHT = 512
 

Function Documentation

◆ DEF_FUZZ() [1/4]

DEF_FUZZ ( _MakeEncoderCorpus  ,
fuzz   
)

Definition at line 79 of file FuzzEncoders.cpp.

79 {
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());
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);
119}
constexpr int MAX_WIDTH
constexpr int MAX_HEIGHT
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
static sk_sp< SkData > MakeWithoutCopy(const void *data, size_t length)
Definition: SkData.h:116
size_t size() const
Definition: SkData.h:30
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)
SkScalar w
SkScalar h
static SkImageInfo MakeN32Premul(int width, int height)

◆ DEF_FUZZ() [2/4]

DEF_FUZZ ( JPEGEncoder  ,
fuzz   
)

Definition at line 50 of file FuzzEncoders.cpp.

50 {
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}
static SkBitmap make_fuzzed_bitmap(Fuzz *fuzz)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
dest
Definition: zip.py:79

◆ DEF_FUZZ() [3/4]

DEF_FUZZ ( PNGEncoder  ,
fuzz   
)

Definition at line 40 of file FuzzEncoders.cpp.

40 {
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}
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)

◆ DEF_FUZZ() [4/4]

DEF_FUZZ ( WEBPEncoder  ,
fuzz   
)

Definition at line 60 of file FuzzEncoders.cpp.

60 {
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}
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
Compression fCompression
Definition: SkWebpEncoder.h:43

◆ make_fuzzed_bitmap()

static SkBitmap make_fuzzed_bitmap ( Fuzz fuzz)
static

Definition at line 27 of file FuzzEncoders.cpp.

27 {
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}
uint32_t SkPMColor
Definition: SkColor.h:205
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

Variable Documentation

◆ MAX_HEIGHT

constexpr int MAX_HEIGHT = 512
constexpr

Definition at line 25 of file FuzzEncoders.cpp.

◆ MAX_WIDTH

constexpr int MAX_WIDTH = 512
constexpr

Definition at line 24 of file FuzzEncoders.cpp.