blob: 0d19027d6e2805eccc4bbb2643a92b8723c5921e [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"
Kevin Lubick0d825662018-01-03 14:38:48 -050023#include "SkPipe.h"
Mike Reedfadbfcd2017-12-06 16:09:20 -050024#include "SkReadBuffer.h"
Hal Canary62176db2017-02-27 16:42:03 -050025#include "SkRegion.h"
26#include "SkStream.h"
27#include "SkSurface.h"
Kevin Lubickd46e85f2017-11-21 17:13:35 -050028#include "SkTextBlob.h"
Hal Canary62176db2017-02-27 16:42:03 -050029
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050030#if SK_SUPPORT_GPU
kjlubicke7195772016-10-18 10:06:24 -070031#include "SkSLCompiler.h"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050032#endif
kjlubickdba57342016-01-21 05:03:28 -080033
Hal Canary62176db2017-02-27 16:42:03 -050034#include <iostream>
mtkleina1159422016-01-15 05:46:54 -080035#include <signal.h>
Hal Canarydb683012016-11-23 08:55:18 -070036#include "sk_tool_utils.h"
37
Hal Canary62176db2017-02-27 16:42:03 -050038DEFINE_string2(bytes, b, "", "A path to a file or a directory. If a file, the "
39 "contents will be used as the fuzz bytes. If a directory, all files "
40 "in the directory will be used as fuzz bytes for the fuzzer, one at a "
41 "time.");
mtkleind4387ea2016-01-21 06:13:52 -080042DEFINE_string2(name, n, "", "If --type is 'api', fuzz the API with this name.");
Hal Canary62176db2017-02-27 16:42:03 -050043DEFINE_string2(dump, d, "", "If not empty, dump 'image*' or 'skp' types as a "
44 "PNG with this name.");
Kevin Lubick9ff5dc92018-01-09 12:47:33 -050045DEFINE_bool2(verbose, v, false, "Print more information while fuzzing.");
46DEFINE_string2(type, t, "", "How to interpret --bytes, one of:\n"
47 "api\n"
48 "color_deserialize\n"
49 "filter_fuzz (equivalent to Chrome's filter_fuzz_stub)\n"
50 "icc\n"
51 "image_mode\n"
52 "image_scale\n"
53 "path_deserialize\n"
54 "pipe\n"
55 "region_deserialize\n"
56 "skp\n"
57 "sksl2glsl\n"
58 "textblob");
kjlubickdba57342016-01-21 05:03:28 -080059
Kevin Lubickf80f1152017-01-06 08:26:56 -050060static int fuzz_file(const char* path);
kjlubick2a42f482016-02-16 16:14:23 -080061static uint8_t calculate_option(SkData*);
kjlubickdba57342016-01-21 05:03:28 -080062
Kevin Lubickf80f1152017-01-06 08:26:56 -050063static void fuzz_api(sk_sp<SkData>);
Kevin Lubickf80f1152017-01-06 08:26:56 -050064static void fuzz_color_deserialize(sk_sp<SkData>);
Kevin Lubickd46e85f2017-11-21 17:13:35 -050065static void fuzz_filter_fuzz(sk_sp<SkData>);
Kevin Lubick0168e042017-02-14 13:12:37 -050066static void fuzz_icc(sk_sp<SkData>);
67static void fuzz_img(sk_sp<SkData>, uint8_t, uint8_t);
Kevin Lubickf04c50a2017-01-06 13:48:19 -050068static void fuzz_path_deserialize(sk_sp<SkData>);
Kevin Lubickedee1ae2017-02-20 17:47:18 -050069static void fuzz_region_deserialize(sk_sp<SkData>);
Kevin Lubick0168e042017-02-14 13:12:37 -050070static void fuzz_skp(sk_sp<SkData>);
Kevin Lubick0d825662018-01-03 14:38:48 -050071static void fuzz_skpipe(sk_sp<SkData>);
Kevin Lubickd46e85f2017-11-21 17:13:35 -050072static void fuzz_textblob_deserialize(sk_sp<SkData>);
Herb Derbya839fc02017-03-16 12:30:43 -040073
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050074#if SK_SUPPORT_GPU
Kevin Lubickf80f1152017-01-06 08:26:56 -050075static void fuzz_sksl2glsl(sk_sp<SkData>);
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050076#endif
mtklein65e58242016-01-13 12:57:57 -080077
78int main(int argc, char** argv) {
Kevin Lubick9ff5dc92018-01-09 12:47:33 -050079 SkCommandLineFlags::SetUsage("Usage: fuzz -t <type> -b <path/to/file> [-n api-to-fuzz]\n"
80 "--help lists the valid types\n");
mtkleinf5e97822016-01-15 06:19:53 -080081 SkCommandLineFlags::Parse(argc, argv);
82
mtkleind0b82342016-01-15 07:56:20 -080083 const char* path = FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0];
Kevin Lubickf80f1152017-01-06 08:26:56 -050084
85 if (!sk_isdir(path)) {
86 return fuzz_file(path);
87 }
88
89 SkOSFile::Iter it(path);
90 for (SkString file; it.next(&file); ) {
91 SkString p = SkOSPath::Join(path, file.c_str());
92 SkDebugf("Fuzzing %s\n", p.c_str());
93 int rv = fuzz_file(p.c_str());
94 if (rv != 0) {
95 return rv;
96 }
97 }
98 return 0;
99}
100
101static int fuzz_file(const char* path) {
bungeman38d909e2016-08-02 14:40:46 -0700102 sk_sp<SkData> bytes(SkData::MakeFromFileName(path));
kjlubickdba57342016-01-21 05:03:28 -0800103 if (!bytes) {
104 SkDebugf("Could not read %s\n", path);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500105 return 1;
kjlubickdba57342016-01-21 05:03:28 -0800106 }
mtklein65e58242016-01-13 12:57:57 -0800107
mtkleind4387ea2016-01-21 06:13:52 -0800108 if (!FLAGS_type.isEmpty()) {
kjlubicke7195772016-10-18 10:06:24 -0700109 if (0 == strcmp("api", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -0500110 fuzz_api(bytes);
111 return 0;
kjlubicke7195772016-10-18 10:06:24 -0700112 }
113 if (0 == strcmp("color_deserialize", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -0500114 fuzz_color_deserialize(bytes);
115 return 0;
kjlubicke7195772016-10-18 10:06:24 -0700116 }
117 if (0 == strcmp("icc", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -0500118 fuzz_icc(bytes);
119 return 0;
kjlubicke7195772016-10-18 10:06:24 -0700120 }
121 if (0 == strcmp("image_scale", FLAGS_type[0])) {
Kevin Lubickc9d28292017-11-09 08:12:56 -0500122 uint8_t option = calculate_option(bytes.get());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500123 fuzz_img(bytes, option, 0);
124 return 0;
kjlubicke7195772016-10-18 10:06:24 -0700125 }
126 if (0 == strcmp("image_mode", FLAGS_type[0])) {
Kevin Lubickc9d28292017-11-09 08:12:56 -0500127 uint8_t option = calculate_option(bytes.get());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500128 fuzz_img(bytes, 0, option);
129 return 0;
kjlubicke7195772016-10-18 10:06:24 -0700130 }
Kevin Lubickf04c50a2017-01-06 13:48:19 -0500131 if (0 == strcmp("path_deserialize", FLAGS_type[0])) {
132 fuzz_path_deserialize(bytes);
133 return 0;
134 }
Kevin Lubickedee1ae2017-02-20 17:47:18 -0500135 if (0 == strcmp("region_deserialize", FLAGS_type[0])) {
136 fuzz_region_deserialize(bytes);
137 return 0;
138 }
Kevin Lubick0d825662018-01-03 14:38:48 -0500139 if (0 == strcmp("pipe", FLAGS_type[0])) {
140 fuzz_skpipe(bytes);
141 return 0;
142 }
kjlubicke7195772016-10-18 10:06:24 -0700143 if (0 == strcmp("skp", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -0500144 fuzz_skp(bytes);
145 return 0;
kjlubicke7195772016-10-18 10:06:24 -0700146 }
Herb Derbya839fc02017-03-16 12:30:43 -0400147 if (0 == strcmp("filter_fuzz", FLAGS_type[0])) {
148 fuzz_filter_fuzz(bytes);
149 return 0;
150 }
Kevin Lubickd46e85f2017-11-21 17:13:35 -0500151 if (0 == strcmp("textblob", FLAGS_type[0])) {
152 fuzz_textblob_deserialize(bytes);
153 return 0;
154 }
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500155#if SK_SUPPORT_GPU
kjlubicke7195772016-10-18 10:06:24 -0700156 if (0 == strcmp("sksl2glsl", FLAGS_type[0])) {
Kevin Lubickf80f1152017-01-06 08:26:56 -0500157 fuzz_sksl2glsl(bytes);
158 return 0;
mtkleind4387ea2016-01-21 06:13:52 -0800159 }
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500160#endif
kjlubickdba57342016-01-21 05:03:28 -0800161 }
Kevin Lubick9ff5dc92018-01-09 12:47:33 -0500162 SkCommandLineFlags::PrintUsage();
163 return 1;
kjlubickdba57342016-01-21 05:03:28 -0800164}
165
kjlubick2a42f482016-02-16 16:14:23 -0800166// This adds up the first 1024 bytes and returns it as an 8 bit integer. This allows afl-fuzz to
167// deterministically excercise different paths, or *options* (such as different scaling sizes or
168// different image modes) without needing to introduce a parameter. This way we don't need a
169// image_scale1, image_scale2, image_scale4, etc fuzzer, we can just have a image_scale fuzzer.
170// Clients are expected to transform this number into a different range, e.g. with modulo (%).
171static uint8_t calculate_option(SkData* bytes) {
172 uint8_t total = 0;
173 const uint8_t* data = bytes->bytes();
174 for (size_t i = 0; i < 1024 && i < bytes->size(); i++) {
175 total += data[i];
176 }
177 return total;
178}
179
Kevin Lubickf80f1152017-01-06 08:26:56 -0500180static void fuzz_api(sk_sp<SkData> bytes) {
mtkleind4387ea2016-01-21 06:13:52 -0800181 const char* name = FLAGS_name.isEmpty() ? "" : FLAGS_name[0];
182
Mike Reedab273fa2017-01-11 13:58:55 -0500183 for (auto r = sk_tools::Registry<Fuzzable>::Head(); r; r = r->next()) {
mtklein65e58242016-01-13 12:57:57 -0800184 auto fuzzable = r->factory();
mtkleind4387ea2016-01-21 06:13:52 -0800185 if (0 == strcmp(name, fuzzable.name)) {
mtkleinf5e97822016-01-15 06:19:53 -0800186 SkDebugf("Fuzzing %s...\n", fuzzable.name);
Kevin Lubick1ac8fd22017-03-01 10:42:45 -0500187 Fuzz fuzz(std::move(bytes));
mtklein65e58242016-01-13 12:57:57 -0800188 fuzzable.fn(&fuzz);
kjlubick47d158e2016-02-01 08:23:50 -0800189 SkDebugf("[terminated] Success!\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500190 return;
mtklein65e58242016-01-13 12:57:57 -0800191 }
192 }
mtkleind4387ea2016-01-21 06:13:52 -0800193
194 SkDebugf("When using --type api, please choose an API to fuzz with --name/-n:\n");
Mike Reedab273fa2017-01-11 13:58:55 -0500195 for (auto r = sk_tools::Registry<Fuzzable>::Head(); r; r = r->next()) {
mtkleind4387ea2016-01-21 06:13:52 -0800196 auto fuzzable = r->factory();
197 SkDebugf("\t%s\n", fuzzable.name);
198 }
kjlubickdba57342016-01-21 05:03:28 -0800199}
200
201static void dump_png(SkBitmap bitmap) {
202 if (!FLAGS_dump.isEmpty()) {
Hal Canarydb683012016-11-23 08:55:18 -0700203 sk_tool_utils::EncodeImageToFile(FLAGS_dump[0], bitmap, SkEncodedImageFormat::kPNG, 100);
kjlubickdba57342016-01-21 05:03:28 -0800204 SkDebugf("Dumped to %s\n", FLAGS_dump[0]);
205 }
206}
207
Kevin Lubickf80f1152017-01-06 08:26:56 -0500208static void fuzz_img(sk_sp<SkData> bytes, uint8_t scale, uint8_t mode) {
kjlubick2a42f482016-02-16 16:14:23 -0800209 // We can scale 1x, 2x, 4x, 8x, 16x
210 scale = scale % 5;
kjlubick5bd98a22016-02-18 06:27:38 -0800211 float fscale = (float)pow(2.0f, scale);
212 SkDebugf("Scaling factor: %f\n", fscale);
kjlubick2a42f482016-02-16 16:14:23 -0800213
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500214 // We have 5 different modes of decoding.
215 mode = mode % 5;
kjlubick2a42f482016-02-16 16:14:23 -0800216 SkDebugf("Mode: %d\n", mode);
217
218 // This is mostly copied from DMSrcSink's CodecSrc::draw method.
kjlubick47d158e2016-02-01 08:23:50 -0800219 SkDebugf("Decoding\n");
Mike Reedede7bac2017-07-23 15:30:02 -0400220 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(bytes));
kjlubickdba57342016-01-21 05:03:28 -0800221 if (nullptr == codec.get()) {
kjlubick47d158e2016-02-01 08:23:50 -0800222 SkDebugf("[terminated] Couldn't create codec.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500223 return;
kjlubickdba57342016-01-21 05:03:28 -0800224 }
225
226 SkImageInfo decodeInfo = codec->getInfo();
kjlubick2a42f482016-02-16 16:14:23 -0800227 SkISize size = codec->getScaledDimensions(fscale);
228 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
229
kjlubickdba57342016-01-21 05:03:28 -0800230 SkBitmap bitmap;
kjlubickdba57342016-01-21 05:03:28 -0800231 SkCodec::Options options;
232 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
233
Mike Reed086a4272017-07-18 10:53:11 -0400234 if (!bitmap.tryAllocPixelsFlags(decodeInfo, SkBitmap::kZeroPixels_AllocFlag)) {
kjlubick47d158e2016-02-01 08:23:50 -0800235 SkDebugf("[terminated] Could not allocate memory. Image might be too large (%d x %d)",
kjlubick2a42f482016-02-16 16:14:23 -0800236 decodeInfo.width(), decodeInfo.height());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500237 return;
kjlubickdba57342016-01-21 05:03:28 -0800238 }
239
kjlubick2a42f482016-02-16 16:14:23 -0800240 switch (mode) {
241 case 0: {//kCodecZeroInit_Mode, kCodec_Mode
Leon Scroggins571b30f2017-07-11 17:35:31 +0000242 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options)) {
kjlubick2a42f482016-02-16 16:14:23 -0800243 case SkCodec::kSuccess:
244 SkDebugf("[terminated] Success!\n");
245 break;
246 case SkCodec::kIncompleteInput:
247 SkDebugf("[terminated] Partial Success\n");
248 break;
Leon Scroggins III674a1842017-07-06 12:26:09 -0400249 case SkCodec::kErrorInInput:
250 SkDebugf("[terminated] Partial Success with error\n");
251 break;
kjlubick2a42f482016-02-16 16:14:23 -0800252 case SkCodec::kInvalidConversion:
253 SkDebugf("Incompatible colortype conversion\n");
254 // Crash to allow afl-fuzz to know this was a bug.
255 raise(SIGSEGV);
256 default:
257 SkDebugf("[terminated] Couldn't getPixels.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500258 return;
kjlubick2a42f482016-02-16 16:14:23 -0800259 }
260 break;
261 }
262 case 1: {//kScanline_Mode
Leon Scroggins571b30f2017-07-11 17:35:31 +0000263 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) {
264 SkDebugf("[terminated] Could not start scanline decoder\n");
265 return;
266 }
kjlubick2a42f482016-02-16 16:14:23 -0800267
268 void* dst = bitmap.getAddr(0, 0);
269 size_t rowBytes = bitmap.rowBytes();
270 uint32_t height = decodeInfo.height();
271 switch (codec->getScanlineOrder()) {
272 case SkCodec::kTopDown_SkScanlineOrder:
273 case SkCodec::kBottomUp_SkScanlineOrder:
kjlubick2a42f482016-02-16 16:14:23 -0800274 // We do not need to check the return value. On an incomplete
275 // image, memory will be filled with a default value.
276 codec->getScanlines(dst, height, rowBytes);
277 break;
kjlubick2a42f482016-02-16 16:14:23 -0800278 }
kjlubick47d158e2016-02-01 08:23:50 -0800279 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800280 break;
kjlubick2a42f482016-02-16 16:14:23 -0800281 }
282 case 2: { //kStripe_Mode
283 const int height = decodeInfo.height();
284 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
285 // does not align with image blocks.
286 const int stripeHeight = 37;
287 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
288
289 // Decode odd stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000290 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)
kjlubick2a42f482016-02-16 16:14:23 -0800291 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
292 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
293 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
294 // to run this test for image types that do not have this scanline ordering.
295 SkDebugf("[terminated] Could not start top-down scanline decoder\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500296 return;
kjlubick2a42f482016-02-16 16:14:23 -0800297 }
298
299 for (int i = 0; i < numStripes; i += 2) {
300 // Skip a stripe
301 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
302 codec->skipScanlines(linesToSkip);
303
304 // Read a stripe
305 const int startY = (i + 1) * stripeHeight;
306 const int linesToRead = SkTMin(stripeHeight, height - startY);
307 if (linesToRead > 0) {
308 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
309 }
310 }
311
312 // Decode even stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000313 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo);
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 Scroggins571b30f2017-07-11 17:35:31 +0000372 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes)) {
kjlubick2a42f482016-02-16 16:14:23 -0800373 SkDebugf("[terminated] Could not install pixels.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500374 return;
kjlubick2a42f482016-02-16 16:14:23 -0800375 }
376 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000377 &opts);
kjlubick2a42f482016-02-16 16:14:23 -0800378 switch (result) {
379 case SkCodec::kSuccess:
380 case SkCodec::kIncompleteInput:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400381 case SkCodec::kErrorInInput:
kjlubick2a42f482016-02-16 16:14:23 -0800382 SkDebugf("okay\n");
383 break;
384 case SkCodec::kInvalidConversion:
385 if (0 == (x|y)) {
386 // First subset is okay to return unimplemented.
387 SkDebugf("[terminated] Incompatible colortype conversion\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500388 return;
kjlubick2a42f482016-02-16 16:14:23 -0800389 }
390 // If the first subset succeeded, a later one should not fail.
391 // fall through to failure
392 case SkCodec::kUnimplemented:
393 if (0 == (x|y)) {
394 // First subset is okay to return unimplemented.
395 SkDebugf("[terminated] subset codec not supported\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500396 return;
kjlubick2a42f482016-02-16 16:14:23 -0800397 }
398 // If the first subset succeeded, why would a later one fail?
399 // fall through to failure
400 default:
401 SkDebugf("[terminated] subset codec failed to decode (%d, %d, %d, %d) "
402 "with dimensions (%d x %d)\t error %d\n",
403 x, y, decodeInfo.width(), decodeInfo.height(),
404 W, H, result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500405 return;
kjlubick2a42f482016-02-16 16:14:23 -0800406 }
407 // translate by the scaled height.
408 top += decodeInfo.height();
409 }
410 // translate by the scaled width.
411 left += decodeInfo.width();
412 }
413 SkDebugf("[terminated] Success!\n");
414 break;
415 }
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500416 case 4: { //kAnimated_Mode
417 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
418 if (frameInfos.size() == 0) {
419 SkDebugf("[terminated] Not an animated image\n");
420 break;
421 }
422
423 for (size_t i = 0; i < frameInfos.size(); i++) {
424 options.fFrameIndex = i;
425 auto result = codec->startIncrementalDecode(decodeInfo, bitmap.getPixels(),
426 bitmap.rowBytes(), &options);
427 if (SkCodec::kSuccess != result) {
428 SkDebugf("[terminated] failed to start incremental decode "
429 "in frame %d with error %d\n", i, result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500430 return;
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500431 }
432
433 result = codec->incrementalDecode();
Leon Scroggins III674a1842017-07-06 12:26:09 -0400434 if (result == SkCodec::kIncompleteInput || result == SkCodec::kErrorInInput) {
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500435 SkDebugf("okay\n");
436 // Frames beyond this one will not decode.
437 break;
438 }
439 if (result == SkCodec::kSuccess) {
440 SkDebugf("okay - decoded frame %d\n", i);
441 } else {
442 SkDebugf("[terminated] incremental decode failed with "
443 "error %d\n", result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500444 return;
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500445 }
446 }
447 SkDebugf("[terminated] Success!\n");
448 break;
449 }
kjlubickdba57342016-01-21 05:03:28 -0800450 default:
kjlubick2a42f482016-02-16 16:14:23 -0800451 SkDebugf("[terminated] Mode not implemented yet\n");
kjlubickdba57342016-01-21 05:03:28 -0800452 }
453
454 dump_png(bitmap);
mtklein65e58242016-01-13 12:57:57 -0800455}
456
Kevin Lubickf80f1152017-01-06 08:26:56 -0500457static void fuzz_skp(sk_sp<SkData> bytes) {
Kevin Lubick09757b22017-12-12 12:52:39 -0500458 SkReadBuffer buf(bytes->data(), bytes->size());
kjlubickdba57342016-01-21 05:03:28 -0800459 SkDebugf("Decoding\n");
Kevin Lubick09757b22017-12-12 12:52:39 -0500460 sk_sp<SkPicture> pic(SkPicture::MakeFromBuffer(buf));
kjlubickdba57342016-01-21 05:03:28 -0800461 if (!pic) {
kjlubick47d158e2016-02-01 08:23:50 -0800462 SkDebugf("[terminated] Couldn't decode as a picture.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500463 return;
kjlubickdba57342016-01-21 05:03:28 -0800464 }
465 SkDebugf("Rendering\n");
466 SkBitmap bitmap;
467 if (!FLAGS_dump.isEmpty()) {
468 SkIRect size = pic->cullRect().roundOut();
469 bitmap.allocN32Pixels(size.width(), size.height());
470 }
471 SkCanvas canvas(bitmap);
472 canvas.drawPicture(pic);
kjlubick47d158e2016-02-01 08:23:50 -0800473 SkDebugf("[terminated] Success! Decoded and rendered an SkPicture!\n");
kjlubickdba57342016-01-21 05:03:28 -0800474 dump_png(bitmap);
kjlubickdba57342016-01-21 05:03:28 -0800475}
mtklein65e58242016-01-13 12:57:57 -0800476
Kevin Lubick0d825662018-01-03 14:38:48 -0500477static void fuzz_skpipe(sk_sp<SkData> bytes) {
478 SkPipeDeserializer d;
479 SkDebugf("Decoding\n");
480 sk_sp<SkPicture> pic(d.readPicture(bytes.get()));
481 if (!pic) {
482 SkDebugf("[terminated] Couldn't decode picture via SkPipe.\n");
483 return;
484 }
485 SkDebugf("Rendering\n");
486 SkBitmap bitmap;
487 SkCanvas canvas(bitmap);
488 canvas.drawPicture(pic);
489 SkDebugf("[terminated] Success! Decoded and rendered an SkPicture from SkPipe!\n");
490}
491
Kevin Lubickf80f1152017-01-06 08:26:56 -0500492static void fuzz_icc(sk_sp<SkData> bytes) {
Brian Osman526972e2016-10-24 09:24:02 -0400493 sk_sp<SkColorSpace> space(SkColorSpace::MakeICC(bytes->data(), bytes->size()));
kjlubick897a8e32016-06-09 07:15:12 -0700494 if (!space) {
495 SkDebugf("[terminated] Couldn't decode ICC.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500496 return;
kjlubick897a8e32016-06-09 07:15:12 -0700497 }
498 SkDebugf("[terminated] Success! Decoded ICC.\n");
kjlubick897a8e32016-06-09 07:15:12 -0700499}
500
Kevin Lubickf80f1152017-01-06 08:26:56 -0500501static void fuzz_color_deserialize(sk_sp<SkData> bytes) {
kjlubick3e3c1a52016-06-23 10:49:27 -0700502 sk_sp<SkColorSpace> space(SkColorSpace::Deserialize(bytes->data(), bytes->size()));
503 if (!space) {
504 SkDebugf("[terminated] Couldn't deserialize Colorspace.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500505 return;
kjlubick3e3c1a52016-06-23 10:49:27 -0700506 }
507 SkDebugf("[terminated] Success! deserialized Colorspace.\n");
kjlubick3e3c1a52016-06-23 10:49:27 -0700508}
509
Kevin Lubickf04c50a2017-01-06 13:48:19 -0500510static void fuzz_path_deserialize(sk_sp<SkData> bytes) {
511 SkPath path;
Mike Reedfadbfcd2017-12-06 16:09:20 -0500512 SkReadBuffer buf(bytes->data(), bytes->size());
Kevin Lubickc9d28292017-11-09 08:12:56 -0500513 buf.readPath(&path);
514 if (!buf.isValid()) {
515 SkDebugf("[terminated] Couldn't deserialize SkPath.\n");
Kevin Lubickf04c50a2017-01-06 13:48:19 -0500516 return;
517 }
Kevin Lubickc9d28292017-11-09 08:12:56 -0500518
Kevin Lubick0d4bc0d2017-02-21 09:37:48 -0500519 auto s = SkSurface::MakeRasterN32Premul(1024, 1024);
520 s->getCanvas()->drawPath(path, SkPaint());
Kevin Lubickf04c50a2017-01-06 13:48:19 -0500521 SkDebugf("[terminated] Success! Initialized SkPath.\n");
522}
523
Kevin Lubickedee1ae2017-02-20 17:47:18 -0500524static void fuzz_region_deserialize(sk_sp<SkData> bytes) {
525 SkRegion region;
526 if (!region.readFromMemory(bytes->data(), bytes->size())) {
527 SkDebugf("[terminated] Couldn't initialize SkRegion.\n");
528 return;
529 }
530 region.computeRegionComplexity();
531 region.isComplex();
532 SkRegion r2;
533 if (region == r2) {
534 region.contains(0,0);
535 } else {
536 region.contains(1,1);
537 }
538 auto s = SkSurface::MakeRasterN32Premul(1024, 1024);
539 s->getCanvas()->drawRegion(region, SkPaint());
540 SkDEBUGCODE(region.validate());
541 SkDebugf("[terminated] Success! Initialized SkRegion.\n");
542}
543
Kevin Lubickd46e85f2017-11-21 17:13:35 -0500544static void fuzz_textblob_deserialize(sk_sp<SkData> bytes) {
Mike Reedfadbfcd2017-12-06 16:09:20 -0500545 SkReadBuffer buf(bytes->data(), bytes->size());
Kevin Lubickd46e85f2017-11-21 17:13:35 -0500546 auto tb = SkTextBlob::MakeFromBuffer(buf);
547 if (!buf.isValid()) {
548 SkDebugf("[terminated] Couldn't deserialize SkTextBlob.\n");
549 return;
550 }
551
552 auto s = SkSurface::MakeRasterN32Premul(512, 512);
553 s->getCanvas()->drawTextBlob(tb, 200, 200, SkPaint());
554 SkDebugf("[terminated] Success! Initialized SkTextBlob.\n");
555}
556
Herb Derbya839fc02017-03-16 12:30:43 -0400557static void fuzz_filter_fuzz(sk_sp<SkData> bytes) {
558
559 const int BitmapSize = 24;
560 SkBitmap bitmap;
561 bitmap.allocN32Pixels(BitmapSize, BitmapSize);
562 SkCanvas canvas(bitmap);
563 canvas.clear(0x00000000);
564
565 sk_sp<SkImageFilter> flattenable = SkValidatingDeserializeImageFilter(
566 bytes->data(), bytes->size());
567
568 // Adding some info, but the test passed if we got here without any trouble
Ben Wagnera93a14a2017-08-28 10:34:05 -0400569 if (flattenable != nullptr) {
Herb Derbya839fc02017-03-16 12:30:43 -0400570 SkDebugf("Valid stream detected.\n");
571 // Let's see if using the filters can cause any trouble...
572 SkPaint paint;
573 paint.setImageFilter(flattenable);
574 canvas.save();
575 canvas.clipRect(SkRect::MakeXYWH(
576 0, 0, SkIntToScalar(BitmapSize), SkIntToScalar(BitmapSize)));
577
578 // This call shouldn't crash or cause ASAN to flag any memory issues
579 // If nothing bad happens within this call, everything is fine
580 canvas.drawBitmap(bitmap, 0, 0, &paint);
581
582 SkDebugf("Filter DAG rendered successfully\n");
583 canvas.restore();
584 } else {
585 SkDebugf("Invalid stream detected.\n");
586 }
587
588 SkDebugf("[terminated] Done\n");
589}
590
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500591#if SK_SUPPORT_GPU
Kevin Lubickf80f1152017-01-06 08:26:56 -0500592static void fuzz_sksl2glsl(sk_sp<SkData> bytes) {
kjlubicke7195772016-10-18 10:06:24 -0700593 SkSL::Compiler compiler;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400594 SkSL::String output;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500595 SkSL::Program::Settings settings;
596 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
597 settings.fCaps = caps.get();
598 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
Brian Osman93ba0a42017-08-14 14:48:10 -0400599 SkSL::String((const char*) bytes->data()),
600 settings);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500601 if (!program || !compiler.toGLSL(*program, &output)) {
kjlubicke7195772016-10-18 10:06:24 -0700602 SkDebugf("[terminated] Couldn't compile input.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500603 return;
kjlubicke7195772016-10-18 10:06:24 -0700604 }
605 SkDebugf("[terminated] Success! Compiled input.\n");
kjlubicke7195772016-10-18 10:06:24 -0700606}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500607#endif
kjlubicke7195772016-10-18 10:06:24 -0700608
reed42943c82016-09-12 12:01:44 -0700609Fuzz::Fuzz(sk_sp<SkData> bytes) : fBytes(bytes), fNextByte(0) {}
mtklein65e58242016-01-13 12:57:57 -0800610
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400611void Fuzz::signalBug() { SkDebugf("Signal bug\n"); raise(SIGSEGV); }
mtkleina1159422016-01-15 05:46:54 -0800612
kjlubicke5654502016-07-19 16:50:03 -0700613size_t Fuzz::size() { return fBytes->size(); }
614
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400615bool Fuzz::exhausted() {
616 return fBytes->size() == fNextByte;
kjlubick5bd98a22016-02-18 06:27:38 -0800617}