blob: eb0675aeb06a07eee945789059f5fdedfa18e92e [file] [log] [blame]
mtklein65e58242016-01-13 12:57:57 -08001/*
2 * Copyright 2016 Google Inc.
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.h"
kjlubickdba57342016-01-21 05:03:28 -08009#include "SkCanvas.h"
10#include "SkCodec.h"
mtkleinf5e97822016-01-15 06:19:53 -080011#include "SkCommandLineFlags.h"
kjlubickdba57342016-01-21 05:03:28 -080012#include "SkData.h"
kjlubickdba57342016-01-21 05:03:28 -080013#include "SkImage.h"
14#include "SkImageEncoder.h"
15#include "SkMallocPixelRef.h"
Kevin Lubickf04c50a2017-01-06 13:48:19 -050016#include "SkPath.h"
Kevin Lubickedee1ae2017-02-20 17:47:18 -050017#include "SkRegion.h"
18#include "SkSurface.h"
Kevin Lubickf80f1152017-01-06 08:26:56 -050019#include "SkOSFile.h"
20#include "SkOSPath.h"
kjlubicke5654502016-07-19 16:50:03 -070021#include "SkPicture.h"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050022#if SK_SUPPORT_GPU
kjlubicke7195772016-10-18 10:06:24 -070023#include "SkSLCompiler.h"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050024#endif
kjlubickdba57342016-01-21 05:03:28 -080025#include "SkStream.h"
26
mtkleina1159422016-01-15 05:46:54 -080027#include <signal.h>
mtkleinf5e97822016-01-15 06:19:53 -080028
Hal Canarydb683012016-11-23 08:55:18 -070029#include "sk_tool_utils.h"
30
Kevin Lubickf80f1152017-01-06 08:26:56 -050031DEFINE_string2(bytes, b, "", "A path to a file or a directory. If a file, the contents will be used as the fuzz bytes. If a directory, all files in the directory will be used as fuzz bytes for the fuzzer, one at a time.");
mtkleind4387ea2016-01-21 06:13:52 -080032DEFINE_string2(name, n, "", "If --type is 'api', fuzz the API with this name.");
kjlubickdba57342016-01-21 05:03:28 -080033
kjlubick897a8e32016-06-09 07:15:12 -070034DEFINE_string2(type, t, "api", "How to interpret --bytes, either 'image_scale', 'image_mode', 'skp', 'icc', or 'api'.");
kjlubick2a42f482016-02-16 16:14:23 -080035DEFINE_string2(dump, d, "", "If not empty, dump 'image*' or 'skp' types as a PNG with this name.");
kjlubickdba57342016-01-21 05:03:28 -080036
Kevin Lubickf80f1152017-01-06 08:26:56 -050037static int printUsage() {
38 SkDebugf("Usage: fuzz -t <type> -b <path/to/file> [-n api-to-fuzz]\n");
kjlubickdba57342016-01-21 05:03:28 -080039 return 1;
40}
Kevin Lubickf80f1152017-01-06 08:26:56 -050041static int fuzz_file(const char* path);
kjlubick2a42f482016-02-16 16:14:23 -080042static uint8_t calculate_option(SkData*);
kjlubickdba57342016-01-21 05:03:28 -080043
Kevin Lubickf80f1152017-01-06 08:26:56 -050044static void fuzz_api(sk_sp<SkData>);
Kevin Lubickf80f1152017-01-06 08:26:56 -050045static void fuzz_color_deserialize(sk_sp<SkData>);
Kevin Lubick0168e042017-02-14 13:12:37 -050046static void fuzz_icc(sk_sp<SkData>);
47static void fuzz_img(sk_sp<SkData>, uint8_t, uint8_t);
Kevin Lubickf04c50a2017-01-06 13:48:19 -050048static void fuzz_path_deserialize(sk_sp<SkData>);
Kevin Lubickedee1ae2017-02-20 17:47:18 -050049static void fuzz_region_deserialize(sk_sp<SkData>);
Kevin Lubick0168e042017-02-14 13:12:37 -050050static void fuzz_skp(sk_sp<SkData>);
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050051#if SK_SUPPORT_GPU
Kevin Lubickf80f1152017-01-06 08:26:56 -050052static void fuzz_sksl2glsl(sk_sp<SkData>);
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050053#endif
mtklein65e58242016-01-13 12:57:57 -080054
55int main(int argc, char** argv) {
mtkleinf5e97822016-01-15 06:19:53 -080056 SkCommandLineFlags::Parse(argc, argv);
57
mtkleind0b82342016-01-15 07:56:20 -080058 const char* path = FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0];
Kevin Lubickf80f1152017-01-06 08:26:56 -050059
60 if (!sk_isdir(path)) {
61 return fuzz_file(path);
62 }
63
64 SkOSFile::Iter it(path);
65 for (SkString file; it.next(&file); ) {
66 SkString p = SkOSPath::Join(path, file.c_str());
67 SkDebugf("Fuzzing %s\n", p.c_str());
68 int rv = fuzz_file(p.c_str());
69 if (rv != 0) {
70 return rv;
71 }
72 }
73 return 0;
74}
75
76static int fuzz_file(const char* path) {
bungeman38d909e2016-08-02 14:40:46 -070077 sk_sp<SkData> bytes(SkData::MakeFromFileName(path));
kjlubickdba57342016-01-21 05:03:28 -080078 if (!bytes) {
79 SkDebugf("Could not read %s\n", path);
Kevin Lubickf80f1152017-01-06 08:26:56 -050080 return 1;
kjlubickdba57342016-01-21 05:03:28 -080081 }
mtklein65e58242016-01-13 12:57:57 -080082
bungeman38d909e2016-08-02 14:40:46 -070083 uint8_t option = calculate_option(bytes.get());
kjlubick2a42f482016-02-16 16:14:23 -080084
mtkleind4387ea2016-01-21 06:13:52 -080085 if (!FLAGS_type.isEmpty()) {
kjlubicke7195772016-10-18 10:06:24 -070086 if (0 == strcmp("api", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -050087 fuzz_api(bytes);
88 return 0;
kjlubicke7195772016-10-18 10:06:24 -070089 }
90 if (0 == strcmp("color_deserialize", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -050091 fuzz_color_deserialize(bytes);
92 return 0;
kjlubicke7195772016-10-18 10:06:24 -070093 }
94 if (0 == strcmp("icc", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -050095 fuzz_icc(bytes);
96 return 0;
kjlubicke7195772016-10-18 10:06:24 -070097 }
98 if (0 == strcmp("image_scale", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -050099 fuzz_img(bytes, option, 0);
100 return 0;
kjlubicke7195772016-10-18 10:06:24 -0700101 }
102 if (0 == strcmp("image_mode", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -0500103 fuzz_img(bytes, 0, option);
104 return 0;
kjlubicke7195772016-10-18 10:06:24 -0700105 }
Kevin Lubickf04c50a2017-01-06 13:48:19 -0500106 if (0 == strcmp("path_deserialize", FLAGS_type[0])) {
107 fuzz_path_deserialize(bytes);
108 return 0;
109 }
Kevin Lubickedee1ae2017-02-20 17:47:18 -0500110 if (0 == strcmp("region_deserialize", FLAGS_type[0])) {
111 fuzz_region_deserialize(bytes);
112 return 0;
113 }
kjlubicke7195772016-10-18 10:06:24 -0700114 if (0 == strcmp("skp", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -0500115 fuzz_skp(bytes);
116 return 0;
kjlubicke7195772016-10-18 10:06:24 -0700117 }
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500118#if SK_SUPPORT_GPU
kjlubicke7195772016-10-18 10:06:24 -0700119 if (0 == strcmp("sksl2glsl", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -0500120 fuzz_sksl2glsl(bytes);
121 return 0;
mtkleind4387ea2016-01-21 06:13:52 -0800122 }
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500123#endif
kjlubickdba57342016-01-21 05:03:28 -0800124 }
Kevin Lubickf80f1152017-01-06 08:26:56 -0500125 return printUsage();
kjlubickdba57342016-01-21 05:03:28 -0800126}
127
kjlubick2a42f482016-02-16 16:14:23 -0800128// This adds up the first 1024 bytes and returns it as an 8 bit integer. This allows afl-fuzz to
129// deterministically excercise different paths, or *options* (such as different scaling sizes or
130// different image modes) without needing to introduce a parameter. This way we don't need a
131// image_scale1, image_scale2, image_scale4, etc fuzzer, we can just have a image_scale fuzzer.
132// Clients are expected to transform this number into a different range, e.g. with modulo (%).
133static uint8_t calculate_option(SkData* bytes) {
134 uint8_t total = 0;
135 const uint8_t* data = bytes->bytes();
136 for (size_t i = 0; i < 1024 && i < bytes->size(); i++) {
137 total += data[i];
138 }
139 return total;
140}
141
Kevin Lubickf80f1152017-01-06 08:26:56 -0500142static void fuzz_api(sk_sp<SkData> bytes) {
mtkleind4387ea2016-01-21 06:13:52 -0800143 const char* name = FLAGS_name.isEmpty() ? "" : FLAGS_name[0];
144
Mike Reedab273fa2017-01-11 13:58:55 -0500145 for (auto r = sk_tools::Registry<Fuzzable>::Head(); r; r = r->next()) {
mtklein65e58242016-01-13 12:57:57 -0800146 auto fuzzable = r->factory();
mtkleind4387ea2016-01-21 06:13:52 -0800147 if (0 == strcmp(name, fuzzable.name)) {
mtkleinf5e97822016-01-15 06:19:53 -0800148 SkDebugf("Fuzzing %s...\n", fuzzable.name);
149 Fuzz fuzz(bytes);
mtklein65e58242016-01-13 12:57:57 -0800150 fuzzable.fn(&fuzz);
kjlubick47d158e2016-02-01 08:23:50 -0800151 SkDebugf("[terminated] Success!\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500152 return;
mtklein65e58242016-01-13 12:57:57 -0800153 }
154 }
mtkleind4387ea2016-01-21 06:13:52 -0800155
156 SkDebugf("When using --type api, please choose an API to fuzz with --name/-n:\n");
Mike Reedab273fa2017-01-11 13:58:55 -0500157 for (auto r = sk_tools::Registry<Fuzzable>::Head(); r; r = r->next()) {
mtkleind4387ea2016-01-21 06:13:52 -0800158 auto fuzzable = r->factory();
159 SkDebugf("\t%s\n", fuzzable.name);
160 }
kjlubickdba57342016-01-21 05:03:28 -0800161}
162
163static void dump_png(SkBitmap bitmap) {
164 if (!FLAGS_dump.isEmpty()) {
Hal Canarydb683012016-11-23 08:55:18 -0700165 sk_tool_utils::EncodeImageToFile(FLAGS_dump[0], bitmap, SkEncodedImageFormat::kPNG, 100);
kjlubickdba57342016-01-21 05:03:28 -0800166 SkDebugf("Dumped to %s\n", FLAGS_dump[0]);
167 }
168}
169
Kevin Lubickf80f1152017-01-06 08:26:56 -0500170static void fuzz_img(sk_sp<SkData> bytes, uint8_t scale, uint8_t mode) {
kjlubick2a42f482016-02-16 16:14:23 -0800171 // We can scale 1x, 2x, 4x, 8x, 16x
172 scale = scale % 5;
kjlubick5bd98a22016-02-18 06:27:38 -0800173 float fscale = (float)pow(2.0f, scale);
174 SkDebugf("Scaling factor: %f\n", fscale);
kjlubick2a42f482016-02-16 16:14:23 -0800175
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500176 // We have 5 different modes of decoding.
177 mode = mode % 5;
kjlubick2a42f482016-02-16 16:14:23 -0800178 SkDebugf("Mode: %d\n", mode);
179
180 // This is mostly copied from DMSrcSink's CodecSrc::draw method.
kjlubick47d158e2016-02-01 08:23:50 -0800181 SkDebugf("Decoding\n");
Ben Wagner145dbcd2016-11-03 14:40:50 -0400182 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(bytes));
kjlubickdba57342016-01-21 05:03:28 -0800183 if (nullptr == codec.get()) {
kjlubick47d158e2016-02-01 08:23:50 -0800184 SkDebugf("[terminated] Couldn't create codec.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500185 return;
kjlubickdba57342016-01-21 05:03:28 -0800186 }
187
188 SkImageInfo decodeInfo = codec->getInfo();
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500189 if (4 == mode && decodeInfo.colorType() == kIndex_8_SkColorType) {
190 // 4 means animated. Frames beyond the first cannot be decoded to
191 // index 8.
192 decodeInfo = decodeInfo.makeColorType(kN32_SkColorType);
193 }
kjlubick2a42f482016-02-16 16:14:23 -0800194
195 SkISize size = codec->getScaledDimensions(fscale);
196 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
197
kjlubickdba57342016-01-21 05:03:28 -0800198 // Construct a color table for the decode if necessary
Hal Canary2db83612016-11-04 13:02:54 -0400199 sk_sp<SkColorTable> colorTable(nullptr);
kjlubickdba57342016-01-21 05:03:28 -0800200 SkPMColor* colorPtr = nullptr;
201 int* colorCountPtr = nullptr;
202 int maxColors = 256;
203 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
204 SkPMColor colors[256];
205 colorTable.reset(new SkColorTable(colors, maxColors));
206 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
207 colorCountPtr = &maxColors;
208 }
209
210 SkBitmap bitmap;
211 SkMallocPixelRef::ZeroedPRFactory zeroFactory;
212 SkCodec::Options options;
213 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
214
kjlubick2a42f482016-02-16 16:14:23 -0800215 if (!bitmap.tryAllocPixels(decodeInfo, &zeroFactory, colorTable.get())) {
kjlubick47d158e2016-02-01 08:23:50 -0800216 SkDebugf("[terminated] Could not allocate memory. Image might be too large (%d x %d)",
kjlubick2a42f482016-02-16 16:14:23 -0800217 decodeInfo.width(), decodeInfo.height());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500218 return;
kjlubickdba57342016-01-21 05:03:28 -0800219 }
220
kjlubick2a42f482016-02-16 16:14:23 -0800221 switch (mode) {
222 case 0: {//kCodecZeroInit_Mode, kCodec_Mode
223 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options,
224 colorPtr, colorCountPtr)) {
225 case SkCodec::kSuccess:
226 SkDebugf("[terminated] Success!\n");
227 break;
228 case SkCodec::kIncompleteInput:
229 SkDebugf("[terminated] Partial Success\n");
230 break;
231 case SkCodec::kInvalidConversion:
232 SkDebugf("Incompatible colortype conversion\n");
233 // Crash to allow afl-fuzz to know this was a bug.
234 raise(SIGSEGV);
235 default:
236 SkDebugf("[terminated] Couldn't getPixels.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500237 return;
kjlubick2a42f482016-02-16 16:14:23 -0800238 }
239 break;
240 }
241 case 1: {//kScanline_Mode
242 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
243 colorCountPtr)) {
244 SkDebugf("[terminated] Could not start scanline decoder\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500245 return;
kjlubick2a42f482016-02-16 16:14:23 -0800246 }
247
248 void* dst = bitmap.getAddr(0, 0);
249 size_t rowBytes = bitmap.rowBytes();
250 uint32_t height = decodeInfo.height();
251 switch (codec->getScanlineOrder()) {
252 case SkCodec::kTopDown_SkScanlineOrder:
253 case SkCodec::kBottomUp_SkScanlineOrder:
kjlubick2a42f482016-02-16 16:14:23 -0800254 // We do not need to check the return value. On an incomplete
255 // image, memory will be filled with a default value.
256 codec->getScanlines(dst, height, rowBytes);
257 break;
kjlubick2a42f482016-02-16 16:14:23 -0800258 }
kjlubick47d158e2016-02-01 08:23:50 -0800259 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800260 break;
kjlubick2a42f482016-02-16 16:14:23 -0800261 }
262 case 2: { //kStripe_Mode
263 const int height = decodeInfo.height();
264 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
265 // does not align with image blocks.
266 const int stripeHeight = 37;
267 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
268
269 // Decode odd stripes
270 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
271 colorCountPtr)
272 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
273 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
274 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
275 // to run this test for image types that do not have this scanline ordering.
276 SkDebugf("[terminated] Could not start top-down scanline decoder\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500277 return;
kjlubick2a42f482016-02-16 16:14:23 -0800278 }
279
280 for (int i = 0; i < numStripes; i += 2) {
281 // Skip a stripe
282 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
283 codec->skipScanlines(linesToSkip);
284
285 // Read a stripe
286 const int startY = (i + 1) * stripeHeight;
287 const int linesToRead = SkTMin(stripeHeight, height - startY);
288 if (linesToRead > 0) {
289 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
290 }
291 }
292
293 // Decode even stripes
294 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr,
295 colorPtr, colorCountPtr);
296 if (SkCodec::kSuccess != startResult) {
297 SkDebugf("[terminated] Failed to restart scanline decoder with same parameters.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500298 return;
kjlubick2a42f482016-02-16 16:14:23 -0800299 }
300 for (int i = 0; i < numStripes; i += 2) {
301 // Read a stripe
302 const int startY = i * stripeHeight;
303 const int linesToRead = SkTMin(stripeHeight, height - startY);
304 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
305
306 // Skip a stripe
307 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
308 if (linesToSkip > 0) {
309 codec->skipScanlines(linesToSkip);
310 }
311 }
312 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800313 break;
kjlubick2a42f482016-02-16 16:14:23 -0800314 }
315 case 3: { //kSubset_Mode
316 // Arbitrarily choose a divisor.
317 int divisor = 2;
318 // Total width/height of the image.
319 const int W = codec->getInfo().width();
320 const int H = codec->getInfo().height();
321 if (divisor > W || divisor > H) {
322 SkDebugf("[terminated] Cannot codec subset: divisor %d is too big "
323 "with dimensions (%d x %d)\n", divisor, W, H);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500324 return;
kjlubick2a42f482016-02-16 16:14:23 -0800325 }
326 // subset dimensions
327 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
328 const int w = SkAlign2(W / divisor);
329 const int h = SkAlign2(H / divisor);
330 SkIRect subset;
331 SkCodec::Options opts;
332 opts.fSubset = &subset;
333 SkBitmap subsetBm;
334 // We will reuse pixel memory from bitmap.
335 void* pixels = bitmap.getPixels();
336 // Keep track of left and top (for drawing subsetBm into canvas). We could use
337 // fscale * x and fscale * y, but we want integers such that the next subset will start
338 // where the last one ended. So we'll add decodeInfo.width() and height().
339 int left = 0;
340 for (int x = 0; x < W; x += w) {
341 int top = 0;
342 for (int y = 0; y < H; y+= h) {
343 // Do not make the subset go off the edge of the image.
344 const int preScaleW = SkTMin(w, W - x);
345 const int preScaleH = SkTMin(h, H - y);
346 subset.setXYWH(x, y, preScaleW, preScaleH);
347 // And fscale
348 // FIXME: Should we have a version of getScaledDimensions that takes a subset
349 // into account?
350 decodeInfo = decodeInfo.makeWH(
351 SkTMax(1, SkScalarRoundToInt(preScaleW * fscale)),
352 SkTMax(1, SkScalarRoundToInt(preScaleH * fscale)));
353 size_t rowBytes = decodeInfo.minRowBytes();
354 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes, colorTable.get(),
355 nullptr, nullptr)) {
356 SkDebugf("[terminated] Could not install pixels.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500357 return;
kjlubick2a42f482016-02-16 16:14:23 -0800358 }
359 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
360 &opts, colorPtr, colorCountPtr);
361 switch (result) {
362 case SkCodec::kSuccess:
363 case SkCodec::kIncompleteInput:
364 SkDebugf("okay\n");
365 break;
366 case SkCodec::kInvalidConversion:
367 if (0 == (x|y)) {
368 // First subset is okay to return unimplemented.
369 SkDebugf("[terminated] Incompatible colortype conversion\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500370 return;
kjlubick2a42f482016-02-16 16:14:23 -0800371 }
372 // If the first subset succeeded, a later one should not fail.
373 // fall through to failure
374 case SkCodec::kUnimplemented:
375 if (0 == (x|y)) {
376 // First subset is okay to return unimplemented.
377 SkDebugf("[terminated] subset codec not supported\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500378 return;
kjlubick2a42f482016-02-16 16:14:23 -0800379 }
380 // If the first subset succeeded, why would a later one fail?
381 // fall through to failure
382 default:
383 SkDebugf("[terminated] subset codec failed to decode (%d, %d, %d, %d) "
384 "with dimensions (%d x %d)\t error %d\n",
385 x, y, decodeInfo.width(), decodeInfo.height(),
386 W, H, result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500387 return;
kjlubick2a42f482016-02-16 16:14:23 -0800388 }
389 // translate by the scaled height.
390 top += decodeInfo.height();
391 }
392 // translate by the scaled width.
393 left += decodeInfo.width();
394 }
395 SkDebugf("[terminated] Success!\n");
396 break;
397 }
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500398 case 4: { //kAnimated_Mode
399 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
400 if (frameInfos.size() == 0) {
401 SkDebugf("[terminated] Not an animated image\n");
402 break;
403 }
404
405 for (size_t i = 0; i < frameInfos.size(); i++) {
406 options.fFrameIndex = i;
407 auto result = codec->startIncrementalDecode(decodeInfo, bitmap.getPixels(),
408 bitmap.rowBytes(), &options);
409 if (SkCodec::kSuccess != result) {
410 SkDebugf("[terminated] failed to start incremental decode "
411 "in frame %d with error %d\n", i, result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500412 return;
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500413 }
414
415 result = codec->incrementalDecode();
416 if (result == SkCodec::kIncompleteInput) {
417 SkDebugf("okay\n");
418 // Frames beyond this one will not decode.
419 break;
420 }
421 if (result == SkCodec::kSuccess) {
422 SkDebugf("okay - decoded frame %d\n", i);
423 } else {
424 SkDebugf("[terminated] incremental decode failed with "
425 "error %d\n", result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500426 return;
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500427 }
428 }
429 SkDebugf("[terminated] Success!\n");
430 break;
431 }
kjlubickdba57342016-01-21 05:03:28 -0800432 default:
kjlubick2a42f482016-02-16 16:14:23 -0800433 SkDebugf("[terminated] Mode not implemented yet\n");
kjlubickdba57342016-01-21 05:03:28 -0800434 }
435
436 dump_png(bitmap);
mtklein65e58242016-01-13 12:57:57 -0800437}
438
Kevin Lubickf80f1152017-01-06 08:26:56 -0500439static void fuzz_skp(sk_sp<SkData> bytes) {
kjlubickdba57342016-01-21 05:03:28 -0800440 SkMemoryStream stream(bytes);
441 SkDebugf("Decoding\n");
reedca2622b2016-03-18 07:25:55 -0700442 sk_sp<SkPicture> pic(SkPicture::MakeFromStream(&stream));
kjlubickdba57342016-01-21 05:03:28 -0800443 if (!pic) {
kjlubick47d158e2016-02-01 08:23:50 -0800444 SkDebugf("[terminated] Couldn't decode as a picture.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500445 return;
kjlubickdba57342016-01-21 05:03:28 -0800446 }
447 SkDebugf("Rendering\n");
448 SkBitmap bitmap;
449 if (!FLAGS_dump.isEmpty()) {
450 SkIRect size = pic->cullRect().roundOut();
451 bitmap.allocN32Pixels(size.width(), size.height());
452 }
453 SkCanvas canvas(bitmap);
454 canvas.drawPicture(pic);
kjlubick47d158e2016-02-01 08:23:50 -0800455 SkDebugf("[terminated] Success! Decoded and rendered an SkPicture!\n");
kjlubickdba57342016-01-21 05:03:28 -0800456 dump_png(bitmap);
kjlubickdba57342016-01-21 05:03:28 -0800457}
mtklein65e58242016-01-13 12:57:57 -0800458
Kevin Lubickf80f1152017-01-06 08:26:56 -0500459static void fuzz_icc(sk_sp<SkData> bytes) {
Brian Osman526972e2016-10-24 09:24:02 -0400460 sk_sp<SkColorSpace> space(SkColorSpace::MakeICC(bytes->data(), bytes->size()));
kjlubick897a8e32016-06-09 07:15:12 -0700461 if (!space) {
462 SkDebugf("[terminated] Couldn't decode ICC.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500463 return;
kjlubick897a8e32016-06-09 07:15:12 -0700464 }
465 SkDebugf("[terminated] Success! Decoded ICC.\n");
kjlubick897a8e32016-06-09 07:15:12 -0700466}
467
Kevin Lubickf80f1152017-01-06 08:26:56 -0500468static void fuzz_color_deserialize(sk_sp<SkData> bytes) {
kjlubick3e3c1a52016-06-23 10:49:27 -0700469 sk_sp<SkColorSpace> space(SkColorSpace::Deserialize(bytes->data(), bytes->size()));
470 if (!space) {
471 SkDebugf("[terminated] Couldn't deserialize Colorspace.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500472 return;
kjlubick3e3c1a52016-06-23 10:49:27 -0700473 }
474 SkDebugf("[terminated] Success! deserialized Colorspace.\n");
kjlubick3e3c1a52016-06-23 10:49:27 -0700475}
476
Kevin Lubickf04c50a2017-01-06 13:48:19 -0500477static void fuzz_path_deserialize(sk_sp<SkData> bytes) {
478 SkPath path;
479 if (!path.readFromMemory(bytes->data(), bytes->size())) {
480 SkDebugf("[terminated] Couldn't initialize SkPath.\n");
481 return;
482 }
Kevin Lubick0d4bc0d2017-02-21 09:37:48 -0500483 auto s = SkSurface::MakeRasterN32Premul(1024, 1024);
484 s->getCanvas()->drawPath(path, SkPaint());
Kevin Lubickf04c50a2017-01-06 13:48:19 -0500485 SkDebugf("[terminated] Success! Initialized SkPath.\n");
486}
487
Kevin Lubickedee1ae2017-02-20 17:47:18 -0500488static void fuzz_region_deserialize(sk_sp<SkData> bytes) {
489 SkRegion region;
490 if (!region.readFromMemory(bytes->data(), bytes->size())) {
491 SkDebugf("[terminated] Couldn't initialize SkRegion.\n");
492 return;
493 }
494 region.computeRegionComplexity();
495 region.isComplex();
496 SkRegion r2;
497 if (region == r2) {
498 region.contains(0,0);
499 } else {
500 region.contains(1,1);
501 }
502 auto s = SkSurface::MakeRasterN32Premul(1024, 1024);
503 s->getCanvas()->drawRegion(region, SkPaint());
504 SkDEBUGCODE(region.validate());
505 SkDebugf("[terminated] Success! Initialized SkRegion.\n");
506}
507
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500508#if SK_SUPPORT_GPU
Kevin Lubickf80f1152017-01-06 08:26:56 -0500509static void fuzz_sksl2glsl(sk_sp<SkData> bytes) {
kjlubicke7195772016-10-18 10:06:24 -0700510 SkSL::Compiler compiler;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500511 SkString output;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500512 SkSL::Program::Settings settings;
513 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
514 settings.fCaps = caps.get();
515 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
516 SkString((const char*) bytes->data()),
517 settings);
518 if (!program || !compiler.toGLSL(*program, &output)) {
kjlubicke7195772016-10-18 10:06:24 -0700519 SkDebugf("[terminated] Couldn't compile input.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500520 return;
kjlubicke7195772016-10-18 10:06:24 -0700521 }
522 SkDebugf("[terminated] Success! Compiled input.\n");
kjlubicke7195772016-10-18 10:06:24 -0700523}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500524#endif
kjlubicke7195772016-10-18 10:06:24 -0700525
reed42943c82016-09-12 12:01:44 -0700526Fuzz::Fuzz(sk_sp<SkData> bytes) : fBytes(bytes), fNextByte(0) {}
mtklein65e58242016-01-13 12:57:57 -0800527
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400528void Fuzz::signalBug() { SkDebugf("Signal bug\n"); raise(SIGSEGV); }
mtkleina1159422016-01-15 05:46:54 -0800529
kjlubicke5654502016-07-19 16:50:03 -0700530size_t Fuzz::size() { return fBytes->size(); }
531
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400532bool Fuzz::exhausted() {
533 return fBytes->size() == fNextByte;
kjlubick5bd98a22016-02-18 06:27:38 -0800534}