blob: bf3b88c2627d235b0c4f30487567f3efaa49d7da [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 Lubickf80f1152017-01-06 08:26:56 -050017#include "SkOSFile.h"
18#include "SkOSPath.h"
kjlubicke5654502016-07-19 16:50:03 -070019#include "SkPicture.h"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050020#if SK_SUPPORT_GPU
kjlubicke7195772016-10-18 10:06:24 -070021#include "SkSLCompiler.h"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050022#endif
kjlubickdba57342016-01-21 05:03:28 -080023#include "SkStream.h"
24
mtkleina1159422016-01-15 05:46:54 -080025#include <signal.h>
mtkleinf5e97822016-01-15 06:19:53 -080026
Hal Canarydb683012016-11-23 08:55:18 -070027#include "sk_tool_utils.h"
28
Kevin Lubickf80f1152017-01-06 08:26:56 -050029DEFINE_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 -080030DEFINE_string2(name, n, "", "If --type is 'api', fuzz the API with this name.");
kjlubickdba57342016-01-21 05:03:28 -080031
kjlubick897a8e32016-06-09 07:15:12 -070032DEFINE_string2(type, t, "api", "How to interpret --bytes, either 'image_scale', 'image_mode', 'skp', 'icc', or 'api'.");
kjlubick2a42f482016-02-16 16:14:23 -080033DEFINE_string2(dump, d, "", "If not empty, dump 'image*' or 'skp' types as a PNG with this name.");
kjlubickdba57342016-01-21 05:03:28 -080034
Kevin Lubickf80f1152017-01-06 08:26:56 -050035static int printUsage() {
36 SkDebugf("Usage: fuzz -t <type> -b <path/to/file> [-n api-to-fuzz]\n");
kjlubickdba57342016-01-21 05:03:28 -080037 return 1;
38}
Kevin Lubickf80f1152017-01-06 08:26:56 -050039static int fuzz_file(const char* path);
kjlubick2a42f482016-02-16 16:14:23 -080040static uint8_t calculate_option(SkData*);
kjlubickdba57342016-01-21 05:03:28 -080041
Kevin Lubickf80f1152017-01-06 08:26:56 -050042static void fuzz_api(sk_sp<SkData>);
43static void fuzz_img(sk_sp<SkData>, uint8_t, uint8_t);
44static void fuzz_skp(sk_sp<SkData>);
45static void fuzz_icc(sk_sp<SkData>);
46static void fuzz_color_deserialize(sk_sp<SkData>);
Kevin Lubickf04c50a2017-01-06 13:48:19 -050047static void fuzz_path_deserialize(sk_sp<SkData>);
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050048#if SK_SUPPORT_GPU
Kevin Lubickf80f1152017-01-06 08:26:56 -050049static void fuzz_sksl2glsl(sk_sp<SkData>);
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050050#endif
mtklein65e58242016-01-13 12:57:57 -080051
52int main(int argc, char** argv) {
mtkleinf5e97822016-01-15 06:19:53 -080053 SkCommandLineFlags::Parse(argc, argv);
54
mtkleind0b82342016-01-15 07:56:20 -080055 const char* path = FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0];
Kevin Lubickf80f1152017-01-06 08:26:56 -050056
57 if (!sk_isdir(path)) {
58 return fuzz_file(path);
59 }
60
61 SkOSFile::Iter it(path);
62 for (SkString file; it.next(&file); ) {
63 SkString p = SkOSPath::Join(path, file.c_str());
64 SkDebugf("Fuzzing %s\n", p.c_str());
65 int rv = fuzz_file(p.c_str());
66 if (rv != 0) {
67 return rv;
68 }
69 }
70 return 0;
71}
72
73static int fuzz_file(const char* path) {
bungeman38d909e2016-08-02 14:40:46 -070074 sk_sp<SkData> bytes(SkData::MakeFromFileName(path));
kjlubickdba57342016-01-21 05:03:28 -080075 if (!bytes) {
76 SkDebugf("Could not read %s\n", path);
Kevin Lubickf80f1152017-01-06 08:26:56 -050077 return 1;
kjlubickdba57342016-01-21 05:03:28 -080078 }
mtklein65e58242016-01-13 12:57:57 -080079
bungeman38d909e2016-08-02 14:40:46 -070080 uint8_t option = calculate_option(bytes.get());
kjlubick2a42f482016-02-16 16:14:23 -080081
mtkleind4387ea2016-01-21 06:13:52 -080082 if (!FLAGS_type.isEmpty()) {
kjlubicke7195772016-10-18 10:06:24 -070083 if (0 == strcmp("api", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -050084 fuzz_api(bytes);
85 return 0;
kjlubicke7195772016-10-18 10:06:24 -070086 }
87 if (0 == strcmp("color_deserialize", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -050088 fuzz_color_deserialize(bytes);
89 return 0;
kjlubicke7195772016-10-18 10:06:24 -070090 }
91 if (0 == strcmp("icc", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -050092 fuzz_icc(bytes);
93 return 0;
kjlubicke7195772016-10-18 10:06:24 -070094 }
95 if (0 == strcmp("image_scale", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -050096 fuzz_img(bytes, option, 0);
97 return 0;
kjlubicke7195772016-10-18 10:06:24 -070098 }
99 if (0 == strcmp("image_mode", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -0500100 fuzz_img(bytes, 0, option);
101 return 0;
kjlubicke7195772016-10-18 10:06:24 -0700102 }
Kevin Lubickf04c50a2017-01-06 13:48:19 -0500103 if (0 == strcmp("path_deserialize", FLAGS_type[0])) {
104 fuzz_path_deserialize(bytes);
105 return 0;
106 }
kjlubicke7195772016-10-18 10:06:24 -0700107 if (0 == strcmp("skp", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -0500108 fuzz_skp(bytes);
109 return 0;
kjlubicke7195772016-10-18 10:06:24 -0700110 }
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500111#if SK_SUPPORT_GPU
kjlubicke7195772016-10-18 10:06:24 -0700112 if (0 == strcmp("sksl2glsl", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -0500113 fuzz_sksl2glsl(bytes);
114 return 0;
mtkleind4387ea2016-01-21 06:13:52 -0800115 }
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500116#endif
kjlubickdba57342016-01-21 05:03:28 -0800117 }
Kevin Lubickf80f1152017-01-06 08:26:56 -0500118 return printUsage();
kjlubickdba57342016-01-21 05:03:28 -0800119}
120
kjlubick2a42f482016-02-16 16:14:23 -0800121// This adds up the first 1024 bytes and returns it as an 8 bit integer. This allows afl-fuzz to
122// deterministically excercise different paths, or *options* (such as different scaling sizes or
123// different image modes) without needing to introduce a parameter. This way we don't need a
124// image_scale1, image_scale2, image_scale4, etc fuzzer, we can just have a image_scale fuzzer.
125// Clients are expected to transform this number into a different range, e.g. with modulo (%).
126static uint8_t calculate_option(SkData* bytes) {
127 uint8_t total = 0;
128 const uint8_t* data = bytes->bytes();
129 for (size_t i = 0; i < 1024 && i < bytes->size(); i++) {
130 total += data[i];
131 }
132 return total;
133}
134
Kevin Lubickf80f1152017-01-06 08:26:56 -0500135static void fuzz_api(sk_sp<SkData> bytes) {
mtkleind4387ea2016-01-21 06:13:52 -0800136 const char* name = FLAGS_name.isEmpty() ? "" : FLAGS_name[0];
137
Mike Reedab273fa2017-01-11 13:58:55 -0500138 for (auto r = sk_tools::Registry<Fuzzable>::Head(); r; r = r->next()) {
mtklein65e58242016-01-13 12:57:57 -0800139 auto fuzzable = r->factory();
mtkleind4387ea2016-01-21 06:13:52 -0800140 if (0 == strcmp(name, fuzzable.name)) {
mtkleinf5e97822016-01-15 06:19:53 -0800141 SkDebugf("Fuzzing %s...\n", fuzzable.name);
142 Fuzz fuzz(bytes);
mtklein65e58242016-01-13 12:57:57 -0800143 fuzzable.fn(&fuzz);
kjlubick47d158e2016-02-01 08:23:50 -0800144 SkDebugf("[terminated] Success!\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500145 return;
mtklein65e58242016-01-13 12:57:57 -0800146 }
147 }
mtkleind4387ea2016-01-21 06:13:52 -0800148
149 SkDebugf("When using --type api, please choose an API to fuzz with --name/-n:\n");
Mike Reedab273fa2017-01-11 13:58:55 -0500150 for (auto r = sk_tools::Registry<Fuzzable>::Head(); r; r = r->next()) {
mtkleind4387ea2016-01-21 06:13:52 -0800151 auto fuzzable = r->factory();
152 SkDebugf("\t%s\n", fuzzable.name);
153 }
kjlubickdba57342016-01-21 05:03:28 -0800154}
155
156static void dump_png(SkBitmap bitmap) {
157 if (!FLAGS_dump.isEmpty()) {
Hal Canarydb683012016-11-23 08:55:18 -0700158 sk_tool_utils::EncodeImageToFile(FLAGS_dump[0], bitmap, SkEncodedImageFormat::kPNG, 100);
kjlubickdba57342016-01-21 05:03:28 -0800159 SkDebugf("Dumped to %s\n", FLAGS_dump[0]);
160 }
161}
162
Kevin Lubickf80f1152017-01-06 08:26:56 -0500163static void fuzz_img(sk_sp<SkData> bytes, uint8_t scale, uint8_t mode) {
kjlubick2a42f482016-02-16 16:14:23 -0800164 // We can scale 1x, 2x, 4x, 8x, 16x
165 scale = scale % 5;
kjlubick5bd98a22016-02-18 06:27:38 -0800166 float fscale = (float)pow(2.0f, scale);
167 SkDebugf("Scaling factor: %f\n", fscale);
kjlubick2a42f482016-02-16 16:14:23 -0800168
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500169 // We have 5 different modes of decoding.
170 mode = mode % 5;
kjlubick2a42f482016-02-16 16:14:23 -0800171 SkDebugf("Mode: %d\n", mode);
172
173 // This is mostly copied from DMSrcSink's CodecSrc::draw method.
kjlubick47d158e2016-02-01 08:23:50 -0800174 SkDebugf("Decoding\n");
Ben Wagner145dbcd2016-11-03 14:40:50 -0400175 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(bytes));
kjlubickdba57342016-01-21 05:03:28 -0800176 if (nullptr == codec.get()) {
kjlubick47d158e2016-02-01 08:23:50 -0800177 SkDebugf("[terminated] Couldn't create codec.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500178 return;
kjlubickdba57342016-01-21 05:03:28 -0800179 }
180
181 SkImageInfo decodeInfo = codec->getInfo();
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500182 if (4 == mode && decodeInfo.colorType() == kIndex_8_SkColorType) {
183 // 4 means animated. Frames beyond the first cannot be decoded to
184 // index 8.
185 decodeInfo = decodeInfo.makeColorType(kN32_SkColorType);
186 }
kjlubick2a42f482016-02-16 16:14:23 -0800187
188 SkISize size = codec->getScaledDimensions(fscale);
189 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
190
kjlubickdba57342016-01-21 05:03:28 -0800191 // Construct a color table for the decode if necessary
Hal Canary2db83612016-11-04 13:02:54 -0400192 sk_sp<SkColorTable> colorTable(nullptr);
kjlubickdba57342016-01-21 05:03:28 -0800193 SkPMColor* colorPtr = nullptr;
194 int* colorCountPtr = nullptr;
195 int maxColors = 256;
196 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
197 SkPMColor colors[256];
198 colorTable.reset(new SkColorTable(colors, maxColors));
199 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
200 colorCountPtr = &maxColors;
201 }
202
203 SkBitmap bitmap;
204 SkMallocPixelRef::ZeroedPRFactory zeroFactory;
205 SkCodec::Options options;
206 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
207
kjlubick2a42f482016-02-16 16:14:23 -0800208 if (!bitmap.tryAllocPixels(decodeInfo, &zeroFactory, colorTable.get())) {
kjlubick47d158e2016-02-01 08:23:50 -0800209 SkDebugf("[terminated] Could not allocate memory. Image might be too large (%d x %d)",
kjlubick2a42f482016-02-16 16:14:23 -0800210 decodeInfo.width(), decodeInfo.height());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500211 return;
kjlubickdba57342016-01-21 05:03:28 -0800212 }
213
kjlubick2a42f482016-02-16 16:14:23 -0800214 switch (mode) {
215 case 0: {//kCodecZeroInit_Mode, kCodec_Mode
216 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options,
217 colorPtr, colorCountPtr)) {
218 case SkCodec::kSuccess:
219 SkDebugf("[terminated] Success!\n");
220 break;
221 case SkCodec::kIncompleteInput:
222 SkDebugf("[terminated] Partial Success\n");
223 break;
224 case SkCodec::kInvalidConversion:
225 SkDebugf("Incompatible colortype conversion\n");
226 // Crash to allow afl-fuzz to know this was a bug.
227 raise(SIGSEGV);
228 default:
229 SkDebugf("[terminated] Couldn't getPixels.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500230 return;
kjlubick2a42f482016-02-16 16:14:23 -0800231 }
232 break;
233 }
234 case 1: {//kScanline_Mode
235 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
236 colorCountPtr)) {
237 SkDebugf("[terminated] Could not start scanline decoder\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500238 return;
kjlubick2a42f482016-02-16 16:14:23 -0800239 }
240
241 void* dst = bitmap.getAddr(0, 0);
242 size_t rowBytes = bitmap.rowBytes();
243 uint32_t height = decodeInfo.height();
244 switch (codec->getScanlineOrder()) {
245 case SkCodec::kTopDown_SkScanlineOrder:
246 case SkCodec::kBottomUp_SkScanlineOrder:
kjlubick2a42f482016-02-16 16:14:23 -0800247 // We do not need to check the return value. On an incomplete
248 // image, memory will be filled with a default value.
249 codec->getScanlines(dst, height, rowBytes);
250 break;
kjlubick2a42f482016-02-16 16:14:23 -0800251 }
kjlubick47d158e2016-02-01 08:23:50 -0800252 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800253 break;
kjlubick2a42f482016-02-16 16:14:23 -0800254 }
255 case 2: { //kStripe_Mode
256 const int height = decodeInfo.height();
257 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
258 // does not align with image blocks.
259 const int stripeHeight = 37;
260 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
261
262 // Decode odd stripes
263 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
264 colorCountPtr)
265 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
266 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
267 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
268 // to run this test for image types that do not have this scanline ordering.
269 SkDebugf("[terminated] Could not start top-down scanline decoder\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500270 return;
kjlubick2a42f482016-02-16 16:14:23 -0800271 }
272
273 for (int i = 0; i < numStripes; i += 2) {
274 // Skip a stripe
275 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
276 codec->skipScanlines(linesToSkip);
277
278 // Read a stripe
279 const int startY = (i + 1) * stripeHeight;
280 const int linesToRead = SkTMin(stripeHeight, height - startY);
281 if (linesToRead > 0) {
282 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
283 }
284 }
285
286 // Decode even stripes
287 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr,
288 colorPtr, colorCountPtr);
289 if (SkCodec::kSuccess != startResult) {
290 SkDebugf("[terminated] Failed to restart scanline decoder with same parameters.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500291 return;
kjlubick2a42f482016-02-16 16:14:23 -0800292 }
293 for (int i = 0; i < numStripes; i += 2) {
294 // Read a stripe
295 const int startY = i * stripeHeight;
296 const int linesToRead = SkTMin(stripeHeight, height - startY);
297 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
298
299 // Skip a stripe
300 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
301 if (linesToSkip > 0) {
302 codec->skipScanlines(linesToSkip);
303 }
304 }
305 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800306 break;
kjlubick2a42f482016-02-16 16:14:23 -0800307 }
308 case 3: { //kSubset_Mode
309 // Arbitrarily choose a divisor.
310 int divisor = 2;
311 // Total width/height of the image.
312 const int W = codec->getInfo().width();
313 const int H = codec->getInfo().height();
314 if (divisor > W || divisor > H) {
315 SkDebugf("[terminated] Cannot codec subset: divisor %d is too big "
316 "with dimensions (%d x %d)\n", divisor, W, H);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500317 return;
kjlubick2a42f482016-02-16 16:14:23 -0800318 }
319 // subset dimensions
320 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
321 const int w = SkAlign2(W / divisor);
322 const int h = SkAlign2(H / divisor);
323 SkIRect subset;
324 SkCodec::Options opts;
325 opts.fSubset = &subset;
326 SkBitmap subsetBm;
327 // We will reuse pixel memory from bitmap.
328 void* pixels = bitmap.getPixels();
329 // Keep track of left and top (for drawing subsetBm into canvas). We could use
330 // fscale * x and fscale * y, but we want integers such that the next subset will start
331 // where the last one ended. So we'll add decodeInfo.width() and height().
332 int left = 0;
333 for (int x = 0; x < W; x += w) {
334 int top = 0;
335 for (int y = 0; y < H; y+= h) {
336 // Do not make the subset go off the edge of the image.
337 const int preScaleW = SkTMin(w, W - x);
338 const int preScaleH = SkTMin(h, H - y);
339 subset.setXYWH(x, y, preScaleW, preScaleH);
340 // And fscale
341 // FIXME: Should we have a version of getScaledDimensions that takes a subset
342 // into account?
343 decodeInfo = decodeInfo.makeWH(
344 SkTMax(1, SkScalarRoundToInt(preScaleW * fscale)),
345 SkTMax(1, SkScalarRoundToInt(preScaleH * fscale)));
346 size_t rowBytes = decodeInfo.minRowBytes();
347 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes, colorTable.get(),
348 nullptr, nullptr)) {
349 SkDebugf("[terminated] Could not install pixels.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500350 return;
kjlubick2a42f482016-02-16 16:14:23 -0800351 }
352 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
353 &opts, colorPtr, colorCountPtr);
354 switch (result) {
355 case SkCodec::kSuccess:
356 case SkCodec::kIncompleteInput:
357 SkDebugf("okay\n");
358 break;
359 case SkCodec::kInvalidConversion:
360 if (0 == (x|y)) {
361 // First subset is okay to return unimplemented.
362 SkDebugf("[terminated] Incompatible colortype conversion\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500363 return;
kjlubick2a42f482016-02-16 16:14:23 -0800364 }
365 // If the first subset succeeded, a later one should not fail.
366 // fall through to failure
367 case SkCodec::kUnimplemented:
368 if (0 == (x|y)) {
369 // First subset is okay to return unimplemented.
370 SkDebugf("[terminated] subset codec not supported\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500371 return;
kjlubick2a42f482016-02-16 16:14:23 -0800372 }
373 // If the first subset succeeded, why would a later one fail?
374 // fall through to failure
375 default:
376 SkDebugf("[terminated] subset codec failed to decode (%d, %d, %d, %d) "
377 "with dimensions (%d x %d)\t error %d\n",
378 x, y, decodeInfo.width(), decodeInfo.height(),
379 W, H, result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500380 return;
kjlubick2a42f482016-02-16 16:14:23 -0800381 }
382 // translate by the scaled height.
383 top += decodeInfo.height();
384 }
385 // translate by the scaled width.
386 left += decodeInfo.width();
387 }
388 SkDebugf("[terminated] Success!\n");
389 break;
390 }
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500391 case 4: { //kAnimated_Mode
392 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
393 if (frameInfos.size() == 0) {
394 SkDebugf("[terminated] Not an animated image\n");
395 break;
396 }
397
398 for (size_t i = 0; i < frameInfos.size(); i++) {
399 options.fFrameIndex = i;
400 auto result = codec->startIncrementalDecode(decodeInfo, bitmap.getPixels(),
401 bitmap.rowBytes(), &options);
402 if (SkCodec::kSuccess != result) {
403 SkDebugf("[terminated] failed to start incremental decode "
404 "in frame %d with error %d\n", i, result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500405 return;
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500406 }
407
408 result = codec->incrementalDecode();
409 if (result == SkCodec::kIncompleteInput) {
410 SkDebugf("okay\n");
411 // Frames beyond this one will not decode.
412 break;
413 }
414 if (result == SkCodec::kSuccess) {
415 SkDebugf("okay - decoded frame %d\n", i);
416 } else {
417 SkDebugf("[terminated] incremental decode failed with "
418 "error %d\n", result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500419 return;
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500420 }
421 }
422 SkDebugf("[terminated] Success!\n");
423 break;
424 }
kjlubickdba57342016-01-21 05:03:28 -0800425 default:
kjlubick2a42f482016-02-16 16:14:23 -0800426 SkDebugf("[terminated] Mode not implemented yet\n");
kjlubickdba57342016-01-21 05:03:28 -0800427 }
428
429 dump_png(bitmap);
mtklein65e58242016-01-13 12:57:57 -0800430}
431
Kevin Lubickf80f1152017-01-06 08:26:56 -0500432static void fuzz_skp(sk_sp<SkData> bytes) {
kjlubickdba57342016-01-21 05:03:28 -0800433 SkMemoryStream stream(bytes);
434 SkDebugf("Decoding\n");
reedca2622b2016-03-18 07:25:55 -0700435 sk_sp<SkPicture> pic(SkPicture::MakeFromStream(&stream));
kjlubickdba57342016-01-21 05:03:28 -0800436 if (!pic) {
kjlubick47d158e2016-02-01 08:23:50 -0800437 SkDebugf("[terminated] Couldn't decode as a picture.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500438 return;
kjlubickdba57342016-01-21 05:03:28 -0800439 }
440 SkDebugf("Rendering\n");
441 SkBitmap bitmap;
442 if (!FLAGS_dump.isEmpty()) {
443 SkIRect size = pic->cullRect().roundOut();
444 bitmap.allocN32Pixels(size.width(), size.height());
445 }
446 SkCanvas canvas(bitmap);
447 canvas.drawPicture(pic);
kjlubick47d158e2016-02-01 08:23:50 -0800448 SkDebugf("[terminated] Success! Decoded and rendered an SkPicture!\n");
kjlubickdba57342016-01-21 05:03:28 -0800449 dump_png(bitmap);
kjlubickdba57342016-01-21 05:03:28 -0800450}
mtklein65e58242016-01-13 12:57:57 -0800451
Kevin Lubickf80f1152017-01-06 08:26:56 -0500452static void fuzz_icc(sk_sp<SkData> bytes) {
Brian Osman526972e2016-10-24 09:24:02 -0400453 sk_sp<SkColorSpace> space(SkColorSpace::MakeICC(bytes->data(), bytes->size()));
kjlubick897a8e32016-06-09 07:15:12 -0700454 if (!space) {
455 SkDebugf("[terminated] Couldn't decode ICC.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500456 return;
kjlubick897a8e32016-06-09 07:15:12 -0700457 }
458 SkDebugf("[terminated] Success! Decoded ICC.\n");
kjlubick897a8e32016-06-09 07:15:12 -0700459}
460
Kevin Lubickf80f1152017-01-06 08:26:56 -0500461static void fuzz_color_deserialize(sk_sp<SkData> bytes) {
kjlubick3e3c1a52016-06-23 10:49:27 -0700462 sk_sp<SkColorSpace> space(SkColorSpace::Deserialize(bytes->data(), bytes->size()));
463 if (!space) {
464 SkDebugf("[terminated] Couldn't deserialize Colorspace.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500465 return;
kjlubick3e3c1a52016-06-23 10:49:27 -0700466 }
467 SkDebugf("[terminated] Success! deserialized Colorspace.\n");
kjlubick3e3c1a52016-06-23 10:49:27 -0700468}
469
Kevin Lubickf04c50a2017-01-06 13:48:19 -0500470static void fuzz_path_deserialize(sk_sp<SkData> bytes) {
471 SkPath path;
472 if (!path.readFromMemory(bytes->data(), bytes->size())) {
473 SkDebugf("[terminated] Couldn't initialize SkPath.\n");
474 return;
475 }
476 SkDebugf("[terminated] Success! Initialized SkPath.\n");
477}
478
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500479#if SK_SUPPORT_GPU
Kevin Lubickf80f1152017-01-06 08:26:56 -0500480static void fuzz_sksl2glsl(sk_sp<SkData> bytes) {
kjlubicke7195772016-10-18 10:06:24 -0700481 SkSL::Compiler compiler;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500482 SkString output;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500483 SkSL::Program::Settings settings;
484 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
485 settings.fCaps = caps.get();
486 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
487 SkString((const char*) bytes->data()),
488 settings);
489 if (!program || !compiler.toGLSL(*program, &output)) {
kjlubicke7195772016-10-18 10:06:24 -0700490 SkDebugf("[terminated] Couldn't compile input.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500491 return;
kjlubicke7195772016-10-18 10:06:24 -0700492 }
493 SkDebugf("[terminated] Success! Compiled input.\n");
kjlubicke7195772016-10-18 10:06:24 -0700494}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500495#endif
kjlubicke7195772016-10-18 10:06:24 -0700496
reed42943c82016-09-12 12:01:44 -0700497Fuzz::Fuzz(sk_sp<SkData> bytes) : fBytes(bytes), fNextByte(0) {}
mtklein65e58242016-01-13 12:57:57 -0800498
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400499void Fuzz::signalBug() { SkDebugf("Signal bug\n"); raise(SIGSEGV); }
mtkleina1159422016-01-15 05:46:54 -0800500
kjlubicke5654502016-07-19 16:50:03 -0700501size_t Fuzz::size() { return fBytes->size(); }
502
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400503bool Fuzz::exhausted() {
504 return fBytes->size() == fNextByte;
kjlubick5bd98a22016-02-18 06:27:38 -0800505}