blob: 13b49b8c0fa8891e9d54c49aa37fadd3fac680cd [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 Lubickf80f1152017-01-06 08:26:56 -050016#include "SkOSFile.h"
17#include "SkOSPath.h"
Herb Derbya839fc02017-03-16 12:30:43 -040018#include "SkPaint.h"
Hal Canary62176db2017-02-27 16:42:03 -050019#include "SkPath.h"
Cary Clarkefd99cc2018-06-11 16:25:43 -040020#include "SkPicturePriv.h"
Kevin Lubick0d825662018-01-03 14:38:48 -050021#include "SkPipe.h"
Mike Reedfadbfcd2017-12-06 16:09:20 -050022#include "SkReadBuffer.h"
Hal Canary62176db2017-02-27 16:42:03 -050023#include "SkStream.h"
24#include "SkSurface.h"
Kevin Lubickd46e85f2017-11-21 17:13:35 -050025#include "SkTextBlob.h"
Hal Canary62176db2017-02-27 16:42:03 -050026
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>
Kevin Lubickacd456a2018-04-24 13:58:16 -040032#include <map>
Kevin Lubickfffa6412018-04-23 16:44:55 -040033#include <regex>
mtkleina1159422016-01-15 05:46:54 -080034#include <signal.h>
Hal Canarydb683012016-11-23 08:55:18 -070035#include "sk_tool_utils.h"
36
Kevin Lubick2541edf2018-01-11 10:27:14 -050037
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.");
Florin Malita7796f002018-06-08 12:25:38 -040046
47// This cannot be inlined in DEFINE_string2 due to interleaved ifdefs
48static constexpr char g_type_message[] = "How to interpret --bytes, one of:\n"
49 "animated_image_decode\n"
50 "api\n"
51 "color_deserialize\n"
52 "filter_fuzz (equivalent to Chrome's filter_fuzz_stub)\n"
53 "image_decode\n"
54 "image_mode\n"
55 "image_scale\n"
Florin Malita80452be2018-06-19 11:27:20 -040056 "json\n"
Florin Malita7796f002018-06-08 12:25:38 -040057 "path_deserialize\n"
58 "pipe\n"
59 "region_deserialize\n"
60 "region_set_path\n"
Florin Malita7796f002018-06-08 12:25:38 -040061 "skp\n"
62 "sksl2glsl\n"
63#if defined(SK_ENABLE_SKOTTIE)
64 "skottie_json\n"
65#endif
66 "textblob";
67
68DEFINE_string2(type, t, "", g_type_message);
kjlubickdba57342016-01-21 05:03:28 -080069
Kevin Lubickfffa6412018-04-23 16:44:55 -040070static int fuzz_file(SkString path, SkString type);
kjlubick2a42f482016-02-16 16:14:23 -080071static uint8_t calculate_option(SkData*);
Kevin Lubickfffa6412018-04-23 16:44:55 -040072static SkString try_auto_detect(SkString path, SkString* name);
kjlubickdba57342016-01-21 05:03:28 -080073
Kevin Lubickfffa6412018-04-23 16:44:55 -040074static void fuzz_api(sk_sp<SkData> bytes, SkString name);
Kevin Lubickf80f1152017-01-06 08:26:56 -050075static void fuzz_color_deserialize(sk_sp<SkData>);
Kevin Lubickd46e85f2017-11-21 17:13:35 -050076static void fuzz_filter_fuzz(sk_sp<SkData>);
Kevin Lubick2416f962018-02-12 08:26:39 -050077static void fuzz_img2(sk_sp<SkData>);
78static void fuzz_animated_img(sk_sp<SkData>);
Kevin Lubick0168e042017-02-14 13:12:37 -050079static void fuzz_img(sk_sp<SkData>, uint8_t, uint8_t);
Florin Malita80452be2018-06-19 11:27:20 -040080static void fuzz_json(sk_sp<SkData>);
Kevin Lubickf04c50a2017-01-06 13:48:19 -050081static void fuzz_path_deserialize(sk_sp<SkData>);
Kevin Lubickedee1ae2017-02-20 17:47:18 -050082static void fuzz_region_deserialize(sk_sp<SkData>);
Kevin Lubick2541edf2018-01-11 10:27:14 -050083static void fuzz_region_set_path(sk_sp<SkData>);
Kevin Lubick0168e042017-02-14 13:12:37 -050084static void fuzz_skp(sk_sp<SkData>);
Kevin Lubick0d825662018-01-03 14:38:48 -050085static void fuzz_skpipe(sk_sp<SkData>);
Kevin Lubickd46e85f2017-11-21 17:13:35 -050086static void fuzz_textblob_deserialize(sk_sp<SkData>);
Herb Derbya839fc02017-03-16 12:30:43 -040087
Kevin Lubickfffa6412018-04-23 16:44:55 -040088static void print_api_names();
89
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050090#if SK_SUPPORT_GPU
Kevin Lubickf80f1152017-01-06 08:26:56 -050091static void fuzz_sksl2glsl(sk_sp<SkData>);
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050092#endif
mtklein65e58242016-01-13 12:57:57 -080093
Florin Malita0b0d93d2018-05-04 15:01:03 -040094#if defined(SK_ENABLE_SKOTTIE)
95static void fuzz_skottie_json(sk_sp<SkData>);
96#endif
97
mtklein65e58242016-01-13 12:57:57 -080098int main(int argc, char** argv) {
Kevin Lubick9ff5dc92018-01-09 12:47:33 -050099 SkCommandLineFlags::SetUsage("Usage: fuzz -t <type> -b <path/to/file> [-n api-to-fuzz]\n"
Kevin Lubickfffa6412018-04-23 16:44:55 -0400100 " fuzz -b <path/to/file>\n"
101 "--help lists the valid types. If type is not specified,\n"
102 "fuzz will make a guess based on the name of the file.\n");
mtkleinf5e97822016-01-15 06:19:53 -0800103 SkCommandLineFlags::Parse(argc, argv);
104
Kevin Lubickfffa6412018-04-23 16:44:55 -0400105 SkString path = SkString(FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0]);
106 SkString type = SkString(FLAGS_type.isEmpty() ? "" : FLAGS_type[0]);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500107
Kevin Lubickfffa6412018-04-23 16:44:55 -0400108 if (!sk_isdir(path.c_str())) {
109 return fuzz_file(path, type);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500110 }
111
Kevin Lubickfffa6412018-04-23 16:44:55 -0400112 SkOSFile::Iter it(path.c_str());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500113 for (SkString file; it.next(&file); ) {
Kevin Lubickfffa6412018-04-23 16:44:55 -0400114 SkString p = SkOSPath::Join(path.c_str(), file.c_str());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500115 SkDebugf("Fuzzing %s\n", p.c_str());
Kevin Lubickfffa6412018-04-23 16:44:55 -0400116 int rv = fuzz_file(p, type);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500117 if (rv != 0) {
118 return rv;
119 }
120 }
121 return 0;
122}
123
Kevin Lubickfffa6412018-04-23 16:44:55 -0400124static int fuzz_file(SkString path, SkString type) {
125 sk_sp<SkData> bytes(SkData::MakeFromFileName(path.c_str()));
kjlubickdba57342016-01-21 05:03:28 -0800126 if (!bytes) {
Kevin Lubickfffa6412018-04-23 16:44:55 -0400127 SkDebugf("Could not read %s\n", path.c_str());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500128 return 1;
kjlubickdba57342016-01-21 05:03:28 -0800129 }
mtklein65e58242016-01-13 12:57:57 -0800130
Kevin Lubickfffa6412018-04-23 16:44:55 -0400131 SkString name = SkString(FLAGS_name.isEmpty() ? "" : FLAGS_name[0]);
132
133 if (type.isEmpty()) {
134 type = try_auto_detect(path, &name);
kjlubickdba57342016-01-21 05:03:28 -0800135 }
Kevin Lubickfffa6412018-04-23 16:44:55 -0400136
137 if (type.isEmpty()) {
138 SkDebugf("Could not autodetect type of %s\n", path.c_str());
139 return 1;
140 }
141
142 if (type.equals("animated_image_decode")) {
143 fuzz_animated_img(bytes);
144 return 0;
145 }
146 if (type.equals("api")) {
147 fuzz_api(bytes, name);
148 return 0;
149 }
150 if (type.equals("color_deserialize")) {
151 fuzz_color_deserialize(bytes);
152 return 0;
153 }
Florin Malita80452be2018-06-19 11:27:20 -0400154 if (type.equals("filter_fuzz")) {
155 fuzz_filter_fuzz(bytes);
156 return 0;
157 }
Kevin Lubickfffa6412018-04-23 16:44:55 -0400158 if (type.equals("image_decode")) {
159 fuzz_img2(bytes);
160 return 0;
161 }
162 if (type.equals("image_scale")) {
163 uint8_t option = calculate_option(bytes.get());
164 fuzz_img(bytes, option, 0);
165 return 0;
166 }
167 if (type.equals("image_mode")) {
168 uint8_t option = calculate_option(bytes.get());
169 fuzz_img(bytes, 0, option);
170 return 0;
171 }
Florin Malita80452be2018-06-19 11:27:20 -0400172 if (type.equals("json")) {
173 fuzz_json(bytes);
Kevin Lubick9eeede22018-05-03 16:26:10 -0400174 return 0;
175 }
Kevin Lubickfffa6412018-04-23 16:44:55 -0400176 if (type.equals("path_deserialize")) {
177 fuzz_path_deserialize(bytes);
178 return 0;
179 }
180 if (type.equals("region_deserialize")) {
181 fuzz_region_deserialize(bytes);
182 return 0;
183 }
184 if (type.equals("region_set_path")) {
185 fuzz_region_set_path(bytes);
186 return 0;
187 }
188 if (type.equals("pipe")) {
189 fuzz_skpipe(bytes);
190 return 0;
191 }
Florin Malita0b0d93d2018-05-04 15:01:03 -0400192#if defined(SK_ENABLE_SKOTTIE)
Kevin Lubick9eeede22018-05-03 16:26:10 -0400193 if (type.equals("skottie_json")) {
194 fuzz_skottie_json(bytes);
Kevin Lubickfffa6412018-04-23 16:44:55 -0400195 return 0;
196 }
Florin Malita0b0d93d2018-05-04 15:01:03 -0400197#endif
Kevin Lubick9eeede22018-05-03 16:26:10 -0400198 if (type.equals("skp")) {
199 fuzz_skp(bytes);
Kevin Lubickfffa6412018-04-23 16:44:55 -0400200 return 0;
201 }
202 if (type.equals("textblob")) {
203 fuzz_textblob_deserialize(bytes);
204 return 0;
205 }
206#if SK_SUPPORT_GPU
207 if (type.equals("sksl2glsl")) {
208 fuzz_sksl2glsl(bytes);
209 return 0;
210 }
211#endif
Kevin Lubick457fa972018-05-29 09:22:06 -0400212 SkDebugf("Unknown type %s\n", type.c_str());
Kevin Lubick9ff5dc92018-01-09 12:47:33 -0500213 SkCommandLineFlags::PrintUsage();
214 return 1;
kjlubickdba57342016-01-21 05:03:28 -0800215}
216
Kevin Lubickfffa6412018-04-23 16:44:55 -0400217static std::map<std::string, std::string> cf_api_map = {
218 {"api_draw_functions", "DrawFunctions"},
219 {"api_gradients", "Gradients"},
220 {"api_image_filter", "ImageFilter"},
221 {"api_mock_gpu_canvas", "MockGPUCanvas"},
222 {"api_null_canvas", "NullCanvas"},
223 {"api_path_measure", "PathMeasure"},
224 {"api_raster_n32_canvas", "RasterN32Canvas"},
225 {"jpeg_encoder", "JPEGEncoder"},
226 {"png_encoder", "PNGEncoder"},
Kevin Lubick6c560552018-06-20 09:29:23 -0400227 {"skia_pathop_fuzzer", "Pathop"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400228 {"webp_encoder", "WEBPEncoder"}
229};
230
Kevin Lubick6c560552018-06-20 09:29:23 -0400231// maps clusterfuzz/oss-fuzz -> Skia's name
Kevin Lubickfffa6412018-04-23 16:44:55 -0400232static std::map<std::string, std::string> cf_map = {
233 {"animated_image_decode", "animated_image_decode"},
234 {"image_decode", "image_decode"},
235 {"image_filter_deserialize", "filter_fuzz"},
236 {"image_filter_deserialize_width", "filter_fuzz"},
237 {"path_deserialize", "path_deserialize"},
238 {"region_deserialize", "region_deserialize"},
239 {"region_set_path", "region_set_path"},
Kevin Lubick6c560552018-06-20 09:29:23 -0400240 {"skjson", "json"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400241 {"textblob_deserialize", "textblob"}
242};
243
244static SkString try_auto_detect(SkString path, SkString* name) {
245 std::cmatch m;
246 std::regex clusterfuzz("clusterfuzz-testcase(-minimized)?-([a-z0-9_]+)-[\\d]+");
247 std::regex skiafuzzer("(api-)?(\\w+)-[a-f0-9]+");
248
249 if (std::regex_search(path.c_str(), m, clusterfuzz)) {
250 std::string type = m.str(2);
Kevin Lubick6c560552018-06-20 09:29:23 -0400251
252 if (cf_api_map.find(type) != cf_api_map.end()) {
253 *name = SkString(cf_api_map[type].c_str());
254 return SkString("api");
Kevin Lubickfffa6412018-04-23 16:44:55 -0400255 } else {
256 if (cf_map.find(type) != cf_map.end()) {
257 return SkString(cf_map[type].c_str());
258 }
259 }
260 } else if (std::regex_search(path.c_str(), m, skiafuzzer)) {
261 std::string a1 = m.str(1);
262 std::string typeOrName = m.str(2);
263 if (a1.length() > 0) {
264 // it's an api fuzzer
265 *name = SkString(typeOrName.c_str());
266 return SkString("api");
267 } else {
268 return SkString(typeOrName.c_str());
269 }
270 }
271
272 return SkString("");
273}
274
Florin Malita80452be2018-06-19 11:27:20 -0400275void FuzzJSON(sk_sp<SkData> bytes);
Florin Malita7796f002018-06-08 12:25:38 -0400276
Florin Malita80452be2018-06-19 11:27:20 -0400277static void fuzz_json(sk_sp<SkData> bytes){
278 FuzzJSON(bytes);
Florin Malita7796f002018-06-08 12:25:38 -0400279 SkDebugf("[terminated] Done parsing!\n");
280}
Florin Malita7796f002018-06-08 12:25:38 -0400281
Florin Malita0b0d93d2018-05-04 15:01:03 -0400282#if defined(SK_ENABLE_SKOTTIE)
Kevin Lubick9eeede22018-05-03 16:26:10 -0400283void FuzzSkottieJSON(sk_sp<SkData> bytes);
284
285static void fuzz_skottie_json(sk_sp<SkData> bytes){
286 FuzzSkottieJSON(bytes);
287 SkDebugf("[terminated] Done animating!\n");
288}
Florin Malita0b0d93d2018-05-04 15:01:03 -0400289#endif
Kevin Lubick9eeede22018-05-03 16:26:10 -0400290
kjlubick2a42f482016-02-16 16:14:23 -0800291// This adds up the first 1024 bytes and returns it as an 8 bit integer. This allows afl-fuzz to
292// deterministically excercise different paths, or *options* (such as different scaling sizes or
293// different image modes) without needing to introduce a parameter. This way we don't need a
294// image_scale1, image_scale2, image_scale4, etc fuzzer, we can just have a image_scale fuzzer.
295// Clients are expected to transform this number into a different range, e.g. with modulo (%).
296static uint8_t calculate_option(SkData* bytes) {
297 uint8_t total = 0;
298 const uint8_t* data = bytes->bytes();
299 for (size_t i = 0; i < 1024 && i < bytes->size(); i++) {
300 total += data[i];
301 }
302 return total;
303}
304
Kevin Lubickfffa6412018-04-23 16:44:55 -0400305static void print_api_names(){
306 SkDebugf("When using --type api, please choose an API to fuzz with --name/-n:\n");
Mike Reedab273fa2017-01-11 13:58:55 -0500307 for (auto r = sk_tools::Registry<Fuzzable>::Head(); r; r = r->next()) {
mtklein65e58242016-01-13 12:57:57 -0800308 auto fuzzable = r->factory();
Kevin Lubickfffa6412018-04-23 16:44:55 -0400309 SkDebugf("\t%s\n", fuzzable.name);
310 }
311}
312
313static void fuzz_api(sk_sp<SkData> bytes, SkString name) {
314 for (auto r = sk_tools::Registry<Fuzzable>::Head(); r; r = r->next()) {
315 auto fuzzable = r->factory();
316 if (name.equals(fuzzable.name)) {
mtkleinf5e97822016-01-15 06:19:53 -0800317 SkDebugf("Fuzzing %s...\n", fuzzable.name);
Kevin Lubick1ac8fd22017-03-01 10:42:45 -0500318 Fuzz fuzz(std::move(bytes));
mtklein65e58242016-01-13 12:57:57 -0800319 fuzzable.fn(&fuzz);
kjlubick47d158e2016-02-01 08:23:50 -0800320 SkDebugf("[terminated] Success!\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500321 return;
mtklein65e58242016-01-13 12:57:57 -0800322 }
323 }
mtkleind4387ea2016-01-21 06:13:52 -0800324
Kevin Lubickfffa6412018-04-23 16:44:55 -0400325 print_api_names();
kjlubickdba57342016-01-21 05:03:28 -0800326}
327
328static void dump_png(SkBitmap bitmap) {
329 if (!FLAGS_dump.isEmpty()) {
Hal Canarydb683012016-11-23 08:55:18 -0700330 sk_tool_utils::EncodeImageToFile(FLAGS_dump[0], bitmap, SkEncodedImageFormat::kPNG, 100);
kjlubickdba57342016-01-21 05:03:28 -0800331 SkDebugf("Dumped to %s\n", FLAGS_dump[0]);
332 }
333}
334
Kevin Lubick2416f962018-02-12 08:26:39 -0500335void FuzzAnimatedImage(sk_sp<SkData> bytes);
336
337static void fuzz_animated_img(sk_sp<SkData> bytes) {
338 FuzzAnimatedImage(bytes);
339 SkDebugf("[terminated] Didn't crash while decoding/drawing animated image!\n");
340}
341
342void FuzzImage(sk_sp<SkData> bytes);
343
344static void fuzz_img2(sk_sp<SkData> bytes) {
345 FuzzImage(bytes);
346 SkDebugf("[terminated] Didn't crash while decoding/drawing image!\n");
347}
348
Kevin Lubickf80f1152017-01-06 08:26:56 -0500349static void fuzz_img(sk_sp<SkData> bytes, uint8_t scale, uint8_t mode) {
kjlubick2a42f482016-02-16 16:14:23 -0800350 // We can scale 1x, 2x, 4x, 8x, 16x
351 scale = scale % 5;
kjlubick5bd98a22016-02-18 06:27:38 -0800352 float fscale = (float)pow(2.0f, scale);
353 SkDebugf("Scaling factor: %f\n", fscale);
kjlubick2a42f482016-02-16 16:14:23 -0800354
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500355 // We have 5 different modes of decoding.
356 mode = mode % 5;
kjlubick2a42f482016-02-16 16:14:23 -0800357 SkDebugf("Mode: %d\n", mode);
358
359 // This is mostly copied from DMSrcSink's CodecSrc::draw method.
kjlubick47d158e2016-02-01 08:23:50 -0800360 SkDebugf("Decoding\n");
Mike Reedede7bac2017-07-23 15:30:02 -0400361 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(bytes));
kjlubickdba57342016-01-21 05:03:28 -0800362 if (nullptr == codec.get()) {
kjlubick47d158e2016-02-01 08:23:50 -0800363 SkDebugf("[terminated] Couldn't create codec.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500364 return;
kjlubickdba57342016-01-21 05:03:28 -0800365 }
366
367 SkImageInfo decodeInfo = codec->getInfo();
kjlubick2a42f482016-02-16 16:14:23 -0800368 SkISize size = codec->getScaledDimensions(fscale);
369 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
370
kjlubickdba57342016-01-21 05:03:28 -0800371 SkBitmap bitmap;
kjlubickdba57342016-01-21 05:03:28 -0800372 SkCodec::Options options;
373 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
374
Mike Reed086a4272017-07-18 10:53:11 -0400375 if (!bitmap.tryAllocPixelsFlags(decodeInfo, SkBitmap::kZeroPixels_AllocFlag)) {
kjlubick47d158e2016-02-01 08:23:50 -0800376 SkDebugf("[terminated] Could not allocate memory. Image might be too large (%d x %d)",
kjlubick2a42f482016-02-16 16:14:23 -0800377 decodeInfo.width(), decodeInfo.height());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500378 return;
kjlubickdba57342016-01-21 05:03:28 -0800379 }
380
kjlubick2a42f482016-02-16 16:14:23 -0800381 switch (mode) {
382 case 0: {//kCodecZeroInit_Mode, kCodec_Mode
Leon Scroggins571b30f2017-07-11 17:35:31 +0000383 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options)) {
kjlubick2a42f482016-02-16 16:14:23 -0800384 case SkCodec::kSuccess:
385 SkDebugf("[terminated] Success!\n");
386 break;
387 case SkCodec::kIncompleteInput:
388 SkDebugf("[terminated] Partial Success\n");
389 break;
Leon Scroggins III674a1842017-07-06 12:26:09 -0400390 case SkCodec::kErrorInInput:
391 SkDebugf("[terminated] Partial Success with error\n");
392 break;
kjlubick2a42f482016-02-16 16:14:23 -0800393 case SkCodec::kInvalidConversion:
394 SkDebugf("Incompatible colortype conversion\n");
395 // Crash to allow afl-fuzz to know this was a bug.
396 raise(SIGSEGV);
397 default:
398 SkDebugf("[terminated] Couldn't getPixels.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500399 return;
kjlubick2a42f482016-02-16 16:14:23 -0800400 }
401 break;
402 }
403 case 1: {//kScanline_Mode
Leon Scroggins571b30f2017-07-11 17:35:31 +0000404 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) {
405 SkDebugf("[terminated] Could not start scanline decoder\n");
406 return;
407 }
kjlubick2a42f482016-02-16 16:14:23 -0800408
409 void* dst = bitmap.getAddr(0, 0);
410 size_t rowBytes = bitmap.rowBytes();
411 uint32_t height = decodeInfo.height();
Brian Osmanea176c62018-04-06 15:28:23 -0400412 // We do not need to check the return value. On an incomplete
413 // image, memory will be filled with a default value.
414 codec->getScanlines(dst, height, rowBytes);
kjlubick47d158e2016-02-01 08:23:50 -0800415 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800416 break;
kjlubick2a42f482016-02-16 16:14:23 -0800417 }
418 case 2: { //kStripe_Mode
419 const int height = decodeInfo.height();
420 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
421 // does not align with image blocks.
422 const int stripeHeight = 37;
423 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
424
425 // Decode odd stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000426 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)
kjlubick2a42f482016-02-16 16:14:23 -0800427 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
428 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
429 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
430 // to run this test for image types that do not have this scanline ordering.
431 SkDebugf("[terminated] Could not start top-down scanline decoder\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500432 return;
kjlubick2a42f482016-02-16 16:14:23 -0800433 }
434
435 for (int i = 0; i < numStripes; i += 2) {
436 // Skip a stripe
437 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
438 codec->skipScanlines(linesToSkip);
439
440 // Read a stripe
441 const int startY = (i + 1) * stripeHeight;
442 const int linesToRead = SkTMin(stripeHeight, height - startY);
443 if (linesToRead > 0) {
444 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
445 }
446 }
447
448 // Decode even stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000449 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo);
kjlubick2a42f482016-02-16 16:14:23 -0800450 if (SkCodec::kSuccess != startResult) {
451 SkDebugf("[terminated] Failed to restart scanline decoder with same parameters.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500452 return;
kjlubick2a42f482016-02-16 16:14:23 -0800453 }
454 for (int i = 0; i < numStripes; i += 2) {
455 // Read a stripe
456 const int startY = i * stripeHeight;
457 const int linesToRead = SkTMin(stripeHeight, height - startY);
458 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
459
460 // Skip a stripe
461 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
462 if (linesToSkip > 0) {
463 codec->skipScanlines(linesToSkip);
464 }
465 }
466 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800467 break;
kjlubick2a42f482016-02-16 16:14:23 -0800468 }
469 case 3: { //kSubset_Mode
470 // Arbitrarily choose a divisor.
471 int divisor = 2;
472 // Total width/height of the image.
473 const int W = codec->getInfo().width();
474 const int H = codec->getInfo().height();
475 if (divisor > W || divisor > H) {
476 SkDebugf("[terminated] Cannot codec subset: divisor %d is too big "
477 "with dimensions (%d x %d)\n", divisor, W, H);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500478 return;
kjlubick2a42f482016-02-16 16:14:23 -0800479 }
480 // subset dimensions
481 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
482 const int w = SkAlign2(W / divisor);
483 const int h = SkAlign2(H / divisor);
484 SkIRect subset;
485 SkCodec::Options opts;
486 opts.fSubset = &subset;
487 SkBitmap subsetBm;
488 // We will reuse pixel memory from bitmap.
489 void* pixels = bitmap.getPixels();
490 // Keep track of left and top (for drawing subsetBm into canvas). We could use
491 // fscale * x and fscale * y, but we want integers such that the next subset will start
492 // where the last one ended. So we'll add decodeInfo.width() and height().
493 int left = 0;
494 for (int x = 0; x < W; x += w) {
495 int top = 0;
496 for (int y = 0; y < H; y+= h) {
497 // Do not make the subset go off the edge of the image.
498 const int preScaleW = SkTMin(w, W - x);
499 const int preScaleH = SkTMin(h, H - y);
500 subset.setXYWH(x, y, preScaleW, preScaleH);
501 // And fscale
502 // FIXME: Should we have a version of getScaledDimensions that takes a subset
503 // into account?
504 decodeInfo = decodeInfo.makeWH(
505 SkTMax(1, SkScalarRoundToInt(preScaleW * fscale)),
506 SkTMax(1, SkScalarRoundToInt(preScaleH * fscale)));
507 size_t rowBytes = decodeInfo.minRowBytes();
Leon Scroggins571b30f2017-07-11 17:35:31 +0000508 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes)) {
kjlubick2a42f482016-02-16 16:14:23 -0800509 SkDebugf("[terminated] Could not install pixels.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500510 return;
kjlubick2a42f482016-02-16 16:14:23 -0800511 }
512 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000513 &opts);
kjlubick2a42f482016-02-16 16:14:23 -0800514 switch (result) {
515 case SkCodec::kSuccess:
516 case SkCodec::kIncompleteInput:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400517 case SkCodec::kErrorInInput:
kjlubick2a42f482016-02-16 16:14:23 -0800518 SkDebugf("okay\n");
519 break;
520 case SkCodec::kInvalidConversion:
521 if (0 == (x|y)) {
522 // First subset is okay to return unimplemented.
523 SkDebugf("[terminated] Incompatible colortype conversion\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500524 return;
kjlubick2a42f482016-02-16 16:14:23 -0800525 }
526 // If the first subset succeeded, a later one should not fail.
527 // fall through to failure
528 case SkCodec::kUnimplemented:
529 if (0 == (x|y)) {
530 // First subset is okay to return unimplemented.
531 SkDebugf("[terminated] subset codec not supported\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500532 return;
kjlubick2a42f482016-02-16 16:14:23 -0800533 }
534 // If the first subset succeeded, why would a later one fail?
535 // fall through to failure
536 default:
537 SkDebugf("[terminated] subset codec failed to decode (%d, %d, %d, %d) "
538 "with dimensions (%d x %d)\t error %d\n",
539 x, y, decodeInfo.width(), decodeInfo.height(),
540 W, H, result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500541 return;
kjlubick2a42f482016-02-16 16:14:23 -0800542 }
543 // translate by the scaled height.
544 top += decodeInfo.height();
545 }
546 // translate by the scaled width.
547 left += decodeInfo.width();
548 }
549 SkDebugf("[terminated] Success!\n");
550 break;
551 }
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500552 case 4: { //kAnimated_Mode
553 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
554 if (frameInfos.size() == 0) {
555 SkDebugf("[terminated] Not an animated image\n");
556 break;
557 }
558
559 for (size_t i = 0; i < frameInfos.size(); i++) {
560 options.fFrameIndex = i;
561 auto result = codec->startIncrementalDecode(decodeInfo, bitmap.getPixels(),
562 bitmap.rowBytes(), &options);
563 if (SkCodec::kSuccess != result) {
564 SkDebugf("[terminated] failed to start incremental decode "
565 "in frame %d with error %d\n", i, result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500566 return;
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500567 }
568
569 result = codec->incrementalDecode();
Leon Scroggins III674a1842017-07-06 12:26:09 -0400570 if (result == SkCodec::kIncompleteInput || result == SkCodec::kErrorInInput) {
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500571 SkDebugf("okay\n");
572 // Frames beyond this one will not decode.
573 break;
574 }
575 if (result == SkCodec::kSuccess) {
576 SkDebugf("okay - decoded frame %d\n", i);
577 } else {
578 SkDebugf("[terminated] incremental decode failed with "
579 "error %d\n", result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500580 return;
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500581 }
582 }
583 SkDebugf("[terminated] Success!\n");
584 break;
585 }
kjlubickdba57342016-01-21 05:03:28 -0800586 default:
kjlubick2a42f482016-02-16 16:14:23 -0800587 SkDebugf("[terminated] Mode not implemented yet\n");
kjlubickdba57342016-01-21 05:03:28 -0800588 }
589
590 dump_png(bitmap);
mtklein65e58242016-01-13 12:57:57 -0800591}
592
Kevin Lubickf80f1152017-01-06 08:26:56 -0500593static void fuzz_skp(sk_sp<SkData> bytes) {
Kevin Lubick09757b22017-12-12 12:52:39 -0500594 SkReadBuffer buf(bytes->data(), bytes->size());
kjlubickdba57342016-01-21 05:03:28 -0800595 SkDebugf("Decoding\n");
Cary Clarkefd99cc2018-06-11 16:25:43 -0400596 sk_sp<SkPicture> pic(SkPicturePriv::MakeFromBuffer(buf));
kjlubickdba57342016-01-21 05:03:28 -0800597 if (!pic) {
kjlubick47d158e2016-02-01 08:23:50 -0800598 SkDebugf("[terminated] Couldn't decode as a picture.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500599 return;
kjlubickdba57342016-01-21 05:03:28 -0800600 }
601 SkDebugf("Rendering\n");
602 SkBitmap bitmap;
603 if (!FLAGS_dump.isEmpty()) {
604 SkIRect size = pic->cullRect().roundOut();
605 bitmap.allocN32Pixels(size.width(), size.height());
606 }
607 SkCanvas canvas(bitmap);
608 canvas.drawPicture(pic);
kjlubick47d158e2016-02-01 08:23:50 -0800609 SkDebugf("[terminated] Success! Decoded and rendered an SkPicture!\n");
kjlubickdba57342016-01-21 05:03:28 -0800610 dump_png(bitmap);
kjlubickdba57342016-01-21 05:03:28 -0800611}
mtklein65e58242016-01-13 12:57:57 -0800612
Kevin Lubick0d825662018-01-03 14:38:48 -0500613static void fuzz_skpipe(sk_sp<SkData> bytes) {
614 SkPipeDeserializer d;
615 SkDebugf("Decoding\n");
616 sk_sp<SkPicture> pic(d.readPicture(bytes.get()));
617 if (!pic) {
618 SkDebugf("[terminated] Couldn't decode picture via SkPipe.\n");
619 return;
620 }
621 SkDebugf("Rendering\n");
622 SkBitmap bitmap;
623 SkCanvas canvas(bitmap);
624 canvas.drawPicture(pic);
625 SkDebugf("[terminated] Success! Decoded and rendered an SkPicture from SkPipe!\n");
626}
627
Kevin Lubickf80f1152017-01-06 08:26:56 -0500628static void fuzz_color_deserialize(sk_sp<SkData> bytes) {
kjlubick3e3c1a52016-06-23 10:49:27 -0700629 sk_sp<SkColorSpace> space(SkColorSpace::Deserialize(bytes->data(), bytes->size()));
630 if (!space) {
631 SkDebugf("[terminated] Couldn't deserialize Colorspace.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500632 return;
kjlubick3e3c1a52016-06-23 10:49:27 -0700633 }
634 SkDebugf("[terminated] Success! deserialized Colorspace.\n");
kjlubick3e3c1a52016-06-23 10:49:27 -0700635}
636
Kevin Lubickf034d112018-02-08 14:31:24 -0500637void FuzzPathDeserialize(SkReadBuffer& buf);
Kevin Lubickc9d28292017-11-09 08:12:56 -0500638
Kevin Lubickf034d112018-02-08 14:31:24 -0500639static void fuzz_path_deserialize(sk_sp<SkData> bytes) {
640 SkReadBuffer buf(bytes->data(), bytes->size());
641 FuzzPathDeserialize(buf);
642 SkDebugf("[terminated] path_deserialize didn't crash!\n");
Kevin Lubickf04c50a2017-01-06 13:48:19 -0500643}
644
Kevin Lubick2541edf2018-01-11 10:27:14 -0500645bool FuzzRegionDeserialize(sk_sp<SkData> bytes);
646
Kevin Lubickedee1ae2017-02-20 17:47:18 -0500647static void fuzz_region_deserialize(sk_sp<SkData> bytes) {
Kevin Lubick2541edf2018-01-11 10:27:14 -0500648 if (!FuzzRegionDeserialize(bytes)) {
Kevin Lubickedee1ae2017-02-20 17:47:18 -0500649 SkDebugf("[terminated] Couldn't initialize SkRegion.\n");
650 return;
651 }
Kevin Lubickedee1ae2017-02-20 17:47:18 -0500652 SkDebugf("[terminated] Success! Initialized SkRegion.\n");
653}
654
Kevin Lubickf034d112018-02-08 14:31:24 -0500655void FuzzTextBlobDeserialize(SkReadBuffer& buf);
656
Kevin Lubickd46e85f2017-11-21 17:13:35 -0500657static void fuzz_textblob_deserialize(sk_sp<SkData> bytes) {
Mike Reedfadbfcd2017-12-06 16:09:20 -0500658 SkReadBuffer buf(bytes->data(), bytes->size());
Kevin Lubickf034d112018-02-08 14:31:24 -0500659 FuzzTextBlobDeserialize(buf);
660 SkDebugf("[terminated] textblob didn't crash!\n");
Kevin Lubickd46e85f2017-11-21 17:13:35 -0500661}
662
Kevin Lubick2541edf2018-01-11 10:27:14 -0500663void FuzzRegionSetPath(Fuzz* fuzz);
664
665static void fuzz_region_set_path(sk_sp<SkData> bytes) {
666 Fuzz fuzz(bytes);
667 FuzzRegionSetPath(&fuzz);
668 SkDebugf("[terminated] region_set_path didn't crash!\n");
669}
670
Kevin Lubickf034d112018-02-08 14:31:24 -0500671void FuzzImageFilterDeserialize(sk_sp<SkData> bytes);
672
Herb Derbya839fc02017-03-16 12:30:43 -0400673static void fuzz_filter_fuzz(sk_sp<SkData> bytes) {
Kevin Lubickf034d112018-02-08 14:31:24 -0500674 FuzzImageFilterDeserialize(bytes);
675 SkDebugf("[terminated] filter_fuzz didn't crash!\n");
Herb Derbya839fc02017-03-16 12:30:43 -0400676}
677
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500678#if SK_SUPPORT_GPU
Kevin Lubickf80f1152017-01-06 08:26:56 -0500679static void fuzz_sksl2glsl(sk_sp<SkData> bytes) {
kjlubicke7195772016-10-18 10:06:24 -0700680 SkSL::Compiler compiler;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400681 SkSL::String output;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500682 SkSL::Program::Settings settings;
683 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
684 settings.fCaps = caps.get();
685 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
Brian Osman93ba0a42017-08-14 14:48:10 -0400686 SkSL::String((const char*) bytes->data()),
687 settings);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500688 if (!program || !compiler.toGLSL(*program, &output)) {
kjlubicke7195772016-10-18 10:06:24 -0700689 SkDebugf("[terminated] Couldn't compile input.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500690 return;
kjlubicke7195772016-10-18 10:06:24 -0700691 }
692 SkDebugf("[terminated] Success! Compiled input.\n");
kjlubicke7195772016-10-18 10:06:24 -0700693}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500694#endif