blob: d451481049ad35e63028c48314defedd274cda57 [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"
Herb Derbya839fc02017-03-16 12:30:43 -040013#include "SkFlattenableSerialization.h"
kjlubickdba57342016-01-21 05:03:28 -080014#include "SkImage.h"
15#include "SkImageEncoder.h"
Herb Derbya839fc02017-03-16 12:30:43 -040016#include "SkImageFilter.h"
kjlubickdba57342016-01-21 05:03:28 -080017#include "SkMallocPixelRef.h"
Kevin Lubickf80f1152017-01-06 08:26:56 -050018#include "SkOSFile.h"
19#include "SkOSPath.h"
Herb Derbya839fc02017-03-16 12:30:43 -040020#include "SkPaint.h"
Hal Canary62176db2017-02-27 16:42:03 -050021#include "SkPath.h"
kjlubicke5654502016-07-19 16:50:03 -070022#include "SkPicture.h"
Hal Canary62176db2017-02-27 16:42:03 -050023#include "SkRegion.h"
24#include "SkStream.h"
25#include "SkSurface.h"
26
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050027#if SK_SUPPORT_GPU
kjlubicke7195772016-10-18 10:06:24 -070028#include "SkSLCompiler.h"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050029#endif
kjlubickdba57342016-01-21 05:03:28 -080030
Hal Canary62176db2017-02-27 16:42:03 -050031#include <iostream>
mtkleina1159422016-01-15 05:46:54 -080032#include <signal.h>
Hal Canarydb683012016-11-23 08:55:18 -070033#include "sk_tool_utils.h"
34
Hal Canary24ac42b2017-02-14 13:35:14 -050035
Hal Canary62176db2017-02-27 16:42:03 -050036DEFINE_string2(bytes, b, "", "A path to a file or a directory. If a file, the "
37 "contents will be used as the fuzz bytes. If a directory, all files "
38 "in the directory will be used as fuzz bytes for the fuzzer, one at a "
39 "time.");
mtkleind4387ea2016-01-21 06:13:52 -080040DEFINE_string2(name, n, "", "If --type is 'api', fuzz the API with this name.");
kjlubickdba57342016-01-21 05:03:28 -080041
Hal Canary62176db2017-02-27 16:42:03 -050042DEFINE_string2(type, t, "api", "How to interpret --bytes, either 'image_scale'"
43 ", 'image_mode', 'skp', 'icc', or 'api'.");
44DEFINE_string2(dump, d, "", "If not empty, dump 'image*' or 'skp' types as a "
45 "PNG with this name.");
kjlubickdba57342016-01-21 05:03:28 -080046
Kevin Lubickf80f1152017-01-06 08:26:56 -050047static int printUsage() {
48 SkDebugf("Usage: fuzz -t <type> -b <path/to/file> [-n api-to-fuzz]\n");
kjlubickdba57342016-01-21 05:03:28 -080049 return 1;
50}
Kevin Lubickf80f1152017-01-06 08:26:56 -050051static int fuzz_file(const char* path);
kjlubick2a42f482016-02-16 16:14:23 -080052static uint8_t calculate_option(SkData*);
kjlubickdba57342016-01-21 05:03:28 -080053
Kevin Lubickf80f1152017-01-06 08:26:56 -050054static void fuzz_api(sk_sp<SkData>);
Kevin Lubickf80f1152017-01-06 08:26:56 -050055static void fuzz_color_deserialize(sk_sp<SkData>);
Kevin Lubick0168e042017-02-14 13:12:37 -050056static void fuzz_icc(sk_sp<SkData>);
57static void fuzz_img(sk_sp<SkData>, uint8_t, uint8_t);
Kevin Lubickf04c50a2017-01-06 13:48:19 -050058static void fuzz_path_deserialize(sk_sp<SkData>);
Kevin Lubickedee1ae2017-02-20 17:47:18 -050059static void fuzz_region_deserialize(sk_sp<SkData>);
Kevin Lubick0168e042017-02-14 13:12:37 -050060static void fuzz_skp(sk_sp<SkData>);
Herb Derbya839fc02017-03-16 12:30:43 -040061static void fuzz_filter_fuzz(sk_sp<SkData>);
62
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050063#if SK_SUPPORT_GPU
Kevin Lubickf80f1152017-01-06 08:26:56 -050064static void fuzz_sksl2glsl(sk_sp<SkData>);
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050065#endif
mtklein65e58242016-01-13 12:57:57 -080066
67int main(int argc, char** argv) {
mtkleinf5e97822016-01-15 06:19:53 -080068 SkCommandLineFlags::Parse(argc, argv);
69
mtkleind0b82342016-01-15 07:56:20 -080070 const char* path = FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0];
Kevin Lubickf80f1152017-01-06 08:26:56 -050071
72 if (!sk_isdir(path)) {
73 return fuzz_file(path);
74 }
75
76 SkOSFile::Iter it(path);
77 for (SkString file; it.next(&file); ) {
78 SkString p = SkOSPath::Join(path, file.c_str());
79 SkDebugf("Fuzzing %s\n", p.c_str());
80 int rv = fuzz_file(p.c_str());
81 if (rv != 0) {
82 return rv;
83 }
84 }
85 return 0;
86}
87
88static int fuzz_file(const char* path) {
bungeman38d909e2016-08-02 14:40:46 -070089 sk_sp<SkData> bytes(SkData::MakeFromFileName(path));
kjlubickdba57342016-01-21 05:03:28 -080090 if (!bytes) {
91 SkDebugf("Could not read %s\n", path);
Kevin Lubickf80f1152017-01-06 08:26:56 -050092 return 1;
kjlubickdba57342016-01-21 05:03:28 -080093 }
mtklein65e58242016-01-13 12:57:57 -080094
bungeman38d909e2016-08-02 14:40:46 -070095 uint8_t option = calculate_option(bytes.get());
kjlubick2a42f482016-02-16 16:14:23 -080096
mtkleind4387ea2016-01-21 06:13:52 -080097 if (!FLAGS_type.isEmpty()) {
kjlubicke7195772016-10-18 10:06:24 -070098 if (0 == strcmp("api", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -050099 fuzz_api(bytes);
100 return 0;
kjlubicke7195772016-10-18 10:06:24 -0700101 }
102 if (0 == strcmp("color_deserialize", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -0500103 fuzz_color_deserialize(bytes);
104 return 0;
kjlubicke7195772016-10-18 10:06:24 -0700105 }
106 if (0 == strcmp("icc", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -0500107 fuzz_icc(bytes);
108 return 0;
kjlubicke7195772016-10-18 10:06:24 -0700109 }
110 if (0 == strcmp("image_scale", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -0500111 fuzz_img(bytes, option, 0);
112 return 0;
kjlubicke7195772016-10-18 10:06:24 -0700113 }
114 if (0 == strcmp("image_mode", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -0500115 fuzz_img(bytes, 0, option);
116 return 0;
kjlubicke7195772016-10-18 10:06:24 -0700117 }
Kevin Lubickf04c50a2017-01-06 13:48:19 -0500118 if (0 == strcmp("path_deserialize", FLAGS_type[0])) {
119 fuzz_path_deserialize(bytes);
120 return 0;
121 }
Kevin Lubickedee1ae2017-02-20 17:47:18 -0500122 if (0 == strcmp("region_deserialize", FLAGS_type[0])) {
123 fuzz_region_deserialize(bytes);
124 return 0;
125 }
kjlubicke7195772016-10-18 10:06:24 -0700126 if (0 == strcmp("skp", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -0500127 fuzz_skp(bytes);
128 return 0;
kjlubicke7195772016-10-18 10:06:24 -0700129 }
Herb Derbya839fc02017-03-16 12:30:43 -0400130 if (0 == strcmp("filter_fuzz", FLAGS_type[0])) {
131 fuzz_filter_fuzz(bytes);
132 return 0;
133 }
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500134#if SK_SUPPORT_GPU
kjlubicke7195772016-10-18 10:06:24 -0700135 if (0 == strcmp("sksl2glsl", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -0500136 fuzz_sksl2glsl(bytes);
137 return 0;
mtkleind4387ea2016-01-21 06:13:52 -0800138 }
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500139#endif
kjlubickdba57342016-01-21 05:03:28 -0800140 }
Kevin Lubickf80f1152017-01-06 08:26:56 -0500141 return printUsage();
kjlubickdba57342016-01-21 05:03:28 -0800142}
143
kjlubick2a42f482016-02-16 16:14:23 -0800144// This adds up the first 1024 bytes and returns it as an 8 bit integer. This allows afl-fuzz to
145// deterministically excercise different paths, or *options* (such as different scaling sizes or
146// different image modes) without needing to introduce a parameter. This way we don't need a
147// image_scale1, image_scale2, image_scale4, etc fuzzer, we can just have a image_scale fuzzer.
148// Clients are expected to transform this number into a different range, e.g. with modulo (%).
149static uint8_t calculate_option(SkData* bytes) {
150 uint8_t total = 0;
151 const uint8_t* data = bytes->bytes();
152 for (size_t i = 0; i < 1024 && i < bytes->size(); i++) {
153 total += data[i];
154 }
155 return total;
156}
157
Kevin Lubickf80f1152017-01-06 08:26:56 -0500158static void fuzz_api(sk_sp<SkData> bytes) {
mtkleind4387ea2016-01-21 06:13:52 -0800159 const char* name = FLAGS_name.isEmpty() ? "" : FLAGS_name[0];
160
Mike Reedab273fa2017-01-11 13:58:55 -0500161 for (auto r = sk_tools::Registry<Fuzzable>::Head(); r; r = r->next()) {
mtklein65e58242016-01-13 12:57:57 -0800162 auto fuzzable = r->factory();
mtkleind4387ea2016-01-21 06:13:52 -0800163 if (0 == strcmp(name, fuzzable.name)) {
mtkleinf5e97822016-01-15 06:19:53 -0800164 SkDebugf("Fuzzing %s...\n", fuzzable.name);
Kevin Lubick1ac8fd22017-03-01 10:42:45 -0500165 Fuzz fuzz(std::move(bytes));
mtklein65e58242016-01-13 12:57:57 -0800166 fuzzable.fn(&fuzz);
kjlubick47d158e2016-02-01 08:23:50 -0800167 SkDebugf("[terminated] Success!\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500168 return;
mtklein65e58242016-01-13 12:57:57 -0800169 }
170 }
mtkleind4387ea2016-01-21 06:13:52 -0800171
172 SkDebugf("When using --type api, please choose an API to fuzz with --name/-n:\n");
Mike Reedab273fa2017-01-11 13:58:55 -0500173 for (auto r = sk_tools::Registry<Fuzzable>::Head(); r; r = r->next()) {
mtkleind4387ea2016-01-21 06:13:52 -0800174 auto fuzzable = r->factory();
175 SkDebugf("\t%s\n", fuzzable.name);
176 }
kjlubickdba57342016-01-21 05:03:28 -0800177}
178
179static void dump_png(SkBitmap bitmap) {
180 if (!FLAGS_dump.isEmpty()) {
Hal Canarydb683012016-11-23 08:55:18 -0700181 sk_tool_utils::EncodeImageToFile(FLAGS_dump[0], bitmap, SkEncodedImageFormat::kPNG, 100);
kjlubickdba57342016-01-21 05:03:28 -0800182 SkDebugf("Dumped to %s\n", FLAGS_dump[0]);
183 }
184}
185
Kevin Lubickf80f1152017-01-06 08:26:56 -0500186static void fuzz_img(sk_sp<SkData> bytes, uint8_t scale, uint8_t mode) {
kjlubick2a42f482016-02-16 16:14:23 -0800187 // We can scale 1x, 2x, 4x, 8x, 16x
188 scale = scale % 5;
kjlubick5bd98a22016-02-18 06:27:38 -0800189 float fscale = (float)pow(2.0f, scale);
190 SkDebugf("Scaling factor: %f\n", fscale);
kjlubick2a42f482016-02-16 16:14:23 -0800191
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500192 // We have 5 different modes of decoding.
193 mode = mode % 5;
kjlubick2a42f482016-02-16 16:14:23 -0800194 SkDebugf("Mode: %d\n", mode);
195
196 // This is mostly copied from DMSrcSink's CodecSrc::draw method.
kjlubick47d158e2016-02-01 08:23:50 -0800197 SkDebugf("Decoding\n");
Ben Wagner145dbcd2016-11-03 14:40:50 -0400198 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(bytes));
kjlubickdba57342016-01-21 05:03:28 -0800199 if (nullptr == codec.get()) {
kjlubick47d158e2016-02-01 08:23:50 -0800200 SkDebugf("[terminated] Couldn't create codec.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500201 return;
kjlubickdba57342016-01-21 05:03:28 -0800202 }
203
204 SkImageInfo decodeInfo = codec->getInfo();
Leon Scroggins8321f752017-07-10 19:51:46 +0000205 if (4 == mode && decodeInfo.colorType() == kIndex_8_SkColorType) {
206 // 4 means animated. Frames beyond the first cannot be decoded to
207 // index 8.
208 decodeInfo = decodeInfo.makeColorType(kN32_SkColorType);
209 }
210
kjlubick2a42f482016-02-16 16:14:23 -0800211 SkISize size = codec->getScaledDimensions(fscale);
212 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
213
Leon Scroggins8321f752017-07-10 19:51:46 +0000214 // Construct a color table for the decode if necessary
215 sk_sp<SkColorTable> colorTable(nullptr);
216 SkPMColor* colorPtr = nullptr;
217 int* colorCountPtr = nullptr;
218 int maxColors = 256;
219 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
220 SkPMColor colors[256];
221 colorTable.reset(new SkColorTable(colors, maxColors));
222 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
223 colorCountPtr = &maxColors;
224 }
225
kjlubickdba57342016-01-21 05:03:28 -0800226 SkBitmap bitmap;
kjlubickdba57342016-01-21 05:03:28 -0800227 SkCodec::Options options;
228 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
229
Leon Scroggins8321f752017-07-10 19:51:46 +0000230 if (!bitmap.tryAllocPixels(decodeInfo, colorTable, SkBitmap::kZeroPixels_AllocFlag)) {
kjlubick47d158e2016-02-01 08:23:50 -0800231 SkDebugf("[terminated] Could not allocate memory. Image might be too large (%d x %d)",
kjlubick2a42f482016-02-16 16:14:23 -0800232 decodeInfo.width(), decodeInfo.height());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500233 return;
kjlubickdba57342016-01-21 05:03:28 -0800234 }
235
kjlubick2a42f482016-02-16 16:14:23 -0800236 switch (mode) {
237 case 0: {//kCodecZeroInit_Mode, kCodec_Mode
Leon Scroggins8321f752017-07-10 19:51:46 +0000238 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options,
239 colorPtr, colorCountPtr)) {
kjlubick2a42f482016-02-16 16:14:23 -0800240 case SkCodec::kSuccess:
241 SkDebugf("[terminated] Success!\n");
242 break;
243 case SkCodec::kIncompleteInput:
244 SkDebugf("[terminated] Partial Success\n");
245 break;
Leon Scroggins III674a1842017-07-06 12:26:09 -0400246 case SkCodec::kErrorInInput:
247 SkDebugf("[terminated] Partial Success with error\n");
248 break;
kjlubick2a42f482016-02-16 16:14:23 -0800249 case SkCodec::kInvalidConversion:
250 SkDebugf("Incompatible colortype conversion\n");
251 // Crash to allow afl-fuzz to know this was a bug.
252 raise(SIGSEGV);
253 default:
254 SkDebugf("[terminated] Couldn't getPixels.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500255 return;
kjlubick2a42f482016-02-16 16:14:23 -0800256 }
257 break;
258 }
259 case 1: {//kScanline_Mode
Leon Scroggins8321f752017-07-10 19:51:46 +0000260 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
261 colorCountPtr)) {
262 SkDebugf("[terminated] Could not start scanline decoder\n");
263 return;
264 }
kjlubick2a42f482016-02-16 16:14:23 -0800265
266 void* dst = bitmap.getAddr(0, 0);
267 size_t rowBytes = bitmap.rowBytes();
268 uint32_t height = decodeInfo.height();
269 switch (codec->getScanlineOrder()) {
270 case SkCodec::kTopDown_SkScanlineOrder:
271 case SkCodec::kBottomUp_SkScanlineOrder:
kjlubick2a42f482016-02-16 16:14:23 -0800272 // We do not need to check the return value. On an incomplete
273 // image, memory will be filled with a default value.
274 codec->getScanlines(dst, height, rowBytes);
275 break;
kjlubick2a42f482016-02-16 16:14:23 -0800276 }
kjlubick47d158e2016-02-01 08:23:50 -0800277 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800278 break;
kjlubick2a42f482016-02-16 16:14:23 -0800279 }
280 case 2: { //kStripe_Mode
281 const int height = decodeInfo.height();
282 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
283 // does not align with image blocks.
284 const int stripeHeight = 37;
285 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
286
287 // Decode odd stripes
Leon Scroggins8321f752017-07-10 19:51:46 +0000288 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
289 colorCountPtr)
kjlubick2a42f482016-02-16 16:14:23 -0800290 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
291 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
292 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
293 // to run this test for image types that do not have this scanline ordering.
294 SkDebugf("[terminated] Could not start top-down scanline decoder\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500295 return;
kjlubick2a42f482016-02-16 16:14:23 -0800296 }
297
298 for (int i = 0; i < numStripes; i += 2) {
299 // Skip a stripe
300 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
301 codec->skipScanlines(linesToSkip);
302
303 // Read a stripe
304 const int startY = (i + 1) * stripeHeight;
305 const int linesToRead = SkTMin(stripeHeight, height - startY);
306 if (linesToRead > 0) {
307 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
308 }
309 }
310
311 // Decode even stripes
Leon Scroggins8321f752017-07-10 19:51:46 +0000312 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr,
313 colorPtr, colorCountPtr);
kjlubick2a42f482016-02-16 16:14:23 -0800314 if (SkCodec::kSuccess != startResult) {
315 SkDebugf("[terminated] Failed to restart scanline decoder with same parameters.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500316 return;
kjlubick2a42f482016-02-16 16:14:23 -0800317 }
318 for (int i = 0; i < numStripes; i += 2) {
319 // Read a stripe
320 const int startY = i * stripeHeight;
321 const int linesToRead = SkTMin(stripeHeight, height - startY);
322 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
323
324 // Skip a stripe
325 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
326 if (linesToSkip > 0) {
327 codec->skipScanlines(linesToSkip);
328 }
329 }
330 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800331 break;
kjlubick2a42f482016-02-16 16:14:23 -0800332 }
333 case 3: { //kSubset_Mode
334 // Arbitrarily choose a divisor.
335 int divisor = 2;
336 // Total width/height of the image.
337 const int W = codec->getInfo().width();
338 const int H = codec->getInfo().height();
339 if (divisor > W || divisor > H) {
340 SkDebugf("[terminated] Cannot codec subset: divisor %d is too big "
341 "with dimensions (%d x %d)\n", divisor, W, H);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500342 return;
kjlubick2a42f482016-02-16 16:14:23 -0800343 }
344 // subset dimensions
345 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
346 const int w = SkAlign2(W / divisor);
347 const int h = SkAlign2(H / divisor);
348 SkIRect subset;
349 SkCodec::Options opts;
350 opts.fSubset = &subset;
351 SkBitmap subsetBm;
352 // We will reuse pixel memory from bitmap.
353 void* pixels = bitmap.getPixels();
354 // Keep track of left and top (for drawing subsetBm into canvas). We could use
355 // fscale * x and fscale * y, but we want integers such that the next subset will start
356 // where the last one ended. So we'll add decodeInfo.width() and height().
357 int left = 0;
358 for (int x = 0; x < W; x += w) {
359 int top = 0;
360 for (int y = 0; y < H; y+= h) {
361 // Do not make the subset go off the edge of the image.
362 const int preScaleW = SkTMin(w, W - x);
363 const int preScaleH = SkTMin(h, H - y);
364 subset.setXYWH(x, y, preScaleW, preScaleH);
365 // And fscale
366 // FIXME: Should we have a version of getScaledDimensions that takes a subset
367 // into account?
368 decodeInfo = decodeInfo.makeWH(
369 SkTMax(1, SkScalarRoundToInt(preScaleW * fscale)),
370 SkTMax(1, SkScalarRoundToInt(preScaleH * fscale)));
371 size_t rowBytes = decodeInfo.minRowBytes();
Leon Scroggins8321f752017-07-10 19:51:46 +0000372 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes, colorTable.get(),
373 nullptr, nullptr)) {
kjlubick2a42f482016-02-16 16:14:23 -0800374 SkDebugf("[terminated] Could not install pixels.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500375 return;
kjlubick2a42f482016-02-16 16:14:23 -0800376 }
377 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
Leon Scroggins8321f752017-07-10 19:51:46 +0000378 &opts, colorPtr, colorCountPtr);
kjlubick2a42f482016-02-16 16:14:23 -0800379 switch (result) {
380 case SkCodec::kSuccess:
381 case SkCodec::kIncompleteInput:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400382 case SkCodec::kErrorInInput:
kjlubick2a42f482016-02-16 16:14:23 -0800383 SkDebugf("okay\n");
384 break;
385 case SkCodec::kInvalidConversion:
386 if (0 == (x|y)) {
387 // First subset is okay to return unimplemented.
388 SkDebugf("[terminated] Incompatible colortype conversion\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500389 return;
kjlubick2a42f482016-02-16 16:14:23 -0800390 }
391 // If the first subset succeeded, a later one should not fail.
392 // fall through to failure
393 case SkCodec::kUnimplemented:
394 if (0 == (x|y)) {
395 // First subset is okay to return unimplemented.
396 SkDebugf("[terminated] subset codec not supported\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500397 return;
kjlubick2a42f482016-02-16 16:14:23 -0800398 }
399 // If the first subset succeeded, why would a later one fail?
400 // fall through to failure
401 default:
402 SkDebugf("[terminated] subset codec failed to decode (%d, %d, %d, %d) "
403 "with dimensions (%d x %d)\t error %d\n",
404 x, y, decodeInfo.width(), decodeInfo.height(),
405 W, H, result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500406 return;
kjlubick2a42f482016-02-16 16:14:23 -0800407 }
408 // translate by the scaled height.
409 top += decodeInfo.height();
410 }
411 // translate by the scaled width.
412 left += decodeInfo.width();
413 }
414 SkDebugf("[terminated] Success!\n");
415 break;
416 }
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500417 case 4: { //kAnimated_Mode
418 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
419 if (frameInfos.size() == 0) {
420 SkDebugf("[terminated] Not an animated image\n");
421 break;
422 }
423
424 for (size_t i = 0; i < frameInfos.size(); i++) {
425 options.fFrameIndex = i;
426 auto result = codec->startIncrementalDecode(decodeInfo, bitmap.getPixels(),
427 bitmap.rowBytes(), &options);
428 if (SkCodec::kSuccess != result) {
429 SkDebugf("[terminated] failed to start incremental decode "
430 "in frame %d with error %d\n", i, result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500431 return;
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500432 }
433
434 result = codec->incrementalDecode();
Leon Scroggins III674a1842017-07-06 12:26:09 -0400435 if (result == SkCodec::kIncompleteInput || result == SkCodec::kErrorInInput) {
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500436 SkDebugf("okay\n");
437 // Frames beyond this one will not decode.
438 break;
439 }
440 if (result == SkCodec::kSuccess) {
441 SkDebugf("okay - decoded frame %d\n", i);
442 } else {
443 SkDebugf("[terminated] incremental decode failed with "
444 "error %d\n", result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500445 return;
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500446 }
447 }
448 SkDebugf("[terminated] Success!\n");
449 break;
450 }
kjlubickdba57342016-01-21 05:03:28 -0800451 default:
kjlubick2a42f482016-02-16 16:14:23 -0800452 SkDebugf("[terminated] Mode not implemented yet\n");
kjlubickdba57342016-01-21 05:03:28 -0800453 }
454
455 dump_png(bitmap);
mtklein65e58242016-01-13 12:57:57 -0800456}
457
Kevin Lubickf80f1152017-01-06 08:26:56 -0500458static void fuzz_skp(sk_sp<SkData> bytes) {
kjlubickdba57342016-01-21 05:03:28 -0800459 SkMemoryStream stream(bytes);
460 SkDebugf("Decoding\n");
reedca2622b2016-03-18 07:25:55 -0700461 sk_sp<SkPicture> pic(SkPicture::MakeFromStream(&stream));
kjlubickdba57342016-01-21 05:03:28 -0800462 if (!pic) {
kjlubick47d158e2016-02-01 08:23:50 -0800463 SkDebugf("[terminated] Couldn't decode as a picture.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500464 return;
kjlubickdba57342016-01-21 05:03:28 -0800465 }
466 SkDebugf("Rendering\n");
467 SkBitmap bitmap;
468 if (!FLAGS_dump.isEmpty()) {
469 SkIRect size = pic->cullRect().roundOut();
470 bitmap.allocN32Pixels(size.width(), size.height());
471 }
472 SkCanvas canvas(bitmap);
473 canvas.drawPicture(pic);
kjlubick47d158e2016-02-01 08:23:50 -0800474 SkDebugf("[terminated] Success! Decoded and rendered an SkPicture!\n");
kjlubickdba57342016-01-21 05:03:28 -0800475 dump_png(bitmap);
kjlubickdba57342016-01-21 05:03:28 -0800476}
mtklein65e58242016-01-13 12:57:57 -0800477
Kevin Lubickf80f1152017-01-06 08:26:56 -0500478static void fuzz_icc(sk_sp<SkData> bytes) {
Brian Osman526972e2016-10-24 09:24:02 -0400479 sk_sp<SkColorSpace> space(SkColorSpace::MakeICC(bytes->data(), bytes->size()));
kjlubick897a8e32016-06-09 07:15:12 -0700480 if (!space) {
481 SkDebugf("[terminated] Couldn't decode ICC.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500482 return;
kjlubick897a8e32016-06-09 07:15:12 -0700483 }
484 SkDebugf("[terminated] Success! Decoded ICC.\n");
kjlubick897a8e32016-06-09 07:15:12 -0700485}
486
Kevin Lubickf80f1152017-01-06 08:26:56 -0500487static void fuzz_color_deserialize(sk_sp<SkData> bytes) {
kjlubick3e3c1a52016-06-23 10:49:27 -0700488 sk_sp<SkColorSpace> space(SkColorSpace::Deserialize(bytes->data(), bytes->size()));
489 if (!space) {
490 SkDebugf("[terminated] Couldn't deserialize Colorspace.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500491 return;
kjlubick3e3c1a52016-06-23 10:49:27 -0700492 }
493 SkDebugf("[terminated] Success! deserialized Colorspace.\n");
kjlubick3e3c1a52016-06-23 10:49:27 -0700494}
495
Kevin Lubickf04c50a2017-01-06 13:48:19 -0500496static void fuzz_path_deserialize(sk_sp<SkData> bytes) {
497 SkPath path;
498 if (!path.readFromMemory(bytes->data(), bytes->size())) {
499 SkDebugf("[terminated] Couldn't initialize SkPath.\n");
500 return;
501 }
Kevin Lubick0d4bc0d2017-02-21 09:37:48 -0500502 auto s = SkSurface::MakeRasterN32Premul(1024, 1024);
503 s->getCanvas()->drawPath(path, SkPaint());
Kevin Lubickf04c50a2017-01-06 13:48:19 -0500504 SkDebugf("[terminated] Success! Initialized SkPath.\n");
505}
506
Kevin Lubickedee1ae2017-02-20 17:47:18 -0500507static void fuzz_region_deserialize(sk_sp<SkData> bytes) {
508 SkRegion region;
509 if (!region.readFromMemory(bytes->data(), bytes->size())) {
510 SkDebugf("[terminated] Couldn't initialize SkRegion.\n");
511 return;
512 }
513 region.computeRegionComplexity();
514 region.isComplex();
515 SkRegion r2;
516 if (region == r2) {
517 region.contains(0,0);
518 } else {
519 region.contains(1,1);
520 }
521 auto s = SkSurface::MakeRasterN32Premul(1024, 1024);
522 s->getCanvas()->drawRegion(region, SkPaint());
523 SkDEBUGCODE(region.validate());
524 SkDebugf("[terminated] Success! Initialized SkRegion.\n");
525}
526
Herb Derbya839fc02017-03-16 12:30:43 -0400527static void fuzz_filter_fuzz(sk_sp<SkData> bytes) {
528
529 const int BitmapSize = 24;
530 SkBitmap bitmap;
531 bitmap.allocN32Pixels(BitmapSize, BitmapSize);
532 SkCanvas canvas(bitmap);
533 canvas.clear(0x00000000);
534
535 sk_sp<SkImageFilter> flattenable = SkValidatingDeserializeImageFilter(
536 bytes->data(), bytes->size());
537
538 // Adding some info, but the test passed if we got here without any trouble
539 if (flattenable != NULL) {
540 SkDebugf("Valid stream detected.\n");
541 // Let's see if using the filters can cause any trouble...
542 SkPaint paint;
543 paint.setImageFilter(flattenable);
544 canvas.save();
545 canvas.clipRect(SkRect::MakeXYWH(
546 0, 0, SkIntToScalar(BitmapSize), SkIntToScalar(BitmapSize)));
547
548 // This call shouldn't crash or cause ASAN to flag any memory issues
549 // If nothing bad happens within this call, everything is fine
550 canvas.drawBitmap(bitmap, 0, 0, &paint);
551
552 SkDebugf("Filter DAG rendered successfully\n");
553 canvas.restore();
554 } else {
555 SkDebugf("Invalid stream detected.\n");
556 }
557
558 SkDebugf("[terminated] Done\n");
559}
560
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500561#if SK_SUPPORT_GPU
Kevin Lubickf80f1152017-01-06 08:26:56 -0500562static void fuzz_sksl2glsl(sk_sp<SkData> bytes) {
kjlubicke7195772016-10-18 10:06:24 -0700563 SkSL::Compiler compiler;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400564 SkSL::String output;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500565 SkSL::Program::Settings settings;
566 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
567 settings.fCaps = caps.get();
568 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
569 SkString((const char*) bytes->data()),
570 settings);
571 if (!program || !compiler.toGLSL(*program, &output)) {
kjlubicke7195772016-10-18 10:06:24 -0700572 SkDebugf("[terminated] Couldn't compile input.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500573 return;
kjlubicke7195772016-10-18 10:06:24 -0700574 }
575 SkDebugf("[terminated] Success! Compiled input.\n");
kjlubicke7195772016-10-18 10:06:24 -0700576}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500577#endif
kjlubicke7195772016-10-18 10:06:24 -0700578
reed42943c82016-09-12 12:01:44 -0700579Fuzz::Fuzz(sk_sp<SkData> bytes) : fBytes(bytes), fNextByte(0) {}
mtklein65e58242016-01-13 12:57:57 -0800580
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400581void Fuzz::signalBug() { SkDebugf("Signal bug\n"); raise(SIGSEGV); }
mtkleina1159422016-01-15 05:46:54 -0800582
kjlubicke5654502016-07-19 16:50:03 -0700583size_t Fuzz::size() { return fBytes->size(); }
584
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400585bool Fuzz::exhausted() {
586 return fBytes->size() == fNextByte;
kjlubick5bd98a22016-02-18 06:27:38 -0800587}